blob: f9dff12d1941d6f41243803fc1c9f47af06023fc [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
Yiwei Zhang5434a782018-12-05 18:06:32 -080028#include <android-base/stringprintf.h>
Steven Thomas62a4cf82020-01-31 12:04:03 -080029#include <android/native_window.h>
Vishnu Nair0f085c62019-08-30 08:49:12 -070030#include <binder/IPCThreadState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080031#include <compositionengine/Display.h>
Lloyd Piquea83776c2019-01-29 18:42:32 -080032#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080033#include <compositionengine/OutputLayer.h>
34#include <compositionengine/impl/OutputLayerCompositionState.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070035#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070036#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070037#include <cutils/properties.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080038#include <gui/BufferItem.h>
39#include <gui/LayerDebugInfo.h>
40#include <gui/Surface.h>
Alec Mourie60041e2019-06-14 18:59:51 -070041#include <math.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080042#include <renderengine/RenderEngine.h>
Alec Mourie60041e2019-06-14 18:59:51 -070043#include <stdint.h>
44#include <stdlib.h>
45#include <sys/types.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080046#include <ui/DebugUtils.h>
47#include <ui/GraphicBuffer.h>
48#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include <utils/Errors.h>
50#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080051#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080053#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054
Alec Mourie60041e2019-06-14 18:59:51 -070055#include <algorithm>
56#include <mutex>
57#include <sstream>
58
Yiwei Zhang60d1a192018-03-07 14:52:28 -080059#include "BufferLayer.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020060#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070061#include "DisplayDevice.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080062#include "DisplayHardware/HWComposer.h"
Vishnu Nairfa247b12020-02-11 08:58:26 -080063#include "EffectLayer.h"
Mikael Pessa90092f42019-08-26 17:22:04 -070064#include "FrameTracer/FrameTracer.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080065#include "LayerProtoHelper.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070066#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070067#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068#include "SurfaceFlinger.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080069#include "TimeStats/TimeStats.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070070
David Sodman41fdfc92017-11-06 16:09:56 -080071#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073namespace android {
74
Yiwei Zhang5434a782018-12-05 18:06:32 -080075using base::StringAppendF;
76
Lloyd Piquef1c675b2018-09-12 20:45:39 -070077std::atomic<int32_t> Layer::sSequence{1};
Mathias Agopian13127d82013-03-05 17:47:11 -080078
Lloyd Pique42ab75e2018-09-12 20:46:03 -070079Layer::Layer(const LayerCreationArgs& args)
Ady Abraham8f1ee7f2019-04-05 10:32:50 -070080 : mFlinger(args.flinger),
81 mName(args.name),
82 mClientRef(args.client),
83 mWindowType(args.metadata.getInt32(METADATA_WINDOW_TYPE, 0)) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -070084 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -070085 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
86 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
87 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070088
Lloyd Pique0449b0f2018-12-20 16:23:45 -080089 mCurrentState.active_legacy.w = args.w;
90 mCurrentState.active_legacy.h = args.h;
91 mCurrentState.flags = layerFlags;
92 mCurrentState.active_legacy.transform.set(0, 0);
93 mCurrentState.crop_legacy.makeInvalid();
94 mCurrentState.requestedCrop_legacy = mCurrentState.crop_legacy;
95 mCurrentState.z = 0;
96 mCurrentState.color.a = 1.0f;
97 mCurrentState.layerStack = 0;
98 mCurrentState.sequence = 0;
99 mCurrentState.requested_legacy = mCurrentState.active_legacy;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800100 mCurrentState.active.w = UINT32_MAX;
101 mCurrentState.active.h = UINT32_MAX;
102 mCurrentState.active.transform.set(0, 0);
Valerie Hau134651a2020-01-28 16:21:22 -0800103 mCurrentState.frameNumber = 0;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800104 mCurrentState.transform = 0;
105 mCurrentState.transformToDisplayInverse = false;
106 mCurrentState.crop.makeInvalid();
107 mCurrentState.acquireFence = new Fence(-1);
108 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
109 mCurrentState.hdrMetadata.validTypes = 0;
Marissa Wall7e0a01f2019-08-14 14:29:25 -0700110 mCurrentState.surfaceDamageRegion = Region::INVALID_REGION;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800111 mCurrentState.cornerRadius = 0.0f;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800112 mCurrentState.backgroundBlurRadius = 0;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800113 mCurrentState.api = -1;
114 mCurrentState.hasColorTransform = false;
Peiyong Linc502cb72019-03-01 15:00:23 -0800115 mCurrentState.colorSpaceAgnostic = false;
Ana Krulecc84d09b2019-11-02 23:10:29 +0100116 mCurrentState.frameRateSelectionPriority = PRIORITY_UNSET;
Evan Roskya1f1e152019-01-24 16:17:46 -0800117 mCurrentState.metadata = args.metadata;
Vishnu Nairf37dcfb2019-11-16 07:37:09 -0800118 mCurrentState.shadowRadius = 0.f;
Ady Abraham60e42ea2020-03-09 19:17:31 -0700119 mCurrentState.treeHasFrameRateVote = false;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700120
121 // drawing state & current state are identical
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800122 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700123
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800124 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700125 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800126 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200127 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700128
Vishnu Nair0f085c62019-08-30 08:49:12 -0700129 mCallingPid = args.callingPid;
130 mCallingUid = args.callingUid;
Dominik Laskowski75848362019-11-11 17:57:20 -0800131}
132
133void Layer::onFirstRef() {
134 mFlinger->onLayerFirstRef(this);
Dan Stoza436ccf32018-06-21 12:10:12 -0700135}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700136
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700137Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800138 sp<Client> c(mClientRef.promote());
139 if (c != 0) {
140 c->detachLayer(this);
141 }
142
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000143 mFrameTracker.logAndResetStats(mName);
chaviw74d90ad2019-04-26 14:45:26 -0700144 mFlinger->onLayerDestroyed(this);
Mathias Agopian96f08192010-06-02 23:28:45 -0700145}
146
chaviwde94d0f2020-01-22 13:11:25 -0800147LayerCreationArgs::LayerCreationArgs(SurfaceFlinger* flinger, const sp<Client> client,
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700148 std::string name, uint32_t w, uint32_t h, uint32_t flags,
Vishnu Nair0f085c62019-08-30 08:49:12 -0700149 LayerMetadata metadata)
150 : flinger(flinger),
151 client(client),
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700152 name(std::move(name)),
Vishnu Nair0f085c62019-08-30 08:49:12 -0700153 w(w),
154 h(h),
155 flags(flags),
156 metadata(std::move(metadata)) {
157 IPCThreadState* ipc = IPCThreadState::self();
158 callingPid = ipc->getCallingPid();
159 callingUid = ipc->getCallingUid();
160}
161
Mathias Agopian13127d82013-03-05 17:47:11 -0800162// ---------------------------------------------------------------------------
163// callbacks
164// ---------------------------------------------------------------------------
165
David Sodmaneb085e02017-10-05 18:49:04 -0700166/*
167 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
168 * Layer. So, the implementation is done in BufferLayer. When called on a
Vishnu Nairfa247b12020-02-11 08:58:26 -0800169 * EffectLayer object, it's essentially a NOP.
David Sodmaneb085e02017-10-05 18:49:04 -0700170 */
David Sodmaneb085e02017-10-05 18:49:04 -0700171void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800172
chaviw43cb3cb2019-05-31 15:23:41 -0700173void Layer::removeRemoteSyncPoints() {
174 for (auto& point : mRemoteSyncPoints) {
175 point->setTransactionApplied();
176 }
177 mRemoteSyncPoints.clear();
178
179 {
chaviw43cb3cb2019-05-31 15:23:41 -0700180 for (State pendingState : mPendingStates) {
181 pendingState.barrierLayer_legacy = nullptr;
182 }
183 }
184}
185
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700186void Layer::removeRelativeZ(const std::vector<Layer*>& layersInTree) {
187 if (mCurrentState.zOrderRelativeOf == nullptr) {
188 return;
189 }
Robert Carr2e102c92018-10-23 12:11:15 -0700190
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700191 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
192 if (strongRelative == nullptr) {
193 setZOrderRelativeOf(nullptr);
194 return;
195 }
196
197 if (!std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
198 strongRelative->removeZOrderRelative(this);
199 mFlinger->setTransactionFlags(eTraversalNeeded);
chaviw606e5cf2019-03-01 10:12:10 -0800200 setZOrderRelativeOf(nullptr);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700201 }
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700202}
203
204void Layer::removeFromCurrentState() {
205 mRemovedFromCurrentState = true;
Rob Carr4bba3702018-10-08 21:53:30 +0000206
Robert Carr2e102c92018-10-23 12:11:15 -0700207 // Since we are no longer reachable from CurrentState SurfaceFlinger
208 // will no longer invoke doTransaction for us, and so we will
209 // never finish applying transactions. We signal the sync point
210 // now so that another layer will not become indefinitely
211 // blocked.
chaviw43cb3cb2019-05-31 15:23:41 -0700212 removeRemoteSyncPoints();
Robert Carr2e102c92018-10-23 12:11:15 -0700213
214 {
215 Mutex::Autolock syncLock(mLocalSyncPointMutex);
216 for (auto& point : mLocalSyncPoints) {
217 point->setFrameAvailable();
218 }
219 mLocalSyncPoints.clear();
220 }
221
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800222 mFlinger->markLayerPendingRemovalLocked(this);
Arthur Hung4df68ab2020-03-11 18:12:01 +0800223 if (hasInput()) {
224 mFlinger->dirtyInput();
225 }
Chia-I Wuc6657022017-08-15 11:18:17 -0700226}
Chia-I Wu38512252017-05-17 14:36:16 -0700227
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700228void Layer::onRemovedFromCurrentState() {
229 auto layersInTree = getLayersInTree(LayerVector::StateSet::Current);
230 std::sort(layersInTree.begin(), layersInTree.end());
231 for (const auto& layer : layersInTree) {
232 layer->removeFromCurrentState();
233 layer->removeRelativeZ(layersInTree);
234 }
235}
236
chaviw61626f22018-11-15 16:26:27 -0800237void Layer::addToCurrentState() {
238 mRemovedFromCurrentState = false;
239
240 for (const auto& child : mCurrentChildren) {
241 child->addToCurrentState();
242 }
243}
244
Mathias Agopian13127d82013-03-05 17:47:11 -0800245// ---------------------------------------------------------------------------
246// set-up
247// ---------------------------------------------------------------------------
248
chaviw13fdc492017-06-27 12:40:18 -0700249bool Layer::getPremultipledAlpha() const {
250 return mPremultipliedAlpha;
251}
252
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700253sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800254 Mutex::Autolock _l(mLock);
Robert Carrc0df3122019-04-11 13:18:21 -0700255 if (mGetHandleCalled) {
256 ALOGE("Get handle called twice" );
257 return nullptr;
258 }
259 mGetHandleCalled = true;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700260 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800261}
262
263// ---------------------------------------------------------------------------
264// h/w composer set-up
265// ---------------------------------------------------------------------------
266
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700267static Rect reduce(const Rect& win, const Region& exclude) {
268 if (CC_LIKELY(exclude.isEmpty())) {
269 return win;
270 }
271 if (exclude.isRect()) {
272 return win.reduce(exclude.getBounds());
273 }
274 return Region(win).subtract(exclude).getBounds();
275}
276
Dan Stoza80d61162017-12-20 15:57:52 -0800277static FloatRect reduce(const FloatRect& win, const Region& exclude) {
278 if (CC_LIKELY(exclude.isEmpty())) {
279 return win;
280 }
281 // Convert through Rect (by rounding) for lack of FloatRegion
282 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
283}
284
Vishnu Nair4351ad52019-02-11 14:13:02 -0800285Rect Layer::getScreenBounds(bool reduceTransparentRegion) const {
Vishnu Nairf0c28512019-02-08 12:40:28 -0800286 if (!reduceTransparentRegion) {
287 return Rect{mScreenBounds};
288 }
289
290 FloatRect bounds = getBounds();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800291 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800292 // Transform to screen space.
293 bounds = t.transform(bounds);
294 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700295}
296
Vishnu Nair4351ad52019-02-11 14:13:02 -0800297FloatRect Layer::getBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000298 const State& s(getDrawingState());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800299 return getBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700300}
301
Vishnu Nairf0c28512019-02-08 12:40:28 -0800302FloatRect Layer::getBounds(const Region& activeTransparentRegion) const {
303 // Subtract the transparent region and snap to the bounds.
304 return reduce(mBounds, activeTransparentRegion);
305}
306
Vishnu Nairc652ff82019-03-15 12:48:54 -0700307ui::Transform Layer::getBufferScaleTransform() const {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800308 // If the layer is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
309 // it isFixedSize) then there may be additional scaling not accounted
Vishnu Nairc652ff82019-03-15 12:48:54 -0700310 // for in the layer transform.
chaviwd62d3062019-09-04 14:48:02 -0700311 if (!isFixedSize() || getBuffer() == nullptr) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700312 return {};
Vishnu Nair4351ad52019-02-11 14:13:02 -0800313 }
314
Marissa Wall290ad082019-03-06 13:23:47 -0800315 // If the layer is a buffer state layer, the active width and height
316 // could be infinite. In that case, return the effective transform.
317 const uint32_t activeWidth = getActiveWidth(getDrawingState());
318 const uint32_t activeHeight = getActiveHeight(getDrawingState());
319 if (activeWidth >= UINT32_MAX && activeHeight >= UINT32_MAX) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700320 return {};
Marissa Wall290ad082019-03-06 13:23:47 -0800321 }
322
chaviwd62d3062019-09-04 14:48:02 -0700323 int bufferWidth = getBuffer()->getWidth();
324 int bufferHeight = getBuffer()->getHeight();
Vishnu Nairc652ff82019-03-15 12:48:54 -0700325
chaviw4244e032019-09-04 11:27:49 -0700326 if (getBufferTransform() & NATIVE_WINDOW_TRANSFORM_ROT_90) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700327 std::swap(bufferWidth, bufferHeight);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800328 }
Vishnu Nairc652ff82019-03-15 12:48:54 -0700329
Marissa Wall290ad082019-03-06 13:23:47 -0800330 float sx = activeWidth / static_cast<float>(bufferWidth);
331 float sy = activeHeight / static_cast<float>(bufferHeight);
332
Vishnu Nair4351ad52019-02-11 14:13:02 -0800333 ui::Transform extraParentScaling;
334 extraParentScaling.set(sx, 0, 0, sy);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700335 return extraParentScaling;
336}
337
338ui::Transform Layer::getTransformWithScale(const ui::Transform& bufferScaleTransform) const {
339 // We need to mirror this scaling to child surfaces or we will break the contract where WM can
340 // treat child surfaces as pixels in the parent surface.
chaviwd62d3062019-09-04 14:48:02 -0700341 if (!isFixedSize() || getBuffer() == nullptr) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700342 return mEffectiveTransform;
343 }
344 return mEffectiveTransform * bufferScaleTransform;
345}
346
347FloatRect Layer::getBoundsPreScaling(const ui::Transform& bufferScaleTransform) const {
348 // We need the pre scaled layer bounds when computing child bounds to make sure the child is
349 // cropped to its parent layer after any buffer transform scaling is applied.
chaviwd62d3062019-09-04 14:48:02 -0700350 if (!isFixedSize() || getBuffer() == nullptr) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700351 return mBounds;
352 }
353 return bufferScaleTransform.inverse().transform(mBounds);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800354}
355
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700356void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform,
357 float parentShadowRadius) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800358 const State& s(getDrawingState());
359
360 // Calculate effective layer transform
361 mEffectiveTransform = parentTransform * getActiveTransform(s);
362
363 // Transform parent bounds to layer space
364 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
365
Vishnu Nairc652ff82019-03-15 12:48:54 -0700366 // Calculate source bounds
Vishnu Nair4351ad52019-02-11 14:13:02 -0800367 mSourceBounds = computeSourceBounds(parentBounds);
368
369 // Calculate bounds by croping diplay frame with layer crop and parent bounds
370 FloatRect bounds = mSourceBounds;
371 const Rect layerCrop = getCrop(s);
372 if (!layerCrop.isEmpty()) {
373 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
374 }
375 bounds = bounds.intersect(parentBounds);
376
377 mBounds = bounds;
378 mScreenBounds = mEffectiveTransform.transform(mBounds);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700379
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700380 // Use the layer's own shadow radius if set. Otherwise get the radius from
381 // parent.
382 if (s.shadowRadius > 0.f) {
383 mEffectiveShadowRadius = s.shadowRadius;
384 } else {
385 mEffectiveShadowRadius = parentShadowRadius;
386 }
387
388 // Shadow radius is passed down to only one layer so if the layer can draw shadows,
389 // don't pass it to its children.
390 const float childShadowRadius = canDrawShadows() ? 0.f : mEffectiveShadowRadius;
391
Vishnu Nairc652ff82019-03-15 12:48:54 -0700392 // Add any buffer scaling to the layer's children.
393 ui::Transform bufferScaleTransform = getBufferScaleTransform();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800394 for (const sp<Layer>& child : mDrawingChildren) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700395 child->computeBounds(getBoundsPreScaling(bufferScaleTransform),
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700396 getTransformWithScale(bufferScaleTransform), childShadowRadius);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800397 }
398}
399
Vishnu Nair60356342018-11-13 13:00:45 -0800400Rect Layer::getCroppedBufferSize(const State& s) const {
401 Rect size = getBufferSize(s);
402 Rect crop = getCrop(s);
403 if (!crop.isEmpty() && size.isValid()) {
404 size.intersect(crop, &size);
405 } else if (!crop.isEmpty()) {
406 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700407 }
Vishnu Nair60356342018-11-13 13:00:45 -0800408 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800409}
410
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700411void Layer::setupRoundedCornersCropCoordinates(Rect win,
412 const FloatRect& roundedCornersCrop) const {
413 // Translate win by the rounded corners rect coordinates, to have all values in
414 // layer coordinate space.
415 win.left -= roundedCornersCrop.left;
416 win.right -= roundedCornersCrop.left;
417 win.top -= roundedCornersCrop.top;
418 win.bottom -= roundedCornersCrop.top;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700419}
420
Lloyd Piquede196652020-01-22 17:29:58 -0800421void Layer::prepareBasicGeometryCompositionState() {
Lloyd Piquec6687342019-03-07 21:34:57 -0800422 const auto& drawingState{getDrawingState()};
423 const uint32_t layerStack = getLayerStack();
424 const auto alpha = static_cast<float>(getAlpha());
425 const bool opaque = isOpaque(drawingState);
426 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
427
428 auto blendMode = Hwc2::IComposerClient::BlendMode::NONE;
429 if (!opaque || alpha != 1.0f) {
430 blendMode = mPremultipliedAlpha ? Hwc2::IComposerClient::BlendMode::PREMULTIPLIED
431 : Hwc2::IComposerClient::BlendMode::COVERAGE;
432 }
433
Lloyd Piquede196652020-01-22 17:29:58 -0800434 auto* compositionState = editCompositionState();
435 compositionState->layerStackId =
Lloyd Piquec6687342019-03-07 21:34:57 -0800436 (layerStack != ~0u) ? std::make_optional(layerStack) : std::nullopt;
Lloyd Piquede196652020-01-22 17:29:58 -0800437 compositionState->internalOnly = getPrimaryDisplayOnly();
438 compositionState->isVisible = isVisible();
439 compositionState->isOpaque = opaque && !usesRoundedCorners && alpha == 1.f;
440 compositionState->shadowRadius = mEffectiveShadowRadius;
Lloyd Piquec6687342019-03-07 21:34:57 -0800441
Lloyd Piquede196652020-01-22 17:29:58 -0800442 compositionState->contentDirty = contentDirty;
Lloyd Piquec6687342019-03-07 21:34:57 -0800443 contentDirty = false;
444
Lloyd Piquede196652020-01-22 17:29:58 -0800445 compositionState->geomLayerBounds = mBounds;
446 compositionState->geomLayerTransform = getTransform();
447 compositionState->geomInverseLayerTransform = compositionState->geomLayerTransform.inverse();
448 compositionState->transparentRegionHint = getActiveTransparentRegion(drawingState);
Lloyd Piquec6687342019-03-07 21:34:57 -0800449
Lloyd Piquede196652020-01-22 17:29:58 -0800450 compositionState->blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
451 compositionState->alpha = alpha;
452 compositionState->backgroundBlurRadius = drawingState.backgroundBlurRadius;
Lloyd Piquec6687342019-03-07 21:34:57 -0800453}
454
Lloyd Piquede196652020-01-22 17:29:58 -0800455void Layer::prepareGeometryCompositionState() {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800456 const auto& drawingState{getDrawingState()};
Mathias Agopian13127d82013-03-05 17:47:11 -0800457
Lloyd Piquea83776c2019-01-29 18:42:32 -0800458 int type = drawingState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
459 int appId = drawingState.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700460 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400461 if (parent.get()) {
462 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800463 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
464 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
Jiwen 'Steve' Cai736c74e2019-05-28 18:33:22 -0700465 if (parentType > 0 && parentAppId > 0) {
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800466 type = parentType;
467 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700468 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400469 }
470
Lloyd Piquede196652020-01-22 17:29:58 -0800471 auto* compositionState = editCompositionState();
David Sodman15094112018-10-11 09:39:37 -0700472
Lloyd Piquede196652020-01-22 17:29:58 -0800473 compositionState->geomBufferSize = getBufferSize(drawingState);
474 compositionState->geomContentCrop = getBufferCrop();
475 compositionState->geomCrop = getCrop(drawingState);
476 compositionState->geomBufferTransform = getBufferTransform();
477 compositionState->geomBufferUsesDisplayInverseTransform = getTransformToDisplayInverse();
478 compositionState->geomUsesSourceCrop = usesSourceCrop();
479 compositionState->isSecure = isSecure();
480
481 compositionState->type = type;
482 compositionState->appId = appId;
Lloyd Pique8d9f8362020-02-11 19:13:09 -0800483
484 compositionState->metadata.clear();
485 const auto& supportedMetadata = mFlinger->getHwComposer().getSupportedLayerGenericMetadata();
486 for (const auto& [key, mandatory] : supportedMetadata) {
487 const auto& genericLayerMetadataCompatibilityMap =
488 mFlinger->getGenericLayerMetadataKeyMap();
489 auto compatIter = genericLayerMetadataCompatibilityMap.find(key);
490 if (compatIter == std::end(genericLayerMetadataCompatibilityMap)) {
491 continue;
492 }
493 const uint32_t id = compatIter->second;
494
495 auto it = drawingState.metadata.mMap.find(id);
496 if (it == std::end(drawingState.metadata.mMap)) {
497 continue;
498 }
499
500 compositionState->metadata
501 .emplace(key, compositionengine::GenericLayerMetadataEntry{mandatory, it->second});
502 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800503}
David Sodmanba340492018-08-05 21:51:33 -0700504
Lloyd Piquede196652020-01-22 17:29:58 -0800505void Layer::preparePerFrameCompositionState() {
Lloyd Pique688abd42019-02-15 15:42:24 -0800506 const auto& drawingState{getDrawingState()};
Lloyd Piquede196652020-01-22 17:29:58 -0800507 auto* compositionState = editCompositionState();
Lloyd Piquef5275482019-01-29 18:42:42 -0800508
Lloyd Piquede196652020-01-22 17:29:58 -0800509 compositionState->forceClientComposition = false;
510
511 compositionState->isColorspaceAgnostic = isColorSpaceAgnostic();
512 compositionState->dataspace = getDataSpace();
513 compositionState->colorTransform = getColorTransform();
514 compositionState->colorTransformIsIdentity = !hasColorTransform();
515 compositionState->surfaceDamage = surfaceDamageRegion;
516 compositionState->hasProtectedContent = isProtected();
Lloyd Pique688abd42019-02-15 15:42:24 -0800517
518 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700519
Lloyd Piquede196652020-01-22 17:29:58 -0800520 compositionState->isOpaque =
Lloyd Pique688abd42019-02-15 15:42:24 -0800521 isOpaque(drawingState) && !usesRoundedCorners && getAlpha() == 1.0_hf;
Lloyd Piquef5275482019-01-29 18:42:42 -0800522
523 // Force client composition for special cases known only to the front-end.
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800524 if (isHdrY410() || usesRoundedCorners || drawShadows()) {
Lloyd Piquede196652020-01-22 17:29:58 -0800525 compositionState->forceClientComposition = true;
Lloyd Piquef5275482019-01-29 18:42:42 -0800526 }
527}
528
Lloyd Piquede196652020-01-22 17:29:58 -0800529void Layer::prepareCursorCompositionState() {
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800530 const State& drawingState{getDrawingState()};
Lloyd Piquede196652020-01-22 17:29:58 -0800531 auto* compositionState = editCompositionState();
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800532
533 // Apply the layer's transform, followed by the display's global transform
534 // Here we're guaranteed that the layer's transform preserves rects
535 Rect win = getCroppedBufferSize(drawingState);
536 // Subtract the transparent region and snap to the bounds
537 Rect bounds = reduce(win, getActiveTransparentRegion(drawingState));
538 Rect frame(getTransform().transform(bounds));
539
Lloyd Piquede196652020-01-22 17:29:58 -0800540 compositionState->cursorFrame = frame;
541}
542
543sp<compositionengine::LayerFE> Layer::asLayerFE() const {
544 return const_cast<compositionengine::LayerFE*>(
545 static_cast<const compositionengine::LayerFE*>(this));
546}
547
548sp<compositionengine::LayerFE> Layer::getCompositionEngineLayerFE() const {
549 return nullptr;
550}
551
552compositionengine::LayerFECompositionState* Layer::editCompositionState() {
553 return nullptr;
554}
555
556const compositionengine::LayerFECompositionState* Layer::getCompositionState() const {
557 return nullptr;
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800558}
559
Lloyd Piquef16688f2019-02-19 17:47:57 -0800560bool Layer::onPreComposition(nsecs_t) {
561 return false;
562}
563
Lloyd Piquede196652020-01-22 17:29:58 -0800564void Layer::prepareCompositionState(compositionengine::LayerFE::StateSubset subset) {
Lloyd Piquec6687342019-03-07 21:34:57 -0800565 using StateSubset = compositionengine::LayerFE::StateSubset;
Lloyd Piquef5275482019-01-29 18:42:42 -0800566
Lloyd Piquec6687342019-03-07 21:34:57 -0800567 switch (subset) {
568 case StateSubset::BasicGeometry:
Lloyd Piquede196652020-01-22 17:29:58 -0800569 prepareBasicGeometryCompositionState();
Lloyd Piquec6687342019-03-07 21:34:57 -0800570 break;
571
572 case StateSubset::GeometryAndContent:
Lloyd Piquede196652020-01-22 17:29:58 -0800573 prepareBasicGeometryCompositionState();
574 prepareGeometryCompositionState();
575 preparePerFrameCompositionState();
Lloyd Piquec6687342019-03-07 21:34:57 -0800576 break;
577
578 case StateSubset::Content:
Lloyd Piquede196652020-01-22 17:29:58 -0800579 preparePerFrameCompositionState();
580 break;
581
582 case StateSubset::Cursor:
583 prepareCursorCompositionState();
Lloyd Piquec6687342019-03-07 21:34:57 -0800584 break;
585 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800586}
Mathias Agopian29a367b2011-07-12 14:51:45 -0700587
Lloyd Piquea83776c2019-01-29 18:42:32 -0800588const char* Layer::getDebugName() const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700589 return mName.c_str();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800590}
591
Mathias Agopian13127d82013-03-05 17:47:11 -0800592// ---------------------------------------------------------------------------
593// drawing...
594// ---------------------------------------------------------------------------
595
Vishnu Nair9b079a22020-01-21 14:36:08 -0800596std::optional<compositionengine::LayerFE::LayerSettings> Layer::prepareClientComposition(
Lloyd Piquef16688f2019-02-19 17:47:57 -0800597 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
Lloyd Piquede196652020-01-22 17:29:58 -0800598 if (!getCompositionState()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800599 return {};
600 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800601
Vishnu Nair4351ad52019-02-11 14:13:02 -0800602 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000603 half alpha = getAlpha();
Vishnu Nair9b079a22020-01-21 14:36:08 -0800604
605 compositionengine::LayerFE::LayerSettings layerSettings;
Lloyd Piquef16688f2019-02-19 17:47:57 -0800606 layerSettings.geometry.boundaries = bounds;
607 if (targetSettings.useIdentityTransform) {
608 layerSettings.geometry.positionTransform = mat4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000609 } else {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800610 layerSettings.geometry.positionTransform = getTransform().asMatrix4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000611 }
612
613 if (hasColorTransform()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800614 layerSettings.colorTransform = getColorTransform();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000615 }
616
617 const auto roundedCornerState = getRoundedCornerState();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800618 layerSettings.geometry.roundedCornersRadius = roundedCornerState.radius;
619 layerSettings.geometry.roundedCornersCrop = roundedCornerState.cropRect;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000620
Lloyd Piquef16688f2019-02-19 17:47:57 -0800621 layerSettings.alpha = alpha;
chaviw4244e032019-09-04 11:27:49 -0700622 layerSettings.sourceDataspace = getDataSpace();
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800623 layerSettings.backgroundBlurRadius = getBackgroundBlurRadius();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800624 return layerSettings;
Mathias Agopian13127d82013-03-05 17:47:11 -0800625}
626
Vishnu Nair9b079a22020-01-21 14:36:08 -0800627std::optional<compositionengine::LayerFE::LayerSettings> Layer::prepareShadowClientComposition(
628 const LayerFE::LayerSettings& casterLayerSettings, const Rect& displayViewport,
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800629 ui::Dataspace outputDataspace) {
630 renderengine::ShadowSettings shadow = getShadowSettings(displayViewport);
631 if (shadow.length <= 0.f) {
632 return {};
633 }
634
635 const float casterAlpha = casterLayerSettings.alpha;
636 const bool casterIsOpaque = ((casterLayerSettings.source.buffer.buffer != nullptr) &&
637 casterLayerSettings.source.buffer.isOpaque);
638
Vishnu Nair9b079a22020-01-21 14:36:08 -0800639 compositionengine::LayerFE::LayerSettings shadowLayer = casterLayerSettings;
640
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800641 shadowLayer.shadow = shadow;
Vishnu Naira483b4a2019-12-12 15:07:52 -0800642 shadowLayer.geometry.boundaries = mBounds; // ignore transparent region
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800643
644 // If the casting layer is translucent, we need to fill in the shadow underneath the layer.
645 // Otherwise the generated shadow will only be shown around the casting layer.
646 shadowLayer.shadow.casterIsTranslucent = !casterIsOpaque || (casterAlpha < 1.0f);
647 shadowLayer.shadow.ambientColor *= casterAlpha;
648 shadowLayer.shadow.spotColor *= casterAlpha;
649 shadowLayer.sourceDataspace = outputDataspace;
650 shadowLayer.source.buffer.buffer = nullptr;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800651 shadowLayer.source.buffer.fence = nullptr;
652 shadowLayer.frameNumber = 0;
653 shadowLayer.bufferId = 0;
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800654
655 if (shadowLayer.shadow.ambientColor.a <= 0.f && shadowLayer.shadow.spotColor.a <= 0.f) {
656 return {};
657 }
658
659 float casterCornerRadius = shadowLayer.geometry.roundedCornersRadius;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800660 const FloatRect& cornerRadiusCropRect = shadowLayer.geometry.roundedCornersCrop;
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800661 const FloatRect& casterRect = shadowLayer.geometry.boundaries;
662
663 // crop used to set the corner radius may be larger than the content rect. Adjust the corner
664 // radius accordingly.
665 if (casterCornerRadius > 0.f) {
666 float cropRectOffset = std::max(std::abs(cornerRadiusCropRect.top - casterRect.top),
667 std::abs(cornerRadiusCropRect.left - casterRect.left));
668 if (cropRectOffset > casterCornerRadius) {
669 casterCornerRadius = 0;
670 } else {
671 casterCornerRadius -= cropRectOffset;
672 }
673 shadowLayer.geometry.roundedCornersRadius = casterCornerRadius;
674 }
675
676 return shadowLayer;
677}
678
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800679void Layer::prepareClearClientComposition(LayerFE::LayerSettings& layerSettings,
680 bool blackout) const {
681 layerSettings.source.buffer.buffer = nullptr;
682 layerSettings.source.solidColor = half3(0.0, 0.0, 0.0);
683 layerSettings.disableBlending = true;
Vishnu Nair3b653e72020-02-24 16:40:50 -0800684 layerSettings.bufferId = 0;
Vishnu Nairb87d94f2020-02-13 09:17:36 -0800685 layerSettings.frameNumber = 0;
686
687 // If layer is blacked out, force alpha to 1 so that we draw a black color layer.
688 layerSettings.alpha = blackout ? 1.0f : 0.0f;
689}
690
691std::vector<compositionengine::LayerFE::LayerSettings> Layer::prepareClientCompositionList(
692 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
693 std::optional<compositionengine::LayerFE::LayerSettings> layerSettings =
694 prepareClientComposition(targetSettings);
695 // Nothing to render.
696 if (!layerSettings) {
697 return {};
698 }
699
700 // HWC requests to clear this layer.
701 if (targetSettings.clearContent) {
702 prepareClearClientComposition(*layerSettings, false /* blackout */);
703 return {*layerSettings};
704 }
705
706 std::optional<compositionengine::LayerFE::LayerSettings> shadowSettings =
707 prepareShadowClientComposition(*layerSettings, targetSettings.viewport,
708 targetSettings.dataspace);
709 // There are no shadows to render.
710 if (!shadowSettings) {
711 return {*layerSettings};
712 }
713
714 // If the layer casts a shadow but the content casting the shadow is occluded, skip
715 // composing the non-shadow content and only draw the shadows.
716 if (targetSettings.realContentIsVisible) {
717 return {*shadowSettings, *layerSettings};
718 }
719
720 return {*shadowSettings};
721}
722
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800723Hwc2::IComposerClient::Composition Layer::getCompositionType(
724 const sp<const DisplayDevice>& display) const {
725 const auto outputLayer = findOutputLayerForDisplay(display);
Alec Mouri6b9e9912020-01-21 10:50:24 -0800726 if (outputLayer == nullptr) {
727 return Hwc2::IComposerClient::Composition::INVALID;
728 }
729 if (outputLayer->getState().hwc) {
730 return (*outputLayer->getState().hwc).hwcCompositionType;
731 } else {
732 return Hwc2::IComposerClient::Composition::CLIENT;
733 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800734}
735
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800736bool Layer::getClearClientTarget(const sp<const DisplayDevice>& display) const {
737 const auto outputLayer = findOutputLayerForDisplay(display);
738 LOG_FATAL_IF(!outputLayer);
739 return outputLayer->getState().clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800740}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800741
Dan Stozacac35382016-01-27 12:21:06 -0800742bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
743 if (point->getFrameNumber() <= mCurrentFrameNumber) {
744 // Don't bother with a SyncPoint, since we've already latched the
745 // relevant frame
746 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700747 }
Robert Carr2e102c92018-10-23 12:11:15 -0700748 if (isRemovedFromCurrentState()) {
749 return false;
750 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700751
Dan Stozacac35382016-01-27 12:21:06 -0800752 Mutex::Autolock lock(mLocalSyncPointMutex);
753 mLocalSyncPoints.push_back(point);
754 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700755}
756
Mathias Agopian13127d82013-03-05 17:47:11 -0800757// ----------------------------------------------------------------------------
758// local state
759// ----------------------------------------------------------------------------
760
David Sodman41fdfc92017-11-06 16:09:56 -0800761bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800762 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700763 return (s.flags & layer_state_t::eLayerSecure);
764}
765
Mathias Agopian13127d82013-03-05 17:47:11 -0800766// ----------------------------------------------------------------------------
767// transaction
768// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800769
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800770void Layer::pushPendingState() {
771 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700772 return;
773 }
Alec Mourie60041e2019-06-14 18:59:51 -0700774 ATRACE_CALL();
Dan Stoza7dde5992015-05-22 09:51:44 -0700775
Dan Stoza7dde5992015-05-22 09:51:44 -0700776 // If this transaction is waiting on the receipt of a frame, generate a sync
777 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700778 // We don't allow installing sync points after we are removed from the current state
779 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800780 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
781 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800782 if (barrierLayer == nullptr) {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700783 ALOGE("[%s] Unable to promote barrier Layer.", getDebugName());
Dan Stoza7dde5992015-05-22 09:51:44 -0700784 // If we can't promote the layer we are intended to wait on,
785 // then it is expired or otherwise invalid. Allow this transaction
786 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800787 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800788 } else {
chaviw43cb3cb2019-05-31 15:23:41 -0700789 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy, this);
Robert Carr0d480722017-01-10 16:42:54 -0800790 if (barrierLayer->addSyncPoint(syncPoint)) {
Alec Mourie60041e2019-06-14 18:59:51 -0700791 std::stringstream ss;
792 ss << "Adding sync point " << mCurrentState.frameNumber_legacy;
793 ATRACE_NAME(ss.str().c_str());
Dan Stozacac35382016-01-27 12:21:06 -0800794 mRemoteSyncPoints.push_back(std::move(syncPoint));
795 } else {
796 // We already missed the frame we're supposed to synchronize
797 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800798 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800799 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700800 }
801
Dan Stoza7dde5992015-05-22 09:51:44 -0700802 // Wake us up to check if the frame has been received
803 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700804 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700805 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800806 mPendingStates.push_back(mCurrentState);
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700807 ATRACE_INT(mTransactionName.c_str(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700808}
809
Pablo Ceballos05289c22016-04-14 15:49:55 -0700810void Layer::popPendingState(State* stateToCommit) {
Alec Mourie60041e2019-06-14 18:59:51 -0700811 ATRACE_CALL();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800812 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700813
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800814 mPendingStates.removeAt(0);
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700815 ATRACE_INT(mTransactionName.c_str(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700816}
817
Pablo Ceballos05289c22016-04-14 15:49:55 -0700818bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700819 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800820 while (!mPendingStates.empty()) {
821 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700822 if (mRemoteSyncPoints.empty()) {
823 // If we don't have a sync point for this, apply it anyway. It
824 // will be visually wrong, but it should keep us from getting
825 // into too much trouble.
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700826 ALOGE("[%s] No local sync point found", getDebugName());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700827 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700828 stateUpdateAvailable = true;
829 continue;
830 }
831
Marissa Wallf58c14b2018-07-24 10:50:43 -0700832 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800833 mPendingStates[0].frameNumber_legacy) {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700834 ALOGE("[%s] Unexpected sync point frame number found", getDebugName());
Dan Stozacac35382016-01-27 12:21:06 -0800835
836 // Signal our end of the sync point and then dispose of it
837 mRemoteSyncPoints.front()->setTransactionApplied();
838 mRemoteSyncPoints.pop_front();
839 continue;
840 }
841
Dan Stoza7dde5992015-05-22 09:51:44 -0700842 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
Alec Mourie60041e2019-06-14 18:59:51 -0700843 ATRACE_NAME("frameIsAvailable");
Dan Stoza7dde5992015-05-22 09:51:44 -0700844 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700845 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700846 stateUpdateAvailable = true;
847
848 // Signal our end of the sync point and then dispose of it
849 mRemoteSyncPoints.front()->setTransactionApplied();
850 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800851 } else {
Alec Mourie60041e2019-06-14 18:59:51 -0700852 ATRACE_NAME("!frameIsAvailable");
Dan Stoza792e5292016-02-11 11:43:58 -0800853 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700854 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700855 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700856 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700857 stateUpdateAvailable = true;
858 }
859 }
860
861 // If we still have pending updates, wake SurfaceFlinger back up and point
862 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800863 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700864 setTransactionFlags(eTransactionNeeded);
865 mFlinger->setTransactionFlags(eTraversalNeeded);
866 }
867
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800868 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700869 return stateUpdateAvailable;
870}
871
Marissa Wall61c58622018-07-18 10:12:20 -0700872uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000873 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800874
Marissa Wall61c58622018-07-18 10:12:20 -0700875 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
876 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700877
David Sodmaneb085e02017-10-05 18:49:04 -0700878 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700879 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000880 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800881 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
882 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
883 " requested={ wh={%4u,%4u} }}\n"
884 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
885 " requested={ wh={%4u,%4u} }}\n",
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700886 this, getName().c_str(), getBufferTransform(), getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700887 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
888 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
889 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
890 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
891 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700892 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
893 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
894 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800895 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700896
Robert Carre392b552017-09-19 12:16:05 -0700897 // Don't let Layer::doTransaction update the drawing state
898 // if we have a pending resize, unless we are in fixed-size mode.
899 // the drawing state will be updated only once we receive a buffer
900 // with the correct size.
901 //
902 // In particular, we want to make sure the clip (which is part
903 // of the geometry state) is latched together with the size but is
904 // latched immediately when no resizing is involved.
905 //
906 // If a sideband stream is attached, however, we want to skip this
907 // optimization so that transactions aren't missed when a buffer
908 // never arrives
909 //
910 // In the case that we don't have a buffer we ignore other factors
911 // and avoid entering the resizePending state. At a high level the
912 // resizePending state is to avoid applying the state of the new buffer
913 // to the old buffer. However in the state where we don't have an old buffer
914 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700915 const bool resizePending =
916 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
917 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
chaviwd62d3062019-09-04 14:48:02 -0700918 (getBuffer() != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700919 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800920 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700921 flags |= eDontUpdateGeometryState;
922 }
923 }
924
Robert Carr7bf247e2017-05-18 14:02:49 -0700925 // Here we apply various requested geometry states, depending on our
926 // latching configuration. See Layer.h for a detailed discussion of
927 // how geometry latching is controlled.
928 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000929 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700930
Robert Carr7bf247e2017-05-18 14:02:49 -0700931 // There is an awkward asymmetry in the handling of the crop states in the position
932 // states, as can be seen below. Largely this arises from position and transform
933 // being stored in the same data structure while having different latching rules.
934 // b/38182305
935 //
Marissa Wall61c58622018-07-18 10:12:20 -0700936 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700937 // applyPendingStates in the presence of deferred transactions.
chaviw214c89d2019-09-04 16:03:53 -0700938 editCurrentState.active_legacy = editCurrentState.requested_legacy;
939 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Mathias Agopian13127d82013-03-05 17:47:11 -0800940 }
941
Marissa Wall61c58622018-07-18 10:12:20 -0700942 return flags;
943}
944
945uint32_t Layer::doTransaction(uint32_t flags) {
946 ATRACE_CALL();
947
chaviw5aedec92018-10-22 10:40:38 -0700948 if (mLayerDetached) {
Robert Carr321e83c2019-08-19 15:49:30 -0700949 // Ensure BLAST buffer callbacks are processed.
950 // detachChildren and mLayerDetached were implemented to avoid geometry updates
951 // to layers in the cases of animation. For BufferQueue layers buffers are still
952 // consumed as normal. This is useful as otherwise the client could get hung
953 // inevitably waiting on a buffer to return. We recreate this semantic for BufferQueue
954 // even though it is a little consistent. detachChildren is shortly slated for removal
955 // by the hierarchy mirroring work so we don't need to worry about it too much.
Valerie Hau7618b232020-01-09 16:03:08 -0800956 forceSendCallbacks();
Robert Carr321e83c2019-08-19 15:49:30 -0700957 mCurrentState.callbackHandles = {};
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800958 return flags;
959 }
960
961 if (mChildrenChanged) {
962 flags |= eVisibleRegion;
963 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -0700964 }
965
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800966 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +0000967 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -0700968 if (!applyPendingStates(&c)) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800969 return flags;
Marissa Wall61c58622018-07-18 10:12:20 -0700970 }
971
972 flags = doTransactionResize(flags, &c);
973
Alec Mourib416efd2018-09-06 21:01:59 +0000974 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700975
976 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800977 // invalidate and recompute the visible regions if needed
978 flags |= Layer::eVisibleRegion;
979 }
980
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700981 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800982 // invalidate and recompute the visible regions if needed
983 flags |= eVisibleRegion;
984 this->contentDirty = true;
985
986 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -0700987 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -0700988 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -0800989 }
990
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800991 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -0700992 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800993 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -0700994 }
995
Mathias Agopian13127d82013-03-05 17:47:11 -0800996 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -0700997 commitTransaction(c);
Vishnu Nair8406fd72019-07-30 11:29:31 -0700998 mPendingStatesSnapshot = mPendingStates;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800999 mCurrentState.callbackHandles = {};
Robert Carra1257842020-01-31 13:48:28 -08001000
1001 maybeDirtyInput();
1002
Mathias Agopian13127d82013-03-05 17:47:11 -08001003 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001004}
1005
Pablo Ceballos05289c22016-04-14 15:49:55 -07001006void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001007 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001008}
1009
Mathias Agopian13127d82013-03-05 17:47:11 -08001010uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001011 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -08001012}
1013
1014uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001015 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -08001016}
1017
chaviw214c89d2019-09-04 16:03:53 -07001018bool Layer::setPosition(float x, float y) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001019 if (mCurrentState.requested_legacy.transform.tx() == x &&
1020 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001021 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001022 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001023
1024 // We update the requested and active position simultaneously because
1025 // we want to apply the position portion of the transform matrix immediately,
1026 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001027 mCurrentState.requested_legacy.transform.set(x, y);
chaviw214c89d2019-09-04 16:03:53 -07001028 // Here we directly update the active state
1029 // unlike other setters, because we store it within
1030 // the transform, but use different latching rules.
1031 // b/38182305
1032 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr69663fb2016-03-27 19:59:19 -07001033
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001034 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001035 setTransactionFlags(eTransactionNeeded);
1036 return true;
1037}
Robert Carr82364e32016-05-15 11:27:47 -07001038
Robert Carr1f0a16a2016-10-24 16:27:39 -07001039bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1040 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1041 if (idx < 0) {
1042 return false;
1043 }
1044 if (childLayer->setLayer(z)) {
1045 mCurrentChildren.removeAt(idx);
1046 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001047 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001048 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001049 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001050}
1051
Robert Carr503c7042017-09-27 15:06:08 -07001052bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1053 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1054 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1055 if (idx < 0) {
1056 return false;
1057 }
1058 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1059 mCurrentChildren.removeAt(idx);
1060 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001061 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001062 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001063 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001064}
1065
Robert Carrae060832016-11-28 10:51:00 -08001066bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001067 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
1068 mCurrentState.sequence++;
1069 mCurrentState.z = z;
1070 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001071
1072 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001073 if (mCurrentState.zOrderRelativeOf != nullptr) {
1074 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -07001075 if (strongRelative != nullptr) {
1076 strongRelative->removeZOrderRelative(this);
1077 }
chaviw606e5cf2019-03-01 10:12:10 -08001078 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -07001079 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001080 setTransactionFlags(eTransactionNeeded);
1081 return true;
1082}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001083
Robert Carrdb66e622017-04-10 16:55:57 -07001084void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001085 mCurrentState.zOrderRelatives.remove(relative);
1086 mCurrentState.sequence++;
1087 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001088 setTransactionFlags(eTransactionNeeded);
1089}
1090
1091void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001092 mCurrentState.zOrderRelatives.add(relative);
1093 mCurrentState.modified = true;
1094 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -07001095 setTransactionFlags(eTransactionNeeded);
1096}
1097
chaviw606e5cf2019-03-01 10:12:10 -08001098void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
1099 mCurrentState.zOrderRelativeOf = relativeOf;
1100 mCurrentState.sequence++;
1101 mCurrentState.modified = true;
chaviwbdb8b802019-10-14 09:17:12 -07001102 mCurrentState.isRelativeOf = relativeOf != nullptr;
1103
chaviw606e5cf2019-03-01 10:12:10 -08001104 setTransactionFlags(eTransactionNeeded);
1105}
1106
Robert Carr503d2bd2017-12-04 15:49:47 -08001107bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001108 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1109 if (handle == nullptr) {
1110 return false;
1111 }
1112 sp<Layer> relative = handle->owner.promote();
1113 if (relative == nullptr) {
1114 return false;
1115 }
1116
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001117 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1118 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001119 return false;
1120 }
1121
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001122 mCurrentState.sequence++;
1123 mCurrentState.modified = true;
1124 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001125
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001126 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -07001127 if (oldZOrderRelativeOf != nullptr) {
1128 oldZOrderRelativeOf->removeZOrderRelative(this);
1129 }
chaviw606e5cf2019-03-01 10:12:10 -08001130 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -07001131 relative->addZOrderRelative(this);
1132
1133 setTransactionFlags(eTransactionNeeded);
1134
1135 return true;
1136}
1137
Mathias Agopian13127d82013-03-05 17:47:11 -08001138bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001139 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001140 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001141 mCurrentState.requested_legacy.w = w;
1142 mCurrentState.requested_legacy.h = h;
1143 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001144 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001145
1146 // record the new size, from this point on, when the client request
1147 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001148 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001149 return true;
1150}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001151bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001152 if (mCurrentState.color.a == alpha) return false;
1153 mCurrentState.sequence++;
1154 mCurrentState.color.a = alpha;
1155 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001156 setTransactionFlags(eTransactionNeeded);
1157 return true;
1158}
chaviw13fdc492017-06-27 12:40:18 -07001159
Valerie Haudd0b7572019-01-29 14:59:27 -08001160bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1161 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001162 return false;
Valerie Hauaa194562019-02-05 16:21:38 -08001163 }
1164 mCurrentState.sequence++;
1165 mCurrentState.modified = true;
1166 setTransactionFlags(eTransactionNeeded);
1167
1168 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001169 // create background color layer if one does not yet exist
Vishnu Nairfa247b12020-02-11 08:58:26 -08001170 uint32_t flags = ISurfaceComposerClient::eFXSurfaceEffect;
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001171 std::string name = mName + "BackgroundColorLayer";
Vishnu Nairfa247b12020-02-11 08:58:26 -08001172 mCurrentState.bgColorLayer = mFlinger->getFactory().createEffectLayer(
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001173 LayerCreationArgs(mFlinger.get(), nullptr, std::move(name), 0, 0, flags,
1174 LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -07001175
Valerie Haudd0b7572019-01-29 14:59:27 -08001176 // add to child list
1177 addChild(mCurrentState.bgColorLayer);
1178 mFlinger->mLayersAdded = true;
1179 // set up SF to handle added color layer
1180 if (isRemovedFromCurrentState()) {
1181 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1182 }
1183 mFlinger->setTransactionFlags(eTransactionNeeded);
1184 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1185 mCurrentState.bgColorLayer->reparent(nullptr);
1186 mCurrentState.bgColorLayer = nullptr;
1187 return true;
1188 }
1189
1190 mCurrentState.bgColorLayer->setColor(color);
1191 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1192 mCurrentState.bgColorLayer->setAlpha(alpha);
1193 mCurrentState.bgColorLayer->setDataspace(dataspace);
1194
chaviw13fdc492017-06-27 12:40:18 -07001195 return true;
1196}
1197
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001198bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001199 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001200
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001201 mCurrentState.sequence++;
1202 mCurrentState.cornerRadius = cornerRadius;
1203 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001204 setTransactionFlags(eTransactionNeeded);
1205 return true;
1206}
1207
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001208bool Layer::setBackgroundBlurRadius(int backgroundBlurRadius) {
1209 if (mCurrentState.backgroundBlurRadius == backgroundBlurRadius) return false;
1210
1211 mCurrentState.sequence++;
1212 mCurrentState.backgroundBlurRadius = backgroundBlurRadius;
1213 mCurrentState.modified = true;
1214 setTransactionFlags(eTransactionNeeded);
1215 return true;
1216}
1217
Robert Carrd4ae7f32018-06-07 16:10:57 -07001218bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1219 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001220 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001221 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1222
1223 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1224 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1225 return false;
1226 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001227 mCurrentState.sequence++;
1228 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1229 matrix.dsdy);
1230 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001231 setTransactionFlags(eTransactionNeeded);
1232 return true;
1233}
Marissa Wall61c58622018-07-18 10:12:20 -07001234
Mathias Agopian13127d82013-03-05 17:47:11 -08001235bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001236 mCurrentState.requestedTransparentRegion_legacy = transparent;
1237 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001238 setTransactionFlags(eTransactionNeeded);
1239 return true;
1240}
Ana Krulecc84d09b2019-11-02 23:10:29 +01001241
Mathias Agopian13127d82013-03-05 17:47:11 -08001242bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001243 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1244 if (mCurrentState.flags == newFlags) return false;
1245 mCurrentState.sequence++;
1246 mCurrentState.flags = newFlags;
1247 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001248 setTransactionFlags(eTransactionNeeded);
1249 return true;
1250}
Robert Carr99e27f02016-06-16 15:18:02 -07001251
chaviw214c89d2019-09-04 16:03:53 -07001252bool Layer::setCrop_legacy(const Rect& crop) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001253 if (mCurrentState.requestedCrop_legacy == crop) return false;
1254 mCurrentState.sequence++;
1255 mCurrentState.requestedCrop_legacy = crop;
chaviw214c89d2019-09-04 16:03:53 -07001256 mCurrentState.crop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001257
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001258 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001259 setTransactionFlags(eTransactionNeeded);
1260 return true;
1261}
Robert Carr8d5227b2017-03-16 15:41:03 -07001262
Robert Carrc3574f72016-03-24 12:19:32 -07001263bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001264 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001265 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001266 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001267 return true;
1268}
1269
Evan Roskyef876c92019-01-25 17:46:06 -08001270bool Layer::setMetadata(const LayerMetadata& data) {
1271 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001272 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001273 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001274 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001275 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001276}
1277
Mathias Agopian13127d82013-03-05 17:47:11 -08001278bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001279 if (mCurrentState.layerStack == layerStack) return false;
1280 mCurrentState.sequence++;
1281 mCurrentState.layerStack = layerStack;
1282 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001283 setTransactionFlags(eTransactionNeeded);
1284 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001285}
1286
Peiyong Linc502cb72019-03-01 15:00:23 -08001287bool Layer::setColorSpaceAgnostic(const bool agnostic) {
1288 if (mCurrentState.colorSpaceAgnostic == agnostic) {
1289 return false;
1290 }
1291 mCurrentState.sequence++;
1292 mCurrentState.colorSpaceAgnostic = agnostic;
1293 mCurrentState.modified = true;
1294 setTransactionFlags(eTransactionNeeded);
1295 return true;
1296}
1297
Ana Krulecc84d09b2019-11-02 23:10:29 +01001298bool Layer::setFrameRateSelectionPriority(int32_t priority) {
1299 if (mCurrentState.frameRateSelectionPriority == priority) return false;
1300 mCurrentState.frameRateSelectionPriority = priority;
1301 mCurrentState.sequence++;
1302 mCurrentState.modified = true;
1303 setTransactionFlags(eTransactionNeeded);
1304 return true;
1305}
1306
1307int32_t Layer::getFrameRateSelectionPriority() const {
1308 // Check if layer has priority set.
1309 if (mDrawingState.frameRateSelectionPriority != PRIORITY_UNSET) {
1310 return mDrawingState.frameRateSelectionPriority;
1311 }
1312 // If not, search whether its parents have it set.
1313 sp<Layer> parent = getParent();
1314 if (parent != nullptr) {
1315 return parent->getFrameRateSelectionPriority();
1316 }
1317
1318 return Layer::PRIORITY_UNSET;
1319}
1320
Robert Carr1f0a16a2016-10-24 16:27:39 -07001321uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001322 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001323 if (p == nullptr) {
1324 return getDrawingState().layerStack;
1325 }
1326 return p->getLayerStack();
1327}
1328
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001329bool Layer::setShadowRadius(float shadowRadius) {
1330 if (mCurrentState.shadowRadius == shadowRadius) {
1331 return false;
1332 }
1333
1334 mCurrentState.sequence++;
1335 mCurrentState.shadowRadius = shadowRadius;
1336 mCurrentState.modified = true;
1337 setTransactionFlags(eTransactionNeeded);
1338 return true;
1339}
1340
Ady Abraham60e42ea2020-03-09 19:17:31 -07001341void Layer::updateTreeHasFrameRateVote() {
1342 const auto traverseTree = [&](const LayerVector::Visitor& visitor) {
1343 auto parent = getParent();
1344 while (parent) {
1345 visitor(parent.get());
1346 parent = parent->getParent();
1347 }
1348
1349 traverse(LayerVector::StateSet::Current, visitor);
1350 };
1351
1352 // update parents and children about the vote
1353 // First traverse the tree and count how many layers has votes
1354 int layersWithVote = 0;
1355 traverseTree([&layersWithVote](Layer* layer) {
1356 if (layer->mCurrentState.frameRate.rate > 0 ||
1357 layer->mCurrentState.frameRate.type == FrameRateCompatibility::NoVote) {
1358 layersWithVote++;
1359 }
1360 });
1361
1362 // Now update the other layers
1363 bool transactionNeeded = false;
1364 traverseTree([layersWithVote, &transactionNeeded](Layer* layer) {
1365 if (layer->mCurrentState.treeHasFrameRateVote != layersWithVote > 0) {
1366 layer->mCurrentState.sequence++;
1367 layer->mCurrentState.treeHasFrameRateVote = layersWithVote > 0;
1368 layer->mCurrentState.modified = true;
1369 layer->setTransactionFlags(eTransactionNeeded);
1370 transactionNeeded = true;
1371 }
1372 });
1373
1374 if (transactionNeeded) {
1375 mFlinger->setTransactionFlags(eTraversalNeeded);
1376 }
1377}
1378
Ady Abraham71c437d2020-01-31 15:56:57 -08001379bool Layer::setFrameRate(FrameRate frameRate) {
Ana Krulec3803b8d2020-02-03 16:35:46 -08001380 if (!mFlinger->useFrameRateApi) {
1381 return false;
1382 }
Steven Thomas3172e202020-01-06 19:25:30 -08001383 if (mCurrentState.frameRate == frameRate) {
1384 return false;
1385 }
1386
Ady Abraham89089c92020-04-15 18:24:41 -07001387 // Activate the layer in Scheduler's LayerHistory
1388 mFlinger->mScheduler->recordLayerHistory(this, systemTime());
1389
Steven Thomas3172e202020-01-06 19:25:30 -08001390 mCurrentState.sequence++;
1391 mCurrentState.frameRate = frameRate;
1392 mCurrentState.modified = true;
Ady Abraham60e42ea2020-03-09 19:17:31 -07001393
1394 updateTreeHasFrameRateVote();
1395
Steven Thomas3172e202020-01-06 19:25:30 -08001396 setTransactionFlags(eTransactionNeeded);
1397 return true;
1398}
1399
Ady Abraham60e42ea2020-03-09 19:17:31 -07001400Layer::FrameRate Layer::getFrameRateForLayerTree() const {
1401 const auto frameRate = getDrawingState().frameRate;
1402 if (frameRate.rate > 0 || frameRate.type == FrameRateCompatibility::NoVote) {
1403 return frameRate;
1404 }
1405
1406 // This layer doesn't have a frame rate. If one of its ancestors or successors
1407 // have a vote, return a NoVote for ancestors/successors to set the vote
1408 if (getDrawingState().treeHasFrameRateVote) {
1409 return {0, FrameRateCompatibility::NoVote};
1410 }
1411
1412 return frameRate;
Steven Thomas3172e202020-01-06 19:25:30 -08001413}
1414
Marissa Wallf58c14b2018-07-24 10:50:43 -07001415void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Alec Mourie60041e2019-06-14 18:59:51 -07001416 ATRACE_CALL();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001417 mCurrentState.barrierLayer_legacy = barrierLayer;
1418 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001419 // We don't set eTransactionNeeded, because just receiving a deferral
1420 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001421 mCurrentState.modified = true;
1422 pushPendingState();
1423 mCurrentState.barrierLayer_legacy = nullptr;
1424 mCurrentState.frameNumber_legacy = 0;
1425 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001426}
1427
Marissa Wallf58c14b2018-07-24 10:50:43 -07001428void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001429 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001430 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001431}
1432
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001433// ----------------------------------------------------------------------------
1434// pageflip handling...
1435// ----------------------------------------------------------------------------
1436
Robert Carr1f0a16a2016-10-24 16:27:39 -07001437bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001438 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001439 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001440 if (parent != nullptr && parent->isHiddenByPolicy()) {
1441 return true;
1442 }
Robert Carr1c5481e2019-07-01 14:42:27 -07001443 if (usingRelativeZ(LayerVector::StateSet::Drawing)) {
1444 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
1445 if (zOrderRelativeOf != nullptr) {
1446 if (zOrderRelativeOf->isHiddenByPolicy()) {
1447 return true;
1448 }
1449 }
1450 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001451 return s.flags & layer_state_t::eLayerHidden;
1452}
1453
David Sodman41fdfc92017-11-06 16:09:56 -08001454uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001455 // TODO: should we do something special if mSecure is set?
1456 if (mProtectedByApp) {
1457 // need a hardware-protected path to external video sink
1458 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001459 }
Riley Andrews03414a12014-07-01 14:22:59 -07001460 if (mPotentialCursor) {
1461 usage |= GraphicBuffer::USAGE_CURSOR;
1462 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001463 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001464 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001465}
1466
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001467void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001468 uint32_t orientation = 0;
Vishnu Nair5eb3f062019-04-08 08:21:03 -07001469 // Disable setting transform hint if the debug flag is set.
1470 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001471 // The transform hint is used to improve performance, but we can
1472 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001473 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001474 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001475 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001476 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001477 orientation = 0;
1478 }
1479 }
David Sodmaneb085e02017-10-05 18:49:04 -07001480 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001481}
1482
Mathias Agopian13127d82013-03-05 17:47:11 -08001483// ----------------------------------------------------------------------------
1484// debugging
1485// ----------------------------------------------------------------------------
1486
Marissa Wall61c58622018-07-18 10:12:20 -07001487// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001488LayerDebugInfo Layer::getLayerDebugInfo() const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001489 using namespace std::string_literals;
1490
Kalle Raitaa099a242017-01-11 11:17:29 -08001491 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001492 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001493 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001494 sp<Layer> parent = getParent();
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001495 info.mParentName = parent ? parent->getName() : "none"s;
chaviw8a01fa42019-08-19 12:39:31 -07001496 info.mType = getType();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001497 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Lloyd Piquea2468662019-03-07 21:31:06 -08001498
1499 info.mVisibleRegion = debugGetVisibleRegionOnDefaultDisplay();
Kalle Raitaa099a242017-01-11 11:17:29 -08001500 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001501 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001502 info.mX = ds.active_legacy.transform.tx();
1503 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001504 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001505 info.mWidth = ds.active_legacy.w;
1506 info.mHeight = ds.active_legacy.h;
1507 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001508 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001509 info.mFlags = ds.flags;
1510 info.mPixelFormat = getPixelFormat();
chaviw4244e032019-09-04 11:27:49 -07001511 info.mDataSpace = static_cast<android_dataspace>(getDataSpace());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001512 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1513 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1514 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1515 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001516 {
chaviwd62d3062019-09-04 14:48:02 -07001517 sp<const GraphicBuffer> buffer = getBuffer();
David Sodman5b4cffc2017-11-23 13:20:29 -08001518 if (buffer != 0) {
1519 info.mActiveBufferWidth = buffer->getWidth();
1520 info.mActiveBufferHeight = buffer->getHeight();
1521 info.mActiveBufferStride = buffer->getStride();
1522 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001523 } else {
1524 info.mActiveBufferWidth = 0;
1525 info.mActiveBufferHeight = 0;
1526 info.mActiveBufferStride = 0;
1527 info.mActiveBufferFormat = 0;
1528 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001529 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001530 info.mNumQueuedFrames = getQueuedFrameCount();
1531 info.mRefreshPending = isBufferLatched();
1532 info.mIsOpaque = isOpaque(ds);
1533 info.mContentDirty = contentDirty;
1534 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001535}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001536
Yiwei Zhang5434a782018-12-05 18:06:32 -08001537void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001538 result.append("-------------------------------");
1539 result.append("-------------------------------");
1540 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001541 result.append(" Layer name\n");
1542 result.append(" Z | ");
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001543 result.append(" Window Type | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001544 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001545 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001546 result.append(" Disp Frame (LTRB) | ");
1547 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001548 result.append("-------------------------------");
1549 result.append("-------------------------------");
1550 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001551}
1552
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001553void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1554 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1555 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001556 return;
1557 }
1558
Yiwei Zhang5434a782018-12-05 18:06:32 -08001559 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001560 if (mName.length() > 77) {
1561 std::string shortened;
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001562 shortened.append(mName, 0, 36);
Dan Stozae22aec72016-08-01 13:20:59 -07001563 shortened.append("[...]");
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001564 shortened.append(mName, mName.length() - 36);
1565 name = std::move(shortened);
Dan Stozae22aec72016-08-01 13:20:59 -07001566 } else {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001567 name = mName;
Dan Stozae22aec72016-08-01 13:20:59 -07001568 }
1569
Yiwei Zhang5434a782018-12-05 18:06:32 -08001570 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001571
Alec Mourib416efd2018-09-06 21:01:59 +00001572 const State& layerState(getDrawingState());
Lloyd Piquede196652020-01-22 17:29:58 -08001573 const auto& outputLayerState = outputLayer->getState();
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001574
Chia-I Wu1e043612018-03-01 09:45:09 -08001575 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001576 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001577 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001578 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001579 }
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001580 StringAppendF(&result, " %10d | ", mWindowType);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001581 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
Lloyd Piquede196652020-01-22 17:29:58 -08001582 StringAppendF(&result, "%10s | ", toString(outputLayerState.bufferTransform).c_str());
1583 const Rect& frame = outputLayerState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001584 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Piquede196652020-01-22 17:29:58 -08001585 const FloatRect& crop = outputLayerState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001586 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1587 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001588
Yichi Chen6ca35192018-05-29 12:20:43 +08001589 result.append("- - - - - - - - - - - - - - - -");
1590 result.append("- - - - - - - - - - - - - - - -");
1591 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001592}
Dan Stozae22aec72016-08-01 13:20:59 -07001593
Yiwei Zhang5434a782018-12-05 18:06:32 -08001594void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001595 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001596}
1597
Svetoslavd85084b2014-03-20 10:28:31 -07001598void Layer::clearFrameStats() {
1599 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001600}
1601
Jamie Gennis6547ff42013-07-16 20:12:42 -07001602void Layer::logFrameStats() {
1603 mFrameTracker.logAndResetStats(mName);
1604}
1605
Svetoslavd85084b2014-03-20 10:28:31 -07001606void Layer::getFrameStats(FrameStats* outStats) const {
1607 mFrameTracker.getStats(outStats);
1608}
1609
Yiwei Zhang5434a782018-12-05 18:06:32 -08001610void Layer::dumpFrameEvents(std::string& result) {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001611 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().c_str(), getType(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001612 Mutex::Autolock lock(mFrameEventHistoryMutex);
1613 mFrameEventHistory.checkFencesForCompletion();
1614 mFrameEventHistory.dump(result);
1615}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001616
Vishnu Nair0f085c62019-08-30 08:49:12 -07001617void Layer::dumpCallingUidPid(std::string& result) const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001618 StringAppendF(&result, "Layer %s (%s) pid:%d uid:%d\n", getName().c_str(), getType(),
Vishnu Nair0f085c62019-08-30 08:49:12 -07001619 mCallingPid, mCallingUid);
1620}
1621
Brian Anderson5ea5e592016-12-01 16:54:33 -08001622void Layer::onDisconnect() {
1623 Mutex::Autolock lock(mFrameEventHistoryMutex);
1624 mFrameEventHistory.onDisconnect();
Yiwei Zhang1a88c402019-11-18 10:43:58 -08001625 const int32_t layerId = getSequence();
1626 mFlinger->mTimeStats->onDestroy(layerId);
1627 mFlinger->mFrameTracer->onDestroy(layerId);
Brian Anderson5ea5e592016-12-01 16:54:33 -08001628}
1629
Brian Anderson3890c392016-07-25 12:48:08 -07001630void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001631 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001632 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001633 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1634 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhang487340f2019-11-18 17:42:12 -08001635 mFlinger->mTimeStats->setAcquireFence(getSequence(), newTimestamps->frameNumber,
1636 newTimestamps->acquireFence);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001637 }
1638
Brian Andersond6927fb2016-07-23 23:37:30 -07001639 Mutex::Autolock lock(mFrameEventHistoryMutex);
1640 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001641 // If there are any unsignaled fences in the aquire timeline at this
1642 // point, the previously queued frame hasn't been latched yet. Go ahead
1643 // and try to get the signal time here so the syscall is taken out of
1644 // the main thread's critical path.
1645 mAcquireTimeline.updateSignalTimes();
1646 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001647 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001648 mFrameEventHistory.addQueue(*newTimestamps);
1649 }
1650
Brian Anderson3890c392016-07-25 12:48:08 -07001651 if (outDelta) {
1652 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001653 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001654}
Dan Stozae77c7662016-05-13 11:37:28 -07001655
Chia-I Wu98f1c102017-05-30 14:54:08 -07001656size_t Layer::getChildrenCount() const {
1657 size_t count = 0;
1658 for (const sp<Layer>& child : mCurrentChildren) {
1659 count += 1 + child->getChildrenCount();
1660 }
1661 return count;
1662}
1663
Robert Carr1f0a16a2016-10-24 16:27:39 -07001664void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001665 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001666 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001667
Robert Carr1f0a16a2016-10-24 16:27:39 -07001668 mCurrentChildren.add(layer);
1669 layer->setParent(this);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001670 updateTreeHasFrameRateVote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001671}
1672
1673ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001674 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001675 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001676
Robert Carr1323c952019-01-28 18:13:27 -08001677 layer->setParent(nullptr);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001678 const auto removeResult = mCurrentChildren.remove(layer);
1679
1680 updateTreeHasFrameRateVote();
1681 layer->updateTreeHasFrameRateVote();
1682
1683 return removeResult;
1684}
1685
1686void Layer::reparentChildren(const sp<Layer>& newParent) {
1687 if (attachChildren()) {
1688 setTransactionFlags(eTransactionNeeded);
1689 }
1690
1691 for (const sp<Layer>& child : mCurrentChildren) {
1692 newParent->addChild(child);
1693 }
1694 mCurrentChildren.clear();
1695 updateTreeHasFrameRateVote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001696}
1697
Robert Carr1db73f62016-12-21 12:58:51 -08001698bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1699 sp<Handle> handle = nullptr;
1700 sp<Layer> newParent = nullptr;
1701 if (newParentHandle == nullptr) {
1702 return false;
1703 }
1704 handle = static_cast<Handle*>(newParentHandle.get());
1705 newParent = handle->owner.promote();
1706 if (newParent == nullptr) {
1707 ALOGE("Unable to promote Layer handle");
1708 return false;
1709 }
1710
Ady Abraham60e42ea2020-03-09 19:17:31 -07001711 reparentChildren(newParent);
Robert Carr1db73f62016-12-21 12:58:51 -08001712
1713 return true;
1714}
1715
Robert Carr15eae092018-03-23 13:43:53 -07001716void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001717 for (const sp<Layer>& child : mDrawingChildren) {
1718 child->mDrawingParent = newParent;
Vishnu Nairc652ff82019-03-15 12:48:54 -07001719 child->computeBounds(newParent->mBounds,
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001720 newParent->getTransformWithScale(newParent->getBufferScaleTransform()),
1721 newParent->mEffectiveShadowRadius);
Robert Carr578038f2018-03-09 12:25:24 -08001722 }
1723}
1724
chaviwf1961f72017-09-18 16:41:07 -07001725bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001726 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001727
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001728 // While layers are detached, we allow most operations
1729 // and simply halt performing the actual transaction. However
1730 // for reparent != null we would enter the mRemovedFromCurrentState
1731 // state, regardless of whether doTransaction was called, and
1732 // so we need to prevent the update here.
1733 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001734 return false;
1735 }
1736
Robert Carr54cf5b12019-01-25 14:02:28 -08001737 sp<Layer> newParent;
1738 if (newParentHandle != nullptr) {
1739 auto handle = static_cast<Handle*>(newParentHandle.get());
1740 newParent = handle->owner.promote();
1741 if (newParent == nullptr) {
1742 ALOGE("Unable to promote Layer handle");
1743 return false;
1744 }
1745 if (newParent == this) {
1746 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1747 return false;
1748 }
1749 }
1750
chaviwf1961f72017-09-18 16:41:07 -07001751 sp<Layer> parent = getParent();
1752 if (parent != nullptr) {
1753 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001754 }
1755
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001756 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001757 newParent->addChild(this);
1758 if (!newParent->isRemovedFromCurrentState()) {
1759 addToCurrentState();
1760 } else {
1761 onRemovedFromCurrentState();
1762 }
1763
1764 if (mLayerDetached) {
1765 mLayerDetached = false;
1766 callSetTransactionFlags = true;
1767 }
1768 } else {
1769 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001770 }
1771
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001772 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001773 setTransactionFlags(eTransactionNeeded);
1774 }
chaviw06178942017-07-27 10:25:59 -07001775 return true;
1776}
1777
Robert Carr9524cb32017-02-13 11:32:32 -08001778bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001779 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001780 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001781 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001782 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001783 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001784 child->detachChildren();
chaviw43cb3cb2019-05-31 15:23:41 -07001785 child->removeRemoteSyncPoints();
Robert Carr9524cb32017-02-13 11:32:32 -08001786 }
Robert Carr7f619b22017-11-06 12:56:35 -08001787 }
Robert Carr9524cb32017-02-13 11:32:32 -08001788
1789 return true;
1790}
1791
chaviw5aedec92018-10-22 10:40:38 -07001792bool Layer::attachChildren() {
1793 bool changed = false;
1794 for (const sp<Layer>& child : mCurrentChildren) {
1795 sp<Client> parentClient = mClientRef.promote();
1796 sp<Client> client(child->mClientRef.promote());
1797 if (client != nullptr && parentClient != client) {
1798 if (child->mLayerDetached) {
1799 child->mLayerDetached = false;
1800 changed = true;
1801 }
1802 changed |= child->attachChildren();
1803 }
1804 }
1805
1806 return changed;
1807}
1808
Peiyong Lind3788632018-09-18 16:01:31 -07001809bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001810 static const mat4 identityMatrix = mat4();
1811
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001812 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001813 return false;
1814 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001815 ++mCurrentState.sequence;
1816 mCurrentState.colorTransform = matrix;
1817 mCurrentState.hasColorTransform = matrix != identityMatrix;
1818 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001819 setTransactionFlags(eTransactionNeeded);
1820 return true;
1821}
1822
chaviwf66724d2018-11-28 16:35:21 -08001823mat4 Layer::getColorTransform() const {
1824 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1825 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1826 colorTransform = parent->getColorTransform() * colorTransform;
1827 }
1828 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001829}
1830
1831bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001832 bool hasColorTransform = getDrawingState().hasColorTransform;
1833 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1834 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1835 }
1836 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001837}
1838
Chia-I Wu11481472018-05-04 10:43:19 -07001839bool Layer::isLegacyDataSpace() const {
1840 // return true when no higher bits are set
chaviw4244e032019-09-04 11:27:49 -07001841 return !(getDataSpace() &
1842 (ui::Dataspace::STANDARD_MASK | ui::Dataspace::TRANSFER_MASK |
1843 ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001844}
1845
Robert Carr1f0a16a2016-10-24 16:27:39 -07001846void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001847 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001848}
1849
chaviw301b1d82019-11-06 13:15:09 -08001850int32_t Layer::getZ(LayerVector::StateSet stateSet) const {
1851 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1852 const State& state = useDrawing ? mDrawingState : mCurrentState;
1853 return state.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001854}
1855
Robert Carr1c5481e2019-07-01 14:42:27 -07001856bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) const {
Robert Carr29abff82017-12-04 13:51:20 -08001857 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001858 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviwe5ac40f2019-09-24 16:36:55 -07001859 return state.isRelativeOf;
Robert Carr29abff82017-12-04 13:51:20 -08001860}
1861
David Sodman41fdfc92017-11-06 16:09:56 -08001862__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001863 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001864 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1865 "makeTraversalList received invalid stateSet");
1866 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1867 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001868 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001869
Robert Carr29abff82017-12-04 13:51:20 -08001870 if (state.zOrderRelatives.size() == 0) {
1871 *outSkipRelativeZUsers = true;
1872 return children;
1873 }
1874
chaviwfd462612018-05-31 16:11:27 -07001875 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001876 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001877 sp<Layer> strongRelative = weakRelative.promote();
1878 if (strongRelative != nullptr) {
1879 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001880 }
1881 }
1882
Dan Stoza412903f2017-04-27 13:42:17 -07001883 for (const sp<Layer>& child : children) {
chaviwe5ac40f2019-09-24 16:36:55 -07001884 if (child->usingRelativeZ(stateSet)) {
Robert Carr503c7042017-09-27 15:06:08 -07001885 continue;
1886 }
Robert Carrdb66e622017-04-10 16:55:57 -07001887 traverse.add(child);
1888 }
1889
1890 return traverse;
1891}
1892
Robert Carr1f0a16a2016-10-24 16:27:39 -07001893/**
Robert Carrdb66e622017-04-10 16:55:57 -07001894 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001895 */
Dan Stoza412903f2017-04-27 13:42:17 -07001896void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001897 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1898 // produce a new list for traversing, including our relatives, and not including our children
1899 // who are relatives of another surface. In the case that there are no relative Z,
1900 // makeTraversalList returns our children directly to avoid significant overhead.
1901 // However in this case we need to take the responsibility for filtering children which
1902 // are relatives of another surface here.
1903 bool skipRelativeZUsers = false;
1904 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001905
Robert Carr1f0a16a2016-10-24 16:27:39 -07001906 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001907 for (; i < list.size(); i++) {
1908 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001909 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1910 continue;
1911 }
1912
chaviw301b1d82019-11-06 13:15:09 -08001913 if (relative->getZ(stateSet) >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001914 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001915 }
Dan Stoza412903f2017-04-27 13:42:17 -07001916 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001917 }
Robert Carr29abff82017-12-04 13:51:20 -08001918
Dan Stoza412903f2017-04-27 13:42:17 -07001919 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001920 for (; i < list.size(); i++) {
1921 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001922
Robert Carr29abff82017-12-04 13:51:20 -08001923 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1924 continue;
1925 }
Dan Stoza412903f2017-04-27 13:42:17 -07001926 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001927 }
1928}
1929
1930/**
Robert Carrdb66e622017-04-10 16:55:57 -07001931 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001932 */
Dan Stoza412903f2017-04-27 13:42:17 -07001933void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1934 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001935 // See traverseInZOrder for documentation.
1936 bool skipRelativeZUsers = false;
1937 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001938
Robert Carr1f0a16a2016-10-24 16:27:39 -07001939 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001940 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001941 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001942
1943 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1944 continue;
1945 }
1946
chaviw301b1d82019-11-06 13:15:09 -08001947 if (relative->getZ(stateSet) < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001948 break;
1949 }
Dan Stoza412903f2017-04-27 13:42:17 -07001950 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001951 }
Dan Stoza412903f2017-04-27 13:42:17 -07001952 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001953 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001954 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001955
1956 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1957 continue;
1958 }
1959
Dan Stoza412903f2017-04-27 13:42:17 -07001960 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001961 }
1962}
1963
Edgar Arriaga844fa672020-01-16 14:21:42 -08001964void Layer::traverse(LayerVector::StateSet state, const LayerVector::Visitor& visitor) {
1965 visitor(this);
1966 const LayerVector& children =
1967 state == LayerVector::StateSet::Drawing ? mDrawingChildren : mCurrentChildren;
1968 for (const sp<Layer>& child : children) {
1969 child->traverse(state, visitor);
1970 }
1971}
1972
chaviw4b129c22018-04-09 16:19:43 -07001973LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1974 const std::vector<Layer*>& layersInTree) {
1975 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1976 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001977 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1978 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001979 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001980
chaviwfd462612018-05-31 16:11:27 -07001981 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001982 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1983 sp<Layer> strongRelative = weakRelative.promote();
1984 // Only add relative layers that are also descendents of the top most parent of the tree.
1985 // If a relative layer is not a descendent, then it should be ignored.
1986 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1987 traverse.add(strongRelative);
1988 }
1989 }
1990
1991 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001992 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001993 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1994 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1995 // the child here since it won't be added later as a relative.
1996 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1997 childState.zOrderRelativeOf.promote().get())) {
1998 continue;
1999 }
2000 traverse.add(child);
2001 }
2002
2003 return traverse;
2004}
2005
2006void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
2007 LayerVector::StateSet stateSet,
2008 const LayerVector::Visitor& visitor) {
2009 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07002010
2011 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07002012 for (; i < list.size(); i++) {
2013 const auto& relative = list[i];
chaviw301b1d82019-11-06 13:15:09 -08002014 if (relative->getZ(stateSet) >= 0) {
chaviwa76b2712017-09-20 12:02:26 -07002015 break;
2016 }
chaviw4b129c22018-04-09 16:19:43 -07002017 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07002018 }
chaviw4b129c22018-04-09 16:19:43 -07002019
chaviwa76b2712017-09-20 12:02:26 -07002020 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07002021 for (; i < list.size(); i++) {
2022 const auto& relative = list[i];
2023 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07002024 }
2025}
2026
chaviw4b129c22018-04-09 16:19:43 -07002027std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
2028 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2029 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
2030
2031 std::vector<Layer*> layersInTree = {this};
2032 for (size_t i = 0; i < children.size(); i++) {
2033 const auto& child = children[i];
2034 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
2035 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
2036 }
2037
2038 return layersInTree;
2039}
2040
2041void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
2042 const LayerVector::Visitor& visitor) {
2043 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
2044 std::sort(layersInTree.begin(), layersInTree.end());
2045 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
2046}
2047
Peiyong Linefefaac2018-08-17 12:27:51 -07002048ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08002049 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002050}
2051
chaviw13fdc492017-06-27 12:40:18 -07002052half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08002053 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002054
chaviw13fdc492017-06-27 12:40:18 -07002055 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
2056 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07002057}
Robert Carr6452f122017-03-21 10:41:29 -07002058
chaviw13fdc492017-06-27 12:40:18 -07002059half4 Layer::getColor() const {
2060 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002061 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07002062}
Robert Carr6452f122017-03-21 10:41:29 -07002063
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08002064int32_t Layer::getBackgroundBlurRadius() const {
2065 return getDrawingState().backgroundBlurRadius;
2066}
2067
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002068Layer::RoundedCornerState Layer::getRoundedCornerState() const {
2069 const auto& p = mDrawingParent.promote();
2070 if (p != nullptr) {
Peiyong Lin27016a92019-03-29 17:36:08 +00002071 RoundedCornerState parentState = p->getRoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002072 if (parentState.radius > 0) {
2073 ui::Transform t = getActiveTransform(getDrawingState());
2074 t = t.inverse();
2075 parentState.cropRect = t.transform(parentState.cropRect);
2076 // The rounded corners shader only accepts 1 corner radius for performance reasons,
2077 // but a transform matrix can define horizontal and vertical scales.
2078 // Let's take the average between both of them and pass into the shader, practically we
2079 // never do this type of transformation on windows anyway.
2080 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
2081 return parentState;
2082 }
2083 }
2084 const float radius = getDrawingState().cornerRadius;
Peiyong Linb9ff23e2019-04-08 11:04:59 -07002085 return radius > 0 && getCrop(getDrawingState()).isValid()
2086 ? RoundedCornerState(getCrop(getDrawingState()).toFloatRect(), radius)
2087 : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002088}
2089
Vishnu Nair08f6eae2019-11-26 14:01:39 -08002090renderengine::ShadowSettings Layer::getShadowSettings(const Rect& viewport) const {
2091 renderengine::ShadowSettings state = mFlinger->mDrawingState.globalShadowSettings;
2092
2093 // Shift the spot light x-position to the middle of the display and then
2094 // offset it by casting layer's screen pos.
2095 state.lightPos.x = (viewport.width() / 2.f) - mScreenBounds.left;
2096 state.lightPos.y -= mScreenBounds.top;
2097
2098 state.length = mEffectiveShadowRadius;
2099 return state;
2100}
2101
Robert Carr1f0a16a2016-10-24 16:27:39 -07002102void Layer::commitChildList() {
2103 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
2104 const auto& child = mCurrentChildren[i];
2105 child->commitChildList();
2106 }
2107 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07002108 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002109}
2110
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002111static wp<Layer> extractLayerFromBinder(const wp<IBinder>& weakBinderHandle) {
2112 if (weakBinderHandle == nullptr) {
2113 return nullptr;
2114 }
2115 sp<IBinder> binderHandle = weakBinderHandle.promote();
2116 if (binderHandle == nullptr) {
2117 return nullptr;
2118 }
2119 sp<Layer::Handle> handle = static_cast<Layer::Handle*>(binderHandle.get());
2120 if (handle == nullptr) {
2121 return nullptr;
2122 }
2123 return handle->owner;
2124}
2125
Robert Carr720e5062018-07-30 17:45:14 -07002126void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002127 mCurrentState.inputInfo = info;
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002128 mCurrentState.touchableRegionCrop = extractLayerFromBinder(info.touchableRegionCropHandle);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002129 mCurrentState.modified = true;
2130 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07002131 setTransactionFlags(eTransactionNeeded);
2132}
2133
Alec Mouri6b9e9912020-01-21 10:50:24 -08002134LayerProto* Layer::writeToProto(LayersProto& layersProto, uint32_t traceFlags,
2135 const sp<const DisplayDevice>& device) const {
chaviw08f3cb22020-01-13 13:17:21 -08002136 LayerProto* layerProto = layersProto.add_layers();
2137 writeToProtoDrawingState(layerProto, traceFlags);
2138 writeToProtoCommonState(layerProto, LayerVector::StateSet::Drawing, traceFlags);
2139
Vishnu Nair60db8c02020-04-02 11:55:16 -07002140 if (traceFlags & SurfaceTracing::TRACE_COMPOSITION) {
2141 // Only populate for the primary display.
2142 if (device) {
2143 const Hwc2::IComposerClient::Composition compositionType = getCompositionType(device);
2144 layerProto->set_hwc_composition_type(static_cast<HwcCompositionType>(compositionType));
2145 }
Alec Mouri6b9e9912020-01-21 10:50:24 -08002146 }
2147
chaviw08f3cb22020-01-13 13:17:21 -08002148 for (const sp<Layer>& layer : mDrawingChildren) {
Alec Mouri6b9e9912020-01-21 10:50:24 -08002149 layer->writeToProto(layersProto, traceFlags, device);
chaviw08f3cb22020-01-13 13:17:21 -08002150 }
chaviw6d89e2d2020-01-14 14:42:01 -08002151
2152 return layerProto;
chaviw08f3cb22020-01-13 13:17:21 -08002153}
2154
Vishnu Nair8406fd72019-07-30 11:29:31 -07002155void Layer::writeToProtoDrawingState(LayerProto* layerInfo, uint32_t traceFlags) const {
2156 ui::Transform transform = getTransform();
2157
2158 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
2159 for (const auto& pendingState : mPendingStatesSnapshot) {
2160 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
2161 if (barrierLayer != nullptr) {
2162 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
2163 barrierLayerProto->set_id(barrierLayer->sequence);
2164 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
2165 }
2166 }
2167
chaviwd62d3062019-09-04 14:48:02 -07002168 auto buffer = getBuffer();
Vishnu Nair8406fd72019-07-30 11:29:31 -07002169 if (buffer != nullptr) {
2170 LayerProtoHelper::writeToProto(buffer,
2171 [&]() { return layerInfo->mutable_active_buffer(); });
chaviw4244e032019-09-04 11:27:49 -07002172 LayerProtoHelper::writeToProto(ui::Transform(getBufferTransform()),
Vishnu Nair8406fd72019-07-30 11:29:31 -07002173 layerInfo->mutable_buffer_transform());
2174 }
2175 layerInfo->set_invalidate(contentDirty);
2176 layerInfo->set_is_protected(isProtected());
chaviw4244e032019-09-04 11:27:49 -07002177 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(getDataSpace())));
Vishnu Nair8406fd72019-07-30 11:29:31 -07002178 layerInfo->set_queued_frames(getQueuedFrameCount());
2179 layerInfo->set_refresh_pending(isBufferLatched());
2180 layerInfo->set_curr_frame(mCurrentFrameNumber);
2181 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
2182
2183 layerInfo->set_corner_radius(getRoundedCornerState().radius);
2184 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
2185 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
2186 [&]() { return layerInfo->mutable_position(); });
2187 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
Vishnu Nair60db8c02020-04-02 11:55:16 -07002188 if (traceFlags & SurfaceTracing::TRACE_COMPOSITION) {
2189 LayerProtoHelper::writeToProto(debugGetVisibleRegionOnDefaultDisplay(),
2190 [&]() { return layerInfo->mutable_visible_region(); });
2191 }
Vishnu Nair8406fd72019-07-30 11:29:31 -07002192 LayerProtoHelper::writeToProto(surfaceDamageRegion,
2193 [&]() { return layerInfo->mutable_damage_region(); });
chaviwddeae262020-01-06 10:31:23 -08002194
2195 if (hasColorTransform()) {
2196 LayerProtoHelper::writeToProto(getColorTransform(),
2197 layerInfo->mutable_color_transform());
2198 }
Vishnu Nair8406fd72019-07-30 11:29:31 -07002199 }
2200
Vishnu Nair60db8c02020-04-02 11:55:16 -07002201 LayerProtoHelper::writeToProto(mSourceBounds,
2202 [&]() { return layerInfo->mutable_source_bounds(); });
2203 LayerProtoHelper::writeToProto(mScreenBounds,
2204 [&]() { return layerInfo->mutable_screen_bounds(); });
2205 LayerProtoHelper::writeToProto(getRoundedCornerState().cropRect,
2206 [&]() { return layerInfo->mutable_corner_radius_crop(); });
2207 layerInfo->set_shadow_radius(mEffectiveShadowRadius);
Vishnu Nair8406fd72019-07-30 11:29:31 -07002208}
2209
2210void Layer::writeToProtoCommonState(LayerProto* layerInfo, LayerVector::StateSet stateSet,
2211 uint32_t traceFlags) const {
chaviw1d044282017-09-27 12:19:28 -07002212 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2213 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002214 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07002215
Peiyong Linefefaac2018-08-17 12:27:51 -07002216 ui::Transform requestedTransform = state.active_legacy.transform;
chaviw1d044282017-09-27 12:19:28 -07002217
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002218 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
2219 layerInfo->set_id(sequence);
2220 layerInfo->set_name(getName().c_str());
chaviw8a01fa42019-08-19 12:39:31 -07002221 layerInfo->set_type(getType());
chaviw1d044282017-09-27 12:19:28 -07002222
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002223 for (const auto& child : children) {
2224 layerInfo->add_children(child->sequence);
chaviw1d044282017-09-27 12:19:28 -07002225 }
chaviw1d044282017-09-27 12:19:28 -07002226
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002227 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
2228 sp<Layer> strongRelative = weakRelative.promote();
2229 if (strongRelative != nullptr) {
2230 layerInfo->add_relatives(strongRelative->sequence);
2231 }
chaviwadc40c22018-07-10 16:57:27 -07002232 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002233
2234 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
2235 [&]() { return layerInfo->mutable_transparent_region(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002236
2237 layerInfo->set_layer_stack(getLayerStack());
2238 layerInfo->set_z(state.z);
2239
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002240 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(),
2241 [&]() {
2242 return layerInfo->mutable_requested_position();
2243 });
2244
2245 LayerProtoHelper::writeSizeToProto(state.active_legacy.w, state.active_legacy.h,
2246 [&]() { return layerInfo->mutable_size(); });
2247
2248 LayerProtoHelper::writeToProto(state.crop_legacy,
2249 [&]() { return layerInfo->mutable_crop(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002250
2251 layerInfo->set_is_opaque(isOpaque(state));
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002252
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002253
2254 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
2255 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
2256 LayerProtoHelper::writeToProto(state.color,
2257 [&]() { return layerInfo->mutable_requested_color(); });
2258 layerInfo->set_flags(state.flags);
2259
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002260 LayerProtoHelper::writeToProto(requestedTransform,
2261 layerInfo->mutable_requested_transform());
2262
2263 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
2264 if (parent != nullptr) {
2265 layerInfo->set_parent(parent->sequence);
2266 } else {
2267 layerInfo->set_parent(-1);
2268 }
2269
2270 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2271 if (zOrderRelativeOf != nullptr) {
2272 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
2273 } else {
2274 layerInfo->set_z_order_relative_of(-1);
2275 }
chaviw08f3cb22020-01-13 13:17:21 -08002276
2277 layerInfo->set_is_relative_of(state.isRelativeOf);
chaviwadc40c22018-07-10 16:57:27 -07002278 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002279
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002280 if (traceFlags & SurfaceTracing::TRACE_INPUT) {
2281 LayerProtoHelper::writeToProto(state.inputInfo, state.touchableRegionCrop,
2282 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002283 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002284
2285 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
2286 auto protoMap = layerInfo->mutable_metadata();
2287 for (const auto& entry : state.metadata.mMap) {
2288 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
2289 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002290 }
chaviw1d044282017-09-27 12:19:28 -07002291}
2292
Robert Carr2e102c92018-10-23 12:11:15 -07002293bool Layer::isRemovedFromCurrentState() const {
2294 return mRemovedFromCurrentState;
2295}
2296
Arthur Hungd20b2702019-01-14 18:16:16 +08002297InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002298 InputWindowInfo info = mDrawingState.inputInfo;
chaviwaf87b3e2019-10-01 16:59:28 -07002299 info.id = sequence;
Robert Carr720e5062018-07-30 17:45:14 -07002300
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002301 if (info.displayId == ADISPLAY_ID_NONE) {
chaviwb15ea8a2019-09-03 12:40:22 -07002302 info.displayId = getLayerStack();
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002303 }
2304
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002305 ui::Transform t = getTransform();
2306 const float xScale = t.sx();
2307 const float yScale = t.sy();
Ady Abraham282f1d72019-07-24 18:05:56 -07002308 int32_t xSurfaceInset = info.surfaceInset;
2309 int32_t ySurfaceInset = info.surfaceInset;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002310 if (xScale != 1.0f || yScale != 1.0f) {
Ady Abraham282f1d72019-07-24 18:05:56 -07002311 info.windowXScale *= (xScale != 0.0f) ? 1.0f / xScale : 0.0f;
2312 info.windowYScale *= (yScale != 0.0f) ? 1.0f / yScale : 0.0f;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002313 info.touchableRegion.scaleSelf(xScale, yScale);
Ady Abraham282f1d72019-07-24 18:05:56 -07002314 xSurfaceInset = std::round(xSurfaceInset * xScale);
2315 ySurfaceInset = std::round(ySurfaceInset * yScale);
Riddle Hsu39d4aa52018-11-30 20:46:53 +08002316 }
Robert Carre07e1032018-11-26 12:55:53 -08002317
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002318 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002319 // If this is a portal window, set the touchableRegion to the layerBounds.
2320 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
2321 ? getBufferSize(getDrawingState())
2322 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002323 if (!layerBounds.isValid()) {
2324 layerBounds = getCroppedBufferSize(getDrawingState());
2325 }
Vishnu Nair8033a492018-12-05 07:27:23 -08002326 layerBounds = t.transform(layerBounds);
Ady Abraham282f1d72019-07-24 18:05:56 -07002327
2328 // clamp inset to layer bounds
2329 xSurfaceInset = (xSurfaceInset >= 0) ? std::min(xSurfaceInset, layerBounds.getWidth() / 2) : 0;
2330 ySurfaceInset = (ySurfaceInset >= 0) ? std::min(ySurfaceInset, layerBounds.getHeight() / 2) : 0;
2331
Arthur Hung118b1142019-05-08 21:25:59 +08002332 layerBounds.inset(xSurfaceInset, ySurfaceInset, xSurfaceInset, ySurfaceInset);
Vishnu Nair8033a492018-12-05 07:27:23 -08002333
Arthur Hungd20b2702019-01-14 18:16:16 +08002334 // Input coordinate should match the layer bounds.
2335 info.frameLeft = layerBounds.left;
2336 info.frameTop = layerBounds.top;
2337 info.frameRight = layerBounds.right;
2338 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08002339
2340 // Position the touchable region relative to frame screen location and restrict it to frame
2341 // bounds.
2342 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08002343 info.visible = canReceiveInput();
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002344
2345 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2346 if (info.replaceTouchableRegionWithCrop) {
2347 if (cropLayer == nullptr) {
2348 info.touchableRegion = Region(Rect{mScreenBounds});
2349 } else {
2350 info.touchableRegion = Region(Rect{cropLayer->mScreenBounds});
2351 }
2352 } else if (cropLayer != nullptr) {
2353 info.touchableRegion = info.touchableRegion.intersect(Rect{cropLayer->mScreenBounds});
2354 }
2355
chaviwaf87b3e2019-10-01 16:59:28 -07002356 // If the layer is a clone, we need to crop the input region to cloned root to prevent
2357 // touches from going outside the cloned area.
2358 if (isClone()) {
2359 sp<Layer> clonedRoot = getClonedRoot();
2360 if (clonedRoot != nullptr) {
2361 Rect rect(clonedRoot->mScreenBounds);
2362 info.touchableRegion = info.touchableRegion.intersect(rect);
2363 }
2364 }
2365
Robert Carr720e5062018-07-30 17:45:14 -07002366 return info;
2367}
2368
chaviwaf87b3e2019-10-01 16:59:28 -07002369sp<Layer> Layer::getClonedRoot() {
2370 if (mClonedChild != nullptr) {
2371 return this;
2372 }
2373 if (mDrawingParent == nullptr || mDrawingParent.promote() == nullptr) {
2374 return nullptr;
2375 }
2376 return mDrawingParent.promote()->getClonedRoot();
2377}
2378
Robert Carr720e5062018-07-30 17:45:14 -07002379bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002380 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002381}
2382
chaviw3e727cd2019-01-31 13:41:05 -08002383bool Layer::canReceiveInput() const {
Vishnu Nair82353e92019-09-12 12:38:47 -07002384 return !isHiddenByPolicy();
chaviw3e727cd2019-01-31 13:41:05 -08002385}
2386
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002387compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
2388 const sp<const DisplayDevice>& display) const {
Lloyd Piquede196652020-01-22 17:29:58 -08002389 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionEngineLayerFE());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002390}
2391
Lloyd Piquea2468662019-03-07 21:31:06 -08002392Region Layer::debugGetVisibleRegionOnDefaultDisplay() const {
2393 sp<DisplayDevice> displayDevice = mFlinger->getDefaultDisplayDeviceLocked();
2394 if (displayDevice == nullptr) {
2395 return {};
2396 }
2397
2398 auto outputLayer = findOutputLayerForDisplay(displayDevice);
2399 if (outputLayer == nullptr) {
2400 return {};
2401 }
2402
2403 return outputLayer->getState().visibleRegion;
2404}
2405
chaviwb4c6e582019-08-16 14:35:07 -07002406void Layer::setInitialValuesForClone(const sp<Layer>& clonedFrom) {
2407 // copy drawing state from cloned layer
2408 mDrawingState = clonedFrom->mDrawingState;
2409 mClonedFrom = clonedFrom;
chaviwb4c6e582019-08-16 14:35:07 -07002410}
chaviw74b03172019-08-19 11:09:03 -07002411
2412void Layer::updateMirrorInfo() {
2413 if (mClonedChild == nullptr || !mClonedChild->isClonedFromAlive()) {
2414 // If mClonedChild is null, there is nothing to mirror. If isClonedFromAlive returns false,
2415 // it means that there is a clone, but the layer it was cloned from has been destroyed. In
2416 // that case, we want to delete the reference to the clone since we want it to get
2417 // destroyed. The root, this layer, will still be around since the client can continue
2418 // to hold a reference, but no cloned layers will be displayed.
2419 mClonedChild = nullptr;
2420 return;
2421 }
2422
2423 std::map<sp<Layer>, sp<Layer>> clonedLayersMap;
2424 // If the real layer exists and is in current state, add the clone as a child of the root.
2425 // There's no need to remove from drawingState when the layer is offscreen since currentState is
2426 // copied to drawingState for the root layer. So the clonedChild is always removed from
2427 // drawingState and then needs to be added back each traversal.
2428 if (!mClonedChild->getClonedFrom()->isRemovedFromCurrentState()) {
2429 addChildToDrawing(mClonedChild);
2430 }
2431
2432 mClonedChild->updateClonedDrawingState(clonedLayersMap);
2433 mClonedChild->updateClonedChildren(this, clonedLayersMap);
2434 mClonedChild->updateClonedRelatives(clonedLayersMap);
2435}
2436
2437void Layer::updateClonedDrawingState(std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2438 // If the layer the clone was cloned from is alive, copy the content of the drawingState
2439 // to the clone. If the real layer is no longer alive, continue traversing the children
2440 // since we may be able to pull out other children that are still alive.
2441 if (isClonedFromAlive()) {
2442 sp<Layer> clonedFrom = getClonedFrom();
2443 mDrawingState = clonedFrom->mDrawingState;
chaviw74b03172019-08-19 11:09:03 -07002444 clonedLayersMap.emplace(clonedFrom, this);
2445 }
2446
2447 // The clone layer may have children in drawingState since they may have been created and
2448 // added from a previous request to updateMirorInfo. This is to ensure we don't recreate clones
2449 // that already exist, since we can just re-use them.
2450 // The drawingChildren will not get overwritten by the currentChildren since the clones are
2451 // not updated in the regular traversal. They are skipped since the root will lose the
2452 // reference to them when it copies its currentChildren to drawing.
2453 for (sp<Layer>& child : mDrawingChildren) {
2454 child->updateClonedDrawingState(clonedLayersMap);
2455 }
2456}
2457
2458void Layer::updateClonedChildren(const sp<Layer>& mirrorRoot,
2459 std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2460 mDrawingChildren.clear();
2461
2462 if (!isClonedFromAlive()) {
2463 return;
2464 }
2465
2466 sp<Layer> clonedFrom = getClonedFrom();
2467 for (sp<Layer>& child : clonedFrom->mDrawingChildren) {
2468 if (child == mirrorRoot) {
2469 // This is to avoid cyclical mirroring.
2470 continue;
2471 }
2472 sp<Layer> clonedChild = clonedLayersMap[child];
2473 if (clonedChild == nullptr) {
2474 clonedChild = child->createClone();
2475 clonedLayersMap[child] = clonedChild;
2476 }
2477 addChildToDrawing(clonedChild);
2478 clonedChild->updateClonedChildren(mirrorRoot, clonedLayersMap);
2479 }
2480}
2481
chaviwaf87b3e2019-10-01 16:59:28 -07002482void Layer::updateClonedInputInfo(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2483 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2484 if (cropLayer != nullptr) {
2485 if (clonedLayersMap.count(cropLayer) == 0) {
2486 // Real layer had a crop layer but it's not in the cloned hierarchy. Just set to
2487 // self as crop layer to avoid going outside bounds.
2488 mDrawingState.touchableRegionCrop = this;
2489 } else {
2490 const sp<Layer>& clonedCropLayer = clonedLayersMap.at(cropLayer);
2491 mDrawingState.touchableRegionCrop = clonedCropLayer;
2492 }
2493 }
2494 // Cloned layers shouldn't handle watch outside since their z order is not determined by
2495 // WM or the client.
2496 mDrawingState.inputInfo.layoutParamsFlags &= ~InputWindowInfo::FLAG_WATCH_OUTSIDE_TOUCH;
2497}
2498
2499void Layer::updateClonedRelatives(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
chaviw74b03172019-08-19 11:09:03 -07002500 mDrawingState.zOrderRelativeOf = nullptr;
2501 mDrawingState.zOrderRelatives.clear();
2502
2503 if (!isClonedFromAlive()) {
2504 return;
2505 }
2506
chaviwaf87b3e2019-10-01 16:59:28 -07002507 const sp<Layer>& clonedFrom = getClonedFrom();
chaviw74b03172019-08-19 11:09:03 -07002508 for (wp<Layer>& relativeWeak : clonedFrom->mDrawingState.zOrderRelatives) {
chaviwaf87b3e2019-10-01 16:59:28 -07002509 const sp<Layer>& relative = relativeWeak.promote();
2510 if (clonedLayersMap.count(relative) > 0) {
2511 auto& clonedRelative = clonedLayersMap.at(relative);
chaviw74b03172019-08-19 11:09:03 -07002512 mDrawingState.zOrderRelatives.add(clonedRelative);
2513 }
2514 }
2515
2516 // Check if the relativeLayer for the real layer is part of the cloned hierarchy.
2517 // It's possible that the layer it's relative to is outside the requested cloned hierarchy.
2518 // In that case, we treat the layer as if the relativeOf has been removed. This way, it will
2519 // still traverse the children, but the layer with the missing relativeOf will not be shown
2520 // on screen.
chaviwaf87b3e2019-10-01 16:59:28 -07002521 const sp<Layer>& relativeOf = clonedFrom->mDrawingState.zOrderRelativeOf.promote();
2522 if (clonedLayersMap.count(relativeOf) > 0) {
2523 const sp<Layer>& clonedRelativeOf = clonedLayersMap.at(relativeOf);
chaviw74b03172019-08-19 11:09:03 -07002524 mDrawingState.zOrderRelativeOf = clonedRelativeOf;
2525 }
2526
chaviwaf87b3e2019-10-01 16:59:28 -07002527 updateClonedInputInfo(clonedLayersMap);
2528
chaviw74b03172019-08-19 11:09:03 -07002529 for (sp<Layer>& child : mDrawingChildren) {
2530 child->updateClonedRelatives(clonedLayersMap);
2531 }
2532}
2533
2534void Layer::addChildToDrawing(const sp<Layer>& layer) {
2535 mDrawingChildren.add(layer);
2536 layer->mDrawingParent = this;
2537}
2538
Steven Thomas62a4cf82020-01-31 12:04:03 -08002539Layer::FrameRateCompatibility Layer::FrameRate::convertCompatibility(int8_t compatibility) {
2540 switch (compatibility) {
2541 case ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT:
2542 return FrameRateCompatibility::Default;
2543 case ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE:
2544 return FrameRateCompatibility::ExactOrMultiple;
2545 default:
2546 LOG_ALWAYS_FATAL("Invalid frame rate compatibility value %d", compatibility);
2547 return FrameRateCompatibility::Default;
2548 }
2549}
2550
Robert Carra1257842020-01-31 13:48:28 -08002551bool Layer::maybeDirtyInput() {
2552 // No sense redirtying input.
2553 if (mFlinger->inputDirty()) return true;
2554
2555 if (hasInput()) {
2556 mFlinger->dirtyInput();
2557 return true;
2558 }
2559
2560 // If a child or relative dirties the input, no sense continuing to traverse
2561 // so we return early and halt the recursion. We traverse ourselves instead
2562 // of using traverse() so we can implement this early halt.
2563 for (const sp<Layer>& child : mDrawingChildren) {
2564 if (child->maybeDirtyInput()) {
2565 return true;
2566 }
2567 }
2568 for (const wp<Layer>& weakRelative : mDrawingState.zOrderRelatives) {
2569 sp<Layer> relative = weakRelative.promote();
2570 if (relative && relative->maybeDirtyInput()) {
2571 return true;
2572 }
2573 }
2574 return false;
2575}
2576
Mathias Agopian13127d82013-03-05 17:47:11 -08002577// ---------------------------------------------------------------------------
2578
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002579}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002580
2581#if defined(__gl_h_)
2582#error "don't include gl/gl.h in this file"
2583#endif
2584
2585#if defined(__gl2_h_)
2586#error "don't include gl2/gl2.h in this file"
2587#endif
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -08002588
2589// TODO(b/129481165): remove the #pragma below and fix conversion issues
2590#pragma clang diagnostic pop // ignored "-Wconversion"