blob: 4fee723f094e32674d58e86c41dbb4bc4c0771ea [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
20
Dan Stoza9e56aa02015-11-02 13:00:03 -080021//#define LOG_NDEBUG 0
22#undef LOG_TAG
23#define LOG_TAG "Layer"
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080024#define ATRACE_TAG ATRACE_TAG_GRAPHICS
25
Alec Mourie60041e2019-06-14 18:59:51 -070026#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027
[1;3C2b9fc252021-02-04 16:16:50 -080028#include <android-base/properties.h>
Yiwei Zhang5434a782018-12-05 18:06:32 -080029#include <android-base/stringprintf.h>
Steven Thomas62a4cf82020-01-31 12:04:03 -080030#include <android/native_window.h>
Vishnu Nair0f085c62019-08-30 08:49:12 -070031#include <binder/IPCThreadState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080032#include <compositionengine/Display.h>
Lloyd Piquea83776c2019-01-29 18:42:32 -080033#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080034#include <compositionengine/OutputLayer.h>
35#include <compositionengine/impl/OutputLayerCompositionState.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070036#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070037#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070038#include <cutils/properties.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080039#include <gui/BufferItem.h>
40#include <gui/LayerDebugInfo.h>
41#include <gui/Surface.h>
Alec Mourie60041e2019-06-14 18:59:51 -070042#include <math.h>
chaviw250bcbb2020-08-05 11:17:54 -070043#include <private/android_filesystem_config.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080044#include <renderengine/RenderEngine.h>
Alec Mourie60041e2019-06-14 18:59:51 -070045#include <stdint.h>
46#include <stdlib.h>
47#include <sys/types.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080048#include <ui/DebugUtils.h>
49#include <ui/GraphicBuffer.h>
50#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051#include <utils/Errors.h>
52#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080053#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080055#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056
Alec Mourie60041e2019-06-14 18:59:51 -070057#include <algorithm>
58#include <mutex>
59#include <sstream>
60
Yiwei Zhang60d1a192018-03-07 14:52:28 -080061#include "BufferLayer.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020062#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070063#include "DisplayDevice.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080064#include "DisplayHardware/HWComposer.h"
Vishnu Nairfa247b12020-02-11 08:58:26 -080065#include "EffectLayer.h"
Ady Abraham22c7b5c2020-09-22 19:33:40 -070066#include "FrameTimeline.h"
Mikael Pessa90092f42019-08-26 17:22:04 -070067#include "FrameTracer/FrameTracer.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080068#include "LayerProtoHelper.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070069#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070070#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071#include "SurfaceFlinger.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080072#include "TimeStats/TimeStats.h"
Michael Wright44753b12020-07-08 13:48:11 +010073#include "input/InputWindow.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070074
David Sodman41fdfc92017-11-06 16:09:56 -080075#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080076
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077namespace android {
Marin Shalamanov1876e2e2020-12-04 13:23:59 +010078namespace {
79constexpr int kDumpTableRowLength = 159;
80} // namespace
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081
Yiwei Zhang5434a782018-12-05 18:06:32 -080082using base::StringAppendF;
Michael Wright44753b12020-07-08 13:48:11 +010083using namespace android::flag_operators;
Ady Abraham22c7b5c2020-09-22 19:33:40 -070084using PresentState = frametimeline::SurfaceFrame::PresentState;
Yiwei Zhang5434a782018-12-05 18:06:32 -080085
Lloyd Piquef1c675b2018-09-12 20:45:39 -070086std::atomic<int32_t> Layer::sSequence{1};
Mathias Agopian13127d82013-03-05 17:47:11 -080087
Lloyd Pique42ab75e2018-09-12 20:46:03 -070088Layer::Layer(const LayerCreationArgs& args)
Ady Abraham8f1ee7f2019-04-05 10:32:50 -070089 : mFlinger(args.flinger),
90 mName(args.name),
91 mClientRef(args.client),
Michael Wright44753b12020-07-08 13:48:11 +010092 mWindowType(static_cast<InputWindowInfo::Type>(
93 args.metadata.getInt32(METADATA_WINDOW_TYPE, 0))) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -070094 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -070095 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
96 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
97 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
chaviwc5676c62020-09-18 15:01:04 -070098 if (args.flags & ISurfaceComposerClient::eSkipScreenshot)
99 layerFlags |= layer_state_t::eLayerSkipScreenshot;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700100
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800101 mCurrentState.active_legacy.w = args.w;
102 mCurrentState.active_legacy.h = args.h;
103 mCurrentState.flags = layerFlags;
104 mCurrentState.active_legacy.transform.set(0, 0);
chaviw25714502021-02-11 10:01:08 -0800105 mCurrentState.crop.makeInvalid();
106 mCurrentState.requestedCrop = mCurrentState.crop;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800107 mCurrentState.z = 0;
108 mCurrentState.color.a = 1.0f;
109 mCurrentState.layerStack = 0;
110 mCurrentState.sequence = 0;
111 mCurrentState.requested_legacy = mCurrentState.active_legacy;
chaviw766c9c52021-02-10 17:36:47 -0800112 mCurrentState.width = UINT32_MAX;
113 mCurrentState.height = UINT32_MAX;
114 mCurrentState.transform.set(0, 0);
Valerie Hau134651a2020-01-28 16:21:22 -0800115 mCurrentState.frameNumber = 0;
chaviw766c9c52021-02-10 17:36:47 -0800116 mCurrentState.bufferTransform = 0;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800117 mCurrentState.transformToDisplayInverse = false;
118 mCurrentState.crop.makeInvalid();
Ady Abraham6c1b7ac2021-03-31 16:56:03 -0700119 mCurrentState.acquireFence = sp<Fence>::make(-1);
120 mCurrentState.acquireFenceTime = std::make_shared<FenceTime>(mCurrentState.acquireFence);
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800121 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
122 mCurrentState.hdrMetadata.validTypes = 0;
Marissa Wall7e0a01f2019-08-14 14:29:25 -0700123 mCurrentState.surfaceDamageRegion = Region::INVALID_REGION;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800124 mCurrentState.cornerRadius = 0.0f;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800125 mCurrentState.backgroundBlurRadius = 0;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800126 mCurrentState.api = -1;
127 mCurrentState.hasColorTransform = false;
Peiyong Linc502cb72019-03-01 15:00:23 -0800128 mCurrentState.colorSpaceAgnostic = false;
Ana Krulecc84d09b2019-11-02 23:10:29 +0100129 mCurrentState.frameRateSelectionPriority = PRIORITY_UNSET;
Evan Roskya1f1e152019-01-24 16:17:46 -0800130 mCurrentState.metadata = args.metadata;
Vishnu Nairf37dcfb2019-11-16 07:37:09 -0800131 mCurrentState.shadowRadius = 0.f;
Vishnu Nair6213bd92020-05-08 17:42:25 -0700132 mCurrentState.fixedTransformHint = ui::Transform::ROT_INVALID;
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000133 mCurrentState.frameTimelineInfo = {};
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700134 mCurrentState.postTime = -1;
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700135 mCurrentState.destinationFrame.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700136
Vishnu Nairc43a23c2020-05-29 14:32:27 -0700137 if (args.flags & ISurfaceComposerClient::eNoColorFill) {
138 // Set an invalid color so there is no color fill.
139 mCurrentState.color.r = -1.0_hf;
140 mCurrentState.color.g = -1.0_hf;
141 mCurrentState.color.b = -1.0_hf;
142 }
143
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700144 // drawing state & current state are identical
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800145 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700146
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800147 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700148 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800149 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200150 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700151
Vishnu Nair0f085c62019-08-30 08:49:12 -0700152 mCallingPid = args.callingPid;
153 mCallingUid = args.callingUid;
chaviw250bcbb2020-08-05 11:17:54 -0700154
155 if (mCallingUid == AID_GRAPHICS || mCallingUid == AID_SYSTEM) {
156 // If the system didn't send an ownerUid, use the callingUid for the ownerUid.
157 mOwnerUid = args.metadata.getInt32(METADATA_OWNER_UID, mCallingUid);
Adithya Srinivasan9febda82020-10-19 10:49:41 -0700158 mOwnerPid = args.metadata.getInt32(METADATA_OWNER_PID, mCallingPid);
chaviw250bcbb2020-08-05 11:17:54 -0700159 } else {
160 // A create layer request from a non system request cannot specify the owner uid
161 mOwnerUid = mCallingUid;
Adithya Srinivasan9febda82020-10-19 10:49:41 -0700162 mOwnerPid = mCallingPid;
chaviw250bcbb2020-08-05 11:17:54 -0700163 }
Dominik Laskowski75848362019-11-11 17:57:20 -0800164}
165
166void Layer::onFirstRef() {
167 mFlinger->onLayerFirstRef(this);
Dan Stoza436ccf32018-06-21 12:10:12 -0700168}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700169
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700170Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800171 sp<Client> c(mClientRef.promote());
172 if (c != 0) {
173 c->detachLayer(this);
174 }
175
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000176 mFrameTracker.logAndResetStats(mName);
chaviw74d90ad2019-04-26 14:45:26 -0700177 mFlinger->onLayerDestroyed(this);
Mathias Agopian96f08192010-06-02 23:28:45 -0700178}
179
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700180LayerCreationArgs::LayerCreationArgs(SurfaceFlinger* flinger, sp<Client> client, std::string name,
181 uint32_t w, uint32_t h, uint32_t flags, LayerMetadata metadata)
Vishnu Nair0f085c62019-08-30 08:49:12 -0700182 : flinger(flinger),
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700183 client(std::move(client)),
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700184 name(std::move(name)),
Vishnu Nair0f085c62019-08-30 08:49:12 -0700185 w(w),
186 h(h),
187 flags(flags),
188 metadata(std::move(metadata)) {
189 IPCThreadState* ipc = IPCThreadState::self();
190 callingPid = ipc->getCallingPid();
191 callingUid = ipc->getCallingUid();
192}
193
Mathias Agopian13127d82013-03-05 17:47:11 -0800194// ---------------------------------------------------------------------------
195// callbacks
196// ---------------------------------------------------------------------------
197
David Sodmaneb085e02017-10-05 18:49:04 -0700198/*
199 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
200 * Layer. So, the implementation is done in BufferLayer. When called on a
Vishnu Nairfa247b12020-02-11 08:58:26 -0800201 * EffectLayer object, it's essentially a NOP.
David Sodmaneb085e02017-10-05 18:49:04 -0700202 */
David Sodmaneb085e02017-10-05 18:49:04 -0700203void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800204
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700205void Layer::removeRelativeZ(const std::vector<Layer*>& layersInTree) {
206 if (mCurrentState.zOrderRelativeOf == nullptr) {
207 return;
208 }
Robert Carr2e102c92018-10-23 12:11:15 -0700209
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700210 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
211 if (strongRelative == nullptr) {
212 setZOrderRelativeOf(nullptr);
213 return;
214 }
215
216 if (!std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
217 strongRelative->removeZOrderRelative(this);
218 mFlinger->setTransactionFlags(eTraversalNeeded);
chaviw606e5cf2019-03-01 10:12:10 -0800219 setZOrderRelativeOf(nullptr);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700220 }
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700221}
222
223void Layer::removeFromCurrentState() {
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 Nairc97b8db2019-10-29 18:19:35 -0700318void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform,
319 float parentShadowRadius) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800320 const State& s(getDrawingState());
321
322 // Calculate effective layer transform
323 mEffectiveTransform = parentTransform * getActiveTransform(s);
324
325 // Transform parent bounds to layer space
326 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
327
Vishnu Nairc652ff82019-03-15 12:48:54 -0700328 // Calculate source bounds
Vishnu Nair4351ad52019-02-11 14:13:02 -0800329 mSourceBounds = computeSourceBounds(parentBounds);
330
331 // Calculate bounds by croping diplay frame with layer crop and parent bounds
332 FloatRect bounds = mSourceBounds;
333 const Rect layerCrop = getCrop(s);
334 if (!layerCrop.isEmpty()) {
335 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
336 }
337 bounds = bounds.intersect(parentBounds);
338
339 mBounds = bounds;
340 mScreenBounds = mEffectiveTransform.transform(mBounds);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700341
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700342 // Use the layer's own shadow radius if set. Otherwise get the radius from
343 // parent.
344 if (s.shadowRadius > 0.f) {
345 mEffectiveShadowRadius = s.shadowRadius;
346 } else {
347 mEffectiveShadowRadius = parentShadowRadius;
348 }
349
350 // Shadow radius is passed down to only one layer so if the layer can draw shadows,
351 // don't pass it to its children.
352 const float childShadowRadius = canDrawShadows() ? 0.f : mEffectiveShadowRadius;
353
Vishnu Nair4351ad52019-02-11 14:13:02 -0800354 for (const sp<Layer>& child : mDrawingChildren) {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700355 child->computeBounds(mBounds, mEffectiveTransform, childShadowRadius);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800356 }
357}
358
Vishnu Nair60356342018-11-13 13:00:45 -0800359Rect Layer::getCroppedBufferSize(const State& s) const {
360 Rect size = getBufferSize(s);
361 Rect crop = getCrop(s);
362 if (!crop.isEmpty() && size.isValid()) {
363 size.intersect(crop, &size);
364 } else if (!crop.isEmpty()) {
365 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700366 }
Vishnu Nair60356342018-11-13 13:00:45 -0800367 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800368}
369
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700370void Layer::setupRoundedCornersCropCoordinates(Rect win,
371 const FloatRect& roundedCornersCrop) const {
372 // Translate win by the rounded corners rect coordinates, to have all values in
373 // layer coordinate space.
374 win.left -= roundedCornersCrop.left;
375 win.right -= roundedCornersCrop.left;
376 win.top -= roundedCornersCrop.top;
377 win.bottom -= roundedCornersCrop.top;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700378}
379
Lloyd Piquede196652020-01-22 17:29:58 -0800380void Layer::prepareBasicGeometryCompositionState() {
Lloyd Piquec6687342019-03-07 21:34:57 -0800381 const auto& drawingState{getDrawingState()};
382 const uint32_t layerStack = getLayerStack();
383 const auto alpha = static_cast<float>(getAlpha());
384 const bool opaque = isOpaque(drawingState);
385 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
386
387 auto blendMode = Hwc2::IComposerClient::BlendMode::NONE;
388 if (!opaque || alpha != 1.0f) {
389 blendMode = mPremultipliedAlpha ? Hwc2::IComposerClient::BlendMode::PREMULTIPLIED
390 : Hwc2::IComposerClient::BlendMode::COVERAGE;
391 }
392
Lloyd Piquede196652020-01-22 17:29:58 -0800393 auto* compositionState = editCompositionState();
394 compositionState->layerStackId =
Lloyd Piquec6687342019-03-07 21:34:57 -0800395 (layerStack != ~0u) ? std::make_optional(layerStack) : std::nullopt;
Lloyd Piquede196652020-01-22 17:29:58 -0800396 compositionState->internalOnly = getPrimaryDisplayOnly();
397 compositionState->isVisible = isVisible();
398 compositionState->isOpaque = opaque && !usesRoundedCorners && alpha == 1.f;
399 compositionState->shadowRadius = mEffectiveShadowRadius;
Lloyd Piquec6687342019-03-07 21:34:57 -0800400
Lloyd Piquede196652020-01-22 17:29:58 -0800401 compositionState->contentDirty = contentDirty;
Lloyd Piquec6687342019-03-07 21:34:57 -0800402 contentDirty = false;
403
Lloyd Piquede196652020-01-22 17:29:58 -0800404 compositionState->geomLayerBounds = mBounds;
405 compositionState->geomLayerTransform = getTransform();
406 compositionState->geomInverseLayerTransform = compositionState->geomLayerTransform.inverse();
407 compositionState->transparentRegionHint = getActiveTransparentRegion(drawingState);
Lloyd Piquec6687342019-03-07 21:34:57 -0800408
Lloyd Piquede196652020-01-22 17:29:58 -0800409 compositionState->blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
410 compositionState->alpha = alpha;
411 compositionState->backgroundBlurRadius = drawingState.backgroundBlurRadius;
Lucas Dupinc3800b82020-10-02 16:24:48 -0700412 compositionState->blurRegions = drawingState.blurRegions;
John Reckc00c6692021-02-16 11:37:33 -0500413 compositionState->stretchEffect = getStretchEffect();
Lloyd Piquec6687342019-03-07 21:34:57 -0800414}
415
Lloyd Piquede196652020-01-22 17:29:58 -0800416void Layer::prepareGeometryCompositionState() {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800417 const auto& drawingState{getDrawingState()};
Mathias Agopian13127d82013-03-05 17:47:11 -0800418
Lloyd Piquea83776c2019-01-29 18:42:32 -0800419 int type = drawingState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
420 int appId = drawingState.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700421 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400422 if (parent.get()) {
423 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800424 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
425 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
Jiwen 'Steve' Cai736c74e2019-05-28 18:33:22 -0700426 if (parentType > 0 && parentAppId > 0) {
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800427 type = parentType;
428 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700429 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400430 }
431
Lloyd Piquede196652020-01-22 17:29:58 -0800432 auto* compositionState = editCompositionState();
David Sodman15094112018-10-11 09:39:37 -0700433
Lloyd Piquede196652020-01-22 17:29:58 -0800434 compositionState->geomBufferSize = getBufferSize(drawingState);
435 compositionState->geomContentCrop = getBufferCrop();
436 compositionState->geomCrop = getCrop(drawingState);
437 compositionState->geomBufferTransform = getBufferTransform();
438 compositionState->geomBufferUsesDisplayInverseTransform = getTransformToDisplayInverse();
439 compositionState->geomUsesSourceCrop = usesSourceCrop();
440 compositionState->isSecure = isSecure();
441
Lloyd Pique8d9f8362020-02-11 19:13:09 -0800442 compositionState->metadata.clear();
443 const auto& supportedMetadata = mFlinger->getHwComposer().getSupportedLayerGenericMetadata();
444 for (const auto& [key, mandatory] : supportedMetadata) {
445 const auto& genericLayerMetadataCompatibilityMap =
446 mFlinger->getGenericLayerMetadataKeyMap();
447 auto compatIter = genericLayerMetadataCompatibilityMap.find(key);
448 if (compatIter == std::end(genericLayerMetadataCompatibilityMap)) {
449 continue;
450 }
451 const uint32_t id = compatIter->second;
452
453 auto it = drawingState.metadata.mMap.find(id);
454 if (it == std::end(drawingState.metadata.mMap)) {
455 continue;
456 }
457
458 compositionState->metadata
459 .emplace(key, compositionengine::GenericLayerMetadataEntry{mandatory, it->second});
460 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800461}
David Sodmanba340492018-08-05 21:51:33 -0700462
Lloyd Piquede196652020-01-22 17:29:58 -0800463void Layer::preparePerFrameCompositionState() {
Lloyd Pique688abd42019-02-15 15:42:24 -0800464 const auto& drawingState{getDrawingState()};
Lloyd Piquede196652020-01-22 17:29:58 -0800465 auto* compositionState = editCompositionState();
Lloyd Piquef5275482019-01-29 18:42:42 -0800466
Lloyd Piquede196652020-01-22 17:29:58 -0800467 compositionState->forceClientComposition = false;
468
469 compositionState->isColorspaceAgnostic = isColorSpaceAgnostic();
470 compositionState->dataspace = getDataSpace();
471 compositionState->colorTransform = getColorTransform();
472 compositionState->colorTransformIsIdentity = !hasColorTransform();
473 compositionState->surfaceDamage = surfaceDamageRegion;
474 compositionState->hasProtectedContent = isProtected();
Lloyd Pique688abd42019-02-15 15:42:24 -0800475
476 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700477
Lloyd Piquede196652020-01-22 17:29:58 -0800478 compositionState->isOpaque =
Lloyd Pique688abd42019-02-15 15:42:24 -0800479 isOpaque(drawingState) && !usesRoundedCorners && getAlpha() == 1.0_hf;
Lloyd Piquef5275482019-01-29 18:42:42 -0800480
481 // Force client composition for special cases known only to the front-end.
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400482 // Rounded corners no longer force client composition, since we may use a
483 // hole punch so that the layer will appear to have rounded corners.
484 if (isHdrY410() || drawShadows() || drawingState.blurRegions.size() > 0 ||
John Reckc00c6692021-02-16 11:37:33 -0500485 compositionState->stretchEffect.hasEffect()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800486 compositionState->forceClientComposition = true;
Lloyd Piquef5275482019-01-29 18:42:42 -0800487 }
488}
489
Lloyd Piquede196652020-01-22 17:29:58 -0800490void Layer::prepareCursorCompositionState() {
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800491 const State& drawingState{getDrawingState()};
Lloyd Piquede196652020-01-22 17:29:58 -0800492 auto* compositionState = editCompositionState();
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800493
494 // Apply the layer's transform, followed by the display's global transform
495 // Here we're guaranteed that the layer's transform preserves rects
496 Rect win = getCroppedBufferSize(drawingState);
497 // Subtract the transparent region and snap to the bounds
498 Rect bounds = reduce(win, getActiveTransparentRegion(drawingState));
499 Rect frame(getTransform().transform(bounds));
500
Lloyd Piquede196652020-01-22 17:29:58 -0800501 compositionState->cursorFrame = frame;
502}
503
504sp<compositionengine::LayerFE> Layer::asLayerFE() const {
505 return const_cast<compositionengine::LayerFE*>(
506 static_cast<const compositionengine::LayerFE*>(this));
507}
508
509sp<compositionengine::LayerFE> Layer::getCompositionEngineLayerFE() const {
510 return nullptr;
511}
512
513compositionengine::LayerFECompositionState* Layer::editCompositionState() {
514 return nullptr;
515}
516
517const compositionengine::LayerFECompositionState* Layer::getCompositionState() const {
518 return nullptr;
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800519}
520
Lloyd Piquef16688f2019-02-19 17:47:57 -0800521bool Layer::onPreComposition(nsecs_t) {
522 return false;
523}
524
Lloyd Piquede196652020-01-22 17:29:58 -0800525void Layer::prepareCompositionState(compositionengine::LayerFE::StateSubset subset) {
Lloyd Piquec6687342019-03-07 21:34:57 -0800526 using StateSubset = compositionengine::LayerFE::StateSubset;
Lloyd Piquef5275482019-01-29 18:42:42 -0800527
Lloyd Piquec6687342019-03-07 21:34:57 -0800528 switch (subset) {
529 case StateSubset::BasicGeometry:
Lloyd Piquede196652020-01-22 17:29:58 -0800530 prepareBasicGeometryCompositionState();
Lloyd Piquec6687342019-03-07 21:34:57 -0800531 break;
532
533 case StateSubset::GeometryAndContent:
Lloyd Piquede196652020-01-22 17:29:58 -0800534 prepareBasicGeometryCompositionState();
535 prepareGeometryCompositionState();
536 preparePerFrameCompositionState();
Lloyd Piquec6687342019-03-07 21:34:57 -0800537 break;
538
539 case StateSubset::Content:
Lloyd Piquede196652020-01-22 17:29:58 -0800540 preparePerFrameCompositionState();
541 break;
542
543 case StateSubset::Cursor:
544 prepareCursorCompositionState();
Lloyd Piquec6687342019-03-07 21:34:57 -0800545 break;
546 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800547}
Mathias Agopian29a367b2011-07-12 14:51:45 -0700548
Lloyd Piquea83776c2019-01-29 18:42:32 -0800549const char* Layer::getDebugName() const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700550 return mName.c_str();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800551}
552
Mathias Agopian13127d82013-03-05 17:47:11 -0800553// ---------------------------------------------------------------------------
554// drawing...
555// ---------------------------------------------------------------------------
556
Vishnu Nair9b079a22020-01-21 14:36:08 -0800557std::optional<compositionengine::LayerFE::LayerSettings> Layer::prepareClientComposition(
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100558 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
Lloyd Piquede196652020-01-22 17:29:58 -0800559 if (!getCompositionState()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800560 return {};
561 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800562
Vishnu Nair4351ad52019-02-11 14:13:02 -0800563 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000564 half alpha = getAlpha();
Vishnu Nair9b079a22020-01-21 14:36:08 -0800565
566 compositionengine::LayerFE::LayerSettings layerSettings;
Lloyd Piquef16688f2019-02-19 17:47:57 -0800567 layerSettings.geometry.boundaries = bounds;
chaviwc6ad8af2020-08-03 11:33:30 -0700568 layerSettings.geometry.positionTransform = getTransform().asMatrix4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000569
570 if (hasColorTransform()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800571 layerSettings.colorTransform = getColorTransform();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000572 }
573
574 const auto roundedCornerState = getRoundedCornerState();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800575 layerSettings.geometry.roundedCornersRadius = roundedCornerState.radius;
576 layerSettings.geometry.roundedCornersCrop = roundedCornerState.cropRect;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000577
Lloyd Piquef16688f2019-02-19 17:47:57 -0800578 layerSettings.alpha = alpha;
chaviw4244e032019-09-04 11:27:49 -0700579 layerSettings.sourceDataspace = getDataSpace();
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100580 if (!targetSettings.disableBlurs) {
581 layerSettings.backgroundBlurRadius = getBackgroundBlurRadius();
582 layerSettings.blurRegions = getBlurRegions();
Derek Sollenberger39feade2021-04-27 16:08:40 -0400583 layerSettings.blurRegionTransform =
584 getActiveTransform(getDrawingState()).inverse().asMatrix4();
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100585 }
Nader Jawad2dfc98b2021-04-08 20:35:39 -0700586 layerSettings.stretchEffect = getStretchEffect();
Ana Krulec6eab17a2020-12-09 15:52:36 -0800587 // Record the name of the layer for debugging further down the stack.
588 layerSettings.name = getName();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800589 return layerSettings;
Mathias Agopian13127d82013-03-05 17:47:11 -0800590}
591
Vishnu Nair9b079a22020-01-21 14:36:08 -0800592std::optional<compositionengine::LayerFE::LayerSettings> Layer::prepareShadowClientComposition(
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200593 const LayerFE::LayerSettings& casterLayerSettings, const Rect& layerStackRect,
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800594 ui::Dataspace outputDataspace) {
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200595 renderengine::ShadowSettings shadow = getShadowSettings(layerStackRect);
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800596 if (shadow.length <= 0.f) {
597 return {};
598 }
599
600 const float casterAlpha = casterLayerSettings.alpha;
601 const bool casterIsOpaque = ((casterLayerSettings.source.buffer.buffer != nullptr) &&
602 casterLayerSettings.source.buffer.isOpaque);
603
Vishnu Nair9b079a22020-01-21 14:36:08 -0800604 compositionengine::LayerFE::LayerSettings shadowLayer = casterLayerSettings;
605
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800606 shadowLayer.shadow = shadow;
Vishnu Naira483b4a2019-12-12 15:07:52 -0800607 shadowLayer.geometry.boundaries = mBounds; // ignore transparent region
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800608
609 // If the casting layer is translucent, we need to fill in the shadow underneath the layer.
610 // Otherwise the generated shadow will only be shown around the casting layer.
611 shadowLayer.shadow.casterIsTranslucent = !casterIsOpaque || (casterAlpha < 1.0f);
612 shadowLayer.shadow.ambientColor *= casterAlpha;
613 shadowLayer.shadow.spotColor *= casterAlpha;
614 shadowLayer.sourceDataspace = outputDataspace;
615 shadowLayer.source.buffer.buffer = nullptr;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800616 shadowLayer.source.buffer.fence = nullptr;
617 shadowLayer.frameNumber = 0;
618 shadowLayer.bufferId = 0;
Alec Mouribd17b3b2020-12-17 11:08:30 -0800619 shadowLayer.name = getName();
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800620
621 if (shadowLayer.shadow.ambientColor.a <= 0.f && shadowLayer.shadow.spotColor.a <= 0.f) {
622 return {};
623 }
624
625 float casterCornerRadius = shadowLayer.geometry.roundedCornersRadius;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800626 const FloatRect& cornerRadiusCropRect = shadowLayer.geometry.roundedCornersCrop;
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800627 const FloatRect& casterRect = shadowLayer.geometry.boundaries;
628
629 // crop used to set the corner radius may be larger than the content rect. Adjust the corner
630 // radius accordingly.
631 if (casterCornerRadius > 0.f) {
632 float cropRectOffset = std::max(std::abs(cornerRadiusCropRect.top - casterRect.top),
633 std::abs(cornerRadiusCropRect.left - casterRect.left));
634 if (cropRectOffset > casterCornerRadius) {
635 casterCornerRadius = 0;
636 } else {
637 casterCornerRadius -= cropRectOffset;
638 }
639 shadowLayer.geometry.roundedCornersRadius = casterCornerRadius;
640 }
641
642 return shadowLayer;
643}
644
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800645void Layer::prepareClearClientComposition(LayerFE::LayerSettings& layerSettings,
646 bool blackout) const {
647 layerSettings.source.buffer.buffer = nullptr;
648 layerSettings.source.solidColor = half3(0.0, 0.0, 0.0);
649 layerSettings.disableBlending = true;
Vishnu Nair3b653e72020-02-24 16:40:50 -0800650 layerSettings.bufferId = 0;
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800651 layerSettings.frameNumber = 0;
652
653 // If layer is blacked out, force alpha to 1 so that we draw a black color layer.
654 layerSettings.alpha = blackout ? 1.0f : 0.0f;
Alec Mouribd17b3b2020-12-17 11:08:30 -0800655 layerSettings.name = getName();
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800656}
657
658std::vector<compositionengine::LayerFE::LayerSettings> Layer::prepareClientCompositionList(
659 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
660 std::optional<compositionengine::LayerFE::LayerSettings> layerSettings =
661 prepareClientComposition(targetSettings);
662 // Nothing to render.
663 if (!layerSettings) {
664 return {};
665 }
666
667 // HWC requests to clear this layer.
668 if (targetSettings.clearContent) {
669 prepareClearClientComposition(*layerSettings, false /* blackout */);
670 return {*layerSettings};
671 }
672
673 std::optional<compositionengine::LayerFE::LayerSettings> shadowSettings =
674 prepareShadowClientComposition(*layerSettings, targetSettings.viewport,
675 targetSettings.dataspace);
676 // There are no shadows to render.
677 if (!shadowSettings) {
678 return {*layerSettings};
679 }
680
681 // If the layer casts a shadow but the content casting the shadow is occluded, skip
682 // composing the non-shadow content and only draw the shadows.
683 if (targetSettings.realContentIsVisible) {
684 return {*shadowSettings, *layerSettings};
685 }
686
687 return {*shadowSettings};
688}
689
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700690Hwc2::IComposerClient::Composition Layer::getCompositionType(const DisplayDevice& display) const {
691 const auto outputLayer = findOutputLayerForDisplay(&display);
Alec Mouri6b9e9912020-01-21 10:50:24 -0800692 if (outputLayer == nullptr) {
693 return Hwc2::IComposerClient::Composition::INVALID;
694 }
695 if (outputLayer->getState().hwc) {
696 return (*outputLayer->getState().hwc).hwcCompositionType;
697 } else {
698 return Hwc2::IComposerClient::Composition::CLIENT;
699 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800700}
701
Mathias Agopian13127d82013-03-05 17:47:11 -0800702// ----------------------------------------------------------------------------
703// local state
704// ----------------------------------------------------------------------------
705
David Sodman41fdfc92017-11-06 16:09:56 -0800706bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800707 const State& s(mDrawingState);
Garfield Tande619fa2020-10-02 17:13:53 -0700708 if (s.flags & layer_state_t::eLayerSecure) {
709 return true;
710 }
711
712 const auto p = mDrawingParent.promote();
713 return (p != nullptr) ? p->isSecure() : false;
Dan Stoza23116082015-06-18 14:58:39 -0700714}
715
Mathias Agopian13127d82013-03-05 17:47:11 -0800716// ----------------------------------------------------------------------------
717// transaction
718// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800719
Marissa Wall61c58622018-07-18 10:12:20 -0700720uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000721 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800722
Marissa Wall61c58622018-07-18 10:12:20 -0700723 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
724 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700725
David Sodmaneb085e02017-10-05 18:49:04 -0700726 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700727 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000728 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800729 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
730 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
731 " requested={ wh={%4u,%4u} }}\n"
732 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
733 " requested={ wh={%4u,%4u} }}\n",
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700734 this, getName().c_str(), getBufferTransform(), getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700735 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
chaviw25714502021-02-11 10:01:08 -0800736 stateToCommit->crop.left, stateToCommit->crop.top, stateToCommit->crop.right,
737 stateToCommit->crop.bottom, stateToCommit->crop.getWidth(),
738 stateToCommit->crop.getHeight(), stateToCommit->requested_legacy.w,
739 stateToCommit->requested_legacy.h, s.active_legacy.w, s.active_legacy.h,
740 s.crop.left, s.crop.top, s.crop.right, s.crop.bottom, s.crop.getWidth(),
741 s.crop.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800742 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700743
Robert Carre392b552017-09-19 12:16:05 -0700744 // Don't let Layer::doTransaction update the drawing state
745 // if we have a pending resize, unless we are in fixed-size mode.
746 // the drawing state will be updated only once we receive a buffer
747 // with the correct size.
748 //
749 // In particular, we want to make sure the clip (which is part
750 // of the geometry state) is latched together with the size but is
751 // latched immediately when no resizing is involved.
752 //
753 // If a sideband stream is attached, however, we want to skip this
754 // optimization so that transactions aren't missed when a buffer
755 // never arrives
756 //
757 // In the case that we don't have a buffer we ignore other factors
758 // and avoid entering the resizePending state. At a high level the
759 // resizePending state is to avoid applying the state of the new buffer
760 // to the old buffer. However in the state where we don't have an old buffer
761 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700762 const bool resizePending =
763 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
764 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
chaviwd62d3062019-09-04 14:48:02 -0700765 (getBuffer() != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700766 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800767 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700768 flags |= eDontUpdateGeometryState;
769 }
770 }
771
Robert Carr7bf247e2017-05-18 14:02:49 -0700772 // Here we apply various requested geometry states, depending on our
773 // latching configuration. See Layer.h for a detailed discussion of
774 // how geometry latching is controlled.
775 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000776 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700777
Robert Carr7bf247e2017-05-18 14:02:49 -0700778 // There is an awkward asymmetry in the handling of the crop states in the position
779 // states, as can be seen below. Largely this arises from position and transform
780 // being stored in the same data structure while having different latching rules.
781 // b/38182305
782 //
Marissa Wall61c58622018-07-18 10:12:20 -0700783 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700784 // applyPendingStates in the presence of deferred transactions.
chaviw214c89d2019-09-04 16:03:53 -0700785 editCurrentState.active_legacy = editCurrentState.requested_legacy;
786 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Mathias Agopian13127d82013-03-05 17:47:11 -0800787 }
788
Marissa Wall61c58622018-07-18 10:12:20 -0700789 return flags;
790}
791
792uint32_t Layer::doTransaction(uint32_t flags) {
793 ATRACE_CALL();
794
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800795 if (mChildrenChanged) {
796 flags |= eVisibleRegion;
797 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -0700798 }
799
Robert Carr0758e5d2021-03-11 22:15:04 -0800800 // TODO: This is unfortunate.
801 mCurrentStateModified = mCurrentState.modified;
802 mCurrentState.modified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700803
Robert Carr0758e5d2021-03-11 22:15:04 -0800804 flags = doTransactionResize(flags, &mCurrentState);
Marissa Wall61c58622018-07-18 10:12:20 -0700805
Alec Mourib416efd2018-09-06 21:01:59 +0000806 const State& s(getDrawingState());
Robert Carr0758e5d2021-03-11 22:15:04 -0800807 State& c(getCurrentState());
Marissa Wall61c58622018-07-18 10:12:20 -0700808
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700809 // Translates dest frame into scale and position updates. This helps align geometry calculations
810 // for BufferStateLayer with other layers. This should ideally happen in the client once client
811 // has the display orientation details from WM.
812 updateGeometry();
813
chaviw4e765532021-04-30 12:11:39 -0500814 if (c.width != s.width || c.height != s.height || !(c.transform == s.transform)) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800815 // invalidate and recompute the visible regions if needed
816 flags |= Layer::eVisibleRegion;
817 }
818
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700819 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800820 // invalidate and recompute the visible regions if needed
821 flags |= eVisibleRegion;
822 this->contentDirty = true;
823
824 // we may use linear filtering, if the matrix scales us
Marin Shalamanov043ba292020-09-10 13:35:20 +0200825 mNeedsFiltering = getActiveTransform(c).needsBilinearFiltering();
Mathias Agopian13127d82013-03-05 17:47:11 -0800826 }
827
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800828 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -0700829 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800830 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -0700831 }
832
chaviw47dbd692020-10-26 17:50:12 -0700833 // Add the callbacks from the drawing state into the current state. This is so when the current
834 // state gets copied to drawing, we don't lose the callback handles that are still in drawing.
835 for (auto& handle : s.callbackHandles) {
836 c.callbackHandles.push_back(handle);
837 }
838
Vishnu Nair1506b182021-02-22 14:35:15 -0800839 // Allow BufferStateLayer to release any unlatched buffers in drawing state.
Alec Mouria90a5702021-04-16 16:36:21 +0000840 bufferMayChange(c.buffer->getBuffer());
Vishnu Nair1506b182021-02-22 14:35:15 -0800841
Mathias Agopian13127d82013-03-05 17:47:11 -0800842 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -0700843 commitTransaction(c);
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800844 mCurrentState.callbackHandles = {};
Robert Carra1257842020-01-31 13:48:28 -0800845
Mathias Agopian13127d82013-03-05 17:47:11 -0800846 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800847}
848
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000849void Layer::commitTransaction(State& stateToCommit) {
Adithya Srinivasand11eb6b2021-03-09 18:46:28 +0000850 if (auto& bufferSurfaceFrame = mDrawingState.bufferSurfaceFrameTX;
Alec Mouria90a5702021-04-16 16:36:21 +0000851 ((mDrawingState.buffer && stateToCommit.buffer &&
852 mDrawingState.buffer->getBuffer() != stateToCommit.buffer->getBuffer()) ||
853 (mDrawingState.buffer && !stateToCommit.buffer) ||
854 (!mDrawingState.buffer && stateToCommit.buffer)) &&
855 bufferSurfaceFrame != nullptr &&
Adithya Srinivasand11eb6b2021-03-09 18:46:28 +0000856 bufferSurfaceFrame->getPresentState() != PresentState::Presented) {
857 // If the previous buffer was committed but not latched (refreshPending - happens during
858 // back to back invalidates), it gets silently dropped here. Mark the corresponding
859 // SurfaceFrame as dropped to prevent it from getting stuck in the pending classification
860 // list.
861 addSurfaceFrameDroppedForBuffer(bufferSurfaceFrame);
862 }
Ady Abraham59fd8ff2021-04-15 20:13:30 -0700863 const bool frameRateVoteChanged =
864 mDrawingState.frameRateForLayerTree != stateToCommit.frameRateForLayerTree;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800865 mDrawingState = stateToCommit;
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000866
Ady Abraham59fd8ff2021-04-15 20:13:30 -0700867 if (frameRateVoteChanged) {
868 mFlinger->mScheduler->recordLayerHistory(this, systemTime(),
869 LayerHistory::LayerUpdateType::SetFrameRate);
870 }
871
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000872 // Set the present state for all bufferlessSurfaceFramesTX to Presented. The
873 // bufferSurfaceFrameTX will be presented in latchBuffer.
874 for (auto& [token, surfaceFrame] : mDrawingState.bufferlessSurfaceFramesTX) {
875 if (surfaceFrame->getPresentState() != PresentState::Presented) {
876 // With applyPendingStates, we could end up having presented surfaceframes from previous
877 // states
878 surfaceFrame->setPresentState(PresentState::Presented);
879 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
880 }
881 }
882 // Clear the surfaceFrames from the old state now that it has been copied into DrawingState.
883 stateToCommit.bufferSurfaceFrameTX.reset();
884 stateToCommit.bufferlessSurfaceFramesTX.clear();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700885}
886
Mathias Agopian13127d82013-03-05 17:47:11 -0800887uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800888 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800889}
890
891uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800892 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -0800893}
894
chaviw214c89d2019-09-04 16:03:53 -0700895bool Layer::setPosition(float x, float y) {
chaviw4e765532021-04-30 12:11:39 -0500896 if (mCurrentState.transform.tx() == x && mCurrentState.transform.ty() == y) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800897 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -0700898
899 // We update the requested and active position simultaneously because
900 // we want to apply the position portion of the transform matrix immediately,
901 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
chaviw4e765532021-04-30 12:11:39 -0500902 mCurrentState.transform.set(x, y);
chaviw214c89d2019-09-04 16:03:53 -0700903 // Here we directly update the active state
904 // unlike other setters, because we store it within
905 // the transform, but use different latching rules.
906 // b/38182305
chaviw4e765532021-04-30 12:11:39 -0500907 mCurrentState.transform.set(x, y);
Robert Carr69663fb2016-03-27 19:59:19 -0700908
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800909 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800910 setTransactionFlags(eTransactionNeeded);
911 return true;
912}
Robert Carr82364e32016-05-15 11:27:47 -0700913
Robert Carr1f0a16a2016-10-24 16:27:39 -0700914bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
915 ssize_t idx = mCurrentChildren.indexOf(childLayer);
916 if (idx < 0) {
917 return false;
918 }
919 if (childLayer->setLayer(z)) {
920 mCurrentChildren.removeAt(idx);
921 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800922 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700923 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800924 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700925}
926
Robert Carr503c7042017-09-27 15:06:08 -0700927bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
928 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
929 ssize_t idx = mCurrentChildren.indexOf(childLayer);
930 if (idx < 0) {
931 return false;
932 }
933 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
934 mCurrentChildren.removeAt(idx);
935 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800936 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700937 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800938 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700939}
940
Robert Carrae060832016-11-28 10:51:00 -0800941bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800942 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
943 mCurrentState.sequence++;
944 mCurrentState.z = z;
945 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700946
947 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800948 if (mCurrentState.zOrderRelativeOf != nullptr) {
949 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700950 if (strongRelative != nullptr) {
951 strongRelative->removeZOrderRelative(this);
952 }
chaviw606e5cf2019-03-01 10:12:10 -0800953 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -0700954 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800955 setTransactionFlags(eTransactionNeeded);
956 return true;
957}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700958
Robert Carrdb66e622017-04-10 16:55:57 -0700959void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800960 mCurrentState.zOrderRelatives.remove(relative);
961 mCurrentState.sequence++;
962 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700963 setTransactionFlags(eTransactionNeeded);
964}
965
966void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800967 mCurrentState.zOrderRelatives.add(relative);
968 mCurrentState.modified = true;
969 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -0700970 setTransactionFlags(eTransactionNeeded);
971}
972
chaviw606e5cf2019-03-01 10:12:10 -0800973void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
974 mCurrentState.zOrderRelativeOf = relativeOf;
975 mCurrentState.sequence++;
976 mCurrentState.modified = true;
chaviwbdb8b802019-10-14 09:17:12 -0700977 mCurrentState.isRelativeOf = relativeOf != nullptr;
978
chaviw606e5cf2019-03-01 10:12:10 -0800979 setTransactionFlags(eTransactionNeeded);
980}
981
Robert Carr503d2bd2017-12-04 15:49:47 -0800982bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -0700983 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
984 if (handle == nullptr) {
985 return false;
986 }
987 sp<Layer> relative = handle->owner.promote();
988 if (relative == nullptr) {
989 return false;
990 }
991
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800992 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
993 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -0800994 return false;
995 }
996
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800997 mCurrentState.sequence++;
998 mCurrentState.modified = true;
999 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001000
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001001 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -07001002 if (oldZOrderRelativeOf != nullptr) {
1003 oldZOrderRelativeOf->removeZOrderRelative(this);
1004 }
chaviw606e5cf2019-03-01 10:12:10 -08001005 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -07001006 relative->addZOrderRelative(this);
1007
1008 setTransactionFlags(eTransactionNeeded);
1009
1010 return true;
1011}
1012
Mathias Agopian13127d82013-03-05 17:47:11 -08001013bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001014 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001015 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001016 mCurrentState.requested_legacy.w = w;
1017 mCurrentState.requested_legacy.h = h;
1018 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001019 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001020
1021 // record the new size, from this point on, when the client request
1022 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001023 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001024 return true;
1025}
chaviw4e765532021-04-30 12:11:39 -05001026
Dan Stoza9e56aa02015-11-02 13:00:03 -08001027bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001028 if (mCurrentState.color.a == alpha) return false;
1029 mCurrentState.sequence++;
1030 mCurrentState.color.a = alpha;
1031 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001032 setTransactionFlags(eTransactionNeeded);
1033 return true;
1034}
chaviw13fdc492017-06-27 12:40:18 -07001035
Valerie Haudd0b7572019-01-29 14:59:27 -08001036bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1037 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001038 return false;
Valerie Hauaa194562019-02-05 16:21:38 -08001039 }
1040 mCurrentState.sequence++;
1041 mCurrentState.modified = true;
1042 setTransactionFlags(eTransactionNeeded);
1043
1044 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001045 // create background color layer if one does not yet exist
Vishnu Nairfa247b12020-02-11 08:58:26 -08001046 uint32_t flags = ISurfaceComposerClient::eFXSurfaceEffect;
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001047 std::string name = mName + "BackgroundColorLayer";
Vishnu Nairfa247b12020-02-11 08:58:26 -08001048 mCurrentState.bgColorLayer = mFlinger->getFactory().createEffectLayer(
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001049 LayerCreationArgs(mFlinger.get(), nullptr, std::move(name), 0, 0, flags,
1050 LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -07001051
Valerie Haudd0b7572019-01-29 14:59:27 -08001052 // add to child list
1053 addChild(mCurrentState.bgColorLayer);
1054 mFlinger->mLayersAdded = true;
1055 // set up SF to handle added color layer
1056 if (isRemovedFromCurrentState()) {
1057 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1058 }
1059 mFlinger->setTransactionFlags(eTransactionNeeded);
1060 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1061 mCurrentState.bgColorLayer->reparent(nullptr);
1062 mCurrentState.bgColorLayer = nullptr;
1063 return true;
1064 }
1065
1066 mCurrentState.bgColorLayer->setColor(color);
1067 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1068 mCurrentState.bgColorLayer->setAlpha(alpha);
1069 mCurrentState.bgColorLayer->setDataspace(dataspace);
1070
chaviw13fdc492017-06-27 12:40:18 -07001071 return true;
1072}
1073
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001074bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001075 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001076
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001077 mCurrentState.sequence++;
1078 mCurrentState.cornerRadius = cornerRadius;
1079 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001080 setTransactionFlags(eTransactionNeeded);
1081 return true;
1082}
1083
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001084bool Layer::setBackgroundBlurRadius(int backgroundBlurRadius) {
1085 if (mCurrentState.backgroundBlurRadius == backgroundBlurRadius) return false;
1086
1087 mCurrentState.sequence++;
1088 mCurrentState.backgroundBlurRadius = backgroundBlurRadius;
1089 mCurrentState.modified = true;
1090 setTransactionFlags(eTransactionNeeded);
1091 return true;
1092}
1093
Robert Carrd4ae7f32018-06-07 16:10:57 -07001094bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1095 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001096 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001097 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1098
1099 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
Hongwei Wang46213ea2020-12-04 17:17:31 -08001100 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER nor "
1101 "ROTATE_SURFACE_FLINGER ignored");
Robert Carrd4ae7f32018-06-07 16:10:57 -07001102 return false;
1103 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001104 mCurrentState.sequence++;
chaviw4e765532021-04-30 12:11:39 -05001105 mCurrentState.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001106 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001107 setTransactionFlags(eTransactionNeeded);
1108 return true;
1109}
Marissa Wall61c58622018-07-18 10:12:20 -07001110
Mathias Agopian13127d82013-03-05 17:47:11 -08001111bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001112 mCurrentState.requestedTransparentRegion_legacy = transparent;
1113 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001114 setTransactionFlags(eTransactionNeeded);
1115 return true;
1116}
Ana Krulecc84d09b2019-11-02 23:10:29 +01001117
Lucas Dupinc3800b82020-10-02 16:24:48 -07001118bool Layer::setBlurRegions(const std::vector<BlurRegion>& blurRegions) {
1119 mCurrentState.sequence++;
1120 mCurrentState.blurRegions = blurRegions;
1121 mCurrentState.modified = true;
1122 setTransactionFlags(eTransactionNeeded);
1123 return true;
1124}
1125
Vishnu Nairf6eddb62021-01-27 22:02:11 -08001126bool Layer::setFlags(uint32_t flags, uint32_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001127 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1128 if (mCurrentState.flags == newFlags) return false;
1129 mCurrentState.sequence++;
1130 mCurrentState.flags = newFlags;
1131 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001132 setTransactionFlags(eTransactionNeeded);
1133 return true;
1134}
Robert Carr99e27f02016-06-16 15:18:02 -07001135
chaviw25714502021-02-11 10:01:08 -08001136bool Layer::setCrop(const Rect& crop) {
1137 if (mCurrentState.requestedCrop == crop) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001138 mCurrentState.sequence++;
chaviw25714502021-02-11 10:01:08 -08001139 mCurrentState.requestedCrop = crop;
1140 mCurrentState.crop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001141
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001142 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001143 setTransactionFlags(eTransactionNeeded);
1144 return true;
1145}
Robert Carr8d5227b2017-03-16 15:41:03 -07001146
Evan Roskyef876c92019-01-25 17:46:06 -08001147bool Layer::setMetadata(const LayerMetadata& data) {
1148 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001149 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001150 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001151 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001152}
1153
Mathias Agopian13127d82013-03-05 17:47:11 -08001154bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001155 if (mCurrentState.layerStack == layerStack) return false;
1156 mCurrentState.sequence++;
1157 mCurrentState.layerStack = layerStack;
1158 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001159 setTransactionFlags(eTransactionNeeded);
1160 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001161}
1162
Peiyong Linc502cb72019-03-01 15:00:23 -08001163bool Layer::setColorSpaceAgnostic(const bool agnostic) {
1164 if (mCurrentState.colorSpaceAgnostic == agnostic) {
1165 return false;
1166 }
1167 mCurrentState.sequence++;
1168 mCurrentState.colorSpaceAgnostic = agnostic;
1169 mCurrentState.modified = true;
1170 setTransactionFlags(eTransactionNeeded);
1171 return true;
1172}
1173
Ana Krulecc84d09b2019-11-02 23:10:29 +01001174bool Layer::setFrameRateSelectionPriority(int32_t priority) {
1175 if (mCurrentState.frameRateSelectionPriority == priority) return false;
1176 mCurrentState.frameRateSelectionPriority = priority;
1177 mCurrentState.sequence++;
1178 mCurrentState.modified = true;
1179 setTransactionFlags(eTransactionNeeded);
1180 return true;
1181}
1182
1183int32_t Layer::getFrameRateSelectionPriority() const {
1184 // Check if layer has priority set.
1185 if (mDrawingState.frameRateSelectionPriority != PRIORITY_UNSET) {
1186 return mDrawingState.frameRateSelectionPriority;
1187 }
1188 // If not, search whether its parents have it set.
1189 sp<Layer> parent = getParent();
1190 if (parent != nullptr) {
1191 return parent->getFrameRateSelectionPriority();
1192 }
1193
1194 return Layer::PRIORITY_UNSET;
1195}
1196
Ady Abrahamaae5ed52020-06-26 09:32:43 -07001197bool Layer::isLayerFocusedBasedOnPriority(int32_t priority) {
1198 return priority == PRIORITY_FOCUSED_WITH_MODE || priority == PRIORITY_FOCUSED_WITHOUT_MODE;
1199};
1200
Robert Carr1f0a16a2016-10-24 16:27:39 -07001201uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001202 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001203 if (p == nullptr) {
1204 return getDrawingState().layerStack;
1205 }
1206 return p->getLayerStack();
1207}
1208
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001209bool Layer::setShadowRadius(float shadowRadius) {
1210 if (mCurrentState.shadowRadius == shadowRadius) {
1211 return false;
1212 }
1213
1214 mCurrentState.sequence++;
1215 mCurrentState.shadowRadius = shadowRadius;
1216 mCurrentState.modified = true;
1217 setTransactionFlags(eTransactionNeeded);
1218 return true;
1219}
1220
Vishnu Nair6213bd92020-05-08 17:42:25 -07001221bool Layer::setFixedTransformHint(ui::Transform::RotationFlags fixedTransformHint) {
1222 if (mCurrentState.fixedTransformHint == fixedTransformHint) {
1223 return false;
1224 }
1225
1226 mCurrentState.sequence++;
1227 mCurrentState.fixedTransformHint = fixedTransformHint;
1228 mCurrentState.modified = true;
1229 setTransactionFlags(eTransactionNeeded);
1230 return true;
1231}
1232
John Reckcdb4ed72021-02-04 13:39:33 -05001233bool Layer::setStretchEffect(const StretchEffect& effect) {
1234 StretchEffect temp = effect;
1235 temp.sanitize();
1236 if (mCurrentState.stretchEffect == temp) {
1237 return false;
1238 }
1239 mCurrentState.sequence++;
1240 mCurrentState.stretchEffect = temp;
1241 mCurrentState.modified = true;
1242 setTransactionFlags(eTransactionNeeded);
1243 return true;
1244}
1245
John Reckc00c6692021-02-16 11:37:33 -05001246StretchEffect Layer::getStretchEffect() const {
1247 if (mDrawingState.stretchEffect.hasEffect()) {
1248 return mDrawingState.stretchEffect;
1249 }
1250
1251 sp<Layer> parent = getParent();
1252 if (parent != nullptr) {
1253 auto effect = parent->getStretchEffect();
1254 if (effect.hasEffect()) {
1255 // TODO(b/179047472): Map it? Or do we make the effect be in global space?
1256 return effect;
1257 }
1258 }
1259 return StretchEffect{};
1260}
1261
Ady Abraham60e42ea2020-03-09 19:17:31 -07001262void Layer::updateTreeHasFrameRateVote() {
1263 const auto traverseTree = [&](const LayerVector::Visitor& visitor) {
1264 auto parent = getParent();
1265 while (parent) {
1266 visitor(parent.get());
1267 parent = parent->getParent();
1268 }
1269
1270 traverse(LayerVector::StateSet::Current, visitor);
1271 };
1272
1273 // update parents and children about the vote
Ady Abraham59fd8ff2021-04-15 20:13:30 -07001274 // First traverse the tree and count how many layers has votes.
Ady Abraham60e42ea2020-03-09 19:17:31 -07001275 int layersWithVote = 0;
Ady Abraham44e9f3b2021-02-16 15:22:58 -08001276 traverseTree([&layersWithVote](Layer* layer) {
Marin Shalamanove8a663d2020-11-24 17:48:00 +01001277 const auto layerVotedWithDefaultCompatibility =
1278 layer->mCurrentState.frameRate.rate.isValid() &&
Ady Abraham414e8b52020-05-21 15:36:04 -07001279 layer->mCurrentState.frameRate.type == FrameRateCompatibility::Default;
1280 const auto layerVotedWithNoVote =
1281 layer->mCurrentState.frameRate.type == FrameRateCompatibility::NoVote;
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08001282 const auto layerVotedWithExactCompatibility =
1283 layer->mCurrentState.frameRate.type == FrameRateCompatibility::Exact;
Ady Abraham414e8b52020-05-21 15:36:04 -07001284
1285 // We do not count layers that are ExactOrMultiple for the same reason
1286 // we are allowing touch boost for those layers. See
1287 // RefreshRateConfigs::getBestRefreshRate for more details.
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08001288 if (layerVotedWithDefaultCompatibility || layerVotedWithNoVote ||
1289 layerVotedWithExactCompatibility) {
Ady Abraham60e42ea2020-03-09 19:17:31 -07001290 layersWithVote++;
1291 }
1292 });
1293
Ady Abraham59fd8ff2021-04-15 20:13:30 -07001294 // Now we can update the tree frame rate vote for each layer in the tree
1295 const bool treeHasFrameRateVote = layersWithVote > 0;
Ady Abraham60e42ea2020-03-09 19:17:31 -07001296 bool transactionNeeded = false;
Ady Abraham59fd8ff2021-04-15 20:13:30 -07001297 traverseTree([treeHasFrameRateVote, &transactionNeeded](Layer* layer) {
1298 transactionNeeded = layer->updateFrameRateForLayerTree(treeHasFrameRateVote);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001299 });
1300
1301 if (transactionNeeded) {
1302 mFlinger->setTransactionFlags(eTraversalNeeded);
1303 }
1304}
1305
Ady Abraham71c437d2020-01-31 15:56:57 -08001306bool Layer::setFrameRate(FrameRate frameRate) {
Ana Krulec3803b8d2020-02-03 16:35:46 -08001307 if (!mFlinger->useFrameRateApi) {
1308 return false;
1309 }
Steven Thomas3172e202020-01-06 19:25:30 -08001310 if (mCurrentState.frameRate == frameRate) {
1311 return false;
1312 }
1313
1314 mCurrentState.sequence++;
1315 mCurrentState.frameRate = frameRate;
1316 mCurrentState.modified = true;
Ady Abraham60e42ea2020-03-09 19:17:31 -07001317
1318 updateTreeHasFrameRateVote();
1319
Steven Thomas3172e202020-01-06 19:25:30 -08001320 setTransactionFlags(eTransactionNeeded);
1321 return true;
1322}
1323
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001324void Layer::setFrameTimelineVsyncForBufferTransaction(const FrameTimelineInfo& info,
1325 nsecs_t postTime) {
1326 mCurrentState.postTime = postTime;
1327
1328 // Check if one of the bufferlessSurfaceFramesTX contains the same vsyncId. This can happen if
1329 // there are two transactions with the same token, the first one without a buffer and the
1330 // second one with a buffer. We promote the bufferlessSurfaceFrame to a bufferSurfaceFrameTX
1331 // in that case.
1332 auto it = mCurrentState.bufferlessSurfaceFramesTX.find(info.vsyncId);
1333 if (it != mCurrentState.bufferlessSurfaceFramesTX.end()) {
1334 // Promote the bufferlessSurfaceFrame to a bufferSurfaceFrameTX
1335 mCurrentState.bufferSurfaceFrameTX = it->second;
1336 mCurrentState.bufferlessSurfaceFramesTX.erase(it);
Adithya Srinivasan785addd2021-03-09 00:38:00 +00001337 mCurrentState.bufferSurfaceFrameTX->promoteToBuffer();
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001338 mCurrentState.bufferSurfaceFrameTX->setActualQueueTime(postTime);
1339 } else {
1340 mCurrentState.bufferSurfaceFrameTX =
1341 createSurfaceFrameForBuffer(info, postTime, mTransactionName);
1342 }
1343}
1344
1345void Layer::setFrameTimelineVsyncForBufferlessTransaction(const FrameTimelineInfo& info,
1346 nsecs_t postTime) {
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -10001347 mCurrentState.frameTimelineInfo = info;
Ady Abraham22c7b5c2020-09-22 19:33:40 -07001348 mCurrentState.postTime = postTime;
1349 mCurrentState.modified = true;
1350 setTransactionFlags(eTransactionNeeded);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001351
1352 if (const auto& bufferSurfaceFrameTX = mCurrentState.bufferSurfaceFrameTX;
1353 bufferSurfaceFrameTX != nullptr) {
1354 if (bufferSurfaceFrameTX->getToken() == info.vsyncId) {
1355 // BufferSurfaceFrame takes precedence over BufferlessSurfaceFrame. If the same token is
1356 // being used for BufferSurfaceFrame, don't create a new one.
1357 return;
1358 }
1359 }
1360 // For Transactions without a buffer, we create only one SurfaceFrame per vsyncId. If multiple
1361 // transactions use the same vsyncId, we just treat them as one SurfaceFrame (unless they are
1362 // targeting different vsyncs).
1363 auto it = mCurrentState.bufferlessSurfaceFramesTX.find(info.vsyncId);
1364 if (it == mCurrentState.bufferlessSurfaceFramesTX.end()) {
1365 auto surfaceFrame = createSurfaceFrameForTransaction(info, postTime);
1366 mCurrentState.bufferlessSurfaceFramesTX[info.vsyncId] = surfaceFrame;
1367 } else {
1368 if (it->second->getPresentState() == PresentState::Presented) {
1369 // If the SurfaceFrame was already presented, its safe to overwrite it since it must
1370 // have been from previous vsync.
1371 it->second = createSurfaceFrameForTransaction(info, postTime);
1372 }
1373 }
1374}
1375
1376void Layer::addSurfaceFrameDroppedForBuffer(
1377 std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame) {
Adithya Srinivasan061c14c2021-02-11 01:19:47 +00001378 surfaceFrame->setDropTime(systemTime());
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001379 surfaceFrame->setPresentState(PresentState::Dropped);
1380 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
1381}
1382
1383void Layer::addSurfaceFramePresentedForBuffer(
1384 std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame, nsecs_t acquireFenceTime,
1385 nsecs_t currentLatchTime) {
1386 surfaceFrame->setAcquireFenceTime(acquireFenceTime);
1387 surfaceFrame->setPresentState(PresentState::Presented, mLastLatchTime);
1388 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
1389 mLastLatchTime = currentLatchTime;
1390}
1391
1392std::shared_ptr<frametimeline::SurfaceFrame> Layer::createSurfaceFrameForTransaction(
1393 const FrameTimelineInfo& info, nsecs_t postTime) {
1394 auto surfaceFrame =
Alec Mouriadebf5c2021-01-05 12:57:36 -08001395 mFlinger->mFrameTimeline->createSurfaceFrameForToken(info, mOwnerPid, mOwnerUid,
1396 getSequence(), mName,
Adithya Srinivasan785addd2021-03-09 00:38:00 +00001397 mTransactionName,
1398 /*isBuffer*/ false);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001399 // For Transactions, the post time is considered to be both queue and acquire fence time.
1400 surfaceFrame->setActualQueueTime(postTime);
1401 surfaceFrame->setAcquireFenceTime(postTime);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001402 const auto fps = mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
1403 if (fps) {
Alec Mouri819f6302021-02-12 15:37:21 -08001404 surfaceFrame->setRenderRate(*fps);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001405 }
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001406 onSurfaceFrameCreated(surfaceFrame);
1407 return surfaceFrame;
1408}
1409
1410std::shared_ptr<frametimeline::SurfaceFrame> Layer::createSurfaceFrameForBuffer(
1411 const FrameTimelineInfo& info, nsecs_t queueTime, std::string debugName) {
1412 auto surfaceFrame =
Alec Mouriadebf5c2021-01-05 12:57:36 -08001413 mFlinger->mFrameTimeline->createSurfaceFrameForToken(info, mOwnerPid, mOwnerUid,
Adithya Srinivasan785addd2021-03-09 00:38:00 +00001414 getSequence(), mName, debugName,
1415 /*isBuffer*/ true);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001416 // For buffers, acquire fence time will set during latch.
1417 surfaceFrame->setActualQueueTime(queueTime);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001418 const auto fps = mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
1419 if (fps) {
Alec Mouri819f6302021-02-12 15:37:21 -08001420 surfaceFrame->setRenderRate(*fps);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001421 }
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001422 // TODO(b/178542907): Implement onSurfaceFrameCreated for BQLayer as well.
1423 onSurfaceFrameCreated(surfaceFrame);
1424 return surfaceFrame;
Ady Abraham74e17562020-08-24 18:18:19 -07001425}
1426
Ady Abraham59fd8ff2021-04-15 20:13:30 -07001427bool Layer::updateFrameRateForLayerTree(bool treeHasFrameRateVote) {
1428 const auto updateCurrentState = [&](FrameRate frameRate) {
1429 if (mCurrentState.frameRateForLayerTree == frameRate) {
1430 return false;
1431 }
1432 mCurrentState.frameRateForLayerTree = frameRate;
1433 mCurrentState.sequence++;
1434 mCurrentState.modified = true;
1435 setTransactionFlags(eTransactionNeeded);
1436 return true;
1437 };
1438
1439 const auto frameRate = mCurrentState.frameRate;
Marin Shalamanove8a663d2020-11-24 17:48:00 +01001440 if (frameRate.rate.isValid() || frameRate.type == FrameRateCompatibility::NoVote) {
Ady Abraham59fd8ff2021-04-15 20:13:30 -07001441 return updateCurrentState(frameRate);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001442 }
1443
Ady Abrahamf467f892020-07-31 16:01:53 -07001444 // This layer doesn't have a frame rate. Check if its ancestors have a vote
Ady Abraham59fd8ff2021-04-15 20:13:30 -07001445 for (sp<Layer> parent = getParent(); parent; parent = parent->getParent()) {
1446 if (parent->mCurrentState.frameRate.rate.isValid()) {
1447 return updateCurrentState(parent->mCurrentState.frameRate);
Ady Abrahamf467f892020-07-31 16:01:53 -07001448 }
1449 }
1450
1451 // This layer and its ancestors don't have a frame rate. If one of successors
1452 // has a vote, return a NoVote for successors to set the vote
Ady Abraham59fd8ff2021-04-15 20:13:30 -07001453 if (treeHasFrameRateVote) {
1454 return updateCurrentState(FrameRate(Fps(0.0f), FrameRateCompatibility::NoVote));
Ady Abraham60e42ea2020-03-09 19:17:31 -07001455 }
1456
Ady Abraham59fd8ff2021-04-15 20:13:30 -07001457 return updateCurrentState(frameRate);
Steven Thomas3172e202020-01-06 19:25:30 -08001458}
1459
Ady Abraham59fd8ff2021-04-15 20:13:30 -07001460Layer::FrameRate Layer::getFrameRateForLayerTree() const {
1461 return getDrawingState().frameRateForLayerTree;
1462}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001463
Robert Carr1f0a16a2016-10-24 16:27:39 -07001464bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001465 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001466 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001467 if (parent != nullptr && parent->isHiddenByPolicy()) {
1468 return true;
1469 }
Robert Carr1c5481e2019-07-01 14:42:27 -07001470 if (usingRelativeZ(LayerVector::StateSet::Drawing)) {
1471 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
1472 if (zOrderRelativeOf != nullptr) {
1473 if (zOrderRelativeOf->isHiddenByPolicy()) {
1474 return true;
1475 }
1476 }
1477 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001478 return s.flags & layer_state_t::eLayerHidden;
1479}
1480
David Sodman41fdfc92017-11-06 16:09:56 -08001481uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001482 // TODO: should we do something special if mSecure is set?
1483 if (mProtectedByApp) {
1484 // need a hardware-protected path to external video sink
1485 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001486 }
Riley Andrews03414a12014-07-01 14:22:59 -07001487 if (mPotentialCursor) {
1488 usage |= GraphicBuffer::USAGE_CURSOR;
1489 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001490 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001491 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001492}
1493
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001494void Layer::updateTransformHint(ui::Transform::RotationFlags transformHint) {
1495 if (mFlinger->mDebugDisableTransformHint || transformHint & ui::Transform::ROT_INVALID) {
1496 transformHint = ui::Transform::ROT_0;
Mathias Agopiana4583642011-08-23 18:03:18 -07001497 }
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001498
Vishnu Nair6213bd92020-05-08 17:42:25 -07001499 setTransformHint(transformHint);
Mathias Agopiana4583642011-08-23 18:03:18 -07001500}
1501
Mathias Agopian13127d82013-03-05 17:47:11 -08001502// ----------------------------------------------------------------------------
1503// debugging
1504// ----------------------------------------------------------------------------
1505
Marissa Wall61c58622018-07-18 10:12:20 -07001506// TODO(marissaw): add new layer state info to layer debugging
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001507LayerDebugInfo Layer::getLayerDebugInfo(const DisplayDevice* display) const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001508 using namespace std::string_literals;
1509
Kalle Raitaa099a242017-01-11 11:17:29 -08001510 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001511 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001512 info.mName = getName();
Vishnu Nair43bccf82020-06-05 10:53:37 -07001513 sp<Layer> parent = mDrawingParent.promote();
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001514 info.mParentName = parent ? parent->getName() : "none"s;
chaviw8a01fa42019-08-19 12:39:31 -07001515 info.mType = getType();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001516 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Lloyd Piquea2468662019-03-07 21:31:06 -08001517
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001518 info.mVisibleRegion = getVisibleRegion(display);
Kalle Raitaa099a242017-01-11 11:17:29 -08001519 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001520 info.mLayerStack = getLayerStack();
chaviw4e765532021-04-30 12:11:39 -05001521 info.mX = ds.transform.tx();
1522 info.mY = ds.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001523 info.mZ = ds.z;
chaviw4e765532021-04-30 12:11:39 -05001524 info.mWidth = ds.width;
1525 info.mHeight = ds.height;
chaviw25714502021-02-11 10:01:08 -08001526 info.mCrop = ds.crop;
chaviw13fdc492017-06-27 12:40:18 -07001527 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001528 info.mFlags = ds.flags;
1529 info.mPixelFormat = getPixelFormat();
chaviw4244e032019-09-04 11:27:49 -07001530 info.mDataSpace = static_cast<android_dataspace>(getDataSpace());
chaviw4e765532021-04-30 12:11:39 -05001531 info.mMatrix[0][0] = ds.transform[0][0];
1532 info.mMatrix[0][1] = ds.transform[0][1];
1533 info.mMatrix[1][0] = ds.transform[1][0];
1534 info.mMatrix[1][1] = ds.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001535 {
chaviwd62d3062019-09-04 14:48:02 -07001536 sp<const GraphicBuffer> buffer = getBuffer();
David Sodman5b4cffc2017-11-23 13:20:29 -08001537 if (buffer != 0) {
1538 info.mActiveBufferWidth = buffer->getWidth();
1539 info.mActiveBufferHeight = buffer->getHeight();
1540 info.mActiveBufferStride = buffer->getStride();
1541 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001542 } else {
1543 info.mActiveBufferWidth = 0;
1544 info.mActiveBufferHeight = 0;
1545 info.mActiveBufferStride = 0;
1546 info.mActiveBufferFormat = 0;
1547 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001548 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001549 info.mNumQueuedFrames = getQueuedFrameCount();
1550 info.mRefreshPending = isBufferLatched();
1551 info.mIsOpaque = isOpaque(ds);
1552 info.mContentDirty = contentDirty;
John Reckc00c6692021-02-16 11:37:33 -05001553 info.mStretchEffect = getStretchEffect();
Kalle Raitaa099a242017-01-11 11:17:29 -08001554 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001555}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001556
Yiwei Zhang5434a782018-12-05 18:06:32 -08001557void Layer::miniDumpHeader(std::string& result) {
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001558 result.append(kDumpTableRowLength, '-');
1559 result.append("\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001560 result.append(" Layer name\n");
1561 result.append(" Z | ");
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001562 result.append(" Window Type | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001563 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001564 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001565 result.append(" Disp Frame (LTRB) | ");
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001566 result.append(" Source Crop (LTRB) | ");
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001567 result.append(" Frame Rate (Explicit) (Seamlessness) [Focused]\n");
1568 result.append(kDumpTableRowLength, '-');
1569 result.append("\n");
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001570}
1571
1572std::string Layer::frameRateCompatibilityString(Layer::FrameRateCompatibility compatibility) {
1573 switch (compatibility) {
1574 case FrameRateCompatibility::Default:
1575 return "Default";
1576 case FrameRateCompatibility::ExactOrMultiple:
1577 return "ExactOrMultiple";
1578 case FrameRateCompatibility::NoVote:
1579 return "NoVote";
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08001580 case FrameRateCompatibility::Exact:
1581 return "Exact";
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001582 }
Dan Stozae22aec72016-08-01 13:20:59 -07001583}
1584
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001585void Layer::miniDump(std::string& result, const DisplayDevice& display) const {
1586 const auto outputLayer = findOutputLayerForDisplay(&display);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001587 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001588 return;
1589 }
1590
Yiwei Zhang5434a782018-12-05 18:06:32 -08001591 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001592 if (mName.length() > 77) {
1593 std::string shortened;
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001594 shortened.append(mName, 0, 36);
Dan Stozae22aec72016-08-01 13:20:59 -07001595 shortened.append("[...]");
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001596 shortened.append(mName, mName.length() - 36);
1597 name = std::move(shortened);
Dan Stozae22aec72016-08-01 13:20:59 -07001598 } else {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001599 name = mName;
Dan Stozae22aec72016-08-01 13:20:59 -07001600 }
1601
Yiwei Zhang5434a782018-12-05 18:06:32 -08001602 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001603
Alec Mourib416efd2018-09-06 21:01:59 +00001604 const State& layerState(getDrawingState());
Lloyd Piquede196652020-01-22 17:29:58 -08001605 const auto& outputLayerState = outputLayer->getState();
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001606
Chia-I Wu1e043612018-03-01 09:45:09 -08001607 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001608 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001609 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001610 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001611 }
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001612 StringAppendF(&result, " %10d | ", mWindowType);
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001613 StringAppendF(&result, "%10s | ", toString(getCompositionType(display)).c_str());
Lloyd Piquede196652020-01-22 17:29:58 -08001614 StringAppendF(&result, "%10s | ", toString(outputLayerState.bufferTransform).c_str());
1615 const Rect& frame = outputLayerState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001616 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Piquede196652020-01-22 17:29:58 -08001617 const FloatRect& crop = outputLayerState.sourceCrop;
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001618 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f | ", crop.left, crop.top, crop.right,
Yiwei Zhang5434a782018-12-05 18:06:32 -08001619 crop.bottom);
Ady Abrahamf467f892020-07-31 16:01:53 -07001620 const auto frameRate = getFrameRateForLayerTree();
1621 if (frameRate.rate.isValid() || frameRate.type != FrameRateCompatibility::Default) {
1622 StringAppendF(&result, "%s %15s %17s", to_string(frameRate.rate).c_str(),
1623 frameRateCompatibilityString(frameRate.type).c_str(),
1624 toString(frameRate.seamlessness).c_str());
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001625 } else {
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001626 result.append(41, ' ');
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001627 }
Dan Stozae22aec72016-08-01 13:20:59 -07001628
Ady Abrahamaae5ed52020-06-26 09:32:43 -07001629 const auto focused = isLayerFocusedBasedOnPriority(getFrameRateSelectionPriority());
1630 StringAppendF(&result, " [%s]\n", focused ? "*" : " ");
1631
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001632 result.append(kDumpTableRowLength, '-');
1633 result.append("\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001634}
Dan Stozae22aec72016-08-01 13:20:59 -07001635
Yiwei Zhang5434a782018-12-05 18:06:32 -08001636void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001637 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001638}
1639
Svetoslavd85084b2014-03-20 10:28:31 -07001640void Layer::clearFrameStats() {
1641 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001642}
1643
Jamie Gennis6547ff42013-07-16 20:12:42 -07001644void Layer::logFrameStats() {
1645 mFrameTracker.logAndResetStats(mName);
1646}
1647
Svetoslavd85084b2014-03-20 10:28:31 -07001648void Layer::getFrameStats(FrameStats* outStats) const {
1649 mFrameTracker.getStats(outStats);
1650}
1651
Yiwei Zhang5434a782018-12-05 18:06:32 -08001652void Layer::dumpFrameEvents(std::string& result) {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001653 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().c_str(), getType(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001654 Mutex::Autolock lock(mFrameEventHistoryMutex);
1655 mFrameEventHistory.checkFencesForCompletion();
1656 mFrameEventHistory.dump(result);
1657}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001658
Vishnu Nair0f085c62019-08-30 08:49:12 -07001659void Layer::dumpCallingUidPid(std::string& result) const {
chaviw250bcbb2020-08-05 11:17:54 -07001660 StringAppendF(&result, "Layer %s (%s) callingPid:%d callingUid:%d ownerUid:%d\n",
1661 getName().c_str(), getType(), mCallingPid, mCallingUid, mOwnerUid);
Vishnu Nair0f085c62019-08-30 08:49:12 -07001662}
1663
Brian Anderson5ea5e592016-12-01 16:54:33 -08001664void Layer::onDisconnect() {
1665 Mutex::Autolock lock(mFrameEventHistoryMutex);
1666 mFrameEventHistory.onDisconnect();
Yiwei Zhang1a88c402019-11-18 10:43:58 -08001667 const int32_t layerId = getSequence();
1668 mFlinger->mTimeStats->onDestroy(layerId);
1669 mFlinger->mFrameTracer->onDestroy(layerId);
Brian Anderson5ea5e592016-12-01 16:54:33 -08001670}
1671
Brian Anderson3890c392016-07-25 12:48:08 -07001672void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001673 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001674 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001675 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
Alec Mouri9a29e672020-09-14 12:39:14 -07001676 getName().c_str(), mOwnerUid, newTimestamps->postedTime);
Yiwei Zhang487340f2019-11-18 17:42:12 -08001677 mFlinger->mTimeStats->setAcquireFence(getSequence(), newTimestamps->frameNumber,
1678 newTimestamps->acquireFence);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001679 }
1680
Brian Andersond6927fb2016-07-23 23:37:30 -07001681 Mutex::Autolock lock(mFrameEventHistoryMutex);
1682 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001683 // If there are any unsignaled fences in the aquire timeline at this
1684 // point, the previously queued frame hasn't been latched yet. Go ahead
1685 // and try to get the signal time here so the syscall is taken out of
1686 // the main thread's critical path.
1687 mAcquireTimeline.updateSignalTimes();
1688 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001689 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001690 mFrameEventHistory.addQueue(*newTimestamps);
1691 }
1692
Brian Anderson3890c392016-07-25 12:48:08 -07001693 if (outDelta) {
1694 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001695 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001696}
Dan Stozae77c7662016-05-13 11:37:28 -07001697
Chia-I Wu98f1c102017-05-30 14:54:08 -07001698size_t Layer::getChildrenCount() const {
1699 size_t count = 0;
1700 for (const sp<Layer>& child : mCurrentChildren) {
1701 count += 1 + child->getChildrenCount();
1702 }
1703 return count;
1704}
1705
Robert Carr1f0a16a2016-10-24 16:27:39 -07001706void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001707 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001708 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001709
Robert Carr1f0a16a2016-10-24 16:27:39 -07001710 mCurrentChildren.add(layer);
1711 layer->setParent(this);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001712 updateTreeHasFrameRateVote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001713}
1714
1715ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001716 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001717 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001718
Robert Carr1323c952019-01-28 18:13:27 -08001719 layer->setParent(nullptr);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001720 const auto removeResult = mCurrentChildren.remove(layer);
1721
1722 updateTreeHasFrameRateVote();
1723 layer->updateTreeHasFrameRateVote();
1724
1725 return removeResult;
1726}
1727
Robert Carr15eae092018-03-23 13:43:53 -07001728void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001729 for (const sp<Layer>& child : mDrawingChildren) {
1730 child->mDrawingParent = newParent;
Vishnu Nair6bdec7d2021-05-10 15:01:13 -07001731 child->computeBounds(newParent->mBounds, newParent->mEffectiveTransform,
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001732 newParent->mEffectiveShadowRadius);
Robert Carr578038f2018-03-09 12:25:24 -08001733 }
1734}
1735
chaviwf1961f72017-09-18 16:41:07 -07001736bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr54cf5b12019-01-25 14:02:28 -08001737 sp<Layer> newParent;
1738 if (newParentHandle != nullptr) {
1739 auto handle = static_cast<Handle*>(newParentHandle.get());
1740 newParent = handle->owner.promote();
1741 if (newParent == nullptr) {
1742 ALOGE("Unable to promote Layer handle");
1743 return false;
1744 }
1745 if (newParent == this) {
1746 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1747 return false;
1748 }
1749 }
1750
chaviwf1961f72017-09-18 16:41:07 -07001751 sp<Layer> parent = getParent();
1752 if (parent != nullptr) {
1753 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001754 }
1755
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001756 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001757 newParent->addChild(this);
1758 if (!newParent->isRemovedFromCurrentState()) {
1759 addToCurrentState();
1760 } else {
1761 onRemovedFromCurrentState();
1762 }
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001763 } else {
1764 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001765 }
1766
chaviw06178942017-07-27 10:25:59 -07001767 return true;
1768}
1769
Peiyong Lind3788632018-09-18 16:01:31 -07001770bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001771 static const mat4 identityMatrix = mat4();
1772
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001773 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001774 return false;
1775 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001776 ++mCurrentState.sequence;
1777 mCurrentState.colorTransform = matrix;
1778 mCurrentState.hasColorTransform = matrix != identityMatrix;
1779 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001780 setTransactionFlags(eTransactionNeeded);
1781 return true;
1782}
1783
chaviwf66724d2018-11-28 16:35:21 -08001784mat4 Layer::getColorTransform() const {
1785 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1786 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1787 colorTransform = parent->getColorTransform() * colorTransform;
1788 }
1789 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001790}
1791
1792bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001793 bool hasColorTransform = getDrawingState().hasColorTransform;
1794 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1795 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1796 }
1797 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001798}
1799
Chia-I Wu11481472018-05-04 10:43:19 -07001800bool Layer::isLegacyDataSpace() const {
1801 // return true when no higher bits are set
chaviw4244e032019-09-04 11:27:49 -07001802 return !(getDataSpace() &
1803 (ui::Dataspace::STANDARD_MASK | ui::Dataspace::TRANSFER_MASK |
1804 ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001805}
1806
Robert Carr1f0a16a2016-10-24 16:27:39 -07001807void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001808 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001809}
1810
chaviw301b1d82019-11-06 13:15:09 -08001811int32_t Layer::getZ(LayerVector::StateSet stateSet) const {
1812 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1813 const State& state = useDrawing ? mDrawingState : mCurrentState;
1814 return state.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001815}
1816
Robert Carr1c5481e2019-07-01 14:42:27 -07001817bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) const {
Robert Carr29abff82017-12-04 13:51:20 -08001818 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001819 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviwe5ac40f2019-09-24 16:36:55 -07001820 return state.isRelativeOf;
Robert Carr29abff82017-12-04 13:51:20 -08001821}
1822
David Sodman41fdfc92017-11-06 16:09:56 -08001823__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001824 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001825 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1826 "makeTraversalList received invalid stateSet");
1827 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1828 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001829 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001830
Robert Carr29abff82017-12-04 13:51:20 -08001831 if (state.zOrderRelatives.size() == 0) {
1832 *outSkipRelativeZUsers = true;
1833 return children;
1834 }
1835
chaviwfd462612018-05-31 16:11:27 -07001836 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001837 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001838 sp<Layer> strongRelative = weakRelative.promote();
1839 if (strongRelative != nullptr) {
1840 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001841 }
1842 }
1843
Dan Stoza412903f2017-04-27 13:42:17 -07001844 for (const sp<Layer>& child : children) {
chaviwe5ac40f2019-09-24 16:36:55 -07001845 if (child->usingRelativeZ(stateSet)) {
Robert Carr503c7042017-09-27 15:06:08 -07001846 continue;
1847 }
Robert Carrdb66e622017-04-10 16:55:57 -07001848 traverse.add(child);
1849 }
1850
1851 return traverse;
1852}
1853
Robert Carr1f0a16a2016-10-24 16:27:39 -07001854/**
Robert Carrdb66e622017-04-10 16:55:57 -07001855 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001856 */
Dan Stoza412903f2017-04-27 13:42:17 -07001857void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001858 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1859 // produce a new list for traversing, including our relatives, and not including our children
1860 // who are relatives of another surface. In the case that there are no relative Z,
1861 // makeTraversalList returns our children directly to avoid significant overhead.
1862 // However in this case we need to take the responsibility for filtering children which
1863 // are relatives of another surface here.
1864 bool skipRelativeZUsers = false;
1865 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001866
Robert Carr1f0a16a2016-10-24 16:27:39 -07001867 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001868 for (; i < list.size(); i++) {
1869 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001870 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1871 continue;
1872 }
1873
chaviw301b1d82019-11-06 13:15:09 -08001874 if (relative->getZ(stateSet) >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001875 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001876 }
Dan Stoza412903f2017-04-27 13:42:17 -07001877 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001878 }
Robert Carr29abff82017-12-04 13:51:20 -08001879
Dan Stoza412903f2017-04-27 13:42:17 -07001880 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001881 for (; i < list.size(); i++) {
1882 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001883
Robert Carr29abff82017-12-04 13:51:20 -08001884 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1885 continue;
1886 }
Dan Stoza412903f2017-04-27 13:42:17 -07001887 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001888 }
1889}
1890
1891/**
Robert Carrdb66e622017-04-10 16:55:57 -07001892 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001893 */
Dan Stoza412903f2017-04-27 13:42:17 -07001894void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1895 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001896 // See traverseInZOrder for documentation.
1897 bool skipRelativeZUsers = false;
1898 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001899
Robert Carr1f0a16a2016-10-24 16:27:39 -07001900 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001901 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001902 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001903
1904 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1905 continue;
1906 }
1907
chaviw301b1d82019-11-06 13:15:09 -08001908 if (relative->getZ(stateSet) < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001909 break;
1910 }
Dan Stoza412903f2017-04-27 13:42:17 -07001911 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001912 }
Dan Stoza412903f2017-04-27 13:42:17 -07001913 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001914 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001915 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001916
1917 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1918 continue;
1919 }
1920
Dan Stoza412903f2017-04-27 13:42:17 -07001921 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001922 }
1923}
1924
Edgar Arriaga844fa672020-01-16 14:21:42 -08001925void Layer::traverse(LayerVector::StateSet state, const LayerVector::Visitor& visitor) {
1926 visitor(this);
1927 const LayerVector& children =
1928 state == LayerVector::StateSet::Drawing ? mDrawingChildren : mCurrentChildren;
1929 for (const sp<Layer>& child : children) {
1930 child->traverse(state, visitor);
1931 }
1932}
1933
chaviw4b129c22018-04-09 16:19:43 -07001934LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1935 const std::vector<Layer*>& layersInTree) {
1936 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1937 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001938 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1939 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001940 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001941
chaviwfd462612018-05-31 16:11:27 -07001942 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001943 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1944 sp<Layer> strongRelative = weakRelative.promote();
1945 // Only add relative layers that are also descendents of the top most parent of the tree.
1946 // If a relative layer is not a descendent, then it should be ignored.
1947 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1948 traverse.add(strongRelative);
1949 }
1950 }
1951
1952 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001953 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001954 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1955 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1956 // the child here since it won't be added later as a relative.
1957 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1958 childState.zOrderRelativeOf.promote().get())) {
1959 continue;
1960 }
1961 traverse.add(child);
1962 }
1963
1964 return traverse;
1965}
1966
1967void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1968 LayerVector::StateSet stateSet,
1969 const LayerVector::Visitor& visitor) {
1970 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001971
1972 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001973 for (; i < list.size(); i++) {
1974 const auto& relative = list[i];
chaviw301b1d82019-11-06 13:15:09 -08001975 if (relative->getZ(stateSet) >= 0) {
chaviwa76b2712017-09-20 12:02:26 -07001976 break;
1977 }
chaviw4b129c22018-04-09 16:19:43 -07001978 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001979 }
chaviw4b129c22018-04-09 16:19:43 -07001980
chaviwa76b2712017-09-20 12:02:26 -07001981 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001982 for (; i < list.size(); i++) {
1983 const auto& relative = list[i];
1984 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001985 }
1986}
1987
chaviw4b129c22018-04-09 16:19:43 -07001988std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1989 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1990 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1991
1992 std::vector<Layer*> layersInTree = {this};
1993 for (size_t i = 0; i < children.size(); i++) {
1994 const auto& child = children[i];
1995 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1996 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1997 }
1998
1999 return layersInTree;
2000}
2001
2002void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
2003 const LayerVector::Visitor& visitor) {
2004 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
2005 std::sort(layersInTree.begin(), layersInTree.end());
2006 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
2007}
2008
Peiyong Linefefaac2018-08-17 12:27:51 -07002009ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08002010 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002011}
2012
chaviw13fdc492017-06-27 12:40:18 -07002013half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08002014 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002015
chaviw13fdc492017-06-27 12:40:18 -07002016 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
2017 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07002018}
Robert Carr6452f122017-03-21 10:41:29 -07002019
Vishnu Nair6213bd92020-05-08 17:42:25 -07002020ui::Transform::RotationFlags Layer::getFixedTransformHint() const {
2021 ui::Transform::RotationFlags fixedTransformHint = mCurrentState.fixedTransformHint;
2022 if (fixedTransformHint != ui::Transform::ROT_INVALID) {
2023 return fixedTransformHint;
2024 }
2025 const auto& p = mCurrentParent.promote();
2026 if (!p) return fixedTransformHint;
2027 return p->getFixedTransformHint();
2028}
2029
chaviw13fdc492017-06-27 12:40:18 -07002030half4 Layer::getColor() const {
2031 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002032 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07002033}
Robert Carr6452f122017-03-21 10:41:29 -07002034
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08002035int32_t Layer::getBackgroundBlurRadius() const {
Galia Peychevada7de0e2020-12-03 17:24:35 +01002036 const auto& p = mDrawingParent.promote();
2037
2038 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
2039 return parentAlpha * getDrawingState().backgroundBlurRadius;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08002040}
2041
Galia Peychevae0acf382021-04-12 21:22:34 +02002042const std::vector<BlurRegion> Layer::getBlurRegions() const {
2043 auto regionsCopy(getDrawingState().blurRegions);
2044 int layerAlpha = getAlpha();
2045 for (auto& region : regionsCopy) {
2046 region.alpha = region.alpha * layerAlpha;
2047 }
2048 return regionsCopy;
Lucas Dupinc3800b82020-10-02 16:24:48 -07002049}
2050
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002051Layer::RoundedCornerState Layer::getRoundedCornerState() const {
2052 const auto& p = mDrawingParent.promote();
2053 if (p != nullptr) {
Peiyong Lin27016a92019-03-29 17:36:08 +00002054 RoundedCornerState parentState = p->getRoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002055 if (parentState.radius > 0) {
2056 ui::Transform t = getActiveTransform(getDrawingState());
2057 t = t.inverse();
2058 parentState.cropRect = t.transform(parentState.cropRect);
2059 // The rounded corners shader only accepts 1 corner radius for performance reasons,
2060 // but a transform matrix can define horizontal and vertical scales.
2061 // Let's take the average between both of them and pass into the shader, practically we
2062 // never do this type of transformation on windows anyway.
Lucas Dupineab7ea02020-06-03 17:27:28 -07002063 auto scaleX = sqrtf(t[0][0] * t[0][0] + t[0][1] * t[0][1]);
2064 auto scaleY = sqrtf(t[1][0] * t[1][0] + t[1][1] * t[1][1]);
2065 parentState.radius *= (scaleX + scaleY) / 2.0f;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002066 return parentState;
2067 }
2068 }
2069 const float radius = getDrawingState().cornerRadius;
Chavi Weingartena5aedbd2021-04-09 13:37:33 +00002070 return radius > 0 && getCroppedBufferSize(getDrawingState()).isValid()
2071 ? RoundedCornerState(getCroppedBufferSize(getDrawingState()).toFloatRect(), radius)
Peiyong Linb9ff23e2019-04-08 11:04:59 -07002072 : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002073}
2074
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02002075renderengine::ShadowSettings Layer::getShadowSettings(const Rect& layerStackRect) const {
Vishnu Nair08f6eae2019-11-26 14:01:39 -08002076 renderengine::ShadowSettings state = mFlinger->mDrawingState.globalShadowSettings;
2077
2078 // Shift the spot light x-position to the middle of the display and then
2079 // offset it by casting layer's screen pos.
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02002080 state.lightPos.x = (layerStackRect.width() / 2.f) - mScreenBounds.left;
Vishnu Nair08f6eae2019-11-26 14:01:39 -08002081 state.lightPos.y -= mScreenBounds.top;
2082
2083 state.length = mEffectiveShadowRadius;
2084 return state;
2085}
2086
Robert Carr1f0a16a2016-10-24 16:27:39 -07002087void Layer::commitChildList() {
2088 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
2089 const auto& child = mCurrentChildren[i];
2090 child->commitChildList();
2091 }
2092 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07002093 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002094}
2095
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002096static wp<Layer> extractLayerFromBinder(const wp<IBinder>& weakBinderHandle) {
2097 if (weakBinderHandle == nullptr) {
2098 return nullptr;
2099 }
2100 sp<IBinder> binderHandle = weakBinderHandle.promote();
2101 if (binderHandle == nullptr) {
2102 return nullptr;
2103 }
2104 sp<Layer::Handle> handle = static_cast<Layer::Handle*>(binderHandle.get());
2105 if (handle == nullptr) {
2106 return nullptr;
2107 }
2108 return handle->owner;
2109}
2110
Robert Carr720e5062018-07-30 17:45:14 -07002111void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002112 mCurrentState.inputInfo = info;
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002113 mCurrentState.touchableRegionCrop = extractLayerFromBinder(info.touchableRegionCropHandle);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002114 mCurrentState.modified = true;
2115 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07002116 setTransactionFlags(eTransactionNeeded);
2117}
2118
Alec Mouri6b9e9912020-01-21 10:50:24 -08002119LayerProto* Layer::writeToProto(LayersProto& layersProto, uint32_t traceFlags,
chaviw4c34a092020-07-08 11:30:06 -07002120 const DisplayDevice* display) {
chaviw08f3cb22020-01-13 13:17:21 -08002121 LayerProto* layerProto = layersProto.add_layers();
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002122 writeToProtoDrawingState(layerProto, traceFlags, display);
chaviw08f3cb22020-01-13 13:17:21 -08002123 writeToProtoCommonState(layerProto, LayerVector::StateSet::Drawing, traceFlags);
2124
Vishnu Nair60db8c02020-04-02 11:55:16 -07002125 if (traceFlags & SurfaceTracing::TRACE_COMPOSITION) {
2126 // Only populate for the primary display.
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002127 if (display) {
2128 const Hwc2::IComposerClient::Composition compositionType = getCompositionType(*display);
Vishnu Nair60db8c02020-04-02 11:55:16 -07002129 layerProto->set_hwc_composition_type(static_cast<HwcCompositionType>(compositionType));
2130 }
Alec Mouri6b9e9912020-01-21 10:50:24 -08002131 }
2132
chaviw08f3cb22020-01-13 13:17:21 -08002133 for (const sp<Layer>& layer : mDrawingChildren) {
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002134 layer->writeToProto(layersProto, traceFlags, display);
chaviw08f3cb22020-01-13 13:17:21 -08002135 }
chaviw6d89e2d2020-01-14 14:42:01 -08002136
2137 return layerProto;
chaviw08f3cb22020-01-13 13:17:21 -08002138}
2139
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002140void Layer::writeToProtoDrawingState(LayerProto* layerInfo, uint32_t traceFlags,
chaviw4c34a092020-07-08 11:30:06 -07002141 const DisplayDevice* display) {
Vishnu Nair6b7c5c92020-09-29 17:27:05 -07002142 const ui::Transform transform = getTransform();
Vishnu Nair8406fd72019-07-30 11:29:31 -07002143
2144 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
Vishnu Nair8406fd72019-07-30 11:29:31 -07002145
chaviwd62d3062019-09-04 14:48:02 -07002146 auto buffer = getBuffer();
Vishnu Nair8406fd72019-07-30 11:29:31 -07002147 if (buffer != nullptr) {
2148 LayerProtoHelper::writeToProto(buffer,
2149 [&]() { return layerInfo->mutable_active_buffer(); });
chaviw4244e032019-09-04 11:27:49 -07002150 LayerProtoHelper::writeToProto(ui::Transform(getBufferTransform()),
Vishnu Nair8406fd72019-07-30 11:29:31 -07002151 layerInfo->mutable_buffer_transform());
2152 }
2153 layerInfo->set_invalidate(contentDirty);
2154 layerInfo->set_is_protected(isProtected());
chaviw4244e032019-09-04 11:27:49 -07002155 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(getDataSpace())));
Vishnu Nair8406fd72019-07-30 11:29:31 -07002156 layerInfo->set_queued_frames(getQueuedFrameCount());
2157 layerInfo->set_refresh_pending(isBufferLatched());
2158 layerInfo->set_curr_frame(mCurrentFrameNumber);
2159 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
2160
2161 layerInfo->set_corner_radius(getRoundedCornerState().radius);
Ana Krulece766cc82020-04-27 13:49:13 -07002162 layerInfo->set_background_blur_radius(getBackgroundBlurRadius());
Vishnu Nair8406fd72019-07-30 11:29:31 -07002163 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
2164 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
2165 [&]() { return layerInfo->mutable_position(); });
2166 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
Vishnu Nair60db8c02020-04-02 11:55:16 -07002167 if (traceFlags & SurfaceTracing::TRACE_COMPOSITION) {
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002168 LayerProtoHelper::writeToProto(getVisibleRegion(display),
Vishnu Nair60db8c02020-04-02 11:55:16 -07002169 [&]() { return layerInfo->mutable_visible_region(); });
2170 }
Vishnu Nair8406fd72019-07-30 11:29:31 -07002171 LayerProtoHelper::writeToProto(surfaceDamageRegion,
2172 [&]() { return layerInfo->mutable_damage_region(); });
chaviwddeae262020-01-06 10:31:23 -08002173
2174 if (hasColorTransform()) {
2175 LayerProtoHelper::writeToProto(getColorTransform(),
2176 layerInfo->mutable_color_transform());
2177 }
Vishnu Nair8406fd72019-07-30 11:29:31 -07002178 }
2179
Vishnu Nair60db8c02020-04-02 11:55:16 -07002180 LayerProtoHelper::writeToProto(mSourceBounds,
2181 [&]() { return layerInfo->mutable_source_bounds(); });
2182 LayerProtoHelper::writeToProto(mScreenBounds,
2183 [&]() { return layerInfo->mutable_screen_bounds(); });
2184 LayerProtoHelper::writeToProto(getRoundedCornerState().cropRect,
2185 [&]() { return layerInfo->mutable_corner_radius_crop(); });
2186 layerInfo->set_shadow_radius(mEffectiveShadowRadius);
Vishnu Nair8406fd72019-07-30 11:29:31 -07002187}
2188
2189void Layer::writeToProtoCommonState(LayerProto* layerInfo, LayerVector::StateSet stateSet,
chaviw4c34a092020-07-08 11:30:06 -07002190 uint32_t traceFlags) {
chaviw1d044282017-09-27 12:19:28 -07002191 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2192 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002193 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07002194
chaviw766c9c52021-02-10 17:36:47 -08002195 ui::Transform requestedTransform = state.transform;
chaviw1d044282017-09-27 12:19:28 -07002196
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002197 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
2198 layerInfo->set_id(sequence);
2199 layerInfo->set_name(getName().c_str());
chaviw8a01fa42019-08-19 12:39:31 -07002200 layerInfo->set_type(getType());
chaviw1d044282017-09-27 12:19:28 -07002201
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002202 for (const auto& child : children) {
2203 layerInfo->add_children(child->sequence);
chaviw1d044282017-09-27 12:19:28 -07002204 }
chaviw1d044282017-09-27 12:19:28 -07002205
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002206 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
2207 sp<Layer> strongRelative = weakRelative.promote();
2208 if (strongRelative != nullptr) {
2209 layerInfo->add_relatives(strongRelative->sequence);
2210 }
chaviwadc40c22018-07-10 16:57:27 -07002211 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002212
2213 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
2214 [&]() { return layerInfo->mutable_transparent_region(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002215
2216 layerInfo->set_layer_stack(getLayerStack());
2217 layerInfo->set_z(state.z);
2218
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002219 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(),
2220 [&]() {
2221 return layerInfo->mutable_requested_position();
2222 });
2223
chaviw766c9c52021-02-10 17:36:47 -08002224 LayerProtoHelper::writeSizeToProto(state.width, state.height,
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002225 [&]() { return layerInfo->mutable_size(); });
2226
chaviw766c9c52021-02-10 17:36:47 -08002227 LayerProtoHelper::writeToProto(state.crop, [&]() { return layerInfo->mutable_crop(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002228
2229 layerInfo->set_is_opaque(isOpaque(state));
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002230
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002231
2232 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
2233 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
2234 LayerProtoHelper::writeToProto(state.color,
2235 [&]() { return layerInfo->mutable_requested_color(); });
2236 layerInfo->set_flags(state.flags);
2237
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002238 LayerProtoHelper::writeToProto(requestedTransform,
2239 layerInfo->mutable_requested_transform());
2240
2241 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
2242 if (parent != nullptr) {
2243 layerInfo->set_parent(parent->sequence);
2244 } else {
2245 layerInfo->set_parent(-1);
2246 }
2247
2248 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2249 if (zOrderRelativeOf != nullptr) {
2250 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
2251 } else {
2252 layerInfo->set_z_order_relative_of(-1);
2253 }
chaviw08f3cb22020-01-13 13:17:21 -08002254
2255 layerInfo->set_is_relative_of(state.isRelativeOf);
chaviw250bcbb2020-08-05 11:17:54 -07002256
2257 layerInfo->set_owner_uid(mOwnerUid);
chaviwadc40c22018-07-10 16:57:27 -07002258 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002259
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002260 if (traceFlags & SurfaceTracing::TRACE_INPUT) {
chaviw4c34a092020-07-08 11:30:06 -07002261 InputWindowInfo info;
2262 if (useDrawing) {
[1;3C2b9fc252021-02-04 16:16:50 -08002263 info = fillInputInfo({nullptr});
chaviw4c34a092020-07-08 11:30:06 -07002264 } else {
2265 info = state.inputInfo;
2266 }
2267
2268 LayerProtoHelper::writeToProto(info, state.touchableRegionCrop,
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002269 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002270 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002271
2272 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
2273 auto protoMap = layerInfo->mutable_metadata();
2274 for (const auto& entry : state.metadata.mMap) {
2275 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
2276 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002277 }
chaviw1d044282017-09-27 12:19:28 -07002278}
2279
Robert Carr2e102c92018-10-23 12:11:15 -07002280bool Layer::isRemovedFromCurrentState() const {
2281 return mRemovedFromCurrentState;
2282}
2283
chaviw39d01472021-04-08 14:26:24 -05002284ui::Transform Layer::getInputTransform() const {
2285 return getTransform();
2286}
2287
2288Rect Layer::getInputBounds() const {
2289 return getCroppedBufferSize(getDrawingState());
2290}
2291
[1;3C2b9fc252021-02-04 16:16:50 -08002292void Layer::fillInputFrameInfo(InputWindowInfo& info, const ui::Transform& toPhysicalDisplay) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002293 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002294 // If this is a portal window, set the touchableRegion to the layerBounds.
2295 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
chaviw39d01472021-04-08 14:26:24 -05002296 ? getInputBounds()
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002297 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002298 if (!layerBounds.isValid()) {
chaviw39d01472021-04-08 14:26:24 -05002299 layerBounds = getInputBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002300 }
chaviw1ff3d1e2020-07-01 15:53:47 -07002301
chaviw7e72caf2020-12-02 16:50:43 -08002302 if (!layerBounds.isValid()) {
2303 // If the layer bounds is empty, set the frame to empty and clear the transform
2304 info.frameLeft = 0;
2305 info.frameTop = 0;
2306 info.frameRight = 0;
2307 info.frameBottom = 0;
2308 info.transform.reset();
2309 return;
2310 }
2311
chaviw39d01472021-04-08 14:26:24 -05002312 ui::Transform layerToDisplay = getInputTransform();
[1;3C2b9fc252021-02-04 16:16:50 -08002313 // Transform that takes window coordinates to unrotated display coordinates
2314 ui::Transform t = toPhysicalDisplay * layerToDisplay;
chaviw7e72caf2020-12-02 16:50:43 -08002315 int32_t xSurfaceInset = info.surfaceInset;
2316 int32_t ySurfaceInset = info.surfaceInset;
[1;3C2b9fc252021-02-04 16:16:50 -08002317 // Bring screenBounds into unrotated space
2318 Rect screenBounds = toPhysicalDisplay.transform(Rect{mScreenBounds});
chaviw7e72caf2020-12-02 16:50:43 -08002319
chaviw1ff3d1e2020-07-01 15:53:47 -07002320 const float xScale = t.getScaleX();
2321 const float yScale = t.getScaleY();
2322 if (xScale != 1.0f || yScale != 1.0f) {
chaviw1ff3d1e2020-07-01 15:53:47 -07002323 xSurfaceInset = std::round(xSurfaceInset * xScale);
2324 ySurfaceInset = std::round(ySurfaceInset * yScale);
2325 }
2326
Peiyong Lin74861ad2020-10-14 11:55:33 -07002327 // Transform the layer bounds from layer coordinate space to display coordinate space.
chaviw44a6d2b2020-09-08 17:14:16 -07002328 Rect transformedLayerBounds = t.transform(layerBounds);
Ady Abraham282f1d72019-07-24 18:05:56 -07002329
2330 // clamp inset to layer bounds
chaviw44a6d2b2020-09-08 17:14:16 -07002331 xSurfaceInset = (xSurfaceInset >= 0)
2332 ? std::min(xSurfaceInset, transformedLayerBounds.getWidth() / 2)
2333 : 0;
2334 ySurfaceInset = (ySurfaceInset >= 0)
2335 ? std::min(ySurfaceInset, transformedLayerBounds.getHeight() / 2)
2336 : 0;
Ady Abraham282f1d72019-07-24 18:05:56 -07002337
Robert Carra7242302020-09-01 18:26:29 -07002338 // inset while protecting from overflow TODO(b/161235021): What is going wrong
2339 // in the overflow scenario?
2340 {
2341 int32_t tmp;
chaviw44a6d2b2020-09-08 17:14:16 -07002342 if (!__builtin_add_overflow(transformedLayerBounds.left, xSurfaceInset, &tmp))
2343 transformedLayerBounds.left = tmp;
2344 if (!__builtin_sub_overflow(transformedLayerBounds.right, xSurfaceInset, &tmp))
2345 transformedLayerBounds.right = tmp;
2346 if (!__builtin_add_overflow(transformedLayerBounds.top, ySurfaceInset, &tmp))
2347 transformedLayerBounds.top = tmp;
2348 if (!__builtin_sub_overflow(transformedLayerBounds.bottom, ySurfaceInset, &tmp))
2349 transformedLayerBounds.bottom = tmp;
Robert Carra7242302020-09-01 18:26:29 -07002350 }
Vishnu Nair8033a492018-12-05 07:27:23 -08002351
chaviw44a6d2b2020-09-08 17:14:16 -07002352 // Compute the correct transform to send to input. This will allow it to transform the
2353 // input coordinates from display space into window space. Therefore, it needs to use the
2354 // final layer frame to create the inverse transform. Since surface insets are added later,
2355 // along with the overflow, the best way to ensure we get the correct transform is to use
2356 // the final frame calculated.
2357 // 1. Take the original transform set on the window and get the inverse transform. This is
2358 // used to get the final bounds in display space (ignorning the transform). Apply the
Peiyong Lin74861ad2020-10-14 11:55:33 -07002359 // inverse transform on the layerBounds to get the untransformed frame (in layer space)
chaviw44a6d2b2020-09-08 17:14:16 -07002360 // 2. Take the top and left of the untransformed frame to get the real position on screen.
2361 // Apply the layer transform on top/left so it includes any scale or rotation. These will
2362 // be the new translation values for the transform.
2363 // 3. Update the translation of the original transform to the new translation values.
2364 // 4. Send the inverse transform to input so the coordinates can be transformed back into
2365 // window space.
2366 ui::Transform inverseTransform = t.inverse();
2367 Rect nonTransformedBounds = inverseTransform.transform(transformedLayerBounds);
2368 vec2 translation = t.transform(nonTransformedBounds.left, nonTransformedBounds.top);
chaviw1ff3d1e2020-07-01 15:53:47 -07002369 ui::Transform inputTransform(t);
chaviw44a6d2b2020-09-08 17:14:16 -07002370 inputTransform.set(translation.x, translation.y);
chaviw1ff3d1e2020-07-01 15:53:47 -07002371 info.transform = inputTransform.inverse();
2372
chaviw39cfa2e2020-11-04 14:19:02 -08002373 // We need to send the layer bounds cropped to the screenbounds since the layer can be cropped.
2374 // The frame should be the area the user sees on screen since it's used for occlusion
2375 // detection.
chaviw39cfa2e2020-11-04 14:19:02 -08002376 transformedLayerBounds.intersect(screenBounds, &transformedLayerBounds);
2377 info.frameLeft = transformedLayerBounds.left;
2378 info.frameTop = transformedLayerBounds.top;
2379 info.frameRight = transformedLayerBounds.right;
2380 info.frameBottom = transformedLayerBounds.bottom;
2381
Vishnu Nair8033a492018-12-05 07:27:23 -08002382 // Position the touchable region relative to frame screen location and restrict it to frame
2383 // bounds.
chaviw44a6d2b2020-09-08 17:14:16 -07002384 info.touchableRegion = inputTransform.transform(info.touchableRegion);
chaviw7e72caf2020-12-02 16:50:43 -08002385}
2386
[1;3C2b9fc252021-02-04 16:16:50 -08002387InputWindowInfo Layer::fillInputInfo(const sp<DisplayDevice>& display) {
chaviw7e72caf2020-12-02 16:50:43 -08002388 if (!hasInputInfo()) {
2389 mDrawingState.inputInfo.name = getName();
2390 mDrawingState.inputInfo.ownerUid = mOwnerUid;
2391 mDrawingState.inputInfo.ownerPid = mOwnerPid;
2392 mDrawingState.inputInfo.inputFeatures = InputWindowInfo::Feature::NO_INPUT_CHANNEL;
2393 mDrawingState.inputInfo.flags = InputWindowInfo::Flag::NOT_TOUCH_MODAL;
2394 mDrawingState.inputInfo.displayId = getLayerStack();
2395 }
2396
2397 InputWindowInfo info = mDrawingState.inputInfo;
2398 info.id = sequence;
2399
2400 if (info.displayId == ADISPLAY_ID_NONE) {
2401 info.displayId = getLayerStack();
2402 }
2403
[1;3C2b9fc252021-02-04 16:16:50 -08002404 // Transform that goes from "logical(rotated)" display to physical/unrotated display.
2405 // This is for when inputflinger operates in physical display-space.
2406 ui::Transform toPhysicalDisplay;
2407 if (display) {
2408 toPhysicalDisplay = display->getTransform();
Evan Rosky84f07f02021-04-16 10:42:42 -07002409 info.displayWidth = display->getWidth();
2410 info.displayHeight = display->getHeight();
[1;3C2b9fc252021-02-04 16:16:50 -08002411 }
2412 fillInputFrameInfo(info, toPhysicalDisplay);
chaviw7e72caf2020-12-02 16:50:43 -08002413
Robert Carr5dc426e2020-06-10 14:29:14 -07002414 // For compatibility reasons we let layers which can receive input
2415 // receive input before they have actually submitted a buffer. Because
2416 // of this we use canReceiveInput instead of isVisible to check the
2417 // policy-visibility, ignoring the buffer state. However for layers with
2418 // hasInputInfo()==false we can use the real visibility state.
2419 // We are just using these layers for occlusion detection in
2420 // InputDispatcher, and obviously if they aren't visible they can't occlude
2421 // anything.
2422 info.visible = hasInputInfo() ? canReceiveInput() : isVisible();
Bernardo Rufinoea97d182020-08-19 14:43:14 +01002423 info.alpha = getAlpha();
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002424
2425 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2426 if (info.replaceTouchableRegionWithCrop) {
2427 if (cropLayer == nullptr) {
[1;3C2b9fc252021-02-04 16:16:50 -08002428 info.touchableRegion = Region(toPhysicalDisplay.transform(Rect{mScreenBounds}));
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002429 } else {
[1;3C2b9fc252021-02-04 16:16:50 -08002430 info.touchableRegion =
2431 Region(toPhysicalDisplay.transform(Rect{cropLayer->mScreenBounds}));
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002432 }
2433 } else if (cropLayer != nullptr) {
[1;3C2b9fc252021-02-04 16:16:50 -08002434 info.touchableRegion = info.touchableRegion.intersect(
2435 toPhysicalDisplay.transform(Rect{cropLayer->mScreenBounds}));
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002436 }
2437
chaviwaf87b3e2019-10-01 16:59:28 -07002438 // If the layer is a clone, we need to crop the input region to cloned root to prevent
2439 // touches from going outside the cloned area.
2440 if (isClone()) {
2441 sp<Layer> clonedRoot = getClonedRoot();
2442 if (clonedRoot != nullptr) {
[1;3C2b9fc252021-02-04 16:16:50 -08002443 Rect rect = toPhysicalDisplay.transform(Rect{clonedRoot->mScreenBounds});
chaviwaf87b3e2019-10-01 16:59:28 -07002444 info.touchableRegion = info.touchableRegion.intersect(rect);
2445 }
2446 }
2447
Robert Carr720e5062018-07-30 17:45:14 -07002448 return info;
2449}
2450
chaviwaf87b3e2019-10-01 16:59:28 -07002451sp<Layer> Layer::getClonedRoot() {
2452 if (mClonedChild != nullptr) {
2453 return this;
2454 }
2455 if (mDrawingParent == nullptr || mDrawingParent.promote() == nullptr) {
2456 return nullptr;
2457 }
2458 return mDrawingParent.promote()->getClonedRoot();
2459}
2460
Robert Carredd13602020-04-13 17:24:34 -07002461bool Layer::hasInputInfo() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002462 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002463}
2464
chaviw3e727cd2019-01-31 13:41:05 -08002465bool Layer::canReceiveInput() const {
Vishnu Nair82353e92019-09-12 12:38:47 -07002466 return !isHiddenByPolicy();
chaviw3e727cd2019-01-31 13:41:05 -08002467}
2468
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002469compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002470 const DisplayDevice* display) const {
2471 if (!display) return nullptr;
Lloyd Piquede196652020-01-22 17:29:58 -08002472 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionEngineLayerFE());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002473}
2474
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002475Region Layer::getVisibleRegion(const DisplayDevice* display) const {
2476 const auto outputLayer = findOutputLayerForDisplay(display);
2477 return outputLayer ? outputLayer->getState().visibleRegion : Region();
Lloyd Piquea2468662019-03-07 21:31:06 -08002478}
2479
chaviwb4c6e582019-08-16 14:35:07 -07002480void Layer::setInitialValuesForClone(const sp<Layer>& clonedFrom) {
2481 // copy drawing state from cloned layer
2482 mDrawingState = clonedFrom->mDrawingState;
2483 mClonedFrom = clonedFrom;
chaviwb4c6e582019-08-16 14:35:07 -07002484}
chaviw74b03172019-08-19 11:09:03 -07002485
2486void Layer::updateMirrorInfo() {
2487 if (mClonedChild == nullptr || !mClonedChild->isClonedFromAlive()) {
2488 // If mClonedChild is null, there is nothing to mirror. If isClonedFromAlive returns false,
2489 // it means that there is a clone, but the layer it was cloned from has been destroyed. In
2490 // that case, we want to delete the reference to the clone since we want it to get
2491 // destroyed. The root, this layer, will still be around since the client can continue
2492 // to hold a reference, but no cloned layers will be displayed.
2493 mClonedChild = nullptr;
2494 return;
2495 }
2496
2497 std::map<sp<Layer>, sp<Layer>> clonedLayersMap;
2498 // If the real layer exists and is in current state, add the clone as a child of the root.
2499 // There's no need to remove from drawingState when the layer is offscreen since currentState is
2500 // copied to drawingState for the root layer. So the clonedChild is always removed from
2501 // drawingState and then needs to be added back each traversal.
2502 if (!mClonedChild->getClonedFrom()->isRemovedFromCurrentState()) {
2503 addChildToDrawing(mClonedChild);
2504 }
2505
2506 mClonedChild->updateClonedDrawingState(clonedLayersMap);
2507 mClonedChild->updateClonedChildren(this, clonedLayersMap);
2508 mClonedChild->updateClonedRelatives(clonedLayersMap);
2509}
2510
2511void Layer::updateClonedDrawingState(std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2512 // If the layer the clone was cloned from is alive, copy the content of the drawingState
2513 // to the clone. If the real layer is no longer alive, continue traversing the children
2514 // since we may be able to pull out other children that are still alive.
2515 if (isClonedFromAlive()) {
2516 sp<Layer> clonedFrom = getClonedFrom();
2517 mDrawingState = clonedFrom->mDrawingState;
chaviw74b03172019-08-19 11:09:03 -07002518 clonedLayersMap.emplace(clonedFrom, this);
2519 }
2520
2521 // The clone layer may have children in drawingState since they may have been created and
2522 // added from a previous request to updateMirorInfo. This is to ensure we don't recreate clones
2523 // that already exist, since we can just re-use them.
2524 // The drawingChildren will not get overwritten by the currentChildren since the clones are
2525 // not updated in the regular traversal. They are skipped since the root will lose the
2526 // reference to them when it copies its currentChildren to drawing.
2527 for (sp<Layer>& child : mDrawingChildren) {
2528 child->updateClonedDrawingState(clonedLayersMap);
2529 }
2530}
2531
2532void Layer::updateClonedChildren(const sp<Layer>& mirrorRoot,
2533 std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2534 mDrawingChildren.clear();
2535
2536 if (!isClonedFromAlive()) {
2537 return;
2538 }
2539
2540 sp<Layer> clonedFrom = getClonedFrom();
2541 for (sp<Layer>& child : clonedFrom->mDrawingChildren) {
2542 if (child == mirrorRoot) {
2543 // This is to avoid cyclical mirroring.
2544 continue;
2545 }
2546 sp<Layer> clonedChild = clonedLayersMap[child];
2547 if (clonedChild == nullptr) {
2548 clonedChild = child->createClone();
2549 clonedLayersMap[child] = clonedChild;
2550 }
2551 addChildToDrawing(clonedChild);
2552 clonedChild->updateClonedChildren(mirrorRoot, clonedLayersMap);
2553 }
2554}
2555
chaviwaf87b3e2019-10-01 16:59:28 -07002556void Layer::updateClonedInputInfo(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2557 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2558 if (cropLayer != nullptr) {
2559 if (clonedLayersMap.count(cropLayer) == 0) {
2560 // Real layer had a crop layer but it's not in the cloned hierarchy. Just set to
2561 // self as crop layer to avoid going outside bounds.
2562 mDrawingState.touchableRegionCrop = this;
2563 } else {
2564 const sp<Layer>& clonedCropLayer = clonedLayersMap.at(cropLayer);
2565 mDrawingState.touchableRegionCrop = clonedCropLayer;
2566 }
2567 }
2568 // Cloned layers shouldn't handle watch outside since their z order is not determined by
2569 // WM or the client.
Michael Wright44753b12020-07-08 13:48:11 +01002570 mDrawingState.inputInfo.flags &= ~InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH;
chaviwaf87b3e2019-10-01 16:59:28 -07002571}
2572
2573void Layer::updateClonedRelatives(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
chaviw74b03172019-08-19 11:09:03 -07002574 mDrawingState.zOrderRelativeOf = nullptr;
2575 mDrawingState.zOrderRelatives.clear();
2576
2577 if (!isClonedFromAlive()) {
2578 return;
2579 }
2580
chaviwaf87b3e2019-10-01 16:59:28 -07002581 const sp<Layer>& clonedFrom = getClonedFrom();
chaviw74b03172019-08-19 11:09:03 -07002582 for (wp<Layer>& relativeWeak : clonedFrom->mDrawingState.zOrderRelatives) {
chaviwaf87b3e2019-10-01 16:59:28 -07002583 const sp<Layer>& relative = relativeWeak.promote();
2584 if (clonedLayersMap.count(relative) > 0) {
2585 auto& clonedRelative = clonedLayersMap.at(relative);
chaviw74b03172019-08-19 11:09:03 -07002586 mDrawingState.zOrderRelatives.add(clonedRelative);
2587 }
2588 }
2589
2590 // Check if the relativeLayer for the real layer is part of the cloned hierarchy.
2591 // It's possible that the layer it's relative to is outside the requested cloned hierarchy.
2592 // In that case, we treat the layer as if the relativeOf has been removed. This way, it will
2593 // still traverse the children, but the layer with the missing relativeOf will not be shown
2594 // on screen.
chaviwaf87b3e2019-10-01 16:59:28 -07002595 const sp<Layer>& relativeOf = clonedFrom->mDrawingState.zOrderRelativeOf.promote();
2596 if (clonedLayersMap.count(relativeOf) > 0) {
2597 const sp<Layer>& clonedRelativeOf = clonedLayersMap.at(relativeOf);
chaviw74b03172019-08-19 11:09:03 -07002598 mDrawingState.zOrderRelativeOf = clonedRelativeOf;
2599 }
2600
chaviwaf87b3e2019-10-01 16:59:28 -07002601 updateClonedInputInfo(clonedLayersMap);
2602
chaviw74b03172019-08-19 11:09:03 -07002603 for (sp<Layer>& child : mDrawingChildren) {
2604 child->updateClonedRelatives(clonedLayersMap);
2605 }
2606}
2607
2608void Layer::addChildToDrawing(const sp<Layer>& layer) {
2609 mDrawingChildren.add(layer);
2610 layer->mDrawingParent = this;
2611}
2612
Steven Thomas62a4cf82020-01-31 12:04:03 -08002613Layer::FrameRateCompatibility Layer::FrameRate::convertCompatibility(int8_t compatibility) {
2614 switch (compatibility) {
2615 case ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT:
2616 return FrameRateCompatibility::Default;
2617 case ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE:
2618 return FrameRateCompatibility::ExactOrMultiple;
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08002619 case ANATIVEWINDOW_FRAME_RATE_EXACT:
2620 return FrameRateCompatibility::Exact;
Steven Thomas62a4cf82020-01-31 12:04:03 -08002621 default:
2622 LOG_ALWAYS_FATAL("Invalid frame rate compatibility value %d", compatibility);
2623 return FrameRateCompatibility::Default;
2624 }
2625}
2626
Marin Shalamanovc5986772021-03-16 16:09:49 +01002627scheduler::Seamlessness Layer::FrameRate::convertChangeFrameRateStrategy(int8_t strategy) {
2628 switch (strategy) {
2629 case ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS:
2630 return Seamlessness::OnlySeamless;
2631 case ANATIVEWINDOW_CHANGE_FRAME_RATE_ALWAYS:
2632 return Seamlessness::SeamedAndSeamless;
2633 default:
2634 LOG_ALWAYS_FATAL("Invalid change frame sate strategy value %d", strategy);
2635 return Seamlessness::Default;
2636 }
2637}
2638
chaviwc5676c62020-09-18 15:01:04 -07002639bool Layer::getPrimaryDisplayOnly() const {
2640 const State& s(mDrawingState);
2641 if (s.flags & layer_state_t::eLayerSkipScreenshot) {
2642 return true;
2643 }
2644
2645 sp<Layer> parent = mDrawingParent.promote();
2646 return parent == nullptr ? false : parent->getPrimaryDisplayOnly();
2647}
2648
Mathias Agopian13127d82013-03-05 17:47:11 -08002649// ---------------------------------------------------------------------------
2650
Marin Shalamanov46084422020-10-13 12:33:42 +02002651std::ostream& operator<<(std::ostream& stream, const Layer::FrameRate& rate) {
2652 return stream << "{rate=" << rate.rate
2653 << " type=" << Layer::frameRateCompatibilityString(rate.type)
Marin Shalamanov53fc11d2020-11-20 14:00:13 +01002654 << " seamlessness=" << toString(rate.seamlessness) << "}";
Marin Shalamanov46084422020-10-13 12:33:42 +02002655}
2656
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002657}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002658
2659#if defined(__gl_h_)
2660#error "don't include gl/gl.h in this file"
2661#endif
2662
2663#if defined(__gl2_h_)
2664#error "don't include gl2/gl2.h in this file"
2665#endif
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -08002666
2667// TODO(b/129481165): remove the #pragma below and fix conversion issues
2668#pragma clang diagnostic pop // ignored "-Wconversion"