blob: e04e0ab996dcb161152301f12dd20c2a8a3a8998 [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>
Vishnu Nair0f085c62019-08-30 08:49:12 -070029#include <binder/IPCThreadState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080030#include <compositionengine/Display.h>
Lloyd Piquea83776c2019-01-29 18:42:32 -080031#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080032#include <compositionengine/OutputLayer.h>
33#include <compositionengine/impl/OutputLayerCompositionState.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070034#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070035#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070036#include <cutils/properties.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080037#include <gui/BufferItem.h>
38#include <gui/LayerDebugInfo.h>
39#include <gui/Surface.h>
Alec Mourie60041e2019-06-14 18:59:51 -070040#include <math.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080041#include <renderengine/RenderEngine.h>
Alec Mourie60041e2019-06-14 18:59:51 -070042#include <stdint.h>
43#include <stdlib.h>
44#include <sys/types.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080045#include <ui/DebugUtils.h>
46#include <ui/GraphicBuffer.h>
47#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048#include <utils/Errors.h>
49#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080050#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080052#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053
Alec Mourie60041e2019-06-14 18:59:51 -070054#include <algorithm>
55#include <mutex>
56#include <sstream>
57
Yiwei Zhang60d1a192018-03-07 14:52:28 -080058#include "BufferLayer.h"
Valerie Haudd0b7572019-01-29 14:59:27 -080059#include "ColorLayer.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"
Mikael Pessa90092f42019-08-26 17:22:04 -070063#include "FrameTracer/FrameTracer.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080064#include "LayerProtoHelper.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070065#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070066#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067#include "SurfaceFlinger.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080068#include "TimeStats/TimeStats.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070069
David Sodman41fdfc92017-11-06 16:09:56 -080070#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072namespace android {
73
Yiwei Zhang5434a782018-12-05 18:06:32 -080074using base::StringAppendF;
75
Lloyd Piquef1c675b2018-09-12 20:45:39 -070076std::atomic<int32_t> Layer::sSequence{1};
Mathias Agopian13127d82013-03-05 17:47:11 -080077
Lloyd Pique42ab75e2018-09-12 20:46:03 -070078Layer::Layer(const LayerCreationArgs& args)
Ady Abraham8f1ee7f2019-04-05 10:32:50 -070079 : mFlinger(args.flinger),
80 mName(args.name),
81 mClientRef(args.client),
82 mWindowType(args.metadata.getInt32(METADATA_WINDOW_TYPE, 0)) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -070083 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -070084 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
85 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
86 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070087
Lloyd Pique0449b0f2018-12-20 16:23:45 -080088 mCurrentState.active_legacy.w = args.w;
89 mCurrentState.active_legacy.h = args.h;
90 mCurrentState.flags = layerFlags;
91 mCurrentState.active_legacy.transform.set(0, 0);
92 mCurrentState.crop_legacy.makeInvalid();
93 mCurrentState.requestedCrop_legacy = mCurrentState.crop_legacy;
94 mCurrentState.z = 0;
95 mCurrentState.color.a = 1.0f;
96 mCurrentState.layerStack = 0;
97 mCurrentState.sequence = 0;
98 mCurrentState.requested_legacy = mCurrentState.active_legacy;
Lloyd Pique0449b0f2018-12-20 16:23:45 -080099 mCurrentState.active.w = UINT32_MAX;
100 mCurrentState.active.h = UINT32_MAX;
101 mCurrentState.active.transform.set(0, 0);
Valerie Hau134651a2020-01-28 16:21:22 -0800102 mCurrentState.frameNumber = 0;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800103 mCurrentState.transform = 0;
104 mCurrentState.transformToDisplayInverse = false;
105 mCurrentState.crop.makeInvalid();
106 mCurrentState.acquireFence = new Fence(-1);
107 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
108 mCurrentState.hdrMetadata.validTypes = 0;
Marissa Wall7e0a01f2019-08-14 14:29:25 -0700109 mCurrentState.surfaceDamageRegion = Region::INVALID_REGION;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800110 mCurrentState.cornerRadius = 0.0f;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800111 mCurrentState.backgroundBlurRadius = 0;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800112 mCurrentState.api = -1;
113 mCurrentState.hasColorTransform = false;
Peiyong Linc502cb72019-03-01 15:00:23 -0800114 mCurrentState.colorSpaceAgnostic = false;
Ana Krulecc84d09b2019-11-02 23:10:29 +0100115 mCurrentState.frameRateSelectionPriority = PRIORITY_UNSET;
Evan Roskya1f1e152019-01-24 16:17:46 -0800116 mCurrentState.metadata = args.metadata;
Vishnu Nairf37dcfb2019-11-16 07:37:09 -0800117 mCurrentState.shadowRadius = 0.f;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700118
119 // drawing state & current state are identical
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800120 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700121
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800122 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700123 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800124 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200125 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700126
Vishnu Nair0f085c62019-08-30 08:49:12 -0700127 mCallingPid = args.callingPid;
128 mCallingUid = args.callingUid;
Dominik Laskowski75848362019-11-11 17:57:20 -0800129}
130
131void Layer::onFirstRef() {
132 mFlinger->onLayerFirstRef(this);
Dan Stoza436ccf32018-06-21 12:10:12 -0700133}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700134
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700135Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800136 sp<Client> c(mClientRef.promote());
137 if (c != 0) {
138 c->detachLayer(this);
139 }
140
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000141 mFrameTracker.logAndResetStats(mName);
chaviw74d90ad2019-04-26 14:45:26 -0700142 mFlinger->onLayerDestroyed(this);
Mathias Agopian96f08192010-06-02 23:28:45 -0700143}
144
chaviwde94d0f2020-01-22 13:11:25 -0800145LayerCreationArgs::LayerCreationArgs(SurfaceFlinger* flinger, const sp<Client> client,
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700146 std::string name, uint32_t w, uint32_t h, uint32_t flags,
Vishnu Nair0f085c62019-08-30 08:49:12 -0700147 LayerMetadata metadata)
148 : flinger(flinger),
149 client(client),
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700150 name(std::move(name)),
Vishnu Nair0f085c62019-08-30 08:49:12 -0700151 w(w),
152 h(h),
153 flags(flags),
154 metadata(std::move(metadata)) {
155 IPCThreadState* ipc = IPCThreadState::self();
156 callingPid = ipc->getCallingPid();
157 callingUid = ipc->getCallingUid();
158}
159
Mathias Agopian13127d82013-03-05 17:47:11 -0800160// ---------------------------------------------------------------------------
161// callbacks
162// ---------------------------------------------------------------------------
163
David Sodmaneb085e02017-10-05 18:49:04 -0700164/*
165 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
166 * Layer. So, the implementation is done in BufferLayer. When called on a
167 * ColorLayer object, it's essentially a NOP.
168 */
David Sodmaneb085e02017-10-05 18:49:04 -0700169void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800170
chaviw43cb3cb2019-05-31 15:23:41 -0700171void Layer::removeRemoteSyncPoints() {
172 for (auto& point : mRemoteSyncPoints) {
173 point->setTransactionApplied();
174 }
175 mRemoteSyncPoints.clear();
176
177 {
chaviw43cb3cb2019-05-31 15:23:41 -0700178 for (State pendingState : mPendingStates) {
179 pendingState.barrierLayer_legacy = nullptr;
180 }
181 }
182}
183
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700184void Layer::removeRelativeZ(const std::vector<Layer*>& layersInTree) {
185 if (mCurrentState.zOrderRelativeOf == nullptr) {
186 return;
187 }
Robert Carr2e102c92018-10-23 12:11:15 -0700188
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700189 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
190 if (strongRelative == nullptr) {
191 setZOrderRelativeOf(nullptr);
192 return;
193 }
194
195 if (!std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
196 strongRelative->removeZOrderRelative(this);
197 mFlinger->setTransactionFlags(eTraversalNeeded);
chaviw606e5cf2019-03-01 10:12:10 -0800198 setZOrderRelativeOf(nullptr);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700199 }
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700200}
201
202void Layer::removeFromCurrentState() {
203 mRemovedFromCurrentState = true;
Rob Carr4bba3702018-10-08 21:53:30 +0000204
Robert Carr2e102c92018-10-23 12:11:15 -0700205 // Since we are no longer reachable from CurrentState SurfaceFlinger
206 // will no longer invoke doTransaction for us, and so we will
207 // never finish applying transactions. We signal the sync point
208 // now so that another layer will not become indefinitely
209 // blocked.
chaviw43cb3cb2019-05-31 15:23:41 -0700210 removeRemoteSyncPoints();
Robert Carr2e102c92018-10-23 12:11:15 -0700211
212 {
213 Mutex::Autolock syncLock(mLocalSyncPointMutex);
214 for (auto& point : mLocalSyncPoints) {
215 point->setFrameAvailable();
216 }
217 mLocalSyncPoints.clear();
218 }
219
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800220 mFlinger->markLayerPendingRemovalLocked(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700221}
Chia-I Wu38512252017-05-17 14:36:16 -0700222
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700223void Layer::onRemovedFromCurrentState() {
224 auto layersInTree = getLayersInTree(LayerVector::StateSet::Current);
225 std::sort(layersInTree.begin(), layersInTree.end());
226 for (const auto& layer : layersInTree) {
227 layer->removeFromCurrentState();
228 layer->removeRelativeZ(layersInTree);
229 }
230}
231
chaviw61626f22018-11-15 16:26:27 -0800232void Layer::addToCurrentState() {
233 mRemovedFromCurrentState = false;
234
235 for (const auto& child : mCurrentChildren) {
236 child->addToCurrentState();
237 }
238}
239
Mathias Agopian13127d82013-03-05 17:47:11 -0800240// ---------------------------------------------------------------------------
241// set-up
242// ---------------------------------------------------------------------------
243
chaviw13fdc492017-06-27 12:40:18 -0700244bool Layer::getPremultipledAlpha() const {
245 return mPremultipliedAlpha;
246}
247
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700248sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800249 Mutex::Autolock _l(mLock);
Robert Carrc0df3122019-04-11 13:18:21 -0700250 if (mGetHandleCalled) {
251 ALOGE("Get handle called twice" );
252 return nullptr;
253 }
254 mGetHandleCalled = true;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700255 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800256}
257
258// ---------------------------------------------------------------------------
259// h/w composer set-up
260// ---------------------------------------------------------------------------
261
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700262static Rect reduce(const Rect& win, const Region& exclude) {
263 if (CC_LIKELY(exclude.isEmpty())) {
264 return win;
265 }
266 if (exclude.isRect()) {
267 return win.reduce(exclude.getBounds());
268 }
269 return Region(win).subtract(exclude).getBounds();
270}
271
Dan Stoza80d61162017-12-20 15:57:52 -0800272static FloatRect reduce(const FloatRect& win, const Region& exclude) {
273 if (CC_LIKELY(exclude.isEmpty())) {
274 return win;
275 }
276 // Convert through Rect (by rounding) for lack of FloatRegion
277 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
278}
279
Vishnu Nair4351ad52019-02-11 14:13:02 -0800280Rect Layer::getScreenBounds(bool reduceTransparentRegion) const {
Vishnu Nairf0c28512019-02-08 12:40:28 -0800281 if (!reduceTransparentRegion) {
282 return Rect{mScreenBounds};
283 }
284
285 FloatRect bounds = getBounds();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800286 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800287 // Transform to screen space.
288 bounds = t.transform(bounds);
289 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700290}
291
Vishnu Nair4351ad52019-02-11 14:13:02 -0800292FloatRect Layer::getBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000293 const State& s(getDrawingState());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800294 return getBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700295}
296
Vishnu Nairf0c28512019-02-08 12:40:28 -0800297FloatRect Layer::getBounds(const Region& activeTransparentRegion) const {
298 // Subtract the transparent region and snap to the bounds.
299 return reduce(mBounds, activeTransparentRegion);
300}
301
Vishnu Nairc652ff82019-03-15 12:48:54 -0700302ui::Transform Layer::getBufferScaleTransform() const {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800303 // If the layer is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
304 // it isFixedSize) then there may be additional scaling not accounted
Vishnu Nairc652ff82019-03-15 12:48:54 -0700305 // for in the layer transform.
chaviwd62d3062019-09-04 14:48:02 -0700306 if (!isFixedSize() || getBuffer() == nullptr) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700307 return {};
Vishnu Nair4351ad52019-02-11 14:13:02 -0800308 }
309
Marissa Wall290ad082019-03-06 13:23:47 -0800310 // If the layer is a buffer state layer, the active width and height
311 // could be infinite. In that case, return the effective transform.
312 const uint32_t activeWidth = getActiveWidth(getDrawingState());
313 const uint32_t activeHeight = getActiveHeight(getDrawingState());
314 if (activeWidth >= UINT32_MAX && activeHeight >= UINT32_MAX) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700315 return {};
Marissa Wall290ad082019-03-06 13:23:47 -0800316 }
317
chaviwd62d3062019-09-04 14:48:02 -0700318 int bufferWidth = getBuffer()->getWidth();
319 int bufferHeight = getBuffer()->getHeight();
Vishnu Nairc652ff82019-03-15 12:48:54 -0700320
chaviw4244e032019-09-04 11:27:49 -0700321 if (getBufferTransform() & NATIVE_WINDOW_TRANSFORM_ROT_90) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700322 std::swap(bufferWidth, bufferHeight);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800323 }
Vishnu Nairc652ff82019-03-15 12:48:54 -0700324
Marissa Wall290ad082019-03-06 13:23:47 -0800325 float sx = activeWidth / static_cast<float>(bufferWidth);
326 float sy = activeHeight / static_cast<float>(bufferHeight);
327
Vishnu Nair4351ad52019-02-11 14:13:02 -0800328 ui::Transform extraParentScaling;
329 extraParentScaling.set(sx, 0, 0, sy);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700330 return extraParentScaling;
331}
332
333ui::Transform Layer::getTransformWithScale(const ui::Transform& bufferScaleTransform) const {
334 // We need to mirror this scaling to child surfaces or we will break the contract where WM can
335 // treat child surfaces as pixels in the parent surface.
chaviwd62d3062019-09-04 14:48:02 -0700336 if (!isFixedSize() || getBuffer() == nullptr) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700337 return mEffectiveTransform;
338 }
339 return mEffectiveTransform * bufferScaleTransform;
340}
341
342FloatRect Layer::getBoundsPreScaling(const ui::Transform& bufferScaleTransform) const {
343 // We need the pre scaled layer bounds when computing child bounds to make sure the child is
344 // cropped to its parent layer after any buffer transform scaling is applied.
chaviwd62d3062019-09-04 14:48:02 -0700345 if (!isFixedSize() || getBuffer() == nullptr) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700346 return mBounds;
347 }
348 return bufferScaleTransform.inverse().transform(mBounds);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800349}
350
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700351void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform,
352 float parentShadowRadius) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800353 const State& s(getDrawingState());
354
355 // Calculate effective layer transform
356 mEffectiveTransform = parentTransform * getActiveTransform(s);
357
358 // Transform parent bounds to layer space
359 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
360
Vishnu Nairc652ff82019-03-15 12:48:54 -0700361 // Calculate source bounds
Vishnu Nair4351ad52019-02-11 14:13:02 -0800362 mSourceBounds = computeSourceBounds(parentBounds);
363
364 // Calculate bounds by croping diplay frame with layer crop and parent bounds
365 FloatRect bounds = mSourceBounds;
366 const Rect layerCrop = getCrop(s);
367 if (!layerCrop.isEmpty()) {
368 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
369 }
370 bounds = bounds.intersect(parentBounds);
371
372 mBounds = bounds;
373 mScreenBounds = mEffectiveTransform.transform(mBounds);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700374
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700375 // Use the layer's own shadow radius if set. Otherwise get the radius from
376 // parent.
377 if (s.shadowRadius > 0.f) {
378 mEffectiveShadowRadius = s.shadowRadius;
379 } else {
380 mEffectiveShadowRadius = parentShadowRadius;
381 }
382
383 // Shadow radius is passed down to only one layer so if the layer can draw shadows,
384 // don't pass it to its children.
385 const float childShadowRadius = canDrawShadows() ? 0.f : mEffectiveShadowRadius;
386
Vishnu Nairc652ff82019-03-15 12:48:54 -0700387 // Add any buffer scaling to the layer's children.
388 ui::Transform bufferScaleTransform = getBufferScaleTransform();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800389 for (const sp<Layer>& child : mDrawingChildren) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700390 child->computeBounds(getBoundsPreScaling(bufferScaleTransform),
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700391 getTransformWithScale(bufferScaleTransform), childShadowRadius);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800392 }
393}
394
Vishnu Nair60356342018-11-13 13:00:45 -0800395Rect Layer::getCroppedBufferSize(const State& s) const {
396 Rect size = getBufferSize(s);
397 Rect crop = getCrop(s);
398 if (!crop.isEmpty() && size.isValid()) {
399 size.intersect(crop, &size);
400 } else if (!crop.isEmpty()) {
401 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700402 }
Vishnu Nair60356342018-11-13 13:00:45 -0800403 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800404}
405
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700406void Layer::setupRoundedCornersCropCoordinates(Rect win,
407 const FloatRect& roundedCornersCrop) const {
408 // Translate win by the rounded corners rect coordinates, to have all values in
409 // layer coordinate space.
410 win.left -= roundedCornersCrop.left;
411 win.right -= roundedCornersCrop.left;
412 win.top -= roundedCornersCrop.top;
413 win.bottom -= roundedCornersCrop.top;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700414}
415
Lloyd Piquede196652020-01-22 17:29:58 -0800416void Layer::prepareBasicGeometryCompositionState() {
Lloyd Piquec6687342019-03-07 21:34:57 -0800417 const auto& drawingState{getDrawingState()};
418 const uint32_t layerStack = getLayerStack();
419 const auto alpha = static_cast<float>(getAlpha());
420 const bool opaque = isOpaque(drawingState);
421 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
422
423 auto blendMode = Hwc2::IComposerClient::BlendMode::NONE;
424 if (!opaque || alpha != 1.0f) {
425 blendMode = mPremultipliedAlpha ? Hwc2::IComposerClient::BlendMode::PREMULTIPLIED
426 : Hwc2::IComposerClient::BlendMode::COVERAGE;
427 }
428
Lloyd Piquede196652020-01-22 17:29:58 -0800429 auto* compositionState = editCompositionState();
430 compositionState->layerStackId =
Lloyd Piquec6687342019-03-07 21:34:57 -0800431 (layerStack != ~0u) ? std::make_optional(layerStack) : std::nullopt;
Lloyd Piquede196652020-01-22 17:29:58 -0800432 compositionState->internalOnly = getPrimaryDisplayOnly();
433 compositionState->isVisible = isVisible();
434 compositionState->isOpaque = opaque && !usesRoundedCorners && alpha == 1.f;
435 compositionState->shadowRadius = mEffectiveShadowRadius;
Lloyd Piquec6687342019-03-07 21:34:57 -0800436
Lloyd Piquede196652020-01-22 17:29:58 -0800437 compositionState->contentDirty = contentDirty;
Lloyd Piquec6687342019-03-07 21:34:57 -0800438 contentDirty = false;
439
Lloyd Piquede196652020-01-22 17:29:58 -0800440 compositionState->geomLayerBounds = mBounds;
441 compositionState->geomLayerTransform = getTransform();
442 compositionState->geomInverseLayerTransform = compositionState->geomLayerTransform.inverse();
443 compositionState->transparentRegionHint = getActiveTransparentRegion(drawingState);
Lloyd Piquec6687342019-03-07 21:34:57 -0800444
Lloyd Piquede196652020-01-22 17:29:58 -0800445 compositionState->blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
446 compositionState->alpha = alpha;
447 compositionState->backgroundBlurRadius = drawingState.backgroundBlurRadius;
Lloyd Piquec6687342019-03-07 21:34:57 -0800448}
449
Lloyd Piquede196652020-01-22 17:29:58 -0800450void Layer::prepareGeometryCompositionState() {
Lloyd Piquea83776c2019-01-29 18:42:32 -0800451 const auto& drawingState{getDrawingState()};
Mathias Agopian13127d82013-03-05 17:47:11 -0800452
Lloyd Piquea83776c2019-01-29 18:42:32 -0800453 int type = drawingState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
454 int appId = drawingState.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700455 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400456 if (parent.get()) {
457 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800458 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
459 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
Jiwen 'Steve' Cai736c74e2019-05-28 18:33:22 -0700460 if (parentType > 0 && parentAppId > 0) {
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800461 type = parentType;
462 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700463 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400464 }
465
Lloyd Piquede196652020-01-22 17:29:58 -0800466 auto* compositionState = editCompositionState();
David Sodman15094112018-10-11 09:39:37 -0700467
Lloyd Piquede196652020-01-22 17:29:58 -0800468 compositionState->geomBufferSize = getBufferSize(drawingState);
469 compositionState->geomContentCrop = getBufferCrop();
470 compositionState->geomCrop = getCrop(drawingState);
471 compositionState->geomBufferTransform = getBufferTransform();
472 compositionState->geomBufferUsesDisplayInverseTransform = getTransformToDisplayInverse();
473 compositionState->geomUsesSourceCrop = usesSourceCrop();
474 compositionState->isSecure = isSecure();
475
476 compositionState->type = type;
477 compositionState->appId = appId;
Lloyd Pique8d9f8362020-02-11 19:13:09 -0800478
479 compositionState->metadata.clear();
480 const auto& supportedMetadata = mFlinger->getHwComposer().getSupportedLayerGenericMetadata();
481 for (const auto& [key, mandatory] : supportedMetadata) {
482 const auto& genericLayerMetadataCompatibilityMap =
483 mFlinger->getGenericLayerMetadataKeyMap();
484 auto compatIter = genericLayerMetadataCompatibilityMap.find(key);
485 if (compatIter == std::end(genericLayerMetadataCompatibilityMap)) {
486 continue;
487 }
488 const uint32_t id = compatIter->second;
489
490 auto it = drawingState.metadata.mMap.find(id);
491 if (it == std::end(drawingState.metadata.mMap)) {
492 continue;
493 }
494
495 compositionState->metadata
496 .emplace(key, compositionengine::GenericLayerMetadataEntry{mandatory, it->second});
497 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800498}
David Sodmanba340492018-08-05 21:51:33 -0700499
Lloyd Piquede196652020-01-22 17:29:58 -0800500void Layer::preparePerFrameCompositionState() {
Lloyd Pique688abd42019-02-15 15:42:24 -0800501 const auto& drawingState{getDrawingState()};
Lloyd Piquede196652020-01-22 17:29:58 -0800502 auto* compositionState = editCompositionState();
Lloyd Piquef5275482019-01-29 18:42:42 -0800503
Lloyd Piquede196652020-01-22 17:29:58 -0800504 compositionState->forceClientComposition = false;
505
506 compositionState->isColorspaceAgnostic = isColorSpaceAgnostic();
507 compositionState->dataspace = getDataSpace();
508 compositionState->colorTransform = getColorTransform();
509 compositionState->colorTransformIsIdentity = !hasColorTransform();
510 compositionState->surfaceDamage = surfaceDamageRegion;
511 compositionState->hasProtectedContent = isProtected();
Lloyd Pique688abd42019-02-15 15:42:24 -0800512
513 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700514 const bool drawsShadows = mEffectiveShadowRadius != 0.f;
515
Lloyd Piquede196652020-01-22 17:29:58 -0800516 compositionState->isOpaque =
Lloyd Pique688abd42019-02-15 15:42:24 -0800517 isOpaque(drawingState) && !usesRoundedCorners && getAlpha() == 1.0_hf;
Lloyd Piquef5275482019-01-29 18:42:42 -0800518
519 // Force client composition for special cases known only to the front-end.
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700520 if (isHdrY410() || usesRoundedCorners || drawsShadows) {
Lloyd Piquede196652020-01-22 17:29:58 -0800521 compositionState->forceClientComposition = true;
Lloyd Piquef5275482019-01-29 18:42:42 -0800522 }
523}
524
Lloyd Piquede196652020-01-22 17:29:58 -0800525void Layer::prepareCursorCompositionState() {
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800526 const State& drawingState{getDrawingState()};
Lloyd Piquede196652020-01-22 17:29:58 -0800527 auto* compositionState = editCompositionState();
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800528
529 // Apply the layer's transform, followed by the display's global transform
530 // Here we're guaranteed that the layer's transform preserves rects
531 Rect win = getCroppedBufferSize(drawingState);
532 // Subtract the transparent region and snap to the bounds
533 Rect bounds = reduce(win, getActiveTransparentRegion(drawingState));
534 Rect frame(getTransform().transform(bounds));
535
Lloyd Piquede196652020-01-22 17:29:58 -0800536 compositionState->cursorFrame = frame;
537}
538
539sp<compositionengine::LayerFE> Layer::asLayerFE() const {
540 return const_cast<compositionengine::LayerFE*>(
541 static_cast<const compositionengine::LayerFE*>(this));
542}
543
544sp<compositionengine::LayerFE> Layer::getCompositionEngineLayerFE() const {
545 return nullptr;
546}
547
548compositionengine::LayerFECompositionState* Layer::editCompositionState() {
549 return nullptr;
550}
551
552const compositionengine::LayerFECompositionState* Layer::getCompositionState() const {
553 return nullptr;
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800554}
555
Lloyd Piquef16688f2019-02-19 17:47:57 -0800556bool Layer::onPreComposition(nsecs_t) {
557 return false;
558}
559
Lloyd Piquede196652020-01-22 17:29:58 -0800560void Layer::prepareCompositionState(compositionengine::LayerFE::StateSubset subset) {
Lloyd Piquec6687342019-03-07 21:34:57 -0800561 using StateSubset = compositionengine::LayerFE::StateSubset;
Lloyd Piquef5275482019-01-29 18:42:42 -0800562
Lloyd Piquec6687342019-03-07 21:34:57 -0800563 switch (subset) {
564 case StateSubset::BasicGeometry:
Lloyd Piquede196652020-01-22 17:29:58 -0800565 prepareBasicGeometryCompositionState();
Lloyd Piquec6687342019-03-07 21:34:57 -0800566 break;
567
568 case StateSubset::GeometryAndContent:
Lloyd Piquede196652020-01-22 17:29:58 -0800569 prepareBasicGeometryCompositionState();
570 prepareGeometryCompositionState();
571 preparePerFrameCompositionState();
Lloyd Piquec6687342019-03-07 21:34:57 -0800572 break;
573
574 case StateSubset::Content:
Lloyd Piquede196652020-01-22 17:29:58 -0800575 preparePerFrameCompositionState();
576 break;
577
578 case StateSubset::Cursor:
579 prepareCursorCompositionState();
Lloyd Piquec6687342019-03-07 21:34:57 -0800580 break;
581 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800582}
Mathias Agopian29a367b2011-07-12 14:51:45 -0700583
Lloyd Piquea83776c2019-01-29 18:42:32 -0800584const char* Layer::getDebugName() const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700585 return mName.c_str();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800586}
587
Mathias Agopian13127d82013-03-05 17:47:11 -0800588// ---------------------------------------------------------------------------
589// drawing...
590// ---------------------------------------------------------------------------
591
Vishnu Nair9b079a22020-01-21 14:36:08 -0800592std::optional<compositionengine::LayerFE::LayerSettings> Layer::prepareClientComposition(
Lloyd Piquef16688f2019-02-19 17:47:57 -0800593 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
Lloyd Piquede196652020-01-22 17:29:58 -0800594 if (!getCompositionState()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800595 return {};
596 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800597
Vishnu Nair4351ad52019-02-11 14:13:02 -0800598 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000599 half alpha = getAlpha();
Vishnu Nair9b079a22020-01-21 14:36:08 -0800600
601 compositionengine::LayerFE::LayerSettings layerSettings;
Lloyd Piquef16688f2019-02-19 17:47:57 -0800602 layerSettings.geometry.boundaries = bounds;
603 if (targetSettings.useIdentityTransform) {
604 layerSettings.geometry.positionTransform = mat4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000605 } else {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800606 layerSettings.geometry.positionTransform = getTransform().asMatrix4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000607 }
608
609 if (hasColorTransform()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800610 layerSettings.colorTransform = getColorTransform();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000611 }
612
613 const auto roundedCornerState = getRoundedCornerState();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800614 layerSettings.geometry.roundedCornersRadius = roundedCornerState.radius;
615 layerSettings.geometry.roundedCornersCrop = roundedCornerState.cropRect;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000616
Lloyd Piquef16688f2019-02-19 17:47:57 -0800617 layerSettings.alpha = alpha;
chaviw4244e032019-09-04 11:27:49 -0700618 layerSettings.sourceDataspace = getDataSpace();
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800619 layerSettings.backgroundBlurRadius = getBackgroundBlurRadius();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800620 return layerSettings;
Mathias Agopian13127d82013-03-05 17:47:11 -0800621}
622
Vishnu Nair9b079a22020-01-21 14:36:08 -0800623std::optional<compositionengine::LayerFE::LayerSettings> Layer::prepareShadowClientComposition(
624 const LayerFE::LayerSettings& casterLayerSettings, const Rect& displayViewport,
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800625 ui::Dataspace outputDataspace) {
626 renderengine::ShadowSettings shadow = getShadowSettings(displayViewport);
627 if (shadow.length <= 0.f) {
628 return {};
629 }
630
631 const float casterAlpha = casterLayerSettings.alpha;
632 const bool casterIsOpaque = ((casterLayerSettings.source.buffer.buffer != nullptr) &&
633 casterLayerSettings.source.buffer.isOpaque);
634
Vishnu Nair9b079a22020-01-21 14:36:08 -0800635 compositionengine::LayerFE::LayerSettings shadowLayer = casterLayerSettings;
636
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800637 shadowLayer.shadow = shadow;
Vishnu Naira483b4a2019-12-12 15:07:52 -0800638 shadowLayer.geometry.boundaries = mBounds; // ignore transparent region
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800639
640 // If the casting layer is translucent, we need to fill in the shadow underneath the layer.
641 // Otherwise the generated shadow will only be shown around the casting layer.
642 shadowLayer.shadow.casterIsTranslucent = !casterIsOpaque || (casterAlpha < 1.0f);
643 shadowLayer.shadow.ambientColor *= casterAlpha;
644 shadowLayer.shadow.spotColor *= casterAlpha;
645 shadowLayer.sourceDataspace = outputDataspace;
646 shadowLayer.source.buffer.buffer = nullptr;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800647 shadowLayer.source.buffer.fence = nullptr;
648 shadowLayer.frameNumber = 0;
649 shadowLayer.bufferId = 0;
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800650
651 if (shadowLayer.shadow.ambientColor.a <= 0.f && shadowLayer.shadow.spotColor.a <= 0.f) {
652 return {};
653 }
654
655 float casterCornerRadius = shadowLayer.geometry.roundedCornersRadius;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800656 const FloatRect& cornerRadiusCropRect = shadowLayer.geometry.roundedCornersCrop;
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800657 const FloatRect& casterRect = shadowLayer.geometry.boundaries;
658
659 // crop used to set the corner radius may be larger than the content rect. Adjust the corner
660 // radius accordingly.
661 if (casterCornerRadius > 0.f) {
662 float cropRectOffset = std::max(std::abs(cornerRadiusCropRect.top - casterRect.top),
663 std::abs(cornerRadiusCropRect.left - casterRect.left));
664 if (cropRectOffset > casterCornerRadius) {
665 casterCornerRadius = 0;
666 } else {
667 casterCornerRadius -= cropRectOffset;
668 }
669 shadowLayer.geometry.roundedCornersRadius = casterCornerRadius;
670 }
671
672 return shadowLayer;
673}
674
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800675Hwc2::IComposerClient::Composition Layer::getCompositionType(
676 const sp<const DisplayDevice>& display) const {
677 const auto outputLayer = findOutputLayerForDisplay(display);
678 LOG_FATAL_IF(!outputLayer);
679 return outputLayer->getState().hwc ? (*outputLayer->getState().hwc).hwcCompositionType
680 : Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800681}
682
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800683bool Layer::getClearClientTarget(const sp<const DisplayDevice>& display) const {
684 const auto outputLayer = findOutputLayerForDisplay(display);
685 LOG_FATAL_IF(!outputLayer);
686 return outputLayer->getState().clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800687}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800688
Dan Stozacac35382016-01-27 12:21:06 -0800689bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
690 if (point->getFrameNumber() <= mCurrentFrameNumber) {
691 // Don't bother with a SyncPoint, since we've already latched the
692 // relevant frame
693 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700694 }
Robert Carr2e102c92018-10-23 12:11:15 -0700695 if (isRemovedFromCurrentState()) {
696 return false;
697 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700698
Dan Stozacac35382016-01-27 12:21:06 -0800699 Mutex::Autolock lock(mLocalSyncPointMutex);
700 mLocalSyncPoints.push_back(point);
701 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700702}
703
Mathias Agopian13127d82013-03-05 17:47:11 -0800704// ----------------------------------------------------------------------------
705// local state
706// ----------------------------------------------------------------------------
707
David Sodman41fdfc92017-11-06 16:09:56 -0800708bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800709 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700710 return (s.flags & layer_state_t::eLayerSecure);
711}
712
Mathias Agopian13127d82013-03-05 17:47:11 -0800713// ----------------------------------------------------------------------------
714// transaction
715// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800716
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800717void Layer::pushPendingState() {
718 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700719 return;
720 }
Alec Mourie60041e2019-06-14 18:59:51 -0700721 ATRACE_CALL();
Dan Stoza7dde5992015-05-22 09:51:44 -0700722
Dan Stoza7dde5992015-05-22 09:51:44 -0700723 // If this transaction is waiting on the receipt of a frame, generate a sync
724 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700725 // We don't allow installing sync points after we are removed from the current state
726 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800727 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
728 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800729 if (barrierLayer == nullptr) {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700730 ALOGE("[%s] Unable to promote barrier Layer.", getDebugName());
Dan Stoza7dde5992015-05-22 09:51:44 -0700731 // If we can't promote the layer we are intended to wait on,
732 // then it is expired or otherwise invalid. Allow this transaction
733 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800734 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800735 } else {
chaviw43cb3cb2019-05-31 15:23:41 -0700736 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy, this);
Robert Carr0d480722017-01-10 16:42:54 -0800737 if (barrierLayer->addSyncPoint(syncPoint)) {
Alec Mourie60041e2019-06-14 18:59:51 -0700738 std::stringstream ss;
739 ss << "Adding sync point " << mCurrentState.frameNumber_legacy;
740 ATRACE_NAME(ss.str().c_str());
Dan Stozacac35382016-01-27 12:21:06 -0800741 mRemoteSyncPoints.push_back(std::move(syncPoint));
742 } else {
743 // We already missed the frame we're supposed to synchronize
744 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800745 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800746 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700747 }
748
Dan Stoza7dde5992015-05-22 09:51:44 -0700749 // Wake us up to check if the frame has been received
750 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700751 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700752 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800753 mPendingStates.push_back(mCurrentState);
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700754 ATRACE_INT(mTransactionName.c_str(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700755}
756
Pablo Ceballos05289c22016-04-14 15:49:55 -0700757void Layer::popPendingState(State* stateToCommit) {
Alec Mourie60041e2019-06-14 18:59:51 -0700758 ATRACE_CALL();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800759 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700760
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800761 mPendingStates.removeAt(0);
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700762 ATRACE_INT(mTransactionName.c_str(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700763}
764
Pablo Ceballos05289c22016-04-14 15:49:55 -0700765bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700766 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800767 while (!mPendingStates.empty()) {
768 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700769 if (mRemoteSyncPoints.empty()) {
770 // If we don't have a sync point for this, apply it anyway. It
771 // will be visually wrong, but it should keep us from getting
772 // into too much trouble.
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700773 ALOGE("[%s] No local sync point found", getDebugName());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700774 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700775 stateUpdateAvailable = true;
776 continue;
777 }
778
Marissa Wallf58c14b2018-07-24 10:50:43 -0700779 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800780 mPendingStates[0].frameNumber_legacy) {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700781 ALOGE("[%s] Unexpected sync point frame number found", getDebugName());
Dan Stozacac35382016-01-27 12:21:06 -0800782
783 // Signal our end of the sync point and then dispose of it
784 mRemoteSyncPoints.front()->setTransactionApplied();
785 mRemoteSyncPoints.pop_front();
786 continue;
787 }
788
Dan Stoza7dde5992015-05-22 09:51:44 -0700789 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
Alec Mourie60041e2019-06-14 18:59:51 -0700790 ATRACE_NAME("frameIsAvailable");
Dan Stoza7dde5992015-05-22 09:51:44 -0700791 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700792 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700793 stateUpdateAvailable = true;
794
795 // Signal our end of the sync point and then dispose of it
796 mRemoteSyncPoints.front()->setTransactionApplied();
797 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800798 } else {
Alec Mourie60041e2019-06-14 18:59:51 -0700799 ATRACE_NAME("!frameIsAvailable");
Dan Stoza792e5292016-02-11 11:43:58 -0800800 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700801 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700802 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700803 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700804 stateUpdateAvailable = true;
805 }
806 }
807
808 // If we still have pending updates, wake SurfaceFlinger back up and point
809 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800810 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700811 setTransactionFlags(eTransactionNeeded);
812 mFlinger->setTransactionFlags(eTraversalNeeded);
813 }
814
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800815 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700816 return stateUpdateAvailable;
817}
818
Marissa Wall61c58622018-07-18 10:12:20 -0700819uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000820 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800821
Marissa Wall61c58622018-07-18 10:12:20 -0700822 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
823 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700824
David Sodmaneb085e02017-10-05 18:49:04 -0700825 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700826 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000827 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800828 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
829 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
830 " requested={ wh={%4u,%4u} }}\n"
831 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
832 " requested={ wh={%4u,%4u} }}\n",
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700833 this, getName().c_str(), getBufferTransform(), getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700834 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
835 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
836 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
837 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
838 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700839 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
840 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
841 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800842 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700843
Robert Carre392b552017-09-19 12:16:05 -0700844 // Don't let Layer::doTransaction update the drawing state
845 // if we have a pending resize, unless we are in fixed-size mode.
846 // the drawing state will be updated only once we receive a buffer
847 // with the correct size.
848 //
849 // In particular, we want to make sure the clip (which is part
850 // of the geometry state) is latched together with the size but is
851 // latched immediately when no resizing is involved.
852 //
853 // If a sideband stream is attached, however, we want to skip this
854 // optimization so that transactions aren't missed when a buffer
855 // never arrives
856 //
857 // In the case that we don't have a buffer we ignore other factors
858 // and avoid entering the resizePending state. At a high level the
859 // resizePending state is to avoid applying the state of the new buffer
860 // to the old buffer. However in the state where we don't have an old buffer
861 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700862 const bool resizePending =
863 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
864 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
chaviwd62d3062019-09-04 14:48:02 -0700865 (getBuffer() != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700866 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800867 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700868 flags |= eDontUpdateGeometryState;
869 }
870 }
871
Robert Carr7bf247e2017-05-18 14:02:49 -0700872 // Here we apply various requested geometry states, depending on our
873 // latching configuration. See Layer.h for a detailed discussion of
874 // how geometry latching is controlled.
875 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000876 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700877
Robert Carr7bf247e2017-05-18 14:02:49 -0700878 // There is an awkward asymmetry in the handling of the crop states in the position
879 // states, as can be seen below. Largely this arises from position and transform
880 // being stored in the same data structure while having different latching rules.
881 // b/38182305
882 //
Marissa Wall61c58622018-07-18 10:12:20 -0700883 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700884 // applyPendingStates in the presence of deferred transactions.
chaviw214c89d2019-09-04 16:03:53 -0700885 editCurrentState.active_legacy = editCurrentState.requested_legacy;
886 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Mathias Agopian13127d82013-03-05 17:47:11 -0800887 }
888
Marissa Wall61c58622018-07-18 10:12:20 -0700889 return flags;
890}
891
892uint32_t Layer::doTransaction(uint32_t flags) {
893 ATRACE_CALL();
894
chaviw5aedec92018-10-22 10:40:38 -0700895 if (mLayerDetached) {
Robert Carr321e83c2019-08-19 15:49:30 -0700896 // Ensure BLAST buffer callbacks are processed.
897 // detachChildren and mLayerDetached were implemented to avoid geometry updates
898 // to layers in the cases of animation. For BufferQueue layers buffers are still
899 // consumed as normal. This is useful as otherwise the client could get hung
900 // inevitably waiting on a buffer to return. We recreate this semantic for BufferQueue
901 // even though it is a little consistent. detachChildren is shortly slated for removal
902 // by the hierarchy mirroring work so we don't need to worry about it too much.
Valerie Hau7618b232020-01-09 16:03:08 -0800903 forceSendCallbacks();
Robert Carr321e83c2019-08-19 15:49:30 -0700904 mCurrentState.callbackHandles = {};
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800905 return flags;
906 }
907
908 if (mChildrenChanged) {
909 flags |= eVisibleRegion;
910 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -0700911 }
912
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800913 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +0000914 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -0700915 if (!applyPendingStates(&c)) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800916 return flags;
Marissa Wall61c58622018-07-18 10:12:20 -0700917 }
918
919 flags = doTransactionResize(flags, &c);
920
Alec Mourib416efd2018-09-06 21:01:59 +0000921 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700922
923 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800924 // invalidate and recompute the visible regions if needed
925 flags |= Layer::eVisibleRegion;
926 }
927
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700928 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800929 // invalidate and recompute the visible regions if needed
930 flags |= eVisibleRegion;
931 this->contentDirty = true;
932
933 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -0700934 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -0700935 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -0800936 }
937
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800938 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -0700939 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800940 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -0700941 }
942
Mathias Agopian13127d82013-03-05 17:47:11 -0800943 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -0700944 commitTransaction(c);
Vishnu Nair8406fd72019-07-30 11:29:31 -0700945 mPendingStatesSnapshot = mPendingStates;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800946 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -0800947 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800948}
949
Pablo Ceballos05289c22016-04-14 15:49:55 -0700950void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800951 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700952}
953
Mathias Agopian13127d82013-03-05 17:47:11 -0800954uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800955 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800956}
957
958uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800959 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -0800960}
961
chaviw214c89d2019-09-04 16:03:53 -0700962bool Layer::setPosition(float x, float y) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800963 if (mCurrentState.requested_legacy.transform.tx() == x &&
964 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -0800965 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800966 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -0700967
968 // We update the requested and active position simultaneously because
969 // we want to apply the position portion of the transform matrix immediately,
970 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800971 mCurrentState.requested_legacy.transform.set(x, y);
chaviw214c89d2019-09-04 16:03:53 -0700972 // Here we directly update the active state
973 // unlike other setters, because we store it within
974 // the transform, but use different latching rules.
975 // b/38182305
976 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr69663fb2016-03-27 19:59:19 -0700977
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800978 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800979 setTransactionFlags(eTransactionNeeded);
980 return true;
981}
Robert Carr82364e32016-05-15 11:27:47 -0700982
Robert Carr1f0a16a2016-10-24 16:27:39 -0700983bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
984 ssize_t idx = mCurrentChildren.indexOf(childLayer);
985 if (idx < 0) {
986 return false;
987 }
988 if (childLayer->setLayer(z)) {
989 mCurrentChildren.removeAt(idx);
990 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800991 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700992 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800993 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700994}
995
Robert Carr503c7042017-09-27 15:06:08 -0700996bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
997 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
998 ssize_t idx = mCurrentChildren.indexOf(childLayer);
999 if (idx < 0) {
1000 return false;
1001 }
1002 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1003 mCurrentChildren.removeAt(idx);
1004 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001005 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001006 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001007 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001008}
1009
Robert Carrae060832016-11-28 10:51:00 -08001010bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001011 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
1012 mCurrentState.sequence++;
1013 mCurrentState.z = z;
1014 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001015
1016 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001017 if (mCurrentState.zOrderRelativeOf != nullptr) {
1018 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -07001019 if (strongRelative != nullptr) {
1020 strongRelative->removeZOrderRelative(this);
1021 }
chaviw606e5cf2019-03-01 10:12:10 -08001022 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -07001023 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001024 setTransactionFlags(eTransactionNeeded);
1025 return true;
1026}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001027
Robert Carrdb66e622017-04-10 16:55:57 -07001028void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001029 mCurrentState.zOrderRelatives.remove(relative);
1030 mCurrentState.sequence++;
1031 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001032 setTransactionFlags(eTransactionNeeded);
1033}
1034
1035void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001036 mCurrentState.zOrderRelatives.add(relative);
1037 mCurrentState.modified = true;
1038 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -07001039 setTransactionFlags(eTransactionNeeded);
1040}
1041
chaviw606e5cf2019-03-01 10:12:10 -08001042void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
1043 mCurrentState.zOrderRelativeOf = relativeOf;
1044 mCurrentState.sequence++;
1045 mCurrentState.modified = true;
chaviwbdb8b802019-10-14 09:17:12 -07001046 mCurrentState.isRelativeOf = relativeOf != nullptr;
1047
chaviw606e5cf2019-03-01 10:12:10 -08001048 setTransactionFlags(eTransactionNeeded);
1049}
1050
Robert Carr503d2bd2017-12-04 15:49:47 -08001051bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001052 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1053 if (handle == nullptr) {
1054 return false;
1055 }
1056 sp<Layer> relative = handle->owner.promote();
1057 if (relative == nullptr) {
1058 return false;
1059 }
1060
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001061 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1062 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001063 return false;
1064 }
1065
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001066 mCurrentState.sequence++;
1067 mCurrentState.modified = true;
1068 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001069
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001070 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -07001071 if (oldZOrderRelativeOf != nullptr) {
1072 oldZOrderRelativeOf->removeZOrderRelative(this);
1073 }
chaviw606e5cf2019-03-01 10:12:10 -08001074 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -07001075 relative->addZOrderRelative(this);
1076
1077 setTransactionFlags(eTransactionNeeded);
1078
1079 return true;
1080}
1081
Mathias Agopian13127d82013-03-05 17:47:11 -08001082bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001083 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001084 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001085 mCurrentState.requested_legacy.w = w;
1086 mCurrentState.requested_legacy.h = h;
1087 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001088 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001089
1090 // record the new size, from this point on, when the client request
1091 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001092 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001093 return true;
1094}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001095bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001096 if (mCurrentState.color.a == alpha) return false;
1097 mCurrentState.sequence++;
1098 mCurrentState.color.a = alpha;
1099 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001100 setTransactionFlags(eTransactionNeeded);
1101 return true;
1102}
chaviw13fdc492017-06-27 12:40:18 -07001103
Valerie Haudd0b7572019-01-29 14:59:27 -08001104bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1105 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001106 return false;
Valerie Hauaa194562019-02-05 16:21:38 -08001107 }
1108 mCurrentState.sequence++;
1109 mCurrentState.modified = true;
1110 setTransactionFlags(eTransactionNeeded);
1111
1112 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001113 // create background color layer if one does not yet exist
1114 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001115 std::string name = mName + "BackgroundColorLayer";
Lloyd Pique958be492019-03-28 15:34:59 -07001116 mCurrentState.bgColorLayer = mFlinger->getFactory().createColorLayer(
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001117 LayerCreationArgs(mFlinger.get(), nullptr, std::move(name), 0, 0, flags,
1118 LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -07001119
Valerie Haudd0b7572019-01-29 14:59:27 -08001120 // add to child list
1121 addChild(mCurrentState.bgColorLayer);
1122 mFlinger->mLayersAdded = true;
1123 // set up SF to handle added color layer
1124 if (isRemovedFromCurrentState()) {
1125 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1126 }
1127 mFlinger->setTransactionFlags(eTransactionNeeded);
1128 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1129 mCurrentState.bgColorLayer->reparent(nullptr);
1130 mCurrentState.bgColorLayer = nullptr;
1131 return true;
1132 }
1133
1134 mCurrentState.bgColorLayer->setColor(color);
1135 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1136 mCurrentState.bgColorLayer->setAlpha(alpha);
1137 mCurrentState.bgColorLayer->setDataspace(dataspace);
1138
chaviw13fdc492017-06-27 12:40:18 -07001139 return true;
1140}
1141
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001142bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001143 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001144
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001145 mCurrentState.sequence++;
1146 mCurrentState.cornerRadius = cornerRadius;
1147 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001148 setTransactionFlags(eTransactionNeeded);
1149 return true;
1150}
1151
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001152bool Layer::setBackgroundBlurRadius(int backgroundBlurRadius) {
1153 if (mCurrentState.backgroundBlurRadius == backgroundBlurRadius) return false;
1154
1155 mCurrentState.sequence++;
1156 mCurrentState.backgroundBlurRadius = backgroundBlurRadius;
1157 mCurrentState.modified = true;
1158 setTransactionFlags(eTransactionNeeded);
1159 return true;
1160}
1161
Robert Carrd4ae7f32018-06-07 16:10:57 -07001162bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1163 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001164 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001165 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1166
1167 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1168 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1169 return false;
1170 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001171 mCurrentState.sequence++;
1172 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1173 matrix.dsdy);
1174 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001175 setTransactionFlags(eTransactionNeeded);
1176 return true;
1177}
Marissa Wall61c58622018-07-18 10:12:20 -07001178
Mathias Agopian13127d82013-03-05 17:47:11 -08001179bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001180 mCurrentState.requestedTransparentRegion_legacy = transparent;
1181 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001182 setTransactionFlags(eTransactionNeeded);
1183 return true;
1184}
Ana Krulecc84d09b2019-11-02 23:10:29 +01001185
Mathias Agopian13127d82013-03-05 17:47:11 -08001186bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001187 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1188 if (mCurrentState.flags == newFlags) return false;
1189 mCurrentState.sequence++;
1190 mCurrentState.flags = newFlags;
1191 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001192 setTransactionFlags(eTransactionNeeded);
1193 return true;
1194}
Robert Carr99e27f02016-06-16 15:18:02 -07001195
chaviw214c89d2019-09-04 16:03:53 -07001196bool Layer::setCrop_legacy(const Rect& crop) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001197 if (mCurrentState.requestedCrop_legacy == crop) return false;
1198 mCurrentState.sequence++;
1199 mCurrentState.requestedCrop_legacy = crop;
chaviw214c89d2019-09-04 16:03:53 -07001200 mCurrentState.crop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001201
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001202 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001203 setTransactionFlags(eTransactionNeeded);
1204 return true;
1205}
Robert Carr8d5227b2017-03-16 15:41:03 -07001206
Robert Carrc3574f72016-03-24 12:19:32 -07001207bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001208 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001209 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001210 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001211 return true;
1212}
1213
Evan Roskyef876c92019-01-25 17:46:06 -08001214bool Layer::setMetadata(const LayerMetadata& data) {
1215 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001216 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001217 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001218 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001219 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001220}
1221
Mathias Agopian13127d82013-03-05 17:47:11 -08001222bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001223 if (mCurrentState.layerStack == layerStack) return false;
1224 mCurrentState.sequence++;
1225 mCurrentState.layerStack = layerStack;
1226 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001227 setTransactionFlags(eTransactionNeeded);
1228 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001229}
1230
Peiyong Linc502cb72019-03-01 15:00:23 -08001231bool Layer::setColorSpaceAgnostic(const bool agnostic) {
1232 if (mCurrentState.colorSpaceAgnostic == agnostic) {
1233 return false;
1234 }
1235 mCurrentState.sequence++;
1236 mCurrentState.colorSpaceAgnostic = agnostic;
1237 mCurrentState.modified = true;
1238 setTransactionFlags(eTransactionNeeded);
1239 return true;
1240}
1241
Ana Krulecc84d09b2019-11-02 23:10:29 +01001242bool Layer::setFrameRateSelectionPriority(int32_t priority) {
1243 if (mCurrentState.frameRateSelectionPriority == priority) return false;
1244 mCurrentState.frameRateSelectionPriority = priority;
1245 mCurrentState.sequence++;
1246 mCurrentState.modified = true;
1247 setTransactionFlags(eTransactionNeeded);
1248 return true;
1249}
1250
1251int32_t Layer::getFrameRateSelectionPriority() const {
1252 // Check if layer has priority set.
1253 if (mDrawingState.frameRateSelectionPriority != PRIORITY_UNSET) {
1254 return mDrawingState.frameRateSelectionPriority;
1255 }
1256 // If not, search whether its parents have it set.
1257 sp<Layer> parent = getParent();
1258 if (parent != nullptr) {
1259 return parent->getFrameRateSelectionPriority();
1260 }
1261
1262 return Layer::PRIORITY_UNSET;
1263}
1264
Robert Carr1f0a16a2016-10-24 16:27:39 -07001265uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001266 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001267 if (p == nullptr) {
1268 return getDrawingState().layerStack;
1269 }
1270 return p->getLayerStack();
1271}
1272
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001273bool Layer::setShadowRadius(float shadowRadius) {
1274 if (mCurrentState.shadowRadius == shadowRadius) {
1275 return false;
1276 }
1277
1278 mCurrentState.sequence++;
1279 mCurrentState.shadowRadius = shadowRadius;
1280 mCurrentState.modified = true;
1281 setTransactionFlags(eTransactionNeeded);
1282 return true;
1283}
1284
Ady Abraham71c437d2020-01-31 15:56:57 -08001285bool Layer::setFrameRate(FrameRate frameRate) {
Ana Krulec3803b8d2020-02-03 16:35:46 -08001286 if (!mFlinger->useFrameRateApi) {
1287 return false;
1288 }
Steven Thomas3172e202020-01-06 19:25:30 -08001289 if (mCurrentState.frameRate == frameRate) {
1290 return false;
1291 }
1292
1293 mCurrentState.sequence++;
1294 mCurrentState.frameRate = frameRate;
1295 mCurrentState.modified = true;
1296 setTransactionFlags(eTransactionNeeded);
1297 return true;
1298}
1299
Ady Abraham71c437d2020-01-31 15:56:57 -08001300Layer::FrameRate Layer::getFrameRate() const {
1301 return getDrawingState().frameRate;
Steven Thomas3172e202020-01-06 19:25:30 -08001302}
1303
Marissa Wallf58c14b2018-07-24 10:50:43 -07001304void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Alec Mourie60041e2019-06-14 18:59:51 -07001305 ATRACE_CALL();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001306 mCurrentState.barrierLayer_legacy = barrierLayer;
1307 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001308 // We don't set eTransactionNeeded, because just receiving a deferral
1309 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001310 mCurrentState.modified = true;
1311 pushPendingState();
1312 mCurrentState.barrierLayer_legacy = nullptr;
1313 mCurrentState.frameNumber_legacy = 0;
1314 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001315}
1316
Marissa Wallf58c14b2018-07-24 10:50:43 -07001317void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001318 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001319 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001320}
1321
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001322// ----------------------------------------------------------------------------
1323// pageflip handling...
1324// ----------------------------------------------------------------------------
1325
Robert Carr1f0a16a2016-10-24 16:27:39 -07001326bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001327 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001328 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001329 if (parent != nullptr && parent->isHiddenByPolicy()) {
1330 return true;
1331 }
Robert Carr1c5481e2019-07-01 14:42:27 -07001332 if (usingRelativeZ(LayerVector::StateSet::Drawing)) {
1333 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
1334 if (zOrderRelativeOf != nullptr) {
1335 if (zOrderRelativeOf->isHiddenByPolicy()) {
1336 return true;
1337 }
1338 }
1339 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001340 return s.flags & layer_state_t::eLayerHidden;
1341}
1342
David Sodman41fdfc92017-11-06 16:09:56 -08001343uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001344 // TODO: should we do something special if mSecure is set?
1345 if (mProtectedByApp) {
1346 // need a hardware-protected path to external video sink
1347 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001348 }
Riley Andrews03414a12014-07-01 14:22:59 -07001349 if (mPotentialCursor) {
1350 usage |= GraphicBuffer::USAGE_CURSOR;
1351 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001352 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001353 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001354}
1355
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001356void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001357 uint32_t orientation = 0;
Vishnu Nair5eb3f062019-04-08 08:21:03 -07001358 // Disable setting transform hint if the debug flag is set.
1359 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001360 // The transform hint is used to improve performance, but we can
1361 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001362 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001363 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001364 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001365 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001366 orientation = 0;
1367 }
1368 }
David Sodmaneb085e02017-10-05 18:49:04 -07001369 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001370}
1371
Mathias Agopian13127d82013-03-05 17:47:11 -08001372// ----------------------------------------------------------------------------
1373// debugging
1374// ----------------------------------------------------------------------------
1375
Marissa Wall61c58622018-07-18 10:12:20 -07001376// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001377LayerDebugInfo Layer::getLayerDebugInfo() const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001378 using namespace std::string_literals;
1379
Kalle Raitaa099a242017-01-11 11:17:29 -08001380 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001381 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001382 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001383 sp<Layer> parent = getParent();
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001384 info.mParentName = parent ? parent->getName() : "none"s;
chaviw8a01fa42019-08-19 12:39:31 -07001385 info.mType = getType();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001386 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Lloyd Piquea2468662019-03-07 21:31:06 -08001387
1388 info.mVisibleRegion = debugGetVisibleRegionOnDefaultDisplay();
Kalle Raitaa099a242017-01-11 11:17:29 -08001389 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001390 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001391 info.mX = ds.active_legacy.transform.tx();
1392 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001393 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001394 info.mWidth = ds.active_legacy.w;
1395 info.mHeight = ds.active_legacy.h;
1396 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001397 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001398 info.mFlags = ds.flags;
1399 info.mPixelFormat = getPixelFormat();
chaviw4244e032019-09-04 11:27:49 -07001400 info.mDataSpace = static_cast<android_dataspace>(getDataSpace());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001401 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1402 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1403 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1404 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001405 {
chaviwd62d3062019-09-04 14:48:02 -07001406 sp<const GraphicBuffer> buffer = getBuffer();
David Sodman5b4cffc2017-11-23 13:20:29 -08001407 if (buffer != 0) {
1408 info.mActiveBufferWidth = buffer->getWidth();
1409 info.mActiveBufferHeight = buffer->getHeight();
1410 info.mActiveBufferStride = buffer->getStride();
1411 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001412 } else {
1413 info.mActiveBufferWidth = 0;
1414 info.mActiveBufferHeight = 0;
1415 info.mActiveBufferStride = 0;
1416 info.mActiveBufferFormat = 0;
1417 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001418 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001419 info.mNumQueuedFrames = getQueuedFrameCount();
1420 info.mRefreshPending = isBufferLatched();
1421 info.mIsOpaque = isOpaque(ds);
1422 info.mContentDirty = contentDirty;
1423 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001424}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001425
Yiwei Zhang5434a782018-12-05 18:06:32 -08001426void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001427 result.append("-------------------------------");
1428 result.append("-------------------------------");
1429 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001430 result.append(" Layer name\n");
1431 result.append(" Z | ");
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001432 result.append(" Window Type | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001433 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001434 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001435 result.append(" Disp Frame (LTRB) | ");
1436 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001437 result.append("-------------------------------");
1438 result.append("-------------------------------");
1439 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001440}
1441
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001442void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1443 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1444 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001445 return;
1446 }
1447
Yiwei Zhang5434a782018-12-05 18:06:32 -08001448 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001449 if (mName.length() > 77) {
1450 std::string shortened;
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001451 shortened.append(mName, 0, 36);
Dan Stozae22aec72016-08-01 13:20:59 -07001452 shortened.append("[...]");
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001453 shortened.append(mName, mName.length() - 36);
1454 name = std::move(shortened);
Dan Stozae22aec72016-08-01 13:20:59 -07001455 } else {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001456 name = mName;
Dan Stozae22aec72016-08-01 13:20:59 -07001457 }
1458
Yiwei Zhang5434a782018-12-05 18:06:32 -08001459 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001460
Alec Mourib416efd2018-09-06 21:01:59 +00001461 const State& layerState(getDrawingState());
Lloyd Piquede196652020-01-22 17:29:58 -08001462 const auto& outputLayerState = outputLayer->getState();
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001463
Chia-I Wu1e043612018-03-01 09:45:09 -08001464 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001465 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001466 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001467 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001468 }
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001469 StringAppendF(&result, " %10d | ", mWindowType);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001470 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
Lloyd Piquede196652020-01-22 17:29:58 -08001471 StringAppendF(&result, "%10s | ", toString(outputLayerState.bufferTransform).c_str());
1472 const Rect& frame = outputLayerState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001473 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Piquede196652020-01-22 17:29:58 -08001474 const FloatRect& crop = outputLayerState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001475 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1476 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001477
Yichi Chen6ca35192018-05-29 12:20:43 +08001478 result.append("- - - - - - - - - - - - - - - -");
1479 result.append("- - - - - - - - - - - - - - - -");
1480 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001481}
Dan Stozae22aec72016-08-01 13:20:59 -07001482
Yiwei Zhang5434a782018-12-05 18:06:32 -08001483void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001484 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001485}
1486
Svetoslavd85084b2014-03-20 10:28:31 -07001487void Layer::clearFrameStats() {
1488 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001489}
1490
Jamie Gennis6547ff42013-07-16 20:12:42 -07001491void Layer::logFrameStats() {
1492 mFrameTracker.logAndResetStats(mName);
1493}
1494
Svetoslavd85084b2014-03-20 10:28:31 -07001495void Layer::getFrameStats(FrameStats* outStats) const {
1496 mFrameTracker.getStats(outStats);
1497}
1498
Yiwei Zhang5434a782018-12-05 18:06:32 -08001499void Layer::dumpFrameEvents(std::string& result) {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001500 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().c_str(), getType(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001501 Mutex::Autolock lock(mFrameEventHistoryMutex);
1502 mFrameEventHistory.checkFencesForCompletion();
1503 mFrameEventHistory.dump(result);
1504}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001505
Vishnu Nair0f085c62019-08-30 08:49:12 -07001506void Layer::dumpCallingUidPid(std::string& result) const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001507 StringAppendF(&result, "Layer %s (%s) pid:%d uid:%d\n", getName().c_str(), getType(),
Vishnu Nair0f085c62019-08-30 08:49:12 -07001508 mCallingPid, mCallingUid);
1509}
1510
Brian Anderson5ea5e592016-12-01 16:54:33 -08001511void Layer::onDisconnect() {
1512 Mutex::Autolock lock(mFrameEventHistoryMutex);
1513 mFrameEventHistory.onDisconnect();
Yiwei Zhang1a88c402019-11-18 10:43:58 -08001514 const int32_t layerId = getSequence();
1515 mFlinger->mTimeStats->onDestroy(layerId);
1516 mFlinger->mFrameTracer->onDestroy(layerId);
Brian Anderson5ea5e592016-12-01 16:54:33 -08001517}
1518
Brian Anderson3890c392016-07-25 12:48:08 -07001519void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001520 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001521 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001522 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1523 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhang487340f2019-11-18 17:42:12 -08001524 mFlinger->mTimeStats->setAcquireFence(getSequence(), newTimestamps->frameNumber,
1525 newTimestamps->acquireFence);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001526 }
1527
Brian Andersond6927fb2016-07-23 23:37:30 -07001528 Mutex::Autolock lock(mFrameEventHistoryMutex);
1529 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001530 // If there are any unsignaled fences in the aquire timeline at this
1531 // point, the previously queued frame hasn't been latched yet. Go ahead
1532 // and try to get the signal time here so the syscall is taken out of
1533 // the main thread's critical path.
1534 mAcquireTimeline.updateSignalTimes();
1535 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001536 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001537 mFrameEventHistory.addQueue(*newTimestamps);
1538 }
1539
Brian Anderson3890c392016-07-25 12:48:08 -07001540 if (outDelta) {
1541 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001542 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001543}
Dan Stozae77c7662016-05-13 11:37:28 -07001544
Chia-I Wu98f1c102017-05-30 14:54:08 -07001545size_t Layer::getChildrenCount() const {
1546 size_t count = 0;
1547 for (const sp<Layer>& child : mCurrentChildren) {
1548 count += 1 + child->getChildrenCount();
1549 }
1550 return count;
1551}
1552
Robert Carr1f0a16a2016-10-24 16:27:39 -07001553void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001554 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001555 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001556
Robert Carr1f0a16a2016-10-24 16:27:39 -07001557 mCurrentChildren.add(layer);
1558 layer->setParent(this);
1559}
1560
1561ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001562 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001563 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001564
Robert Carr1323c952019-01-28 18:13:27 -08001565 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001566 return mCurrentChildren.remove(layer);
1567}
1568
Robert Carr1db73f62016-12-21 12:58:51 -08001569bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1570 sp<Handle> handle = nullptr;
1571 sp<Layer> newParent = nullptr;
1572 if (newParentHandle == nullptr) {
1573 return false;
1574 }
1575 handle = static_cast<Handle*>(newParentHandle.get());
1576 newParent = handle->owner.promote();
1577 if (newParent == nullptr) {
1578 ALOGE("Unable to promote Layer handle");
1579 return false;
1580 }
1581
chaviw5aedec92018-10-22 10:40:38 -07001582 if (attachChildren()) {
1583 setTransactionFlags(eTransactionNeeded);
1584 }
Robert Carr1db73f62016-12-21 12:58:51 -08001585 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001586 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001587 }
1588 mCurrentChildren.clear();
1589
1590 return true;
1591}
1592
Robert Carr15eae092018-03-23 13:43:53 -07001593void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001594 for (const sp<Layer>& child : mDrawingChildren) {
1595 child->mDrawingParent = newParent;
Vishnu Nairc652ff82019-03-15 12:48:54 -07001596 child->computeBounds(newParent->mBounds,
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001597 newParent->getTransformWithScale(newParent->getBufferScaleTransform()),
1598 newParent->mEffectiveShadowRadius);
Robert Carr578038f2018-03-09 12:25:24 -08001599 }
1600}
1601
chaviwf1961f72017-09-18 16:41:07 -07001602bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001603 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001604
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001605 // While layers are detached, we allow most operations
1606 // and simply halt performing the actual transaction. However
1607 // for reparent != null we would enter the mRemovedFromCurrentState
1608 // state, regardless of whether doTransaction was called, and
1609 // so we need to prevent the update here.
1610 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001611 return false;
1612 }
1613
Robert Carr54cf5b12019-01-25 14:02:28 -08001614 sp<Layer> newParent;
1615 if (newParentHandle != nullptr) {
1616 auto handle = static_cast<Handle*>(newParentHandle.get());
1617 newParent = handle->owner.promote();
1618 if (newParent == nullptr) {
1619 ALOGE("Unable to promote Layer handle");
1620 return false;
1621 }
1622 if (newParent == this) {
1623 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1624 return false;
1625 }
1626 }
1627
chaviwf1961f72017-09-18 16:41:07 -07001628 sp<Layer> parent = getParent();
1629 if (parent != nullptr) {
1630 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001631 }
1632
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001633 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001634 newParent->addChild(this);
1635 if (!newParent->isRemovedFromCurrentState()) {
1636 addToCurrentState();
1637 } else {
1638 onRemovedFromCurrentState();
1639 }
1640
1641 if (mLayerDetached) {
1642 mLayerDetached = false;
1643 callSetTransactionFlags = true;
1644 }
1645 } else {
1646 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001647 }
1648
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001649 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001650 setTransactionFlags(eTransactionNeeded);
1651 }
chaviw06178942017-07-27 10:25:59 -07001652 return true;
1653}
1654
Robert Carr9524cb32017-02-13 11:32:32 -08001655bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001656 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001657 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001658 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001659 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001660 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001661 child->detachChildren();
chaviw43cb3cb2019-05-31 15:23:41 -07001662 child->removeRemoteSyncPoints();
Robert Carr9524cb32017-02-13 11:32:32 -08001663 }
Robert Carr7f619b22017-11-06 12:56:35 -08001664 }
Robert Carr9524cb32017-02-13 11:32:32 -08001665
1666 return true;
1667}
1668
chaviw5aedec92018-10-22 10:40:38 -07001669bool Layer::attachChildren() {
1670 bool changed = false;
1671 for (const sp<Layer>& child : mCurrentChildren) {
1672 sp<Client> parentClient = mClientRef.promote();
1673 sp<Client> client(child->mClientRef.promote());
1674 if (client != nullptr && parentClient != client) {
1675 if (child->mLayerDetached) {
1676 child->mLayerDetached = false;
1677 changed = true;
1678 }
1679 changed |= child->attachChildren();
1680 }
1681 }
1682
1683 return changed;
1684}
1685
Peiyong Lind3788632018-09-18 16:01:31 -07001686bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001687 static const mat4 identityMatrix = mat4();
1688
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001689 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001690 return false;
1691 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001692 ++mCurrentState.sequence;
1693 mCurrentState.colorTransform = matrix;
1694 mCurrentState.hasColorTransform = matrix != identityMatrix;
1695 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001696 setTransactionFlags(eTransactionNeeded);
1697 return true;
1698}
1699
chaviwf66724d2018-11-28 16:35:21 -08001700mat4 Layer::getColorTransform() const {
1701 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1702 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1703 colorTransform = parent->getColorTransform() * colorTransform;
1704 }
1705 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001706}
1707
1708bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001709 bool hasColorTransform = getDrawingState().hasColorTransform;
1710 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1711 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1712 }
1713 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001714}
1715
Chia-I Wu11481472018-05-04 10:43:19 -07001716bool Layer::isLegacyDataSpace() const {
1717 // return true when no higher bits are set
chaviw4244e032019-09-04 11:27:49 -07001718 return !(getDataSpace() &
1719 (ui::Dataspace::STANDARD_MASK | ui::Dataspace::TRANSFER_MASK |
1720 ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001721}
1722
Robert Carr1f0a16a2016-10-24 16:27:39 -07001723void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001724 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001725}
1726
chaviw301b1d82019-11-06 13:15:09 -08001727int32_t Layer::getZ(LayerVector::StateSet stateSet) const {
1728 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1729 const State& state = useDrawing ? mDrawingState : mCurrentState;
1730 return state.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001731}
1732
Robert Carr1c5481e2019-07-01 14:42:27 -07001733bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) const {
Robert Carr29abff82017-12-04 13:51:20 -08001734 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001735 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviwe5ac40f2019-09-24 16:36:55 -07001736 return state.isRelativeOf;
Robert Carr29abff82017-12-04 13:51:20 -08001737}
1738
David Sodman41fdfc92017-11-06 16:09:56 -08001739__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001740 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001741 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1742 "makeTraversalList received invalid stateSet");
1743 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1744 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001745 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001746
Robert Carr29abff82017-12-04 13:51:20 -08001747 if (state.zOrderRelatives.size() == 0) {
1748 *outSkipRelativeZUsers = true;
1749 return children;
1750 }
1751
chaviwfd462612018-05-31 16:11:27 -07001752 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001753 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001754 sp<Layer> strongRelative = weakRelative.promote();
1755 if (strongRelative != nullptr) {
1756 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001757 }
1758 }
1759
Dan Stoza412903f2017-04-27 13:42:17 -07001760 for (const sp<Layer>& child : children) {
chaviwe5ac40f2019-09-24 16:36:55 -07001761 if (child->usingRelativeZ(stateSet)) {
Robert Carr503c7042017-09-27 15:06:08 -07001762 continue;
1763 }
Robert Carrdb66e622017-04-10 16:55:57 -07001764 traverse.add(child);
1765 }
1766
1767 return traverse;
1768}
1769
Robert Carr1f0a16a2016-10-24 16:27:39 -07001770/**
Robert Carrdb66e622017-04-10 16:55:57 -07001771 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001772 */
Dan Stoza412903f2017-04-27 13:42:17 -07001773void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001774 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1775 // produce a new list for traversing, including our relatives, and not including our children
1776 // who are relatives of another surface. In the case that there are no relative Z,
1777 // makeTraversalList returns our children directly to avoid significant overhead.
1778 // However in this case we need to take the responsibility for filtering children which
1779 // are relatives of another surface here.
1780 bool skipRelativeZUsers = false;
1781 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001782
Robert Carr1f0a16a2016-10-24 16:27:39 -07001783 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001784 for (; i < list.size(); i++) {
1785 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001786 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1787 continue;
1788 }
1789
chaviw301b1d82019-11-06 13:15:09 -08001790 if (relative->getZ(stateSet) >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001791 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001792 }
Dan Stoza412903f2017-04-27 13:42:17 -07001793 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001794 }
Robert Carr29abff82017-12-04 13:51:20 -08001795
Dan Stoza412903f2017-04-27 13:42:17 -07001796 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001797 for (; i < list.size(); i++) {
1798 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001799
Robert Carr29abff82017-12-04 13:51:20 -08001800 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1801 continue;
1802 }
Dan Stoza412903f2017-04-27 13:42:17 -07001803 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001804 }
1805}
1806
1807/**
Robert Carrdb66e622017-04-10 16:55:57 -07001808 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001809 */
Dan Stoza412903f2017-04-27 13:42:17 -07001810void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1811 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001812 // See traverseInZOrder for documentation.
1813 bool skipRelativeZUsers = false;
1814 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001815
Robert Carr1f0a16a2016-10-24 16:27:39 -07001816 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001817 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001818 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001819
1820 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1821 continue;
1822 }
1823
chaviw301b1d82019-11-06 13:15:09 -08001824 if (relative->getZ(stateSet) < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001825 break;
1826 }
Dan Stoza412903f2017-04-27 13:42:17 -07001827 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001828 }
Dan Stoza412903f2017-04-27 13:42:17 -07001829 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001830 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001831 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001832
1833 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1834 continue;
1835 }
1836
Dan Stoza412903f2017-04-27 13:42:17 -07001837 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001838 }
1839}
1840
Edgar Arriaga844fa672020-01-16 14:21:42 -08001841void Layer::traverse(LayerVector::StateSet state, const LayerVector::Visitor& visitor) {
1842 visitor(this);
1843 const LayerVector& children =
1844 state == LayerVector::StateSet::Drawing ? mDrawingChildren : mCurrentChildren;
1845 for (const sp<Layer>& child : children) {
1846 child->traverse(state, visitor);
1847 }
1848}
1849
chaviw4b129c22018-04-09 16:19:43 -07001850LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1851 const std::vector<Layer*>& layersInTree) {
1852 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1853 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001854 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1855 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001856 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001857
chaviwfd462612018-05-31 16:11:27 -07001858 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001859 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1860 sp<Layer> strongRelative = weakRelative.promote();
1861 // Only add relative layers that are also descendents of the top most parent of the tree.
1862 // If a relative layer is not a descendent, then it should be ignored.
1863 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1864 traverse.add(strongRelative);
1865 }
1866 }
1867
1868 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001869 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001870 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1871 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1872 // the child here since it won't be added later as a relative.
1873 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1874 childState.zOrderRelativeOf.promote().get())) {
1875 continue;
1876 }
1877 traverse.add(child);
1878 }
1879
1880 return traverse;
1881}
1882
1883void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1884 LayerVector::StateSet stateSet,
1885 const LayerVector::Visitor& visitor) {
1886 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001887
1888 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001889 for (; i < list.size(); i++) {
1890 const auto& relative = list[i];
chaviw301b1d82019-11-06 13:15:09 -08001891 if (relative->getZ(stateSet) >= 0) {
chaviwa76b2712017-09-20 12:02:26 -07001892 break;
1893 }
chaviw4b129c22018-04-09 16:19:43 -07001894 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001895 }
chaviw4b129c22018-04-09 16:19:43 -07001896
chaviwa76b2712017-09-20 12:02:26 -07001897 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001898 for (; i < list.size(); i++) {
1899 const auto& relative = list[i];
1900 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001901 }
1902}
1903
chaviw4b129c22018-04-09 16:19:43 -07001904std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1905 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1906 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1907
1908 std::vector<Layer*> layersInTree = {this};
1909 for (size_t i = 0; i < children.size(); i++) {
1910 const auto& child = children[i];
1911 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1912 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1913 }
1914
1915 return layersInTree;
1916}
1917
1918void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1919 const LayerVector::Visitor& visitor) {
1920 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1921 std::sort(layersInTree.begin(), layersInTree.end());
1922 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1923}
1924
Peiyong Linefefaac2018-08-17 12:27:51 -07001925ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001926 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001927}
1928
chaviw13fdc492017-06-27 12:40:18 -07001929half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001930 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001931
chaviw13fdc492017-06-27 12:40:18 -07001932 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1933 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001934}
Robert Carr6452f122017-03-21 10:41:29 -07001935
chaviw13fdc492017-06-27 12:40:18 -07001936half4 Layer::getColor() const {
1937 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001938 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001939}
Robert Carr6452f122017-03-21 10:41:29 -07001940
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001941int32_t Layer::getBackgroundBlurRadius() const {
1942 return getDrawingState().backgroundBlurRadius;
1943}
1944
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001945Layer::RoundedCornerState Layer::getRoundedCornerState() const {
1946 const auto& p = mDrawingParent.promote();
1947 if (p != nullptr) {
Peiyong Lin27016a92019-03-29 17:36:08 +00001948 RoundedCornerState parentState = p->getRoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001949 if (parentState.radius > 0) {
1950 ui::Transform t = getActiveTransform(getDrawingState());
1951 t = t.inverse();
1952 parentState.cropRect = t.transform(parentState.cropRect);
1953 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1954 // but a transform matrix can define horizontal and vertical scales.
1955 // Let's take the average between both of them and pass into the shader, practically we
1956 // never do this type of transformation on windows anyway.
1957 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
1958 return parentState;
1959 }
1960 }
1961 const float radius = getDrawingState().cornerRadius;
Peiyong Linb9ff23e2019-04-08 11:04:59 -07001962 return radius > 0 && getCrop(getDrawingState()).isValid()
1963 ? RoundedCornerState(getCrop(getDrawingState()).toFloatRect(), radius)
1964 : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001965}
1966
Vishnu Nair08f6eae2019-11-26 14:01:39 -08001967renderengine::ShadowSettings Layer::getShadowSettings(const Rect& viewport) const {
1968 renderengine::ShadowSettings state = mFlinger->mDrawingState.globalShadowSettings;
1969
1970 // Shift the spot light x-position to the middle of the display and then
1971 // offset it by casting layer's screen pos.
1972 state.lightPos.x = (viewport.width() / 2.f) - mScreenBounds.left;
1973 state.lightPos.y -= mScreenBounds.top;
1974
1975 state.length = mEffectiveShadowRadius;
1976 return state;
1977}
1978
Robert Carr1f0a16a2016-10-24 16:27:39 -07001979void Layer::commitChildList() {
1980 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1981 const auto& child = mCurrentChildren[i];
1982 child->commitChildList();
1983 }
1984 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001985 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001986}
1987
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001988static wp<Layer> extractLayerFromBinder(const wp<IBinder>& weakBinderHandle) {
1989 if (weakBinderHandle == nullptr) {
1990 return nullptr;
1991 }
1992 sp<IBinder> binderHandle = weakBinderHandle.promote();
1993 if (binderHandle == nullptr) {
1994 return nullptr;
1995 }
1996 sp<Layer::Handle> handle = static_cast<Layer::Handle*>(binderHandle.get());
1997 if (handle == nullptr) {
1998 return nullptr;
1999 }
2000 return handle->owner;
2001}
2002
Robert Carr720e5062018-07-30 17:45:14 -07002003void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002004 mCurrentState.inputInfo = info;
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002005 mCurrentState.touchableRegionCrop = extractLayerFromBinder(info.touchableRegionCropHandle);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002006 mCurrentState.modified = true;
2007 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07002008 setTransactionFlags(eTransactionNeeded);
2009}
2010
chaviw6d89e2d2020-01-14 14:42:01 -08002011LayerProto* Layer::writeToProto(LayersProto& layersProto, uint32_t traceFlags) const {
chaviw08f3cb22020-01-13 13:17:21 -08002012 LayerProto* layerProto = layersProto.add_layers();
2013 writeToProtoDrawingState(layerProto, traceFlags);
2014 writeToProtoCommonState(layerProto, LayerVector::StateSet::Drawing, traceFlags);
2015
2016 for (const sp<Layer>& layer : mDrawingChildren) {
2017 layer->writeToProto(layersProto, traceFlags);
2018 }
chaviw6d89e2d2020-01-14 14:42:01 -08002019
2020 return layerProto;
chaviw08f3cb22020-01-13 13:17:21 -08002021}
2022
Vishnu Nair8406fd72019-07-30 11:29:31 -07002023void Layer::writeToProtoDrawingState(LayerProto* layerInfo, uint32_t traceFlags) const {
2024 ui::Transform transform = getTransform();
2025
2026 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
2027 for (const auto& pendingState : mPendingStatesSnapshot) {
2028 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
2029 if (barrierLayer != nullptr) {
2030 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
2031 barrierLayerProto->set_id(barrierLayer->sequence);
2032 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
2033 }
2034 }
2035
chaviwd62d3062019-09-04 14:48:02 -07002036 auto buffer = getBuffer();
Vishnu Nair8406fd72019-07-30 11:29:31 -07002037 if (buffer != nullptr) {
2038 LayerProtoHelper::writeToProto(buffer,
2039 [&]() { return layerInfo->mutable_active_buffer(); });
chaviw4244e032019-09-04 11:27:49 -07002040 LayerProtoHelper::writeToProto(ui::Transform(getBufferTransform()),
Vishnu Nair8406fd72019-07-30 11:29:31 -07002041 layerInfo->mutable_buffer_transform());
2042 }
2043 layerInfo->set_invalidate(contentDirty);
2044 layerInfo->set_is_protected(isProtected());
chaviw4244e032019-09-04 11:27:49 -07002045 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(getDataSpace())));
Vishnu Nair8406fd72019-07-30 11:29:31 -07002046 layerInfo->set_queued_frames(getQueuedFrameCount());
2047 layerInfo->set_refresh_pending(isBufferLatched());
2048 layerInfo->set_curr_frame(mCurrentFrameNumber);
2049 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
2050
2051 layerInfo->set_corner_radius(getRoundedCornerState().radius);
2052 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
2053 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
2054 [&]() { return layerInfo->mutable_position(); });
2055 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
Lloyd Piquea2468662019-03-07 21:31:06 -08002056 LayerProtoHelper::writeToProto(debugGetVisibleRegionOnDefaultDisplay(),
Vishnu Nair8406fd72019-07-30 11:29:31 -07002057 [&]() { return layerInfo->mutable_visible_region(); });
2058 LayerProtoHelper::writeToProto(surfaceDamageRegion,
2059 [&]() { return layerInfo->mutable_damage_region(); });
chaviwddeae262020-01-06 10:31:23 -08002060
2061 if (hasColorTransform()) {
2062 LayerProtoHelper::writeToProto(getColorTransform(),
2063 layerInfo->mutable_color_transform());
2064 }
Vishnu Nair8406fd72019-07-30 11:29:31 -07002065 }
2066
2067 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
2068 LayerProtoHelper::writeToProto(mSourceBounds,
2069 [&]() { return layerInfo->mutable_source_bounds(); });
2070 LayerProtoHelper::writeToProto(mScreenBounds,
2071 [&]() { return layerInfo->mutable_screen_bounds(); });
Vishnu Nair95a1ed42019-12-06 12:25:11 -08002072 LayerProtoHelper::writeToProto(getRoundedCornerState().cropRect,
2073 [&]() { return layerInfo->mutable_corner_radius_crop(); });
2074 layerInfo->set_shadow_radius(mEffectiveShadowRadius);
Vishnu Nair8406fd72019-07-30 11:29:31 -07002075 }
2076}
2077
2078void Layer::writeToProtoCommonState(LayerProto* layerInfo, LayerVector::StateSet stateSet,
2079 uint32_t traceFlags) const {
chaviw1d044282017-09-27 12:19:28 -07002080 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2081 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002082 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07002083
Peiyong Linefefaac2018-08-17 12:27:51 -07002084 ui::Transform requestedTransform = state.active_legacy.transform;
chaviw1d044282017-09-27 12:19:28 -07002085
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002086 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
2087 layerInfo->set_id(sequence);
2088 layerInfo->set_name(getName().c_str());
chaviw8a01fa42019-08-19 12:39:31 -07002089 layerInfo->set_type(getType());
chaviw1d044282017-09-27 12:19:28 -07002090
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002091 for (const auto& child : children) {
2092 layerInfo->add_children(child->sequence);
chaviw1d044282017-09-27 12:19:28 -07002093 }
chaviw1d044282017-09-27 12:19:28 -07002094
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002095 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
2096 sp<Layer> strongRelative = weakRelative.promote();
2097 if (strongRelative != nullptr) {
2098 layerInfo->add_relatives(strongRelative->sequence);
2099 }
chaviwadc40c22018-07-10 16:57:27 -07002100 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002101
2102 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
2103 [&]() { return layerInfo->mutable_transparent_region(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002104
2105 layerInfo->set_layer_stack(getLayerStack());
2106 layerInfo->set_z(state.z);
2107
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002108 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(),
2109 [&]() {
2110 return layerInfo->mutable_requested_position();
2111 });
2112
2113 LayerProtoHelper::writeSizeToProto(state.active_legacy.w, state.active_legacy.h,
2114 [&]() { return layerInfo->mutable_size(); });
2115
2116 LayerProtoHelper::writeToProto(state.crop_legacy,
2117 [&]() { return layerInfo->mutable_crop(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002118
2119 layerInfo->set_is_opaque(isOpaque(state));
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002120
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002121
2122 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
2123 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
2124 LayerProtoHelper::writeToProto(state.color,
2125 [&]() { return layerInfo->mutable_requested_color(); });
2126 layerInfo->set_flags(state.flags);
2127
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002128 LayerProtoHelper::writeToProto(requestedTransform,
2129 layerInfo->mutable_requested_transform());
2130
2131 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
2132 if (parent != nullptr) {
2133 layerInfo->set_parent(parent->sequence);
2134 } else {
2135 layerInfo->set_parent(-1);
2136 }
2137
2138 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2139 if (zOrderRelativeOf != nullptr) {
2140 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
2141 } else {
2142 layerInfo->set_z_order_relative_of(-1);
2143 }
chaviw08f3cb22020-01-13 13:17:21 -08002144
2145 layerInfo->set_is_relative_of(state.isRelativeOf);
chaviwadc40c22018-07-10 16:57:27 -07002146 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002147
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002148 if (traceFlags & SurfaceTracing::TRACE_INPUT) {
2149 LayerProtoHelper::writeToProto(state.inputInfo, state.touchableRegionCrop,
2150 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002151 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002152
2153 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
2154 auto protoMap = layerInfo->mutable_metadata();
2155 for (const auto& entry : state.metadata.mMap) {
2156 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
2157 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002158 }
chaviw1d044282017-09-27 12:19:28 -07002159}
2160
Robert Carr2e102c92018-10-23 12:11:15 -07002161bool Layer::isRemovedFromCurrentState() const {
2162 return mRemovedFromCurrentState;
2163}
2164
Arthur Hungd20b2702019-01-14 18:16:16 +08002165InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002166 InputWindowInfo info = mDrawingState.inputInfo;
chaviwaf87b3e2019-10-01 16:59:28 -07002167 info.id = sequence;
Robert Carr720e5062018-07-30 17:45:14 -07002168
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002169 if (info.displayId == ADISPLAY_ID_NONE) {
chaviwb15ea8a2019-09-03 12:40:22 -07002170 info.displayId = getLayerStack();
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002171 }
2172
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002173 ui::Transform t = getTransform();
2174 const float xScale = t.sx();
2175 const float yScale = t.sy();
Ady Abraham282f1d72019-07-24 18:05:56 -07002176 int32_t xSurfaceInset = info.surfaceInset;
2177 int32_t ySurfaceInset = info.surfaceInset;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002178 if (xScale != 1.0f || yScale != 1.0f) {
Ady Abraham282f1d72019-07-24 18:05:56 -07002179 info.windowXScale *= (xScale != 0.0f) ? 1.0f / xScale : 0.0f;
2180 info.windowYScale *= (yScale != 0.0f) ? 1.0f / yScale : 0.0f;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002181 info.touchableRegion.scaleSelf(xScale, yScale);
Ady Abraham282f1d72019-07-24 18:05:56 -07002182 xSurfaceInset = std::round(xSurfaceInset * xScale);
2183 ySurfaceInset = std::round(ySurfaceInset * yScale);
Riddle Hsu39d4aa52018-11-30 20:46:53 +08002184 }
Robert Carre07e1032018-11-26 12:55:53 -08002185
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002186 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002187 // If this is a portal window, set the touchableRegion to the layerBounds.
2188 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
2189 ? getBufferSize(getDrawingState())
2190 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002191 if (!layerBounds.isValid()) {
2192 layerBounds = getCroppedBufferSize(getDrawingState());
2193 }
Vishnu Nair8033a492018-12-05 07:27:23 -08002194 layerBounds = t.transform(layerBounds);
Ady Abraham282f1d72019-07-24 18:05:56 -07002195
2196 // clamp inset to layer bounds
2197 xSurfaceInset = (xSurfaceInset >= 0) ? std::min(xSurfaceInset, layerBounds.getWidth() / 2) : 0;
2198 ySurfaceInset = (ySurfaceInset >= 0) ? std::min(ySurfaceInset, layerBounds.getHeight() / 2) : 0;
2199
Arthur Hung118b1142019-05-08 21:25:59 +08002200 layerBounds.inset(xSurfaceInset, ySurfaceInset, xSurfaceInset, ySurfaceInset);
Vishnu Nair8033a492018-12-05 07:27:23 -08002201
Arthur Hungd20b2702019-01-14 18:16:16 +08002202 // Input coordinate should match the layer bounds.
2203 info.frameLeft = layerBounds.left;
2204 info.frameTop = layerBounds.top;
2205 info.frameRight = layerBounds.right;
2206 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08002207
2208 // Position the touchable region relative to frame screen location and restrict it to frame
2209 // bounds.
2210 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08002211 info.visible = canReceiveInput();
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002212
2213 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2214 if (info.replaceTouchableRegionWithCrop) {
2215 if (cropLayer == nullptr) {
2216 info.touchableRegion = Region(Rect{mScreenBounds});
2217 } else {
2218 info.touchableRegion = Region(Rect{cropLayer->mScreenBounds});
2219 }
2220 } else if (cropLayer != nullptr) {
2221 info.touchableRegion = info.touchableRegion.intersect(Rect{cropLayer->mScreenBounds});
2222 }
2223
chaviwaf87b3e2019-10-01 16:59:28 -07002224 // If the layer is a clone, we need to crop the input region to cloned root to prevent
2225 // touches from going outside the cloned area.
2226 if (isClone()) {
2227 sp<Layer> clonedRoot = getClonedRoot();
2228 if (clonedRoot != nullptr) {
2229 Rect rect(clonedRoot->mScreenBounds);
2230 info.touchableRegion = info.touchableRegion.intersect(rect);
2231 }
2232 }
2233
Robert Carr720e5062018-07-30 17:45:14 -07002234 return info;
2235}
2236
chaviwaf87b3e2019-10-01 16:59:28 -07002237sp<Layer> Layer::getClonedRoot() {
2238 if (mClonedChild != nullptr) {
2239 return this;
2240 }
2241 if (mDrawingParent == nullptr || mDrawingParent.promote() == nullptr) {
2242 return nullptr;
2243 }
2244 return mDrawingParent.promote()->getClonedRoot();
2245}
2246
Robert Carr720e5062018-07-30 17:45:14 -07002247bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002248 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002249}
2250
chaviw3e727cd2019-01-31 13:41:05 -08002251bool Layer::canReceiveInput() const {
Vishnu Nair82353e92019-09-12 12:38:47 -07002252 return !isHiddenByPolicy();
chaviw3e727cd2019-01-31 13:41:05 -08002253}
2254
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002255compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
2256 const sp<const DisplayDevice>& display) const {
Lloyd Piquede196652020-01-22 17:29:58 -08002257 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionEngineLayerFE());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002258}
2259
Lloyd Piquea2468662019-03-07 21:31:06 -08002260Region Layer::debugGetVisibleRegionOnDefaultDisplay() const {
2261 sp<DisplayDevice> displayDevice = mFlinger->getDefaultDisplayDeviceLocked();
2262 if (displayDevice == nullptr) {
2263 return {};
2264 }
2265
2266 auto outputLayer = findOutputLayerForDisplay(displayDevice);
2267 if (outputLayer == nullptr) {
2268 return {};
2269 }
2270
2271 return outputLayer->getState().visibleRegion;
2272}
2273
chaviwb4c6e582019-08-16 14:35:07 -07002274void Layer::setInitialValuesForClone(const sp<Layer>& clonedFrom) {
2275 // copy drawing state from cloned layer
2276 mDrawingState = clonedFrom->mDrawingState;
2277 mClonedFrom = clonedFrom;
chaviwb4c6e582019-08-16 14:35:07 -07002278}
chaviw74b03172019-08-19 11:09:03 -07002279
2280void Layer::updateMirrorInfo() {
2281 if (mClonedChild == nullptr || !mClonedChild->isClonedFromAlive()) {
2282 // If mClonedChild is null, there is nothing to mirror. If isClonedFromAlive returns false,
2283 // it means that there is a clone, but the layer it was cloned from has been destroyed. In
2284 // that case, we want to delete the reference to the clone since we want it to get
2285 // destroyed. The root, this layer, will still be around since the client can continue
2286 // to hold a reference, but no cloned layers will be displayed.
2287 mClonedChild = nullptr;
2288 return;
2289 }
2290
2291 std::map<sp<Layer>, sp<Layer>> clonedLayersMap;
2292 // If the real layer exists and is in current state, add the clone as a child of the root.
2293 // There's no need to remove from drawingState when the layer is offscreen since currentState is
2294 // copied to drawingState for the root layer. So the clonedChild is always removed from
2295 // drawingState and then needs to be added back each traversal.
2296 if (!mClonedChild->getClonedFrom()->isRemovedFromCurrentState()) {
2297 addChildToDrawing(mClonedChild);
2298 }
2299
2300 mClonedChild->updateClonedDrawingState(clonedLayersMap);
2301 mClonedChild->updateClonedChildren(this, clonedLayersMap);
2302 mClonedChild->updateClonedRelatives(clonedLayersMap);
2303}
2304
2305void Layer::updateClonedDrawingState(std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2306 // If the layer the clone was cloned from is alive, copy the content of the drawingState
2307 // to the clone. If the real layer is no longer alive, continue traversing the children
2308 // since we may be able to pull out other children that are still alive.
2309 if (isClonedFromAlive()) {
2310 sp<Layer> clonedFrom = getClonedFrom();
2311 mDrawingState = clonedFrom->mDrawingState;
chaviw74b03172019-08-19 11:09:03 -07002312 clonedLayersMap.emplace(clonedFrom, this);
2313 }
2314
2315 // The clone layer may have children in drawingState since they may have been created and
2316 // added from a previous request to updateMirorInfo. This is to ensure we don't recreate clones
2317 // that already exist, since we can just re-use them.
2318 // The drawingChildren will not get overwritten by the currentChildren since the clones are
2319 // not updated in the regular traversal. They are skipped since the root will lose the
2320 // reference to them when it copies its currentChildren to drawing.
2321 for (sp<Layer>& child : mDrawingChildren) {
2322 child->updateClonedDrawingState(clonedLayersMap);
2323 }
2324}
2325
2326void Layer::updateClonedChildren(const sp<Layer>& mirrorRoot,
2327 std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2328 mDrawingChildren.clear();
2329
2330 if (!isClonedFromAlive()) {
2331 return;
2332 }
2333
2334 sp<Layer> clonedFrom = getClonedFrom();
2335 for (sp<Layer>& child : clonedFrom->mDrawingChildren) {
2336 if (child == mirrorRoot) {
2337 // This is to avoid cyclical mirroring.
2338 continue;
2339 }
2340 sp<Layer> clonedChild = clonedLayersMap[child];
2341 if (clonedChild == nullptr) {
2342 clonedChild = child->createClone();
2343 clonedLayersMap[child] = clonedChild;
2344 }
2345 addChildToDrawing(clonedChild);
2346 clonedChild->updateClonedChildren(mirrorRoot, clonedLayersMap);
2347 }
2348}
2349
chaviwaf87b3e2019-10-01 16:59:28 -07002350void Layer::updateClonedInputInfo(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2351 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2352 if (cropLayer != nullptr) {
2353 if (clonedLayersMap.count(cropLayer) == 0) {
2354 // Real layer had a crop layer but it's not in the cloned hierarchy. Just set to
2355 // self as crop layer to avoid going outside bounds.
2356 mDrawingState.touchableRegionCrop = this;
2357 } else {
2358 const sp<Layer>& clonedCropLayer = clonedLayersMap.at(cropLayer);
2359 mDrawingState.touchableRegionCrop = clonedCropLayer;
2360 }
2361 }
2362 // Cloned layers shouldn't handle watch outside since their z order is not determined by
2363 // WM or the client.
2364 mDrawingState.inputInfo.layoutParamsFlags &= ~InputWindowInfo::FLAG_WATCH_OUTSIDE_TOUCH;
2365}
2366
2367void Layer::updateClonedRelatives(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
chaviw74b03172019-08-19 11:09:03 -07002368 mDrawingState.zOrderRelativeOf = nullptr;
2369 mDrawingState.zOrderRelatives.clear();
2370
2371 if (!isClonedFromAlive()) {
2372 return;
2373 }
2374
chaviwaf87b3e2019-10-01 16:59:28 -07002375 const sp<Layer>& clonedFrom = getClonedFrom();
chaviw74b03172019-08-19 11:09:03 -07002376 for (wp<Layer>& relativeWeak : clonedFrom->mDrawingState.zOrderRelatives) {
chaviwaf87b3e2019-10-01 16:59:28 -07002377 const sp<Layer>& relative = relativeWeak.promote();
2378 if (clonedLayersMap.count(relative) > 0) {
2379 auto& clonedRelative = clonedLayersMap.at(relative);
chaviw74b03172019-08-19 11:09:03 -07002380 mDrawingState.zOrderRelatives.add(clonedRelative);
2381 }
2382 }
2383
2384 // Check if the relativeLayer for the real layer is part of the cloned hierarchy.
2385 // It's possible that the layer it's relative to is outside the requested cloned hierarchy.
2386 // In that case, we treat the layer as if the relativeOf has been removed. This way, it will
2387 // still traverse the children, but the layer with the missing relativeOf will not be shown
2388 // on screen.
chaviwaf87b3e2019-10-01 16:59:28 -07002389 const sp<Layer>& relativeOf = clonedFrom->mDrawingState.zOrderRelativeOf.promote();
2390 if (clonedLayersMap.count(relativeOf) > 0) {
2391 const sp<Layer>& clonedRelativeOf = clonedLayersMap.at(relativeOf);
chaviw74b03172019-08-19 11:09:03 -07002392 mDrawingState.zOrderRelativeOf = clonedRelativeOf;
2393 }
2394
chaviwaf87b3e2019-10-01 16:59:28 -07002395 updateClonedInputInfo(clonedLayersMap);
2396
chaviw74b03172019-08-19 11:09:03 -07002397 for (sp<Layer>& child : mDrawingChildren) {
2398 child->updateClonedRelatives(clonedLayersMap);
2399 }
2400}
2401
2402void Layer::addChildToDrawing(const sp<Layer>& layer) {
2403 mDrawingChildren.add(layer);
2404 layer->mDrawingParent = this;
2405}
2406
Mathias Agopian13127d82013-03-05 17:47:11 -08002407// ---------------------------------------------------------------------------
2408
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002409}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002410
2411#if defined(__gl_h_)
2412#error "don't include gl/gl.h in this file"
2413#endif
2414
2415#if defined(__gl2_h_)
2416#error "don't include gl2/gl2.h in this file"
2417#endif
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -08002418
2419// TODO(b/129481165): remove the #pragma below and fix conversion issues
2420#pragma clang diagnostic pop // ignored "-Wconversion"