blob: a83d0a7248c8b19a8979c5690357cdb38975f501 [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
1387 mCurrentState.sequence++;
1388 mCurrentState.frameRate = frameRate;
1389 mCurrentState.modified = true;
Ady Abraham60e42ea2020-03-09 19:17:31 -07001390
1391 updateTreeHasFrameRateVote();
1392
Steven Thomas3172e202020-01-06 19:25:30 -08001393 setTransactionFlags(eTransactionNeeded);
1394 return true;
1395}
1396
Ady Abraham60e42ea2020-03-09 19:17:31 -07001397Layer::FrameRate Layer::getFrameRateForLayerTree() const {
1398 const auto frameRate = getDrawingState().frameRate;
1399 if (frameRate.rate > 0 || frameRate.type == FrameRateCompatibility::NoVote) {
1400 return frameRate;
1401 }
1402
1403 // This layer doesn't have a frame rate. If one of its ancestors or successors
1404 // have a vote, return a NoVote for ancestors/successors to set the vote
1405 if (getDrawingState().treeHasFrameRateVote) {
1406 return {0, FrameRateCompatibility::NoVote};
1407 }
1408
1409 return frameRate;
Steven Thomas3172e202020-01-06 19:25:30 -08001410}
1411
Marissa Wallf58c14b2018-07-24 10:50:43 -07001412void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Alec Mourie60041e2019-06-14 18:59:51 -07001413 ATRACE_CALL();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001414 mCurrentState.barrierLayer_legacy = barrierLayer;
1415 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001416 // We don't set eTransactionNeeded, because just receiving a deferral
1417 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001418 mCurrentState.modified = true;
1419 pushPendingState();
1420 mCurrentState.barrierLayer_legacy = nullptr;
1421 mCurrentState.frameNumber_legacy = 0;
1422 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001423}
1424
Marissa Wallf58c14b2018-07-24 10:50:43 -07001425void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001426 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001427 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001428}
1429
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001430// ----------------------------------------------------------------------------
1431// pageflip handling...
1432// ----------------------------------------------------------------------------
1433
Robert Carr1f0a16a2016-10-24 16:27:39 -07001434bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001435 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001436 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001437 if (parent != nullptr && parent->isHiddenByPolicy()) {
1438 return true;
1439 }
Robert Carr1c5481e2019-07-01 14:42:27 -07001440 if (usingRelativeZ(LayerVector::StateSet::Drawing)) {
1441 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
1442 if (zOrderRelativeOf != nullptr) {
1443 if (zOrderRelativeOf->isHiddenByPolicy()) {
1444 return true;
1445 }
1446 }
1447 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001448 return s.flags & layer_state_t::eLayerHidden;
1449}
1450
David Sodman41fdfc92017-11-06 16:09:56 -08001451uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001452 // TODO: should we do something special if mSecure is set?
1453 if (mProtectedByApp) {
1454 // need a hardware-protected path to external video sink
1455 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001456 }
Riley Andrews03414a12014-07-01 14:22:59 -07001457 if (mPotentialCursor) {
1458 usage |= GraphicBuffer::USAGE_CURSOR;
1459 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001460 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001461 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001462}
1463
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001464void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001465 uint32_t orientation = 0;
Vishnu Nair5eb3f062019-04-08 08:21:03 -07001466 // Disable setting transform hint if the debug flag is set.
1467 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001468 // The transform hint is used to improve performance, but we can
1469 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001470 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001471 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001472 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001473 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001474 orientation = 0;
1475 }
1476 }
David Sodmaneb085e02017-10-05 18:49:04 -07001477 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001478}
1479
Mathias Agopian13127d82013-03-05 17:47:11 -08001480// ----------------------------------------------------------------------------
1481// debugging
1482// ----------------------------------------------------------------------------
1483
Marissa Wall61c58622018-07-18 10:12:20 -07001484// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001485LayerDebugInfo Layer::getLayerDebugInfo() const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001486 using namespace std::string_literals;
1487
Kalle Raitaa099a242017-01-11 11:17:29 -08001488 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001489 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001490 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001491 sp<Layer> parent = getParent();
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001492 info.mParentName = parent ? parent->getName() : "none"s;
chaviw8a01fa42019-08-19 12:39:31 -07001493 info.mType = getType();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001494 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Lloyd Piquea2468662019-03-07 21:31:06 -08001495
1496 info.mVisibleRegion = debugGetVisibleRegionOnDefaultDisplay();
Kalle Raitaa099a242017-01-11 11:17:29 -08001497 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001498 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001499 info.mX = ds.active_legacy.transform.tx();
1500 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001501 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001502 info.mWidth = ds.active_legacy.w;
1503 info.mHeight = ds.active_legacy.h;
1504 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001505 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001506 info.mFlags = ds.flags;
1507 info.mPixelFormat = getPixelFormat();
chaviw4244e032019-09-04 11:27:49 -07001508 info.mDataSpace = static_cast<android_dataspace>(getDataSpace());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001509 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1510 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1511 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1512 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001513 {
chaviwd62d3062019-09-04 14:48:02 -07001514 sp<const GraphicBuffer> buffer = getBuffer();
David Sodman5b4cffc2017-11-23 13:20:29 -08001515 if (buffer != 0) {
1516 info.mActiveBufferWidth = buffer->getWidth();
1517 info.mActiveBufferHeight = buffer->getHeight();
1518 info.mActiveBufferStride = buffer->getStride();
1519 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001520 } else {
1521 info.mActiveBufferWidth = 0;
1522 info.mActiveBufferHeight = 0;
1523 info.mActiveBufferStride = 0;
1524 info.mActiveBufferFormat = 0;
1525 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001526 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001527 info.mNumQueuedFrames = getQueuedFrameCount();
1528 info.mRefreshPending = isBufferLatched();
1529 info.mIsOpaque = isOpaque(ds);
1530 info.mContentDirty = contentDirty;
1531 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001532}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001533
Yiwei Zhang5434a782018-12-05 18:06:32 -08001534void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001535 result.append("-------------------------------");
1536 result.append("-------------------------------");
1537 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001538 result.append(" Layer name\n");
1539 result.append(" Z | ");
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001540 result.append(" Window Type | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001541 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001542 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001543 result.append(" Disp Frame (LTRB) | ");
1544 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001545 result.append("-------------------------------");
1546 result.append("-------------------------------");
1547 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001548}
1549
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001550void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1551 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1552 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001553 return;
1554 }
1555
Yiwei Zhang5434a782018-12-05 18:06:32 -08001556 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001557 if (mName.length() > 77) {
1558 std::string shortened;
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001559 shortened.append(mName, 0, 36);
Dan Stozae22aec72016-08-01 13:20:59 -07001560 shortened.append("[...]");
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001561 shortened.append(mName, mName.length() - 36);
1562 name = std::move(shortened);
Dan Stozae22aec72016-08-01 13:20:59 -07001563 } else {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001564 name = mName;
Dan Stozae22aec72016-08-01 13:20:59 -07001565 }
1566
Yiwei Zhang5434a782018-12-05 18:06:32 -08001567 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001568
Alec Mourib416efd2018-09-06 21:01:59 +00001569 const State& layerState(getDrawingState());
Lloyd Piquede196652020-01-22 17:29:58 -08001570 const auto& outputLayerState = outputLayer->getState();
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001571
Chia-I Wu1e043612018-03-01 09:45:09 -08001572 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001573 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001574 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001575 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001576 }
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001577 StringAppendF(&result, " %10d | ", mWindowType);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001578 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
Lloyd Piquede196652020-01-22 17:29:58 -08001579 StringAppendF(&result, "%10s | ", toString(outputLayerState.bufferTransform).c_str());
1580 const Rect& frame = outputLayerState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001581 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Piquede196652020-01-22 17:29:58 -08001582 const FloatRect& crop = outputLayerState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001583 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1584 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001585
Yichi Chen6ca35192018-05-29 12:20:43 +08001586 result.append("- - - - - - - - - - - - - - - -");
1587 result.append("- - - - - - - - - - - - - - - -");
1588 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001589}
Dan Stozae22aec72016-08-01 13:20:59 -07001590
Yiwei Zhang5434a782018-12-05 18:06:32 -08001591void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001592 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001593}
1594
Svetoslavd85084b2014-03-20 10:28:31 -07001595void Layer::clearFrameStats() {
1596 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001597}
1598
Jamie Gennis6547ff42013-07-16 20:12:42 -07001599void Layer::logFrameStats() {
1600 mFrameTracker.logAndResetStats(mName);
1601}
1602
Svetoslavd85084b2014-03-20 10:28:31 -07001603void Layer::getFrameStats(FrameStats* outStats) const {
1604 mFrameTracker.getStats(outStats);
1605}
1606
Yiwei Zhang5434a782018-12-05 18:06:32 -08001607void Layer::dumpFrameEvents(std::string& result) {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001608 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().c_str(), getType(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001609 Mutex::Autolock lock(mFrameEventHistoryMutex);
1610 mFrameEventHistory.checkFencesForCompletion();
1611 mFrameEventHistory.dump(result);
1612}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001613
Vishnu Nair0f085c62019-08-30 08:49:12 -07001614void Layer::dumpCallingUidPid(std::string& result) const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001615 StringAppendF(&result, "Layer %s (%s) pid:%d uid:%d\n", getName().c_str(), getType(),
Vishnu Nair0f085c62019-08-30 08:49:12 -07001616 mCallingPid, mCallingUid);
1617}
1618
Brian Anderson5ea5e592016-12-01 16:54:33 -08001619void Layer::onDisconnect() {
1620 Mutex::Autolock lock(mFrameEventHistoryMutex);
1621 mFrameEventHistory.onDisconnect();
Yiwei Zhang1a88c402019-11-18 10:43:58 -08001622 const int32_t layerId = getSequence();
1623 mFlinger->mTimeStats->onDestroy(layerId);
1624 mFlinger->mFrameTracer->onDestroy(layerId);
Brian Anderson5ea5e592016-12-01 16:54:33 -08001625}
1626
Brian Anderson3890c392016-07-25 12:48:08 -07001627void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001628 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001629 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001630 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1631 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhang487340f2019-11-18 17:42:12 -08001632 mFlinger->mTimeStats->setAcquireFence(getSequence(), newTimestamps->frameNumber,
1633 newTimestamps->acquireFence);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001634 }
1635
Brian Andersond6927fb2016-07-23 23:37:30 -07001636 Mutex::Autolock lock(mFrameEventHistoryMutex);
1637 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001638 // If there are any unsignaled fences in the aquire timeline at this
1639 // point, the previously queued frame hasn't been latched yet. Go ahead
1640 // and try to get the signal time here so the syscall is taken out of
1641 // the main thread's critical path.
1642 mAcquireTimeline.updateSignalTimes();
1643 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001644 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001645 mFrameEventHistory.addQueue(*newTimestamps);
1646 }
1647
Brian Anderson3890c392016-07-25 12:48:08 -07001648 if (outDelta) {
1649 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001650 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001651}
Dan Stozae77c7662016-05-13 11:37:28 -07001652
Chia-I Wu98f1c102017-05-30 14:54:08 -07001653size_t Layer::getChildrenCount() const {
1654 size_t count = 0;
1655 for (const sp<Layer>& child : mCurrentChildren) {
1656 count += 1 + child->getChildrenCount();
1657 }
1658 return count;
1659}
1660
Robert Carr1f0a16a2016-10-24 16:27:39 -07001661void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001662 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001663 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001664
Robert Carr1f0a16a2016-10-24 16:27:39 -07001665 mCurrentChildren.add(layer);
1666 layer->setParent(this);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001667 updateTreeHasFrameRateVote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001668}
1669
1670ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001671 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001672 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001673
Robert Carr1323c952019-01-28 18:13:27 -08001674 layer->setParent(nullptr);
Ady Abraham60e42ea2020-03-09 19:17:31 -07001675 const auto removeResult = mCurrentChildren.remove(layer);
1676
1677 updateTreeHasFrameRateVote();
1678 layer->updateTreeHasFrameRateVote();
1679
1680 return removeResult;
1681}
1682
1683void Layer::reparentChildren(const sp<Layer>& newParent) {
1684 if (attachChildren()) {
1685 setTransactionFlags(eTransactionNeeded);
1686 }
1687
1688 for (const sp<Layer>& child : mCurrentChildren) {
1689 newParent->addChild(child);
1690 }
1691 mCurrentChildren.clear();
1692 updateTreeHasFrameRateVote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001693}
1694
Robert Carr1db73f62016-12-21 12:58:51 -08001695bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1696 sp<Handle> handle = nullptr;
1697 sp<Layer> newParent = nullptr;
1698 if (newParentHandle == nullptr) {
1699 return false;
1700 }
1701 handle = static_cast<Handle*>(newParentHandle.get());
1702 newParent = handle->owner.promote();
1703 if (newParent == nullptr) {
1704 ALOGE("Unable to promote Layer handle");
1705 return false;
1706 }
1707
Ady Abraham60e42ea2020-03-09 19:17:31 -07001708 reparentChildren(newParent);
Robert Carr1db73f62016-12-21 12:58:51 -08001709
1710 return true;
1711}
1712
Robert Carr15eae092018-03-23 13:43:53 -07001713void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001714 for (const sp<Layer>& child : mDrawingChildren) {
1715 child->mDrawingParent = newParent;
Vishnu Nairc652ff82019-03-15 12:48:54 -07001716 child->computeBounds(newParent->mBounds,
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001717 newParent->getTransformWithScale(newParent->getBufferScaleTransform()),
1718 newParent->mEffectiveShadowRadius);
Robert Carr578038f2018-03-09 12:25:24 -08001719 }
1720}
1721
chaviwf1961f72017-09-18 16:41:07 -07001722bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001723 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001724
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001725 // While layers are detached, we allow most operations
1726 // and simply halt performing the actual transaction. However
1727 // for reparent != null we would enter the mRemovedFromCurrentState
1728 // state, regardless of whether doTransaction was called, and
1729 // so we need to prevent the update here.
1730 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001731 return false;
1732 }
1733
Robert Carr54cf5b12019-01-25 14:02:28 -08001734 sp<Layer> newParent;
1735 if (newParentHandle != nullptr) {
1736 auto handle = static_cast<Handle*>(newParentHandle.get());
1737 newParent = handle->owner.promote();
1738 if (newParent == nullptr) {
1739 ALOGE("Unable to promote Layer handle");
1740 return false;
1741 }
1742 if (newParent == this) {
1743 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1744 return false;
1745 }
1746 }
1747
chaviwf1961f72017-09-18 16:41:07 -07001748 sp<Layer> parent = getParent();
1749 if (parent != nullptr) {
1750 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001751 }
1752
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001753 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001754 newParent->addChild(this);
1755 if (!newParent->isRemovedFromCurrentState()) {
1756 addToCurrentState();
1757 } else {
1758 onRemovedFromCurrentState();
1759 }
1760
1761 if (mLayerDetached) {
1762 mLayerDetached = false;
1763 callSetTransactionFlags = true;
1764 }
1765 } else {
1766 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001767 }
1768
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001769 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001770 setTransactionFlags(eTransactionNeeded);
1771 }
chaviw06178942017-07-27 10:25:59 -07001772 return true;
1773}
1774
Robert Carr9524cb32017-02-13 11:32:32 -08001775bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001776 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001777 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001778 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001779 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001780 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001781 child->detachChildren();
chaviw43cb3cb2019-05-31 15:23:41 -07001782 child->removeRemoteSyncPoints();
Robert Carr9524cb32017-02-13 11:32:32 -08001783 }
Robert Carr7f619b22017-11-06 12:56:35 -08001784 }
Robert Carr9524cb32017-02-13 11:32:32 -08001785
1786 return true;
1787}
1788
chaviw5aedec92018-10-22 10:40:38 -07001789bool Layer::attachChildren() {
1790 bool changed = false;
1791 for (const sp<Layer>& child : mCurrentChildren) {
1792 sp<Client> parentClient = mClientRef.promote();
1793 sp<Client> client(child->mClientRef.promote());
1794 if (client != nullptr && parentClient != client) {
1795 if (child->mLayerDetached) {
1796 child->mLayerDetached = false;
1797 changed = true;
1798 }
1799 changed |= child->attachChildren();
1800 }
1801 }
1802
1803 return changed;
1804}
1805
Peiyong Lind3788632018-09-18 16:01:31 -07001806bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001807 static const mat4 identityMatrix = mat4();
1808
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001809 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001810 return false;
1811 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001812 ++mCurrentState.sequence;
1813 mCurrentState.colorTransform = matrix;
1814 mCurrentState.hasColorTransform = matrix != identityMatrix;
1815 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001816 setTransactionFlags(eTransactionNeeded);
1817 return true;
1818}
1819
chaviwf66724d2018-11-28 16:35:21 -08001820mat4 Layer::getColorTransform() const {
1821 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1822 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1823 colorTransform = parent->getColorTransform() * colorTransform;
1824 }
1825 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001826}
1827
1828bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001829 bool hasColorTransform = getDrawingState().hasColorTransform;
1830 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1831 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1832 }
1833 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001834}
1835
Chia-I Wu11481472018-05-04 10:43:19 -07001836bool Layer::isLegacyDataSpace() const {
1837 // return true when no higher bits are set
chaviw4244e032019-09-04 11:27:49 -07001838 return !(getDataSpace() &
1839 (ui::Dataspace::STANDARD_MASK | ui::Dataspace::TRANSFER_MASK |
1840 ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001841}
1842
Robert Carr1f0a16a2016-10-24 16:27:39 -07001843void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001844 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001845}
1846
chaviw301b1d82019-11-06 13:15:09 -08001847int32_t Layer::getZ(LayerVector::StateSet stateSet) const {
1848 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1849 const State& state = useDrawing ? mDrawingState : mCurrentState;
1850 return state.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001851}
1852
Robert Carr1c5481e2019-07-01 14:42:27 -07001853bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) const {
Robert Carr29abff82017-12-04 13:51:20 -08001854 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001855 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviwe5ac40f2019-09-24 16:36:55 -07001856 return state.isRelativeOf;
Robert Carr29abff82017-12-04 13:51:20 -08001857}
1858
David Sodman41fdfc92017-11-06 16:09:56 -08001859__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001860 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001861 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1862 "makeTraversalList received invalid stateSet");
1863 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1864 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001865 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001866
Robert Carr29abff82017-12-04 13:51:20 -08001867 if (state.zOrderRelatives.size() == 0) {
1868 *outSkipRelativeZUsers = true;
1869 return children;
1870 }
1871
chaviwfd462612018-05-31 16:11:27 -07001872 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001873 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001874 sp<Layer> strongRelative = weakRelative.promote();
1875 if (strongRelative != nullptr) {
1876 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001877 }
1878 }
1879
Dan Stoza412903f2017-04-27 13:42:17 -07001880 for (const sp<Layer>& child : children) {
chaviwe5ac40f2019-09-24 16:36:55 -07001881 if (child->usingRelativeZ(stateSet)) {
Robert Carr503c7042017-09-27 15:06:08 -07001882 continue;
1883 }
Robert Carrdb66e622017-04-10 16:55:57 -07001884 traverse.add(child);
1885 }
1886
1887 return traverse;
1888}
1889
Robert Carr1f0a16a2016-10-24 16:27:39 -07001890/**
Robert Carrdb66e622017-04-10 16:55:57 -07001891 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001892 */
Dan Stoza412903f2017-04-27 13:42:17 -07001893void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001894 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1895 // produce a new list for traversing, including our relatives, and not including our children
1896 // who are relatives of another surface. In the case that there are no relative Z,
1897 // makeTraversalList returns our children directly to avoid significant overhead.
1898 // However in this case we need to take the responsibility for filtering children which
1899 // are relatives of another surface here.
1900 bool skipRelativeZUsers = false;
1901 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001902
Robert Carr1f0a16a2016-10-24 16:27:39 -07001903 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001904 for (; i < list.size(); i++) {
1905 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001906 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1907 continue;
1908 }
1909
chaviw301b1d82019-11-06 13:15:09 -08001910 if (relative->getZ(stateSet) >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001911 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001912 }
Dan Stoza412903f2017-04-27 13:42:17 -07001913 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001914 }
Robert Carr29abff82017-12-04 13:51:20 -08001915
Dan Stoza412903f2017-04-27 13:42:17 -07001916 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001917 for (; i < list.size(); i++) {
1918 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001919
Robert Carr29abff82017-12-04 13:51:20 -08001920 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1921 continue;
1922 }
Dan Stoza412903f2017-04-27 13:42:17 -07001923 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001924 }
1925}
1926
1927/**
Robert Carrdb66e622017-04-10 16:55:57 -07001928 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001929 */
Dan Stoza412903f2017-04-27 13:42:17 -07001930void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1931 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001932 // See traverseInZOrder for documentation.
1933 bool skipRelativeZUsers = false;
1934 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001935
Robert Carr1f0a16a2016-10-24 16:27:39 -07001936 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001937 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001938 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001939
1940 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1941 continue;
1942 }
1943
chaviw301b1d82019-11-06 13:15:09 -08001944 if (relative->getZ(stateSet) < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001945 break;
1946 }
Dan Stoza412903f2017-04-27 13:42:17 -07001947 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001948 }
Dan Stoza412903f2017-04-27 13:42:17 -07001949 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001950 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001951 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001952
1953 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1954 continue;
1955 }
1956
Dan Stoza412903f2017-04-27 13:42:17 -07001957 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001958 }
1959}
1960
Edgar Arriaga844fa672020-01-16 14:21:42 -08001961void Layer::traverse(LayerVector::StateSet state, const LayerVector::Visitor& visitor) {
1962 visitor(this);
1963 const LayerVector& children =
1964 state == LayerVector::StateSet::Drawing ? mDrawingChildren : mCurrentChildren;
1965 for (const sp<Layer>& child : children) {
1966 child->traverse(state, visitor);
1967 }
1968}
1969
chaviw4b129c22018-04-09 16:19:43 -07001970LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1971 const std::vector<Layer*>& layersInTree) {
1972 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1973 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001974 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1975 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001976 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001977
chaviwfd462612018-05-31 16:11:27 -07001978 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001979 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1980 sp<Layer> strongRelative = weakRelative.promote();
1981 // Only add relative layers that are also descendents of the top most parent of the tree.
1982 // If a relative layer is not a descendent, then it should be ignored.
1983 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1984 traverse.add(strongRelative);
1985 }
1986 }
1987
1988 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001989 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001990 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1991 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1992 // the child here since it won't be added later as a relative.
1993 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1994 childState.zOrderRelativeOf.promote().get())) {
1995 continue;
1996 }
1997 traverse.add(child);
1998 }
1999
2000 return traverse;
2001}
2002
2003void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
2004 LayerVector::StateSet stateSet,
2005 const LayerVector::Visitor& visitor) {
2006 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07002007
2008 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07002009 for (; i < list.size(); i++) {
2010 const auto& relative = list[i];
chaviw301b1d82019-11-06 13:15:09 -08002011 if (relative->getZ(stateSet) >= 0) {
chaviwa76b2712017-09-20 12:02:26 -07002012 break;
2013 }
chaviw4b129c22018-04-09 16:19:43 -07002014 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07002015 }
chaviw4b129c22018-04-09 16:19:43 -07002016
chaviwa76b2712017-09-20 12:02:26 -07002017 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07002018 for (; i < list.size(); i++) {
2019 const auto& relative = list[i];
2020 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07002021 }
2022}
2023
chaviw4b129c22018-04-09 16:19:43 -07002024std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
2025 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2026 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
2027
2028 std::vector<Layer*> layersInTree = {this};
2029 for (size_t i = 0; i < children.size(); i++) {
2030 const auto& child = children[i];
2031 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
2032 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
2033 }
2034
2035 return layersInTree;
2036}
2037
2038void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
2039 const LayerVector::Visitor& visitor) {
2040 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
2041 std::sort(layersInTree.begin(), layersInTree.end());
2042 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
2043}
2044
Peiyong Linefefaac2018-08-17 12:27:51 -07002045ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08002046 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002047}
2048
chaviw13fdc492017-06-27 12:40:18 -07002049half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08002050 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002051
chaviw13fdc492017-06-27 12:40:18 -07002052 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
2053 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07002054}
Robert Carr6452f122017-03-21 10:41:29 -07002055
chaviw13fdc492017-06-27 12:40:18 -07002056half4 Layer::getColor() const {
2057 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002058 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07002059}
Robert Carr6452f122017-03-21 10:41:29 -07002060
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08002061int32_t Layer::getBackgroundBlurRadius() const {
2062 return getDrawingState().backgroundBlurRadius;
2063}
2064
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002065Layer::RoundedCornerState Layer::getRoundedCornerState() const {
2066 const auto& p = mDrawingParent.promote();
2067 if (p != nullptr) {
Peiyong Lin27016a92019-03-29 17:36:08 +00002068 RoundedCornerState parentState = p->getRoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002069 if (parentState.radius > 0) {
2070 ui::Transform t = getActiveTransform(getDrawingState());
2071 t = t.inverse();
2072 parentState.cropRect = t.transform(parentState.cropRect);
2073 // The rounded corners shader only accepts 1 corner radius for performance reasons,
2074 // but a transform matrix can define horizontal and vertical scales.
2075 // Let's take the average between both of them and pass into the shader, practically we
2076 // never do this type of transformation on windows anyway.
2077 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
2078 return parentState;
2079 }
2080 }
2081 const float radius = getDrawingState().cornerRadius;
Peiyong Linb9ff23e2019-04-08 11:04:59 -07002082 return radius > 0 && getCrop(getDrawingState()).isValid()
2083 ? RoundedCornerState(getCrop(getDrawingState()).toFloatRect(), radius)
2084 : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002085}
2086
Vishnu Nair08f6eae2019-11-26 14:01:39 -08002087renderengine::ShadowSettings Layer::getShadowSettings(const Rect& viewport) const {
2088 renderengine::ShadowSettings state = mFlinger->mDrawingState.globalShadowSettings;
2089
2090 // Shift the spot light x-position to the middle of the display and then
2091 // offset it by casting layer's screen pos.
2092 state.lightPos.x = (viewport.width() / 2.f) - mScreenBounds.left;
2093 state.lightPos.y -= mScreenBounds.top;
2094
2095 state.length = mEffectiveShadowRadius;
2096 return state;
2097}
2098
Robert Carr1f0a16a2016-10-24 16:27:39 -07002099void Layer::commitChildList() {
2100 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
2101 const auto& child = mCurrentChildren[i];
2102 child->commitChildList();
2103 }
2104 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07002105 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002106}
2107
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002108static wp<Layer> extractLayerFromBinder(const wp<IBinder>& weakBinderHandle) {
2109 if (weakBinderHandle == nullptr) {
2110 return nullptr;
2111 }
2112 sp<IBinder> binderHandle = weakBinderHandle.promote();
2113 if (binderHandle == nullptr) {
2114 return nullptr;
2115 }
2116 sp<Layer::Handle> handle = static_cast<Layer::Handle*>(binderHandle.get());
2117 if (handle == nullptr) {
2118 return nullptr;
2119 }
2120 return handle->owner;
2121}
2122
Robert Carr720e5062018-07-30 17:45:14 -07002123void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002124 mCurrentState.inputInfo = info;
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002125 mCurrentState.touchableRegionCrop = extractLayerFromBinder(info.touchableRegionCropHandle);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002126 mCurrentState.modified = true;
2127 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07002128 setTransactionFlags(eTransactionNeeded);
2129}
2130
Alec Mouri6b9e9912020-01-21 10:50:24 -08002131LayerProto* Layer::writeToProto(LayersProto& layersProto, uint32_t traceFlags,
2132 const sp<const DisplayDevice>& device) const {
chaviw08f3cb22020-01-13 13:17:21 -08002133 LayerProto* layerProto = layersProto.add_layers();
2134 writeToProtoDrawingState(layerProto, traceFlags);
2135 writeToProtoCommonState(layerProto, LayerVector::StateSet::Drawing, traceFlags);
2136
Vishnu Nair60db8c02020-04-02 11:55:16 -07002137 if (traceFlags & SurfaceTracing::TRACE_COMPOSITION) {
2138 // Only populate for the primary display.
2139 if (device) {
2140 const Hwc2::IComposerClient::Composition compositionType = getCompositionType(device);
2141 layerProto->set_hwc_composition_type(static_cast<HwcCompositionType>(compositionType));
2142 }
Alec Mouri6b9e9912020-01-21 10:50:24 -08002143 }
2144
chaviw08f3cb22020-01-13 13:17:21 -08002145 for (const sp<Layer>& layer : mDrawingChildren) {
Alec Mouri6b9e9912020-01-21 10:50:24 -08002146 layer->writeToProto(layersProto, traceFlags, device);
chaviw08f3cb22020-01-13 13:17:21 -08002147 }
chaviw6d89e2d2020-01-14 14:42:01 -08002148
2149 return layerProto;
chaviw08f3cb22020-01-13 13:17:21 -08002150}
2151
Vishnu Nair8406fd72019-07-30 11:29:31 -07002152void Layer::writeToProtoDrawingState(LayerProto* layerInfo, uint32_t traceFlags) const {
2153 ui::Transform transform = getTransform();
2154
2155 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
2156 for (const auto& pendingState : mPendingStatesSnapshot) {
2157 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
2158 if (barrierLayer != nullptr) {
2159 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
2160 barrierLayerProto->set_id(barrierLayer->sequence);
2161 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
2162 }
2163 }
2164
chaviwd62d3062019-09-04 14:48:02 -07002165 auto buffer = getBuffer();
Vishnu Nair8406fd72019-07-30 11:29:31 -07002166 if (buffer != nullptr) {
2167 LayerProtoHelper::writeToProto(buffer,
2168 [&]() { return layerInfo->mutable_active_buffer(); });
chaviw4244e032019-09-04 11:27:49 -07002169 LayerProtoHelper::writeToProto(ui::Transform(getBufferTransform()),
Vishnu Nair8406fd72019-07-30 11:29:31 -07002170 layerInfo->mutable_buffer_transform());
2171 }
2172 layerInfo->set_invalidate(contentDirty);
2173 layerInfo->set_is_protected(isProtected());
chaviw4244e032019-09-04 11:27:49 -07002174 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(getDataSpace())));
Vishnu Nair8406fd72019-07-30 11:29:31 -07002175 layerInfo->set_queued_frames(getQueuedFrameCount());
2176 layerInfo->set_refresh_pending(isBufferLatched());
2177 layerInfo->set_curr_frame(mCurrentFrameNumber);
2178 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
2179
2180 layerInfo->set_corner_radius(getRoundedCornerState().radius);
Ana Krulece766cc82020-04-27 13:49:13 -07002181 layerInfo->set_background_blur_radius(getBackgroundBlurRadius());
Vishnu Nair8406fd72019-07-30 11:29:31 -07002182 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
2183 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
2184 [&]() { return layerInfo->mutable_position(); });
2185 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
Vishnu Nair60db8c02020-04-02 11:55:16 -07002186 if (traceFlags & SurfaceTracing::TRACE_COMPOSITION) {
2187 LayerProtoHelper::writeToProto(debugGetVisibleRegionOnDefaultDisplay(),
2188 [&]() { return layerInfo->mutable_visible_region(); });
2189 }
Vishnu Nair8406fd72019-07-30 11:29:31 -07002190 LayerProtoHelper::writeToProto(surfaceDamageRegion,
2191 [&]() { return layerInfo->mutable_damage_region(); });
chaviwddeae262020-01-06 10:31:23 -08002192
2193 if (hasColorTransform()) {
2194 LayerProtoHelper::writeToProto(getColorTransform(),
2195 layerInfo->mutable_color_transform());
2196 }
Vishnu Nair8406fd72019-07-30 11:29:31 -07002197 }
2198
Vishnu Nair60db8c02020-04-02 11:55:16 -07002199 LayerProtoHelper::writeToProto(mSourceBounds,
2200 [&]() { return layerInfo->mutable_source_bounds(); });
2201 LayerProtoHelper::writeToProto(mScreenBounds,
2202 [&]() { return layerInfo->mutable_screen_bounds(); });
2203 LayerProtoHelper::writeToProto(getRoundedCornerState().cropRect,
2204 [&]() { return layerInfo->mutable_corner_radius_crop(); });
2205 layerInfo->set_shadow_radius(mEffectiveShadowRadius);
Vishnu Nair8406fd72019-07-30 11:29:31 -07002206}
2207
2208void Layer::writeToProtoCommonState(LayerProto* layerInfo, LayerVector::StateSet stateSet,
2209 uint32_t traceFlags) const {
chaviw1d044282017-09-27 12:19:28 -07002210 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2211 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002212 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07002213
Peiyong Linefefaac2018-08-17 12:27:51 -07002214 ui::Transform requestedTransform = state.active_legacy.transform;
chaviw1d044282017-09-27 12:19:28 -07002215
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002216 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
2217 layerInfo->set_id(sequence);
2218 layerInfo->set_name(getName().c_str());
chaviw8a01fa42019-08-19 12:39:31 -07002219 layerInfo->set_type(getType());
chaviw1d044282017-09-27 12:19:28 -07002220
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002221 for (const auto& child : children) {
2222 layerInfo->add_children(child->sequence);
chaviw1d044282017-09-27 12:19:28 -07002223 }
chaviw1d044282017-09-27 12:19:28 -07002224
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002225 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
2226 sp<Layer> strongRelative = weakRelative.promote();
2227 if (strongRelative != nullptr) {
2228 layerInfo->add_relatives(strongRelative->sequence);
2229 }
chaviwadc40c22018-07-10 16:57:27 -07002230 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002231
2232 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
2233 [&]() { return layerInfo->mutable_transparent_region(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002234
2235 layerInfo->set_layer_stack(getLayerStack());
2236 layerInfo->set_z(state.z);
2237
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002238 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(),
2239 [&]() {
2240 return layerInfo->mutable_requested_position();
2241 });
2242
2243 LayerProtoHelper::writeSizeToProto(state.active_legacy.w, state.active_legacy.h,
2244 [&]() { return layerInfo->mutable_size(); });
2245
2246 LayerProtoHelper::writeToProto(state.crop_legacy,
2247 [&]() { return layerInfo->mutable_crop(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002248
2249 layerInfo->set_is_opaque(isOpaque(state));
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002250
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002251
2252 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
2253 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
2254 LayerProtoHelper::writeToProto(state.color,
2255 [&]() { return layerInfo->mutable_requested_color(); });
2256 layerInfo->set_flags(state.flags);
2257
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002258 LayerProtoHelper::writeToProto(requestedTransform,
2259 layerInfo->mutable_requested_transform());
2260
2261 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
2262 if (parent != nullptr) {
2263 layerInfo->set_parent(parent->sequence);
2264 } else {
2265 layerInfo->set_parent(-1);
2266 }
2267
2268 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2269 if (zOrderRelativeOf != nullptr) {
2270 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
2271 } else {
2272 layerInfo->set_z_order_relative_of(-1);
2273 }
chaviw08f3cb22020-01-13 13:17:21 -08002274
2275 layerInfo->set_is_relative_of(state.isRelativeOf);
chaviwadc40c22018-07-10 16:57:27 -07002276 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002277
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002278 if (traceFlags & SurfaceTracing::TRACE_INPUT) {
2279 LayerProtoHelper::writeToProto(state.inputInfo, state.touchableRegionCrop,
2280 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002281 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002282
2283 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
2284 auto protoMap = layerInfo->mutable_metadata();
2285 for (const auto& entry : state.metadata.mMap) {
2286 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
2287 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002288 }
chaviw1d044282017-09-27 12:19:28 -07002289}
2290
Robert Carr2e102c92018-10-23 12:11:15 -07002291bool Layer::isRemovedFromCurrentState() const {
2292 return mRemovedFromCurrentState;
2293}
2294
Arthur Hungd20b2702019-01-14 18:16:16 +08002295InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002296 InputWindowInfo info = mDrawingState.inputInfo;
chaviwaf87b3e2019-10-01 16:59:28 -07002297 info.id = sequence;
Robert Carr720e5062018-07-30 17:45:14 -07002298
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002299 if (info.displayId == ADISPLAY_ID_NONE) {
chaviwb15ea8a2019-09-03 12:40:22 -07002300 info.displayId = getLayerStack();
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002301 }
2302
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002303 ui::Transform t = getTransform();
2304 const float xScale = t.sx();
2305 const float yScale = t.sy();
Ady Abraham282f1d72019-07-24 18:05:56 -07002306 int32_t xSurfaceInset = info.surfaceInset;
2307 int32_t ySurfaceInset = info.surfaceInset;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002308 if (xScale != 1.0f || yScale != 1.0f) {
Ady Abraham282f1d72019-07-24 18:05:56 -07002309 info.windowXScale *= (xScale != 0.0f) ? 1.0f / xScale : 0.0f;
2310 info.windowYScale *= (yScale != 0.0f) ? 1.0f / yScale : 0.0f;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002311 info.touchableRegion.scaleSelf(xScale, yScale);
Ady Abraham282f1d72019-07-24 18:05:56 -07002312 xSurfaceInset = std::round(xSurfaceInset * xScale);
2313 ySurfaceInset = std::round(ySurfaceInset * yScale);
Riddle Hsu39d4aa52018-11-30 20:46:53 +08002314 }
Robert Carre07e1032018-11-26 12:55:53 -08002315
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002316 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002317 // If this is a portal window, set the touchableRegion to the layerBounds.
2318 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
2319 ? getBufferSize(getDrawingState())
2320 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002321 if (!layerBounds.isValid()) {
2322 layerBounds = getCroppedBufferSize(getDrawingState());
2323 }
Vishnu Nair8033a492018-12-05 07:27:23 -08002324 layerBounds = t.transform(layerBounds);
Ady Abraham282f1d72019-07-24 18:05:56 -07002325
2326 // clamp inset to layer bounds
2327 xSurfaceInset = (xSurfaceInset >= 0) ? std::min(xSurfaceInset, layerBounds.getWidth() / 2) : 0;
2328 ySurfaceInset = (ySurfaceInset >= 0) ? std::min(ySurfaceInset, layerBounds.getHeight() / 2) : 0;
2329
Arthur Hung118b1142019-05-08 21:25:59 +08002330 layerBounds.inset(xSurfaceInset, ySurfaceInset, xSurfaceInset, ySurfaceInset);
Vishnu Nair8033a492018-12-05 07:27:23 -08002331
Arthur Hungd20b2702019-01-14 18:16:16 +08002332 // Input coordinate should match the layer bounds.
2333 info.frameLeft = layerBounds.left;
2334 info.frameTop = layerBounds.top;
2335 info.frameRight = layerBounds.right;
2336 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08002337
2338 // Position the touchable region relative to frame screen location and restrict it to frame
2339 // bounds.
2340 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08002341 info.visible = canReceiveInput();
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002342
2343 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2344 if (info.replaceTouchableRegionWithCrop) {
2345 if (cropLayer == nullptr) {
2346 info.touchableRegion = Region(Rect{mScreenBounds});
2347 } else {
2348 info.touchableRegion = Region(Rect{cropLayer->mScreenBounds});
2349 }
2350 } else if (cropLayer != nullptr) {
2351 info.touchableRegion = info.touchableRegion.intersect(Rect{cropLayer->mScreenBounds});
2352 }
2353
chaviwaf87b3e2019-10-01 16:59:28 -07002354 // If the layer is a clone, we need to crop the input region to cloned root to prevent
2355 // touches from going outside the cloned area.
2356 if (isClone()) {
2357 sp<Layer> clonedRoot = getClonedRoot();
2358 if (clonedRoot != nullptr) {
2359 Rect rect(clonedRoot->mScreenBounds);
2360 info.touchableRegion = info.touchableRegion.intersect(rect);
2361 }
2362 }
2363
Robert Carr720e5062018-07-30 17:45:14 -07002364 return info;
2365}
2366
chaviwaf87b3e2019-10-01 16:59:28 -07002367sp<Layer> Layer::getClonedRoot() {
2368 if (mClonedChild != nullptr) {
2369 return this;
2370 }
2371 if (mDrawingParent == nullptr || mDrawingParent.promote() == nullptr) {
2372 return nullptr;
2373 }
2374 return mDrawingParent.promote()->getClonedRoot();
2375}
2376
Robert Carr720e5062018-07-30 17:45:14 -07002377bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002378 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002379}
2380
chaviw3e727cd2019-01-31 13:41:05 -08002381bool Layer::canReceiveInput() const {
Vishnu Nair82353e92019-09-12 12:38:47 -07002382 return !isHiddenByPolicy();
chaviw3e727cd2019-01-31 13:41:05 -08002383}
2384
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002385compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
2386 const sp<const DisplayDevice>& display) const {
Lloyd Piquede196652020-01-22 17:29:58 -08002387 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionEngineLayerFE());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002388}
2389
Lloyd Piquea2468662019-03-07 21:31:06 -08002390Region Layer::debugGetVisibleRegionOnDefaultDisplay() const {
2391 sp<DisplayDevice> displayDevice = mFlinger->getDefaultDisplayDeviceLocked();
2392 if (displayDevice == nullptr) {
2393 return {};
2394 }
2395
2396 auto outputLayer = findOutputLayerForDisplay(displayDevice);
2397 if (outputLayer == nullptr) {
2398 return {};
2399 }
2400
2401 return outputLayer->getState().visibleRegion;
2402}
2403
chaviwb4c6e582019-08-16 14:35:07 -07002404void Layer::setInitialValuesForClone(const sp<Layer>& clonedFrom) {
2405 // copy drawing state from cloned layer
2406 mDrawingState = clonedFrom->mDrawingState;
2407 mClonedFrom = clonedFrom;
chaviwb4c6e582019-08-16 14:35:07 -07002408}
chaviw74b03172019-08-19 11:09:03 -07002409
2410void Layer::updateMirrorInfo() {
2411 if (mClonedChild == nullptr || !mClonedChild->isClonedFromAlive()) {
2412 // If mClonedChild is null, there is nothing to mirror. If isClonedFromAlive returns false,
2413 // it means that there is a clone, but the layer it was cloned from has been destroyed. In
2414 // that case, we want to delete the reference to the clone since we want it to get
2415 // destroyed. The root, this layer, will still be around since the client can continue
2416 // to hold a reference, but no cloned layers will be displayed.
2417 mClonedChild = nullptr;
2418 return;
2419 }
2420
2421 std::map<sp<Layer>, sp<Layer>> clonedLayersMap;
2422 // If the real layer exists and is in current state, add the clone as a child of the root.
2423 // There's no need to remove from drawingState when the layer is offscreen since currentState is
2424 // copied to drawingState for the root layer. So the clonedChild is always removed from
2425 // drawingState and then needs to be added back each traversal.
2426 if (!mClonedChild->getClonedFrom()->isRemovedFromCurrentState()) {
2427 addChildToDrawing(mClonedChild);
2428 }
2429
2430 mClonedChild->updateClonedDrawingState(clonedLayersMap);
2431 mClonedChild->updateClonedChildren(this, clonedLayersMap);
2432 mClonedChild->updateClonedRelatives(clonedLayersMap);
2433}
2434
2435void Layer::updateClonedDrawingState(std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2436 // If the layer the clone was cloned from is alive, copy the content of the drawingState
2437 // to the clone. If the real layer is no longer alive, continue traversing the children
2438 // since we may be able to pull out other children that are still alive.
2439 if (isClonedFromAlive()) {
2440 sp<Layer> clonedFrom = getClonedFrom();
2441 mDrawingState = clonedFrom->mDrawingState;
chaviw74b03172019-08-19 11:09:03 -07002442 clonedLayersMap.emplace(clonedFrom, this);
2443 }
2444
2445 // The clone layer may have children in drawingState since they may have been created and
2446 // added from a previous request to updateMirorInfo. This is to ensure we don't recreate clones
2447 // that already exist, since we can just re-use them.
2448 // The drawingChildren will not get overwritten by the currentChildren since the clones are
2449 // not updated in the regular traversal. They are skipped since the root will lose the
2450 // reference to them when it copies its currentChildren to drawing.
2451 for (sp<Layer>& child : mDrawingChildren) {
2452 child->updateClonedDrawingState(clonedLayersMap);
2453 }
2454}
2455
2456void Layer::updateClonedChildren(const sp<Layer>& mirrorRoot,
2457 std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2458 mDrawingChildren.clear();
2459
2460 if (!isClonedFromAlive()) {
2461 return;
2462 }
2463
2464 sp<Layer> clonedFrom = getClonedFrom();
2465 for (sp<Layer>& child : clonedFrom->mDrawingChildren) {
2466 if (child == mirrorRoot) {
2467 // This is to avoid cyclical mirroring.
2468 continue;
2469 }
2470 sp<Layer> clonedChild = clonedLayersMap[child];
2471 if (clonedChild == nullptr) {
2472 clonedChild = child->createClone();
2473 clonedLayersMap[child] = clonedChild;
2474 }
2475 addChildToDrawing(clonedChild);
2476 clonedChild->updateClonedChildren(mirrorRoot, clonedLayersMap);
2477 }
2478}
2479
chaviwaf87b3e2019-10-01 16:59:28 -07002480void Layer::updateClonedInputInfo(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2481 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2482 if (cropLayer != nullptr) {
2483 if (clonedLayersMap.count(cropLayer) == 0) {
2484 // Real layer had a crop layer but it's not in the cloned hierarchy. Just set to
2485 // self as crop layer to avoid going outside bounds.
2486 mDrawingState.touchableRegionCrop = this;
2487 } else {
2488 const sp<Layer>& clonedCropLayer = clonedLayersMap.at(cropLayer);
2489 mDrawingState.touchableRegionCrop = clonedCropLayer;
2490 }
2491 }
2492 // Cloned layers shouldn't handle watch outside since their z order is not determined by
2493 // WM or the client.
2494 mDrawingState.inputInfo.layoutParamsFlags &= ~InputWindowInfo::FLAG_WATCH_OUTSIDE_TOUCH;
2495}
2496
2497void Layer::updateClonedRelatives(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
chaviw74b03172019-08-19 11:09:03 -07002498 mDrawingState.zOrderRelativeOf = nullptr;
2499 mDrawingState.zOrderRelatives.clear();
2500
2501 if (!isClonedFromAlive()) {
2502 return;
2503 }
2504
chaviwaf87b3e2019-10-01 16:59:28 -07002505 const sp<Layer>& clonedFrom = getClonedFrom();
chaviw74b03172019-08-19 11:09:03 -07002506 for (wp<Layer>& relativeWeak : clonedFrom->mDrawingState.zOrderRelatives) {
chaviwaf87b3e2019-10-01 16:59:28 -07002507 const sp<Layer>& relative = relativeWeak.promote();
2508 if (clonedLayersMap.count(relative) > 0) {
2509 auto& clonedRelative = clonedLayersMap.at(relative);
chaviw74b03172019-08-19 11:09:03 -07002510 mDrawingState.zOrderRelatives.add(clonedRelative);
2511 }
2512 }
2513
2514 // Check if the relativeLayer for the real layer is part of the cloned hierarchy.
2515 // It's possible that the layer it's relative to is outside the requested cloned hierarchy.
2516 // In that case, we treat the layer as if the relativeOf has been removed. This way, it will
2517 // still traverse the children, but the layer with the missing relativeOf will not be shown
2518 // on screen.
chaviwaf87b3e2019-10-01 16:59:28 -07002519 const sp<Layer>& relativeOf = clonedFrom->mDrawingState.zOrderRelativeOf.promote();
2520 if (clonedLayersMap.count(relativeOf) > 0) {
2521 const sp<Layer>& clonedRelativeOf = clonedLayersMap.at(relativeOf);
chaviw74b03172019-08-19 11:09:03 -07002522 mDrawingState.zOrderRelativeOf = clonedRelativeOf;
2523 }
2524
chaviwaf87b3e2019-10-01 16:59:28 -07002525 updateClonedInputInfo(clonedLayersMap);
2526
chaviw74b03172019-08-19 11:09:03 -07002527 for (sp<Layer>& child : mDrawingChildren) {
2528 child->updateClonedRelatives(clonedLayersMap);
2529 }
2530}
2531
2532void Layer::addChildToDrawing(const sp<Layer>& layer) {
2533 mDrawingChildren.add(layer);
2534 layer->mDrawingParent = this;
2535}
2536
Steven Thomas62a4cf82020-01-31 12:04:03 -08002537Layer::FrameRateCompatibility Layer::FrameRate::convertCompatibility(int8_t compatibility) {
2538 switch (compatibility) {
2539 case ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT:
2540 return FrameRateCompatibility::Default;
2541 case ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE:
2542 return FrameRateCompatibility::ExactOrMultiple;
2543 default:
2544 LOG_ALWAYS_FATAL("Invalid frame rate compatibility value %d", compatibility);
2545 return FrameRateCompatibility::Default;
2546 }
2547}
2548
Robert Carra1257842020-01-31 13:48:28 -08002549bool Layer::maybeDirtyInput() {
2550 // No sense redirtying input.
2551 if (mFlinger->inputDirty()) return true;
2552
2553 if (hasInput()) {
2554 mFlinger->dirtyInput();
2555 return true;
2556 }
2557
2558 // If a child or relative dirties the input, no sense continuing to traverse
2559 // so we return early and halt the recursion. We traverse ourselves instead
2560 // of using traverse() so we can implement this early halt.
2561 for (const sp<Layer>& child : mDrawingChildren) {
2562 if (child->maybeDirtyInput()) {
2563 return true;
2564 }
2565 }
2566 for (const wp<Layer>& weakRelative : mDrawingState.zOrderRelatives) {
2567 sp<Layer> relative = weakRelative.promote();
2568 if (relative && relative->maybeDirtyInput()) {
2569 return true;
2570 }
2571 }
2572 return false;
2573}
2574
Mathias Agopian13127d82013-03-05 17:47:11 -08002575// ---------------------------------------------------------------------------
2576
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002577}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002578
2579#if defined(__gl_h_)
2580#error "don't include gl/gl.h in this file"
2581#endif
2582
2583#if defined(__gl2_h_)
2584#error "don't include gl2/gl2.h in this file"
2585#endif
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -08002586
2587// TODO(b/129481165): remove the #pragma below and fix conversion issues
2588#pragma clang diagnostic pop // ignored "-Wconversion"