blob: b819dbe7ce9083a64282e64c0ec1a62cabcbcf22 [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
Robert Carr6a160312021-05-17 12:08:20 -0700101 mDrawingState.active_legacy.w = args.w;
102 mDrawingState.active_legacy.h = args.h;
103 mDrawingState.flags = layerFlags;
104 mDrawingState.active_legacy.transform.set(0, 0);
105 mDrawingState.crop.makeInvalid();
106 mDrawingState.requestedCrop = mDrawingState.crop;
107 mDrawingState.z = 0;
108 mDrawingState.color.a = 1.0f;
109 mDrawingState.layerStack = 0;
110 mDrawingState.sequence = 0;
111 mDrawingState.requested_legacy = mDrawingState.active_legacy;
112 mDrawingState.width = UINT32_MAX;
113 mDrawingState.height = UINT32_MAX;
114 mDrawingState.transform.set(0, 0);
115 mDrawingState.frameNumber = 0;
116 mDrawingState.bufferTransform = 0;
117 mDrawingState.transformToDisplayInverse = false;
118 mDrawingState.crop.makeInvalid();
119 mDrawingState.acquireFence = sp<Fence>::make(-1);
120 mDrawingState.acquireFenceTime = std::make_shared<FenceTime>(mDrawingState.acquireFence);
121 mDrawingState.dataspace = ui::Dataspace::UNKNOWN;
122 mDrawingState.hdrMetadata.validTypes = 0;
123 mDrawingState.surfaceDamageRegion = Region::INVALID_REGION;
124 mDrawingState.cornerRadius = 0.0f;
125 mDrawingState.backgroundBlurRadius = 0;
126 mDrawingState.api = -1;
127 mDrawingState.hasColorTransform = false;
128 mDrawingState.colorSpaceAgnostic = false;
129 mDrawingState.frameRateSelectionPriority = PRIORITY_UNSET;
130 mDrawingState.metadata = args.metadata;
131 mDrawingState.shadowRadius = 0.f;
132 mDrawingState.fixedTransformHint = ui::Transform::ROT_INVALID;
133 mDrawingState.frameTimelineInfo = {};
134 mDrawingState.postTime = -1;
135 mDrawingState.destinationFrame.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700136
Vishnu Nairc43a23c2020-05-29 14:32:27 -0700137 if (args.flags & ISurfaceComposerClient::eNoColorFill) {
138 // Set an invalid color so there is no color fill.
Robert Carr6a160312021-05-17 12:08:20 -0700139 mDrawingState.color.r = -1.0_hf;
140 mDrawingState.color.g = -1.0_hf;
141 mDrawingState.color.b = -1.0_hf;
Vishnu Nairc43a23c2020-05-29 14:32:27 -0700142 }
143
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800144 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700145 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800146 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200147 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700148
Vishnu Nair0f085c62019-08-30 08:49:12 -0700149 mCallingPid = args.callingPid;
150 mCallingUid = args.callingUid;
chaviw250bcbb2020-08-05 11:17:54 -0700151
152 if (mCallingUid == AID_GRAPHICS || mCallingUid == AID_SYSTEM) {
153 // If the system didn't send an ownerUid, use the callingUid for the ownerUid.
154 mOwnerUid = args.metadata.getInt32(METADATA_OWNER_UID, mCallingUid);
Adithya Srinivasan9febda82020-10-19 10:49:41 -0700155 mOwnerPid = args.metadata.getInt32(METADATA_OWNER_PID, mCallingPid);
chaviw250bcbb2020-08-05 11:17:54 -0700156 } else {
157 // A create layer request from a non system request cannot specify the owner uid
158 mOwnerUid = mCallingUid;
Adithya Srinivasan9febda82020-10-19 10:49:41 -0700159 mOwnerPid = mCallingPid;
chaviw250bcbb2020-08-05 11:17:54 -0700160 }
Dominik Laskowski75848362019-11-11 17:57:20 -0800161}
162
163void Layer::onFirstRef() {
164 mFlinger->onLayerFirstRef(this);
Dan Stoza436ccf32018-06-21 12:10:12 -0700165}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700166
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700167Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800168 sp<Client> c(mClientRef.promote());
169 if (c != 0) {
170 c->detachLayer(this);
171 }
172
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000173 mFrameTracker.logAndResetStats(mName);
chaviw74d90ad2019-04-26 14:45:26 -0700174 mFlinger->onLayerDestroyed(this);
Mathias Agopian96f08192010-06-02 23:28:45 -0700175}
176
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700177LayerCreationArgs::LayerCreationArgs(SurfaceFlinger* flinger, sp<Client> client, std::string name,
178 uint32_t w, uint32_t h, uint32_t flags, LayerMetadata metadata)
Vishnu Nair0f085c62019-08-30 08:49:12 -0700179 : flinger(flinger),
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700180 client(std::move(client)),
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700181 name(std::move(name)),
Vishnu Nair0f085c62019-08-30 08:49:12 -0700182 w(w),
183 h(h),
184 flags(flags),
185 metadata(std::move(metadata)) {
186 IPCThreadState* ipc = IPCThreadState::self();
187 callingPid = ipc->getCallingPid();
188 callingUid = ipc->getCallingUid();
189}
190
Mathias Agopian13127d82013-03-05 17:47:11 -0800191// ---------------------------------------------------------------------------
192// callbacks
193// ---------------------------------------------------------------------------
194
David Sodmaneb085e02017-10-05 18:49:04 -0700195/*
196 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
197 * Layer. So, the implementation is done in BufferLayer. When called on a
Vishnu Nairfa247b12020-02-11 08:58:26 -0800198 * EffectLayer object, it's essentially a NOP.
David Sodmaneb085e02017-10-05 18:49:04 -0700199 */
David Sodmaneb085e02017-10-05 18:49:04 -0700200void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800201
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700202void Layer::removeRelativeZ(const std::vector<Layer*>& layersInTree) {
Robert Carr6a160312021-05-17 12:08:20 -0700203 if (mDrawingState.zOrderRelativeOf == nullptr) {
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700204 return;
205 }
Robert Carr2e102c92018-10-23 12:11:15 -0700206
Robert Carr6a160312021-05-17 12:08:20 -0700207 sp<Layer> strongRelative = mDrawingState.zOrderRelativeOf.promote();
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700208 if (strongRelative == nullptr) {
209 setZOrderRelativeOf(nullptr);
210 return;
211 }
212
213 if (!std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
214 strongRelative->removeZOrderRelative(this);
215 mFlinger->setTransactionFlags(eTraversalNeeded);
chaviw606e5cf2019-03-01 10:12:10 -0800216 setZOrderRelativeOf(nullptr);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700217 }
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700218}
219
220void Layer::removeFromCurrentState() {
Robert Carr6a160312021-05-17 12:08:20 -0700221 if (!mRemovedFromDrawingState) {
222 mRemovedFromDrawingState = true;
Ady Abrahambe09aad2021-05-03 18:59:38 -0700223 mFlinger->mScheduler->deregisterLayer(this);
224 }
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() {
Robert Carr6a160312021-05-17 12:08:20 -0700249 if (mRemovedFromDrawingState) {
250 mRemovedFromDrawingState = false;
Ady Abrahambe09aad2021-05-03 18:59:38 -0700251 mFlinger->mScheduler->registerLayer(this);
252 }
chaviw61626f22018-11-15 16:26:27 -0800253
254 for (const auto& child : mCurrentChildren) {
255 child->addToCurrentState();
256 }
257}
258
Mathias Agopian13127d82013-03-05 17:47:11 -0800259// ---------------------------------------------------------------------------
260// set-up
261// ---------------------------------------------------------------------------
262
chaviw13fdc492017-06-27 12:40:18 -0700263bool Layer::getPremultipledAlpha() const {
264 return mPremultipliedAlpha;
265}
266
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700267sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800268 Mutex::Autolock _l(mLock);
Robert Carrc0df3122019-04-11 13:18:21 -0700269 if (mGetHandleCalled) {
270 ALOGE("Get handle called twice" );
271 return nullptr;
272 }
273 mGetHandleCalled = true;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700274 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800275}
276
277// ---------------------------------------------------------------------------
278// h/w composer set-up
279// ---------------------------------------------------------------------------
280
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700281static Rect reduce(const Rect& win, const Region& exclude) {
282 if (CC_LIKELY(exclude.isEmpty())) {
283 return win;
284 }
285 if (exclude.isRect()) {
286 return win.reduce(exclude.getBounds());
287 }
288 return Region(win).subtract(exclude).getBounds();
289}
290
Dan Stoza80d61162017-12-20 15:57:52 -0800291static FloatRect reduce(const FloatRect& win, const Region& exclude) {
292 if (CC_LIKELY(exclude.isEmpty())) {
293 return win;
294 }
295 // Convert through Rect (by rounding) for lack of FloatRegion
296 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
297}
298
Vishnu Nair4351ad52019-02-11 14:13:02 -0800299Rect Layer::getScreenBounds(bool reduceTransparentRegion) const {
Vishnu Nairf0c28512019-02-08 12:40:28 -0800300 if (!reduceTransparentRegion) {
301 return Rect{mScreenBounds};
302 }
303
304 FloatRect bounds = getBounds();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800305 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800306 // Transform to screen space.
307 bounds = t.transform(bounds);
308 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700309}
310
Vishnu Nair4351ad52019-02-11 14:13:02 -0800311FloatRect Layer::getBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000312 const State& s(getDrawingState());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800313 return getBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700314}
315
Vishnu Nairf0c28512019-02-08 12:40:28 -0800316FloatRect Layer::getBounds(const Region& activeTransparentRegion) const {
317 // Subtract the transparent region and snap to the bounds.
318 return reduce(mBounds, activeTransparentRegion);
319}
320
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700321void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform,
322 float parentShadowRadius) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800323 const State& s(getDrawingState());
324
325 // Calculate effective layer transform
326 mEffectiveTransform = parentTransform * getActiveTransform(s);
327
328 // Transform parent bounds to layer space
329 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
330
Vishnu Nairc652ff82019-03-15 12:48:54 -0700331 // Calculate source bounds
Vishnu Nair4351ad52019-02-11 14:13:02 -0800332 mSourceBounds = computeSourceBounds(parentBounds);
333
334 // Calculate bounds by croping diplay frame with layer crop and parent bounds
335 FloatRect bounds = mSourceBounds;
336 const Rect layerCrop = getCrop(s);
337 if (!layerCrop.isEmpty()) {
338 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
339 }
340 bounds = bounds.intersect(parentBounds);
341
342 mBounds = bounds;
343 mScreenBounds = mEffectiveTransform.transform(mBounds);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700344
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700345 // Use the layer's own shadow radius if set. Otherwise get the radius from
346 // parent.
347 if (s.shadowRadius > 0.f) {
348 mEffectiveShadowRadius = s.shadowRadius;
349 } else {
350 mEffectiveShadowRadius = parentShadowRadius;
351 }
352
353 // Shadow radius is passed down to only one layer so if the layer can draw shadows,
354 // don't pass it to its children.
355 const float childShadowRadius = canDrawShadows() ? 0.f : mEffectiveShadowRadius;
356
Vishnu Nair4351ad52019-02-11 14:13:02 -0800357 for (const sp<Layer>& child : mDrawingChildren) {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700358 child->computeBounds(mBounds, mEffectiveTransform, childShadowRadius);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800359 }
360}
361
Vishnu Nair60356342018-11-13 13:00:45 -0800362Rect Layer::getCroppedBufferSize(const State& s) const {
363 Rect size = getBufferSize(s);
364 Rect crop = getCrop(s);
365 if (!crop.isEmpty() && size.isValid()) {
366 size.intersect(crop, &size);
367 } else if (!crop.isEmpty()) {
368 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700369 }
Vishnu Nair60356342018-11-13 13:00:45 -0800370 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800371}
372
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700373void Layer::setupRoundedCornersCropCoordinates(Rect win,
374 const FloatRect& roundedCornersCrop) const {
375 // Translate win by the rounded corners rect coordinates, to have all values in
376 // layer coordinate space.
377 win.left -= roundedCornersCrop.left;
378 win.right -= roundedCornersCrop.left;
379 win.top -= roundedCornersCrop.top;
380 win.bottom -= roundedCornersCrop.top;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700381}
382
Lloyd Piquede196652020-01-22 17:29:58 -0800383void Layer::prepareBasicGeometryCompositionState() {
Lloyd Piquec6687342019-03-07 21:34:57 -0800384 const auto& drawingState{getDrawingState()};
385 const uint32_t layerStack = getLayerStack();
386 const auto alpha = static_cast<float>(getAlpha());
387 const bool opaque = isOpaque(drawingState);
388 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
389
390 auto blendMode = Hwc2::IComposerClient::BlendMode::NONE;
391 if (!opaque || alpha != 1.0f) {
392 blendMode = mPremultipliedAlpha ? Hwc2::IComposerClient::BlendMode::PREMULTIPLIED
393 : Hwc2::IComposerClient::BlendMode::COVERAGE;
394 }
395
Lloyd Piquede196652020-01-22 17:29:58 -0800396 auto* compositionState = editCompositionState();
397 compositionState->layerStackId =
Lloyd Piquec6687342019-03-07 21:34:57 -0800398 (layerStack != ~0u) ? std::make_optional(layerStack) : std::nullopt;
Lloyd Piquede196652020-01-22 17:29:58 -0800399 compositionState->internalOnly = getPrimaryDisplayOnly();
400 compositionState->isVisible = isVisible();
401 compositionState->isOpaque = opaque && !usesRoundedCorners && alpha == 1.f;
402 compositionState->shadowRadius = mEffectiveShadowRadius;
Lloyd Piquec6687342019-03-07 21:34:57 -0800403
Lloyd Piquede196652020-01-22 17:29:58 -0800404 compositionState->contentDirty = contentDirty;
Lloyd Piquec6687342019-03-07 21:34:57 -0800405 contentDirty = false;
406
Lloyd Piquede196652020-01-22 17:29:58 -0800407 compositionState->geomLayerBounds = mBounds;
408 compositionState->geomLayerTransform = getTransform();
409 compositionState->geomInverseLayerTransform = compositionState->geomLayerTransform.inverse();
410 compositionState->transparentRegionHint = getActiveTransparentRegion(drawingState);
Lloyd Piquec6687342019-03-07 21:34:57 -0800411
Lloyd Piquede196652020-01-22 17:29:58 -0800412 compositionState->blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
413 compositionState->alpha = alpha;
414 compositionState->backgroundBlurRadius = drawingState.backgroundBlurRadius;
Lucas Dupinc3800b82020-10-02 16:24:48 -0700415 compositionState->blurRegions = drawingState.blurRegions;
John Reckc00c6692021-02-16 11:37:33 -0500416 compositionState->stretchEffect = getStretchEffect();
Lloyd Piquec6687342019-03-07 21:34:57 -0800417}
418
Lloyd Piquede196652020-01-22 17:29:58 -0800419void Layer::prepareGeometryCompositionState() {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800420 const auto& drawingState{getDrawingState()};
Mathias Agopian13127d82013-03-05 17:47:11 -0800421
Lloyd Piquea83776c2019-01-29 18:42:32 -0800422 int type = drawingState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
423 int appId = drawingState.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700424 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400425 if (parent.get()) {
426 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800427 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
428 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
Jiwen 'Steve' Cai736c74e2019-05-28 18:33:22 -0700429 if (parentType > 0 && parentAppId > 0) {
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800430 type = parentType;
431 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700432 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400433 }
434
Lloyd Piquede196652020-01-22 17:29:58 -0800435 auto* compositionState = editCompositionState();
David Sodman15094112018-10-11 09:39:37 -0700436
Lloyd Piquede196652020-01-22 17:29:58 -0800437 compositionState->geomBufferSize = getBufferSize(drawingState);
438 compositionState->geomContentCrop = getBufferCrop();
439 compositionState->geomCrop = getCrop(drawingState);
440 compositionState->geomBufferTransform = getBufferTransform();
441 compositionState->geomBufferUsesDisplayInverseTransform = getTransformToDisplayInverse();
442 compositionState->geomUsesSourceCrop = usesSourceCrop();
443 compositionState->isSecure = isSecure();
444
Lloyd Pique8d9f8362020-02-11 19:13:09 -0800445 compositionState->metadata.clear();
446 const auto& supportedMetadata = mFlinger->getHwComposer().getSupportedLayerGenericMetadata();
447 for (const auto& [key, mandatory] : supportedMetadata) {
448 const auto& genericLayerMetadataCompatibilityMap =
449 mFlinger->getGenericLayerMetadataKeyMap();
450 auto compatIter = genericLayerMetadataCompatibilityMap.find(key);
451 if (compatIter == std::end(genericLayerMetadataCompatibilityMap)) {
452 continue;
453 }
454 const uint32_t id = compatIter->second;
455
456 auto it = drawingState.metadata.mMap.find(id);
457 if (it == std::end(drawingState.metadata.mMap)) {
458 continue;
459 }
460
461 compositionState->metadata
462 .emplace(key, compositionengine::GenericLayerMetadataEntry{mandatory, it->second});
463 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800464}
David Sodmanba340492018-08-05 21:51:33 -0700465
Lloyd Piquede196652020-01-22 17:29:58 -0800466void Layer::preparePerFrameCompositionState() {
Lloyd Pique688abd42019-02-15 15:42:24 -0800467 const auto& drawingState{getDrawingState()};
Lloyd Piquede196652020-01-22 17:29:58 -0800468 auto* compositionState = editCompositionState();
Lloyd Piquef5275482019-01-29 18:42:42 -0800469
Lloyd Piquede196652020-01-22 17:29:58 -0800470 compositionState->forceClientComposition = false;
471
472 compositionState->isColorspaceAgnostic = isColorSpaceAgnostic();
473 compositionState->dataspace = getDataSpace();
474 compositionState->colorTransform = getColorTransform();
475 compositionState->colorTransformIsIdentity = !hasColorTransform();
476 compositionState->surfaceDamage = surfaceDamageRegion;
477 compositionState->hasProtectedContent = isProtected();
Lloyd Pique688abd42019-02-15 15:42:24 -0800478
479 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700480
Lloyd Piquede196652020-01-22 17:29:58 -0800481 compositionState->isOpaque =
Lloyd Pique688abd42019-02-15 15:42:24 -0800482 isOpaque(drawingState) && !usesRoundedCorners && getAlpha() == 1.0_hf;
Lloyd Piquef5275482019-01-29 18:42:42 -0800483
484 // Force client composition for special cases known only to the front-end.
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400485 // Rounded corners no longer force client composition, since we may use a
486 // hole punch so that the layer will appear to have rounded corners.
487 if (isHdrY410() || drawShadows() || drawingState.blurRegions.size() > 0 ||
John Reckc00c6692021-02-16 11:37:33 -0500488 compositionState->stretchEffect.hasEffect()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800489 compositionState->forceClientComposition = true;
Lloyd Piquef5275482019-01-29 18:42:42 -0800490 }
491}
492
Lloyd Piquede196652020-01-22 17:29:58 -0800493void Layer::prepareCursorCompositionState() {
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800494 const State& drawingState{getDrawingState()};
Lloyd Piquede196652020-01-22 17:29:58 -0800495 auto* compositionState = editCompositionState();
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800496
497 // Apply the layer's transform, followed by the display's global transform
498 // Here we're guaranteed that the layer's transform preserves rects
499 Rect win = getCroppedBufferSize(drawingState);
500 // Subtract the transparent region and snap to the bounds
501 Rect bounds = reduce(win, getActiveTransparentRegion(drawingState));
502 Rect frame(getTransform().transform(bounds));
503
Lloyd Piquede196652020-01-22 17:29:58 -0800504 compositionState->cursorFrame = frame;
505}
506
507sp<compositionengine::LayerFE> Layer::asLayerFE() const {
508 return const_cast<compositionengine::LayerFE*>(
509 static_cast<const compositionengine::LayerFE*>(this));
510}
511
512sp<compositionengine::LayerFE> Layer::getCompositionEngineLayerFE() const {
513 return nullptr;
514}
515
516compositionengine::LayerFECompositionState* Layer::editCompositionState() {
517 return nullptr;
518}
519
520const compositionengine::LayerFECompositionState* Layer::getCompositionState() const {
521 return nullptr;
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800522}
523
Lloyd Piquef16688f2019-02-19 17:47:57 -0800524bool Layer::onPreComposition(nsecs_t) {
525 return false;
526}
527
Lloyd Piquede196652020-01-22 17:29:58 -0800528void Layer::prepareCompositionState(compositionengine::LayerFE::StateSubset subset) {
Lloyd Piquec6687342019-03-07 21:34:57 -0800529 using StateSubset = compositionengine::LayerFE::StateSubset;
Lloyd Piquef5275482019-01-29 18:42:42 -0800530
Lloyd Piquec6687342019-03-07 21:34:57 -0800531 switch (subset) {
532 case StateSubset::BasicGeometry:
Lloyd Piquede196652020-01-22 17:29:58 -0800533 prepareBasicGeometryCompositionState();
Lloyd Piquec6687342019-03-07 21:34:57 -0800534 break;
535
536 case StateSubset::GeometryAndContent:
Lloyd Piquede196652020-01-22 17:29:58 -0800537 prepareBasicGeometryCompositionState();
538 prepareGeometryCompositionState();
539 preparePerFrameCompositionState();
Lloyd Piquec6687342019-03-07 21:34:57 -0800540 break;
541
542 case StateSubset::Content:
Lloyd Piquede196652020-01-22 17:29:58 -0800543 preparePerFrameCompositionState();
544 break;
545
546 case StateSubset::Cursor:
547 prepareCursorCompositionState();
Lloyd Piquec6687342019-03-07 21:34:57 -0800548 break;
549 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800550}
Mathias Agopian29a367b2011-07-12 14:51:45 -0700551
Lloyd Piquea83776c2019-01-29 18:42:32 -0800552const char* Layer::getDebugName() const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700553 return mName.c_str();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800554}
555
Mathias Agopian13127d82013-03-05 17:47:11 -0800556// ---------------------------------------------------------------------------
557// drawing...
558// ---------------------------------------------------------------------------
559
Vishnu Nair9b079a22020-01-21 14:36:08 -0800560std::optional<compositionengine::LayerFE::LayerSettings> Layer::prepareClientComposition(
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100561 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
Lloyd Piquede196652020-01-22 17:29:58 -0800562 if (!getCompositionState()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800563 return {};
564 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800565
Vishnu Nair4351ad52019-02-11 14:13:02 -0800566 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000567 half alpha = getAlpha();
Vishnu Nair9b079a22020-01-21 14:36:08 -0800568
569 compositionengine::LayerFE::LayerSettings layerSettings;
Lloyd Piquef16688f2019-02-19 17:47:57 -0800570 layerSettings.geometry.boundaries = bounds;
chaviwc6ad8af2020-08-03 11:33:30 -0700571 layerSettings.geometry.positionTransform = getTransform().asMatrix4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000572
Derek Sollenbergerc31985e2021-05-18 16:38:17 -0400573 // skip drawing content if the targetSettings indicate the content will be occluded
Lucas Dupin0fe42e52021-05-27 17:27:15 -0700574 layerSettings.skipContentDraw =
575 layerSettings.skipContentDraw || !targetSettings.realContentIsVisible;
Derek Sollenbergerc31985e2021-05-18 16:38:17 -0400576
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000577 if (hasColorTransform()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800578 layerSettings.colorTransform = getColorTransform();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000579 }
580
581 const auto roundedCornerState = getRoundedCornerState();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800582 layerSettings.geometry.roundedCornersRadius = roundedCornerState.radius;
583 layerSettings.geometry.roundedCornersCrop = roundedCornerState.cropRect;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000584
Lloyd Piquef16688f2019-02-19 17:47:57 -0800585 layerSettings.alpha = alpha;
chaviw4244e032019-09-04 11:27:49 -0700586 layerSettings.sourceDataspace = getDataSpace();
Alec Mourif54453c2021-05-13 16:28:28 -0700587 switch (targetSettings.blurSetting) {
588 case LayerFE::ClientCompositionTargetSettings::BlurSetting::Enabled:
589 layerSettings.backgroundBlurRadius = getBackgroundBlurRadius();
590 layerSettings.blurRegions = getBlurRegions();
591 layerSettings.blurRegionTransform =
592 getActiveTransform(getDrawingState()).inverse().asMatrix4();
593 break;
594 case LayerFE::ClientCompositionTargetSettings::BlurSetting::BackgroundBlurOnly:
595 layerSettings.backgroundBlurRadius = getBackgroundBlurRadius();
596 break;
597 case LayerFE::ClientCompositionTargetSettings::BlurSetting::BlurRegionsOnly:
598 layerSettings.blurRegions = getBlurRegions();
599 layerSettings.blurRegionTransform =
600 getActiveTransform(getDrawingState()).inverse().asMatrix4();
601 break;
602 case LayerFE::ClientCompositionTargetSettings::BlurSetting::Disabled:
603 default:
604 break;
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100605 }
Nader Jawad2dfc98b2021-04-08 20:35:39 -0700606 layerSettings.stretchEffect = getStretchEffect();
Ana Krulec6eab17a2020-12-09 15:52:36 -0800607 // Record the name of the layer for debugging further down the stack.
608 layerSettings.name = getName();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800609 return layerSettings;
Mathias Agopian13127d82013-03-05 17:47:11 -0800610}
611
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800612void Layer::prepareClearClientComposition(LayerFE::LayerSettings& layerSettings,
613 bool blackout) const {
614 layerSettings.source.buffer.buffer = nullptr;
615 layerSettings.source.solidColor = half3(0.0, 0.0, 0.0);
616 layerSettings.disableBlending = true;
Vishnu Nair3b653e72020-02-24 16:40:50 -0800617 layerSettings.bufferId = 0;
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800618 layerSettings.frameNumber = 0;
619
620 // If layer is blacked out, force alpha to 1 so that we draw a black color layer.
621 layerSettings.alpha = blackout ? 1.0f : 0.0f;
Alec Mouribd17b3b2020-12-17 11:08:30 -0800622 layerSettings.name = getName();
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800623}
624
Derek Sollenbergerc31985e2021-05-18 16:38:17 -0400625// TODO(b/188891810): This method now only ever returns 0 or 1 layers so we should return
626// std::optional instead of a vector. Additionally, we should consider removing
627// this method entirely in favor of calling prepareClientComposition directly.
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800628std::vector<compositionengine::LayerFE::LayerSettings> Layer::prepareClientCompositionList(
629 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
630 std::optional<compositionengine::LayerFE::LayerSettings> layerSettings =
631 prepareClientComposition(targetSettings);
632 // Nothing to render.
633 if (!layerSettings) {
634 return {};
635 }
636
637 // HWC requests to clear this layer.
638 if (targetSettings.clearContent) {
639 prepareClearClientComposition(*layerSettings, false /* blackout */);
640 return {*layerSettings};
641 }
642
Derek Sollenbergerc31985e2021-05-18 16:38:17 -0400643 // set the shadow for the layer if needed
644 prepareShadowClientComposition(*layerSettings, targetSettings.viewport);
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800645
Derek Sollenbergerc31985e2021-05-18 16:38:17 -0400646 return {*layerSettings};
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800647}
648
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700649Hwc2::IComposerClient::Composition Layer::getCompositionType(const DisplayDevice& display) const {
650 const auto outputLayer = findOutputLayerForDisplay(&display);
Alec Mouri6b9e9912020-01-21 10:50:24 -0800651 if (outputLayer == nullptr) {
652 return Hwc2::IComposerClient::Composition::INVALID;
653 }
654 if (outputLayer->getState().hwc) {
655 return (*outputLayer->getState().hwc).hwcCompositionType;
656 } else {
657 return Hwc2::IComposerClient::Composition::CLIENT;
658 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800659}
660
Mathias Agopian13127d82013-03-05 17:47:11 -0800661// ----------------------------------------------------------------------------
662// local state
663// ----------------------------------------------------------------------------
664
David Sodman41fdfc92017-11-06 16:09:56 -0800665bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800666 const State& s(mDrawingState);
Garfield Tande619fa2020-10-02 17:13:53 -0700667 if (s.flags & layer_state_t::eLayerSecure) {
668 return true;
669 }
670
671 const auto p = mDrawingParent.promote();
672 return (p != nullptr) ? p->isSecure() : false;
Dan Stoza23116082015-06-18 14:58:39 -0700673}
674
Mathias Agopian13127d82013-03-05 17:47:11 -0800675// ----------------------------------------------------------------------------
676// transaction
677// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800678
Marissa Wall61c58622018-07-18 10:12:20 -0700679uint32_t Layer::doTransaction(uint32_t flags) {
680 ATRACE_CALL();
681
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800682 if (mChildrenChanged) {
683 flags |= eVisibleRegion;
684 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -0700685 }
686
Robert Carr0758e5d2021-03-11 22:15:04 -0800687 // TODO: This is unfortunate.
Robert Carr6a160312021-05-17 12:08:20 -0700688 mDrawingStateModified = mDrawingState.modified;
689 mDrawingState.modified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700690
Alec Mourib416efd2018-09-06 21:01:59 +0000691 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700692
Robert Carr6a160312021-05-17 12:08:20 -0700693 if (updateGeometry()) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800694 // invalidate and recompute the visible regions if needed
695 flags |= Layer::eVisibleRegion;
696 }
697
Robert Carr6a160312021-05-17 12:08:20 -0700698 if (s.sequence != mLastCommittedTxSequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800699 // invalidate and recompute the visible regions if needed
Robert Carr6a160312021-05-17 12:08:20 -0700700 mLastCommittedTxSequence = s.sequence;
Mathias Agopian13127d82013-03-05 17:47:11 -0800701 flags |= eVisibleRegion;
702 this->contentDirty = true;
703
704 // we may use linear filtering, if the matrix scales us
Robert Carr6a160312021-05-17 12:08:20 -0700705 mNeedsFiltering = getActiveTransform(s).needsBilinearFiltering();
Mathias Agopian13127d82013-03-05 17:47:11 -0800706 }
707
Robert Carr6a160312021-05-17 12:08:20 -0700708 if (mDrawingState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -0700709 flags |= eInputInfoChanged;
Robert Carr6a160312021-05-17 12:08:20 -0700710 mDrawingState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -0700711 }
712
Robert Carr6a160312021-05-17 12:08:20 -0700713 commitTransaction(mDrawingState);
Robert Carra1257842020-01-31 13:48:28 -0800714
Mathias Agopian13127d82013-03-05 17:47:11 -0800715 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800716}
717
Robert Carr6a160312021-05-17 12:08:20 -0700718void Layer::commitTransaction(State&) {
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000719 // Set the present state for all bufferlessSurfaceFramesTX to Presented. The
720 // bufferSurfaceFrameTX will be presented in latchBuffer.
721 for (auto& [token, surfaceFrame] : mDrawingState.bufferlessSurfaceFramesTX) {
722 if (surfaceFrame->getPresentState() != PresentState::Presented) {
723 // With applyPendingStates, we could end up having presented surfaceframes from previous
724 // states
725 surfaceFrame->setPresentState(PresentState::Presented);
726 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
727 }
728 }
Robert Carr6a160312021-05-17 12:08:20 -0700729 mDrawingState.bufferlessSurfaceFramesTX.clear();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700730}
731
Mathias Agopian13127d82013-03-05 17:47:11 -0800732uint32_t Layer::getTransactionFlags(uint32_t flags) {
Robert Carr38d25002021-06-11 14:30:09 -0700733 auto ret = mTransactionFlags & flags;
734 mTransactionFlags &= ~flags;
735 return ret;
Mathias Agopian13127d82013-03-05 17:47:11 -0800736}
737
738uint32_t Layer::setTransactionFlags(uint32_t flags) {
Robert Carr38d25002021-06-11 14:30:09 -0700739 return mTransactionFlags |= flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800740}
741
chaviw214c89d2019-09-04 16:03:53 -0700742bool Layer::setPosition(float x, float y) {
Robert Carr6a160312021-05-17 12:08:20 -0700743 if (mDrawingState.transform.tx() == x && mDrawingState.transform.ty() == y) return false;
744 mDrawingState.sequence++;
745 mDrawingState.transform.set(x, y);
Robert Carr69663fb2016-03-27 19:59:19 -0700746
Robert Carr6a160312021-05-17 12:08:20 -0700747 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800748 setTransactionFlags(eTransactionNeeded);
749 return true;
750}
Robert Carr82364e32016-05-15 11:27:47 -0700751
Robert Carr1f0a16a2016-10-24 16:27:39 -0700752bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
753 ssize_t idx = mCurrentChildren.indexOf(childLayer);
754 if (idx < 0) {
755 return false;
756 }
757 if (childLayer->setLayer(z)) {
758 mCurrentChildren.removeAt(idx);
759 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800760 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700761 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800762 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700763}
764
Robert Carr503c7042017-09-27 15:06:08 -0700765bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
766 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
767 ssize_t idx = mCurrentChildren.indexOf(childLayer);
768 if (idx < 0) {
769 return false;
770 }
771 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
772 mCurrentChildren.removeAt(idx);
773 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800774 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700775 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800776 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700777}
778
Robert Carrae060832016-11-28 10:51:00 -0800779bool Layer::setLayer(int32_t z) {
Robert Carr6a160312021-05-17 12:08:20 -0700780 if (mDrawingState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
781 mDrawingState.sequence++;
782 mDrawingState.z = z;
783 mDrawingState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700784
785 // Discard all relative layering.
Robert Carr6a160312021-05-17 12:08:20 -0700786 if (mDrawingState.zOrderRelativeOf != nullptr) {
787 sp<Layer> strongRelative = mDrawingState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700788 if (strongRelative != nullptr) {
789 strongRelative->removeZOrderRelative(this);
790 }
chaviw606e5cf2019-03-01 10:12:10 -0800791 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -0700792 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800793 setTransactionFlags(eTransactionNeeded);
794 return true;
795}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700796
Robert Carrdb66e622017-04-10 16:55:57 -0700797void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Robert Carr6a160312021-05-17 12:08:20 -0700798 mDrawingState.zOrderRelatives.remove(relative);
799 mDrawingState.sequence++;
800 mDrawingState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700801 setTransactionFlags(eTransactionNeeded);
802}
803
804void Layer::addZOrderRelative(const wp<Layer>& relative) {
Robert Carr6a160312021-05-17 12:08:20 -0700805 mDrawingState.zOrderRelatives.add(relative);
806 mDrawingState.modified = true;
807 mDrawingState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -0700808 setTransactionFlags(eTransactionNeeded);
809}
810
chaviw606e5cf2019-03-01 10:12:10 -0800811void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
Robert Carr6a160312021-05-17 12:08:20 -0700812 mDrawingState.zOrderRelativeOf = relativeOf;
813 mDrawingState.sequence++;
814 mDrawingState.modified = true;
815 mDrawingState.isRelativeOf = relativeOf != nullptr;
chaviwbdb8b802019-10-14 09:17:12 -0700816
chaviw606e5cf2019-03-01 10:12:10 -0800817 setTransactionFlags(eTransactionNeeded);
818}
819
Robert Carr503d2bd2017-12-04 15:49:47 -0800820bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -0700821 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
822 if (handle == nullptr) {
823 return false;
824 }
825 sp<Layer> relative = handle->owner.promote();
826 if (relative == nullptr) {
827 return false;
828 }
829
Robert Carr6a160312021-05-17 12:08:20 -0700830 if (mDrawingState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
831 mDrawingState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -0800832 return false;
833 }
834
Robert Carr6a160312021-05-17 12:08:20 -0700835 mDrawingState.sequence++;
836 mDrawingState.modified = true;
837 mDrawingState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -0700838
Robert Carr6a160312021-05-17 12:08:20 -0700839 auto oldZOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -0700840 if (oldZOrderRelativeOf != nullptr) {
841 oldZOrderRelativeOf->removeZOrderRelative(this);
842 }
chaviw606e5cf2019-03-01 10:12:10 -0800843 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -0700844 relative->addZOrderRelative(this);
845
846 setTransactionFlags(eTransactionNeeded);
847
848 return true;
849}
850
Mathias Agopian13127d82013-03-05 17:47:11 -0800851bool Layer::setSize(uint32_t w, uint32_t h) {
Robert Carr6a160312021-05-17 12:08:20 -0700852 if (mDrawingState.requested_legacy.w == w && mDrawingState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -0700853 return false;
Robert Carr6a160312021-05-17 12:08:20 -0700854 mDrawingState.requested_legacy.w = w;
855 mDrawingState.requested_legacy.h = h;
856 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800857 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -0700858
859 // record the new size, from this point on, when the client request
860 // a buffer, it'll get the new size.
Robert Carr6a160312021-05-17 12:08:20 -0700861 setDefaultBufferSize(mDrawingState.requested_legacy.w, mDrawingState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -0800862 return true;
863}
chaviw4e765532021-04-30 12:11:39 -0500864
Dan Stoza9e56aa02015-11-02 13:00:03 -0800865bool Layer::setAlpha(float alpha) {
Robert Carr6a160312021-05-17 12:08:20 -0700866 if (mDrawingState.color.a == alpha) return false;
867 mDrawingState.sequence++;
868 mDrawingState.color.a = alpha;
869 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800870 setTransactionFlags(eTransactionNeeded);
871 return true;
872}
chaviw13fdc492017-06-27 12:40:18 -0700873
Valerie Haudd0b7572019-01-29 14:59:27 -0800874bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
Robert Carr6a160312021-05-17 12:08:20 -0700875 if (!mDrawingState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -0700876 return false;
Valerie Hauaa194562019-02-05 16:21:38 -0800877 }
Robert Carr6a160312021-05-17 12:08:20 -0700878 mDrawingState.sequence++;
879 mDrawingState.modified = true;
Valerie Hauaa194562019-02-05 16:21:38 -0800880 setTransactionFlags(eTransactionNeeded);
881
Robert Carr6a160312021-05-17 12:08:20 -0700882 if (!mDrawingState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -0800883 // create background color layer if one does not yet exist
Vishnu Nairfa247b12020-02-11 08:58:26 -0800884 uint32_t flags = ISurfaceComposerClient::eFXSurfaceEffect;
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700885 std::string name = mName + "BackgroundColorLayer";
Robert Carr6a160312021-05-17 12:08:20 -0700886 mDrawingState.bgColorLayer = mFlinger->getFactory().createEffectLayer(
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700887 LayerCreationArgs(mFlinger.get(), nullptr, std::move(name), 0, 0, flags,
888 LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -0700889
Valerie Haudd0b7572019-01-29 14:59:27 -0800890 // add to child list
Robert Carr6a160312021-05-17 12:08:20 -0700891 addChild(mDrawingState.bgColorLayer);
Valerie Haudd0b7572019-01-29 14:59:27 -0800892 mFlinger->mLayersAdded = true;
893 // set up SF to handle added color layer
894 if (isRemovedFromCurrentState()) {
Robert Carr6a160312021-05-17 12:08:20 -0700895 mDrawingState.bgColorLayer->onRemovedFromCurrentState();
Valerie Haudd0b7572019-01-29 14:59:27 -0800896 }
897 mFlinger->setTransactionFlags(eTransactionNeeded);
Robert Carr6a160312021-05-17 12:08:20 -0700898 } else if (mDrawingState.bgColorLayer && alpha == 0) {
899 mDrawingState.bgColorLayer->reparent(nullptr);
900 mDrawingState.bgColorLayer = nullptr;
Valerie Haudd0b7572019-01-29 14:59:27 -0800901 return true;
902 }
903
Robert Carr6a160312021-05-17 12:08:20 -0700904 mDrawingState.bgColorLayer->setColor(color);
905 mDrawingState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
906 mDrawingState.bgColorLayer->setAlpha(alpha);
907 mDrawingState.bgColorLayer->setDataspace(dataspace);
Valerie Haudd0b7572019-01-29 14:59:27 -0800908
chaviw13fdc492017-06-27 12:40:18 -0700909 return true;
910}
911
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700912bool Layer::setCornerRadius(float cornerRadius) {
Robert Carr6a160312021-05-17 12:08:20 -0700913 if (mDrawingState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700914
Robert Carr6a160312021-05-17 12:08:20 -0700915 mDrawingState.sequence++;
916 mDrawingState.cornerRadius = cornerRadius;
917 mDrawingState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700918 setTransactionFlags(eTransactionNeeded);
919 return true;
920}
921
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800922bool Layer::setBackgroundBlurRadius(int backgroundBlurRadius) {
Robert Carr6a160312021-05-17 12:08:20 -0700923 if (mDrawingState.backgroundBlurRadius == backgroundBlurRadius) return false;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800924
Robert Carr6a160312021-05-17 12:08:20 -0700925 mDrawingState.sequence++;
926 mDrawingState.backgroundBlurRadius = backgroundBlurRadius;
927 mDrawingState.modified = true;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800928 setTransactionFlags(eTransactionNeeded);
929 return true;
930}
931
Robert Carrd4ae7f32018-06-07 16:10:57 -0700932bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
933 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -0700934 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -0700935 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
936
937 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
Hongwei Wang46213ea2020-12-04 17:17:31 -0800938 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER nor "
939 "ROTATE_SURFACE_FLINGER ignored");
Robert Carrd4ae7f32018-06-07 16:10:57 -0700940 return false;
941 }
Robert Carr6a160312021-05-17 12:08:20 -0700942 mDrawingState.sequence++;
943 mDrawingState.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
944 mDrawingState.modified = true;
945
Mathias Agopian13127d82013-03-05 17:47:11 -0800946 setTransactionFlags(eTransactionNeeded);
947 return true;
948}
Marissa Wall61c58622018-07-18 10:12:20 -0700949
Mathias Agopian13127d82013-03-05 17:47:11 -0800950bool Layer::setTransparentRegionHint(const Region& transparent) {
Robert Carr6a160312021-05-17 12:08:20 -0700951 mDrawingState.requestedTransparentRegion_legacy = transparent;
952 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800953 setTransactionFlags(eTransactionNeeded);
954 return true;
955}
Ana Krulecc84d09b2019-11-02 23:10:29 +0100956
Lucas Dupinc3800b82020-10-02 16:24:48 -0700957bool Layer::setBlurRegions(const std::vector<BlurRegion>& blurRegions) {
Robert Carr6a160312021-05-17 12:08:20 -0700958 mDrawingState.sequence++;
959 mDrawingState.blurRegions = blurRegions;
960 mDrawingState.modified = true;
Lucas Dupinc3800b82020-10-02 16:24:48 -0700961 setTransactionFlags(eTransactionNeeded);
962 return true;
963}
964
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800965bool Layer::setFlags(uint32_t flags, uint32_t mask) {
Robert Carr6a160312021-05-17 12:08:20 -0700966 const uint32_t newFlags = (mDrawingState.flags & ~mask) | (flags & mask);
967 if (mDrawingState.flags == newFlags) return false;
968 mDrawingState.sequence++;
969 mDrawingState.flags = newFlags;
970 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800971 setTransactionFlags(eTransactionNeeded);
972 return true;
973}
Robert Carr99e27f02016-06-16 15:18:02 -0700974
chaviw25714502021-02-11 10:01:08 -0800975bool Layer::setCrop(const Rect& crop) {
Robert Carr6a160312021-05-17 12:08:20 -0700976 if (mDrawingState.requestedCrop == crop) return false;
977 mDrawingState.sequence++;
978 mDrawingState.requestedCrop = crop;
979 mDrawingState.crop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -0700980
Robert Carr6a160312021-05-17 12:08:20 -0700981 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800982 setTransactionFlags(eTransactionNeeded);
983 return true;
984}
Robert Carr8d5227b2017-03-16 15:41:03 -0700985
Evan Roskyef876c92019-01-25 17:46:06 -0800986bool Layer::setMetadata(const LayerMetadata& data) {
Robert Carr6a160312021-05-17 12:08:20 -0700987 if (!mDrawingState.metadata.merge(data, true /* eraseEmpty */)) return false;
988 mDrawingState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -0800989 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800990 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500991}
992
Mathias Agopian13127d82013-03-05 17:47:11 -0800993bool Layer::setLayerStack(uint32_t layerStack) {
Robert Carr6a160312021-05-17 12:08:20 -0700994 if (mDrawingState.layerStack == layerStack) return false;
995 mDrawingState.sequence++;
996 mDrawingState.layerStack = layerStack;
997 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800998 setTransactionFlags(eTransactionNeeded);
999 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001000}
1001
Peiyong Linc502cb72019-03-01 15:00:23 -08001002bool Layer::setColorSpaceAgnostic(const bool agnostic) {
Robert Carr6a160312021-05-17 12:08:20 -07001003 if (mDrawingState.colorSpaceAgnostic == agnostic) {
Peiyong Linc502cb72019-03-01 15:00:23 -08001004 return false;
1005 }
Robert Carr6a160312021-05-17 12:08:20 -07001006 mDrawingState.sequence++;
1007 mDrawingState.colorSpaceAgnostic = agnostic;
1008 mDrawingState.modified = true;
Peiyong Linc502cb72019-03-01 15:00:23 -08001009 setTransactionFlags(eTransactionNeeded);
1010 return true;
1011}
1012
Ana Krulecc84d09b2019-11-02 23:10:29 +01001013bool Layer::setFrameRateSelectionPriority(int32_t priority) {
Robert Carr6a160312021-05-17 12:08:20 -07001014 if (mDrawingState.frameRateSelectionPriority == priority) return false;
1015 mDrawingState.frameRateSelectionPriority = priority;
1016 mDrawingState.sequence++;
1017 mDrawingState.modified = true;
Ana Krulecc84d09b2019-11-02 23:10:29 +01001018 setTransactionFlags(eTransactionNeeded);
1019 return true;
1020}
1021
1022int32_t Layer::getFrameRateSelectionPriority() const {
1023 // Check if layer has priority set.
1024 if (mDrawingState.frameRateSelectionPriority != PRIORITY_UNSET) {
1025 return mDrawingState.frameRateSelectionPriority;
1026 }
1027 // If not, search whether its parents have it set.
1028 sp<Layer> parent = getParent();
1029 if (parent != nullptr) {
1030 return parent->getFrameRateSelectionPriority();
1031 }
1032
1033 return Layer::PRIORITY_UNSET;
1034}
1035
Ady Abrahamaae5ed52020-06-26 09:32:43 -07001036bool Layer::isLayerFocusedBasedOnPriority(int32_t priority) {
1037 return priority == PRIORITY_FOCUSED_WITH_MODE || priority == PRIORITY_FOCUSED_WITHOUT_MODE;
1038};
1039
Robert Carr1f0a16a2016-10-24 16:27:39 -07001040uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001041 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001042 if (p == nullptr) {
1043 return getDrawingState().layerStack;
1044 }
1045 return p->getLayerStack();
1046}
1047
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001048bool Layer::setShadowRadius(float shadowRadius) {
Robert Carr6a160312021-05-17 12:08:20 -07001049 if (mDrawingState.shadowRadius == shadowRadius) {
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001050 return false;
1051 }
1052
Robert Carr6a160312021-05-17 12:08:20 -07001053 mDrawingState.sequence++;
1054 mDrawingState.shadowRadius = shadowRadius;
1055 mDrawingState.modified = true;
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001056 setTransactionFlags(eTransactionNeeded);
1057 return true;
1058}
1059
Vishnu Nair6213bd92020-05-08 17:42:25 -07001060bool Layer::setFixedTransformHint(ui::Transform::RotationFlags fixedTransformHint) {
Robert Carr6a160312021-05-17 12:08:20 -07001061 if (mDrawingState.fixedTransformHint == fixedTransformHint) {
Vishnu Nair6213bd92020-05-08 17:42:25 -07001062 return false;
1063 }
1064
Robert Carr6a160312021-05-17 12:08:20 -07001065 mDrawingState.sequence++;
1066 mDrawingState.fixedTransformHint = fixedTransformHint;
1067 mDrawingState.modified = true;
Vishnu Nair6213bd92020-05-08 17:42:25 -07001068 setTransactionFlags(eTransactionNeeded);
1069 return true;
1070}
1071
John Reckcdb4ed72021-02-04 13:39:33 -05001072bool Layer::setStretchEffect(const StretchEffect& effect) {
1073 StretchEffect temp = effect;
1074 temp.sanitize();
Robert Carr6a160312021-05-17 12:08:20 -07001075 if (mDrawingState.stretchEffect == temp) {
John Reckcdb4ed72021-02-04 13:39:33 -05001076 return false;
1077 }
Robert Carr6a160312021-05-17 12:08:20 -07001078 mDrawingState.sequence++;
1079 mDrawingState.stretchEffect = temp;
1080 mDrawingState.modified = true;
John Reckcdb4ed72021-02-04 13:39:33 -05001081 setTransactionFlags(eTransactionNeeded);
1082 return true;
1083}
1084
John Reckc00c6692021-02-16 11:37:33 -05001085StretchEffect Layer::getStretchEffect() const {
1086 if (mDrawingState.stretchEffect.hasEffect()) {
1087 return mDrawingState.stretchEffect;
1088 }
1089
1090 sp<Layer> parent = getParent();
1091 if (parent != nullptr) {
1092 auto effect = parent->getStretchEffect();
1093 if (effect.hasEffect()) {
1094 // TODO(b/179047472): Map it? Or do we make the effect be in global space?
1095 return effect;
1096 }
1097 }
1098 return StretchEffect{};
1099}
1100
Ady Abraham60e42ea2020-03-09 19:17:31 -07001101void Layer::updateTreeHasFrameRateVote() {
1102 const auto traverseTree = [&](const LayerVector::Visitor& visitor) {
1103 auto parent = getParent();
1104 while (parent) {
1105 visitor(parent.get());
1106 parent = parent->getParent();
1107 }
1108
1109 traverse(LayerVector::StateSet::Current, visitor);
1110 };
1111
1112 // update parents and children about the vote
Ady Abraham59fd8ff2021-04-15 20:13:30 -07001113 // First traverse the tree and count how many layers has votes.
Ady Abraham60e42ea2020-03-09 19:17:31 -07001114 int layersWithVote = 0;
Ady Abraham44e9f3b2021-02-16 15:22:58 -08001115 traverseTree([&layersWithVote](Layer* layer) {
Marin Shalamanove8a663d2020-11-24 17:48:00 +01001116 const auto layerVotedWithDefaultCompatibility =
Robert Carr6a160312021-05-17 12:08:20 -07001117 layer->mDrawingState.frameRate.rate.isValid() &&
1118 layer->mDrawingState.frameRate.type == FrameRateCompatibility::Default;
Ady Abraham414e8b52020-05-21 15:36:04 -07001119 const auto layerVotedWithNoVote =
Robert Carr6a160312021-05-17 12:08:20 -07001120 layer->mDrawingState.frameRate.type == FrameRateCompatibility::NoVote;
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08001121 const auto layerVotedWithExactCompatibility =
Robert Carr6a160312021-05-17 12:08:20 -07001122 layer->mDrawingState.frameRate.type == FrameRateCompatibility::Exact;
Ady Abraham414e8b52020-05-21 15:36:04 -07001123
1124 // We do not count layers that are ExactOrMultiple for the same reason
1125 // we are allowing touch boost for those layers. See
1126 // RefreshRateConfigs::getBestRefreshRate for more details.
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08001127 if (layerVotedWithDefaultCompatibility || layerVotedWithNoVote ||
1128 layerVotedWithExactCompatibility) {
Ady Abraham60e42ea2020-03-09 19:17:31 -07001129 layersWithVote++;
1130 }
1131 });
1132
Ady Abraham59fd8ff2021-04-15 20:13:30 -07001133 // Now we can update the tree frame rate vote for each layer in the tree
1134 const bool treeHasFrameRateVote = layersWithVote > 0;
Ady Abraham60e42ea2020-03-09 19:17:31 -07001135 bool transactionNeeded = false;
Robert Carr6a160312021-05-17 12:08:20 -07001136
Ady Abraham59fd8ff2021-04-15 20:13:30 -07001137 traverseTree([treeHasFrameRateVote, &transactionNeeded](Layer* layer) {
1138 transactionNeeded = layer->updateFrameRateForLayerTree(treeHasFrameRateVote);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001139 });
1140
1141 if (transactionNeeded) {
1142 mFlinger->setTransactionFlags(eTraversalNeeded);
1143 }
1144}
1145
Ady Abraham71c437d2020-01-31 15:56:57 -08001146bool Layer::setFrameRate(FrameRate frameRate) {
Ana Krulec3803b8d2020-02-03 16:35:46 -08001147 if (!mFlinger->useFrameRateApi) {
1148 return false;
1149 }
Robert Carr6a160312021-05-17 12:08:20 -07001150 if (mDrawingState.frameRate == frameRate) {
Steven Thomas3172e202020-01-06 19:25:30 -08001151 return false;
1152 }
1153
Robert Carr6a160312021-05-17 12:08:20 -07001154 mDrawingState.sequence++;
1155 mDrawingState.frameRate = frameRate;
1156 mDrawingState.modified = true;
Ady Abraham60e42ea2020-03-09 19:17:31 -07001157
1158 updateTreeHasFrameRateVote();
1159
Steven Thomas3172e202020-01-06 19:25:30 -08001160 setTransactionFlags(eTransactionNeeded);
1161 return true;
1162}
1163
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001164void Layer::setFrameTimelineVsyncForBufferTransaction(const FrameTimelineInfo& info,
1165 nsecs_t postTime) {
Robert Carr6a160312021-05-17 12:08:20 -07001166 mDrawingState.postTime = postTime;
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001167
1168 // Check if one of the bufferlessSurfaceFramesTX contains the same vsyncId. This can happen if
1169 // there are two transactions with the same token, the first one without a buffer and the
1170 // second one with a buffer. We promote the bufferlessSurfaceFrame to a bufferSurfaceFrameTX
1171 // in that case.
Robert Carr6a160312021-05-17 12:08:20 -07001172 auto it = mDrawingState.bufferlessSurfaceFramesTX.find(info.vsyncId);
1173 if (it != mDrawingState.bufferlessSurfaceFramesTX.end()) {
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001174 // Promote the bufferlessSurfaceFrame to a bufferSurfaceFrameTX
Robert Carr6a160312021-05-17 12:08:20 -07001175 mDrawingState.bufferSurfaceFrameTX = it->second;
1176 mDrawingState.bufferlessSurfaceFramesTX.erase(it);
1177 mDrawingState.bufferSurfaceFrameTX->promoteToBuffer();
1178 mDrawingState.bufferSurfaceFrameTX->setActualQueueTime(postTime);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001179 } else {
Robert Carr6a160312021-05-17 12:08:20 -07001180 mDrawingState.bufferSurfaceFrameTX =
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001181 createSurfaceFrameForBuffer(info, postTime, mTransactionName);
1182 }
1183}
1184
1185void Layer::setFrameTimelineVsyncForBufferlessTransaction(const FrameTimelineInfo& info,
1186 nsecs_t postTime) {
Robert Carr6a160312021-05-17 12:08:20 -07001187 mDrawingState.frameTimelineInfo = info;
1188 mDrawingState.postTime = postTime;
1189 mDrawingState.modified = true;
Ady Abraham22c7b5c2020-09-22 19:33:40 -07001190 setTransactionFlags(eTransactionNeeded);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001191
Robert Carr6a160312021-05-17 12:08:20 -07001192 if (const auto& bufferSurfaceFrameTX = mDrawingState.bufferSurfaceFrameTX;
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001193 bufferSurfaceFrameTX != nullptr) {
1194 if (bufferSurfaceFrameTX->getToken() == info.vsyncId) {
1195 // BufferSurfaceFrame takes precedence over BufferlessSurfaceFrame. If the same token is
1196 // being used for BufferSurfaceFrame, don't create a new one.
1197 return;
1198 }
1199 }
1200 // For Transactions without a buffer, we create only one SurfaceFrame per vsyncId. If multiple
1201 // transactions use the same vsyncId, we just treat them as one SurfaceFrame (unless they are
1202 // targeting different vsyncs).
Robert Carr6a160312021-05-17 12:08:20 -07001203 auto it = mDrawingState.bufferlessSurfaceFramesTX.find(info.vsyncId);
1204 if (it == mDrawingState.bufferlessSurfaceFramesTX.end()) {
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001205 auto surfaceFrame = createSurfaceFrameForTransaction(info, postTime);
Robert Carr6a160312021-05-17 12:08:20 -07001206 mDrawingState.bufferlessSurfaceFramesTX[info.vsyncId] = surfaceFrame;
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001207 } else {
1208 if (it->second->getPresentState() == PresentState::Presented) {
1209 // If the SurfaceFrame was already presented, its safe to overwrite it since it must
1210 // have been from previous vsync.
1211 it->second = createSurfaceFrameForTransaction(info, postTime);
1212 }
1213 }
1214}
1215
1216void Layer::addSurfaceFrameDroppedForBuffer(
1217 std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame) {
Adithya Srinivasan061c14c2021-02-11 01:19:47 +00001218 surfaceFrame->setDropTime(systemTime());
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001219 surfaceFrame->setPresentState(PresentState::Dropped);
1220 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
1221}
1222
1223void Layer::addSurfaceFramePresentedForBuffer(
1224 std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame, nsecs_t acquireFenceTime,
1225 nsecs_t currentLatchTime) {
1226 surfaceFrame->setAcquireFenceTime(acquireFenceTime);
1227 surfaceFrame->setPresentState(PresentState::Presented, mLastLatchTime);
1228 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
1229 mLastLatchTime = currentLatchTime;
1230}
1231
1232std::shared_ptr<frametimeline::SurfaceFrame> Layer::createSurfaceFrameForTransaction(
1233 const FrameTimelineInfo& info, nsecs_t postTime) {
1234 auto surfaceFrame =
Alec Mouriadebf5c2021-01-05 12:57:36 -08001235 mFlinger->mFrameTimeline->createSurfaceFrameForToken(info, mOwnerPid, mOwnerUid,
1236 getSequence(), mName,
Adithya Srinivasan785addd2021-03-09 00:38:00 +00001237 mTransactionName,
Adithya Srinivasan58069dc2021-06-04 20:37:02 +00001238 /*isBuffer*/ false, getGameMode());
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001239 // For Transactions, the post time is considered to be both queue and acquire fence time.
1240 surfaceFrame->setActualQueueTime(postTime);
1241 surfaceFrame->setAcquireFenceTime(postTime);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001242 const auto fps = mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
1243 if (fps) {
Alec Mouri819f6302021-02-12 15:37:21 -08001244 surfaceFrame->setRenderRate(*fps);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001245 }
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001246 onSurfaceFrameCreated(surfaceFrame);
1247 return surfaceFrame;
1248}
1249
1250std::shared_ptr<frametimeline::SurfaceFrame> Layer::createSurfaceFrameForBuffer(
1251 const FrameTimelineInfo& info, nsecs_t queueTime, std::string debugName) {
1252 auto surfaceFrame =
Alec Mouriadebf5c2021-01-05 12:57:36 -08001253 mFlinger->mFrameTimeline->createSurfaceFrameForToken(info, mOwnerPid, mOwnerUid,
Adithya Srinivasan785addd2021-03-09 00:38:00 +00001254 getSequence(), mName, debugName,
Adithya Srinivasan58069dc2021-06-04 20:37:02 +00001255 /*isBuffer*/ true, getGameMode());
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001256 // For buffers, acquire fence time will set during latch.
1257 surfaceFrame->setActualQueueTime(queueTime);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001258 const auto fps = mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
1259 if (fps) {
Alec Mouri819f6302021-02-12 15:37:21 -08001260 surfaceFrame->setRenderRate(*fps);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001261 }
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001262 // TODO(b/178542907): Implement onSurfaceFrameCreated for BQLayer as well.
1263 onSurfaceFrameCreated(surfaceFrame);
1264 return surfaceFrame;
Ady Abraham74e17562020-08-24 18:18:19 -07001265}
1266
Ady Abraham59fd8ff2021-04-15 20:13:30 -07001267bool Layer::updateFrameRateForLayerTree(bool treeHasFrameRateVote) {
Robert Carr6a160312021-05-17 12:08:20 -07001268 const auto updateDrawingState = [&](FrameRate frameRate) {
1269 if (mDrawingState.frameRateForLayerTree == frameRate) {
Ady Abraham59fd8ff2021-04-15 20:13:30 -07001270 return false;
1271 }
Robert Carr6a160312021-05-17 12:08:20 -07001272
1273 mDrawingState.frameRateForLayerTree = frameRate;
1274 mDrawingState.sequence++;
1275 mDrawingState.modified = true;
Ady Abraham59fd8ff2021-04-15 20:13:30 -07001276 setTransactionFlags(eTransactionNeeded);
Robert Carr6a160312021-05-17 12:08:20 -07001277
1278 mFlinger->mScheduler->recordLayerHistory(this, systemTime(),
1279 LayerHistory::LayerUpdateType::SetFrameRate);
1280
Ady Abraham59fd8ff2021-04-15 20:13:30 -07001281 return true;
1282 };
1283
Robert Carr6a160312021-05-17 12:08:20 -07001284 const auto frameRate = mDrawingState.frameRate;
Marin Shalamanove8a663d2020-11-24 17:48:00 +01001285 if (frameRate.rate.isValid() || frameRate.type == FrameRateCompatibility::NoVote) {
Robert Carr6a160312021-05-17 12:08:20 -07001286 return updateDrawingState(frameRate);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001287 }
1288
Ady Abrahamf467f892020-07-31 16:01:53 -07001289 // This layer doesn't have a frame rate. Check if its ancestors have a vote
Ady Abraham59fd8ff2021-04-15 20:13:30 -07001290 for (sp<Layer> parent = getParent(); parent; parent = parent->getParent()) {
Robert Carr6a160312021-05-17 12:08:20 -07001291 if (parent->mDrawingState.frameRate.rate.isValid()) {
1292 return updateDrawingState(parent->mDrawingState.frameRate);
Ady Abrahamf467f892020-07-31 16:01:53 -07001293 }
1294 }
1295
1296 // This layer and its ancestors don't have a frame rate. If one of successors
1297 // has a vote, return a NoVote for successors to set the vote
Ady Abraham59fd8ff2021-04-15 20:13:30 -07001298 if (treeHasFrameRateVote) {
Robert Carr6a160312021-05-17 12:08:20 -07001299 return updateDrawingState(FrameRate(Fps(0.0f), FrameRateCompatibility::NoVote));
Ady Abraham60e42ea2020-03-09 19:17:31 -07001300 }
1301
Robert Carr6a160312021-05-17 12:08:20 -07001302 return updateDrawingState(frameRate);
Steven Thomas3172e202020-01-06 19:25:30 -08001303}
1304
Ady Abraham59fd8ff2021-04-15 20:13:30 -07001305Layer::FrameRate Layer::getFrameRateForLayerTree() const {
1306 return getDrawingState().frameRateForLayerTree;
1307}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001308
Robert Carr1f0a16a2016-10-24 16:27:39 -07001309bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001310 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001311 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001312 if (parent != nullptr && parent->isHiddenByPolicy()) {
1313 return true;
1314 }
Robert Carr1c5481e2019-07-01 14:42:27 -07001315 if (usingRelativeZ(LayerVector::StateSet::Drawing)) {
1316 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
1317 if (zOrderRelativeOf != nullptr) {
1318 if (zOrderRelativeOf->isHiddenByPolicy()) {
1319 return true;
1320 }
1321 }
1322 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001323 return s.flags & layer_state_t::eLayerHidden;
1324}
1325
David Sodman41fdfc92017-11-06 16:09:56 -08001326uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001327 // TODO: should we do something special if mSecure is set?
1328 if (mProtectedByApp) {
1329 // need a hardware-protected path to external video sink
1330 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001331 }
Riley Andrews03414a12014-07-01 14:22:59 -07001332 if (mPotentialCursor) {
1333 usage |= GraphicBuffer::USAGE_CURSOR;
1334 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001335 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001336 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001337}
1338
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001339void Layer::updateTransformHint(ui::Transform::RotationFlags transformHint) {
1340 if (mFlinger->mDebugDisableTransformHint || transformHint & ui::Transform::ROT_INVALID) {
1341 transformHint = ui::Transform::ROT_0;
Mathias Agopiana4583642011-08-23 18:03:18 -07001342 }
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001343
Vishnu Nair6213bd92020-05-08 17:42:25 -07001344 setTransformHint(transformHint);
Mathias Agopiana4583642011-08-23 18:03:18 -07001345}
1346
Mathias Agopian13127d82013-03-05 17:47:11 -08001347// ----------------------------------------------------------------------------
1348// debugging
1349// ----------------------------------------------------------------------------
1350
Marissa Wall61c58622018-07-18 10:12:20 -07001351// TODO(marissaw): add new layer state info to layer debugging
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001352LayerDebugInfo Layer::getLayerDebugInfo(const DisplayDevice* display) const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001353 using namespace std::string_literals;
1354
Kalle Raitaa099a242017-01-11 11:17:29 -08001355 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001356 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001357 info.mName = getName();
Vishnu Nair43bccf82020-06-05 10:53:37 -07001358 sp<Layer> parent = mDrawingParent.promote();
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001359 info.mParentName = parent ? parent->getName() : "none"s;
chaviw8a01fa42019-08-19 12:39:31 -07001360 info.mType = getType();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001361 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Lloyd Piquea2468662019-03-07 21:31:06 -08001362
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001363 info.mVisibleRegion = getVisibleRegion(display);
Kalle Raitaa099a242017-01-11 11:17:29 -08001364 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001365 info.mLayerStack = getLayerStack();
chaviw4e765532021-04-30 12:11:39 -05001366 info.mX = ds.transform.tx();
1367 info.mY = ds.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001368 info.mZ = ds.z;
chaviw4e765532021-04-30 12:11:39 -05001369 info.mWidth = ds.width;
1370 info.mHeight = ds.height;
chaviw25714502021-02-11 10:01:08 -08001371 info.mCrop = ds.crop;
chaviw13fdc492017-06-27 12:40:18 -07001372 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001373 info.mFlags = ds.flags;
1374 info.mPixelFormat = getPixelFormat();
chaviw4244e032019-09-04 11:27:49 -07001375 info.mDataSpace = static_cast<android_dataspace>(getDataSpace());
chaviw4e765532021-04-30 12:11:39 -05001376 info.mMatrix[0][0] = ds.transform[0][0];
1377 info.mMatrix[0][1] = ds.transform[0][1];
1378 info.mMatrix[1][0] = ds.transform[1][0];
1379 info.mMatrix[1][1] = ds.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001380 {
chaviwd62d3062019-09-04 14:48:02 -07001381 sp<const GraphicBuffer> buffer = getBuffer();
David Sodman5b4cffc2017-11-23 13:20:29 -08001382 if (buffer != 0) {
1383 info.mActiveBufferWidth = buffer->getWidth();
1384 info.mActiveBufferHeight = buffer->getHeight();
1385 info.mActiveBufferStride = buffer->getStride();
1386 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001387 } else {
1388 info.mActiveBufferWidth = 0;
1389 info.mActiveBufferHeight = 0;
1390 info.mActiveBufferStride = 0;
1391 info.mActiveBufferFormat = 0;
1392 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001393 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001394 info.mNumQueuedFrames = getQueuedFrameCount();
1395 info.mRefreshPending = isBufferLatched();
1396 info.mIsOpaque = isOpaque(ds);
1397 info.mContentDirty = contentDirty;
John Reckc00c6692021-02-16 11:37:33 -05001398 info.mStretchEffect = getStretchEffect();
Kalle Raitaa099a242017-01-11 11:17:29 -08001399 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001400}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001401
Yiwei Zhang5434a782018-12-05 18:06:32 -08001402void Layer::miniDumpHeader(std::string& result) {
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001403 result.append(kDumpTableRowLength, '-');
1404 result.append("\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001405 result.append(" Layer name\n");
1406 result.append(" Z | ");
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001407 result.append(" Window Type | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001408 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001409 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001410 result.append(" Disp Frame (LTRB) | ");
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001411 result.append(" Source Crop (LTRB) | ");
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001412 result.append(" Frame Rate (Explicit) (Seamlessness) [Focused]\n");
1413 result.append(kDumpTableRowLength, '-');
1414 result.append("\n");
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001415}
1416
1417std::string Layer::frameRateCompatibilityString(Layer::FrameRateCompatibility compatibility) {
1418 switch (compatibility) {
1419 case FrameRateCompatibility::Default:
1420 return "Default";
1421 case FrameRateCompatibility::ExactOrMultiple:
1422 return "ExactOrMultiple";
1423 case FrameRateCompatibility::NoVote:
1424 return "NoVote";
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08001425 case FrameRateCompatibility::Exact:
1426 return "Exact";
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001427 }
Dan Stozae22aec72016-08-01 13:20:59 -07001428}
1429
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001430void Layer::miniDump(std::string& result, const DisplayDevice& display) const {
1431 const auto outputLayer = findOutputLayerForDisplay(&display);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001432 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001433 return;
1434 }
1435
Yiwei Zhang5434a782018-12-05 18:06:32 -08001436 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001437 if (mName.length() > 77) {
1438 std::string shortened;
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001439 shortened.append(mName, 0, 36);
Dan Stozae22aec72016-08-01 13:20:59 -07001440 shortened.append("[...]");
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001441 shortened.append(mName, mName.length() - 36);
1442 name = std::move(shortened);
Dan Stozae22aec72016-08-01 13:20:59 -07001443 } else {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001444 name = mName;
Dan Stozae22aec72016-08-01 13:20:59 -07001445 }
1446
Yiwei Zhang5434a782018-12-05 18:06:32 -08001447 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001448
Alec Mourib416efd2018-09-06 21:01:59 +00001449 const State& layerState(getDrawingState());
Lloyd Piquede196652020-01-22 17:29:58 -08001450 const auto& outputLayerState = outputLayer->getState();
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001451
Chia-I Wu1e043612018-03-01 09:45:09 -08001452 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001453 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001454 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001455 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001456 }
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001457 StringAppendF(&result, " %10d | ", mWindowType);
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001458 StringAppendF(&result, "%10s | ", toString(getCompositionType(display)).c_str());
Lloyd Piquede196652020-01-22 17:29:58 -08001459 StringAppendF(&result, "%10s | ", toString(outputLayerState.bufferTransform).c_str());
1460 const Rect& frame = outputLayerState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001461 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Piquede196652020-01-22 17:29:58 -08001462 const FloatRect& crop = outputLayerState.sourceCrop;
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001463 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f | ", crop.left, crop.top, crop.right,
Yiwei Zhang5434a782018-12-05 18:06:32 -08001464 crop.bottom);
Ady Abrahamf467f892020-07-31 16:01:53 -07001465 const auto frameRate = getFrameRateForLayerTree();
1466 if (frameRate.rate.isValid() || frameRate.type != FrameRateCompatibility::Default) {
1467 StringAppendF(&result, "%s %15s %17s", to_string(frameRate.rate).c_str(),
1468 frameRateCompatibilityString(frameRate.type).c_str(),
1469 toString(frameRate.seamlessness).c_str());
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001470 } else {
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001471 result.append(41, ' ');
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001472 }
Dan Stozae22aec72016-08-01 13:20:59 -07001473
Ady Abrahamaae5ed52020-06-26 09:32:43 -07001474 const auto focused = isLayerFocusedBasedOnPriority(getFrameRateSelectionPriority());
1475 StringAppendF(&result, " [%s]\n", focused ? "*" : " ");
1476
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001477 result.append(kDumpTableRowLength, '-');
1478 result.append("\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001479}
Dan Stozae22aec72016-08-01 13:20:59 -07001480
Yiwei Zhang5434a782018-12-05 18:06:32 -08001481void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001482 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001483}
1484
Svetoslavd85084b2014-03-20 10:28:31 -07001485void Layer::clearFrameStats() {
1486 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001487}
1488
Jamie Gennis6547ff42013-07-16 20:12:42 -07001489void Layer::logFrameStats() {
1490 mFrameTracker.logAndResetStats(mName);
1491}
1492
Svetoslavd85084b2014-03-20 10:28:31 -07001493void Layer::getFrameStats(FrameStats* outStats) const {
1494 mFrameTracker.getStats(outStats);
1495}
1496
Yiwei Zhang5434a782018-12-05 18:06:32 -08001497void Layer::dumpFrameEvents(std::string& result) {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001498 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().c_str(), getType(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001499 Mutex::Autolock lock(mFrameEventHistoryMutex);
1500 mFrameEventHistory.checkFencesForCompletion();
1501 mFrameEventHistory.dump(result);
1502}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001503
Vishnu Nair0f085c62019-08-30 08:49:12 -07001504void Layer::dumpCallingUidPid(std::string& result) const {
chaviw250bcbb2020-08-05 11:17:54 -07001505 StringAppendF(&result, "Layer %s (%s) callingPid:%d callingUid:%d ownerUid:%d\n",
1506 getName().c_str(), getType(), mCallingPid, mCallingUid, mOwnerUid);
Vishnu Nair0f085c62019-08-30 08:49:12 -07001507}
1508
Brian Anderson5ea5e592016-12-01 16:54:33 -08001509void Layer::onDisconnect() {
1510 Mutex::Autolock lock(mFrameEventHistoryMutex);
1511 mFrameEventHistory.onDisconnect();
Yiwei Zhang1a88c402019-11-18 10:43:58 -08001512 const int32_t layerId = getSequence();
1513 mFlinger->mTimeStats->onDestroy(layerId);
1514 mFlinger->mFrameTracer->onDestroy(layerId);
Brian Anderson5ea5e592016-12-01 16:54:33 -08001515}
1516
Brian Anderson3890c392016-07-25 12:48:08 -07001517void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001518 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001519 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001520 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
Adithya Srinivasan58069dc2021-06-04 20:37:02 +00001521 getName().c_str(), mOwnerUid, newTimestamps->postedTime,
1522 getGameMode());
Yiwei Zhang487340f2019-11-18 17:42:12 -08001523 mFlinger->mTimeStats->setAcquireFence(getSequence(), newTimestamps->frameNumber,
1524 newTimestamps->acquireFence);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001525 }
1526
Brian Andersond6927fb2016-07-23 23:37:30 -07001527 Mutex::Autolock lock(mFrameEventHistoryMutex);
1528 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001529 // If there are any unsignaled fences in the aquire timeline at this
1530 // point, the previously queued frame hasn't been latched yet. Go ahead
1531 // and try to get the signal time here so the syscall is taken out of
1532 // the main thread's critical path.
1533 mAcquireTimeline.updateSignalTimes();
1534 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001535 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001536 mFrameEventHistory.addQueue(*newTimestamps);
1537 }
1538
Brian Anderson3890c392016-07-25 12:48:08 -07001539 if (outDelta) {
1540 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001541 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001542}
Dan Stozae77c7662016-05-13 11:37:28 -07001543
Chia-I Wu98f1c102017-05-30 14:54:08 -07001544size_t Layer::getChildrenCount() const {
1545 size_t count = 0;
1546 for (const sp<Layer>& child : mCurrentChildren) {
1547 count += 1 + child->getChildrenCount();
1548 }
1549 return count;
1550}
1551
Adithya Srinivasanac977e62021-05-21 22:50:56 +00001552void Layer::setGameModeForTree(int parentGameMode) {
1553 int gameMode = parentGameMode;
Robert Carr6a160312021-05-17 12:08:20 -07001554 auto& currentState = getDrawingState();
Adithya Srinivasanac977e62021-05-21 22:50:56 +00001555 if (currentState.metadata.has(METADATA_GAME_MODE)) {
1556 gameMode = currentState.metadata.getInt32(METADATA_GAME_MODE, 0);
1557 }
1558 setGameMode(gameMode);
1559 for (const sp<Layer>& child : mCurrentChildren) {
1560 child->setGameModeForTree(gameMode);
1561 }
1562}
1563
Robert Carr1f0a16a2016-10-24 16:27:39 -07001564void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001565 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001566 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001567
Robert Carr1f0a16a2016-10-24 16:27:39 -07001568 mCurrentChildren.add(layer);
1569 layer->setParent(this);
Adithya Srinivasanac977e62021-05-21 22:50:56 +00001570 layer->setGameModeForTree(mGameMode);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001571 updateTreeHasFrameRateVote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001572}
1573
1574ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001575 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001576 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001577
Robert Carr1323c952019-01-28 18:13:27 -08001578 layer->setParent(nullptr);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001579 const auto removeResult = mCurrentChildren.remove(layer);
1580
1581 updateTreeHasFrameRateVote();
Adithya Srinivasanac977e62021-05-21 22:50:56 +00001582 layer->setGameModeForTree(0);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001583 layer->updateTreeHasFrameRateVote();
1584
1585 return removeResult;
1586}
1587
Robert Carr15eae092018-03-23 13:43:53 -07001588void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001589 for (const sp<Layer>& child : mDrawingChildren) {
1590 child->mDrawingParent = newParent;
Vishnu Nair6bdec7d2021-05-10 15:01:13 -07001591 child->computeBounds(newParent->mBounds, newParent->mEffectiveTransform,
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001592 newParent->mEffectiveShadowRadius);
Robert Carr578038f2018-03-09 12:25:24 -08001593 }
1594}
1595
chaviwf1961f72017-09-18 16:41:07 -07001596bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr54cf5b12019-01-25 14:02:28 -08001597 sp<Layer> newParent;
1598 if (newParentHandle != nullptr) {
1599 auto handle = static_cast<Handle*>(newParentHandle.get());
1600 newParent = handle->owner.promote();
1601 if (newParent == nullptr) {
1602 ALOGE("Unable to promote Layer handle");
1603 return false;
1604 }
1605 if (newParent == this) {
1606 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1607 return false;
1608 }
1609 }
1610
chaviwf1961f72017-09-18 16:41:07 -07001611 sp<Layer> parent = getParent();
1612 if (parent != nullptr) {
1613 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001614 }
1615
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001616 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001617 newParent->addChild(this);
1618 if (!newParent->isRemovedFromCurrentState()) {
1619 addToCurrentState();
1620 } else {
1621 onRemovedFromCurrentState();
1622 }
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001623 } else {
1624 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001625 }
1626
chaviw06178942017-07-27 10:25:59 -07001627 return true;
1628}
1629
Peiyong Lind3788632018-09-18 16:01:31 -07001630bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001631 static const mat4 identityMatrix = mat4();
1632
Robert Carr6a160312021-05-17 12:08:20 -07001633 if (mDrawingState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001634 return false;
1635 }
Robert Carr6a160312021-05-17 12:08:20 -07001636 ++mDrawingState.sequence;
1637 mDrawingState.colorTransform = matrix;
1638 mDrawingState.hasColorTransform = matrix != identityMatrix;
1639 mDrawingState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001640 setTransactionFlags(eTransactionNeeded);
1641 return true;
1642}
1643
chaviwf66724d2018-11-28 16:35:21 -08001644mat4 Layer::getColorTransform() const {
1645 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1646 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1647 colorTransform = parent->getColorTransform() * colorTransform;
1648 }
1649 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001650}
1651
1652bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001653 bool hasColorTransform = getDrawingState().hasColorTransform;
1654 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1655 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1656 }
1657 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001658}
1659
Chia-I Wu11481472018-05-04 10:43:19 -07001660bool Layer::isLegacyDataSpace() const {
1661 // return true when no higher bits are set
chaviw4244e032019-09-04 11:27:49 -07001662 return !(getDataSpace() &
1663 (ui::Dataspace::STANDARD_MASK | ui::Dataspace::TRANSFER_MASK |
1664 ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001665}
1666
Robert Carr1f0a16a2016-10-24 16:27:39 -07001667void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001668 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001669}
1670
Robert Carr6a160312021-05-17 12:08:20 -07001671int32_t Layer::getZ(LayerVector::StateSet) const {
1672 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001673}
1674
Robert Carr1c5481e2019-07-01 14:42:27 -07001675bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) const {
Robert Carr29abff82017-12-04 13:51:20 -08001676 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Robert Carr6a160312021-05-17 12:08:20 -07001677 const State& state = useDrawing ? mDrawingState : mDrawingState;
chaviwe5ac40f2019-09-24 16:36:55 -07001678 return state.isRelativeOf;
Robert Carr29abff82017-12-04 13:51:20 -08001679}
1680
David Sodman41fdfc92017-11-06 16:09:56 -08001681__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001682 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001683 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1684 "makeTraversalList received invalid stateSet");
1685 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1686 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Robert Carr6a160312021-05-17 12:08:20 -07001687 const State& state = useDrawing ? mDrawingState : mDrawingState;
Dan Stoza412903f2017-04-27 13:42:17 -07001688
Robert Carr29abff82017-12-04 13:51:20 -08001689 if (state.zOrderRelatives.size() == 0) {
1690 *outSkipRelativeZUsers = true;
1691 return children;
1692 }
1693
chaviwfd462612018-05-31 16:11:27 -07001694 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001695 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001696 sp<Layer> strongRelative = weakRelative.promote();
1697 if (strongRelative != nullptr) {
1698 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001699 }
1700 }
1701
Dan Stoza412903f2017-04-27 13:42:17 -07001702 for (const sp<Layer>& child : children) {
chaviwe5ac40f2019-09-24 16:36:55 -07001703 if (child->usingRelativeZ(stateSet)) {
Robert Carr503c7042017-09-27 15:06:08 -07001704 continue;
1705 }
Robert Carrdb66e622017-04-10 16:55:57 -07001706 traverse.add(child);
1707 }
1708
1709 return traverse;
1710}
1711
Robert Carr1f0a16a2016-10-24 16:27:39 -07001712/**
Robert Carrdb66e622017-04-10 16:55:57 -07001713 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001714 */
Dan Stoza412903f2017-04-27 13:42:17 -07001715void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001716 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1717 // produce a new list for traversing, including our relatives, and not including our children
1718 // who are relatives of another surface. In the case that there are no relative Z,
1719 // makeTraversalList returns our children directly to avoid significant overhead.
1720 // However in this case we need to take the responsibility for filtering children which
1721 // are relatives of another surface here.
1722 bool skipRelativeZUsers = false;
1723 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001724
Robert Carr1f0a16a2016-10-24 16:27:39 -07001725 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001726 for (; i < list.size(); i++) {
1727 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001728 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1729 continue;
1730 }
1731
chaviw301b1d82019-11-06 13:15:09 -08001732 if (relative->getZ(stateSet) >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001733 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001734 }
Dan Stoza412903f2017-04-27 13:42:17 -07001735 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001736 }
Robert Carr29abff82017-12-04 13:51:20 -08001737
Dan Stoza412903f2017-04-27 13:42:17 -07001738 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001739 for (; i < list.size(); i++) {
1740 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001741
Robert Carr29abff82017-12-04 13:51:20 -08001742 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1743 continue;
1744 }
Dan Stoza412903f2017-04-27 13:42:17 -07001745 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001746 }
1747}
1748
1749/**
Robert Carrdb66e622017-04-10 16:55:57 -07001750 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001751 */
Dan Stoza412903f2017-04-27 13:42:17 -07001752void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1753 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001754 // See traverseInZOrder for documentation.
1755 bool skipRelativeZUsers = false;
1756 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001757
Robert Carr1f0a16a2016-10-24 16:27:39 -07001758 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001759 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001760 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001761
1762 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1763 continue;
1764 }
1765
chaviw301b1d82019-11-06 13:15:09 -08001766 if (relative->getZ(stateSet) < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001767 break;
1768 }
Dan Stoza412903f2017-04-27 13:42:17 -07001769 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001770 }
Dan Stoza412903f2017-04-27 13:42:17 -07001771 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001772 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001773 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001774
1775 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1776 continue;
1777 }
1778
Dan Stoza412903f2017-04-27 13:42:17 -07001779 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001780 }
1781}
1782
Edgar Arriaga844fa672020-01-16 14:21:42 -08001783void Layer::traverse(LayerVector::StateSet state, const LayerVector::Visitor& visitor) {
1784 visitor(this);
1785 const LayerVector& children =
Robert Carr6a160312021-05-17 12:08:20 -07001786 state == LayerVector::StateSet::Drawing ? mDrawingChildren : mCurrentChildren;
Edgar Arriaga844fa672020-01-16 14:21:42 -08001787 for (const sp<Layer>& child : children) {
1788 child->traverse(state, visitor);
1789 }
1790}
1791
chaviw4b129c22018-04-09 16:19:43 -07001792LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1793 const std::vector<Layer*>& layersInTree) {
1794 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1795 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001796 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1797 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Robert Carr6a160312021-05-17 12:08:20 -07001798 const State& state = useDrawing ? mDrawingState : mDrawingState;
chaviw4b129c22018-04-09 16:19:43 -07001799
chaviwfd462612018-05-31 16:11:27 -07001800 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001801 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1802 sp<Layer> strongRelative = weakRelative.promote();
1803 // Only add relative layers that are also descendents of the top most parent of the tree.
1804 // If a relative layer is not a descendent, then it should be ignored.
1805 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1806 traverse.add(strongRelative);
1807 }
1808 }
1809
1810 for (const sp<Layer>& child : children) {
Robert Carr6a160312021-05-17 12:08:20 -07001811 const State& childState = useDrawing ? child->mDrawingState : child->mDrawingState;
chaviw4b129c22018-04-09 16:19:43 -07001812 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1813 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1814 // the child here since it won't be added later as a relative.
1815 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1816 childState.zOrderRelativeOf.promote().get())) {
1817 continue;
1818 }
1819 traverse.add(child);
1820 }
1821
1822 return traverse;
1823}
1824
1825void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1826 LayerVector::StateSet stateSet,
1827 const LayerVector::Visitor& visitor) {
1828 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001829
1830 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001831 for (; i < list.size(); i++) {
1832 const auto& relative = list[i];
chaviw301b1d82019-11-06 13:15:09 -08001833 if (relative->getZ(stateSet) >= 0) {
chaviwa76b2712017-09-20 12:02:26 -07001834 break;
1835 }
chaviw4b129c22018-04-09 16:19:43 -07001836 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001837 }
chaviw4b129c22018-04-09 16:19:43 -07001838
chaviwa76b2712017-09-20 12:02:26 -07001839 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001840 for (; i < list.size(); i++) {
1841 const auto& relative = list[i];
1842 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001843 }
1844}
1845
chaviw4b129c22018-04-09 16:19:43 -07001846std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1847 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1848 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1849
1850 std::vector<Layer*> layersInTree = {this};
1851 for (size_t i = 0; i < children.size(); i++) {
1852 const auto& child = children[i];
1853 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1854 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1855 }
1856
1857 return layersInTree;
1858}
1859
1860void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1861 const LayerVector::Visitor& visitor) {
1862 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1863 std::sort(layersInTree.begin(), layersInTree.end());
1864 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1865}
1866
Peiyong Linefefaac2018-08-17 12:27:51 -07001867ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001868 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001869}
1870
chaviw13fdc492017-06-27 12:40:18 -07001871half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001872 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001873
chaviw13fdc492017-06-27 12:40:18 -07001874 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1875 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001876}
Robert Carr6452f122017-03-21 10:41:29 -07001877
Vishnu Nair6213bd92020-05-08 17:42:25 -07001878ui::Transform::RotationFlags Layer::getFixedTransformHint() const {
Robert Carr6a160312021-05-17 12:08:20 -07001879 ui::Transform::RotationFlags fixedTransformHint = mDrawingState.fixedTransformHint;
Vishnu Nair6213bd92020-05-08 17:42:25 -07001880 if (fixedTransformHint != ui::Transform::ROT_INVALID) {
1881 return fixedTransformHint;
1882 }
1883 const auto& p = mCurrentParent.promote();
1884 if (!p) return fixedTransformHint;
1885 return p->getFixedTransformHint();
1886}
1887
chaviw13fdc492017-06-27 12:40:18 -07001888half4 Layer::getColor() const {
1889 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001890 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001891}
Robert Carr6452f122017-03-21 10:41:29 -07001892
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001893int32_t Layer::getBackgroundBlurRadius() const {
Galia Peychevada7de0e2020-12-03 17:24:35 +01001894 const auto& p = mDrawingParent.promote();
1895
1896 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1897 return parentAlpha * getDrawingState().backgroundBlurRadius;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001898}
1899
Galia Peychevae0acf382021-04-12 21:22:34 +02001900const std::vector<BlurRegion> Layer::getBlurRegions() const {
1901 auto regionsCopy(getDrawingState().blurRegions);
Galia Peycheva3c286542021-06-17 15:21:28 +02001902 float layerAlpha = getAlpha();
Galia Peychevae0acf382021-04-12 21:22:34 +02001903 for (auto& region : regionsCopy) {
1904 region.alpha = region.alpha * layerAlpha;
1905 }
1906 return regionsCopy;
Lucas Dupinc3800b82020-10-02 16:24:48 -07001907}
1908
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001909Layer::RoundedCornerState Layer::getRoundedCornerState() const {
1910 const auto& p = mDrawingParent.promote();
1911 if (p != nullptr) {
Peiyong Lin27016a92019-03-29 17:36:08 +00001912 RoundedCornerState parentState = p->getRoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001913 if (parentState.radius > 0) {
1914 ui::Transform t = getActiveTransform(getDrawingState());
1915 t = t.inverse();
1916 parentState.cropRect = t.transform(parentState.cropRect);
1917 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1918 // but a transform matrix can define horizontal and vertical scales.
1919 // Let's take the average between both of them and pass into the shader, practically we
1920 // never do this type of transformation on windows anyway.
Lucas Dupineab7ea02020-06-03 17:27:28 -07001921 auto scaleX = sqrtf(t[0][0] * t[0][0] + t[0][1] * t[0][1]);
1922 auto scaleY = sqrtf(t[1][0] * t[1][0] + t[1][1] * t[1][1]);
1923 parentState.radius *= (scaleX + scaleY) / 2.0f;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001924 return parentState;
1925 }
1926 }
1927 const float radius = getDrawingState().cornerRadius;
Chavi Weingartena5aedbd2021-04-09 13:37:33 +00001928 return radius > 0 && getCroppedBufferSize(getDrawingState()).isValid()
1929 ? RoundedCornerState(getCroppedBufferSize(getDrawingState()).toFloatRect(), radius)
Peiyong Linb9ff23e2019-04-08 11:04:59 -07001930 : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001931}
1932
Derek Sollenbergerc31985e2021-05-18 16:38:17 -04001933void Layer::prepareShadowClientComposition(LayerFE::LayerSettings& caster,
1934 const Rect& layerStackRect) {
Vishnu Nair08f6eae2019-11-26 14:01:39 -08001935 renderengine::ShadowSettings state = mFlinger->mDrawingState.globalShadowSettings;
1936
Derek Sollenbergerc31985e2021-05-18 16:38:17 -04001937 // Note: this preserves existing behavior of shadowing the entire layer and not cropping it if
1938 // transparent regions are present. This may not be necessary since shadows are only cast by
1939 // SurfaceFlinger's EffectLayers, which do not typically use transparent regions.
1940 state.boundaries = mBounds;
1941
Vishnu Nair08f6eae2019-11-26 14:01:39 -08001942 // Shift the spot light x-position to the middle of the display and then
1943 // offset it by casting layer's screen pos.
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001944 state.lightPos.x = (layerStackRect.width() / 2.f) - mScreenBounds.left;
Vishnu Nair08f6eae2019-11-26 14:01:39 -08001945 state.lightPos.y -= mScreenBounds.top;
1946
1947 state.length = mEffectiveShadowRadius;
Derek Sollenbergerc31985e2021-05-18 16:38:17 -04001948
1949 if (state.length > 0.f) {
1950 const float casterAlpha = caster.alpha;
1951 const bool casterIsOpaque =
1952 ((caster.source.buffer.buffer != nullptr) && caster.source.buffer.isOpaque);
1953
1954 // If the casting layer is translucent, we need to fill in the shadow underneath the layer.
1955 // Otherwise the generated shadow will only be shown around the casting layer.
1956 state.casterIsTranslucent = !casterIsOpaque || (casterAlpha < 1.0f);
1957 state.ambientColor *= casterAlpha;
1958 state.spotColor *= casterAlpha;
1959
1960 if (state.ambientColor.a > 0.f && state.spotColor.a > 0.f) {
1961 caster.shadow = state;
1962 }
1963 }
Vishnu Nair08f6eae2019-11-26 14:01:39 -08001964}
1965
Robert Carr1f0a16a2016-10-24 16:27:39 -07001966void Layer::commitChildList() {
1967 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1968 const auto& child = mCurrentChildren[i];
1969 child->commitChildList();
1970 }
1971 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001972 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001973}
1974
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001975static wp<Layer> extractLayerFromBinder(const wp<IBinder>& weakBinderHandle) {
1976 if (weakBinderHandle == nullptr) {
1977 return nullptr;
1978 }
1979 sp<IBinder> binderHandle = weakBinderHandle.promote();
1980 if (binderHandle == nullptr) {
1981 return nullptr;
1982 }
1983 sp<Layer::Handle> handle = static_cast<Layer::Handle*>(binderHandle.get());
1984 if (handle == nullptr) {
1985 return nullptr;
1986 }
1987 return handle->owner;
1988}
1989
Robert Carr720e5062018-07-30 17:45:14 -07001990void Layer::setInputInfo(const InputWindowInfo& info) {
Robert Carr6a160312021-05-17 12:08:20 -07001991 mDrawingState.inputInfo = info;
1992 mDrawingState.touchableRegionCrop = extractLayerFromBinder(info.touchableRegionCropHandle);
1993 mDrawingState.modified = true;
1994 mDrawingState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07001995 setTransactionFlags(eTransactionNeeded);
1996}
1997
Alec Mouri6b9e9912020-01-21 10:50:24 -08001998LayerProto* Layer::writeToProto(LayersProto& layersProto, uint32_t traceFlags,
chaviw4c34a092020-07-08 11:30:06 -07001999 const DisplayDevice* display) {
chaviw08f3cb22020-01-13 13:17:21 -08002000 LayerProto* layerProto = layersProto.add_layers();
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002001 writeToProtoDrawingState(layerProto, traceFlags, display);
chaviw08f3cb22020-01-13 13:17:21 -08002002 writeToProtoCommonState(layerProto, LayerVector::StateSet::Drawing, traceFlags);
2003
Vishnu Nair60db8c02020-04-02 11:55:16 -07002004 if (traceFlags & SurfaceTracing::TRACE_COMPOSITION) {
2005 // Only populate for the primary display.
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002006 if (display) {
2007 const Hwc2::IComposerClient::Composition compositionType = getCompositionType(*display);
Vishnu Nair60db8c02020-04-02 11:55:16 -07002008 layerProto->set_hwc_composition_type(static_cast<HwcCompositionType>(compositionType));
2009 }
Alec Mouri6b9e9912020-01-21 10:50:24 -08002010 }
2011
chaviw08f3cb22020-01-13 13:17:21 -08002012 for (const sp<Layer>& layer : mDrawingChildren) {
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002013 layer->writeToProto(layersProto, traceFlags, display);
chaviw08f3cb22020-01-13 13:17:21 -08002014 }
chaviw6d89e2d2020-01-14 14:42:01 -08002015
2016 return layerProto;
chaviw08f3cb22020-01-13 13:17:21 -08002017}
2018
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002019void Layer::writeToProtoDrawingState(LayerProto* layerInfo, uint32_t traceFlags,
chaviw4c34a092020-07-08 11:30:06 -07002020 const DisplayDevice* display) {
Vishnu Nair6b7c5c92020-09-29 17:27:05 -07002021 const ui::Transform transform = getTransform();
Vishnu Nair8406fd72019-07-30 11:29:31 -07002022
2023 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
Vishnu Nair8406fd72019-07-30 11:29:31 -07002024
chaviwd62d3062019-09-04 14:48:02 -07002025 auto buffer = getBuffer();
Vishnu Nair8406fd72019-07-30 11:29:31 -07002026 if (buffer != nullptr) {
2027 LayerProtoHelper::writeToProto(buffer,
2028 [&]() { return layerInfo->mutable_active_buffer(); });
chaviw4244e032019-09-04 11:27:49 -07002029 LayerProtoHelper::writeToProto(ui::Transform(getBufferTransform()),
Vishnu Nair8406fd72019-07-30 11:29:31 -07002030 layerInfo->mutable_buffer_transform());
2031 }
2032 layerInfo->set_invalidate(contentDirty);
2033 layerInfo->set_is_protected(isProtected());
chaviw4244e032019-09-04 11:27:49 -07002034 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(getDataSpace())));
Vishnu Nair8406fd72019-07-30 11:29:31 -07002035 layerInfo->set_queued_frames(getQueuedFrameCount());
2036 layerInfo->set_refresh_pending(isBufferLatched());
2037 layerInfo->set_curr_frame(mCurrentFrameNumber);
2038 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
2039
2040 layerInfo->set_corner_radius(getRoundedCornerState().radius);
Ana Krulece766cc82020-04-27 13:49:13 -07002041 layerInfo->set_background_blur_radius(getBackgroundBlurRadius());
Vishnu Nair8406fd72019-07-30 11:29:31 -07002042 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
2043 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
2044 [&]() { return layerInfo->mutable_position(); });
2045 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
Vishnu Nair60db8c02020-04-02 11:55:16 -07002046 if (traceFlags & SurfaceTracing::TRACE_COMPOSITION) {
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002047 LayerProtoHelper::writeToProto(getVisibleRegion(display),
Vishnu Nair60db8c02020-04-02 11:55:16 -07002048 [&]() { return layerInfo->mutable_visible_region(); });
2049 }
Vishnu Nair8406fd72019-07-30 11:29:31 -07002050 LayerProtoHelper::writeToProto(surfaceDamageRegion,
2051 [&]() { return layerInfo->mutable_damage_region(); });
chaviwddeae262020-01-06 10:31:23 -08002052
2053 if (hasColorTransform()) {
2054 LayerProtoHelper::writeToProto(getColorTransform(),
2055 layerInfo->mutable_color_transform());
2056 }
Vishnu Nair8406fd72019-07-30 11:29:31 -07002057 }
2058
Vishnu Nair60db8c02020-04-02 11:55:16 -07002059 LayerProtoHelper::writeToProto(mSourceBounds,
2060 [&]() { return layerInfo->mutable_source_bounds(); });
2061 LayerProtoHelper::writeToProto(mScreenBounds,
2062 [&]() { return layerInfo->mutable_screen_bounds(); });
2063 LayerProtoHelper::writeToProto(getRoundedCornerState().cropRect,
2064 [&]() { return layerInfo->mutable_corner_radius_crop(); });
2065 layerInfo->set_shadow_radius(mEffectiveShadowRadius);
Vishnu Nair8406fd72019-07-30 11:29:31 -07002066}
2067
2068void Layer::writeToProtoCommonState(LayerProto* layerInfo, LayerVector::StateSet stateSet,
chaviw4c34a092020-07-08 11:30:06 -07002069 uint32_t traceFlags) {
chaviw1d044282017-09-27 12:19:28 -07002070 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2071 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Robert Carr6a160312021-05-17 12:08:20 -07002072 const State& state = useDrawing ? mDrawingState : mDrawingState;
chaviw1d044282017-09-27 12:19:28 -07002073
chaviw766c9c52021-02-10 17:36:47 -08002074 ui::Transform requestedTransform = state.transform;
chaviw1d044282017-09-27 12:19:28 -07002075
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002076 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
2077 layerInfo->set_id(sequence);
2078 layerInfo->set_name(getName().c_str());
chaviw8a01fa42019-08-19 12:39:31 -07002079 layerInfo->set_type(getType());
chaviw1d044282017-09-27 12:19:28 -07002080
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002081 for (const auto& child : children) {
2082 layerInfo->add_children(child->sequence);
chaviw1d044282017-09-27 12:19:28 -07002083 }
chaviw1d044282017-09-27 12:19:28 -07002084
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002085 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
2086 sp<Layer> strongRelative = weakRelative.promote();
2087 if (strongRelative != nullptr) {
2088 layerInfo->add_relatives(strongRelative->sequence);
2089 }
chaviwadc40c22018-07-10 16:57:27 -07002090 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002091
2092 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
2093 [&]() { return layerInfo->mutable_transparent_region(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002094
2095 layerInfo->set_layer_stack(getLayerStack());
2096 layerInfo->set_z(state.z);
2097
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002098 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(),
2099 [&]() {
2100 return layerInfo->mutable_requested_position();
2101 });
2102
chaviw766c9c52021-02-10 17:36:47 -08002103 LayerProtoHelper::writeSizeToProto(state.width, state.height,
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002104 [&]() { return layerInfo->mutable_size(); });
2105
chaviw766c9c52021-02-10 17:36:47 -08002106 LayerProtoHelper::writeToProto(state.crop, [&]() { return layerInfo->mutable_crop(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002107
2108 layerInfo->set_is_opaque(isOpaque(state));
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002109
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002110
2111 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
2112 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
2113 LayerProtoHelper::writeToProto(state.color,
2114 [&]() { return layerInfo->mutable_requested_color(); });
2115 layerInfo->set_flags(state.flags);
2116
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002117 LayerProtoHelper::writeToProto(requestedTransform,
2118 layerInfo->mutable_requested_transform());
2119
2120 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
2121 if (parent != nullptr) {
2122 layerInfo->set_parent(parent->sequence);
2123 } else {
2124 layerInfo->set_parent(-1);
2125 }
2126
2127 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2128 if (zOrderRelativeOf != nullptr) {
2129 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
2130 } else {
2131 layerInfo->set_z_order_relative_of(-1);
2132 }
chaviw08f3cb22020-01-13 13:17:21 -08002133
2134 layerInfo->set_is_relative_of(state.isRelativeOf);
chaviw250bcbb2020-08-05 11:17:54 -07002135
2136 layerInfo->set_owner_uid(mOwnerUid);
chaviwadc40c22018-07-10 16:57:27 -07002137 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002138
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002139 if (traceFlags & SurfaceTracing::TRACE_INPUT) {
chaviw4c34a092020-07-08 11:30:06 -07002140 InputWindowInfo info;
2141 if (useDrawing) {
[1;3C2b9fc252021-02-04 16:16:50 -08002142 info = fillInputInfo({nullptr});
chaviw4c34a092020-07-08 11:30:06 -07002143 } else {
2144 info = state.inputInfo;
2145 }
2146
2147 LayerProtoHelper::writeToProto(info, state.touchableRegionCrop,
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002148 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002149 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002150
2151 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
2152 auto protoMap = layerInfo->mutable_metadata();
2153 for (const auto& entry : state.metadata.mMap) {
2154 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
2155 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002156 }
chaviw1d044282017-09-27 12:19:28 -07002157}
2158
Robert Carr2e102c92018-10-23 12:11:15 -07002159bool Layer::isRemovedFromCurrentState() const {
Robert Carr6a160312021-05-17 12:08:20 -07002160 return mRemovedFromDrawingState;
Robert Carr2e102c92018-10-23 12:11:15 -07002161}
2162
chaviw39d01472021-04-08 14:26:24 -05002163ui::Transform Layer::getInputTransform() const {
2164 return getTransform();
2165}
2166
2167Rect Layer::getInputBounds() const {
2168 return getCroppedBufferSize(getDrawingState());
2169}
2170
[1;3C2b9fc252021-02-04 16:16:50 -08002171void Layer::fillInputFrameInfo(InputWindowInfo& info, const ui::Transform& toPhysicalDisplay) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002172 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002173 // If this is a portal window, set the touchableRegion to the layerBounds.
2174 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
chaviw39d01472021-04-08 14:26:24 -05002175 ? getInputBounds()
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002176 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002177 if (!layerBounds.isValid()) {
chaviw39d01472021-04-08 14:26:24 -05002178 layerBounds = getInputBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002179 }
chaviw1ff3d1e2020-07-01 15:53:47 -07002180
chaviw7e72caf2020-12-02 16:50:43 -08002181 if (!layerBounds.isValid()) {
2182 // If the layer bounds is empty, set the frame to empty and clear the transform
2183 info.frameLeft = 0;
2184 info.frameTop = 0;
2185 info.frameRight = 0;
2186 info.frameBottom = 0;
2187 info.transform.reset();
2188 return;
2189 }
2190
chaviw39d01472021-04-08 14:26:24 -05002191 ui::Transform layerToDisplay = getInputTransform();
[1;3C2b9fc252021-02-04 16:16:50 -08002192 // Transform that takes window coordinates to unrotated display coordinates
2193 ui::Transform t = toPhysicalDisplay * layerToDisplay;
chaviw7e72caf2020-12-02 16:50:43 -08002194 int32_t xSurfaceInset = info.surfaceInset;
2195 int32_t ySurfaceInset = info.surfaceInset;
[1;3C2b9fc252021-02-04 16:16:50 -08002196 // Bring screenBounds into unrotated space
2197 Rect screenBounds = toPhysicalDisplay.transform(Rect{mScreenBounds});
chaviw7e72caf2020-12-02 16:50:43 -08002198
chaviw1ff3d1e2020-07-01 15:53:47 -07002199 const float xScale = t.getScaleX();
2200 const float yScale = t.getScaleY();
2201 if (xScale != 1.0f || yScale != 1.0f) {
chaviw1ff3d1e2020-07-01 15:53:47 -07002202 xSurfaceInset = std::round(xSurfaceInset * xScale);
2203 ySurfaceInset = std::round(ySurfaceInset * yScale);
2204 }
2205
Peiyong Lin74861ad2020-10-14 11:55:33 -07002206 // Transform the layer bounds from layer coordinate space to display coordinate space.
chaviw44a6d2b2020-09-08 17:14:16 -07002207 Rect transformedLayerBounds = t.transform(layerBounds);
Ady Abraham282f1d72019-07-24 18:05:56 -07002208
2209 // clamp inset to layer bounds
chaviw44a6d2b2020-09-08 17:14:16 -07002210 xSurfaceInset = (xSurfaceInset >= 0)
2211 ? std::min(xSurfaceInset, transformedLayerBounds.getWidth() / 2)
2212 : 0;
2213 ySurfaceInset = (ySurfaceInset >= 0)
2214 ? std::min(ySurfaceInset, transformedLayerBounds.getHeight() / 2)
2215 : 0;
Ady Abraham282f1d72019-07-24 18:05:56 -07002216
Robert Carra7242302020-09-01 18:26:29 -07002217 // inset while protecting from overflow TODO(b/161235021): What is going wrong
2218 // in the overflow scenario?
2219 {
2220 int32_t tmp;
chaviw44a6d2b2020-09-08 17:14:16 -07002221 if (!__builtin_add_overflow(transformedLayerBounds.left, xSurfaceInset, &tmp))
2222 transformedLayerBounds.left = tmp;
2223 if (!__builtin_sub_overflow(transformedLayerBounds.right, xSurfaceInset, &tmp))
2224 transformedLayerBounds.right = tmp;
2225 if (!__builtin_add_overflow(transformedLayerBounds.top, ySurfaceInset, &tmp))
2226 transformedLayerBounds.top = tmp;
2227 if (!__builtin_sub_overflow(transformedLayerBounds.bottom, ySurfaceInset, &tmp))
2228 transformedLayerBounds.bottom = tmp;
Robert Carra7242302020-09-01 18:26:29 -07002229 }
Vishnu Nair8033a492018-12-05 07:27:23 -08002230
chaviw44a6d2b2020-09-08 17:14:16 -07002231 // Compute the correct transform to send to input. This will allow it to transform the
2232 // input coordinates from display space into window space. Therefore, it needs to use the
2233 // final layer frame to create the inverse transform. Since surface insets are added later,
2234 // along with the overflow, the best way to ensure we get the correct transform is to use
2235 // the final frame calculated.
2236 // 1. Take the original transform set on the window and get the inverse transform. This is
2237 // used to get the final bounds in display space (ignorning the transform). Apply the
Peiyong Lin74861ad2020-10-14 11:55:33 -07002238 // inverse transform on the layerBounds to get the untransformed frame (in layer space)
chaviw44a6d2b2020-09-08 17:14:16 -07002239 // 2. Take the top and left of the untransformed frame to get the real position on screen.
2240 // Apply the layer transform on top/left so it includes any scale or rotation. These will
2241 // be the new translation values for the transform.
2242 // 3. Update the translation of the original transform to the new translation values.
2243 // 4. Send the inverse transform to input so the coordinates can be transformed back into
2244 // window space.
2245 ui::Transform inverseTransform = t.inverse();
2246 Rect nonTransformedBounds = inverseTransform.transform(transformedLayerBounds);
2247 vec2 translation = t.transform(nonTransformedBounds.left, nonTransformedBounds.top);
chaviw1ff3d1e2020-07-01 15:53:47 -07002248 ui::Transform inputTransform(t);
chaviw44a6d2b2020-09-08 17:14:16 -07002249 inputTransform.set(translation.x, translation.y);
chaviw1ff3d1e2020-07-01 15:53:47 -07002250 info.transform = inputTransform.inverse();
2251
chaviw39cfa2e2020-11-04 14:19:02 -08002252 // We need to send the layer bounds cropped to the screenbounds since the layer can be cropped.
2253 // The frame should be the area the user sees on screen since it's used for occlusion
2254 // detection.
chaviw39cfa2e2020-11-04 14:19:02 -08002255 transformedLayerBounds.intersect(screenBounds, &transformedLayerBounds);
2256 info.frameLeft = transformedLayerBounds.left;
2257 info.frameTop = transformedLayerBounds.top;
2258 info.frameRight = transformedLayerBounds.right;
2259 info.frameBottom = transformedLayerBounds.bottom;
2260
Vishnu Nair8033a492018-12-05 07:27:23 -08002261 // Position the touchable region relative to frame screen location and restrict it to frame
2262 // bounds.
chaviw44a6d2b2020-09-08 17:14:16 -07002263 info.touchableRegion = inputTransform.transform(info.touchableRegion);
chaviw7e72caf2020-12-02 16:50:43 -08002264}
2265
Bernardo Rufinoa9d0a532021-06-11 15:59:12 +01002266void Layer::fillTouchOcclusionMode(InputWindowInfo& info) {
2267 sp<Layer> p = this;
2268 while (p != nullptr && !p->hasInputInfo()) {
2269 p = p->mDrawingParent.promote();
2270 }
2271 if (p != nullptr) {
2272 info.touchOcclusionMode = p->mDrawingState.inputInfo.touchOcclusionMode;
2273 }
2274}
2275
[1;3C2b9fc252021-02-04 16:16:50 -08002276InputWindowInfo Layer::fillInputInfo(const sp<DisplayDevice>& display) {
chaviw7e72caf2020-12-02 16:50:43 -08002277 if (!hasInputInfo()) {
2278 mDrawingState.inputInfo.name = getName();
2279 mDrawingState.inputInfo.ownerUid = mOwnerUid;
2280 mDrawingState.inputInfo.ownerPid = mOwnerPid;
2281 mDrawingState.inputInfo.inputFeatures = InputWindowInfo::Feature::NO_INPUT_CHANNEL;
2282 mDrawingState.inputInfo.flags = InputWindowInfo::Flag::NOT_TOUCH_MODAL;
2283 mDrawingState.inputInfo.displayId = getLayerStack();
2284 }
2285
2286 InputWindowInfo info = mDrawingState.inputInfo;
2287 info.id = sequence;
2288
2289 if (info.displayId == ADISPLAY_ID_NONE) {
2290 info.displayId = getLayerStack();
2291 }
2292
[1;3C2b9fc252021-02-04 16:16:50 -08002293 // Transform that goes from "logical(rotated)" display to physical/unrotated display.
2294 // This is for when inputflinger operates in physical display-space.
2295 ui::Transform toPhysicalDisplay;
2296 if (display) {
2297 toPhysicalDisplay = display->getTransform();
Evan Rosky84f07f02021-04-16 10:42:42 -07002298 info.displayWidth = display->getWidth();
2299 info.displayHeight = display->getHeight();
[1;3C2b9fc252021-02-04 16:16:50 -08002300 }
2301 fillInputFrameInfo(info, toPhysicalDisplay);
chaviw7e72caf2020-12-02 16:50:43 -08002302
Robert Carr5dc426e2020-06-10 14:29:14 -07002303 // For compatibility reasons we let layers which can receive input
2304 // receive input before they have actually submitted a buffer. Because
2305 // of this we use canReceiveInput instead of isVisible to check the
2306 // policy-visibility, ignoring the buffer state. However for layers with
2307 // hasInputInfo()==false we can use the real visibility state.
2308 // We are just using these layers for occlusion detection in
2309 // InputDispatcher, and obviously if they aren't visible they can't occlude
2310 // anything.
2311 info.visible = hasInputInfo() ? canReceiveInput() : isVisible();
Bernardo Rufinoea97d182020-08-19 14:43:14 +01002312 info.alpha = getAlpha();
Bernardo Rufinoa9d0a532021-06-11 15:59:12 +01002313 fillTouchOcclusionMode(info);
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002314
2315 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2316 if (info.replaceTouchableRegionWithCrop) {
2317 if (cropLayer == nullptr) {
[1;3C2b9fc252021-02-04 16:16:50 -08002318 info.touchableRegion = Region(toPhysicalDisplay.transform(Rect{mScreenBounds}));
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002319 } else {
[1;3C2b9fc252021-02-04 16:16:50 -08002320 info.touchableRegion =
2321 Region(toPhysicalDisplay.transform(Rect{cropLayer->mScreenBounds}));
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002322 }
2323 } else if (cropLayer != nullptr) {
[1;3C2b9fc252021-02-04 16:16:50 -08002324 info.touchableRegion = info.touchableRegion.intersect(
2325 toPhysicalDisplay.transform(Rect{cropLayer->mScreenBounds}));
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002326 }
2327
chaviwaf87b3e2019-10-01 16:59:28 -07002328 // If the layer is a clone, we need to crop the input region to cloned root to prevent
2329 // touches from going outside the cloned area.
2330 if (isClone()) {
2331 sp<Layer> clonedRoot = getClonedRoot();
2332 if (clonedRoot != nullptr) {
[1;3C2b9fc252021-02-04 16:16:50 -08002333 Rect rect = toPhysicalDisplay.transform(Rect{clonedRoot->mScreenBounds});
chaviwaf87b3e2019-10-01 16:59:28 -07002334 info.touchableRegion = info.touchableRegion.intersect(rect);
2335 }
2336 }
2337
Robert Carr720e5062018-07-30 17:45:14 -07002338 return info;
2339}
2340
chaviwaf87b3e2019-10-01 16:59:28 -07002341sp<Layer> Layer::getClonedRoot() {
2342 if (mClonedChild != nullptr) {
2343 return this;
2344 }
2345 if (mDrawingParent == nullptr || mDrawingParent.promote() == nullptr) {
2346 return nullptr;
2347 }
2348 return mDrawingParent.promote()->getClonedRoot();
2349}
2350
Robert Carredd13602020-04-13 17:24:34 -07002351bool Layer::hasInputInfo() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002352 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002353}
2354
chaviw3e727cd2019-01-31 13:41:05 -08002355bool Layer::canReceiveInput() const {
Vishnu Nair82353e92019-09-12 12:38:47 -07002356 return !isHiddenByPolicy();
chaviw3e727cd2019-01-31 13:41:05 -08002357}
2358
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002359compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002360 const DisplayDevice* display) const {
2361 if (!display) return nullptr;
Lloyd Piquede196652020-01-22 17:29:58 -08002362 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionEngineLayerFE());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002363}
2364
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002365Region Layer::getVisibleRegion(const DisplayDevice* display) const {
2366 const auto outputLayer = findOutputLayerForDisplay(display);
2367 return outputLayer ? outputLayer->getState().visibleRegion : Region();
Lloyd Piquea2468662019-03-07 21:31:06 -08002368}
2369
chaviwb4c6e582019-08-16 14:35:07 -07002370void Layer::setInitialValuesForClone(const sp<Layer>& clonedFrom) {
2371 // copy drawing state from cloned layer
2372 mDrawingState = clonedFrom->mDrawingState;
2373 mClonedFrom = clonedFrom;
chaviwb4c6e582019-08-16 14:35:07 -07002374}
chaviw74b03172019-08-19 11:09:03 -07002375
2376void Layer::updateMirrorInfo() {
2377 if (mClonedChild == nullptr || !mClonedChild->isClonedFromAlive()) {
2378 // If mClonedChild is null, there is nothing to mirror. If isClonedFromAlive returns false,
2379 // it means that there is a clone, but the layer it was cloned from has been destroyed. In
2380 // that case, we want to delete the reference to the clone since we want it to get
2381 // destroyed. The root, this layer, will still be around since the client can continue
2382 // to hold a reference, but no cloned layers will be displayed.
2383 mClonedChild = nullptr;
2384 return;
2385 }
2386
2387 std::map<sp<Layer>, sp<Layer>> clonedLayersMap;
2388 // If the real layer exists and is in current state, add the clone as a child of the root.
2389 // There's no need to remove from drawingState when the layer is offscreen since currentState is
2390 // copied to drawingState for the root layer. So the clonedChild is always removed from
2391 // drawingState and then needs to be added back each traversal.
2392 if (!mClonedChild->getClonedFrom()->isRemovedFromCurrentState()) {
2393 addChildToDrawing(mClonedChild);
2394 }
2395
2396 mClonedChild->updateClonedDrawingState(clonedLayersMap);
2397 mClonedChild->updateClonedChildren(this, clonedLayersMap);
2398 mClonedChild->updateClonedRelatives(clonedLayersMap);
2399}
2400
2401void Layer::updateClonedDrawingState(std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2402 // If the layer the clone was cloned from is alive, copy the content of the drawingState
2403 // to the clone. If the real layer is no longer alive, continue traversing the children
2404 // since we may be able to pull out other children that are still alive.
2405 if (isClonedFromAlive()) {
2406 sp<Layer> clonedFrom = getClonedFrom();
2407 mDrawingState = clonedFrom->mDrawingState;
chaviw74b03172019-08-19 11:09:03 -07002408 clonedLayersMap.emplace(clonedFrom, this);
2409 }
2410
2411 // The clone layer may have children in drawingState since they may have been created and
2412 // added from a previous request to updateMirorInfo. This is to ensure we don't recreate clones
2413 // that already exist, since we can just re-use them.
2414 // The drawingChildren will not get overwritten by the currentChildren since the clones are
2415 // not updated in the regular traversal. They are skipped since the root will lose the
2416 // reference to them when it copies its currentChildren to drawing.
2417 for (sp<Layer>& child : mDrawingChildren) {
2418 child->updateClonedDrawingState(clonedLayersMap);
2419 }
2420}
2421
2422void Layer::updateClonedChildren(const sp<Layer>& mirrorRoot,
2423 std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2424 mDrawingChildren.clear();
2425
2426 if (!isClonedFromAlive()) {
2427 return;
2428 }
2429
2430 sp<Layer> clonedFrom = getClonedFrom();
2431 for (sp<Layer>& child : clonedFrom->mDrawingChildren) {
2432 if (child == mirrorRoot) {
2433 // This is to avoid cyclical mirroring.
2434 continue;
2435 }
2436 sp<Layer> clonedChild = clonedLayersMap[child];
2437 if (clonedChild == nullptr) {
2438 clonedChild = child->createClone();
2439 clonedLayersMap[child] = clonedChild;
2440 }
2441 addChildToDrawing(clonedChild);
2442 clonedChild->updateClonedChildren(mirrorRoot, clonedLayersMap);
2443 }
2444}
2445
chaviwaf87b3e2019-10-01 16:59:28 -07002446void Layer::updateClonedInputInfo(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2447 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2448 if (cropLayer != nullptr) {
2449 if (clonedLayersMap.count(cropLayer) == 0) {
2450 // Real layer had a crop layer but it's not in the cloned hierarchy. Just set to
2451 // self as crop layer to avoid going outside bounds.
2452 mDrawingState.touchableRegionCrop = this;
2453 } else {
2454 const sp<Layer>& clonedCropLayer = clonedLayersMap.at(cropLayer);
2455 mDrawingState.touchableRegionCrop = clonedCropLayer;
2456 }
2457 }
2458 // Cloned layers shouldn't handle watch outside since their z order is not determined by
2459 // WM or the client.
Michael Wright44753b12020-07-08 13:48:11 +01002460 mDrawingState.inputInfo.flags &= ~InputWindowInfo::Flag::WATCH_OUTSIDE_TOUCH;
chaviwaf87b3e2019-10-01 16:59:28 -07002461}
2462
2463void Layer::updateClonedRelatives(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
chaviw74b03172019-08-19 11:09:03 -07002464 mDrawingState.zOrderRelativeOf = nullptr;
2465 mDrawingState.zOrderRelatives.clear();
2466
2467 if (!isClonedFromAlive()) {
2468 return;
2469 }
2470
chaviwaf87b3e2019-10-01 16:59:28 -07002471 const sp<Layer>& clonedFrom = getClonedFrom();
chaviw74b03172019-08-19 11:09:03 -07002472 for (wp<Layer>& relativeWeak : clonedFrom->mDrawingState.zOrderRelatives) {
chaviwaf87b3e2019-10-01 16:59:28 -07002473 const sp<Layer>& relative = relativeWeak.promote();
2474 if (clonedLayersMap.count(relative) > 0) {
2475 auto& clonedRelative = clonedLayersMap.at(relative);
chaviw74b03172019-08-19 11:09:03 -07002476 mDrawingState.zOrderRelatives.add(clonedRelative);
2477 }
2478 }
2479
2480 // Check if the relativeLayer for the real layer is part of the cloned hierarchy.
2481 // It's possible that the layer it's relative to is outside the requested cloned hierarchy.
2482 // In that case, we treat the layer as if the relativeOf has been removed. This way, it will
2483 // still traverse the children, but the layer with the missing relativeOf will not be shown
2484 // on screen.
chaviwaf87b3e2019-10-01 16:59:28 -07002485 const sp<Layer>& relativeOf = clonedFrom->mDrawingState.zOrderRelativeOf.promote();
2486 if (clonedLayersMap.count(relativeOf) > 0) {
2487 const sp<Layer>& clonedRelativeOf = clonedLayersMap.at(relativeOf);
chaviw74b03172019-08-19 11:09:03 -07002488 mDrawingState.zOrderRelativeOf = clonedRelativeOf;
2489 }
2490
chaviwaf87b3e2019-10-01 16:59:28 -07002491 updateClonedInputInfo(clonedLayersMap);
2492
chaviw74b03172019-08-19 11:09:03 -07002493 for (sp<Layer>& child : mDrawingChildren) {
2494 child->updateClonedRelatives(clonedLayersMap);
2495 }
2496}
2497
2498void Layer::addChildToDrawing(const sp<Layer>& layer) {
2499 mDrawingChildren.add(layer);
2500 layer->mDrawingParent = this;
2501}
2502
Steven Thomas62a4cf82020-01-31 12:04:03 -08002503Layer::FrameRateCompatibility Layer::FrameRate::convertCompatibility(int8_t compatibility) {
2504 switch (compatibility) {
2505 case ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT:
2506 return FrameRateCompatibility::Default;
2507 case ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE:
2508 return FrameRateCompatibility::ExactOrMultiple;
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08002509 case ANATIVEWINDOW_FRAME_RATE_EXACT:
2510 return FrameRateCompatibility::Exact;
Steven Thomas62a4cf82020-01-31 12:04:03 -08002511 default:
2512 LOG_ALWAYS_FATAL("Invalid frame rate compatibility value %d", compatibility);
2513 return FrameRateCompatibility::Default;
2514 }
2515}
2516
Marin Shalamanovc5986772021-03-16 16:09:49 +01002517scheduler::Seamlessness Layer::FrameRate::convertChangeFrameRateStrategy(int8_t strategy) {
2518 switch (strategy) {
2519 case ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS:
2520 return Seamlessness::OnlySeamless;
2521 case ANATIVEWINDOW_CHANGE_FRAME_RATE_ALWAYS:
2522 return Seamlessness::SeamedAndSeamless;
2523 default:
2524 LOG_ALWAYS_FATAL("Invalid change frame sate strategy value %d", strategy);
2525 return Seamlessness::Default;
2526 }
2527}
2528
chaviwc5676c62020-09-18 15:01:04 -07002529bool Layer::getPrimaryDisplayOnly() const {
2530 const State& s(mDrawingState);
2531 if (s.flags & layer_state_t::eLayerSkipScreenshot) {
2532 return true;
2533 }
2534
2535 sp<Layer> parent = mDrawingParent.promote();
2536 return parent == nullptr ? false : parent->getPrimaryDisplayOnly();
2537}
2538
Mathias Agopian13127d82013-03-05 17:47:11 -08002539// ---------------------------------------------------------------------------
2540
Marin Shalamanov46084422020-10-13 12:33:42 +02002541std::ostream& operator<<(std::ostream& stream, const Layer::FrameRate& rate) {
2542 return stream << "{rate=" << rate.rate
2543 << " type=" << Layer::frameRateCompatibilityString(rate.type)
Marin Shalamanov53fc11d2020-11-20 14:00:13 +01002544 << " seamlessness=" << toString(rate.seamlessness) << "}";
Marin Shalamanov46084422020-10-13 12:33:42 +02002545}
2546
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002547}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002548
2549#if defined(__gl_h_)
2550#error "don't include gl/gl.h in this file"
2551#endif
2552
2553#if defined(__gl2_h_)
2554#error "don't include gl2/gl2.h in this file"
2555#endif
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -08002556
2557// TODO(b/129481165): remove the #pragma below and fix conversion issues
2558#pragma clang diagnostic pop // ignored "-Wconversion"