blob: d27b51b3451cb6093aa3c328b9ae05d18ef9d85c [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"
Robert Carr3e2a2992021-06-11 13:42:55 -070073#include "TunnelModeEnabledReporter.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;
chaviw3277faf2021-05-19 16:45:23 -050085using gui::WindowInfo;
Yiwei Zhang5434a782018-12-05 18:06:32 -080086
Lloyd Piquef1c675b2018-09-12 20:45:39 -070087std::atomic<int32_t> Layer::sSequence{1};
Mathias Agopian13127d82013-03-05 17:47:11 -080088
Lloyd Pique42ab75e2018-09-12 20:46:03 -070089Layer::Layer(const LayerCreationArgs& args)
Ady Abraham8f1ee7f2019-04-05 10:32:50 -070090 : mFlinger(args.flinger),
91 mName(args.name),
92 mClientRef(args.client),
chaviw3277faf2021-05-19 16:45:23 -050093 mWindowType(
94 static_cast<WindowInfo::Type>(args.metadata.getInt32(METADATA_WINDOW_TYPE, 0))) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -070095 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -070096 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
97 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
98 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
chaviwc5676c62020-09-18 15:01:04 -070099 if (args.flags & ISurfaceComposerClient::eSkipScreenshot)
100 layerFlags |= layer_state_t::eLayerSkipScreenshot;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700101
Robert Carr6a160312021-05-17 12:08:20 -0700102 mDrawingState.active_legacy.w = args.w;
103 mDrawingState.active_legacy.h = args.h;
104 mDrawingState.flags = layerFlags;
105 mDrawingState.active_legacy.transform.set(0, 0);
106 mDrawingState.crop.makeInvalid();
107 mDrawingState.requestedCrop = mDrawingState.crop;
108 mDrawingState.z = 0;
109 mDrawingState.color.a = 1.0f;
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700110 mDrawingState.layerStack = ui::DEFAULT_LAYER_STACK;
Robert Carr6a160312021-05-17 12:08:20 -0700111 mDrawingState.sequence = 0;
112 mDrawingState.requested_legacy = mDrawingState.active_legacy;
113 mDrawingState.width = UINT32_MAX;
114 mDrawingState.height = UINT32_MAX;
115 mDrawingState.transform.set(0, 0);
116 mDrawingState.frameNumber = 0;
117 mDrawingState.bufferTransform = 0;
118 mDrawingState.transformToDisplayInverse = false;
119 mDrawingState.crop.makeInvalid();
120 mDrawingState.acquireFence = sp<Fence>::make(-1);
121 mDrawingState.acquireFenceTime = std::make_shared<FenceTime>(mDrawingState.acquireFence);
122 mDrawingState.dataspace = ui::Dataspace::UNKNOWN;
123 mDrawingState.hdrMetadata.validTypes = 0;
124 mDrawingState.surfaceDamageRegion = Region::INVALID_REGION;
125 mDrawingState.cornerRadius = 0.0f;
126 mDrawingState.backgroundBlurRadius = 0;
127 mDrawingState.api = -1;
128 mDrawingState.hasColorTransform = false;
129 mDrawingState.colorSpaceAgnostic = false;
130 mDrawingState.frameRateSelectionPriority = PRIORITY_UNSET;
131 mDrawingState.metadata = args.metadata;
132 mDrawingState.shadowRadius = 0.f;
133 mDrawingState.fixedTransformHint = ui::Transform::ROT_INVALID;
134 mDrawingState.frameTimelineInfo = {};
135 mDrawingState.postTime = -1;
136 mDrawingState.destinationFrame.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700137
Vishnu Nairc43a23c2020-05-29 14:32:27 -0700138 if (args.flags & ISurfaceComposerClient::eNoColorFill) {
139 // Set an invalid color so there is no color fill.
Robert Carr6a160312021-05-17 12:08:20 -0700140 mDrawingState.color.r = -1.0_hf;
141 mDrawingState.color.g = -1.0_hf;
142 mDrawingState.color.b = -1.0_hf;
Vishnu Nairc43a23c2020-05-29 14:32:27 -0700143 }
144
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800145 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700146 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800147 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200148 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700149
Vishnu Nair0f085c62019-08-30 08:49:12 -0700150 mCallingPid = args.callingPid;
151 mCallingUid = args.callingUid;
chaviw250bcbb2020-08-05 11:17:54 -0700152
153 if (mCallingUid == AID_GRAPHICS || mCallingUid == AID_SYSTEM) {
154 // If the system didn't send an ownerUid, use the callingUid for the ownerUid.
155 mOwnerUid = args.metadata.getInt32(METADATA_OWNER_UID, mCallingUid);
Adithya Srinivasan9febda82020-10-19 10:49:41 -0700156 mOwnerPid = args.metadata.getInt32(METADATA_OWNER_PID, mCallingPid);
chaviw250bcbb2020-08-05 11:17:54 -0700157 } else {
158 // A create layer request from a non system request cannot specify the owner uid
159 mOwnerUid = mCallingUid;
Adithya Srinivasan9febda82020-10-19 10:49:41 -0700160 mOwnerPid = mCallingPid;
chaviw250bcbb2020-08-05 11:17:54 -0700161 }
Dominik Laskowski75848362019-11-11 17:57:20 -0800162}
163
164void Layer::onFirstRef() {
165 mFlinger->onLayerFirstRef(this);
Dan Stoza436ccf32018-06-21 12:10:12 -0700166}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700167
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700168Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800169 sp<Client> c(mClientRef.promote());
170 if (c != 0) {
171 c->detachLayer(this);
172 }
173
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000174 mFrameTracker.logAndResetStats(mName);
chaviw74d90ad2019-04-26 14:45:26 -0700175 mFlinger->onLayerDestroyed(this);
Robert Carr3e2a2992021-06-11 13:42:55 -0700176
177 if (mDrawingState.sidebandStream != nullptr) {
178 mFlinger->mTunnelModeEnabledReporter->decrementTunnelModeCount();
179 }
Robert Carr6a0382d2021-07-01 15:57:17 -0700180 if (mHadClonedChild) {
181 mFlinger->mNumClones--;
182 }
Mathias Agopian96f08192010-06-02 23:28:45 -0700183}
184
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700185LayerCreationArgs::LayerCreationArgs(SurfaceFlinger* flinger, sp<Client> client, std::string name,
186 uint32_t w, uint32_t h, uint32_t flags, LayerMetadata metadata)
Vishnu Nair0f085c62019-08-30 08:49:12 -0700187 : flinger(flinger),
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700188 client(std::move(client)),
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700189 name(std::move(name)),
Vishnu Nair0f085c62019-08-30 08:49:12 -0700190 w(w),
191 h(h),
192 flags(flags),
193 metadata(std::move(metadata)) {
194 IPCThreadState* ipc = IPCThreadState::self();
195 callingPid = ipc->getCallingPid();
196 callingUid = ipc->getCallingUid();
197}
198
Mathias Agopian13127d82013-03-05 17:47:11 -0800199// ---------------------------------------------------------------------------
200// callbacks
201// ---------------------------------------------------------------------------
202
David Sodmaneb085e02017-10-05 18:49:04 -0700203/*
204 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
205 * Layer. So, the implementation is done in BufferLayer. When called on a
Vishnu Nairfa247b12020-02-11 08:58:26 -0800206 * EffectLayer object, it's essentially a NOP.
David Sodmaneb085e02017-10-05 18:49:04 -0700207 */
David Sodmaneb085e02017-10-05 18:49:04 -0700208void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800209
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700210void Layer::removeRelativeZ(const std::vector<Layer*>& layersInTree) {
Robert Carr6a160312021-05-17 12:08:20 -0700211 if (mDrawingState.zOrderRelativeOf == nullptr) {
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700212 return;
213 }
Robert Carr2e102c92018-10-23 12:11:15 -0700214
Robert Carr6a160312021-05-17 12:08:20 -0700215 sp<Layer> strongRelative = mDrawingState.zOrderRelativeOf.promote();
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700216 if (strongRelative == nullptr) {
217 setZOrderRelativeOf(nullptr);
218 return;
219 }
220
221 if (!std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
222 strongRelative->removeZOrderRelative(this);
223 mFlinger->setTransactionFlags(eTraversalNeeded);
chaviw606e5cf2019-03-01 10:12:10 -0800224 setZOrderRelativeOf(nullptr);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700225 }
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700226}
227
228void Layer::removeFromCurrentState() {
Robert Carr6a160312021-05-17 12:08:20 -0700229 if (!mRemovedFromDrawingState) {
230 mRemovedFromDrawingState = true;
Ady Abrahambe09aad2021-05-03 18:59:38 -0700231 mFlinger->mScheduler->deregisterLayer(this);
232 }
Rob Carr4bba3702018-10-08 21:53:30 +0000233
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800234 mFlinger->markLayerPendingRemovalLocked(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700235}
Chia-I Wu38512252017-05-17 14:36:16 -0700236
chaviw68d4dab2020-06-08 15:07:32 -0700237sp<Layer> Layer::getRootLayer() {
238 sp<Layer> parent = getParent();
239 if (parent == nullptr) {
240 return this;
241 }
242 return parent->getRootLayer();
243}
244
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700245void Layer::onRemovedFromCurrentState() {
chaviw68d4dab2020-06-08 15:07:32 -0700246 // Use the root layer since we want to maintain the hierarchy for the entire subtree.
247 auto layersInTree = getRootLayer()->getLayersInTree(LayerVector::StateSet::Current);
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700248 std::sort(layersInTree.begin(), layersInTree.end());
chaviw68d4dab2020-06-08 15:07:32 -0700249
250 traverse(LayerVector::StateSet::Current, [&](Layer* layer) {
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700251 layer->removeFromCurrentState();
252 layer->removeRelativeZ(layersInTree);
chaviw68d4dab2020-06-08 15:07:32 -0700253 });
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700254}
255
chaviw61626f22018-11-15 16:26:27 -0800256void Layer::addToCurrentState() {
Robert Carr6a160312021-05-17 12:08:20 -0700257 if (mRemovedFromDrawingState) {
258 mRemovedFromDrawingState = false;
Ady Abrahambe09aad2021-05-03 18:59:38 -0700259 mFlinger->mScheduler->registerLayer(this);
Robert Carr867be372021-06-29 17:36:02 -0700260 mFlinger->removeFromOffscreenLayers(this);
Ady Abrahambe09aad2021-05-03 18:59:38 -0700261 }
chaviw61626f22018-11-15 16:26:27 -0800262
263 for (const auto& child : mCurrentChildren) {
264 child->addToCurrentState();
265 }
266}
267
Mathias Agopian13127d82013-03-05 17:47:11 -0800268// ---------------------------------------------------------------------------
269// set-up
270// ---------------------------------------------------------------------------
271
chaviw13fdc492017-06-27 12:40:18 -0700272bool Layer::getPremultipledAlpha() const {
273 return mPremultipliedAlpha;
274}
275
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700276sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800277 Mutex::Autolock _l(mLock);
Robert Carrc0df3122019-04-11 13:18:21 -0700278 if (mGetHandleCalled) {
279 ALOGE("Get handle called twice" );
280 return nullptr;
281 }
282 mGetHandleCalled = true;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700283 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800284}
285
286// ---------------------------------------------------------------------------
287// h/w composer set-up
288// ---------------------------------------------------------------------------
289
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700290static Rect reduce(const Rect& win, const Region& exclude) {
291 if (CC_LIKELY(exclude.isEmpty())) {
292 return win;
293 }
294 if (exclude.isRect()) {
295 return win.reduce(exclude.getBounds());
296 }
297 return Region(win).subtract(exclude).getBounds();
298}
299
Dan Stoza80d61162017-12-20 15:57:52 -0800300static FloatRect reduce(const FloatRect& win, const Region& exclude) {
301 if (CC_LIKELY(exclude.isEmpty())) {
302 return win;
303 }
304 // Convert through Rect (by rounding) for lack of FloatRegion
305 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
306}
307
Vishnu Nair4351ad52019-02-11 14:13:02 -0800308Rect Layer::getScreenBounds(bool reduceTransparentRegion) const {
Vishnu Nairf0c28512019-02-08 12:40:28 -0800309 if (!reduceTransparentRegion) {
310 return Rect{mScreenBounds};
311 }
312
313 FloatRect bounds = getBounds();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800314 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800315 // Transform to screen space.
316 bounds = t.transform(bounds);
317 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700318}
319
Vishnu Nair4351ad52019-02-11 14:13:02 -0800320FloatRect Layer::getBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000321 const State& s(getDrawingState());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800322 return getBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700323}
324
Vishnu Nairf0c28512019-02-08 12:40:28 -0800325FloatRect Layer::getBounds(const Region& activeTransparentRegion) const {
326 // Subtract the transparent region and snap to the bounds.
327 return reduce(mBounds, activeTransparentRegion);
328}
329
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700330void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform,
331 float parentShadowRadius) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800332 const State& s(getDrawingState());
333
334 // Calculate effective layer transform
335 mEffectiveTransform = parentTransform * getActiveTransform(s);
336
337 // Transform parent bounds to layer space
338 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
339
Vishnu Nairc652ff82019-03-15 12:48:54 -0700340 // Calculate source bounds
Vishnu Nair4351ad52019-02-11 14:13:02 -0800341 mSourceBounds = computeSourceBounds(parentBounds);
342
343 // Calculate bounds by croping diplay frame with layer crop and parent bounds
344 FloatRect bounds = mSourceBounds;
345 const Rect layerCrop = getCrop(s);
346 if (!layerCrop.isEmpty()) {
347 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
348 }
349 bounds = bounds.intersect(parentBounds);
350
351 mBounds = bounds;
352 mScreenBounds = mEffectiveTransform.transform(mBounds);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700353
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700354 // Use the layer's own shadow radius if set. Otherwise get the radius from
355 // parent.
356 if (s.shadowRadius > 0.f) {
357 mEffectiveShadowRadius = s.shadowRadius;
358 } else {
359 mEffectiveShadowRadius = parentShadowRadius;
360 }
361
362 // Shadow radius is passed down to only one layer so if the layer can draw shadows,
363 // don't pass it to its children.
364 const float childShadowRadius = canDrawShadows() ? 0.f : mEffectiveShadowRadius;
365
Vishnu Nair4351ad52019-02-11 14:13:02 -0800366 for (const sp<Layer>& child : mDrawingChildren) {
Vishnu Nair6bdec7d2021-05-10 15:01:13 -0700367 child->computeBounds(mBounds, mEffectiveTransform, childShadowRadius);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800368 }
369}
370
Vishnu Nair60356342018-11-13 13:00:45 -0800371Rect Layer::getCroppedBufferSize(const State& s) const {
372 Rect size = getBufferSize(s);
373 Rect crop = getCrop(s);
374 if (!crop.isEmpty() && size.isValid()) {
375 size.intersect(crop, &size);
376 } else if (!crop.isEmpty()) {
377 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700378 }
Vishnu Nair60356342018-11-13 13:00:45 -0800379 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800380}
381
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700382void Layer::setupRoundedCornersCropCoordinates(Rect win,
383 const FloatRect& roundedCornersCrop) const {
384 // Translate win by the rounded corners rect coordinates, to have all values in
385 // layer coordinate space.
386 win.left -= roundedCornersCrop.left;
387 win.right -= roundedCornersCrop.left;
388 win.top -= roundedCornersCrop.top;
389 win.bottom -= roundedCornersCrop.top;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700390}
391
Lloyd Piquede196652020-01-22 17:29:58 -0800392void Layer::prepareBasicGeometryCompositionState() {
Lloyd Piquec6687342019-03-07 21:34:57 -0800393 const auto& drawingState{getDrawingState()};
Lloyd Piquec6687342019-03-07 21:34:57 -0800394 const auto alpha = static_cast<float>(getAlpha());
395 const bool opaque = isOpaque(drawingState);
396 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
397
398 auto blendMode = Hwc2::IComposerClient::BlendMode::NONE;
399 if (!opaque || alpha != 1.0f) {
400 blendMode = mPremultipliedAlpha ? Hwc2::IComposerClient::BlendMode::PREMULTIPLIED
401 : Hwc2::IComposerClient::BlendMode::COVERAGE;
402 }
403
Lloyd Piquede196652020-01-22 17:29:58 -0800404 auto* compositionState = editCompositionState();
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700405 compositionState->outputFilter = getOutputFilter();
Lloyd Piquede196652020-01-22 17:29:58 -0800406 compositionState->isVisible = isVisible();
407 compositionState->isOpaque = opaque && !usesRoundedCorners && alpha == 1.f;
408 compositionState->shadowRadius = mEffectiveShadowRadius;
Lloyd Piquec6687342019-03-07 21:34:57 -0800409
Lloyd Piquede196652020-01-22 17:29:58 -0800410 compositionState->contentDirty = contentDirty;
Lloyd Piquec6687342019-03-07 21:34:57 -0800411 contentDirty = false;
412
Lloyd Piquede196652020-01-22 17:29:58 -0800413 compositionState->geomLayerBounds = mBounds;
414 compositionState->geomLayerTransform = getTransform();
415 compositionState->geomInverseLayerTransform = compositionState->geomLayerTransform.inverse();
416 compositionState->transparentRegionHint = getActiveTransparentRegion(drawingState);
Lloyd Piquec6687342019-03-07 21:34:57 -0800417
Lloyd Piquede196652020-01-22 17:29:58 -0800418 compositionState->blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
419 compositionState->alpha = alpha;
420 compositionState->backgroundBlurRadius = drawingState.backgroundBlurRadius;
Lucas Dupinc3800b82020-10-02 16:24:48 -0700421 compositionState->blurRegions = drawingState.blurRegions;
John Reckc00c6692021-02-16 11:37:33 -0500422 compositionState->stretchEffect = getStretchEffect();
Lloyd Piquec6687342019-03-07 21:34:57 -0800423}
424
Lloyd Piquede196652020-01-22 17:29:58 -0800425void Layer::prepareGeometryCompositionState() {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800426 const auto& drawingState{getDrawingState()};
Mathias Agopian13127d82013-03-05 17:47:11 -0800427
Lloyd Piquea83776c2019-01-29 18:42:32 -0800428 int type = drawingState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
429 int appId = drawingState.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700430 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400431 if (parent.get()) {
432 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800433 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
434 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
Jiwen 'Steve' Cai736c74e2019-05-28 18:33:22 -0700435 if (parentType > 0 && parentAppId > 0) {
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800436 type = parentType;
437 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700438 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400439 }
440
Lloyd Piquede196652020-01-22 17:29:58 -0800441 auto* compositionState = editCompositionState();
David Sodman15094112018-10-11 09:39:37 -0700442
Lloyd Piquede196652020-01-22 17:29:58 -0800443 compositionState->geomBufferSize = getBufferSize(drawingState);
444 compositionState->geomContentCrop = getBufferCrop();
445 compositionState->geomCrop = getCrop(drawingState);
446 compositionState->geomBufferTransform = getBufferTransform();
447 compositionState->geomBufferUsesDisplayInverseTransform = getTransformToDisplayInverse();
448 compositionState->geomUsesSourceCrop = usesSourceCrop();
449 compositionState->isSecure = isSecure();
450
Lloyd Pique8d9f8362020-02-11 19:13:09 -0800451 compositionState->metadata.clear();
452 const auto& supportedMetadata = mFlinger->getHwComposer().getSupportedLayerGenericMetadata();
453 for (const auto& [key, mandatory] : supportedMetadata) {
454 const auto& genericLayerMetadataCompatibilityMap =
455 mFlinger->getGenericLayerMetadataKeyMap();
456 auto compatIter = genericLayerMetadataCompatibilityMap.find(key);
457 if (compatIter == std::end(genericLayerMetadataCompatibilityMap)) {
458 continue;
459 }
460 const uint32_t id = compatIter->second;
461
462 auto it = drawingState.metadata.mMap.find(id);
463 if (it == std::end(drawingState.metadata.mMap)) {
464 continue;
465 }
466
467 compositionState->metadata
468 .emplace(key, compositionengine::GenericLayerMetadataEntry{mandatory, it->second});
469 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800470}
David Sodmanba340492018-08-05 21:51:33 -0700471
Lloyd Piquede196652020-01-22 17:29:58 -0800472void Layer::preparePerFrameCompositionState() {
Lloyd Pique688abd42019-02-15 15:42:24 -0800473 const auto& drawingState{getDrawingState()};
Lloyd Piquede196652020-01-22 17:29:58 -0800474 auto* compositionState = editCompositionState();
Lloyd Piquef5275482019-01-29 18:42:42 -0800475
Lloyd Piquede196652020-01-22 17:29:58 -0800476 compositionState->forceClientComposition = false;
477
478 compositionState->isColorspaceAgnostic = isColorSpaceAgnostic();
479 compositionState->dataspace = getDataSpace();
480 compositionState->colorTransform = getColorTransform();
481 compositionState->colorTransformIsIdentity = !hasColorTransform();
482 compositionState->surfaceDamage = surfaceDamageRegion;
483 compositionState->hasProtectedContent = isProtected();
Lloyd Pique688abd42019-02-15 15:42:24 -0800484
485 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700486
Lloyd Piquede196652020-01-22 17:29:58 -0800487 compositionState->isOpaque =
Lloyd Pique688abd42019-02-15 15:42:24 -0800488 isOpaque(drawingState) && !usesRoundedCorners && getAlpha() == 1.0_hf;
Lloyd Piquef5275482019-01-29 18:42:42 -0800489
490 // Force client composition for special cases known only to the front-end.
Leon Scroggins IIId305ef22021-04-06 09:53:26 -0400491 // Rounded corners no longer force client composition, since we may use a
492 // hole punch so that the layer will appear to have rounded corners.
493 if (isHdrY410() || drawShadows() || drawingState.blurRegions.size() > 0 ||
John Reckc00c6692021-02-16 11:37:33 -0500494 compositionState->stretchEffect.hasEffect()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800495 compositionState->forceClientComposition = true;
Lloyd Piquef5275482019-01-29 18:42:42 -0800496 }
497}
498
Lloyd Piquede196652020-01-22 17:29:58 -0800499void Layer::prepareCursorCompositionState() {
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800500 const State& drawingState{getDrawingState()};
Lloyd Piquede196652020-01-22 17:29:58 -0800501 auto* compositionState = editCompositionState();
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800502
503 // Apply the layer's transform, followed by the display's global transform
504 // Here we're guaranteed that the layer's transform preserves rects
505 Rect win = getCroppedBufferSize(drawingState);
506 // Subtract the transparent region and snap to the bounds
507 Rect bounds = reduce(win, getActiveTransparentRegion(drawingState));
508 Rect frame(getTransform().transform(bounds));
509
Lloyd Piquede196652020-01-22 17:29:58 -0800510 compositionState->cursorFrame = frame;
511}
512
513sp<compositionengine::LayerFE> Layer::asLayerFE() const {
514 return const_cast<compositionengine::LayerFE*>(
515 static_cast<const compositionengine::LayerFE*>(this));
516}
517
518sp<compositionengine::LayerFE> Layer::getCompositionEngineLayerFE() const {
519 return nullptr;
520}
521
522compositionengine::LayerFECompositionState* Layer::editCompositionState() {
523 return nullptr;
524}
525
526const compositionengine::LayerFECompositionState* Layer::getCompositionState() const {
527 return nullptr;
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800528}
529
Lloyd Piquef16688f2019-02-19 17:47:57 -0800530bool Layer::onPreComposition(nsecs_t) {
531 return false;
532}
533
Lloyd Piquede196652020-01-22 17:29:58 -0800534void Layer::prepareCompositionState(compositionengine::LayerFE::StateSubset subset) {
Lloyd Piquec6687342019-03-07 21:34:57 -0800535 using StateSubset = compositionengine::LayerFE::StateSubset;
Lloyd Piquef5275482019-01-29 18:42:42 -0800536
Lloyd Piquec6687342019-03-07 21:34:57 -0800537 switch (subset) {
538 case StateSubset::BasicGeometry:
Lloyd Piquede196652020-01-22 17:29:58 -0800539 prepareBasicGeometryCompositionState();
Lloyd Piquec6687342019-03-07 21:34:57 -0800540 break;
541
542 case StateSubset::GeometryAndContent:
Lloyd Piquede196652020-01-22 17:29:58 -0800543 prepareBasicGeometryCompositionState();
544 prepareGeometryCompositionState();
545 preparePerFrameCompositionState();
Lloyd Piquec6687342019-03-07 21:34:57 -0800546 break;
547
548 case StateSubset::Content:
Lloyd Piquede196652020-01-22 17:29:58 -0800549 preparePerFrameCompositionState();
550 break;
551
552 case StateSubset::Cursor:
553 prepareCursorCompositionState();
Lloyd Piquec6687342019-03-07 21:34:57 -0800554 break;
555 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800556}
Mathias Agopian29a367b2011-07-12 14:51:45 -0700557
Lloyd Piquea83776c2019-01-29 18:42:32 -0800558const char* Layer::getDebugName() const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700559 return mName.c_str();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800560}
561
Mathias Agopian13127d82013-03-05 17:47:11 -0800562// ---------------------------------------------------------------------------
563// drawing...
564// ---------------------------------------------------------------------------
565
Vishnu Nair9b079a22020-01-21 14:36:08 -0800566std::optional<compositionengine::LayerFE::LayerSettings> Layer::prepareClientComposition(
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100567 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
Lloyd Piquede196652020-01-22 17:29:58 -0800568 if (!getCompositionState()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800569 return {};
570 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800571
Vishnu Nair4351ad52019-02-11 14:13:02 -0800572 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000573 half alpha = getAlpha();
Vishnu Nair9b079a22020-01-21 14:36:08 -0800574
575 compositionengine::LayerFE::LayerSettings layerSettings;
Lloyd Piquef16688f2019-02-19 17:47:57 -0800576 layerSettings.geometry.boundaries = bounds;
chaviwc6ad8af2020-08-03 11:33:30 -0700577 layerSettings.geometry.positionTransform = getTransform().asMatrix4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000578
Derek Sollenbergerc31985e2021-05-18 16:38:17 -0400579 // skip drawing content if the targetSettings indicate the content will be occluded
Ady Abrahambaa6cf52021-07-13 14:02:19 -0700580 const bool drawContent = targetSettings.realContentIsVisible || targetSettings.clearContent;
581 layerSettings.skipContentDraw = !drawContent;
Derek Sollenbergerc31985e2021-05-18 16:38:17 -0400582
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000583 if (hasColorTransform()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800584 layerSettings.colorTransform = getColorTransform();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000585 }
586
587 const auto roundedCornerState = getRoundedCornerState();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800588 layerSettings.geometry.roundedCornersRadius = roundedCornerState.radius;
589 layerSettings.geometry.roundedCornersCrop = roundedCornerState.cropRect;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000590
Lloyd Piquef16688f2019-02-19 17:47:57 -0800591 layerSettings.alpha = alpha;
chaviw4244e032019-09-04 11:27:49 -0700592 layerSettings.sourceDataspace = getDataSpace();
Alec Mourif54453c2021-05-13 16:28:28 -0700593 switch (targetSettings.blurSetting) {
594 case LayerFE::ClientCompositionTargetSettings::BlurSetting::Enabled:
595 layerSettings.backgroundBlurRadius = getBackgroundBlurRadius();
596 layerSettings.blurRegions = getBlurRegions();
597 layerSettings.blurRegionTransform =
598 getActiveTransform(getDrawingState()).inverse().asMatrix4();
599 break;
600 case LayerFE::ClientCompositionTargetSettings::BlurSetting::BackgroundBlurOnly:
601 layerSettings.backgroundBlurRadius = getBackgroundBlurRadius();
602 break;
603 case LayerFE::ClientCompositionTargetSettings::BlurSetting::BlurRegionsOnly:
604 layerSettings.blurRegions = getBlurRegions();
605 layerSettings.blurRegionTransform =
606 getActiveTransform(getDrawingState()).inverse().asMatrix4();
607 break;
608 case LayerFE::ClientCompositionTargetSettings::BlurSetting::Disabled:
609 default:
610 break;
Galia Peycheva66eaf4a2020-11-09 13:17:57 +0100611 }
Nader Jawad2dfc98b2021-04-08 20:35:39 -0700612 layerSettings.stretchEffect = getStretchEffect();
Ana Krulec6eab17a2020-12-09 15:52:36 -0800613 // Record the name of the layer for debugging further down the stack.
614 layerSettings.name = getName();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800615 return layerSettings;
Mathias Agopian13127d82013-03-05 17:47:11 -0800616}
617
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800618void Layer::prepareClearClientComposition(LayerFE::LayerSettings& layerSettings,
619 bool blackout) const {
620 layerSettings.source.buffer.buffer = nullptr;
621 layerSettings.source.solidColor = half3(0.0, 0.0, 0.0);
622 layerSettings.disableBlending = true;
Vishnu Nair3b653e72020-02-24 16:40:50 -0800623 layerSettings.bufferId = 0;
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800624 layerSettings.frameNumber = 0;
625
626 // If layer is blacked out, force alpha to 1 so that we draw a black color layer.
627 layerSettings.alpha = blackout ? 1.0f : 0.0f;
Alec Mouribd17b3b2020-12-17 11:08:30 -0800628 layerSettings.name = getName();
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800629}
630
Derek Sollenbergerc31985e2021-05-18 16:38:17 -0400631// TODO(b/188891810): This method now only ever returns 0 or 1 layers so we should return
632// std::optional instead of a vector. Additionally, we should consider removing
633// this method entirely in favor of calling prepareClientComposition directly.
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800634std::vector<compositionengine::LayerFE::LayerSettings> Layer::prepareClientCompositionList(
635 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
636 std::optional<compositionengine::LayerFE::LayerSettings> layerSettings =
637 prepareClientComposition(targetSettings);
638 // Nothing to render.
639 if (!layerSettings) {
640 return {};
641 }
642
643 // HWC requests to clear this layer.
644 if (targetSettings.clearContent) {
645 prepareClearClientComposition(*layerSettings, false /* blackout */);
646 return {*layerSettings};
647 }
648
Derek Sollenbergerc31985e2021-05-18 16:38:17 -0400649 // set the shadow for the layer if needed
650 prepareShadowClientComposition(*layerSettings, targetSettings.viewport);
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800651
Derek Sollenbergerc31985e2021-05-18 16:38:17 -0400652 return {*layerSettings};
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800653}
654
Dominik Laskowskib7251f42020-04-20 17:42:59 -0700655Hwc2::IComposerClient::Composition Layer::getCompositionType(const DisplayDevice& display) const {
656 const auto outputLayer = findOutputLayerForDisplay(&display);
Alec Mouri6b9e9912020-01-21 10:50:24 -0800657 if (outputLayer == nullptr) {
658 return Hwc2::IComposerClient::Composition::INVALID;
659 }
660 if (outputLayer->getState().hwc) {
661 return (*outputLayer->getState().hwc).hwcCompositionType;
662 } else {
663 return Hwc2::IComposerClient::Composition::CLIENT;
664 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800665}
666
Mathias Agopian13127d82013-03-05 17:47:11 -0800667// ----------------------------------------------------------------------------
668// local state
669// ----------------------------------------------------------------------------
670
David Sodman41fdfc92017-11-06 16:09:56 -0800671bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800672 const State& s(mDrawingState);
Garfield Tande619fa2020-10-02 17:13:53 -0700673 if (s.flags & layer_state_t::eLayerSecure) {
674 return true;
675 }
676
677 const auto p = mDrawingParent.promote();
678 return (p != nullptr) ? p->isSecure() : false;
Dan Stoza23116082015-06-18 14:58:39 -0700679}
680
Mathias Agopian13127d82013-03-05 17:47:11 -0800681// ----------------------------------------------------------------------------
682// transaction
683// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800684
Marissa Wall61c58622018-07-18 10:12:20 -0700685uint32_t Layer::doTransaction(uint32_t flags) {
686 ATRACE_CALL();
687
Robert Carr0758e5d2021-03-11 22:15:04 -0800688 // TODO: This is unfortunate.
Robert Carr6a160312021-05-17 12:08:20 -0700689 mDrawingStateModified = mDrawingState.modified;
690 mDrawingState.modified = false;
Marissa Wall61c58622018-07-18 10:12:20 -0700691
Alec Mourib416efd2018-09-06 21:01:59 +0000692 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700693
Robert Carr6a160312021-05-17 12:08:20 -0700694 if (updateGeometry()) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800695 // invalidate and recompute the visible regions if needed
696 flags |= Layer::eVisibleRegion;
697 }
698
Robert Carr6a160312021-05-17 12:08:20 -0700699 if (s.sequence != mLastCommittedTxSequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800700 // invalidate and recompute the visible regions if needed
Robert Carr6a160312021-05-17 12:08:20 -0700701 mLastCommittedTxSequence = s.sequence;
Mathias Agopian13127d82013-03-05 17:47:11 -0800702 flags |= eVisibleRegion;
703 this->contentDirty = true;
704
705 // we may use linear filtering, if the matrix scales us
Robert Carr6a160312021-05-17 12:08:20 -0700706 mNeedsFiltering = getActiveTransform(s).needsBilinearFiltering();
Mathias Agopian13127d82013-03-05 17:47:11 -0800707 }
708
Robert Carr6a160312021-05-17 12:08:20 -0700709 commitTransaction(mDrawingState);
Robert Carra1257842020-01-31 13:48:28 -0800710
Mathias Agopian13127d82013-03-05 17:47:11 -0800711 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800712}
713
Robert Carr6a160312021-05-17 12:08:20 -0700714void Layer::commitTransaction(State&) {
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +0000715 // Set the present state for all bufferlessSurfaceFramesTX to Presented. The
716 // bufferSurfaceFrameTX will be presented in latchBuffer.
717 for (auto& [token, surfaceFrame] : mDrawingState.bufferlessSurfaceFramesTX) {
718 if (surfaceFrame->getPresentState() != PresentState::Presented) {
719 // With applyPendingStates, we could end up having presented surfaceframes from previous
720 // states
721 surfaceFrame->setPresentState(PresentState::Presented);
722 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
723 }
724 }
Robert Carr6a160312021-05-17 12:08:20 -0700725 mDrawingState.bufferlessSurfaceFramesTX.clear();
Mathias Agopiana67932f2011-04-20 14:20:59 -0700726}
727
Mathias Agopian13127d82013-03-05 17:47:11 -0800728uint32_t Layer::getTransactionFlags(uint32_t flags) {
Robert Carr38d25002021-06-11 14:30:09 -0700729 auto ret = mTransactionFlags & flags;
730 mTransactionFlags &= ~flags;
731 return ret;
Mathias Agopian13127d82013-03-05 17:47:11 -0800732}
733
734uint32_t Layer::setTransactionFlags(uint32_t flags) {
Robert Carr38d25002021-06-11 14:30:09 -0700735 return mTransactionFlags |= flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800736}
737
chaviw214c89d2019-09-04 16:03:53 -0700738bool Layer::setPosition(float x, float y) {
Robert Carr6a160312021-05-17 12:08:20 -0700739 if (mDrawingState.transform.tx() == x && mDrawingState.transform.ty() == y) return false;
740 mDrawingState.sequence++;
741 mDrawingState.transform.set(x, y);
Robert Carr69663fb2016-03-27 19:59:19 -0700742
Robert Carr6a160312021-05-17 12:08:20 -0700743 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800744 setTransactionFlags(eTransactionNeeded);
745 return true;
746}
Robert Carr82364e32016-05-15 11:27:47 -0700747
Robert Carr1f0a16a2016-10-24 16:27:39 -0700748bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
749 ssize_t idx = mCurrentChildren.indexOf(childLayer);
750 if (idx < 0) {
751 return false;
752 }
753 if (childLayer->setLayer(z)) {
754 mCurrentChildren.removeAt(idx);
755 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800756 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700757 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800758 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700759}
760
Robert Carr503c7042017-09-27 15:06:08 -0700761bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
762 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
763 ssize_t idx = mCurrentChildren.indexOf(childLayer);
764 if (idx < 0) {
765 return false;
766 }
767 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
768 mCurrentChildren.removeAt(idx);
769 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800770 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700771 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800772 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700773}
774
Robert Carrae060832016-11-28 10:51:00 -0800775bool Layer::setLayer(int32_t z) {
Robert Carr6a160312021-05-17 12:08:20 -0700776 if (mDrawingState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
777 mDrawingState.sequence++;
778 mDrawingState.z = z;
779 mDrawingState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700780
Robert Carra70e91c2021-06-11 13:59:52 -0700781 mFlinger->mSomeChildrenChanged = true;
782
Robert Carrdb66e622017-04-10 16:55:57 -0700783 // Discard all relative layering.
Robert Carr6a160312021-05-17 12:08:20 -0700784 if (mDrawingState.zOrderRelativeOf != nullptr) {
785 sp<Layer> strongRelative = mDrawingState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700786 if (strongRelative != nullptr) {
787 strongRelative->removeZOrderRelative(this);
788 }
chaviw606e5cf2019-03-01 10:12:10 -0800789 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -0700790 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800791 setTransactionFlags(eTransactionNeeded);
792 return true;
793}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700794
Robert Carrdb66e622017-04-10 16:55:57 -0700795void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Robert Carr6a160312021-05-17 12:08:20 -0700796 mDrawingState.zOrderRelatives.remove(relative);
797 mDrawingState.sequence++;
798 mDrawingState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700799 setTransactionFlags(eTransactionNeeded);
800}
801
802void Layer::addZOrderRelative(const wp<Layer>& relative) {
Robert Carr6a160312021-05-17 12:08:20 -0700803 mDrawingState.zOrderRelatives.add(relative);
804 mDrawingState.modified = true;
805 mDrawingState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -0700806 setTransactionFlags(eTransactionNeeded);
807}
808
chaviw606e5cf2019-03-01 10:12:10 -0800809void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
Robert Carr6a160312021-05-17 12:08:20 -0700810 mDrawingState.zOrderRelativeOf = relativeOf;
811 mDrawingState.sequence++;
812 mDrawingState.modified = true;
813 mDrawingState.isRelativeOf = relativeOf != nullptr;
chaviwbdb8b802019-10-14 09:17:12 -0700814
chaviw606e5cf2019-03-01 10:12:10 -0800815 setTransactionFlags(eTransactionNeeded);
816}
817
Robert Carr503d2bd2017-12-04 15:49:47 -0800818bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Vishnu Nairf9096652021-07-20 18:49:42 -0700819 sp<Layer> relative = fromHandle(relativeToHandle).promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700820 if (relative == nullptr) {
821 return false;
822 }
823
Robert Carr6a160312021-05-17 12:08:20 -0700824 if (mDrawingState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
825 mDrawingState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -0800826 return false;
827 }
828
Robert Carra70e91c2021-06-11 13:59:52 -0700829 mFlinger->mSomeChildrenChanged = true;
830
Robert Carr6a160312021-05-17 12:08:20 -0700831 mDrawingState.sequence++;
832 mDrawingState.modified = true;
833 mDrawingState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -0700834
Robert Carr6a160312021-05-17 12:08:20 -0700835 auto oldZOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -0700836 if (oldZOrderRelativeOf != nullptr) {
837 oldZOrderRelativeOf->removeZOrderRelative(this);
838 }
chaviw606e5cf2019-03-01 10:12:10 -0800839 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -0700840 relative->addZOrderRelative(this);
841
842 setTransactionFlags(eTransactionNeeded);
843
844 return true;
845}
846
Winson Chunga30f7c92021-06-29 15:42:56 -0700847bool Layer::setTrustedOverlay(bool isTrustedOverlay) {
848 if (mDrawingState.isTrustedOverlay == isTrustedOverlay) return false;
849 mDrawingState.isTrustedOverlay = isTrustedOverlay;
850 mDrawingState.modified = true;
851 mFlinger->mInputInfoChanged = true;
852 setTransactionFlags(eTransactionNeeded);
853 return true;
854}
855
856bool Layer::isTrustedOverlay() const {
857 if (getDrawingState().isTrustedOverlay) {
858 return true;
859 }
860 const auto& p = mDrawingParent.promote();
861 return (p != nullptr) && p->isTrustedOverlay();
862}
863
Mathias Agopian13127d82013-03-05 17:47:11 -0800864bool Layer::setSize(uint32_t w, uint32_t h) {
Robert Carr6a160312021-05-17 12:08:20 -0700865 if (mDrawingState.requested_legacy.w == w && mDrawingState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -0700866 return false;
Robert Carr6a160312021-05-17 12:08:20 -0700867 mDrawingState.requested_legacy.w = w;
868 mDrawingState.requested_legacy.h = h;
869 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800870 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -0700871
872 // record the new size, from this point on, when the client request
873 // a buffer, it'll get the new size.
Robert Carr6a160312021-05-17 12:08:20 -0700874 setDefaultBufferSize(mDrawingState.requested_legacy.w, mDrawingState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -0800875 return true;
876}
chaviw4e765532021-04-30 12:11:39 -0500877
Dan Stoza9e56aa02015-11-02 13:00:03 -0800878bool Layer::setAlpha(float alpha) {
Robert Carr6a160312021-05-17 12:08:20 -0700879 if (mDrawingState.color.a == alpha) return false;
880 mDrawingState.sequence++;
881 mDrawingState.color.a = alpha;
882 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800883 setTransactionFlags(eTransactionNeeded);
884 return true;
885}
chaviw13fdc492017-06-27 12:40:18 -0700886
Valerie Haudd0b7572019-01-29 14:59:27 -0800887bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
Robert Carr6a160312021-05-17 12:08:20 -0700888 if (!mDrawingState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -0700889 return false;
Valerie Hauaa194562019-02-05 16:21:38 -0800890 }
Robert Carr6a160312021-05-17 12:08:20 -0700891 mDrawingState.sequence++;
892 mDrawingState.modified = true;
Valerie Hauaa194562019-02-05 16:21:38 -0800893 setTransactionFlags(eTransactionNeeded);
894
Robert Carr6a160312021-05-17 12:08:20 -0700895 if (!mDrawingState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -0800896 // create background color layer if one does not yet exist
Vishnu Nairfa247b12020-02-11 08:58:26 -0800897 uint32_t flags = ISurfaceComposerClient::eFXSurfaceEffect;
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700898 std::string name = mName + "BackgroundColorLayer";
Robert Carr6a160312021-05-17 12:08:20 -0700899 mDrawingState.bgColorLayer = mFlinger->getFactory().createEffectLayer(
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700900 LayerCreationArgs(mFlinger.get(), nullptr, std::move(name), 0, 0, flags,
901 LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -0700902
Valerie Haudd0b7572019-01-29 14:59:27 -0800903 // add to child list
Robert Carr6a160312021-05-17 12:08:20 -0700904 addChild(mDrawingState.bgColorLayer);
Valerie Haudd0b7572019-01-29 14:59:27 -0800905 mFlinger->mLayersAdded = true;
906 // set up SF to handle added color layer
907 if (isRemovedFromCurrentState()) {
Robert Carr6a160312021-05-17 12:08:20 -0700908 mDrawingState.bgColorLayer->onRemovedFromCurrentState();
Valerie Haudd0b7572019-01-29 14:59:27 -0800909 }
910 mFlinger->setTransactionFlags(eTransactionNeeded);
Robert Carr6a160312021-05-17 12:08:20 -0700911 } else if (mDrawingState.bgColorLayer && alpha == 0) {
912 mDrawingState.bgColorLayer->reparent(nullptr);
913 mDrawingState.bgColorLayer = nullptr;
Valerie Haudd0b7572019-01-29 14:59:27 -0800914 return true;
915 }
916
Robert Carr6a160312021-05-17 12:08:20 -0700917 mDrawingState.bgColorLayer->setColor(color);
918 mDrawingState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
919 mDrawingState.bgColorLayer->setAlpha(alpha);
920 mDrawingState.bgColorLayer->setDataspace(dataspace);
Valerie Haudd0b7572019-01-29 14:59:27 -0800921
chaviw13fdc492017-06-27 12:40:18 -0700922 return true;
923}
924
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700925bool Layer::setCornerRadius(float cornerRadius) {
Robert Carr6a160312021-05-17 12:08:20 -0700926 if (mDrawingState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700927
Robert Carr6a160312021-05-17 12:08:20 -0700928 mDrawingState.sequence++;
929 mDrawingState.cornerRadius = cornerRadius;
930 mDrawingState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700931 setTransactionFlags(eTransactionNeeded);
932 return true;
933}
934
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800935bool Layer::setBackgroundBlurRadius(int backgroundBlurRadius) {
Robert Carr6a160312021-05-17 12:08:20 -0700936 if (mDrawingState.backgroundBlurRadius == backgroundBlurRadius) return false;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800937
Robert Carr6a160312021-05-17 12:08:20 -0700938 mDrawingState.sequence++;
939 mDrawingState.backgroundBlurRadius = backgroundBlurRadius;
940 mDrawingState.modified = true;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800941 setTransactionFlags(eTransactionNeeded);
942 return true;
943}
944
Robert Carrd4ae7f32018-06-07 16:10:57 -0700945bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
946 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -0700947 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -0700948 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
949
950 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
Hongwei Wang46213ea2020-12-04 17:17:31 -0800951 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER nor "
952 "ROTATE_SURFACE_FLINGER ignored");
Robert Carrd4ae7f32018-06-07 16:10:57 -0700953 return false;
954 }
Robert Carr6a160312021-05-17 12:08:20 -0700955 mDrawingState.sequence++;
956 mDrawingState.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
957 mDrawingState.modified = true;
958
Mathias Agopian13127d82013-03-05 17:47:11 -0800959 setTransactionFlags(eTransactionNeeded);
960 return true;
961}
Marissa Wall61c58622018-07-18 10:12:20 -0700962
Mathias Agopian13127d82013-03-05 17:47:11 -0800963bool Layer::setTransparentRegionHint(const Region& transparent) {
Robert Carr6a160312021-05-17 12:08:20 -0700964 mDrawingState.requestedTransparentRegion_legacy = transparent;
965 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800966 setTransactionFlags(eTransactionNeeded);
967 return true;
968}
Ana Krulecc84d09b2019-11-02 23:10:29 +0100969
Lucas Dupinc3800b82020-10-02 16:24:48 -0700970bool Layer::setBlurRegions(const std::vector<BlurRegion>& blurRegions) {
Robert Carr6a160312021-05-17 12:08:20 -0700971 mDrawingState.blurRegions = blurRegions;
972 mDrawingState.modified = true;
Lucas Dupinc3800b82020-10-02 16:24:48 -0700973 setTransactionFlags(eTransactionNeeded);
974 return true;
975}
976
Vishnu Nairf6eddb62021-01-27 22:02:11 -0800977bool Layer::setFlags(uint32_t flags, uint32_t mask) {
Robert Carr6a160312021-05-17 12:08:20 -0700978 const uint32_t newFlags = (mDrawingState.flags & ~mask) | (flags & mask);
979 if (mDrawingState.flags == newFlags) return false;
980 mDrawingState.sequence++;
981 mDrawingState.flags = newFlags;
982 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800983 setTransactionFlags(eTransactionNeeded);
984 return true;
985}
Robert Carr99e27f02016-06-16 15:18:02 -0700986
chaviw25714502021-02-11 10:01:08 -0800987bool Layer::setCrop(const Rect& crop) {
Robert Carr6a160312021-05-17 12:08:20 -0700988 if (mDrawingState.requestedCrop == crop) return false;
989 mDrawingState.sequence++;
990 mDrawingState.requestedCrop = crop;
991 mDrawingState.crop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -0700992
Robert Carr6a160312021-05-17 12:08:20 -0700993 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800994 setTransactionFlags(eTransactionNeeded);
995 return true;
996}
Robert Carr8d5227b2017-03-16 15:41:03 -0700997
Evan Roskyef876c92019-01-25 17:46:06 -0800998bool Layer::setMetadata(const LayerMetadata& data) {
Robert Carr6a160312021-05-17 12:08:20 -0700999 if (!mDrawingState.metadata.merge(data, true /* eraseEmpty */)) return false;
1000 mDrawingState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001001 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001002 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001003}
1004
Dominik Laskowski29fa1462021-04-27 15:51:50 -07001005bool Layer::setLayerStack(ui::LayerStack layerStack) {
Robert Carr6a160312021-05-17 12:08:20 -07001006 if (mDrawingState.layerStack == layerStack) return false;
1007 mDrawingState.sequence++;
1008 mDrawingState.layerStack = layerStack;
1009 mDrawingState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001010 setTransactionFlags(eTransactionNeeded);
1011 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001012}
1013
Peiyong Linc502cb72019-03-01 15:00:23 -08001014bool Layer::setColorSpaceAgnostic(const bool agnostic) {
Robert Carr6a160312021-05-17 12:08:20 -07001015 if (mDrawingState.colorSpaceAgnostic == agnostic) {
Peiyong Linc502cb72019-03-01 15:00:23 -08001016 return false;
1017 }
Robert Carr6a160312021-05-17 12:08:20 -07001018 mDrawingState.sequence++;
1019 mDrawingState.colorSpaceAgnostic = agnostic;
1020 mDrawingState.modified = true;
Peiyong Linc502cb72019-03-01 15:00:23 -08001021 setTransactionFlags(eTransactionNeeded);
1022 return true;
1023}
1024
Ana Krulecc84d09b2019-11-02 23:10:29 +01001025bool Layer::setFrameRateSelectionPriority(int32_t priority) {
Robert Carr6a160312021-05-17 12:08:20 -07001026 if (mDrawingState.frameRateSelectionPriority == priority) return false;
1027 mDrawingState.frameRateSelectionPriority = priority;
1028 mDrawingState.sequence++;
1029 mDrawingState.modified = true;
Ana Krulecc84d09b2019-11-02 23:10:29 +01001030 setTransactionFlags(eTransactionNeeded);
1031 return true;
1032}
1033
1034int32_t Layer::getFrameRateSelectionPriority() const {
1035 // Check if layer has priority set.
1036 if (mDrawingState.frameRateSelectionPriority != PRIORITY_UNSET) {
1037 return mDrawingState.frameRateSelectionPriority;
1038 }
1039 // If not, search whether its parents have it set.
1040 sp<Layer> parent = getParent();
1041 if (parent != nullptr) {
1042 return parent->getFrameRateSelectionPriority();
1043 }
1044
1045 return Layer::PRIORITY_UNSET;
1046}
1047
Ady Abrahamaae5ed52020-06-26 09:32:43 -07001048bool Layer::isLayerFocusedBasedOnPriority(int32_t priority) {
1049 return priority == PRIORITY_FOCUSED_WITH_MODE || priority == PRIORITY_FOCUSED_WITHOUT_MODE;
1050};
1051
Dominik Laskowski29fa1462021-04-27 15:51:50 -07001052ui::LayerStack Layer::getLayerStack() const {
1053 if (const auto parent = mDrawingParent.promote()) {
1054 return parent->getLayerStack();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001055 }
Dominik Laskowski29fa1462021-04-27 15:51:50 -07001056 return getDrawingState().layerStack;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001057}
1058
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001059bool Layer::setShadowRadius(float shadowRadius) {
Robert Carr6a160312021-05-17 12:08:20 -07001060 if (mDrawingState.shadowRadius == shadowRadius) {
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001061 return false;
1062 }
1063
Robert Carr6a160312021-05-17 12:08:20 -07001064 mDrawingState.sequence++;
1065 mDrawingState.shadowRadius = shadowRadius;
1066 mDrawingState.modified = true;
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001067 setTransactionFlags(eTransactionNeeded);
1068 return true;
1069}
1070
Vishnu Nair6213bd92020-05-08 17:42:25 -07001071bool Layer::setFixedTransformHint(ui::Transform::RotationFlags fixedTransformHint) {
Robert Carr6a160312021-05-17 12:08:20 -07001072 if (mDrawingState.fixedTransformHint == fixedTransformHint) {
Vishnu Nair6213bd92020-05-08 17:42:25 -07001073 return false;
1074 }
1075
Robert Carr6a160312021-05-17 12:08:20 -07001076 mDrawingState.sequence++;
1077 mDrawingState.fixedTransformHint = fixedTransformHint;
1078 mDrawingState.modified = true;
Vishnu Nair6213bd92020-05-08 17:42:25 -07001079 setTransactionFlags(eTransactionNeeded);
1080 return true;
1081}
1082
John Reckcdb4ed72021-02-04 13:39:33 -05001083bool Layer::setStretchEffect(const StretchEffect& effect) {
1084 StretchEffect temp = effect;
1085 temp.sanitize();
Robert Carr6a160312021-05-17 12:08:20 -07001086 if (mDrawingState.stretchEffect == temp) {
John Reckcdb4ed72021-02-04 13:39:33 -05001087 return false;
1088 }
Robert Carr6a160312021-05-17 12:08:20 -07001089 mDrawingState.sequence++;
1090 mDrawingState.stretchEffect = temp;
1091 mDrawingState.modified = true;
John Reckcdb4ed72021-02-04 13:39:33 -05001092 setTransactionFlags(eTransactionNeeded);
1093 return true;
1094}
1095
John Reckc00c6692021-02-16 11:37:33 -05001096StretchEffect Layer::getStretchEffect() const {
1097 if (mDrawingState.stretchEffect.hasEffect()) {
1098 return mDrawingState.stretchEffect;
1099 }
1100
1101 sp<Layer> parent = getParent();
1102 if (parent != nullptr) {
1103 auto effect = parent->getStretchEffect();
1104 if (effect.hasEffect()) {
1105 // TODO(b/179047472): Map it? Or do we make the effect be in global space?
1106 return effect;
1107 }
1108 }
1109 return StretchEffect{};
1110}
1111
Ady Abrahama850c182021-08-04 13:04:37 -07001112bool Layer::propagateFrameRateForLayerTree(FrameRate parentFrameRate, bool* transactionNeeded) {
1113 // The frame rate for layer tree is this layer's frame rate if present, or the parent frame rate
1114 const auto frameRate = [&] {
1115 if (mDrawingState.frameRate.rate.isValid() ||
1116 mDrawingState.frameRate.type == FrameRateCompatibility::NoVote) {
1117 return mDrawingState.frameRate;
1118 }
1119
1120 return parentFrameRate;
1121 }();
1122
1123 *transactionNeeded |= setFrameRateForLayerTree(frameRate);
1124
1125 // The frame rate is propagated to the children
1126 bool childrenHaveFrameRate = false;
1127 for (const sp<Layer>& child : mCurrentChildren) {
1128 childrenHaveFrameRate |=
1129 child->propagateFrameRateForLayerTree(frameRate, transactionNeeded);
1130 }
1131
1132 // If we don't have a valid frame rate, but the children do, we set this
1133 // layer as NoVote to allow the children to control the refresh rate
1134 if (!frameRate.rate.isValid() && frameRate.type != FrameRateCompatibility::NoVote &&
1135 childrenHaveFrameRate) {
1136 *transactionNeeded |=
1137 setFrameRateForLayerTree(FrameRate(Fps(0.0f), FrameRateCompatibility::NoVote));
1138 }
1139
1140 // We return whether this layer ot its children has a vote. We ignore ExactOrMultiple votes for
1141 // the same reason we are allowing touch boost for those layers. See
1142 // RefreshRateConfigs::getBestRefreshRate for more details.
1143 const auto layerVotedWithDefaultCompatibility =
1144 frameRate.rate.isValid() && frameRate.type == FrameRateCompatibility::Default;
1145 const auto layerVotedWithNoVote = frameRate.type == FrameRateCompatibility::NoVote;
1146 const auto layerVotedWithExactCompatibility =
1147 frameRate.rate.isValid() && frameRate.type == FrameRateCompatibility::Exact;
1148 return layerVotedWithDefaultCompatibility || layerVotedWithNoVote ||
1149 layerVotedWithExactCompatibility || childrenHaveFrameRate;
1150}
1151
Ady Abraham60e42ea2020-03-09 19:17:31 -07001152void Layer::updateTreeHasFrameRateVote() {
Ady Abrahama850c182021-08-04 13:04:37 -07001153 const auto root = [&]() -> sp<Layer> {
1154 sp<Layer> layer = this;
1155 while (auto parent = layer->getParent()) {
1156 layer = parent;
Ady Abraham60e42ea2020-03-09 19:17:31 -07001157 }
Ady Abrahama850c182021-08-04 13:04:37 -07001158 return layer;
1159 }();
Ady Abraham60e42ea2020-03-09 19:17:31 -07001160
Ady Abraham60e42ea2020-03-09 19:17:31 -07001161 bool transactionNeeded = false;
Ady Abrahama850c182021-08-04 13:04:37 -07001162 root->propagateFrameRateForLayerTree({}, &transactionNeeded);
Robert Carr6a160312021-05-17 12:08:20 -07001163
Ady Abrahama850c182021-08-04 13:04:37 -07001164 // TODO(b/195668952): we probably don't need eTraversalNeeded here
Ady Abraham60e42ea2020-03-09 19:17:31 -07001165 if (transactionNeeded) {
1166 mFlinger->setTransactionFlags(eTraversalNeeded);
1167 }
1168}
1169
Ady Abraham71c437d2020-01-31 15:56:57 -08001170bool Layer::setFrameRate(FrameRate frameRate) {
Ana Krulec3803b8d2020-02-03 16:35:46 -08001171 if (!mFlinger->useFrameRateApi) {
1172 return false;
1173 }
Robert Carr6a160312021-05-17 12:08:20 -07001174 if (mDrawingState.frameRate == frameRate) {
Steven Thomas3172e202020-01-06 19:25:30 -08001175 return false;
1176 }
1177
Robert Carr6a160312021-05-17 12:08:20 -07001178 mDrawingState.sequence++;
1179 mDrawingState.frameRate = frameRate;
1180 mDrawingState.modified = true;
Ady Abraham60e42ea2020-03-09 19:17:31 -07001181
1182 updateTreeHasFrameRateVote();
1183
Steven Thomas3172e202020-01-06 19:25:30 -08001184 setTransactionFlags(eTransactionNeeded);
1185 return true;
1186}
1187
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001188void Layer::setFrameTimelineVsyncForBufferTransaction(const FrameTimelineInfo& info,
1189 nsecs_t postTime) {
Robert Carr6a160312021-05-17 12:08:20 -07001190 mDrawingState.postTime = postTime;
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001191
1192 // Check if one of the bufferlessSurfaceFramesTX contains the same vsyncId. This can happen if
1193 // there are two transactions with the same token, the first one without a buffer and the
1194 // second one with a buffer. We promote the bufferlessSurfaceFrame to a bufferSurfaceFrameTX
1195 // in that case.
Robert Carr6a160312021-05-17 12:08:20 -07001196 auto it = mDrawingState.bufferlessSurfaceFramesTX.find(info.vsyncId);
1197 if (it != mDrawingState.bufferlessSurfaceFramesTX.end()) {
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001198 // Promote the bufferlessSurfaceFrame to a bufferSurfaceFrameTX
Robert Carr6a160312021-05-17 12:08:20 -07001199 mDrawingState.bufferSurfaceFrameTX = it->second;
1200 mDrawingState.bufferlessSurfaceFramesTX.erase(it);
1201 mDrawingState.bufferSurfaceFrameTX->promoteToBuffer();
1202 mDrawingState.bufferSurfaceFrameTX->setActualQueueTime(postTime);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001203 } else {
Robert Carr6a160312021-05-17 12:08:20 -07001204 mDrawingState.bufferSurfaceFrameTX =
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001205 createSurfaceFrameForBuffer(info, postTime, mTransactionName);
1206 }
1207}
1208
1209void Layer::setFrameTimelineVsyncForBufferlessTransaction(const FrameTimelineInfo& info,
1210 nsecs_t postTime) {
Robert Carr6a160312021-05-17 12:08:20 -07001211 mDrawingState.frameTimelineInfo = info;
1212 mDrawingState.postTime = postTime;
1213 mDrawingState.modified = true;
Ady Abraham22c7b5c2020-09-22 19:33:40 -07001214 setTransactionFlags(eTransactionNeeded);
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001215
Robert Carr6a160312021-05-17 12:08:20 -07001216 if (const auto& bufferSurfaceFrameTX = mDrawingState.bufferSurfaceFrameTX;
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001217 bufferSurfaceFrameTX != nullptr) {
1218 if (bufferSurfaceFrameTX->getToken() == info.vsyncId) {
1219 // BufferSurfaceFrame takes precedence over BufferlessSurfaceFrame. If the same token is
1220 // being used for BufferSurfaceFrame, don't create a new one.
1221 return;
1222 }
1223 }
1224 // For Transactions without a buffer, we create only one SurfaceFrame per vsyncId. If multiple
1225 // transactions use the same vsyncId, we just treat them as one SurfaceFrame (unless they are
1226 // targeting different vsyncs).
Robert Carr6a160312021-05-17 12:08:20 -07001227 auto it = mDrawingState.bufferlessSurfaceFramesTX.find(info.vsyncId);
1228 if (it == mDrawingState.bufferlessSurfaceFramesTX.end()) {
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001229 auto surfaceFrame = createSurfaceFrameForTransaction(info, postTime);
Robert Carr6a160312021-05-17 12:08:20 -07001230 mDrawingState.bufferlessSurfaceFramesTX[info.vsyncId] = surfaceFrame;
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001231 } else {
1232 if (it->second->getPresentState() == PresentState::Presented) {
1233 // If the SurfaceFrame was already presented, its safe to overwrite it since it must
1234 // have been from previous vsync.
1235 it->second = createSurfaceFrameForTransaction(info, postTime);
1236 }
1237 }
1238}
1239
1240void Layer::addSurfaceFrameDroppedForBuffer(
1241 std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame) {
Adithya Srinivasan061c14c2021-02-11 01:19:47 +00001242 surfaceFrame->setDropTime(systemTime());
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001243 surfaceFrame->setPresentState(PresentState::Dropped);
1244 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
1245}
1246
1247void Layer::addSurfaceFramePresentedForBuffer(
1248 std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame, nsecs_t acquireFenceTime,
1249 nsecs_t currentLatchTime) {
1250 surfaceFrame->setAcquireFenceTime(acquireFenceTime);
1251 surfaceFrame->setPresentState(PresentState::Presented, mLastLatchTime);
1252 mFlinger->mFrameTimeline->addSurfaceFrame(surfaceFrame);
1253 mLastLatchTime = currentLatchTime;
1254}
1255
1256std::shared_ptr<frametimeline::SurfaceFrame> Layer::createSurfaceFrameForTransaction(
1257 const FrameTimelineInfo& info, nsecs_t postTime) {
1258 auto surfaceFrame =
Alec Mouriadebf5c2021-01-05 12:57:36 -08001259 mFlinger->mFrameTimeline->createSurfaceFrameForToken(info, mOwnerPid, mOwnerUid,
1260 getSequence(), mName,
Adithya Srinivasan785addd2021-03-09 00:38:00 +00001261 mTransactionName,
Adithya Srinivasan58069dc2021-06-04 20:37:02 +00001262 /*isBuffer*/ false, getGameMode());
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001263 // For Transactions, the post time is considered to be both queue and acquire fence time.
1264 surfaceFrame->setActualQueueTime(postTime);
1265 surfaceFrame->setAcquireFenceTime(postTime);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001266 const auto fps = mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
1267 if (fps) {
Alec Mouri819f6302021-02-12 15:37:21 -08001268 surfaceFrame->setRenderRate(*fps);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001269 }
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001270 onSurfaceFrameCreated(surfaceFrame);
1271 return surfaceFrame;
1272}
1273
1274std::shared_ptr<frametimeline::SurfaceFrame> Layer::createSurfaceFrameForBuffer(
1275 const FrameTimelineInfo& info, nsecs_t queueTime, std::string debugName) {
1276 auto surfaceFrame =
Alec Mouriadebf5c2021-01-05 12:57:36 -08001277 mFlinger->mFrameTimeline->createSurfaceFrameForToken(info, mOwnerPid, mOwnerUid,
Adithya Srinivasan785addd2021-03-09 00:38:00 +00001278 getSequence(), mName, debugName,
Adithya Srinivasan58069dc2021-06-04 20:37:02 +00001279 /*isBuffer*/ true, getGameMode());
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001280 // For buffers, acquire fence time will set during latch.
1281 surfaceFrame->setActualQueueTime(queueTime);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001282 const auto fps = mFlinger->mScheduler->getFrameRateOverride(getOwnerUid());
1283 if (fps) {
Alec Mouri819f6302021-02-12 15:37:21 -08001284 surfaceFrame->setRenderRate(*fps);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001285 }
Adithya Srinivasanb9a7dab2021-01-14 23:49:46 +00001286 // TODO(b/178542907): Implement onSurfaceFrameCreated for BQLayer as well.
1287 onSurfaceFrameCreated(surfaceFrame);
1288 return surfaceFrame;
Ady Abraham74e17562020-08-24 18:18:19 -07001289}
1290
Ady Abrahama850c182021-08-04 13:04:37 -07001291bool Layer::setFrameRateForLayerTree(FrameRate frameRate) {
1292 if (mDrawingState.frameRateForLayerTree == frameRate) {
1293 return false;
Ady Abraham60e42ea2020-03-09 19:17:31 -07001294 }
1295
Ady Abrahama850c182021-08-04 13:04:37 -07001296 mDrawingState.frameRateForLayerTree = frameRate;
Ady Abrahamf467f892020-07-31 16:01:53 -07001297
Ady Abrahama850c182021-08-04 13:04:37 -07001298 // TODO(b/195668952): we probably don't need to dirty visible regions here
1299 // or even store frameRateForLayerTree in mDrawingState
1300 mDrawingState.sequence++;
1301 mDrawingState.modified = true;
1302 setTransactionFlags(eTransactionNeeded);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001303
Ady Abrahama850c182021-08-04 13:04:37 -07001304 mFlinger->mScheduler->recordLayerHistory(this, systemTime(),
1305 LayerHistory::LayerUpdateType::SetFrameRate);
1306
1307 return true;
Steven Thomas3172e202020-01-06 19:25:30 -08001308}
1309
Ady Abraham59fd8ff2021-04-15 20:13:30 -07001310Layer::FrameRate Layer::getFrameRateForLayerTree() const {
1311 return getDrawingState().frameRateForLayerTree;
1312}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001313
Robert Carr1f0a16a2016-10-24 16:27:39 -07001314bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001315 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001316 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001317 if (parent != nullptr && parent->isHiddenByPolicy()) {
1318 return true;
1319 }
Robert Carr1c5481e2019-07-01 14:42:27 -07001320 if (usingRelativeZ(LayerVector::StateSet::Drawing)) {
1321 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
1322 if (zOrderRelativeOf != nullptr) {
1323 if (zOrderRelativeOf->isHiddenByPolicy()) {
1324 return true;
1325 }
1326 }
1327 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001328 return s.flags & layer_state_t::eLayerHidden;
1329}
1330
David Sodman41fdfc92017-11-06 16:09:56 -08001331uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001332 // TODO: should we do something special if mSecure is set?
1333 if (mProtectedByApp) {
1334 // need a hardware-protected path to external video sink
1335 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001336 }
Riley Andrews03414a12014-07-01 14:22:59 -07001337 if (mPotentialCursor) {
1338 usage |= GraphicBuffer::USAGE_CURSOR;
1339 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001340 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001341 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001342}
1343
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001344void Layer::updateTransformHint(ui::Transform::RotationFlags transformHint) {
1345 if (mFlinger->mDebugDisableTransformHint || transformHint & ui::Transform::ROT_INVALID) {
1346 transformHint = ui::Transform::ROT_0;
Mathias Agopiana4583642011-08-23 18:03:18 -07001347 }
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001348
Vishnu Nair6213bd92020-05-08 17:42:25 -07001349 setTransformHint(transformHint);
Mathias Agopiana4583642011-08-23 18:03:18 -07001350}
1351
Mathias Agopian13127d82013-03-05 17:47:11 -08001352// ----------------------------------------------------------------------------
1353// debugging
1354// ----------------------------------------------------------------------------
1355
Marissa Wall61c58622018-07-18 10:12:20 -07001356// TODO(marissaw): add new layer state info to layer debugging
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001357LayerDebugInfo Layer::getLayerDebugInfo(const DisplayDevice* display) const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001358 using namespace std::string_literals;
1359
Kalle Raitaa099a242017-01-11 11:17:29 -08001360 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001361 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001362 info.mName = getName();
Vishnu Nair43bccf82020-06-05 10:53:37 -07001363 sp<Layer> parent = mDrawingParent.promote();
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001364 info.mParentName = parent ? parent->getName() : "none"s;
chaviw8a01fa42019-08-19 12:39:31 -07001365 info.mType = getType();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001366 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Lloyd Piquea2468662019-03-07 21:31:06 -08001367
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001368 info.mVisibleRegion = getVisibleRegion(display);
Kalle Raitaa099a242017-01-11 11:17:29 -08001369 info.mSurfaceDamageRegion = surfaceDamageRegion;
Dominik Laskowski29fa1462021-04-27 15:51:50 -07001370 info.mLayerStack = getLayerStack().id;
chaviw4e765532021-04-30 12:11:39 -05001371 info.mX = ds.transform.tx();
1372 info.mY = ds.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001373 info.mZ = ds.z;
chaviw4e765532021-04-30 12:11:39 -05001374 info.mWidth = ds.width;
1375 info.mHeight = ds.height;
chaviw25714502021-02-11 10:01:08 -08001376 info.mCrop = ds.crop;
chaviw13fdc492017-06-27 12:40:18 -07001377 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001378 info.mFlags = ds.flags;
1379 info.mPixelFormat = getPixelFormat();
chaviw4244e032019-09-04 11:27:49 -07001380 info.mDataSpace = static_cast<android_dataspace>(getDataSpace());
chaviw4e765532021-04-30 12:11:39 -05001381 info.mMatrix[0][0] = ds.transform[0][0];
1382 info.mMatrix[0][1] = ds.transform[0][1];
1383 info.mMatrix[1][0] = ds.transform[1][0];
1384 info.mMatrix[1][1] = ds.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001385 {
chaviwd62d3062019-09-04 14:48:02 -07001386 sp<const GraphicBuffer> buffer = getBuffer();
David Sodman5b4cffc2017-11-23 13:20:29 -08001387 if (buffer != 0) {
1388 info.mActiveBufferWidth = buffer->getWidth();
1389 info.mActiveBufferHeight = buffer->getHeight();
1390 info.mActiveBufferStride = buffer->getStride();
1391 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001392 } else {
1393 info.mActiveBufferWidth = 0;
1394 info.mActiveBufferHeight = 0;
1395 info.mActiveBufferStride = 0;
1396 info.mActiveBufferFormat = 0;
1397 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001398 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001399 info.mNumQueuedFrames = getQueuedFrameCount();
1400 info.mRefreshPending = isBufferLatched();
1401 info.mIsOpaque = isOpaque(ds);
1402 info.mContentDirty = contentDirty;
John Reckc00c6692021-02-16 11:37:33 -05001403 info.mStretchEffect = getStretchEffect();
Kalle Raitaa099a242017-01-11 11:17:29 -08001404 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001405}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001406
Yiwei Zhang5434a782018-12-05 18:06:32 -08001407void Layer::miniDumpHeader(std::string& result) {
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001408 result.append(kDumpTableRowLength, '-');
1409 result.append("\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001410 result.append(" Layer name\n");
1411 result.append(" Z | ");
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001412 result.append(" Window Type | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001413 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001414 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001415 result.append(" Disp Frame (LTRB) | ");
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001416 result.append(" Source Crop (LTRB) | ");
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001417 result.append(" Frame Rate (Explicit) (Seamlessness) [Focused]\n");
1418 result.append(kDumpTableRowLength, '-');
1419 result.append("\n");
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001420}
1421
1422std::string Layer::frameRateCompatibilityString(Layer::FrameRateCompatibility compatibility) {
1423 switch (compatibility) {
1424 case FrameRateCompatibility::Default:
1425 return "Default";
1426 case FrameRateCompatibility::ExactOrMultiple:
1427 return "ExactOrMultiple";
1428 case FrameRateCompatibility::NoVote:
1429 return "NoVote";
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08001430 case FrameRateCompatibility::Exact:
1431 return "Exact";
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001432 }
Dan Stozae22aec72016-08-01 13:20:59 -07001433}
1434
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001435void Layer::miniDump(std::string& result, const DisplayDevice& display) const {
1436 const auto outputLayer = findOutputLayerForDisplay(&display);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001437 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001438 return;
1439 }
1440
Yiwei Zhang5434a782018-12-05 18:06:32 -08001441 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001442 if (mName.length() > 77) {
1443 std::string shortened;
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001444 shortened.append(mName, 0, 36);
Dan Stozae22aec72016-08-01 13:20:59 -07001445 shortened.append("[...]");
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001446 shortened.append(mName, mName.length() - 36);
1447 name = std::move(shortened);
Dan Stozae22aec72016-08-01 13:20:59 -07001448 } else {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001449 name = mName;
Dan Stozae22aec72016-08-01 13:20:59 -07001450 }
1451
Yiwei Zhang5434a782018-12-05 18:06:32 -08001452 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001453
Alec Mourib416efd2018-09-06 21:01:59 +00001454 const State& layerState(getDrawingState());
Lloyd Piquede196652020-01-22 17:29:58 -08001455 const auto& outputLayerState = outputLayer->getState();
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001456
Chia-I Wu1e043612018-03-01 09:45:09 -08001457 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001458 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001459 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001460 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001461 }
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001462 StringAppendF(&result, " %10d | ", mWindowType);
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001463 StringAppendF(&result, "%10s | ", toString(getCompositionType(display)).c_str());
Lloyd Piquede196652020-01-22 17:29:58 -08001464 StringAppendF(&result, "%10s | ", toString(outputLayerState.bufferTransform).c_str());
1465 const Rect& frame = outputLayerState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001466 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Piquede196652020-01-22 17:29:58 -08001467 const FloatRect& crop = outputLayerState.sourceCrop;
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001468 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f | ", crop.left, crop.top, crop.right,
Yiwei Zhang5434a782018-12-05 18:06:32 -08001469 crop.bottom);
Ady Abrahamf467f892020-07-31 16:01:53 -07001470 const auto frameRate = getFrameRateForLayerTree();
1471 if (frameRate.rate.isValid() || frameRate.type != FrameRateCompatibility::Default) {
1472 StringAppendF(&result, "%s %15s %17s", to_string(frameRate.rate).c_str(),
1473 frameRateCompatibilityString(frameRate.type).c_str(),
1474 toString(frameRate.seamlessness).c_str());
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001475 } else {
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001476 result.append(41, ' ');
Ady Abrahambe23e6a2020-05-04 14:51:16 -07001477 }
Dan Stozae22aec72016-08-01 13:20:59 -07001478
Ady Abrahamaae5ed52020-06-26 09:32:43 -07001479 const auto focused = isLayerFocusedBasedOnPriority(getFrameRateSelectionPriority());
1480 StringAppendF(&result, " [%s]\n", focused ? "*" : " ");
1481
Marin Shalamanov1876e2e2020-12-04 13:23:59 +01001482 result.append(kDumpTableRowLength, '-');
1483 result.append("\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001484}
Dan Stozae22aec72016-08-01 13:20:59 -07001485
Yiwei Zhang5434a782018-12-05 18:06:32 -08001486void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001487 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001488}
1489
Svetoslavd85084b2014-03-20 10:28:31 -07001490void Layer::clearFrameStats() {
1491 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001492}
1493
Jamie Gennis6547ff42013-07-16 20:12:42 -07001494void Layer::logFrameStats() {
1495 mFrameTracker.logAndResetStats(mName);
1496}
1497
Svetoslavd85084b2014-03-20 10:28:31 -07001498void Layer::getFrameStats(FrameStats* outStats) const {
1499 mFrameTracker.getStats(outStats);
1500}
1501
Yiwei Zhang5434a782018-12-05 18:06:32 -08001502void Layer::dumpFrameEvents(std::string& result) {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001503 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().c_str(), getType(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001504 Mutex::Autolock lock(mFrameEventHistoryMutex);
1505 mFrameEventHistory.checkFencesForCompletion();
1506 mFrameEventHistory.dump(result);
1507}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001508
Vishnu Nair0f085c62019-08-30 08:49:12 -07001509void Layer::dumpCallingUidPid(std::string& result) const {
chaviw250bcbb2020-08-05 11:17:54 -07001510 StringAppendF(&result, "Layer %s (%s) callingPid:%d callingUid:%d ownerUid:%d\n",
1511 getName().c_str(), getType(), mCallingPid, mCallingUid, mOwnerUid);
Vishnu Nair0f085c62019-08-30 08:49:12 -07001512}
1513
Brian Anderson5ea5e592016-12-01 16:54:33 -08001514void Layer::onDisconnect() {
1515 Mutex::Autolock lock(mFrameEventHistoryMutex);
1516 mFrameEventHistory.onDisconnect();
Yiwei Zhang1a88c402019-11-18 10:43:58 -08001517 const int32_t layerId = getSequence();
1518 mFlinger->mTimeStats->onDestroy(layerId);
1519 mFlinger->mFrameTracer->onDestroy(layerId);
Brian Anderson5ea5e592016-12-01 16:54:33 -08001520}
1521
Brian Anderson3890c392016-07-25 12:48:08 -07001522void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001523 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001524 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001525 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
Adithya Srinivasan58069dc2021-06-04 20:37:02 +00001526 getName().c_str(), mOwnerUid, newTimestamps->postedTime,
1527 getGameMode());
Yiwei Zhang487340f2019-11-18 17:42:12 -08001528 mFlinger->mTimeStats->setAcquireFence(getSequence(), newTimestamps->frameNumber,
1529 newTimestamps->acquireFence);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001530 }
1531
Brian Andersond6927fb2016-07-23 23:37:30 -07001532 Mutex::Autolock lock(mFrameEventHistoryMutex);
1533 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001534 // If there are any unsignaled fences in the aquire timeline at this
1535 // point, the previously queued frame hasn't been latched yet. Go ahead
1536 // and try to get the signal time here so the syscall is taken out of
1537 // the main thread's critical path.
1538 mAcquireTimeline.updateSignalTimes();
1539 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001540 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001541 mFrameEventHistory.addQueue(*newTimestamps);
1542 }
1543
Brian Anderson3890c392016-07-25 12:48:08 -07001544 if (outDelta) {
1545 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001546 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001547}
Dan Stozae77c7662016-05-13 11:37:28 -07001548
Chia-I Wu98f1c102017-05-30 14:54:08 -07001549size_t Layer::getChildrenCount() const {
1550 size_t count = 0;
1551 for (const sp<Layer>& child : mCurrentChildren) {
1552 count += 1 + child->getChildrenCount();
1553 }
1554 return count;
1555}
1556
Adithya Srinivasanac977e62021-05-21 22:50:56 +00001557void Layer::setGameModeForTree(int parentGameMode) {
1558 int gameMode = parentGameMode;
Robert Carr6a160312021-05-17 12:08:20 -07001559 auto& currentState = getDrawingState();
Adithya Srinivasanac977e62021-05-21 22:50:56 +00001560 if (currentState.metadata.has(METADATA_GAME_MODE)) {
1561 gameMode = currentState.metadata.getInt32(METADATA_GAME_MODE, 0);
1562 }
1563 setGameMode(gameMode);
1564 for (const sp<Layer>& child : mCurrentChildren) {
1565 child->setGameModeForTree(gameMode);
1566 }
1567}
1568
Robert Carr1f0a16a2016-10-24 16:27:39 -07001569void Layer::addChild(const sp<Layer>& layer) {
Robert Carre450fb52021-06-11 13:21:09 -07001570 mFlinger->mSomeChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001571 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001572
Robert Carr1f0a16a2016-10-24 16:27:39 -07001573 mCurrentChildren.add(layer);
1574 layer->setParent(this);
Adithya Srinivasanac977e62021-05-21 22:50:56 +00001575 layer->setGameModeForTree(mGameMode);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001576 updateTreeHasFrameRateVote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001577}
1578
1579ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carre450fb52021-06-11 13:21:09 -07001580 mFlinger->mSomeChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001581 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001582
Robert Carr1323c952019-01-28 18:13:27 -08001583 layer->setParent(nullptr);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001584 const auto removeResult = mCurrentChildren.remove(layer);
1585
1586 updateTreeHasFrameRateVote();
Adithya Srinivasanac977e62021-05-21 22:50:56 +00001587 layer->setGameModeForTree(0);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001588 layer->updateTreeHasFrameRateVote();
1589
1590 return removeResult;
1591}
1592
Robert Carr15eae092018-03-23 13:43:53 -07001593void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001594 for (const sp<Layer>& child : mDrawingChildren) {
1595 child->mDrawingParent = newParent;
Vishnu Nair6bdec7d2021-05-10 15:01:13 -07001596 child->computeBounds(newParent->mBounds, newParent->mEffectiveTransform,
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001597 newParent->mEffectiveShadowRadius);
Robert Carr578038f2018-03-09 12:25:24 -08001598 }
1599}
1600
chaviwf1961f72017-09-18 16:41:07 -07001601bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr54cf5b12019-01-25 14:02:28 -08001602 sp<Layer> newParent;
1603 if (newParentHandle != nullptr) {
Vishnu Nairf9096652021-07-20 18:49:42 -07001604 newParent = fromHandle(newParentHandle).promote();
Robert Carr54cf5b12019-01-25 14:02:28 -08001605 if (newParent == nullptr) {
1606 ALOGE("Unable to promote Layer handle");
1607 return false;
1608 }
1609 if (newParent == this) {
1610 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1611 return false;
1612 }
1613 }
1614
chaviwf1961f72017-09-18 16:41:07 -07001615 sp<Layer> parent = getParent();
1616 if (parent != nullptr) {
1617 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001618 }
1619
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001620 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001621 newParent->addChild(this);
1622 if (!newParent->isRemovedFromCurrentState()) {
1623 addToCurrentState();
1624 } else {
1625 onRemovedFromCurrentState();
1626 }
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001627 } else {
1628 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001629 }
1630
chaviw06178942017-07-27 10:25:59 -07001631 return true;
1632}
1633
Peiyong Lind3788632018-09-18 16:01:31 -07001634bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001635 static const mat4 identityMatrix = mat4();
1636
Robert Carr6a160312021-05-17 12:08:20 -07001637 if (mDrawingState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001638 return false;
1639 }
Robert Carr6a160312021-05-17 12:08:20 -07001640 ++mDrawingState.sequence;
1641 mDrawingState.colorTransform = matrix;
1642 mDrawingState.hasColorTransform = matrix != identityMatrix;
1643 mDrawingState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001644 setTransactionFlags(eTransactionNeeded);
1645 return true;
1646}
1647
chaviwf66724d2018-11-28 16:35:21 -08001648mat4 Layer::getColorTransform() const {
1649 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1650 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1651 colorTransform = parent->getColorTransform() * colorTransform;
1652 }
1653 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001654}
1655
1656bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001657 bool hasColorTransform = getDrawingState().hasColorTransform;
1658 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1659 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1660 }
1661 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001662}
1663
Chia-I Wu11481472018-05-04 10:43:19 -07001664bool Layer::isLegacyDataSpace() const {
1665 // return true when no higher bits are set
chaviw4244e032019-09-04 11:27:49 -07001666 return !(getDataSpace() &
1667 (ui::Dataspace::STANDARD_MASK | ui::Dataspace::TRANSFER_MASK |
1668 ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001669}
1670
Robert Carr1f0a16a2016-10-24 16:27:39 -07001671void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001672 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001673}
1674
Robert Carr6a160312021-05-17 12:08:20 -07001675int32_t Layer::getZ(LayerVector::StateSet) const {
1676 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001677}
1678
Robert Carr1c5481e2019-07-01 14:42:27 -07001679bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) const {
Robert Carr29abff82017-12-04 13:51:20 -08001680 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Robert Carr6a160312021-05-17 12:08:20 -07001681 const State& state = useDrawing ? mDrawingState : mDrawingState;
chaviwe5ac40f2019-09-24 16:36:55 -07001682 return state.isRelativeOf;
Robert Carr29abff82017-12-04 13:51:20 -08001683}
1684
David Sodman41fdfc92017-11-06 16:09:56 -08001685__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001686 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001687 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1688 "makeTraversalList received invalid stateSet");
1689 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1690 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Robert Carr6a160312021-05-17 12:08:20 -07001691 const State& state = useDrawing ? mDrawingState : mDrawingState;
Dan Stoza412903f2017-04-27 13:42:17 -07001692
Robert Carr29abff82017-12-04 13:51:20 -08001693 if (state.zOrderRelatives.size() == 0) {
1694 *outSkipRelativeZUsers = true;
1695 return children;
1696 }
1697
chaviwfd462612018-05-31 16:11:27 -07001698 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001699 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001700 sp<Layer> strongRelative = weakRelative.promote();
1701 if (strongRelative != nullptr) {
1702 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001703 }
1704 }
1705
Dan Stoza412903f2017-04-27 13:42:17 -07001706 for (const sp<Layer>& child : children) {
chaviwe5ac40f2019-09-24 16:36:55 -07001707 if (child->usingRelativeZ(stateSet)) {
Robert Carr503c7042017-09-27 15:06:08 -07001708 continue;
1709 }
Robert Carrdb66e622017-04-10 16:55:57 -07001710 traverse.add(child);
1711 }
1712
1713 return traverse;
1714}
1715
Robert Carr1f0a16a2016-10-24 16:27:39 -07001716/**
Robert Carrdb66e622017-04-10 16:55:57 -07001717 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001718 */
Dan Stoza412903f2017-04-27 13:42:17 -07001719void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001720 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1721 // produce a new list for traversing, including our relatives, and not including our children
1722 // who are relatives of another surface. In the case that there are no relative Z,
1723 // makeTraversalList returns our children directly to avoid significant overhead.
1724 // However in this case we need to take the responsibility for filtering children which
1725 // are relatives of another surface here.
1726 bool skipRelativeZUsers = false;
1727 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001728
Robert Carr1f0a16a2016-10-24 16:27:39 -07001729 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001730 for (; i < list.size(); i++) {
1731 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001732 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1733 continue;
1734 }
1735
chaviw301b1d82019-11-06 13:15:09 -08001736 if (relative->getZ(stateSet) >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001737 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001738 }
Dan Stoza412903f2017-04-27 13:42:17 -07001739 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001740 }
Robert Carr29abff82017-12-04 13:51:20 -08001741
Dan Stoza412903f2017-04-27 13:42:17 -07001742 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001743 for (; i < list.size(); i++) {
1744 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001745
Robert Carr29abff82017-12-04 13:51:20 -08001746 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1747 continue;
1748 }
Dan Stoza412903f2017-04-27 13:42:17 -07001749 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001750 }
1751}
1752
1753/**
Robert Carrdb66e622017-04-10 16:55:57 -07001754 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001755 */
Dan Stoza412903f2017-04-27 13:42:17 -07001756void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1757 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001758 // See traverseInZOrder for documentation.
1759 bool skipRelativeZUsers = false;
1760 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001761
Robert Carr1f0a16a2016-10-24 16:27:39 -07001762 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001763 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001764 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001765
1766 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1767 continue;
1768 }
1769
chaviw301b1d82019-11-06 13:15:09 -08001770 if (relative->getZ(stateSet) < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001771 break;
1772 }
Dan Stoza412903f2017-04-27 13:42:17 -07001773 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001774 }
Dan Stoza412903f2017-04-27 13:42:17 -07001775 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001776 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001777 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001778
1779 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1780 continue;
1781 }
1782
Dan Stoza412903f2017-04-27 13:42:17 -07001783 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001784 }
1785}
1786
Edgar Arriaga844fa672020-01-16 14:21:42 -08001787void Layer::traverse(LayerVector::StateSet state, const LayerVector::Visitor& visitor) {
1788 visitor(this);
1789 const LayerVector& children =
Robert Carr6a160312021-05-17 12:08:20 -07001790 state == LayerVector::StateSet::Drawing ? mDrawingChildren : mCurrentChildren;
Edgar Arriaga844fa672020-01-16 14:21:42 -08001791 for (const sp<Layer>& child : children) {
1792 child->traverse(state, visitor);
1793 }
1794}
1795
chaviw4b129c22018-04-09 16:19:43 -07001796LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1797 const std::vector<Layer*>& layersInTree) {
1798 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1799 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001800 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1801 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Robert Carr6a160312021-05-17 12:08:20 -07001802 const State& state = useDrawing ? mDrawingState : mDrawingState;
chaviw4b129c22018-04-09 16:19:43 -07001803
chaviwfd462612018-05-31 16:11:27 -07001804 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001805 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1806 sp<Layer> strongRelative = weakRelative.promote();
1807 // Only add relative layers that are also descendents of the top most parent of the tree.
1808 // If a relative layer is not a descendent, then it should be ignored.
1809 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1810 traverse.add(strongRelative);
1811 }
1812 }
1813
1814 for (const sp<Layer>& child : children) {
Robert Carr6a160312021-05-17 12:08:20 -07001815 const State& childState = useDrawing ? child->mDrawingState : child->mDrawingState;
chaviw4b129c22018-04-09 16:19:43 -07001816 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1817 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1818 // the child here since it won't be added later as a relative.
1819 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1820 childState.zOrderRelativeOf.promote().get())) {
1821 continue;
1822 }
1823 traverse.add(child);
1824 }
1825
1826 return traverse;
1827}
1828
1829void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1830 LayerVector::StateSet stateSet,
1831 const LayerVector::Visitor& visitor) {
1832 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001833
1834 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001835 for (; i < list.size(); i++) {
1836 const auto& relative = list[i];
chaviw301b1d82019-11-06 13:15:09 -08001837 if (relative->getZ(stateSet) >= 0) {
chaviwa76b2712017-09-20 12:02:26 -07001838 break;
1839 }
chaviw4b129c22018-04-09 16:19:43 -07001840 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001841 }
chaviw4b129c22018-04-09 16:19:43 -07001842
chaviwa76b2712017-09-20 12:02:26 -07001843 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001844 for (; i < list.size(); i++) {
1845 const auto& relative = list[i];
1846 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001847 }
1848}
1849
chaviw4b129c22018-04-09 16:19:43 -07001850std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1851 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1852 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1853
1854 std::vector<Layer*> layersInTree = {this};
1855 for (size_t i = 0; i < children.size(); i++) {
1856 const auto& child = children[i];
1857 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1858 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1859 }
1860
1861 return layersInTree;
1862}
1863
1864void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1865 const LayerVector::Visitor& visitor) {
1866 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1867 std::sort(layersInTree.begin(), layersInTree.end());
1868 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1869}
1870
Peiyong Linefefaac2018-08-17 12:27:51 -07001871ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001872 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001873}
1874
chaviw13fdc492017-06-27 12:40:18 -07001875half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001876 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001877
chaviw13fdc492017-06-27 12:40:18 -07001878 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1879 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001880}
Robert Carr6452f122017-03-21 10:41:29 -07001881
Vishnu Nair6213bd92020-05-08 17:42:25 -07001882ui::Transform::RotationFlags Layer::getFixedTransformHint() const {
Robert Carr6a160312021-05-17 12:08:20 -07001883 ui::Transform::RotationFlags fixedTransformHint = mDrawingState.fixedTransformHint;
Vishnu Nair6213bd92020-05-08 17:42:25 -07001884 if (fixedTransformHint != ui::Transform::ROT_INVALID) {
1885 return fixedTransformHint;
1886 }
1887 const auto& p = mCurrentParent.promote();
1888 if (!p) return fixedTransformHint;
1889 return p->getFixedTransformHint();
1890}
1891
chaviw13fdc492017-06-27 12:40:18 -07001892half4 Layer::getColor() const {
1893 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001894 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001895}
Robert Carr6452f122017-03-21 10:41:29 -07001896
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001897int32_t Layer::getBackgroundBlurRadius() const {
Galia Peychevada7de0e2020-12-03 17:24:35 +01001898 const auto& p = mDrawingParent.promote();
1899
1900 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1901 return parentAlpha * getDrawingState().backgroundBlurRadius;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001902}
1903
Galia Peychevae0acf382021-04-12 21:22:34 +02001904const std::vector<BlurRegion> Layer::getBlurRegions() const {
1905 auto regionsCopy(getDrawingState().blurRegions);
Galia Peycheva3c286542021-06-17 15:21:28 +02001906 float layerAlpha = getAlpha();
Galia Peychevae0acf382021-04-12 21:22:34 +02001907 for (auto& region : regionsCopy) {
1908 region.alpha = region.alpha * layerAlpha;
1909 }
1910 return regionsCopy;
Lucas Dupinc3800b82020-10-02 16:24:48 -07001911}
1912
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001913Layer::RoundedCornerState Layer::getRoundedCornerState() const {
1914 const auto& p = mDrawingParent.promote();
1915 if (p != nullptr) {
Peiyong Lin27016a92019-03-29 17:36:08 +00001916 RoundedCornerState parentState = p->getRoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001917 if (parentState.radius > 0) {
1918 ui::Transform t = getActiveTransform(getDrawingState());
1919 t = t.inverse();
1920 parentState.cropRect = t.transform(parentState.cropRect);
1921 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1922 // but a transform matrix can define horizontal and vertical scales.
1923 // Let's take the average between both of them and pass into the shader, practically we
1924 // never do this type of transformation on windows anyway.
Lucas Dupineab7ea02020-06-03 17:27:28 -07001925 auto scaleX = sqrtf(t[0][0] * t[0][0] + t[0][1] * t[0][1]);
1926 auto scaleY = sqrtf(t[1][0] * t[1][0] + t[1][1] * t[1][1]);
1927 parentState.radius *= (scaleX + scaleY) / 2.0f;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001928 return parentState;
1929 }
1930 }
1931 const float radius = getDrawingState().cornerRadius;
Chavi Weingartena5aedbd2021-04-09 13:37:33 +00001932 return radius > 0 && getCroppedBufferSize(getDrawingState()).isValid()
1933 ? RoundedCornerState(getCroppedBufferSize(getDrawingState()).toFloatRect(), radius)
Peiyong Linb9ff23e2019-04-08 11:04:59 -07001934 : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001935}
1936
Derek Sollenbergerc31985e2021-05-18 16:38:17 -04001937void Layer::prepareShadowClientComposition(LayerFE::LayerSettings& caster,
1938 const Rect& layerStackRect) {
Vishnu Nair08f6eae2019-11-26 14:01:39 -08001939 renderengine::ShadowSettings state = mFlinger->mDrawingState.globalShadowSettings;
1940
Derek Sollenbergerc31985e2021-05-18 16:38:17 -04001941 // Note: this preserves existing behavior of shadowing the entire layer and not cropping it if
1942 // transparent regions are present. This may not be necessary since shadows are only cast by
1943 // SurfaceFlinger's EffectLayers, which do not typically use transparent regions.
1944 state.boundaries = mBounds;
1945
Vishnu Nair08f6eae2019-11-26 14:01:39 -08001946 // Shift the spot light x-position to the middle of the display and then
1947 // offset it by casting layer's screen pos.
Marin Shalamanov6ad317c2020-07-29 23:34:07 +02001948 state.lightPos.x = (layerStackRect.width() / 2.f) - mScreenBounds.left;
Vishnu Nair08f6eae2019-11-26 14:01:39 -08001949 state.lightPos.y -= mScreenBounds.top;
1950
1951 state.length = mEffectiveShadowRadius;
Derek Sollenbergerc31985e2021-05-18 16:38:17 -04001952
1953 if (state.length > 0.f) {
1954 const float casterAlpha = caster.alpha;
1955 const bool casterIsOpaque =
1956 ((caster.source.buffer.buffer != nullptr) && caster.source.buffer.isOpaque);
1957
1958 // If the casting layer is translucent, we need to fill in the shadow underneath the layer.
1959 // Otherwise the generated shadow will only be shown around the casting layer.
1960 state.casterIsTranslucent = !casterIsOpaque || (casterAlpha < 1.0f);
1961 state.ambientColor *= casterAlpha;
1962 state.spotColor *= casterAlpha;
1963
1964 if (state.ambientColor.a > 0.f && state.spotColor.a > 0.f) {
1965 caster.shadow = state;
1966 }
1967 }
Vishnu Nair08f6eae2019-11-26 14:01:39 -08001968}
1969
Robert Carr1f0a16a2016-10-24 16:27:39 -07001970void Layer::commitChildList() {
1971 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1972 const auto& child = mCurrentChildren[i];
1973 child->commitChildList();
1974 }
1975 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001976 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001977}
1978
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001979
chaviw3277faf2021-05-19 16:45:23 -05001980void Layer::setInputInfo(const WindowInfo& info) {
Robert Carr6a160312021-05-17 12:08:20 -07001981 mDrawingState.inputInfo = info;
Vishnu Nairf9096652021-07-20 18:49:42 -07001982 mDrawingState.touchableRegionCrop = fromHandle(info.touchableRegionCropHandle.promote());
Robert Carr6a160312021-05-17 12:08:20 -07001983 mDrawingState.modified = true;
Robert Carrbf14dbb2021-06-11 13:26:56 -07001984 mFlinger->mInputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07001985 setTransactionFlags(eTransactionNeeded);
1986}
1987
Alec Mouri6b9e9912020-01-21 10:50:24 -08001988LayerProto* Layer::writeToProto(LayersProto& layersProto, uint32_t traceFlags,
chaviw4c34a092020-07-08 11:30:06 -07001989 const DisplayDevice* display) {
chaviw08f3cb22020-01-13 13:17:21 -08001990 LayerProto* layerProto = layersProto.add_layers();
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001991 writeToProtoDrawingState(layerProto, traceFlags, display);
chaviw08f3cb22020-01-13 13:17:21 -08001992 writeToProtoCommonState(layerProto, LayerVector::StateSet::Drawing, traceFlags);
1993
Vishnu Nair60db8c02020-04-02 11:55:16 -07001994 if (traceFlags & SurfaceTracing::TRACE_COMPOSITION) {
1995 // Only populate for the primary display.
Dominik Laskowskib7251f42020-04-20 17:42:59 -07001996 if (display) {
1997 const Hwc2::IComposerClient::Composition compositionType = getCompositionType(*display);
Vishnu Nair60db8c02020-04-02 11:55:16 -07001998 layerProto->set_hwc_composition_type(static_cast<HwcCompositionType>(compositionType));
1999 }
Alec Mouri6b9e9912020-01-21 10:50:24 -08002000 }
2001
chaviw08f3cb22020-01-13 13:17:21 -08002002 for (const sp<Layer>& layer : mDrawingChildren) {
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002003 layer->writeToProto(layersProto, traceFlags, display);
chaviw08f3cb22020-01-13 13:17:21 -08002004 }
chaviw6d89e2d2020-01-14 14:42:01 -08002005
2006 return layerProto;
chaviw08f3cb22020-01-13 13:17:21 -08002007}
2008
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002009void Layer::writeToProtoDrawingState(LayerProto* layerInfo, uint32_t traceFlags,
chaviw4c34a092020-07-08 11:30:06 -07002010 const DisplayDevice* display) {
Vishnu Nair6b7c5c92020-09-29 17:27:05 -07002011 const ui::Transform transform = getTransform();
Vishnu Nair8406fd72019-07-30 11:29:31 -07002012
2013 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
Vishnu Nair8406fd72019-07-30 11:29:31 -07002014
chaviwd62d3062019-09-04 14:48:02 -07002015 auto buffer = getBuffer();
Vishnu Nair8406fd72019-07-30 11:29:31 -07002016 if (buffer != nullptr) {
2017 LayerProtoHelper::writeToProto(buffer,
2018 [&]() { return layerInfo->mutable_active_buffer(); });
chaviw4244e032019-09-04 11:27:49 -07002019 LayerProtoHelper::writeToProto(ui::Transform(getBufferTransform()),
Vishnu Nair8406fd72019-07-30 11:29:31 -07002020 layerInfo->mutable_buffer_transform());
2021 }
2022 layerInfo->set_invalidate(contentDirty);
2023 layerInfo->set_is_protected(isProtected());
chaviw4244e032019-09-04 11:27:49 -07002024 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(getDataSpace())));
Vishnu Nair8406fd72019-07-30 11:29:31 -07002025 layerInfo->set_queued_frames(getQueuedFrameCount());
2026 layerInfo->set_refresh_pending(isBufferLatched());
2027 layerInfo->set_curr_frame(mCurrentFrameNumber);
2028 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
2029
Pablo Gamitoabdf4da2021-08-10 09:55:42 +00002030 layerInfo->set_requested_corner_radius(getDrawingState().cornerRadius);
Vishnu Nair8406fd72019-07-30 11:29:31 -07002031 layerInfo->set_corner_radius(getRoundedCornerState().radius);
Ana Krulece766cc82020-04-27 13:49:13 -07002032 layerInfo->set_background_blur_radius(getBackgroundBlurRadius());
Winson Chunga30f7c92021-06-29 15:42:56 -07002033 layerInfo->set_is_trusted_overlay(isTrustedOverlay());
Vishnu Nair8406fd72019-07-30 11:29:31 -07002034 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
2035 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
2036 [&]() { return layerInfo->mutable_position(); });
2037 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
Vishnu Nair60db8c02020-04-02 11:55:16 -07002038 if (traceFlags & SurfaceTracing::TRACE_COMPOSITION) {
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002039 LayerProtoHelper::writeToProto(getVisibleRegion(display),
Vishnu Nair60db8c02020-04-02 11:55:16 -07002040 [&]() { return layerInfo->mutable_visible_region(); });
2041 }
Vishnu Nair8406fd72019-07-30 11:29:31 -07002042 LayerProtoHelper::writeToProto(surfaceDamageRegion,
2043 [&]() { return layerInfo->mutable_damage_region(); });
chaviwddeae262020-01-06 10:31:23 -08002044
2045 if (hasColorTransform()) {
2046 LayerProtoHelper::writeToProto(getColorTransform(),
2047 layerInfo->mutable_color_transform());
2048 }
Vishnu Nair8406fd72019-07-30 11:29:31 -07002049 }
2050
Vishnu Nair60db8c02020-04-02 11:55:16 -07002051 LayerProtoHelper::writeToProto(mSourceBounds,
2052 [&]() { return layerInfo->mutable_source_bounds(); });
2053 LayerProtoHelper::writeToProto(mScreenBounds,
2054 [&]() { return layerInfo->mutable_screen_bounds(); });
2055 LayerProtoHelper::writeToProto(getRoundedCornerState().cropRect,
2056 [&]() { return layerInfo->mutable_corner_radius_crop(); });
2057 layerInfo->set_shadow_radius(mEffectiveShadowRadius);
Vishnu Nair8406fd72019-07-30 11:29:31 -07002058}
2059
2060void Layer::writeToProtoCommonState(LayerProto* layerInfo, LayerVector::StateSet stateSet,
chaviw4c34a092020-07-08 11:30:06 -07002061 uint32_t traceFlags) {
chaviw1d044282017-09-27 12:19:28 -07002062 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2063 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Robert Carr6a160312021-05-17 12:08:20 -07002064 const State& state = useDrawing ? mDrawingState : mDrawingState;
chaviw1d044282017-09-27 12:19:28 -07002065
chaviw766c9c52021-02-10 17:36:47 -08002066 ui::Transform requestedTransform = state.transform;
chaviw1d044282017-09-27 12:19:28 -07002067
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002068 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
2069 layerInfo->set_id(sequence);
2070 layerInfo->set_name(getName().c_str());
chaviw8a01fa42019-08-19 12:39:31 -07002071 layerInfo->set_type(getType());
chaviw1d044282017-09-27 12:19:28 -07002072
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002073 for (const auto& child : children) {
2074 layerInfo->add_children(child->sequence);
chaviw1d044282017-09-27 12:19:28 -07002075 }
chaviw1d044282017-09-27 12:19:28 -07002076
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002077 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
2078 sp<Layer> strongRelative = weakRelative.promote();
2079 if (strongRelative != nullptr) {
2080 layerInfo->add_relatives(strongRelative->sequence);
2081 }
chaviwadc40c22018-07-10 16:57:27 -07002082 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002083
2084 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
2085 [&]() { return layerInfo->mutable_transparent_region(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002086
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002087 layerInfo->set_layer_stack(getLayerStack().id);
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002088 layerInfo->set_z(state.z);
2089
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002090 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(),
2091 [&]() {
2092 return layerInfo->mutable_requested_position();
2093 });
2094
chaviw766c9c52021-02-10 17:36:47 -08002095 LayerProtoHelper::writeSizeToProto(state.width, state.height,
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002096 [&]() { return layerInfo->mutable_size(); });
2097
chaviw766c9c52021-02-10 17:36:47 -08002098 LayerProtoHelper::writeToProto(state.crop, [&]() { return layerInfo->mutable_crop(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002099
2100 layerInfo->set_is_opaque(isOpaque(state));
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002101
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002102
2103 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
2104 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
2105 LayerProtoHelper::writeToProto(state.color,
2106 [&]() { return layerInfo->mutable_requested_color(); });
2107 layerInfo->set_flags(state.flags);
2108
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002109 LayerProtoHelper::writeToProto(requestedTransform,
2110 layerInfo->mutable_requested_transform());
2111
2112 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
2113 if (parent != nullptr) {
2114 layerInfo->set_parent(parent->sequence);
2115 } else {
2116 layerInfo->set_parent(-1);
2117 }
2118
2119 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2120 if (zOrderRelativeOf != nullptr) {
2121 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
2122 } else {
2123 layerInfo->set_z_order_relative_of(-1);
2124 }
chaviw08f3cb22020-01-13 13:17:21 -08002125
2126 layerInfo->set_is_relative_of(state.isRelativeOf);
chaviw250bcbb2020-08-05 11:17:54 -07002127
2128 layerInfo->set_owner_uid(mOwnerUid);
chaviwadc40c22018-07-10 16:57:27 -07002129 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002130
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002131 if (traceFlags & SurfaceTracing::TRACE_INPUT) {
chaviw3277faf2021-05-19 16:45:23 -05002132 WindowInfo info;
chaviw4c34a092020-07-08 11:30:06 -07002133 if (useDrawing) {
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002134 info = fillInputInfo(nullptr);
chaviw4c34a092020-07-08 11:30:06 -07002135 } else {
2136 info = state.inputInfo;
2137 }
2138
2139 LayerProtoHelper::writeToProto(info, state.touchableRegionCrop,
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002140 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002141 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002142
2143 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
2144 auto protoMap = layerInfo->mutable_metadata();
2145 for (const auto& entry : state.metadata.mMap) {
2146 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
2147 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002148 }
chaviw1d044282017-09-27 12:19:28 -07002149}
2150
Robert Carr2e102c92018-10-23 12:11:15 -07002151bool Layer::isRemovedFromCurrentState() const {
Robert Carr6a160312021-05-17 12:08:20 -07002152 return mRemovedFromDrawingState;
Robert Carr2e102c92018-10-23 12:11:15 -07002153}
2154
chaviw39d01472021-04-08 14:26:24 -05002155ui::Transform Layer::getInputTransform() const {
2156 return getTransform();
2157}
2158
2159Rect Layer::getInputBounds() const {
2160 return getCroppedBufferSize(getDrawingState());
2161}
2162
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002163void Layer::fillInputFrameInfo(WindowInfo& info, const ui::Transform& displayTransform) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002164 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002165 // If this is a portal window, set the touchableRegion to the layerBounds.
2166 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
chaviw39d01472021-04-08 14:26:24 -05002167 ? getInputBounds()
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002168 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002169 if (!layerBounds.isValid()) {
chaviw39d01472021-04-08 14:26:24 -05002170 layerBounds = getInputBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002171 }
chaviw1ff3d1e2020-07-01 15:53:47 -07002172
chaviw7e72caf2020-12-02 16:50:43 -08002173 if (!layerBounds.isValid()) {
2174 // If the layer bounds is empty, set the frame to empty and clear the transform
2175 info.frameLeft = 0;
2176 info.frameTop = 0;
2177 info.frameRight = 0;
2178 info.frameBottom = 0;
2179 info.transform.reset();
2180 return;
2181 }
2182
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002183 const ui::Transform layerTransform = getInputTransform();
[1;3C2b9fc252021-02-04 16:16:50 -08002184 // Transform that takes window coordinates to unrotated display coordinates
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002185 ui::Transform t = displayTransform * layerTransform;
chaviw7e72caf2020-12-02 16:50:43 -08002186 int32_t xSurfaceInset = info.surfaceInset;
2187 int32_t ySurfaceInset = info.surfaceInset;
[1;3C2b9fc252021-02-04 16:16:50 -08002188 // Bring screenBounds into unrotated space
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002189 Rect screenBounds = displayTransform.transform(Rect{mScreenBounds});
chaviw7e72caf2020-12-02 16:50:43 -08002190
chaviw1ff3d1e2020-07-01 15:53:47 -07002191 const float xScale = t.getScaleX();
2192 const float yScale = t.getScaleY();
2193 if (xScale != 1.0f || yScale != 1.0f) {
chaviw1ff3d1e2020-07-01 15:53:47 -07002194 xSurfaceInset = std::round(xSurfaceInset * xScale);
2195 ySurfaceInset = std::round(ySurfaceInset * yScale);
2196 }
2197
Peiyong Lin74861ad2020-10-14 11:55:33 -07002198 // Transform the layer bounds from layer coordinate space to display coordinate space.
chaviw44a6d2b2020-09-08 17:14:16 -07002199 Rect transformedLayerBounds = t.transform(layerBounds);
Ady Abraham282f1d72019-07-24 18:05:56 -07002200
2201 // clamp inset to layer bounds
chaviw44a6d2b2020-09-08 17:14:16 -07002202 xSurfaceInset = (xSurfaceInset >= 0)
2203 ? std::min(xSurfaceInset, transformedLayerBounds.getWidth() / 2)
2204 : 0;
2205 ySurfaceInset = (ySurfaceInset >= 0)
2206 ? std::min(ySurfaceInset, transformedLayerBounds.getHeight() / 2)
2207 : 0;
Ady Abraham282f1d72019-07-24 18:05:56 -07002208
Robert Carra7242302020-09-01 18:26:29 -07002209 // inset while protecting from overflow TODO(b/161235021): What is going wrong
2210 // in the overflow scenario?
2211 {
2212 int32_t tmp;
chaviw44a6d2b2020-09-08 17:14:16 -07002213 if (!__builtin_add_overflow(transformedLayerBounds.left, xSurfaceInset, &tmp))
2214 transformedLayerBounds.left = tmp;
2215 if (!__builtin_sub_overflow(transformedLayerBounds.right, xSurfaceInset, &tmp))
2216 transformedLayerBounds.right = tmp;
2217 if (!__builtin_add_overflow(transformedLayerBounds.top, ySurfaceInset, &tmp))
2218 transformedLayerBounds.top = tmp;
2219 if (!__builtin_sub_overflow(transformedLayerBounds.bottom, ySurfaceInset, &tmp))
2220 transformedLayerBounds.bottom = tmp;
Robert Carra7242302020-09-01 18:26:29 -07002221 }
Vishnu Nair8033a492018-12-05 07:27:23 -08002222
chaviw44a6d2b2020-09-08 17:14:16 -07002223 // Compute the correct transform to send to input. This will allow it to transform the
2224 // input coordinates from display space into window space. Therefore, it needs to use the
2225 // final layer frame to create the inverse transform. Since surface insets are added later,
2226 // along with the overflow, the best way to ensure we get the correct transform is to use
2227 // the final frame calculated.
2228 // 1. Take the original transform set on the window and get the inverse transform. This is
2229 // used to get the final bounds in display space (ignorning the transform). Apply the
Peiyong Lin74861ad2020-10-14 11:55:33 -07002230 // inverse transform on the layerBounds to get the untransformed frame (in layer space)
chaviw44a6d2b2020-09-08 17:14:16 -07002231 // 2. Take the top and left of the untransformed frame to get the real position on screen.
2232 // Apply the layer transform on top/left so it includes any scale or rotation. These will
2233 // be the new translation values for the transform.
2234 // 3. Update the translation of the original transform to the new translation values.
2235 // 4. Send the inverse transform to input so the coordinates can be transformed back into
2236 // window space.
2237 ui::Transform inverseTransform = t.inverse();
2238 Rect nonTransformedBounds = inverseTransform.transform(transformedLayerBounds);
2239 vec2 translation = t.transform(nonTransformedBounds.left, nonTransformedBounds.top);
chaviw1ff3d1e2020-07-01 15:53:47 -07002240 ui::Transform inputTransform(t);
chaviw44a6d2b2020-09-08 17:14:16 -07002241 inputTransform.set(translation.x, translation.y);
chaviw1ff3d1e2020-07-01 15:53:47 -07002242 info.transform = inputTransform.inverse();
2243
chaviw39cfa2e2020-11-04 14:19:02 -08002244 // We need to send the layer bounds cropped to the screenbounds since the layer can be cropped.
2245 // The frame should be the area the user sees on screen since it's used for occlusion
2246 // detection.
chaviw39cfa2e2020-11-04 14:19:02 -08002247 transformedLayerBounds.intersect(screenBounds, &transformedLayerBounds);
2248 info.frameLeft = transformedLayerBounds.left;
2249 info.frameTop = transformedLayerBounds.top;
2250 info.frameRight = transformedLayerBounds.right;
2251 info.frameBottom = transformedLayerBounds.bottom;
2252
Vishnu Nair8033a492018-12-05 07:27:23 -08002253 // Position the touchable region relative to frame screen location and restrict it to frame
2254 // bounds.
chaviw44a6d2b2020-09-08 17:14:16 -07002255 info.touchableRegion = inputTransform.transform(info.touchableRegion);
chaviw7e72caf2020-12-02 16:50:43 -08002256}
2257
chaviw3277faf2021-05-19 16:45:23 -05002258void Layer::fillTouchOcclusionMode(WindowInfo& info) {
Bernardo Rufinoa9d0a532021-06-11 15:59:12 +01002259 sp<Layer> p = this;
2260 while (p != nullptr && !p->hasInputInfo()) {
2261 p = p->mDrawingParent.promote();
2262 }
2263 if (p != nullptr) {
2264 info.touchOcclusionMode = p->mDrawingState.inputInfo.touchOcclusionMode;
2265 }
2266}
2267
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002268WindowInfo Layer::fillInputInfo(const DisplayDevice* display) {
chaviw7e72caf2020-12-02 16:50:43 -08002269 if (!hasInputInfo()) {
2270 mDrawingState.inputInfo.name = getName();
2271 mDrawingState.inputInfo.ownerUid = mOwnerUid;
2272 mDrawingState.inputInfo.ownerPid = mOwnerPid;
chaviw3277faf2021-05-19 16:45:23 -05002273 mDrawingState.inputInfo.inputFeatures = WindowInfo::Feature::NO_INPUT_CHANNEL;
2274 mDrawingState.inputInfo.flags = WindowInfo::Flag::NOT_TOUCH_MODAL;
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002275 mDrawingState.inputInfo.displayId = getLayerStack().id;
chaviw7e72caf2020-12-02 16:50:43 -08002276 }
2277
chaviw3277faf2021-05-19 16:45:23 -05002278 WindowInfo info = mDrawingState.inputInfo;
chaviw7e72caf2020-12-02 16:50:43 -08002279 info.id = sequence;
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002280 info.displayId = getLayerStack().id;
chaviw7e72caf2020-12-02 16:50:43 -08002281
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002282 // Transform that maps from LayerStack space to display space, e.g. rotated to unrotated.
2283 // Used when InputFlinger operates in display space.
2284 ui::Transform displayTransform;
[1;3C2b9fc252021-02-04 16:16:50 -08002285 if (display) {
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002286 displayTransform = display->getTransform();
Evan Rosky09576692021-07-01 12:22:09 -07002287 // getOrientation() without masking can contain more-significant bits (eg. ROT_INVALID).
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002288 constexpr uint32_t kAllRotationsMask =
Evan Rosky09576692021-07-01 12:22:09 -07002289 ui::Transform::ROT_90 | ui::Transform::ROT_180 | ui::Transform::ROT_270;
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002290 info.displayOrientation = displayTransform.getOrientation() & kAllRotationsMask;
Evan Rosky84f07f02021-04-16 10:42:42 -07002291 info.displayWidth = display->getWidth();
2292 info.displayHeight = display->getHeight();
[1;3C2b9fc252021-02-04 16:16:50 -08002293 }
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002294 fillInputFrameInfo(info, displayTransform);
chaviw7e72caf2020-12-02 16:50:43 -08002295
Robert Carr5dc426e2020-06-10 14:29:14 -07002296 // For compatibility reasons we let layers which can receive input
2297 // receive input before they have actually submitted a buffer. Because
2298 // of this we use canReceiveInput instead of isVisible to check the
2299 // policy-visibility, ignoring the buffer state. However for layers with
2300 // hasInputInfo()==false we can use the real visibility state.
2301 // We are just using these layers for occlusion detection in
2302 // InputDispatcher, and obviously if they aren't visible they can't occlude
2303 // anything.
2304 info.visible = hasInputInfo() ? canReceiveInput() : isVisible();
Bernardo Rufinoea97d182020-08-19 14:43:14 +01002305 info.alpha = getAlpha();
Bernardo Rufinoa9d0a532021-06-11 15:59:12 +01002306 fillTouchOcclusionMode(info);
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002307
2308 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2309 if (info.replaceTouchableRegionWithCrop) {
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002310 const Rect bounds(cropLayer ? cropLayer->mScreenBounds : mScreenBounds);
2311 info.touchableRegion = Region(displayTransform.transform(bounds));
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002312 } else if (cropLayer != nullptr) {
[1;3C2b9fc252021-02-04 16:16:50 -08002313 info.touchableRegion = info.touchableRegion.intersect(
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002314 displayTransform.transform(Rect{cropLayer->mScreenBounds}));
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002315 }
2316
Winson Chunga30f7c92021-06-29 15:42:56 -07002317 // Inherit the trusted state from the parent hierarchy, but don't clobber the trusted state
2318 // if it was set by WM for a known system overlay
2319 info.trustedOverlay = info.trustedOverlay || isTrustedOverlay();
2320
2321
chaviwaf87b3e2019-10-01 16:59:28 -07002322 // If the layer is a clone, we need to crop the input region to cloned root to prevent
2323 // touches from going outside the cloned area.
2324 if (isClone()) {
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002325 if (const sp<Layer> clonedRoot = getClonedRoot()) {
2326 const Rect rect = displayTransform.transform(Rect{clonedRoot->mScreenBounds});
chaviwaf87b3e2019-10-01 16:59:28 -07002327 info.touchableRegion = info.touchableRegion.intersect(rect);
2328 }
2329 }
2330
Robert Carr720e5062018-07-30 17:45:14 -07002331 return info;
2332}
2333
chaviwaf87b3e2019-10-01 16:59:28 -07002334sp<Layer> Layer::getClonedRoot() {
2335 if (mClonedChild != nullptr) {
2336 return this;
2337 }
2338 if (mDrawingParent == nullptr || mDrawingParent.promote() == nullptr) {
2339 return nullptr;
2340 }
2341 return mDrawingParent.promote()->getClonedRoot();
2342}
2343
Robert Carredd13602020-04-13 17:24:34 -07002344bool Layer::hasInputInfo() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002345 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002346}
2347
chaviw3e727cd2019-01-31 13:41:05 -08002348bool Layer::canReceiveInput() const {
Vishnu Nair82353e92019-09-12 12:38:47 -07002349 return !isHiddenByPolicy();
chaviw3e727cd2019-01-31 13:41:05 -08002350}
2351
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002352compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002353 const DisplayDevice* display) const {
2354 if (!display) return nullptr;
Lloyd Piquede196652020-01-22 17:29:58 -08002355 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionEngineLayerFE());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002356}
2357
Dominik Laskowskib7251f42020-04-20 17:42:59 -07002358Region Layer::getVisibleRegion(const DisplayDevice* display) const {
2359 const auto outputLayer = findOutputLayerForDisplay(display);
2360 return outputLayer ? outputLayer->getState().visibleRegion : Region();
Lloyd Piquea2468662019-03-07 21:31:06 -08002361}
2362
chaviwb4c6e582019-08-16 14:35:07 -07002363void Layer::setInitialValuesForClone(const sp<Layer>& clonedFrom) {
2364 // copy drawing state from cloned layer
2365 mDrawingState = clonedFrom->mDrawingState;
2366 mClonedFrom = clonedFrom;
chaviwb4c6e582019-08-16 14:35:07 -07002367}
chaviw74b03172019-08-19 11:09:03 -07002368
2369void Layer::updateMirrorInfo() {
2370 if (mClonedChild == nullptr || !mClonedChild->isClonedFromAlive()) {
2371 // If mClonedChild is null, there is nothing to mirror. If isClonedFromAlive returns false,
2372 // it means that there is a clone, but the layer it was cloned from has been destroyed. In
2373 // that case, we want to delete the reference to the clone since we want it to get
2374 // destroyed. The root, this layer, will still be around since the client can continue
2375 // to hold a reference, but no cloned layers will be displayed.
2376 mClonedChild = nullptr;
2377 return;
2378 }
2379
2380 std::map<sp<Layer>, sp<Layer>> clonedLayersMap;
2381 // If the real layer exists and is in current state, add the clone as a child of the root.
2382 // There's no need to remove from drawingState when the layer is offscreen since currentState is
2383 // copied to drawingState for the root layer. So the clonedChild is always removed from
2384 // drawingState and then needs to be added back each traversal.
2385 if (!mClonedChild->getClonedFrom()->isRemovedFromCurrentState()) {
2386 addChildToDrawing(mClonedChild);
2387 }
2388
2389 mClonedChild->updateClonedDrawingState(clonedLayersMap);
2390 mClonedChild->updateClonedChildren(this, clonedLayersMap);
2391 mClonedChild->updateClonedRelatives(clonedLayersMap);
2392}
2393
2394void Layer::updateClonedDrawingState(std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2395 // If the layer the clone was cloned from is alive, copy the content of the drawingState
2396 // to the clone. If the real layer is no longer alive, continue traversing the children
2397 // since we may be able to pull out other children that are still alive.
2398 if (isClonedFromAlive()) {
2399 sp<Layer> clonedFrom = getClonedFrom();
2400 mDrawingState = clonedFrom->mDrawingState;
chaviw74b03172019-08-19 11:09:03 -07002401 clonedLayersMap.emplace(clonedFrom, this);
2402 }
2403
2404 // The clone layer may have children in drawingState since they may have been created and
2405 // added from a previous request to updateMirorInfo. This is to ensure we don't recreate clones
2406 // that already exist, since we can just re-use them.
2407 // The drawingChildren will not get overwritten by the currentChildren since the clones are
2408 // not updated in the regular traversal. They are skipped since the root will lose the
2409 // reference to them when it copies its currentChildren to drawing.
2410 for (sp<Layer>& child : mDrawingChildren) {
2411 child->updateClonedDrawingState(clonedLayersMap);
2412 }
2413}
2414
2415void Layer::updateClonedChildren(const sp<Layer>& mirrorRoot,
2416 std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2417 mDrawingChildren.clear();
2418
2419 if (!isClonedFromAlive()) {
2420 return;
2421 }
2422
2423 sp<Layer> clonedFrom = getClonedFrom();
2424 for (sp<Layer>& child : clonedFrom->mDrawingChildren) {
2425 if (child == mirrorRoot) {
2426 // This is to avoid cyclical mirroring.
2427 continue;
2428 }
2429 sp<Layer> clonedChild = clonedLayersMap[child];
2430 if (clonedChild == nullptr) {
2431 clonedChild = child->createClone();
2432 clonedLayersMap[child] = clonedChild;
2433 }
2434 addChildToDrawing(clonedChild);
2435 clonedChild->updateClonedChildren(mirrorRoot, clonedLayersMap);
2436 }
2437}
2438
chaviwaf87b3e2019-10-01 16:59:28 -07002439void Layer::updateClonedInputInfo(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2440 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2441 if (cropLayer != nullptr) {
2442 if (clonedLayersMap.count(cropLayer) == 0) {
2443 // Real layer had a crop layer but it's not in the cloned hierarchy. Just set to
2444 // self as crop layer to avoid going outside bounds.
2445 mDrawingState.touchableRegionCrop = this;
2446 } else {
2447 const sp<Layer>& clonedCropLayer = clonedLayersMap.at(cropLayer);
2448 mDrawingState.touchableRegionCrop = clonedCropLayer;
2449 }
2450 }
2451 // Cloned layers shouldn't handle watch outside since their z order is not determined by
2452 // WM or the client.
chaviw3277faf2021-05-19 16:45:23 -05002453 mDrawingState.inputInfo.flags &= ~WindowInfo::Flag::WATCH_OUTSIDE_TOUCH;
chaviwaf87b3e2019-10-01 16:59:28 -07002454}
2455
2456void Layer::updateClonedRelatives(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
chaviw74b03172019-08-19 11:09:03 -07002457 mDrawingState.zOrderRelativeOf = nullptr;
2458 mDrawingState.zOrderRelatives.clear();
2459
2460 if (!isClonedFromAlive()) {
2461 return;
2462 }
2463
chaviwaf87b3e2019-10-01 16:59:28 -07002464 const sp<Layer>& clonedFrom = getClonedFrom();
chaviw74b03172019-08-19 11:09:03 -07002465 for (wp<Layer>& relativeWeak : clonedFrom->mDrawingState.zOrderRelatives) {
chaviwaf87b3e2019-10-01 16:59:28 -07002466 const sp<Layer>& relative = relativeWeak.promote();
2467 if (clonedLayersMap.count(relative) > 0) {
2468 auto& clonedRelative = clonedLayersMap.at(relative);
chaviw74b03172019-08-19 11:09:03 -07002469 mDrawingState.zOrderRelatives.add(clonedRelative);
2470 }
2471 }
2472
2473 // Check if the relativeLayer for the real layer is part of the cloned hierarchy.
2474 // It's possible that the layer it's relative to is outside the requested cloned hierarchy.
2475 // In that case, we treat the layer as if the relativeOf has been removed. This way, it will
2476 // still traverse the children, but the layer with the missing relativeOf will not be shown
2477 // on screen.
chaviwaf87b3e2019-10-01 16:59:28 -07002478 const sp<Layer>& relativeOf = clonedFrom->mDrawingState.zOrderRelativeOf.promote();
2479 if (clonedLayersMap.count(relativeOf) > 0) {
2480 const sp<Layer>& clonedRelativeOf = clonedLayersMap.at(relativeOf);
chaviw74b03172019-08-19 11:09:03 -07002481 mDrawingState.zOrderRelativeOf = clonedRelativeOf;
2482 }
2483
chaviwaf87b3e2019-10-01 16:59:28 -07002484 updateClonedInputInfo(clonedLayersMap);
2485
chaviw74b03172019-08-19 11:09:03 -07002486 for (sp<Layer>& child : mDrawingChildren) {
2487 child->updateClonedRelatives(clonedLayersMap);
2488 }
2489}
2490
2491void Layer::addChildToDrawing(const sp<Layer>& layer) {
2492 mDrawingChildren.add(layer);
2493 layer->mDrawingParent = this;
2494}
2495
Steven Thomas62a4cf82020-01-31 12:04:03 -08002496Layer::FrameRateCompatibility Layer::FrameRate::convertCompatibility(int8_t compatibility) {
2497 switch (compatibility) {
2498 case ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT:
2499 return FrameRateCompatibility::Default;
2500 case ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE:
2501 return FrameRateCompatibility::ExactOrMultiple;
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08002502 case ANATIVEWINDOW_FRAME_RATE_EXACT:
2503 return FrameRateCompatibility::Exact;
Steven Thomas62a4cf82020-01-31 12:04:03 -08002504 default:
2505 LOG_ALWAYS_FATAL("Invalid frame rate compatibility value %d", compatibility);
2506 return FrameRateCompatibility::Default;
2507 }
2508}
2509
Marin Shalamanovc5986772021-03-16 16:09:49 +01002510scheduler::Seamlessness Layer::FrameRate::convertChangeFrameRateStrategy(int8_t strategy) {
2511 switch (strategy) {
2512 case ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS:
2513 return Seamlessness::OnlySeamless;
2514 case ANATIVEWINDOW_CHANGE_FRAME_RATE_ALWAYS:
2515 return Seamlessness::SeamedAndSeamless;
2516 default:
2517 LOG_ALWAYS_FATAL("Invalid change frame sate strategy value %d", strategy);
2518 return Seamlessness::Default;
2519 }
2520}
2521
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002522bool Layer::isInternalDisplayOverlay() const {
chaviwc5676c62020-09-18 15:01:04 -07002523 const State& s(mDrawingState);
2524 if (s.flags & layer_state_t::eLayerSkipScreenshot) {
2525 return true;
2526 }
2527
2528 sp<Layer> parent = mDrawingParent.promote();
Dominik Laskowski29fa1462021-04-27 15:51:50 -07002529 return parent && parent->isInternalDisplayOverlay();
chaviwc5676c62020-09-18 15:01:04 -07002530}
2531
Robert Carr6a0382d2021-07-01 15:57:17 -07002532void Layer::setClonedChild(const sp<Layer>& clonedChild) {
2533 mClonedChild = clonedChild;
2534 mHadClonedChild = true;
2535 mFlinger->mNumClones++;
2536}
2537
Vishnu Nairf9096652021-07-20 18:49:42 -07002538const String16 Layer::Handle::kDescriptor = String16("android.Layer.Handle");
2539
2540wp<Layer> Layer::fromHandle(const sp<IBinder>& handleBinder) {
2541 if (handleBinder == nullptr) {
2542 return nullptr;
2543 }
2544
2545 BBinder* b = handleBinder->localBinder();
2546 if (b == nullptr || b->getInterfaceDescriptor() != Handle::kDescriptor) {
2547 return nullptr;
2548 }
2549
2550 // We can safely cast this binder since its local and we verified its interface descriptor.
2551 sp<Handle> handle = static_cast<Handle*>(handleBinder.get());
2552 return handle->owner;
2553}
2554
Mathias Agopian13127d82013-03-05 17:47:11 -08002555// ---------------------------------------------------------------------------
2556
Marin Shalamanov46084422020-10-13 12:33:42 +02002557std::ostream& operator<<(std::ostream& stream, const Layer::FrameRate& rate) {
2558 return stream << "{rate=" << rate.rate
2559 << " type=" << Layer::frameRateCompatibilityString(rate.type)
Marin Shalamanov53fc11d2020-11-20 14:00:13 +01002560 << " seamlessness=" << toString(rate.seamlessness) << "}";
Marin Shalamanov46084422020-10-13 12:33:42 +02002561}
2562
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002563}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002564
2565#if defined(__gl_h_)
2566#error "don't include gl/gl.h in this file"
2567#endif
2568
2569#if defined(__gl2_h_)
2570#error "don't include gl2/gl2.h in this file"
2571#endif
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -08002572
2573// TODO(b/129481165): remove the #pragma below and fix conversion issues
2574#pragma clang diagnostic pop // ignored "-Wconversion"