blob: 7707aaf624525f9feb3e3a8da9d36dab5e33758b [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
20
Dan Stoza9e56aa02015-11-02 13:00:03 -080021//#define LOG_NDEBUG 0
22#undef LOG_TAG
23#define LOG_TAG "Layer"
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080024#define ATRACE_TAG ATRACE_TAG_GRAPHICS
25
Alec Mourie60041e2019-06-14 18:59:51 -070026#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027
[1;3C2b9fc252021-02-04 16:16:50 -080028#include <android-base/properties.h>
Yiwei Zhang5434a782018-12-05 18:06:32 -080029#include <android-base/stringprintf.h>
Steven Thomas62a4cf82020-01-31 12:04:03 -080030#include <android/native_window.h>
Vishnu Nair0f085c62019-08-30 08:49:12 -070031#include <binder/IPCThreadState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080032#include <compositionengine/Display.h>
Lloyd Piquea83776c2019-01-29 18:42:32 -080033#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080034#include <compositionengine/OutputLayer.h>
35#include <compositionengine/impl/OutputLayerCompositionState.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070036#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070037#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070038#include <cutils/properties.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080039#include <gui/BufferItem.h>
40#include <gui/LayerDebugInfo.h>
41#include <gui/Surface.h>
Alec Mourie60041e2019-06-14 18:59:51 -070042#include <math.h>
chaviw250bcbb2020-08-05 11:17:54 -070043#include <private/android_filesystem_config.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080044#include <renderengine/RenderEngine.h>
Alec Mourie60041e2019-06-14 18:59:51 -070045#include <stdint.h>
46#include <stdlib.h>
47#include <sys/types.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080048#include <ui/DebugUtils.h>
49#include <ui/GraphicBuffer.h>
50#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051#include <utils/Errors.h>
52#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080053#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080055#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056
Alec Mourie60041e2019-06-14 18:59:51 -070057#include <algorithm>
58#include <mutex>
59#include <sstream>
60
Yiwei Zhang60d1a192018-03-07 14:52:28 -080061#include "BufferLayer.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020062#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070063#include "DisplayDevice.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080064#include "DisplayHardware/HWComposer.h"
Vishnu Nairfa247b12020-02-11 08:58:26 -080065#include "EffectLayer.h"
Ady Abraham22c7b5c2020-09-22 19:33:40 -070066#include "FrameTimeline.h"
Mikael Pessa90092f42019-08-26 17:22:04 -070067#include "FrameTracer/FrameTracer.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080068#include "LayerProtoHelper.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070069#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070070#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071#include "SurfaceFlinger.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080072#include "TimeStats/TimeStats.h"
Michael Wright44753b12020-07-08 13:48:11 +010073#include "input/InputWindow.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070074
David Sodman41fdfc92017-11-06 16:09:56 -080075#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080076
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077namespace android {
Marin Shalamanov1876e2e2020-12-04 13:23:59 +010078namespace {
79constexpr int kDumpTableRowLength = 159;
80} // namespace
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081
Yiwei Zhang5434a782018-12-05 18:06:32 -080082using base::StringAppendF;
Michael Wright44753b12020-07-08 13:48:11 +010083using namespace android::flag_operators;
Ady Abraham22c7b5c2020-09-22 19:33:40 -070084using PresentState = frametimeline::SurfaceFrame::PresentState;
Yiwei Zhang5434a782018-12-05 18:06:32 -080085
Lloyd Piquef1c675b2018-09-12 20:45:39 -070086std::atomic<int32_t> Layer::sSequence{1};
Mathias Agopian13127d82013-03-05 17:47:11 -080087
Lloyd Pique42ab75e2018-09-12 20:46:03 -070088Layer::Layer(const LayerCreationArgs& args)
Ady Abraham8f1ee7f2019-04-05 10:32:50 -070089 : mFlinger(args.flinger),
90 mName(args.name),
91 mClientRef(args.client),
Michael Wright44753b12020-07-08 13:48:11 +010092 mWindowType(static_cast<InputWindowInfo::Type>(
93 args.metadata.getInt32(METADATA_WINDOW_TYPE, 0))) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -070094 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -070095 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
96 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
97 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
chaviwc5676c62020-09-18 15:01:04 -070098 if (args.flags & ISurfaceComposerClient::eSkipScreenshot)
99 layerFlags |= layer_state_t::eLayerSkipScreenshot;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700100
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800101 mCurrentState.active_legacy.w = args.w;
102 mCurrentState.active_legacy.h = args.h;
103 mCurrentState.flags = layerFlags;
104 mCurrentState.active_legacy.transform.set(0, 0);
chaviw25714502021-02-11 10:01:08 -0800105 mCurrentState.crop.makeInvalid();
106 mCurrentState.requestedCrop = mCurrentState.crop;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800107 mCurrentState.z = 0;
108 mCurrentState.color.a = 1.0f;
109 mCurrentState.layerStack = 0;
110 mCurrentState.sequence = 0;
111 mCurrentState.requested_legacy = mCurrentState.active_legacy;
chaviw766c9c52021-02-10 17:36:47 -0800112 mCurrentState.width = UINT32_MAX;
113 mCurrentState.height = UINT32_MAX;
114 mCurrentState.transform.set(0, 0);
Valerie Hau134651a2020-01-28 16:21:22 -0800115 mCurrentState.frameNumber = 0;
chaviw766c9c52021-02-10 17:36:47 -0800116 mCurrentState.bufferTransform = 0;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800117 mCurrentState.transformToDisplayInverse = false;
118 mCurrentState.crop.makeInvalid();
Ady Abraham6c1b7ac2021-03-31 16:56:03 -0700119 mCurrentState.acquireFence = sp<Fence>::make(-1);
120 mCurrentState.acquireFenceTime = std::make_shared<FenceTime>(mCurrentState.acquireFence);
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800121 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
122 mCurrentState.hdrMetadata.validTypes = 0;
Marissa Wall7e0a01f2019-08-14 14:29:25 -0700123 mCurrentState.surfaceDamageRegion = Region::INVALID_REGION;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800124 mCurrentState.cornerRadius = 0.0f;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800125 mCurrentState.backgroundBlurRadius = 0;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800126 mCurrentState.api = -1;
127 mCurrentState.hasColorTransform = false;
Peiyong Linc502cb72019-03-01 15:00:23 -0800128 mCurrentState.colorSpaceAgnostic = false;
Ana Krulecc84d09b2019-11-02 23:10:29 +0100129 mCurrentState.frameRateSelectionPriority = PRIORITY_UNSET;
Evan Roskya1f1e152019-01-24 16:17:46 -0800130 mCurrentState.metadata = args.metadata;
Vishnu Nairf37dcfb2019-11-16 07:37:09 -0800131 mCurrentState.shadowRadius = 0.f;
Ady Abraham60e42ea2020-03-09 19:17:31 -0700132 mCurrentState.treeHasFrameRateVote = false;
Vishnu Nair6213bd92020-05-08 17:42:25 -0700133 mCurrentState.fixedTransformHint = ui::Transform::ROT_INVALID;
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000134 mCurrentState.frameTimelineInfo = {};
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700135 mCurrentState.postTime = -1;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700136
Vishnu Nairc43a23c2020-05-29 14:32:27 -0700137 if (args.flags & ISurfaceComposerClient::eNoColorFill) {
138 // Set an invalid color so there is no color fill.
139 mCurrentState.color.r = -1.0_hf;
140 mCurrentState.color.g = -1.0_hf;
141 mCurrentState.color.b = -1.0_hf;
142 }
143
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700144 // drawing state & current state are identical
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800145 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700146
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800147 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700148 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800149 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200150 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700151
Vishnu Nair0f085c62019-08-30 08:49:12 -0700152 mCallingPid = args.callingPid;
153 mCallingUid = args.callingUid;
chaviw250bcbb2020-08-05 11:17:54 -0700154
155 if (mCallingUid == AID_GRAPHICS || mCallingUid == AID_SYSTEM) {
156 // If the system didn't send an ownerUid, use the callingUid for the ownerUid.
157 mOwnerUid = args.metadata.getInt32(METADATA_OWNER_UID, mCallingUid);
Adithya Srinivasan9febda82020-10-19 10:49:41 -0700158 mOwnerPid = args.metadata.getInt32(METADATA_OWNER_PID, mCallingPid);
chaviw250bcbb2020-08-05 11:17:54 -0700159 } else {
160 // A create layer request from a non system request cannot specify the owner uid
161 mOwnerUid = mCallingUid;
Adithya Srinivasan9febda82020-10-19 10:49:41 -0700162 mOwnerPid = mCallingPid;
chaviw250bcbb2020-08-05 11:17:54 -0700163 }
Dominik Laskowski75848362019-11-11 17:57:20 -0800164}
165
166void Layer::onFirstRef() {
167 mFlinger->onLayerFirstRef(this);
Dan Stoza436ccf32018-06-21 12:10:12 -0700168}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700169
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700170Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800171 sp<Client> c(mClientRef.promote());
172 if (c != 0) {
173 c->detachLayer(this);
174 }
175
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000176 mFrameTracker.logAndResetStats(mName);
chaviw74d90ad2019-04-26 14:45:26 -0700177 mFlinger->onLayerDestroyed(this);
Mathias Agopian96f08192010-06-02 23:28:45 -0700178}
179
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700180LayerCreationArgs::LayerCreationArgs(SurfaceFlinger* flinger, sp<Client> client, std::string name,
181 uint32_t w, uint32_t h, uint32_t flags, LayerMetadata metadata)
Vishnu Nair0f085c62019-08-30 08:49:12 -0700182 : flinger(flinger),
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700183 client(std::move(client)),
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700184 name(std::move(name)),
Vishnu Nair0f085c62019-08-30 08:49:12 -0700185 w(w),
186 h(h),
187 flags(flags),
188 metadata(std::move(metadata)) {
189 IPCThreadState* ipc = IPCThreadState::self();
190 callingPid = ipc->getCallingPid();
191 callingUid = ipc->getCallingUid();
192}
193
Mathias Agopian13127d82013-03-05 17:47:11 -0800194// ---------------------------------------------------------------------------
195// callbacks
196// ---------------------------------------------------------------------------
197
David Sodmaneb085e02017-10-05 18:49:04 -0700198/*
199 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
200 * Layer. So, the implementation is done in BufferLayer. When called on a
Vishnu Nairfa247b12020-02-11 08:58:26 -0800201 * EffectLayer object, it's essentially a NOP.
David Sodmaneb085e02017-10-05 18:49:04 -0700202 */
David Sodmaneb085e02017-10-05 18:49:04 -0700203void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800204
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700205void Layer::removeRelativeZ(const std::vector<Layer*>& layersInTree) {
206 if (mCurrentState.zOrderRelativeOf == nullptr) {
207 return;
208 }
Robert Carr2e102c92018-10-23 12:11:15 -0700209
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700210 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
211 if (strongRelative == nullptr) {
212 setZOrderRelativeOf(nullptr);
213 return;
214 }
215
216 if (!std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
217 strongRelative->removeZOrderRelative(this);
218 mFlinger->setTransactionFlags(eTraversalNeeded);
chaviw606e5cf2019-03-01 10:12:10 -0800219 setZOrderRelativeOf(nullptr);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700220 }
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700221}
222
223void Layer::removeFromCurrentState() {
224 mRemovedFromCurrentState = true;
Rob Carr4bba3702018-10-08 21:53:30 +0000225
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800226 mFlinger->markLayerPendingRemovalLocked(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700227}
Chia-I Wu38512252017-05-17 14:36:16 -0700228
chaviw68d4dab2020-06-08 15:07:32 -0700229sp<Layer> Layer::getRootLayer() {
230 sp<Layer> parent = getParent();
231 if (parent == nullptr) {
232 return this;
233 }
234 return parent->getRootLayer();
235}
236
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700237void Layer::onRemovedFromCurrentState() {
chaviw68d4dab2020-06-08 15:07:32 -0700238 // Use the root layer since we want to maintain the hierarchy for the entire subtree.
239 auto layersInTree = getRootLayer()->getLayersInTree(LayerVector::StateSet::Current);
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700240 std::sort(layersInTree.begin(), layersInTree.end());
chaviw68d4dab2020-06-08 15:07:32 -0700241
242 traverse(LayerVector::StateSet::Current, [&](Layer* layer) {
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700243 layer->removeFromCurrentState();
244 layer->removeRelativeZ(layersInTree);
chaviw68d4dab2020-06-08 15:07:32 -0700245 });
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700246}
247
chaviw61626f22018-11-15 16:26:27 -0800248void Layer::addToCurrentState() {
249 mRemovedFromCurrentState = false;
250
251 for (const auto& child : mCurrentChildren) {
252 child->addToCurrentState();
253 }
254}
255
Mathias Agopian13127d82013-03-05 17:47:11 -0800256// ---------------------------------------------------------------------------
257// set-up
258// ---------------------------------------------------------------------------
259
chaviw13fdc492017-06-27 12:40:18 -0700260bool Layer::getPremultipledAlpha() const {
261 return mPremultipliedAlpha;
262}
263
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700264sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800265 Mutex::Autolock _l(mLock);
Robert Carrc0df3122019-04-11 13:18:21 -0700266 if (mGetHandleCalled) {
267 ALOGE("Get handle called twice" );
268 return nullptr;
269 }
270 mGetHandleCalled = true;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700271 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800272}
273
274// ---------------------------------------------------------------------------
275// h/w composer set-up
276// ---------------------------------------------------------------------------
277
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700278static Rect reduce(const Rect& win, const Region& exclude) {
279 if (CC_LIKELY(exclude.isEmpty())) {
280 return win;
281 }
282 if (exclude.isRect()) {
283 return win.reduce(exclude.getBounds());
284 }
285 return Region(win).subtract(exclude).getBounds();
286}
287
Dan Stoza80d61162017-12-20 15:57:52 -0800288static FloatRect reduce(const FloatRect& win, const Region& exclude) {
289 if (CC_LIKELY(exclude.isEmpty())) {
290 return win;
291 }
292 // Convert through Rect (by rounding) for lack of FloatRegion
293 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
294}
295
Vishnu Nair4351ad52019-02-11 14:13:02 -0800296Rect Layer::getScreenBounds(bool reduceTransparentRegion) const {
Vishnu Nairf0c28512019-02-08 12:40:28 -0800297 if (!reduceTransparentRegion) {
298 return Rect{mScreenBounds};
299 }
300
301 FloatRect bounds = getBounds();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800302 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800303 // Transform to screen space.
304 bounds = t.transform(bounds);
305 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700306}
307
Vishnu Nair4351ad52019-02-11 14:13:02 -0800308FloatRect Layer::getBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000309 const State& s(getDrawingState());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800310 return getBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700311}
312
Vishnu Nairf0c28512019-02-08 12:40:28 -0800313FloatRect Layer::getBounds(const Region& activeTransparentRegion) const {
314 // Subtract the transparent region and snap to the bounds.
315 return reduce(mBounds, activeTransparentRegion);
316}
317
Vishnu Nairc652ff82019-03-15 12:48:54 -0700318ui::Transform Layer::getBufferScaleTransform() const {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800319 // If the layer is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
320 // it isFixedSize) then there may be additional scaling not accounted
Vishnu Nairc652ff82019-03-15 12:48:54 -0700321 // for in the layer transform.
chaviwd62d3062019-09-04 14:48:02 -0700322 if (!isFixedSize() || getBuffer() == nullptr) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700323 return {};
Vishnu Nair4351ad52019-02-11 14:13:02 -0800324 }
325
Marissa Wall290ad082019-03-06 13:23:47 -0800326 // If the layer is a buffer state layer, the active width and height
327 // could be infinite. In that case, return the effective transform.
328 const uint32_t activeWidth = getActiveWidth(getDrawingState());
329 const uint32_t activeHeight = getActiveHeight(getDrawingState());
330 if (activeWidth >= UINT32_MAX && activeHeight >= UINT32_MAX) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700331 return {};
Marissa Wall290ad082019-03-06 13:23:47 -0800332 }
333
chaviwd62d3062019-09-04 14:48:02 -0700334 int bufferWidth = getBuffer()->getWidth();
335 int bufferHeight = getBuffer()->getHeight();
Vishnu Nairc652ff82019-03-15 12:48:54 -0700336
chaviw4244e032019-09-04 11:27:49 -0700337 if (getBufferTransform() & NATIVE_WINDOW_TRANSFORM_ROT_90) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700338 std::swap(bufferWidth, bufferHeight);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800339 }
Vishnu Nairc652ff82019-03-15 12:48:54 -0700340
Marissa Wall290ad082019-03-06 13:23:47 -0800341 float sx = activeWidth / static_cast<float>(bufferWidth);
342 float sy = activeHeight / static_cast<float>(bufferHeight);
343
Vishnu Nair4351ad52019-02-11 14:13:02 -0800344 ui::Transform extraParentScaling;
345 extraParentScaling.set(sx, 0, 0, sy);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700346 return extraParentScaling;
347}
348
349ui::Transform Layer::getTransformWithScale(const ui::Transform& bufferScaleTransform) const {
350 // We need to mirror this scaling to child surfaces or we will break the contract where WM can
351 // treat child surfaces as pixels in the parent surface.
chaviwd62d3062019-09-04 14:48:02 -0700352 if (!isFixedSize() || getBuffer() == nullptr) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700353 return mEffectiveTransform;
354 }
355 return mEffectiveTransform * bufferScaleTransform;
356}
357
358FloatRect Layer::getBoundsPreScaling(const ui::Transform& bufferScaleTransform) const {
359 // We need the pre scaled layer bounds when computing child bounds to make sure the child is
360 // cropped to its parent layer after any buffer transform scaling is applied.
chaviwd62d3062019-09-04 14:48:02 -0700361 if (!isFixedSize() || getBuffer() == nullptr) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700362 return mBounds;
363 }
364 return bufferScaleTransform.inverse().transform(mBounds);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800365}
366
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700367void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform,
368 float parentShadowRadius) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800369 const State& s(getDrawingState());
370
371 // Calculate effective layer transform
372 mEffectiveTransform = parentTransform * getActiveTransform(s);
373
374 // Transform parent bounds to layer space
375 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
376
Vishnu Nairc652ff82019-03-15 12:48:54 -0700377 // Calculate source bounds
Vishnu Nair4351ad52019-02-11 14:13:02 -0800378 mSourceBounds = computeSourceBounds(parentBounds);
379
380 // Calculate bounds by croping diplay frame with layer crop and parent bounds
381 FloatRect bounds = mSourceBounds;
382 const Rect layerCrop = getCrop(s);
383 if (!layerCrop.isEmpty()) {
384 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
385 }
386 bounds = bounds.intersect(parentBounds);
387
388 mBounds = bounds;
389 mScreenBounds = mEffectiveTransform.transform(mBounds);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700390
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700391 // Use the layer's own shadow radius if set. Otherwise get the radius from
392 // parent.
393 if (s.shadowRadius > 0.f) {
394 mEffectiveShadowRadius = s.shadowRadius;
395 } else {
396 mEffectiveShadowRadius = parentShadowRadius;
397 }
398
399 // Shadow radius is passed down to only one layer so if the layer can draw shadows,
400 // don't pass it to its children.
401 const float childShadowRadius = canDrawShadows() ? 0.f : mEffectiveShadowRadius;
402
Vishnu Nairc652ff82019-03-15 12:48:54 -0700403 // Add any buffer scaling to the layer's children.
404 ui::Transform bufferScaleTransform = getBufferScaleTransform();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800405 for (const sp<Layer>& child : mDrawingChildren) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700406 child->computeBounds(getBoundsPreScaling(bufferScaleTransform),
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700407 getTransformWithScale(bufferScaleTransform), childShadowRadius);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800408 }
409}
410
Vishnu Nair60356342018-11-13 13:00:45 -0800411Rect Layer::getCroppedBufferSize(const State& s) const {
412 Rect size = getBufferSize(s);
413 Rect crop = getCrop(s);
414 if (!crop.isEmpty() && size.isValid()) {
415 size.intersect(crop, &size);
416 } else if (!crop.isEmpty()) {
417 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700418 }
Vishnu Nair60356342018-11-13 13:00:45 -0800419 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800420}
421
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700422void Layer::setupRoundedCornersCropCoordinates(Rect win,
423 const FloatRect& roundedCornersCrop) const {
424 // Translate win by the rounded corners rect coordinates, to have all values in
425 // layer coordinate space.
426 win.left -= roundedCornersCrop.left;
427 win.right -= roundedCornersCrop.left;
428 win.top -= roundedCornersCrop.top;
429 win.bottom -= roundedCornersCrop.top;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700430}
431
Lloyd Piquede196652020-01-22 17:29:58 -0800432void Layer::prepareBasicGeometryCompositionState() {
Lloyd Piquec6687342019-03-07 21:34:57 -0800433 const auto& drawingState{getDrawingState()};
434 const uint32_t layerStack = getLayerStack();
435 const auto alpha = static_cast<float>(getAlpha());
436 const bool opaque = isOpaque(drawingState);
437 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
438
439 auto blendMode = Hwc2::IComposerClient::BlendMode::NONE;
440 if (!opaque || alpha != 1.0f) {
441 blendMode = mPremultipliedAlpha ? Hwc2::IComposerClient::BlendMode::PREMULTIPLIED
442 : Hwc2::IComposerClient::BlendMode::COVERAGE;
443 }
444
Lloyd Piquede196652020-01-22 17:29:58 -0800445 auto* compositionState = editCompositionState();
446 compositionState->layerStackId =
Lloyd Piquec6687342019-03-07 21:34:57 -0800447 (layerStack != ~0u) ? std::make_optional(layerStack) : std::nullopt;
Lloyd Piquede196652020-01-22 17:29:58 -0800448 compositionState->internalOnly = getPrimaryDisplayOnly();
449 compositionState->isVisible = isVisible();
450 compositionState->isOpaque = opaque && !usesRoundedCorners && alpha == 1.f;
451 compositionState->shadowRadius = mEffectiveShadowRadius;
Lloyd Piquec6687342019-03-07 21:34:57 -0800452
Lloyd Piquede196652020-01-22 17:29:58 -0800453 compositionState->contentDirty = contentDirty;
Lloyd Piquec6687342019-03-07 21:34:57 -0800454 contentDirty = false;
455
Lloyd Piquede196652020-01-22 17:29:58 -0800456 compositionState->geomLayerBounds = mBounds;
457 compositionState->geomLayerTransform = getTransform();
458 compositionState->geomInverseLayerTransform = compositionState->geomLayerTransform.inverse();
459 compositionState->transparentRegionHint = getActiveTransparentRegion(drawingState);
Lloyd Piquec6687342019-03-07 21:34:57 -0800460
Lloyd Piquede196652020-01-22 17:29:58 -0800461 compositionState->blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
462 compositionState->alpha = alpha;
463 compositionState->backgroundBlurRadius = drawingState.backgroundBlurRadius;
Lucas Dupinc3800b82020-10-02 16:24:48 -0700464 compositionState->blurRegions = drawingState.blurRegions;
John Reckc00c6692021-02-16 11:37:33 -0500465 compositionState->stretchEffect = getStretchEffect();
Lloyd Piquec6687342019-03-07 21:34:57 -0800466}
467
Lloyd Piquede196652020-01-22 17:29:58 -0800468void Layer::prepareGeometryCompositionState() {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800469 const auto& drawingState{getDrawingState()};
Mathias Agopian13127d82013-03-05 17:47:11 -0800470
Lloyd Piquea83776c2019-01-29 18:42:32 -0800471 int type = drawingState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
472 int appId = drawingState.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700473 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400474 if (parent.get()) {
475 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800476 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
477 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
Jiwen 'Steve' Cai736c74e2019-05-28 18:33:22 -0700478 if (parentType > 0 && parentAppId > 0) {
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800479 type = parentType;
480 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700481 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400482 }
483
Lloyd Piquede196652020-01-22 17:29:58 -0800484 auto* compositionState = editCompositionState();
David Sodman15094112018-10-11 09:39:37 -0700485
Lloyd Piquede196652020-01-22 17:29:58 -0800486 compositionState->geomBufferSize = getBufferSize(drawingState);
487 compositionState->geomContentCrop = getBufferCrop();
488 compositionState->geomCrop = getCrop(drawingState);
489 compositionState->geomBufferTransform = getBufferTransform();
490 compositionState->geomBufferUsesDisplayInverseTransform = getTransformToDisplayInverse();
491 compositionState->geomUsesSourceCrop = usesSourceCrop();
492 compositionState->isSecure = isSecure();
493
Lloyd Pique8d9f8362020-02-11 19:13:09 -0800494 compositionState->metadata.clear();
495 const auto& supportedMetadata = mFlinger->getHwComposer().getSupportedLayerGenericMetadata();
496 for (const auto& [key, mandatory] : supportedMetadata) {
497 const auto& genericLayerMetadataCompatibilityMap =
498 mFlinger->getGenericLayerMetadataKeyMap();
499 auto compatIter = genericLayerMetadataCompatibilityMap.find(key);
500 if (compatIter == std::end(genericLayerMetadataCompatibilityMap)) {
501 continue;
502 }
503 const uint32_t id = compatIter->second;
504
505 auto it = drawingState.metadata.mMap.find(id);
506 if (it == std::end(drawingState.metadata.mMap)) {
507 continue;
508 }
509
510 compositionState->metadata
511 .emplace(key, compositionengine::GenericLayerMetadataEntry{mandatory, it->second});
512 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800513}
David Sodmanba340492018-08-05 21:51:33 -0700514
Lloyd Piquede196652020-01-22 17:29:58 -0800515void Layer::preparePerFrameCompositionState() {
Lloyd Pique688abd42019-02-15 15:42:24 -0800516 const auto& drawingState{getDrawingState()};
Lloyd Piquede196652020-01-22 17:29:58 -0800517 auto* compositionState = editCompositionState();
Lloyd Piquef5275482019-01-29 18:42:42 -0800518
Lloyd Piquede196652020-01-22 17:29:58 -0800519 compositionState->forceClientComposition = false;
520
521 compositionState->isColorspaceAgnostic = isColorSpaceAgnostic();
522 compositionState->dataspace = getDataSpace();
523 compositionState->colorTransform = getColorTransform();
524 compositionState->colorTransformIsIdentity = !hasColorTransform();
525 compositionState->surfaceDamage = surfaceDamageRegion;
526 compositionState->hasProtectedContent = isProtected();
Lloyd Pique688abd42019-02-15 15:42:24 -0800527
528 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700529
Lloyd Piquede196652020-01-22 17:29:58 -0800530 compositionState->isOpaque =
Lloyd Pique688abd42019-02-15 15:42:24 -0800531 isOpaque(drawingState) && !usesRoundedCorners && getAlpha() == 1.0_hf;
Lloyd Piquef5275482019-01-29 18:42:42 -0800532
533 // Force client composition for special cases known only to the front-end.
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400534 // Rounded corners no longer force client composition, since we may use a
535 // hole punch so that the layer will appear to have rounded corners.
536 if (isHdrY410() || drawShadows() || drawingState.blurRegions.size() > 0 ||
John Reckc00c6692021-02-16 11:37:33 -0500537 compositionState->stretchEffect.hasEffect()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800538 compositionState->forceClientComposition = true;
Lloyd Piquef5275482019-01-29 18:42:42 -0800539 }
540}
541
Lloyd Piquede196652020-01-22 17:29:58 -0800542void Layer::prepareCursorCompositionState() {
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800543 const State& drawingState{getDrawingState()};
Lloyd Piquede196652020-01-22 17:29:58 -0800544 auto* compositionState = editCompositionState();
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800545
546 // Apply the layer's transform, followed by the display's global transform
547 // Here we're guaranteed that the layer's transform preserves rects
548 Rect win = getCroppedBufferSize(drawingState);
549 // Subtract the transparent region and snap to the bounds
550 Rect bounds = reduce(win, getActiveTransparentRegion(drawingState));
551 Rect frame(getTransform().transform(bounds));
552
Lloyd Piquede196652020-01-22 17:29:58 -0800553 compositionState->cursorFrame = frame;
554}
555
556sp<compositionengine::LayerFE> Layer::asLayerFE() const {
557 return const_cast<compositionengine::LayerFE*>(
558 static_cast<const compositionengine::LayerFE*>(this));
559}
560
561sp<compositionengine::LayerFE> Layer::getCompositionEngineLayerFE() const {
562 return nullptr;
563}
564
565compositionengine::LayerFECompositionState* Layer::editCompositionState() {
566 return nullptr;
567}
568
569const compositionengine::LayerFECompositionState* Layer::getCompositionState() const {
570 return nullptr;
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800571}
572
Lloyd Piquef16688f2019-02-19 17:47:57 -0800573bool Layer::onPreComposition(nsecs_t) {
574 return false;
575}
576
Lloyd Piquede196652020-01-22 17:29:58 -0800577void Layer::prepareCompositionState(compositionengine::LayerFE::StateSubset subset) {
Lloyd Piquec6687342019-03-07 21:34:57 -0800578 using StateSubset = compositionengine::LayerFE::StateSubset;
Lloyd Piquef5275482019-01-29 18:42:42 -0800579
Lloyd Piquec6687342019-03-07 21:34:57 -0800580 switch (subset) {
581 case StateSubset::BasicGeometry:
Lloyd Piquede196652020-01-22 17:29:58 -0800582 prepareBasicGeometryCompositionState();
Lloyd Piquec6687342019-03-07 21:34:57 -0800583 break;
584
585 case StateSubset::GeometryAndContent:
Lloyd Piquede196652020-01-22 17:29:58 -0800586 prepareBasicGeometryCompositionState();
587 prepareGeometryCompositionState();
588 preparePerFrameCompositionState();
Lloyd Piquec6687342019-03-07 21:34:57 -0800589 break;
590
591 case StateSubset::Content:
Lloyd Piquede196652020-01-22 17:29:58 -0800592 preparePerFrameCompositionState();
593 break;
594
595 case StateSubset::Cursor:
596 prepareCursorCompositionState();
Lloyd Piquec6687342019-03-07 21:34:57 -0800597 break;
598 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800599}
Mathias Agopian29a367b2011-07-12 14:51:45 -0700600
Lloyd Piquea83776c2019-01-29 18:42:32 -0800601const char* Layer::getDebugName() const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700602 return mName.c_str();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800603}
604
Mathias Agopian13127d82013-03-05 17:47:11 -0800605// ---------------------------------------------------------------------------
606// drawing...
607// ---------------------------------------------------------------------------
608
Vishnu Nair9b079a22020-01-21 14:36:08 -0800609std::optional<compositionengine::LayerFE::LayerSettings> Layer::prepareClientComposition(
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100610 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
Lloyd Piquede196652020-01-22 17:29:58 -0800611 if (!getCompositionState()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800612 return {};
613 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800614
Vishnu Nair4351ad52019-02-11 14:13:02 -0800615 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000616 half alpha = getAlpha();
Vishnu Nair9b079a22020-01-21 14:36:08 -0800617
618 compositionengine::LayerFE::LayerSettings layerSettings;
Lloyd Piquef16688f2019-02-19 17:47:57 -0800619 layerSettings.geometry.boundaries = bounds;
chaviwc6ad8af2020-08-03 11:33:30 -0700620 layerSettings.geometry.positionTransform = getTransform().asMatrix4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000621
622 if (hasColorTransform()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800623 layerSettings.colorTransform = getColorTransform();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000624 }
625
626 const auto roundedCornerState = getRoundedCornerState();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800627 layerSettings.geometry.roundedCornersRadius = roundedCornerState.radius;
628 layerSettings.geometry.roundedCornersCrop = roundedCornerState.cropRect;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000629
Lloyd Piquef16688f2019-02-19 17:47:57 -0800630 layerSettings.alpha = alpha;
chaviw4244e032019-09-04 11:27:49 -0700631 layerSettings.sourceDataspace = getDataSpace();
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100632 if (!targetSettings.disableBlurs) {
633 layerSettings.backgroundBlurRadius = getBackgroundBlurRadius();
634 layerSettings.blurRegions = getBlurRegions();
Derek Sollenberger39feade2021-04-27 16:08:40 -0400635 layerSettings.blurRegionTransform =
636 getActiveTransform(getDrawingState()).inverse().asMatrix4();
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100637 }
John Reckcdb4ed72021-02-04 13:39:33 -0500638 layerSettings.stretchEffect = getDrawingState().stretchEffect;
Ana Krulec6eab17a2020-12-09 15:52:36 -0800639 // Record the name of the layer for debugging further down the stack.
640 layerSettings.name = getName();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800641 return layerSettings;
Mathias Agopian13127d82013-03-05 17:47:11 -0800642}
643
Vishnu Nair9b079a22020-01-21 14:36:08 -0800644std::optional<compositionengine::LayerFE::LayerSettings> Layer::prepareShadowClientComposition(
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200645 const LayerFE::LayerSettings& casterLayerSettings, const Rect& layerStackRect,
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800646 ui::Dataspace outputDataspace) {
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200647 renderengine::ShadowSettings shadow = getShadowSettings(layerStackRect);
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800648 if (shadow.length <= 0.f) {
649 return {};
650 }
651
652 const float casterAlpha = casterLayerSettings.alpha;
653 const bool casterIsOpaque = ((casterLayerSettings.source.buffer.buffer != nullptr) &&
654 casterLayerSettings.source.buffer.isOpaque);
655
Vishnu Nair9b079a22020-01-21 14:36:08 -0800656 compositionengine::LayerFE::LayerSettings shadowLayer = casterLayerSettings;
657
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800658 shadowLayer.shadow = shadow;
Vishnu Naira483b4a2019-12-12 15:07:52 -0800659 shadowLayer.geometry.boundaries = mBounds; // ignore transparent region
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800660
661 // If the casting layer is translucent, we need to fill in the shadow underneath the layer.
662 // Otherwise the generated shadow will only be shown around the casting layer.
663 shadowLayer.shadow.casterIsTranslucent = !casterIsOpaque || (casterAlpha < 1.0f);
664 shadowLayer.shadow.ambientColor *= casterAlpha;
665 shadowLayer.shadow.spotColor *= casterAlpha;
666 shadowLayer.sourceDataspace = outputDataspace;
667 shadowLayer.source.buffer.buffer = nullptr;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800668 shadowLayer.source.buffer.fence = nullptr;
669 shadowLayer.frameNumber = 0;
670 shadowLayer.bufferId = 0;
Alec Mouribd17b3b2020-12-17 11:08:30 -0800671 shadowLayer.name = getName();
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800672
673 if (shadowLayer.shadow.ambientColor.a <= 0.f && shadowLayer.shadow.spotColor.a <= 0.f) {
674 return {};
675 }
676
677 float casterCornerRadius = shadowLayer.geometry.roundedCornersRadius;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800678 const FloatRect& cornerRadiusCropRect = shadowLayer.geometry.roundedCornersCrop;
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800679 const FloatRect& casterRect = shadowLayer.geometry.boundaries;
680
681 // crop used to set the corner radius may be larger than the content rect. Adjust the corner
682 // radius accordingly.
683 if (casterCornerRadius > 0.f) {
684 float cropRectOffset = std::max(std::abs(cornerRadiusCropRect.top - casterRect.top),
685 std::abs(cornerRadiusCropRect.left - casterRect.left));
686 if (cropRectOffset > casterCornerRadius) {
687 casterCornerRadius = 0;
688 } else {
689 casterCornerRadius -= cropRectOffset;
690 }
691 shadowLayer.geometry.roundedCornersRadius = casterCornerRadius;
692 }
693
694 return shadowLayer;
695}
696
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800697void Layer::prepareClearClientComposition(LayerFE::LayerSettings& layerSettings,
698 bool blackout) const {
699 layerSettings.source.buffer.buffer = nullptr;
700 layerSettings.source.solidColor = half3(0.0, 0.0, 0.0);
701 layerSettings.disableBlending = true;
Vishnu Nair3b653e72020-02-24 16:40:50 -0800702 layerSettings.bufferId = 0;
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800703 layerSettings.frameNumber = 0;
704
705 // If layer is blacked out, force alpha to 1 so that we draw a black color layer.
706 layerSettings.alpha = blackout ? 1.0f : 0.0f;
Alec Mouribd17b3b2020-12-17 11:08:30 -0800707 layerSettings.name = getName();
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800708}
709
710std::vector<compositionengine::LayerFE::LayerSettings> Layer::prepareClientCompositionList(
711 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
712 std::optional<compositionengine::LayerFE::LayerSettings> layerSettings =
713 prepareClientComposition(targetSettings);
714 // Nothing to render.
715 if (!layerSettings) {
716 return {};
717 }
718
719 // HWC requests to clear this layer.
720 if (targetSettings.clearContent) {
721 prepareClearClientComposition(*layerSettings, false /* blackout */);
722 return {*layerSettings};
723 }
724
725 std::optional<compositionengine::LayerFE::LayerSettings> shadowSettings =
726 prepareShadowClientComposition(*layerSettings, targetSettings.viewport,
727 targetSettings.dataspace);
728 // There are no shadows to render.
729 if (!shadowSettings) {
730 return {*layerSettings};
731 }
732
733 // If the layer casts a shadow but the content casting the shadow is occluded, skip
734 // composing the non-shadow content and only draw the shadows.
735 if (targetSettings.realContentIsVisible) {
736 return {*shadowSettings, *layerSettings};
737 }
738
739 return {*shadowSettings};
740}
741
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700742Hwc2::IComposerClient::Composition Layer::getCompositionType(const DisplayDevice& display) const {
743 const auto outputLayer = findOutputLayerForDisplay(&display);
Alec Mouri6b9e9912020-01-21 10:50:24 -0800744 if (outputLayer == nullptr) {
745 return Hwc2::IComposerClient::Composition::INVALID;
746 }
747 if (outputLayer->getState().hwc) {
748 return (*outputLayer->getState().hwc).hwcCompositionType;
749 } else {
750 return Hwc2::IComposerClient::Composition::CLIENT;
751 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800752}
753
Mathias Agopian13127d82013-03-05 17:47:11 -0800754// ----------------------------------------------------------------------------
755// local state
756// ----------------------------------------------------------------------------
757
David Sodman41fdfc92017-11-06 16:09:56 -0800758bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800759 const State& s(mDrawingState);
Garfield Tande619fa2020-10-02 17:13:53 -0700760 if (s.flags & layer_state_t::eLayerSecure) {
761 return true;
762 }
763
764 const auto p = mDrawingParent.promote();
765 return (p != nullptr) ? p->isSecure() : false;
Dan Stoza23116082015-06-18 14:58:39 -0700766}
767
Mathias Agopian13127d82013-03-05 17:47:11 -0800768// ----------------------------------------------------------------------------
769// transaction
770// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800771
Marissa Wall61c58622018-07-18 10:12:20 -0700772uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000773 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800774
Marissa Wall61c58622018-07-18 10:12:20 -0700775 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
776 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700777
David Sodmaneb085e02017-10-05 18:49:04 -0700778 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700779 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000780 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800781 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
782 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
783 " requested={ wh={%4u,%4u} }}\n"
784 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
785 " requested={ wh={%4u,%4u} }}\n",
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700786 this, getName().c_str(), getBufferTransform(), getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700787 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
chaviw25714502021-02-11 10:01:08 -0800788 stateToCommit->crop.left, stateToCommit->crop.top, stateToCommit->crop.right,
789 stateToCommit->crop.bottom, stateToCommit->crop.getWidth(),
790 stateToCommit->crop.getHeight(), stateToCommit->requested_legacy.w,
791 stateToCommit->requested_legacy.h, s.active_legacy.w, s.active_legacy.h,
792 s.crop.left, s.crop.top, s.crop.right, s.crop.bottom, s.crop.getWidth(),
793 s.crop.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800794 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700795
Robert Carre392b552017-09-19 12:16:05 -0700796 // Don't let Layer::doTransaction update the drawing state
797 // if we have a pending resize, unless we are in fixed-size mode.
798 // the drawing state will be updated only once we receive a buffer
799 // with the correct size.
800 //
801 // In particular, we want to make sure the clip (which is part
802 // of the geometry state) is latched together with the size but is
803 // latched immediately when no resizing is involved.
804 //
805 // If a sideband stream is attached, however, we want to skip this
806 // optimization so that transactions aren't missed when a buffer
807 // never arrives
808 //
809 // In the case that we don't have a buffer we ignore other factors
810 // and avoid entering the resizePending state. At a high level the
811 // resizePending state is to avoid applying the state of the new buffer
812 // to the old buffer. However in the state where we don't have an old buffer
813 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700814 const bool resizePending =
815 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
816 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
chaviwd62d3062019-09-04 14:48:02 -0700817 (getBuffer() != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700818 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800819 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700820 flags |= eDontUpdateGeometryState;
821 }
822 }
823
Robert Carr7bf247e2017-05-18 14:02:49 -0700824 // Here we apply various requested geometry states, depending on our
825 // latching configuration. See Layer.h for a detailed discussion of
826 // how geometry latching is controlled.
827 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000828 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700829
Robert Carr7bf247e2017-05-18 14:02:49 -0700830 // There is an awkward asymmetry in the handling of the crop states in the position
831 // states, as can be seen below. Largely this arises from position and transform
832 // being stored in the same data structure while having different latching rules.
833 // b/38182305
834 //
Marissa Wall61c58622018-07-18 10:12:20 -0700835 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700836 // applyPendingStates in the presence of deferred transactions.
chaviw214c89d2019-09-04 16:03:53 -0700837 editCurrentState.active_legacy = editCurrentState.requested_legacy;
838 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Mathias Agopian13127d82013-03-05 17:47:11 -0800839 }
840
Marissa Wall61c58622018-07-18 10:12:20 -0700841 return flags;
842}
843
844uint32_t Layer::doTransaction(uint32_t flags) {
845 ATRACE_CALL();
846
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800847 if (mChildrenChanged) {
848 flags |= eVisibleRegion;
849 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -0700850 }
851
Robert Carr0758e5d2021-03-11 22:15:04 -0800852 // TODO: This is unfortunate.
853 mCurrentStateModified = mCurrentState.modified;
854 mCurrentState.modified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700855
Robert Carr0758e5d2021-03-11 22:15:04 -0800856 flags = doTransactionResize(flags, &mCurrentState);
Marissa Wall61c58622018-07-18 10:12:20 -0700857
Alec Mourib416efd2018-09-06 21:01:59 +0000858 const State& s(getDrawingState());
Robert Carr0758e5d2021-03-11 22:15:04 -0800859 State& c(getCurrentState());
Marissa Wall61c58622018-07-18 10:12:20 -0700860
861 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800862 // invalidate and recompute the visible regions if needed
863 flags |= Layer::eVisibleRegion;
864 }
865
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700866 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800867 // invalidate and recompute the visible regions if needed
868 flags |= eVisibleRegion;
869 this->contentDirty = true;
870
871 // we may use linear filtering, if the matrix scales us
Marin Shalamanov043ba292020-09-10 13:35:20 +0200872 mNeedsFiltering = getActiveTransform(c).needsBilinearFiltering();
Mathias Agopian13127d82013-03-05 17:47:11 -0800873 }
874
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800875 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -0700876 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800877 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -0700878 }
879
chaviw47dbd692020-10-26 17:50:12 -0700880 // Add the callbacks from the drawing state into the current state. This is so when the current
881 // state gets copied to drawing, we don't lose the callback handles that are still in drawing.
882 for (auto& handle : s.callbackHandles) {
883 c.callbackHandles.push_back(handle);
884 }
885
Vishnu Nair1506b182021-02-22 14:35:15 -0800886 // Allow BufferStateLayer to release any unlatched buffers in drawing state.
Alec Mouria90a5702021-04-16 16:36:21 +0000887 bufferMayChange(c.buffer->getBuffer());
Vishnu Nair1506b182021-02-22 14:35:15 -0800888
Mathias Agopian13127d82013-03-05 17:47:11 -0800889 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -0700890 commitTransaction(c);
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800891 mCurrentState.callbackHandles = {};
Robert Carra1257842020-01-31 13:48:28 -0800892
Mathias Agopian13127d82013-03-05 17:47:11 -0800893 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800894}
895
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000896void Layer::commitTransaction(State& stateToCommit) {
Adithya Srinivasand11eb6b2021-03-09 18:46:28 +0000897 if (auto& bufferSurfaceFrame = mDrawingState.bufferSurfaceFrameTX;
Alec Mouria90a5702021-04-16 16:36:21 +0000898 ((mDrawingState.buffer && stateToCommit.buffer &&
899 mDrawingState.buffer->getBuffer() != stateToCommit.buffer->getBuffer()) ||
900 (mDrawingState.buffer && !stateToCommit.buffer) ||
901 (!mDrawingState.buffer && stateToCommit.buffer)) &&
902 bufferSurfaceFrame != nullptr &&
Adithya Srinivasand11eb6b2021-03-09 18:46:28 +0000903 bufferSurfaceFrame->getPresentState() != PresentState::Presented) {
904 // If the previous buffer was committed but not latched (refreshPending - happens during
905 // back to back invalidates), it gets silently dropped here. Mark the corresponding
906 // SurfaceFrame as dropped to prevent it from getting stuck in the pending classification
907 // list.
908 addSurfaceFrameDroppedForBuffer(bufferSurfaceFrame);
909 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800910 mDrawingState = stateToCommit;
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000911
912 // Set the present state for all bufferlessSurfaceFramesTX to Presented. The
913 // bufferSurfaceFrameTX will be presented in latchBuffer.
914 for (auto& [token, surfaceFrame] : mDrawingState.bufferlessSurfaceFramesTX) {
915 if (surfaceFrame->getPresentState() != PresentState::Presented) {
916 // With applyPendingStates, we could end up having presented surfaceframes from previous
917 // states
918 surfaceFrame->setPresentState(PresentState::Presented);
919 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
920 }
921 }
922 // Clear the surfaceFrames from the old state now that it has been copied into DrawingState.
923 stateToCommit.bufferSurfaceFrameTX.reset();
924 stateToCommit.bufferlessSurfaceFramesTX.clear();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700925}
926
Mathias Agopian13127d82013-03-05 17:47:11 -0800927uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800928 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800929}
930
931uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800932 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -0800933}
934
chaviw214c89d2019-09-04 16:03:53 -0700935bool Layer::setPosition(float x, float y) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800936 if (mCurrentState.requested_legacy.transform.tx() == x &&
937 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -0800938 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800939 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -0700940
941 // We update the requested and active position simultaneously because
942 // we want to apply the position portion of the transform matrix immediately,
943 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800944 mCurrentState.requested_legacy.transform.set(x, y);
chaviw214c89d2019-09-04 16:03:53 -0700945 // Here we directly update the active state
946 // unlike other setters, because we store it within
947 // the transform, but use different latching rules.
948 // b/38182305
949 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr69663fb2016-03-27 19:59:19 -0700950
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800951 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800952 setTransactionFlags(eTransactionNeeded);
953 return true;
954}
Robert Carr82364e32016-05-15 11:27:47 -0700955
Robert Carr1f0a16a2016-10-24 16:27:39 -0700956bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
957 ssize_t idx = mCurrentChildren.indexOf(childLayer);
958 if (idx < 0) {
959 return false;
960 }
961 if (childLayer->setLayer(z)) {
962 mCurrentChildren.removeAt(idx);
963 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800964 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700965 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800966 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700967}
968
Robert Carr503c7042017-09-27 15:06:08 -0700969bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
970 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
971 ssize_t idx = mCurrentChildren.indexOf(childLayer);
972 if (idx < 0) {
973 return false;
974 }
975 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
976 mCurrentChildren.removeAt(idx);
977 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800978 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700979 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800980 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700981}
982
Robert Carrae060832016-11-28 10:51:00 -0800983bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800984 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
985 mCurrentState.sequence++;
986 mCurrentState.z = z;
987 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700988
989 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800990 if (mCurrentState.zOrderRelativeOf != nullptr) {
991 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700992 if (strongRelative != nullptr) {
993 strongRelative->removeZOrderRelative(this);
994 }
chaviw606e5cf2019-03-01 10:12:10 -0800995 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -0700996 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800997 setTransactionFlags(eTransactionNeeded);
998 return true;
999}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001000
Robert Carrdb66e622017-04-10 16:55:57 -07001001void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001002 mCurrentState.zOrderRelatives.remove(relative);
1003 mCurrentState.sequence++;
1004 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001005 setTransactionFlags(eTransactionNeeded);
1006}
1007
1008void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001009 mCurrentState.zOrderRelatives.add(relative);
1010 mCurrentState.modified = true;
1011 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -07001012 setTransactionFlags(eTransactionNeeded);
1013}
1014
chaviw606e5cf2019-03-01 10:12:10 -08001015void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
1016 mCurrentState.zOrderRelativeOf = relativeOf;
1017 mCurrentState.sequence++;
1018 mCurrentState.modified = true;
chaviwbdb8b802019-10-14 09:17:12 -07001019 mCurrentState.isRelativeOf = relativeOf != nullptr;
1020
chaviw606e5cf2019-03-01 10:12:10 -08001021 setTransactionFlags(eTransactionNeeded);
1022}
1023
Robert Carr503d2bd2017-12-04 15:49:47 -08001024bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001025 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1026 if (handle == nullptr) {
1027 return false;
1028 }
1029 sp<Layer> relative = handle->owner.promote();
1030 if (relative == nullptr) {
1031 return false;
1032 }
1033
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001034 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1035 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001036 return false;
1037 }
1038
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001039 mCurrentState.sequence++;
1040 mCurrentState.modified = true;
1041 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001042
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001043 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -07001044 if (oldZOrderRelativeOf != nullptr) {
1045 oldZOrderRelativeOf->removeZOrderRelative(this);
1046 }
chaviw606e5cf2019-03-01 10:12:10 -08001047 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -07001048 relative->addZOrderRelative(this);
1049
1050 setTransactionFlags(eTransactionNeeded);
1051
1052 return true;
1053}
1054
Mathias Agopian13127d82013-03-05 17:47:11 -08001055bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001056 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001057 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001058 mCurrentState.requested_legacy.w = w;
1059 mCurrentState.requested_legacy.h = h;
1060 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001061 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001062
1063 // record the new size, from this point on, when the client request
1064 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001065 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001066 return true;
1067}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001068bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001069 if (mCurrentState.color.a == alpha) return false;
1070 mCurrentState.sequence++;
1071 mCurrentState.color.a = alpha;
1072 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001073 setTransactionFlags(eTransactionNeeded);
1074 return true;
1075}
chaviw13fdc492017-06-27 12:40:18 -07001076
Valerie Haudd0b7572019-01-29 14:59:27 -08001077bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1078 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001079 return false;
Valerie Hauaa194562019-02-05 16:21:38 -08001080 }
1081 mCurrentState.sequence++;
1082 mCurrentState.modified = true;
1083 setTransactionFlags(eTransactionNeeded);
1084
1085 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001086 // create background color layer if one does not yet exist
Vishnu Nairfa247b12020-02-11 08:58:26 -08001087 uint32_t flags = ISurfaceComposerClient::eFXSurfaceEffect;
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001088 std::string name = mName + "BackgroundColorLayer";
Vishnu Nairfa247b12020-02-11 08:58:26 -08001089 mCurrentState.bgColorLayer = mFlinger->getFactory().createEffectLayer(
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001090 LayerCreationArgs(mFlinger.get(), nullptr, std::move(name), 0, 0, flags,
1091 LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -07001092
Valerie Haudd0b7572019-01-29 14:59:27 -08001093 // add to child list
1094 addChild(mCurrentState.bgColorLayer);
1095 mFlinger->mLayersAdded = true;
1096 // set up SF to handle added color layer
1097 if (isRemovedFromCurrentState()) {
1098 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1099 }
1100 mFlinger->setTransactionFlags(eTransactionNeeded);
1101 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1102 mCurrentState.bgColorLayer->reparent(nullptr);
1103 mCurrentState.bgColorLayer = nullptr;
1104 return true;
1105 }
1106
1107 mCurrentState.bgColorLayer->setColor(color);
1108 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1109 mCurrentState.bgColorLayer->setAlpha(alpha);
1110 mCurrentState.bgColorLayer->setDataspace(dataspace);
1111
chaviw13fdc492017-06-27 12:40:18 -07001112 return true;
1113}
1114
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001115bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001116 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001117
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001118 mCurrentState.sequence++;
1119 mCurrentState.cornerRadius = cornerRadius;
1120 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001121 setTransactionFlags(eTransactionNeeded);
1122 return true;
1123}
1124
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001125bool Layer::setBackgroundBlurRadius(int backgroundBlurRadius) {
1126 if (mCurrentState.backgroundBlurRadius == backgroundBlurRadius) return false;
1127
1128 mCurrentState.sequence++;
1129 mCurrentState.backgroundBlurRadius = backgroundBlurRadius;
1130 mCurrentState.modified = true;
1131 setTransactionFlags(eTransactionNeeded);
1132 return true;
1133}
1134
Robert Carrd4ae7f32018-06-07 16:10:57 -07001135bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1136 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001137 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001138 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1139
1140 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
Hongwei Wang46213ea2020-12-04 17:17:31 -08001141 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER nor "
1142 "ROTATE_SURFACE_FLINGER ignored");
Robert Carrd4ae7f32018-06-07 16:10:57 -07001143 return false;
1144 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001145 mCurrentState.sequence++;
1146 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1147 matrix.dsdy);
1148 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001149 setTransactionFlags(eTransactionNeeded);
1150 return true;
1151}
Marissa Wall61c58622018-07-18 10:12:20 -07001152
Mathias Agopian13127d82013-03-05 17:47:11 -08001153bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001154 mCurrentState.requestedTransparentRegion_legacy = transparent;
1155 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001156 setTransactionFlags(eTransactionNeeded);
1157 return true;
1158}
Ana Krulecc84d09b2019-11-02 23:10:29 +01001159
Lucas Dupinc3800b82020-10-02 16:24:48 -07001160bool Layer::setBlurRegions(const std::vector<BlurRegion>& blurRegions) {
1161 mCurrentState.sequence++;
1162 mCurrentState.blurRegions = blurRegions;
1163 mCurrentState.modified = true;
1164 setTransactionFlags(eTransactionNeeded);
1165 return true;
1166}
1167
Vishnu Nairf6eddb62021-01-27 22:02:11 -08001168bool Layer::setFlags(uint32_t flags, uint32_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001169 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1170 if (mCurrentState.flags == newFlags) return false;
1171 mCurrentState.sequence++;
1172 mCurrentState.flags = newFlags;
1173 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001174 setTransactionFlags(eTransactionNeeded);
1175 return true;
1176}
Robert Carr99e27f02016-06-16 15:18:02 -07001177
chaviw25714502021-02-11 10:01:08 -08001178bool Layer::setCrop(const Rect& crop) {
1179 if (mCurrentState.requestedCrop == crop) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001180 mCurrentState.sequence++;
chaviw25714502021-02-11 10:01:08 -08001181 mCurrentState.requestedCrop = crop;
1182 mCurrentState.crop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001183
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001184 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001185 setTransactionFlags(eTransactionNeeded);
1186 return true;
1187}
Robert Carr8d5227b2017-03-16 15:41:03 -07001188
Evan Roskyef876c92019-01-25 17:46:06 -08001189bool Layer::setMetadata(const LayerMetadata& data) {
1190 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001191 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001192 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001193 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001194}
1195
Mathias Agopian13127d82013-03-05 17:47:11 -08001196bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001197 if (mCurrentState.layerStack == layerStack) return false;
1198 mCurrentState.sequence++;
1199 mCurrentState.layerStack = layerStack;
1200 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001201 setTransactionFlags(eTransactionNeeded);
1202 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001203}
1204
Peiyong Linc502cb72019-03-01 15:00:23 -08001205bool Layer::setColorSpaceAgnostic(const bool agnostic) {
1206 if (mCurrentState.colorSpaceAgnostic == agnostic) {
1207 return false;
1208 }
1209 mCurrentState.sequence++;
1210 mCurrentState.colorSpaceAgnostic = agnostic;
1211 mCurrentState.modified = true;
1212 setTransactionFlags(eTransactionNeeded);
1213 return true;
1214}
1215
Ana Krulecc84d09b2019-11-02 23:10:29 +01001216bool Layer::setFrameRateSelectionPriority(int32_t priority) {
1217 if (mCurrentState.frameRateSelectionPriority == priority) return false;
1218 mCurrentState.frameRateSelectionPriority = priority;
1219 mCurrentState.sequence++;
1220 mCurrentState.modified = true;
1221 setTransactionFlags(eTransactionNeeded);
1222 return true;
1223}
1224
1225int32_t Layer::getFrameRateSelectionPriority() const {
1226 // Check if layer has priority set.
1227 if (mDrawingState.frameRateSelectionPriority != PRIORITY_UNSET) {
1228 return mDrawingState.frameRateSelectionPriority;
1229 }
1230 // If not, search whether its parents have it set.
1231 sp<Layer> parent = getParent();
1232 if (parent != nullptr) {
1233 return parent->getFrameRateSelectionPriority();
1234 }
1235
1236 return Layer::PRIORITY_UNSET;
1237}
1238
Ady Abrahamaae5ed52020-06-26 09:32:43 -07001239bool Layer::isLayerFocusedBasedOnPriority(int32_t priority) {
1240 return priority == PRIORITY_FOCUSED_WITH_MODE || priority == PRIORITY_FOCUSED_WITHOUT_MODE;
1241};
1242
Robert Carr1f0a16a2016-10-24 16:27:39 -07001243uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001244 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001245 if (p == nullptr) {
1246 return getDrawingState().layerStack;
1247 }
1248 return p->getLayerStack();
1249}
1250
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001251bool Layer::setShadowRadius(float shadowRadius) {
1252 if (mCurrentState.shadowRadius == shadowRadius) {
1253 return false;
1254 }
1255
1256 mCurrentState.sequence++;
1257 mCurrentState.shadowRadius = shadowRadius;
1258 mCurrentState.modified = true;
1259 setTransactionFlags(eTransactionNeeded);
1260 return true;
1261}
1262
Vishnu Nair6213bd92020-05-08 17:42:25 -07001263bool Layer::setFixedTransformHint(ui::Transform::RotationFlags fixedTransformHint) {
1264 if (mCurrentState.fixedTransformHint == fixedTransformHint) {
1265 return false;
1266 }
1267
1268 mCurrentState.sequence++;
1269 mCurrentState.fixedTransformHint = fixedTransformHint;
1270 mCurrentState.modified = true;
1271 setTransactionFlags(eTransactionNeeded);
1272 return true;
1273}
1274
John Reckcdb4ed72021-02-04 13:39:33 -05001275bool Layer::setStretchEffect(const StretchEffect& effect) {
1276 StretchEffect temp = effect;
1277 temp.sanitize();
1278 if (mCurrentState.stretchEffect == temp) {
1279 return false;
1280 }
1281 mCurrentState.sequence++;
1282 mCurrentState.stretchEffect = temp;
1283 mCurrentState.modified = true;
1284 setTransactionFlags(eTransactionNeeded);
1285 return true;
1286}
1287
John Reckc00c6692021-02-16 11:37:33 -05001288StretchEffect Layer::getStretchEffect() const {
1289 if (mDrawingState.stretchEffect.hasEffect()) {
1290 return mDrawingState.stretchEffect;
1291 }
1292
1293 sp<Layer> parent = getParent();
1294 if (parent != nullptr) {
1295 auto effect = parent->getStretchEffect();
1296 if (effect.hasEffect()) {
1297 // TODO(b/179047472): Map it? Or do we make the effect be in global space?
1298 return effect;
1299 }
1300 }
1301 return StretchEffect{};
1302}
1303
Ady Abraham60e42ea2020-03-09 19:17:31 -07001304void Layer::updateTreeHasFrameRateVote() {
1305 const auto traverseTree = [&](const LayerVector::Visitor& visitor) {
1306 auto parent = getParent();
1307 while (parent) {
1308 visitor(parent.get());
1309 parent = parent->getParent();
1310 }
1311
1312 traverse(LayerVector::StateSet::Current, visitor);
1313 };
1314
1315 // update parents and children about the vote
Ady Abrahamf467f892020-07-31 16:01:53 -07001316 // First traverse the tree and count how many layers has votes. In addition
1317 // activate the layers in Scheduler's LayerHistory for it to check for changes
Ady Abraham60e42ea2020-03-09 19:17:31 -07001318 int layersWithVote = 0;
Ady Abraham44e9f3b2021-02-16 15:22:58 -08001319 traverseTree([&layersWithVote](Layer* layer) {
Marin Shalamanove8a663d2020-11-24 17:48:00 +01001320 const auto layerVotedWithDefaultCompatibility =
1321 layer->mCurrentState.frameRate.rate.isValid() &&
Ady Abraham414e8b52020-05-21 15:36:04 -07001322 layer->mCurrentState.frameRate.type == FrameRateCompatibility::Default;
1323 const auto layerVotedWithNoVote =
1324 layer->mCurrentState.frameRate.type == FrameRateCompatibility::NoVote;
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08001325 const auto layerVotedWithExactCompatibility =
1326 layer->mCurrentState.frameRate.type == FrameRateCompatibility::Exact;
Ady Abraham414e8b52020-05-21 15:36:04 -07001327
1328 // We do not count layers that are ExactOrMultiple for the same reason
1329 // we are allowing touch boost for those layers. See
1330 // RefreshRateConfigs::getBestRefreshRate for more details.
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08001331 if (layerVotedWithDefaultCompatibility || layerVotedWithNoVote ||
1332 layerVotedWithExactCompatibility) {
Ady Abraham60e42ea2020-03-09 19:17:31 -07001333 layersWithVote++;
1334 }
1335 });
1336
1337 // Now update the other layers
1338 bool transactionNeeded = false;
Ady Abraham44e9f3b2021-02-16 15:22:58 -08001339 traverseTree([layersWithVote, &transactionNeeded, this](Layer* layer) {
1340 const bool treeHasFrameRateVote = layersWithVote > 0;
1341 if (layer->mCurrentState.treeHasFrameRateVote != treeHasFrameRateVote) {
Ady Abraham60e42ea2020-03-09 19:17:31 -07001342 layer->mCurrentState.sequence++;
Ady Abraham44e9f3b2021-02-16 15:22:58 -08001343 layer->mCurrentState.treeHasFrameRateVote = treeHasFrameRateVote;
Ady Abraham60e42ea2020-03-09 19:17:31 -07001344 layer->mCurrentState.modified = true;
1345 layer->setTransactionFlags(eTransactionNeeded);
1346 transactionNeeded = true;
Ady Abraham44e9f3b2021-02-16 15:22:58 -08001347
1348 mFlinger->mScheduler->recordLayerHistory(layer, systemTime(),
1349 LayerHistory::LayerUpdateType::SetFrameRate);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001350 }
1351 });
1352
1353 if (transactionNeeded) {
1354 mFlinger->setTransactionFlags(eTraversalNeeded);
1355 }
1356}
1357
Ady Abraham71c437d2020-01-31 15:56:57 -08001358bool Layer::setFrameRate(FrameRate frameRate) {
Ana Krulec3803b8d2020-02-03 16:35:46 -08001359 if (!mFlinger->useFrameRateApi) {
1360 return false;
1361 }
Steven Thomas3172e202020-01-06 19:25:30 -08001362 if (mCurrentState.frameRate == frameRate) {
1363 return false;
1364 }
1365
1366 mCurrentState.sequence++;
1367 mCurrentState.frameRate = frameRate;
1368 mCurrentState.modified = true;
Ady Abraham60e42ea2020-03-09 19:17:31 -07001369
1370 updateTreeHasFrameRateVote();
1371
Steven Thomas3172e202020-01-06 19:25:30 -08001372 setTransactionFlags(eTransactionNeeded);
1373 return true;
1374}
1375
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001376void Layer::setFrameTimelineVsyncForBufferTransaction(const FrameTimelineInfo& info,
1377 nsecs_t postTime) {
1378 mCurrentState.postTime = postTime;
1379
1380 // Check if one of the bufferlessSurfaceFramesTX contains the same vsyncId. This can happen if
1381 // there are two transactions with the same token, the first one without a buffer and the
1382 // second one with a buffer. We promote the bufferlessSurfaceFrame to a bufferSurfaceFrameTX
1383 // in that case.
1384 auto it = mCurrentState.bufferlessSurfaceFramesTX.find(info.vsyncId);
1385 if (it != mCurrentState.bufferlessSurfaceFramesTX.end()) {
1386 // Promote the bufferlessSurfaceFrame to a bufferSurfaceFrameTX
1387 mCurrentState.bufferSurfaceFrameTX = it->second;
1388 mCurrentState.bufferlessSurfaceFramesTX.erase(it);
Adithya Srinivasan785addd2021-03-09 00:38:00 +00001389 mCurrentState.bufferSurfaceFrameTX->promoteToBuffer();
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001390 mCurrentState.bufferSurfaceFrameTX->setActualQueueTime(postTime);
1391 } else {
1392 mCurrentState.bufferSurfaceFrameTX =
1393 createSurfaceFrameForBuffer(info, postTime, mTransactionName);
1394 }
1395}
1396
1397void Layer::setFrameTimelineVsyncForBufferlessTransaction(const FrameTimelineInfo& info,
1398 nsecs_t postTime) {
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -10001399 mCurrentState.frameTimelineInfo = info;
Ady Abraham22c7b5c2020-09-22 19:33:40 -07001400 mCurrentState.postTime = postTime;
1401 mCurrentState.modified = true;
1402 setTransactionFlags(eTransactionNeeded);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001403
1404 if (const auto& bufferSurfaceFrameTX = mCurrentState.bufferSurfaceFrameTX;
1405 bufferSurfaceFrameTX != nullptr) {
1406 if (bufferSurfaceFrameTX->getToken() == info.vsyncId) {
1407 // BufferSurfaceFrame takes precedence over BufferlessSurfaceFrame. If the same token is
1408 // being used for BufferSurfaceFrame, don't create a new one.
1409 return;
1410 }
1411 }
1412 // For Transactions without a buffer, we create only one SurfaceFrame per vsyncId. If multiple
1413 // transactions use the same vsyncId, we just treat them as one SurfaceFrame (unless they are
1414 // targeting different vsyncs).
1415 auto it = mCurrentState.bufferlessSurfaceFramesTX.find(info.vsyncId);
1416 if (it == mCurrentState.bufferlessSurfaceFramesTX.end()) {
1417 auto surfaceFrame = createSurfaceFrameForTransaction(info, postTime);
1418 mCurrentState.bufferlessSurfaceFramesTX[info.vsyncId] = surfaceFrame;
1419 } else {
1420 if (it->second->getPresentState() == PresentState::Presented) {
1421 // If the SurfaceFrame was already presented, its safe to overwrite it since it must
1422 // have been from previous vsync.
1423 it->second = createSurfaceFrameForTransaction(info, postTime);
1424 }
1425 }
1426}
1427
1428void Layer::addSurfaceFrameDroppedForBuffer(
1429 std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame) {
Adithya Srinivasan061c14c2021-02-11 01:19:47 +00001430 surfaceFrame->setDropTime(systemTime());
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001431 surfaceFrame->setPresentState(PresentState::Dropped);
1432 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
1433}
1434
1435void Layer::addSurfaceFramePresentedForBuffer(
1436 std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame, nsecs_t acquireFenceTime,
1437 nsecs_t currentLatchTime) {
1438 surfaceFrame->setAcquireFenceTime(acquireFenceTime);
1439 surfaceFrame->setPresentState(PresentState::Presented, mLastLatchTime);
1440 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
1441 mLastLatchTime = currentLatchTime;
1442}
1443
1444std::shared_ptr<frametimeline::SurfaceFrame> Layer::createSurfaceFrameForTransaction(
1445 const FrameTimelineInfo& info, nsecs_t postTime) {
1446 auto surfaceFrame =
Alec Mouriadebf5c2021-01-05 12:57:36 -08001447 mFlinger->mFrameTimeline->createSurfaceFrameForToken(info, mOwnerPid, mOwnerUid,
1448 getSequence(), mName,
Adithya Srinivasan785addd2021-03-09 00:38:00 +00001449 mTransactionName,
1450 /*isBuffer*/ false);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001451 // For Transactions, the post time is considered to be both queue and acquire fence time.
1452 surfaceFrame->setActualQueueTime(postTime);
1453 surfaceFrame->setAcquireFenceTime(postTime);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001454 const auto fps = mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
1455 if (fps) {
Alec Mouri819f6302021-02-12 15:37:21 -08001456 surfaceFrame->setRenderRate(*fps);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001457 }
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001458 onSurfaceFrameCreated(surfaceFrame);
1459 return surfaceFrame;
1460}
1461
1462std::shared_ptr<frametimeline::SurfaceFrame> Layer::createSurfaceFrameForBuffer(
1463 const FrameTimelineInfo& info, nsecs_t queueTime, std::string debugName) {
1464 auto surfaceFrame =
Alec Mouriadebf5c2021-01-05 12:57:36 -08001465 mFlinger->mFrameTimeline->createSurfaceFrameForToken(info, mOwnerPid, mOwnerUid,
Adithya Srinivasan785addd2021-03-09 00:38:00 +00001466 getSequence(), mName, debugName,
1467 /*isBuffer*/ true);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001468 // For buffers, acquire fence time will set during latch.
1469 surfaceFrame->setActualQueueTime(queueTime);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001470 const auto fps = mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
1471 if (fps) {
Alec Mouri819f6302021-02-12 15:37:21 -08001472 surfaceFrame->setRenderRate(*fps);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001473 }
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001474 // TODO(b/178542907): Implement onSurfaceFrameCreated for BQLayer as well.
1475 onSurfaceFrameCreated(surfaceFrame);
1476 return surfaceFrame;
Ady Abraham74e17562020-08-24 18:18:19 -07001477}
1478
Ady Abraham60e42ea2020-03-09 19:17:31 -07001479Layer::FrameRate Layer::getFrameRateForLayerTree() const {
1480 const auto frameRate = getDrawingState().frameRate;
Marin Shalamanove8a663d2020-11-24 17:48:00 +01001481 if (frameRate.rate.isValid() || frameRate.type == FrameRateCompatibility::NoVote) {
Ady Abraham60e42ea2020-03-09 19:17:31 -07001482 return frameRate;
1483 }
1484
Ady Abrahamf467f892020-07-31 16:01:53 -07001485 // This layer doesn't have a frame rate. Check if its ancestors have a vote
1486 if (sp<Layer> parent = getParent(); parent) {
1487 if (const auto parentFrameRate = parent->getFrameRateForLayerTree();
1488 parentFrameRate.rate.isValid()) {
1489 return parentFrameRate;
1490 }
1491 }
1492
1493 // This layer and its ancestors don't have a frame rate. If one of successors
1494 // has a vote, return a NoVote for successors to set the vote
Ady Abraham60e42ea2020-03-09 19:17:31 -07001495 if (getDrawingState().treeHasFrameRateVote) {
Marin Shalamanove8a663d2020-11-24 17:48:00 +01001496 return {Fps(0.0f), FrameRateCompatibility::NoVote};
Ady Abraham60e42ea2020-03-09 19:17:31 -07001497 }
1498
1499 return frameRate;
Steven Thomas3172e202020-01-06 19:25:30 -08001500}
1501
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001502// ----------------------------------------------------------------------------
1503// pageflip handling...
1504// ----------------------------------------------------------------------------
1505
Robert Carr1f0a16a2016-10-24 16:27:39 -07001506bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001507 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001508 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001509 if (parent != nullptr && parent->isHiddenByPolicy()) {
1510 return true;
1511 }
Robert Carr1c5481e2019-07-01 14:42:27 -07001512 if (usingRelativeZ(LayerVector::StateSet::Drawing)) {
1513 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
1514 if (zOrderRelativeOf != nullptr) {
1515 if (zOrderRelativeOf->isHiddenByPolicy()) {
1516 return true;
1517 }
1518 }
1519 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001520 return s.flags & layer_state_t::eLayerHidden;
1521}
1522
David Sodman41fdfc92017-11-06 16:09:56 -08001523uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001524 // TODO: should we do something special if mSecure is set?
1525 if (mProtectedByApp) {
1526 // need a hardware-protected path to external video sink
1527 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001528 }
Riley Andrews03414a12014-07-01 14:22:59 -07001529 if (mPotentialCursor) {
1530 usage |= GraphicBuffer::USAGE_CURSOR;
1531 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001532 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001533 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001534}
1535
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001536void Layer::updateTransformHint(ui::Transform::RotationFlags transformHint) {
1537 if (mFlinger->mDebugDisableTransformHint || transformHint & ui::Transform::ROT_INVALID) {
1538 transformHint = ui::Transform::ROT_0;
Mathias Agopiana4583642011-08-23 18:03:18 -07001539 }
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001540
Vishnu Nair6213bd92020-05-08 17:42:25 -07001541 setTransformHint(transformHint);
Mathias Agopiana4583642011-08-23 18:03:18 -07001542}
1543
Mathias Agopian13127d82013-03-05 17:47:11 -08001544// ----------------------------------------------------------------------------
1545// debugging
1546// ----------------------------------------------------------------------------
1547
Marissa Wall61c58622018-07-18 10:12:20 -07001548// TODO(marissaw): add new layer state info to layer debugging
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001549LayerDebugInfo Layer::getLayerDebugInfo(const DisplayDevice* display) const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001550 using namespace std::string_literals;
1551
Kalle Raitaa099a242017-01-11 11:17:29 -08001552 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001553 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001554 info.mName = getName();
Vishnu Nair43bccf82020-06-05 10:53:37 -07001555 sp<Layer> parent = mDrawingParent.promote();
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001556 info.mParentName = parent ? parent->getName() : "none"s;
chaviw8a01fa42019-08-19 12:39:31 -07001557 info.mType = getType();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001558 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Lloyd Piquea2468662019-03-07 21:31:06 -08001559
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001560 info.mVisibleRegion = getVisibleRegion(display);
Kalle Raitaa099a242017-01-11 11:17:29 -08001561 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001562 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001563 info.mX = ds.active_legacy.transform.tx();
1564 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001565 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001566 info.mWidth = ds.active_legacy.w;
1567 info.mHeight = ds.active_legacy.h;
chaviw25714502021-02-11 10:01:08 -08001568 info.mCrop = ds.crop;
chaviw13fdc492017-06-27 12:40:18 -07001569 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001570 info.mFlags = ds.flags;
1571 info.mPixelFormat = getPixelFormat();
chaviw4244e032019-09-04 11:27:49 -07001572 info.mDataSpace = static_cast<android_dataspace>(getDataSpace());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001573 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1574 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1575 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1576 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001577 {
chaviwd62d3062019-09-04 14:48:02 -07001578 sp<const GraphicBuffer> buffer = getBuffer();
David Sodman5b4cffc2017-11-23 13:20:29 -08001579 if (buffer != 0) {
1580 info.mActiveBufferWidth = buffer->getWidth();
1581 info.mActiveBufferHeight = buffer->getHeight();
1582 info.mActiveBufferStride = buffer->getStride();
1583 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001584 } else {
1585 info.mActiveBufferWidth = 0;
1586 info.mActiveBufferHeight = 0;
1587 info.mActiveBufferStride = 0;
1588 info.mActiveBufferFormat = 0;
1589 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001590 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001591 info.mNumQueuedFrames = getQueuedFrameCount();
1592 info.mRefreshPending = isBufferLatched();
1593 info.mIsOpaque = isOpaque(ds);
1594 info.mContentDirty = contentDirty;
John Reckc00c6692021-02-16 11:37:33 -05001595 info.mStretchEffect = getStretchEffect();
Kalle Raitaa099a242017-01-11 11:17:29 -08001596 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001597}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001598
Yiwei Zhang5434a782018-12-05 18:06:32 -08001599void Layer::miniDumpHeader(std::string& result) {
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001600 result.append(kDumpTableRowLength, '-');
1601 result.append("\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001602 result.append(" Layer name\n");
1603 result.append(" Z | ");
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001604 result.append(" Window Type | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001605 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001606 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001607 result.append(" Disp Frame (LTRB) | ");
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001608 result.append(" Source Crop (LTRB) | ");
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001609 result.append(" Frame Rate (Explicit) (Seamlessness) [Focused]\n");
1610 result.append(kDumpTableRowLength, '-');
1611 result.append("\n");
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001612}
1613
1614std::string Layer::frameRateCompatibilityString(Layer::FrameRateCompatibility compatibility) {
1615 switch (compatibility) {
1616 case FrameRateCompatibility::Default:
1617 return "Default";
1618 case FrameRateCompatibility::ExactOrMultiple:
1619 return "ExactOrMultiple";
1620 case FrameRateCompatibility::NoVote:
1621 return "NoVote";
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08001622 case FrameRateCompatibility::Exact:
1623 return "Exact";
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001624 }
Dan Stozae22aec72016-08-01 13:20:59 -07001625}
1626
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001627void Layer::miniDump(std::string& result, const DisplayDevice& display) const {
1628 const auto outputLayer = findOutputLayerForDisplay(&display);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001629 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001630 return;
1631 }
1632
Yiwei Zhang5434a782018-12-05 18:06:32 -08001633 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001634 if (mName.length() > 77) {
1635 std::string shortened;
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001636 shortened.append(mName, 0, 36);
Dan Stozae22aec72016-08-01 13:20:59 -07001637 shortened.append("[...]");
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001638 shortened.append(mName, mName.length() - 36);
1639 name = std::move(shortened);
Dan Stozae22aec72016-08-01 13:20:59 -07001640 } else {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001641 name = mName;
Dan Stozae22aec72016-08-01 13:20:59 -07001642 }
1643
Yiwei Zhang5434a782018-12-05 18:06:32 -08001644 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001645
Alec Mourib416efd2018-09-06 21:01:59 +00001646 const State& layerState(getDrawingState());
Lloyd Piquede196652020-01-22 17:29:58 -08001647 const auto& outputLayerState = outputLayer->getState();
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001648
Chia-I Wu1e043612018-03-01 09:45:09 -08001649 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001650 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001651 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001652 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001653 }
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001654 StringAppendF(&result, " %10d | ", mWindowType);
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001655 StringAppendF(&result, "%10s | ", toString(getCompositionType(display)).c_str());
Lloyd Piquede196652020-01-22 17:29:58 -08001656 StringAppendF(&result, "%10s | ", toString(outputLayerState.bufferTransform).c_str());
1657 const Rect& frame = outputLayerState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001658 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Piquede196652020-01-22 17:29:58 -08001659 const FloatRect& crop = outputLayerState.sourceCrop;
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001660 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f | ", crop.left, crop.top, crop.right,
Yiwei Zhang5434a782018-12-05 18:06:32 -08001661 crop.bottom);
Ady Abrahamf467f892020-07-31 16:01:53 -07001662 const auto frameRate = getFrameRateForLayerTree();
1663 if (frameRate.rate.isValid() || frameRate.type != FrameRateCompatibility::Default) {
1664 StringAppendF(&result, "%s %15s %17s", to_string(frameRate.rate).c_str(),
1665 frameRateCompatibilityString(frameRate.type).c_str(),
1666 toString(frameRate.seamlessness).c_str());
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001667 } else {
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001668 result.append(41, ' ');
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001669 }
Dan Stozae22aec72016-08-01 13:20:59 -07001670
Ady Abrahamaae5ed52020-06-26 09:32:43 -07001671 const auto focused = isLayerFocusedBasedOnPriority(getFrameRateSelectionPriority());
1672 StringAppendF(&result, " [%s]\n", focused ? "*" : " ");
1673
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001674 result.append(kDumpTableRowLength, '-');
1675 result.append("\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001676}
Dan Stozae22aec72016-08-01 13:20:59 -07001677
Yiwei Zhang5434a782018-12-05 18:06:32 -08001678void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001679 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001680}
1681
Svetoslavd85084b2014-03-20 10:28:31 -07001682void Layer::clearFrameStats() {
1683 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001684}
1685
Jamie Gennis6547ff42013-07-16 20:12:42 -07001686void Layer::logFrameStats() {
1687 mFrameTracker.logAndResetStats(mName);
1688}
1689
Svetoslavd85084b2014-03-20 10:28:31 -07001690void Layer::getFrameStats(FrameStats* outStats) const {
1691 mFrameTracker.getStats(outStats);
1692}
1693
Yiwei Zhang5434a782018-12-05 18:06:32 -08001694void Layer::dumpFrameEvents(std::string& result) {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001695 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().c_str(), getType(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001696 Mutex::Autolock lock(mFrameEventHistoryMutex);
1697 mFrameEventHistory.checkFencesForCompletion();
1698 mFrameEventHistory.dump(result);
1699}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001700
Vishnu Nair0f085c62019-08-30 08:49:12 -07001701void Layer::dumpCallingUidPid(std::string& result) const {
chaviw250bcbb2020-08-05 11:17:54 -07001702 StringAppendF(&result, "Layer %s (%s) callingPid:%d callingUid:%d ownerUid:%d\n",
1703 getName().c_str(), getType(), mCallingPid, mCallingUid, mOwnerUid);
Vishnu Nair0f085c62019-08-30 08:49:12 -07001704}
1705
Brian Anderson5ea5e592016-12-01 16:54:33 -08001706void Layer::onDisconnect() {
1707 Mutex::Autolock lock(mFrameEventHistoryMutex);
1708 mFrameEventHistory.onDisconnect();
Yiwei Zhang1a88c402019-11-18 10:43:58 -08001709 const int32_t layerId = getSequence();
1710 mFlinger->mTimeStats->onDestroy(layerId);
1711 mFlinger->mFrameTracer->onDestroy(layerId);
Brian Anderson5ea5e592016-12-01 16:54:33 -08001712}
1713
Brian Anderson3890c392016-07-25 12:48:08 -07001714void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001715 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001716 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001717 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
Alec Mouri9a29e672020-09-14 12:39:14 -07001718 getName().c_str(), mOwnerUid, newTimestamps->postedTime);
Yiwei Zhang487340f2019-11-18 17:42:12 -08001719 mFlinger->mTimeStats->setAcquireFence(getSequence(), newTimestamps->frameNumber,
1720 newTimestamps->acquireFence);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001721 }
1722
Brian Andersond6927fb2016-07-23 23:37:30 -07001723 Mutex::Autolock lock(mFrameEventHistoryMutex);
1724 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001725 // If there are any unsignaled fences in the aquire timeline at this
1726 // point, the previously queued frame hasn't been latched yet. Go ahead
1727 // and try to get the signal time here so the syscall is taken out of
1728 // the main thread's critical path.
1729 mAcquireTimeline.updateSignalTimes();
1730 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001731 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001732 mFrameEventHistory.addQueue(*newTimestamps);
1733 }
1734
Brian Anderson3890c392016-07-25 12:48:08 -07001735 if (outDelta) {
1736 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001737 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001738}
Dan Stozae77c7662016-05-13 11:37:28 -07001739
Chia-I Wu98f1c102017-05-30 14:54:08 -07001740size_t Layer::getChildrenCount() const {
1741 size_t count = 0;
1742 for (const sp<Layer>& child : mCurrentChildren) {
1743 count += 1 + child->getChildrenCount();
1744 }
1745 return count;
1746}
1747
Robert Carr1f0a16a2016-10-24 16:27:39 -07001748void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001749 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001750 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001751
Robert Carr1f0a16a2016-10-24 16:27:39 -07001752 mCurrentChildren.add(layer);
1753 layer->setParent(this);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001754 updateTreeHasFrameRateVote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001755}
1756
1757ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001758 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001759 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001760
Robert Carr1323c952019-01-28 18:13:27 -08001761 layer->setParent(nullptr);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001762 const auto removeResult = mCurrentChildren.remove(layer);
1763
1764 updateTreeHasFrameRateVote();
1765 layer->updateTreeHasFrameRateVote();
1766
1767 return removeResult;
1768}
1769
Robert Carr15eae092018-03-23 13:43:53 -07001770void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001771 for (const sp<Layer>& child : mDrawingChildren) {
1772 child->mDrawingParent = newParent;
Vishnu Nairc652ff82019-03-15 12:48:54 -07001773 child->computeBounds(newParent->mBounds,
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001774 newParent->getTransformWithScale(newParent->getBufferScaleTransform()),
1775 newParent->mEffectiveShadowRadius);
Robert Carr578038f2018-03-09 12:25:24 -08001776 }
1777}
1778
chaviwf1961f72017-09-18 16:41:07 -07001779bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr54cf5b12019-01-25 14:02:28 -08001780 sp<Layer> newParent;
1781 if (newParentHandle != nullptr) {
1782 auto handle = static_cast<Handle*>(newParentHandle.get());
1783 newParent = handle->owner.promote();
1784 if (newParent == nullptr) {
1785 ALOGE("Unable to promote Layer handle");
1786 return false;
1787 }
1788 if (newParent == this) {
1789 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1790 return false;
1791 }
1792 }
1793
chaviwf1961f72017-09-18 16:41:07 -07001794 sp<Layer> parent = getParent();
1795 if (parent != nullptr) {
1796 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001797 }
1798
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001799 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001800 newParent->addChild(this);
1801 if (!newParent->isRemovedFromCurrentState()) {
1802 addToCurrentState();
1803 } else {
1804 onRemovedFromCurrentState();
1805 }
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001806 } else {
1807 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001808 }
1809
chaviw06178942017-07-27 10:25:59 -07001810 return true;
1811}
1812
Peiyong Lind3788632018-09-18 16:01:31 -07001813bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001814 static const mat4 identityMatrix = mat4();
1815
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001816 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001817 return false;
1818 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001819 ++mCurrentState.sequence;
1820 mCurrentState.colorTransform = matrix;
1821 mCurrentState.hasColorTransform = matrix != identityMatrix;
1822 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001823 setTransactionFlags(eTransactionNeeded);
1824 return true;
1825}
1826
chaviwf66724d2018-11-28 16:35:21 -08001827mat4 Layer::getColorTransform() const {
1828 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1829 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1830 colorTransform = parent->getColorTransform() * colorTransform;
1831 }
1832 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001833}
1834
1835bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001836 bool hasColorTransform = getDrawingState().hasColorTransform;
1837 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1838 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1839 }
1840 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001841}
1842
Chia-I Wu11481472018-05-04 10:43:19 -07001843bool Layer::isLegacyDataSpace() const {
1844 // return true when no higher bits are set
chaviw4244e032019-09-04 11:27:49 -07001845 return !(getDataSpace() &
1846 (ui::Dataspace::STANDARD_MASK | ui::Dataspace::TRANSFER_MASK |
1847 ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001848}
1849
Robert Carr1f0a16a2016-10-24 16:27:39 -07001850void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001851 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001852}
1853
chaviw301b1d82019-11-06 13:15:09 -08001854int32_t Layer::getZ(LayerVector::StateSet stateSet) const {
1855 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1856 const State& state = useDrawing ? mDrawingState : mCurrentState;
1857 return state.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001858}
1859
Robert Carr1c5481e2019-07-01 14:42:27 -07001860bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) const {
Robert Carr29abff82017-12-04 13:51:20 -08001861 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001862 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviwe5ac40f2019-09-24 16:36:55 -07001863 return state.isRelativeOf;
Robert Carr29abff82017-12-04 13:51:20 -08001864}
1865
David Sodman41fdfc92017-11-06 16:09:56 -08001866__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001867 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001868 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1869 "makeTraversalList received invalid stateSet");
1870 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1871 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001872 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001873
Robert Carr29abff82017-12-04 13:51:20 -08001874 if (state.zOrderRelatives.size() == 0) {
1875 *outSkipRelativeZUsers = true;
1876 return children;
1877 }
1878
chaviwfd462612018-05-31 16:11:27 -07001879 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001880 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001881 sp<Layer> strongRelative = weakRelative.promote();
1882 if (strongRelative != nullptr) {
1883 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001884 }
1885 }
1886
Dan Stoza412903f2017-04-27 13:42:17 -07001887 for (const sp<Layer>& child : children) {
chaviwe5ac40f2019-09-24 16:36:55 -07001888 if (child->usingRelativeZ(stateSet)) {
Robert Carr503c7042017-09-27 15:06:08 -07001889 continue;
1890 }
Robert Carrdb66e622017-04-10 16:55:57 -07001891 traverse.add(child);
1892 }
1893
1894 return traverse;
1895}
1896
Robert Carr1f0a16a2016-10-24 16:27:39 -07001897/**
Robert Carrdb66e622017-04-10 16:55:57 -07001898 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001899 */
Dan Stoza412903f2017-04-27 13:42:17 -07001900void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001901 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1902 // produce a new list for traversing, including our relatives, and not including our children
1903 // who are relatives of another surface. In the case that there are no relative Z,
1904 // makeTraversalList returns our children directly to avoid significant overhead.
1905 // However in this case we need to take the responsibility for filtering children which
1906 // are relatives of another surface here.
1907 bool skipRelativeZUsers = false;
1908 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001909
Robert Carr1f0a16a2016-10-24 16:27:39 -07001910 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001911 for (; i < list.size(); i++) {
1912 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001913 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1914 continue;
1915 }
1916
chaviw301b1d82019-11-06 13:15:09 -08001917 if (relative->getZ(stateSet) >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001918 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001919 }
Dan Stoza412903f2017-04-27 13:42:17 -07001920 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001921 }
Robert Carr29abff82017-12-04 13:51:20 -08001922
Dan Stoza412903f2017-04-27 13:42:17 -07001923 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001924 for (; i < list.size(); i++) {
1925 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001926
Robert Carr29abff82017-12-04 13:51:20 -08001927 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1928 continue;
1929 }
Dan Stoza412903f2017-04-27 13:42:17 -07001930 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001931 }
1932}
1933
1934/**
Robert Carrdb66e622017-04-10 16:55:57 -07001935 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001936 */
Dan Stoza412903f2017-04-27 13:42:17 -07001937void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1938 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001939 // See traverseInZOrder for documentation.
1940 bool skipRelativeZUsers = false;
1941 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001942
Robert Carr1f0a16a2016-10-24 16:27:39 -07001943 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001944 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001945 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001946
1947 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1948 continue;
1949 }
1950
chaviw301b1d82019-11-06 13:15:09 -08001951 if (relative->getZ(stateSet) < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001952 break;
1953 }
Dan Stoza412903f2017-04-27 13:42:17 -07001954 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001955 }
Dan Stoza412903f2017-04-27 13:42:17 -07001956 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001957 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001958 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001959
1960 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1961 continue;
1962 }
1963
Dan Stoza412903f2017-04-27 13:42:17 -07001964 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001965 }
1966}
1967
Edgar Arriaga844fa672020-01-16 14:21:42 -08001968void Layer::traverse(LayerVector::StateSet state, const LayerVector::Visitor& visitor) {
1969 visitor(this);
1970 const LayerVector& children =
1971 state == LayerVector::StateSet::Drawing ? mDrawingChildren : mCurrentChildren;
1972 for (const sp<Layer>& child : children) {
1973 child->traverse(state, visitor);
1974 }
1975}
1976
chaviw4b129c22018-04-09 16:19:43 -07001977LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1978 const std::vector<Layer*>& layersInTree) {
1979 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1980 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001981 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1982 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001983 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001984
chaviwfd462612018-05-31 16:11:27 -07001985 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001986 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1987 sp<Layer> strongRelative = weakRelative.promote();
1988 // Only add relative layers that are also descendents of the top most parent of the tree.
1989 // If a relative layer is not a descendent, then it should be ignored.
1990 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1991 traverse.add(strongRelative);
1992 }
1993 }
1994
1995 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001996 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001997 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1998 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1999 // the child here since it won't be added later as a relative.
2000 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
2001 childState.zOrderRelativeOf.promote().get())) {
2002 continue;
2003 }
2004 traverse.add(child);
2005 }
2006
2007 return traverse;
2008}
2009
2010void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
2011 LayerVector::StateSet stateSet,
2012 const LayerVector::Visitor& visitor) {
2013 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07002014
2015 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07002016 for (; i < list.size(); i++) {
2017 const auto& relative = list[i];
chaviw301b1d82019-11-06 13:15:09 -08002018 if (relative->getZ(stateSet) >= 0) {
chaviwa76b2712017-09-20 12:02:26 -07002019 break;
2020 }
chaviw4b129c22018-04-09 16:19:43 -07002021 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07002022 }
chaviw4b129c22018-04-09 16:19:43 -07002023
chaviwa76b2712017-09-20 12:02:26 -07002024 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07002025 for (; i < list.size(); i++) {
2026 const auto& relative = list[i];
2027 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07002028 }
2029}
2030
chaviw4b129c22018-04-09 16:19:43 -07002031std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
2032 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2033 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
2034
2035 std::vector<Layer*> layersInTree = {this};
2036 for (size_t i = 0; i < children.size(); i++) {
2037 const auto& child = children[i];
2038 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
2039 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
2040 }
2041
2042 return layersInTree;
2043}
2044
2045void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
2046 const LayerVector::Visitor& visitor) {
2047 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
2048 std::sort(layersInTree.begin(), layersInTree.end());
2049 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
2050}
2051
Peiyong Linefefaac2018-08-17 12:27:51 -07002052ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08002053 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002054}
2055
chaviw13fdc492017-06-27 12:40:18 -07002056half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08002057 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002058
chaviw13fdc492017-06-27 12:40:18 -07002059 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
2060 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07002061}
Robert Carr6452f122017-03-21 10:41:29 -07002062
Vishnu Nair6213bd92020-05-08 17:42:25 -07002063ui::Transform::RotationFlags Layer::getFixedTransformHint() const {
2064 ui::Transform::RotationFlags fixedTransformHint = mCurrentState.fixedTransformHint;
2065 if (fixedTransformHint != ui::Transform::ROT_INVALID) {
2066 return fixedTransformHint;
2067 }
2068 const auto& p = mCurrentParent.promote();
2069 if (!p) return fixedTransformHint;
2070 return p->getFixedTransformHint();
2071}
2072
chaviw13fdc492017-06-27 12:40:18 -07002073half4 Layer::getColor() const {
2074 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002075 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07002076}
Robert Carr6452f122017-03-21 10:41:29 -07002077
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08002078int32_t Layer::getBackgroundBlurRadius() const {
Galia Peychevada7de0e2020-12-03 17:24:35 +01002079 const auto& p = mDrawingParent.promote();
2080
2081 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
2082 return parentAlpha * getDrawingState().backgroundBlurRadius;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08002083}
2084
Galia Peychevae0acf382021-04-12 21:22:34 +02002085const std::vector<BlurRegion> Layer::getBlurRegions() const {
2086 auto regionsCopy(getDrawingState().blurRegions);
2087 int layerAlpha = getAlpha();
2088 for (auto& region : regionsCopy) {
2089 region.alpha = region.alpha * layerAlpha;
2090 }
2091 return regionsCopy;
Lucas Dupinc3800b82020-10-02 16:24:48 -07002092}
2093
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002094Layer::RoundedCornerState Layer::getRoundedCornerState() const {
2095 const auto& p = mDrawingParent.promote();
2096 if (p != nullptr) {
Peiyong Lin27016a92019-03-29 17:36:08 +00002097 RoundedCornerState parentState = p->getRoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002098 if (parentState.radius > 0) {
2099 ui::Transform t = getActiveTransform(getDrawingState());
2100 t = t.inverse();
2101 parentState.cropRect = t.transform(parentState.cropRect);
2102 // The rounded corners shader only accepts 1 corner radius for performance reasons,
2103 // but a transform matrix can define horizontal and vertical scales.
2104 // Let's take the average between both of them and pass into the shader, practically we
2105 // never do this type of transformation on windows anyway.
Lucas Dupineab7ea02020-06-03 17:27:28 -07002106 auto scaleX = sqrtf(t[0][0] * t[0][0] + t[0][1] * t[0][1]);
2107 auto scaleY = sqrtf(t[1][0] * t[1][0] + t[1][1] * t[1][1]);
2108 parentState.radius *= (scaleX + scaleY) / 2.0f;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002109 return parentState;
2110 }
2111 }
2112 const float radius = getDrawingState().cornerRadius;
Chavi Weingartena5aedbd2021-04-09 13:37:33 +00002113 return radius > 0 && getCroppedBufferSize(getDrawingState()).isValid()
2114 ? RoundedCornerState(getCroppedBufferSize(getDrawingState()).toFloatRect(), radius)
Peiyong Linb9ff23e2019-04-08 11:04:59 -07002115 : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002116}
2117
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02002118renderengine::ShadowSettings Layer::getShadowSettings(const Rect& layerStackRect) const {
Vishnu Nair08f6eae2019-11-26 14:01:39 -08002119 renderengine::ShadowSettings state = mFlinger->mDrawingState.globalShadowSettings;
2120
2121 // Shift the spot light x-position to the middle of the display and then
2122 // offset it by casting layer's screen pos.
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02002123 state.lightPos.x = (layerStackRect.width() / 2.f) - mScreenBounds.left;
Vishnu Nair08f6eae2019-11-26 14:01:39 -08002124 state.lightPos.y -= mScreenBounds.top;
2125
2126 state.length = mEffectiveShadowRadius;
2127 return state;
2128}
2129
Robert Carr1f0a16a2016-10-24 16:27:39 -07002130void Layer::commitChildList() {
2131 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
2132 const auto& child = mCurrentChildren[i];
2133 child->commitChildList();
2134 }
2135 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07002136 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002137}
2138
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002139static wp<Layer> extractLayerFromBinder(const wp<IBinder>& weakBinderHandle) {
2140 if (weakBinderHandle == nullptr) {
2141 return nullptr;
2142 }
2143 sp<IBinder> binderHandle = weakBinderHandle.promote();
2144 if (binderHandle == nullptr) {
2145 return nullptr;
2146 }
2147 sp<Layer::Handle> handle = static_cast<Layer::Handle*>(binderHandle.get());
2148 if (handle == nullptr) {
2149 return nullptr;
2150 }
2151 return handle->owner;
2152}
2153
Robert Carr720e5062018-07-30 17:45:14 -07002154void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002155 mCurrentState.inputInfo = info;
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002156 mCurrentState.touchableRegionCrop = extractLayerFromBinder(info.touchableRegionCropHandle);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002157 mCurrentState.modified = true;
2158 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07002159 setTransactionFlags(eTransactionNeeded);
2160}
2161
Alec Mouri6b9e9912020-01-21 10:50:24 -08002162LayerProto* Layer::writeToProto(LayersProto& layersProto, uint32_t traceFlags,
chaviw4c34a092020-07-08 11:30:06 -07002163 const DisplayDevice* display) {
chaviw08f3cb22020-01-13 13:17:21 -08002164 LayerProto* layerProto = layersProto.add_layers();
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002165 writeToProtoDrawingState(layerProto, traceFlags, display);
chaviw08f3cb22020-01-13 13:17:21 -08002166 writeToProtoCommonState(layerProto, LayerVector::StateSet::Drawing, traceFlags);
2167
Vishnu Nair60db8c02020-04-02 11:55:16 -07002168 if (traceFlags & SurfaceTracing::TRACE_COMPOSITION) {
2169 // Only populate for the primary display.
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002170 if (display) {
2171 const Hwc2::IComposerClient::Composition compositionType = getCompositionType(*display);
Vishnu Nair60db8c02020-04-02 11:55:16 -07002172 layerProto->set_hwc_composition_type(static_cast<HwcCompositionType>(compositionType));
2173 }
Alec Mouri6b9e9912020-01-21 10:50:24 -08002174 }
2175
chaviw08f3cb22020-01-13 13:17:21 -08002176 for (const sp<Layer>& layer : mDrawingChildren) {
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002177 layer->writeToProto(layersProto, traceFlags, display);
chaviw08f3cb22020-01-13 13:17:21 -08002178 }
chaviw6d89e2d2020-01-14 14:42:01 -08002179
2180 return layerProto;
chaviw08f3cb22020-01-13 13:17:21 -08002181}
2182
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002183void Layer::writeToProtoDrawingState(LayerProto* layerInfo, uint32_t traceFlags,
chaviw4c34a092020-07-08 11:30:06 -07002184 const DisplayDevice* display) {
Vishnu Nair6b7c5c92020-09-29 17:27:05 -07002185 const ui::Transform transform = getTransform();
Vishnu Nair8406fd72019-07-30 11:29:31 -07002186
2187 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
Vishnu Nair8406fd72019-07-30 11:29:31 -07002188
chaviwd62d3062019-09-04 14:48:02 -07002189 auto buffer = getBuffer();
Vishnu Nair8406fd72019-07-30 11:29:31 -07002190 if (buffer != nullptr) {
2191 LayerProtoHelper::writeToProto(buffer,
2192 [&]() { return layerInfo->mutable_active_buffer(); });
chaviw4244e032019-09-04 11:27:49 -07002193 LayerProtoHelper::writeToProto(ui::Transform(getBufferTransform()),
Vishnu Nair8406fd72019-07-30 11:29:31 -07002194 layerInfo->mutable_buffer_transform());
2195 }
2196 layerInfo->set_invalidate(contentDirty);
2197 layerInfo->set_is_protected(isProtected());
chaviw4244e032019-09-04 11:27:49 -07002198 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(getDataSpace())));
Vishnu Nair8406fd72019-07-30 11:29:31 -07002199 layerInfo->set_queued_frames(getQueuedFrameCount());
2200 layerInfo->set_refresh_pending(isBufferLatched());
2201 layerInfo->set_curr_frame(mCurrentFrameNumber);
2202 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
2203
2204 layerInfo->set_corner_radius(getRoundedCornerState().radius);
Ana Krulece766cc82020-04-27 13:49:13 -07002205 layerInfo->set_background_blur_radius(getBackgroundBlurRadius());
Vishnu Nair8406fd72019-07-30 11:29:31 -07002206 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
2207 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
2208 [&]() { return layerInfo->mutable_position(); });
2209 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
Vishnu Nair60db8c02020-04-02 11:55:16 -07002210 if (traceFlags & SurfaceTracing::TRACE_COMPOSITION) {
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002211 LayerProtoHelper::writeToProto(getVisibleRegion(display),
Vishnu Nair60db8c02020-04-02 11:55:16 -07002212 [&]() { return layerInfo->mutable_visible_region(); });
2213 }
Vishnu Nair8406fd72019-07-30 11:29:31 -07002214 LayerProtoHelper::writeToProto(surfaceDamageRegion,
2215 [&]() { return layerInfo->mutable_damage_region(); });
chaviwddeae262020-01-06 10:31:23 -08002216
2217 if (hasColorTransform()) {
2218 LayerProtoHelper::writeToProto(getColorTransform(),
2219 layerInfo->mutable_color_transform());
2220 }
Vishnu Nair8406fd72019-07-30 11:29:31 -07002221 }
2222
Vishnu Nair60db8c02020-04-02 11:55:16 -07002223 LayerProtoHelper::writeToProto(mSourceBounds,
2224 [&]() { return layerInfo->mutable_source_bounds(); });
2225 LayerProtoHelper::writeToProto(mScreenBounds,
2226 [&]() { return layerInfo->mutable_screen_bounds(); });
2227 LayerProtoHelper::writeToProto(getRoundedCornerState().cropRect,
2228 [&]() { return layerInfo->mutable_corner_radius_crop(); });
2229 layerInfo->set_shadow_radius(mEffectiveShadowRadius);
Vishnu Nair8406fd72019-07-30 11:29:31 -07002230}
2231
2232void Layer::writeToProtoCommonState(LayerProto* layerInfo, LayerVector::StateSet stateSet,
chaviw4c34a092020-07-08 11:30:06 -07002233 uint32_t traceFlags) {
chaviw1d044282017-09-27 12:19:28 -07002234 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2235 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002236 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07002237
chaviw766c9c52021-02-10 17:36:47 -08002238 ui::Transform requestedTransform = state.transform;
chaviw1d044282017-09-27 12:19:28 -07002239
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002240 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
2241 layerInfo->set_id(sequence);
2242 layerInfo->set_name(getName().c_str());
chaviw8a01fa42019-08-19 12:39:31 -07002243 layerInfo->set_type(getType());
chaviw1d044282017-09-27 12:19:28 -07002244
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002245 for (const auto& child : children) {
2246 layerInfo->add_children(child->sequence);
chaviw1d044282017-09-27 12:19:28 -07002247 }
chaviw1d044282017-09-27 12:19:28 -07002248
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002249 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
2250 sp<Layer> strongRelative = weakRelative.promote();
2251 if (strongRelative != nullptr) {
2252 layerInfo->add_relatives(strongRelative->sequence);
2253 }
chaviwadc40c22018-07-10 16:57:27 -07002254 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002255
2256 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
2257 [&]() { return layerInfo->mutable_transparent_region(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002258
2259 layerInfo->set_layer_stack(getLayerStack());
2260 layerInfo->set_z(state.z);
2261
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002262 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(),
2263 [&]() {
2264 return layerInfo->mutable_requested_position();
2265 });
2266
chaviw766c9c52021-02-10 17:36:47 -08002267 LayerProtoHelper::writeSizeToProto(state.width, state.height,
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002268 [&]() { return layerInfo->mutable_size(); });
2269
chaviw766c9c52021-02-10 17:36:47 -08002270 LayerProtoHelper::writeToProto(state.crop, [&]() { return layerInfo->mutable_crop(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002271
2272 layerInfo->set_is_opaque(isOpaque(state));
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002273
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002274
2275 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
2276 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
2277 LayerProtoHelper::writeToProto(state.color,
2278 [&]() { return layerInfo->mutable_requested_color(); });
2279 layerInfo->set_flags(state.flags);
2280
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002281 LayerProtoHelper::writeToProto(requestedTransform,
2282 layerInfo->mutable_requested_transform());
2283
2284 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
2285 if (parent != nullptr) {
2286 layerInfo->set_parent(parent->sequence);
2287 } else {
2288 layerInfo->set_parent(-1);
2289 }
2290
2291 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2292 if (zOrderRelativeOf != nullptr) {
2293 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
2294 } else {
2295 layerInfo->set_z_order_relative_of(-1);
2296 }
chaviw08f3cb22020-01-13 13:17:21 -08002297
2298 layerInfo->set_is_relative_of(state.isRelativeOf);
chaviw250bcbb2020-08-05 11:17:54 -07002299
2300 layerInfo->set_owner_uid(mOwnerUid);
chaviwadc40c22018-07-10 16:57:27 -07002301 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002302
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002303 if (traceFlags & SurfaceTracing::TRACE_INPUT) {
chaviw4c34a092020-07-08 11:30:06 -07002304 InputWindowInfo info;
2305 if (useDrawing) {
[1;3C2b9fc252021-02-04 16:16:50 -08002306 info = fillInputInfo({nullptr});
chaviw4c34a092020-07-08 11:30:06 -07002307 } else {
2308 info = state.inputInfo;
2309 }
2310
2311 LayerProtoHelper::writeToProto(info, state.touchableRegionCrop,
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002312 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002313 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002314
2315 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
2316 auto protoMap = layerInfo->mutable_metadata();
2317 for (const auto& entry : state.metadata.mMap) {
2318 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
2319 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002320 }
chaviw1d044282017-09-27 12:19:28 -07002321}
2322
Robert Carr2e102c92018-10-23 12:11:15 -07002323bool Layer::isRemovedFromCurrentState() const {
2324 return mRemovedFromCurrentState;
2325}
2326
chaviw39d01472021-04-08 14:26:24 -05002327ui::Transform Layer::getInputTransform() const {
2328 return getTransform();
2329}
2330
2331Rect Layer::getInputBounds() const {
2332 return getCroppedBufferSize(getDrawingState());
2333}
2334
[1;3C2b9fc252021-02-04 16:16:50 -08002335void Layer::fillInputFrameInfo(InputWindowInfo& info, const ui::Transform& toPhysicalDisplay) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002336 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002337 // If this is a portal window, set the touchableRegion to the layerBounds.
2338 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
chaviw39d01472021-04-08 14:26:24 -05002339 ? getInputBounds()
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002340 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002341 if (!layerBounds.isValid()) {
chaviw39d01472021-04-08 14:26:24 -05002342 layerBounds = getInputBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002343 }
chaviw1ff3d1e2020-07-01 15:53:47 -07002344
chaviw7e72caf2020-12-02 16:50:43 -08002345 if (!layerBounds.isValid()) {
2346 // If the layer bounds is empty, set the frame to empty and clear the transform
2347 info.frameLeft = 0;
2348 info.frameTop = 0;
2349 info.frameRight = 0;
2350 info.frameBottom = 0;
2351 info.transform.reset();
2352 return;
2353 }
2354
chaviw39d01472021-04-08 14:26:24 -05002355 ui::Transform layerToDisplay = getInputTransform();
[1;3C2b9fc252021-02-04 16:16:50 -08002356 // Transform that takes window coordinates to unrotated display coordinates
2357 ui::Transform t = toPhysicalDisplay * layerToDisplay;
chaviw7e72caf2020-12-02 16:50:43 -08002358 int32_t xSurfaceInset = info.surfaceInset;
2359 int32_t ySurfaceInset = info.surfaceInset;
[1;3C2b9fc252021-02-04 16:16:50 -08002360 // Bring screenBounds into unrotated space
2361 Rect screenBounds = toPhysicalDisplay.transform(Rect{mScreenBounds});
chaviw7e72caf2020-12-02 16:50:43 -08002362
chaviw1ff3d1e2020-07-01 15:53:47 -07002363 const float xScale = t.getScaleX();
2364 const float yScale = t.getScaleY();
2365 if (xScale != 1.0f || yScale != 1.0f) {
chaviw1ff3d1e2020-07-01 15:53:47 -07002366 xSurfaceInset = std::round(xSurfaceInset * xScale);
2367 ySurfaceInset = std::round(ySurfaceInset * yScale);
2368 }
2369
Peiyong Lin74861ad2020-10-14 11:55:33 -07002370 // Transform the layer bounds from layer coordinate space to display coordinate space.
chaviw44a6d2b2020-09-08 17:14:16 -07002371 Rect transformedLayerBounds = t.transform(layerBounds);
Ady Abraham282f1d72019-07-24 18:05:56 -07002372
2373 // clamp inset to layer bounds
chaviw44a6d2b2020-09-08 17:14:16 -07002374 xSurfaceInset = (xSurfaceInset >= 0)
2375 ? std::min(xSurfaceInset, transformedLayerBounds.getWidth() / 2)
2376 : 0;
2377 ySurfaceInset = (ySurfaceInset >= 0)
2378 ? std::min(ySurfaceInset, transformedLayerBounds.getHeight() / 2)
2379 : 0;
Ady Abraham282f1d72019-07-24 18:05:56 -07002380
Robert Carra7242302020-09-01 18:26:29 -07002381 // inset while protecting from overflow TODO(b/161235021): What is going wrong
2382 // in the overflow scenario?
2383 {
2384 int32_t tmp;
chaviw44a6d2b2020-09-08 17:14:16 -07002385 if (!__builtin_add_overflow(transformedLayerBounds.left, xSurfaceInset, &tmp))
2386 transformedLayerBounds.left = tmp;
2387 if (!__builtin_sub_overflow(transformedLayerBounds.right, xSurfaceInset, &tmp))
2388 transformedLayerBounds.right = tmp;
2389 if (!__builtin_add_overflow(transformedLayerBounds.top, ySurfaceInset, &tmp))
2390 transformedLayerBounds.top = tmp;
2391 if (!__builtin_sub_overflow(transformedLayerBounds.bottom, ySurfaceInset, &tmp))
2392 transformedLayerBounds.bottom = tmp;
Robert Carra7242302020-09-01 18:26:29 -07002393 }
Vishnu Nair8033a492018-12-05 07:27:23 -08002394
chaviw44a6d2b2020-09-08 17:14:16 -07002395 // Compute the correct transform to send to input. This will allow it to transform the
2396 // input coordinates from display space into window space. Therefore, it needs to use the
2397 // final layer frame to create the inverse transform. Since surface insets are added later,
2398 // along with the overflow, the best way to ensure we get the correct transform is to use
2399 // the final frame calculated.
2400 // 1. Take the original transform set on the window and get the inverse transform. This is
2401 // used to get the final bounds in display space (ignorning the transform). Apply the
Peiyong Lin74861ad2020-10-14 11:55:33 -07002402 // inverse transform on the layerBounds to get the untransformed frame (in layer space)
chaviw44a6d2b2020-09-08 17:14:16 -07002403 // 2. Take the top and left of the untransformed frame to get the real position on screen.
2404 // Apply the layer transform on top/left so it includes any scale or rotation. These will
2405 // be the new translation values for the transform.
2406 // 3. Update the translation of the original transform to the new translation values.
2407 // 4. Send the inverse transform to input so the coordinates can be transformed back into
2408 // window space.
2409 ui::Transform inverseTransform = t.inverse();
2410 Rect nonTransformedBounds = inverseTransform.transform(transformedLayerBounds);
2411 vec2 translation = t.transform(nonTransformedBounds.left, nonTransformedBounds.top);
chaviw1ff3d1e2020-07-01 15:53:47 -07002412 ui::Transform inputTransform(t);
chaviw44a6d2b2020-09-08 17:14:16 -07002413 inputTransform.set(translation.x, translation.y);
chaviw1ff3d1e2020-07-01 15:53:47 -07002414 info.transform = inputTransform.inverse();
2415
chaviw39cfa2e2020-11-04 14:19:02 -08002416 // We need to send the layer bounds cropped to the screenbounds since the layer can be cropped.
2417 // The frame should be the area the user sees on screen since it's used for occlusion
2418 // detection.
chaviw39cfa2e2020-11-04 14:19:02 -08002419 transformedLayerBounds.intersect(screenBounds, &transformedLayerBounds);
2420 info.frameLeft = transformedLayerBounds.left;
2421 info.frameTop = transformedLayerBounds.top;
2422 info.frameRight = transformedLayerBounds.right;
2423 info.frameBottom = transformedLayerBounds.bottom;
2424
Vishnu Nair8033a492018-12-05 07:27:23 -08002425 // Position the touchable region relative to frame screen location and restrict it to frame
2426 // bounds.
chaviw44a6d2b2020-09-08 17:14:16 -07002427 info.touchableRegion = inputTransform.transform(info.touchableRegion);
chaviw7e72caf2020-12-02 16:50:43 -08002428}
2429
[1;3C2b9fc252021-02-04 16:16:50 -08002430InputWindowInfo Layer::fillInputInfo(const sp<DisplayDevice>& display) {
chaviw7e72caf2020-12-02 16:50:43 -08002431 if (!hasInputInfo()) {
2432 mDrawingState.inputInfo.name = getName();
2433 mDrawingState.inputInfo.ownerUid = mOwnerUid;
2434 mDrawingState.inputInfo.ownerPid = mOwnerPid;
2435 mDrawingState.inputInfo.inputFeatures = InputWindowInfo::Feature::NO_INPUT_CHANNEL;
2436 mDrawingState.inputInfo.flags = InputWindowInfo::Flag::NOT_TOUCH_MODAL;
2437 mDrawingState.inputInfo.displayId = getLayerStack();
2438 }
2439
2440 InputWindowInfo info = mDrawingState.inputInfo;
2441 info.id = sequence;
2442
2443 if (info.displayId == ADISPLAY_ID_NONE) {
2444 info.displayId = getLayerStack();
2445 }
2446
[1;3C2b9fc252021-02-04 16:16:50 -08002447 // Transform that goes from "logical(rotated)" display to physical/unrotated display.
2448 // This is for when inputflinger operates in physical display-space.
2449 ui::Transform toPhysicalDisplay;
2450 if (display) {
2451 toPhysicalDisplay = display->getTransform();
2452 }
2453 fillInputFrameInfo(info, toPhysicalDisplay);
chaviw7e72caf2020-12-02 16:50:43 -08002454
Robert Carr5dc426e2020-06-10 14:29:14 -07002455 // For compatibility reasons we let layers which can receive input
2456 // receive input before they have actually submitted a buffer. Because
2457 // of this we use canReceiveInput instead of isVisible to check the
2458 // policy-visibility, ignoring the buffer state. However for layers with
2459 // hasInputInfo()==false we can use the real visibility state.
2460 // We are just using these layers for occlusion detection in
2461 // InputDispatcher, and obviously if they aren't visible they can't occlude
2462 // anything.
2463 info.visible = hasInputInfo() ? canReceiveInput() : isVisible();
Bernardo Rufinoea97d182020-08-19 14:43:14 +01002464 info.alpha = getAlpha();
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002465
2466 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2467 if (info.replaceTouchableRegionWithCrop) {
2468 if (cropLayer == nullptr) {
[1;3C2b9fc252021-02-04 16:16:50 -08002469 info.touchableRegion = Region(toPhysicalDisplay.transform(Rect{mScreenBounds}));
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002470 } else {
[1;3C2b9fc252021-02-04 16:16:50 -08002471 info.touchableRegion =
2472 Region(toPhysicalDisplay.transform(Rect{cropLayer->mScreenBounds}));
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002473 }
2474 } else if (cropLayer != nullptr) {
[1;3C2b9fc252021-02-04 16:16:50 -08002475 info.touchableRegion = info.touchableRegion.intersect(
2476 toPhysicalDisplay.transform(Rect{cropLayer->mScreenBounds}));
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002477 }
2478
chaviwaf87b3e2019-10-01 16:59:28 -07002479 // If the layer is a clone, we need to crop the input region to cloned root to prevent
2480 // touches from going outside the cloned area.
2481 if (isClone()) {
2482 sp<Layer> clonedRoot = getClonedRoot();
2483 if (clonedRoot != nullptr) {
[1;3C2b9fc252021-02-04 16:16:50 -08002484 Rect rect = toPhysicalDisplay.transform(Rect{clonedRoot->mScreenBounds});
chaviwaf87b3e2019-10-01 16:59:28 -07002485 info.touchableRegion = info.touchableRegion.intersect(rect);
2486 }
2487 }
2488
Robert Carr720e5062018-07-30 17:45:14 -07002489 return info;
2490}
2491
chaviwaf87b3e2019-10-01 16:59:28 -07002492sp<Layer> Layer::getClonedRoot() {
2493 if (mClonedChild != nullptr) {
2494 return this;
2495 }
2496 if (mDrawingParent == nullptr || mDrawingParent.promote() == nullptr) {
2497 return nullptr;
2498 }
2499 return mDrawingParent.promote()->getClonedRoot();
2500}
2501
Robert Carredd13602020-04-13 17:24:34 -07002502bool Layer::hasInputInfo() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002503 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002504}
2505
chaviw3e727cd2019-01-31 13:41:05 -08002506bool Layer::canReceiveInput() const {
Vishnu Nair82353e92019-09-12 12:38:47 -07002507 return !isHiddenByPolicy();
chaviw3e727cd2019-01-31 13:41:05 -08002508}
2509
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002510compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002511 const DisplayDevice* display) const {
2512 if (!display) return nullptr;
Lloyd Piquede196652020-01-22 17:29:58 -08002513 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionEngineLayerFE());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002514}
2515
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002516Region Layer::getVisibleRegion(const DisplayDevice* display) const {
2517 const auto outputLayer = findOutputLayerForDisplay(display);
2518 return outputLayer ? outputLayer->getState().visibleRegion : Region();
Lloyd Piquea2468662019-03-07 21:31:06 -08002519}
2520
chaviwb4c6e582019-08-16 14:35:07 -07002521void Layer::setInitialValuesForClone(const sp<Layer>& clonedFrom) {
2522 // copy drawing state from cloned layer
2523 mDrawingState = clonedFrom->mDrawingState;
2524 mClonedFrom = clonedFrom;
chaviwb4c6e582019-08-16 14:35:07 -07002525}
chaviw74b03172019-08-19 11:09:03 -07002526
2527void Layer::updateMirrorInfo() {
2528 if (mClonedChild == nullptr || !mClonedChild->isClonedFromAlive()) {
2529 // If mClonedChild is null, there is nothing to mirror. If isClonedFromAlive returns false,
2530 // it means that there is a clone, but the layer it was cloned from has been destroyed. In
2531 // that case, we want to delete the reference to the clone since we want it to get
2532 // destroyed. The root, this layer, will still be around since the client can continue
2533 // to hold a reference, but no cloned layers will be displayed.
2534 mClonedChild = nullptr;
2535 return;
2536 }
2537
2538 std::map<sp<Layer>, sp<Layer>> clonedLayersMap;
2539 // If the real layer exists and is in current state, add the clone as a child of the root.
2540 // There's no need to remove from drawingState when the layer is offscreen since currentState is
2541 // copied to drawingState for the root layer. So the clonedChild is always removed from
2542 // drawingState and then needs to be added back each traversal.
2543 if (!mClonedChild->getClonedFrom()->isRemovedFromCurrentState()) {
2544 addChildToDrawing(mClonedChild);
2545 }
2546
2547 mClonedChild->updateClonedDrawingState(clonedLayersMap);
2548 mClonedChild->updateClonedChildren(this, clonedLayersMap);
2549 mClonedChild->updateClonedRelatives(clonedLayersMap);
2550}
2551
2552void Layer::updateClonedDrawingState(std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2553 // If the layer the clone was cloned from is alive, copy the content of the drawingState
2554 // to the clone. If the real layer is no longer alive, continue traversing the children
2555 // since we may be able to pull out other children that are still alive.
2556 if (isClonedFromAlive()) {
2557 sp<Layer> clonedFrom = getClonedFrom();
2558 mDrawingState = clonedFrom->mDrawingState;
chaviw74b03172019-08-19 11:09:03 -07002559 clonedLayersMap.emplace(clonedFrom, this);
2560 }
2561
2562 // The clone layer may have children in drawingState since they may have been created and
2563 // added from a previous request to updateMirorInfo. This is to ensure we don't recreate clones
2564 // that already exist, since we can just re-use them.
2565 // The drawingChildren will not get overwritten by the currentChildren since the clones are
2566 // not updated in the regular traversal. They are skipped since the root will lose the
2567 // reference to them when it copies its currentChildren to drawing.
2568 for (sp<Layer>& child : mDrawingChildren) {
2569 child->updateClonedDrawingState(clonedLayersMap);
2570 }
2571}
2572
2573void Layer::updateClonedChildren(const sp<Layer>& mirrorRoot,
2574 std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2575 mDrawingChildren.clear();
2576
2577 if (!isClonedFromAlive()) {
2578 return;
2579 }
2580
2581 sp<Layer> clonedFrom = getClonedFrom();
2582 for (sp<Layer>& child : clonedFrom->mDrawingChildren) {
2583 if (child == mirrorRoot) {
2584 // This is to avoid cyclical mirroring.
2585 continue;
2586 }
2587 sp<Layer> clonedChild = clonedLayersMap[child];
2588 if (clonedChild == nullptr) {
2589 clonedChild = child->createClone();
2590 clonedLayersMap[child] = clonedChild;
2591 }
2592 addChildToDrawing(clonedChild);
2593 clonedChild->updateClonedChildren(mirrorRoot, clonedLayersMap);
2594 }
2595}
2596
chaviwaf87b3e2019-10-01 16:59:28 -07002597void Layer::updateClonedInputInfo(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2598 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2599 if (cropLayer != nullptr) {
2600 if (clonedLayersMap.count(cropLayer) == 0) {
2601 // Real layer had a crop layer but it's not in the cloned hierarchy. Just set to
2602 // self as crop layer to avoid going outside bounds.
2603 mDrawingState.touchableRegionCrop = this;
2604 } else {
2605 const sp<Layer>& clonedCropLayer = clonedLayersMap.at(cropLayer);
2606 mDrawingState.touchableRegionCrop = clonedCropLayer;
2607 }
2608 }
2609 // Cloned layers shouldn't handle watch outside since their z order is not determined by
2610 // WM or the client.
Michael Wright44753b12020-07-08 13:48:11 +01002611 mDrawingState.inputInfo.flags &= ~InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH;
chaviwaf87b3e2019-10-01 16:59:28 -07002612}
2613
2614void Layer::updateClonedRelatives(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
chaviw74b03172019-08-19 11:09:03 -07002615 mDrawingState.zOrderRelativeOf = nullptr;
2616 mDrawingState.zOrderRelatives.clear();
2617
2618 if (!isClonedFromAlive()) {
2619 return;
2620 }
2621
chaviwaf87b3e2019-10-01 16:59:28 -07002622 const sp<Layer>& clonedFrom = getClonedFrom();
chaviw74b03172019-08-19 11:09:03 -07002623 for (wp<Layer>& relativeWeak : clonedFrom->mDrawingState.zOrderRelatives) {
chaviwaf87b3e2019-10-01 16:59:28 -07002624 const sp<Layer>& relative = relativeWeak.promote();
2625 if (clonedLayersMap.count(relative) > 0) {
2626 auto& clonedRelative = clonedLayersMap.at(relative);
chaviw74b03172019-08-19 11:09:03 -07002627 mDrawingState.zOrderRelatives.add(clonedRelative);
2628 }
2629 }
2630
2631 // Check if the relativeLayer for the real layer is part of the cloned hierarchy.
2632 // It's possible that the layer it's relative to is outside the requested cloned hierarchy.
2633 // In that case, we treat the layer as if the relativeOf has been removed. This way, it will
2634 // still traverse the children, but the layer with the missing relativeOf will not be shown
2635 // on screen.
chaviwaf87b3e2019-10-01 16:59:28 -07002636 const sp<Layer>& relativeOf = clonedFrom->mDrawingState.zOrderRelativeOf.promote();
2637 if (clonedLayersMap.count(relativeOf) > 0) {
2638 const sp<Layer>& clonedRelativeOf = clonedLayersMap.at(relativeOf);
chaviw74b03172019-08-19 11:09:03 -07002639 mDrawingState.zOrderRelativeOf = clonedRelativeOf;
2640 }
2641
chaviwaf87b3e2019-10-01 16:59:28 -07002642 updateClonedInputInfo(clonedLayersMap);
2643
chaviw74b03172019-08-19 11:09:03 -07002644 for (sp<Layer>& child : mDrawingChildren) {
2645 child->updateClonedRelatives(clonedLayersMap);
2646 }
2647}
2648
2649void Layer::addChildToDrawing(const sp<Layer>& layer) {
2650 mDrawingChildren.add(layer);
2651 layer->mDrawingParent = this;
2652}
2653
Steven Thomas62a4cf82020-01-31 12:04:03 -08002654Layer::FrameRateCompatibility Layer::FrameRate::convertCompatibility(int8_t compatibility) {
2655 switch (compatibility) {
2656 case ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT:
2657 return FrameRateCompatibility::Default;
2658 case ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE:
2659 return FrameRateCompatibility::ExactOrMultiple;
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08002660 case ANATIVEWINDOW_FRAME_RATE_EXACT:
2661 return FrameRateCompatibility::Exact;
Steven Thomas62a4cf82020-01-31 12:04:03 -08002662 default:
2663 LOG_ALWAYS_FATAL("Invalid frame rate compatibility value %d", compatibility);
2664 return FrameRateCompatibility::Default;
2665 }
2666}
2667
Marin Shalamanovc5986772021-03-16 16:09:49 +01002668scheduler::Seamlessness Layer::FrameRate::convertChangeFrameRateStrategy(int8_t strategy) {
2669 switch (strategy) {
2670 case ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS:
2671 return Seamlessness::OnlySeamless;
2672 case ANATIVEWINDOW_CHANGE_FRAME_RATE_ALWAYS:
2673 return Seamlessness::SeamedAndSeamless;
2674 default:
2675 LOG_ALWAYS_FATAL("Invalid change frame sate strategy value %d", strategy);
2676 return Seamlessness::Default;
2677 }
2678}
2679
chaviwc5676c62020-09-18 15:01:04 -07002680bool Layer::getPrimaryDisplayOnly() const {
2681 const State& s(mDrawingState);
2682 if (s.flags & layer_state_t::eLayerSkipScreenshot) {
2683 return true;
2684 }
2685
2686 sp<Layer> parent = mDrawingParent.promote();
2687 return parent == nullptr ? false : parent->getPrimaryDisplayOnly();
2688}
2689
Mathias Agopian13127d82013-03-05 17:47:11 -08002690// ---------------------------------------------------------------------------
2691
Marin Shalamanov46084422020-10-13 12:33:42 +02002692std::ostream& operator<<(std::ostream& stream, const Layer::FrameRate& rate) {
2693 return stream << "{rate=" << rate.rate
2694 << " type=" << Layer::frameRateCompatibilityString(rate.type)
Marin Shalamanov53fc11d2020-11-20 14:00:13 +01002695 << " seamlessness=" << toString(rate.seamlessness) << "}";
Marin Shalamanov46084422020-10-13 12:33:42 +02002696}
2697
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002698}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002699
2700#if defined(__gl_h_)
2701#error "don't include gl/gl.h in this file"
2702#endif
2703
2704#if defined(__gl2_h_)
2705#error "don't include gl2/gl2.h in this file"
2706#endif
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -08002707
2708// TODO(b/129481165): remove the #pragma below and fix conversion issues
2709#pragma clang diagnostic pop // ignored "-Wconversion"