blob: 6ff23c5f51c24af4dee69e8aa25316dfac42ca29 [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 Piquea83776c2019-01-29 18:42:32 -0800478}
David Sodmanba340492018-08-05 21:51:33 -0700479
Lloyd Piquede196652020-01-22 17:29:58 -0800480void Layer::preparePerFrameCompositionState() {
Lloyd Pique688abd42019-02-15 15:42:24 -0800481 const auto& drawingState{getDrawingState()};
Lloyd Piquede196652020-01-22 17:29:58 -0800482 auto* compositionState = editCompositionState();
Lloyd Piquef5275482019-01-29 18:42:42 -0800483
Lloyd Piquede196652020-01-22 17:29:58 -0800484 compositionState->forceClientComposition = false;
485
486 compositionState->isColorspaceAgnostic = isColorSpaceAgnostic();
487 compositionState->dataspace = getDataSpace();
488 compositionState->colorTransform = getColorTransform();
489 compositionState->colorTransformIsIdentity = !hasColorTransform();
490 compositionState->surfaceDamage = surfaceDamageRegion;
491 compositionState->hasProtectedContent = isProtected();
Lloyd Pique688abd42019-02-15 15:42:24 -0800492
493 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700494 const bool drawsShadows = mEffectiveShadowRadius != 0.f;
495
Lloyd Piquede196652020-01-22 17:29:58 -0800496 compositionState->isOpaque =
Lloyd Pique688abd42019-02-15 15:42:24 -0800497 isOpaque(drawingState) && !usesRoundedCorners && getAlpha() == 1.0_hf;
Lloyd Piquef5275482019-01-29 18:42:42 -0800498
499 // Force client composition for special cases known only to the front-end.
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700500 if (isHdrY410() || usesRoundedCorners || drawsShadows) {
Lloyd Piquede196652020-01-22 17:29:58 -0800501 compositionState->forceClientComposition = true;
Lloyd Piquef5275482019-01-29 18:42:42 -0800502 }
503}
504
Lloyd Piquede196652020-01-22 17:29:58 -0800505void Layer::prepareCursorCompositionState() {
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800506 const State& drawingState{getDrawingState()};
Lloyd Piquede196652020-01-22 17:29:58 -0800507 auto* compositionState = editCompositionState();
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800508
509 // Apply the layer's transform, followed by the display's global transform
510 // Here we're guaranteed that the layer's transform preserves rects
511 Rect win = getCroppedBufferSize(drawingState);
512 // Subtract the transparent region and snap to the bounds
513 Rect bounds = reduce(win, getActiveTransparentRegion(drawingState));
514 Rect frame(getTransform().transform(bounds));
515
Lloyd Piquede196652020-01-22 17:29:58 -0800516 compositionState->cursorFrame = frame;
517}
518
519sp<compositionengine::LayerFE> Layer::asLayerFE() const {
520 return const_cast<compositionengine::LayerFE*>(
521 static_cast<const compositionengine::LayerFE*>(this));
522}
523
524sp<compositionengine::LayerFE> Layer::getCompositionEngineLayerFE() const {
525 return nullptr;
526}
527
528compositionengine::LayerFECompositionState* Layer::editCompositionState() {
529 return nullptr;
530}
531
532const compositionengine::LayerFECompositionState* Layer::getCompositionState() const {
533 return nullptr;
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800534}
535
Lloyd Piquef16688f2019-02-19 17:47:57 -0800536bool Layer::onPreComposition(nsecs_t) {
537 return false;
538}
539
Lloyd Piquede196652020-01-22 17:29:58 -0800540void Layer::prepareCompositionState(compositionengine::LayerFE::StateSubset subset) {
Lloyd Piquec6687342019-03-07 21:34:57 -0800541 using StateSubset = compositionengine::LayerFE::StateSubset;
Lloyd Piquef5275482019-01-29 18:42:42 -0800542
Lloyd Piquec6687342019-03-07 21:34:57 -0800543 switch (subset) {
544 case StateSubset::BasicGeometry:
Lloyd Piquede196652020-01-22 17:29:58 -0800545 prepareBasicGeometryCompositionState();
Lloyd Piquec6687342019-03-07 21:34:57 -0800546 break;
547
548 case StateSubset::GeometryAndContent:
Lloyd Piquede196652020-01-22 17:29:58 -0800549 prepareBasicGeometryCompositionState();
550 prepareGeometryCompositionState();
551 preparePerFrameCompositionState();
Lloyd Piquec6687342019-03-07 21:34:57 -0800552 break;
553
554 case StateSubset::Content:
Lloyd Piquede196652020-01-22 17:29:58 -0800555 preparePerFrameCompositionState();
556 break;
557
558 case StateSubset::Cursor:
559 prepareCursorCompositionState();
Lloyd Piquec6687342019-03-07 21:34:57 -0800560 break;
561 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800562}
Mathias Agopian29a367b2011-07-12 14:51:45 -0700563
Lloyd Piquea83776c2019-01-29 18:42:32 -0800564const char* Layer::getDebugName() const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700565 return mName.c_str();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800566}
567
Mathias Agopian13127d82013-03-05 17:47:11 -0800568// ---------------------------------------------------------------------------
569// drawing...
570// ---------------------------------------------------------------------------
571
Vishnu Nair9b079a22020-01-21 14:36:08 -0800572std::optional<compositionengine::LayerFE::LayerSettings> Layer::prepareClientComposition(
Lloyd Piquef16688f2019-02-19 17:47:57 -0800573 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
Lloyd Piquede196652020-01-22 17:29:58 -0800574 if (!getCompositionState()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800575 return {};
576 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800577
Vishnu Nair4351ad52019-02-11 14:13:02 -0800578 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000579 half alpha = getAlpha();
Vishnu Nair9b079a22020-01-21 14:36:08 -0800580
581 compositionengine::LayerFE::LayerSettings layerSettings;
Lloyd Piquef16688f2019-02-19 17:47:57 -0800582 layerSettings.geometry.boundaries = bounds;
583 if (targetSettings.useIdentityTransform) {
584 layerSettings.geometry.positionTransform = mat4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000585 } else {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800586 layerSettings.geometry.positionTransform = getTransform().asMatrix4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000587 }
588
589 if (hasColorTransform()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800590 layerSettings.colorTransform = getColorTransform();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000591 }
592
593 const auto roundedCornerState = getRoundedCornerState();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800594 layerSettings.geometry.roundedCornersRadius = roundedCornerState.radius;
595 layerSettings.geometry.roundedCornersCrop = roundedCornerState.cropRect;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000596
Lloyd Piquef16688f2019-02-19 17:47:57 -0800597 layerSettings.alpha = alpha;
chaviw4244e032019-09-04 11:27:49 -0700598 layerSettings.sourceDataspace = getDataSpace();
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800599 layerSettings.backgroundBlurRadius = getBackgroundBlurRadius();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800600 return layerSettings;
Mathias Agopian13127d82013-03-05 17:47:11 -0800601}
602
Vishnu Nair9b079a22020-01-21 14:36:08 -0800603std::optional<compositionengine::LayerFE::LayerSettings> Layer::prepareShadowClientComposition(
604 const LayerFE::LayerSettings& casterLayerSettings, const Rect& displayViewport,
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800605 ui::Dataspace outputDataspace) {
606 renderengine::ShadowSettings shadow = getShadowSettings(displayViewport);
607 if (shadow.length <= 0.f) {
608 return {};
609 }
610
611 const float casterAlpha = casterLayerSettings.alpha;
612 const bool casterIsOpaque = ((casterLayerSettings.source.buffer.buffer != nullptr) &&
613 casterLayerSettings.source.buffer.isOpaque);
614
Vishnu Nair9b079a22020-01-21 14:36:08 -0800615 compositionengine::LayerFE::LayerSettings shadowLayer = casterLayerSettings;
616
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800617 shadowLayer.shadow = shadow;
Vishnu Naira483b4a2019-12-12 15:07:52 -0800618 shadowLayer.geometry.boundaries = mBounds; // ignore transparent region
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800619
620 // If the casting layer is translucent, we need to fill in the shadow underneath the layer.
621 // Otherwise the generated shadow will only be shown around the casting layer.
622 shadowLayer.shadow.casterIsTranslucent = !casterIsOpaque || (casterAlpha < 1.0f);
623 shadowLayer.shadow.ambientColor *= casterAlpha;
624 shadowLayer.shadow.spotColor *= casterAlpha;
625 shadowLayer.sourceDataspace = outputDataspace;
626 shadowLayer.source.buffer.buffer = nullptr;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800627 shadowLayer.source.buffer.fence = nullptr;
628 shadowLayer.frameNumber = 0;
629 shadowLayer.bufferId = 0;
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800630
631 if (shadowLayer.shadow.ambientColor.a <= 0.f && shadowLayer.shadow.spotColor.a <= 0.f) {
632 return {};
633 }
634
635 float casterCornerRadius = shadowLayer.geometry.roundedCornersRadius;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800636 const FloatRect& cornerRadiusCropRect = shadowLayer.geometry.roundedCornersCrop;
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800637 const FloatRect& casterRect = shadowLayer.geometry.boundaries;
638
639 // crop used to set the corner radius may be larger than the content rect. Adjust the corner
640 // radius accordingly.
641 if (casterCornerRadius > 0.f) {
642 float cropRectOffset = std::max(std::abs(cornerRadiusCropRect.top - casterRect.top),
643 std::abs(cornerRadiusCropRect.left - casterRect.left));
644 if (cropRectOffset > casterCornerRadius) {
645 casterCornerRadius = 0;
646 } else {
647 casterCornerRadius -= cropRectOffset;
648 }
649 shadowLayer.geometry.roundedCornersRadius = casterCornerRadius;
650 }
651
652 return shadowLayer;
653}
654
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800655Hwc2::IComposerClient::Composition Layer::getCompositionType(
656 const sp<const DisplayDevice>& display) const {
657 const auto outputLayer = findOutputLayerForDisplay(display);
658 LOG_FATAL_IF(!outputLayer);
659 return outputLayer->getState().hwc ? (*outputLayer->getState().hwc).hwcCompositionType
660 : Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800661}
662
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800663bool Layer::getClearClientTarget(const sp<const DisplayDevice>& display) const {
664 const auto outputLayer = findOutputLayerForDisplay(display);
665 LOG_FATAL_IF(!outputLayer);
666 return outputLayer->getState().clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800667}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800668
Dan Stozacac35382016-01-27 12:21:06 -0800669bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
670 if (point->getFrameNumber() <= mCurrentFrameNumber) {
671 // Don't bother with a SyncPoint, since we've already latched the
672 // relevant frame
673 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700674 }
Robert Carr2e102c92018-10-23 12:11:15 -0700675 if (isRemovedFromCurrentState()) {
676 return false;
677 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700678
Dan Stozacac35382016-01-27 12:21:06 -0800679 Mutex::Autolock lock(mLocalSyncPointMutex);
680 mLocalSyncPoints.push_back(point);
681 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700682}
683
Mathias Agopian13127d82013-03-05 17:47:11 -0800684// ----------------------------------------------------------------------------
685// local state
686// ----------------------------------------------------------------------------
687
David Sodman41fdfc92017-11-06 16:09:56 -0800688bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800689 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700690 return (s.flags & layer_state_t::eLayerSecure);
691}
692
Mathias Agopian13127d82013-03-05 17:47:11 -0800693// ----------------------------------------------------------------------------
694// transaction
695// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800696
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800697void Layer::pushPendingState() {
698 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700699 return;
700 }
Alec Mourie60041e2019-06-14 18:59:51 -0700701 ATRACE_CALL();
Dan Stoza7dde5992015-05-22 09:51:44 -0700702
Dan Stoza7dde5992015-05-22 09:51:44 -0700703 // If this transaction is waiting on the receipt of a frame, generate a sync
704 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700705 // We don't allow installing sync points after we are removed from the current state
706 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800707 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
708 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800709 if (barrierLayer == nullptr) {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700710 ALOGE("[%s] Unable to promote barrier Layer.", getDebugName());
Dan Stoza7dde5992015-05-22 09:51:44 -0700711 // If we can't promote the layer we are intended to wait on,
712 // then it is expired or otherwise invalid. Allow this transaction
713 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800714 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800715 } else {
chaviw43cb3cb2019-05-31 15:23:41 -0700716 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy, this);
Robert Carr0d480722017-01-10 16:42:54 -0800717 if (barrierLayer->addSyncPoint(syncPoint)) {
Alec Mourie60041e2019-06-14 18:59:51 -0700718 std::stringstream ss;
719 ss << "Adding sync point " << mCurrentState.frameNumber_legacy;
720 ATRACE_NAME(ss.str().c_str());
Dan Stozacac35382016-01-27 12:21:06 -0800721 mRemoteSyncPoints.push_back(std::move(syncPoint));
722 } else {
723 // We already missed the frame we're supposed to synchronize
724 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800725 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800726 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700727 }
728
Dan Stoza7dde5992015-05-22 09:51:44 -0700729 // Wake us up to check if the frame has been received
730 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700731 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700732 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800733 mPendingStates.push_back(mCurrentState);
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700734 ATRACE_INT(mTransactionName.c_str(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700735}
736
Pablo Ceballos05289c22016-04-14 15:49:55 -0700737void Layer::popPendingState(State* stateToCommit) {
Alec Mourie60041e2019-06-14 18:59:51 -0700738 ATRACE_CALL();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800739 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700740
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800741 mPendingStates.removeAt(0);
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700742 ATRACE_INT(mTransactionName.c_str(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700743}
744
Pablo Ceballos05289c22016-04-14 15:49:55 -0700745bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700746 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800747 while (!mPendingStates.empty()) {
748 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700749 if (mRemoteSyncPoints.empty()) {
750 // If we don't have a sync point for this, apply it anyway. It
751 // will be visually wrong, but it should keep us from getting
752 // into too much trouble.
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700753 ALOGE("[%s] No local sync point found", getDebugName());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700754 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700755 stateUpdateAvailable = true;
756 continue;
757 }
758
Marissa Wallf58c14b2018-07-24 10:50:43 -0700759 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800760 mPendingStates[0].frameNumber_legacy) {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700761 ALOGE("[%s] Unexpected sync point frame number found", getDebugName());
Dan Stozacac35382016-01-27 12:21:06 -0800762
763 // Signal our end of the sync point and then dispose of it
764 mRemoteSyncPoints.front()->setTransactionApplied();
765 mRemoteSyncPoints.pop_front();
766 continue;
767 }
768
Dan Stoza7dde5992015-05-22 09:51:44 -0700769 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
Alec Mourie60041e2019-06-14 18:59:51 -0700770 ATRACE_NAME("frameIsAvailable");
Dan Stoza7dde5992015-05-22 09:51:44 -0700771 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700772 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700773 stateUpdateAvailable = true;
774
775 // Signal our end of the sync point and then dispose of it
776 mRemoteSyncPoints.front()->setTransactionApplied();
777 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800778 } else {
Alec Mourie60041e2019-06-14 18:59:51 -0700779 ATRACE_NAME("!frameIsAvailable");
Dan Stoza792e5292016-02-11 11:43:58 -0800780 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700781 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700782 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700783 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700784 stateUpdateAvailable = true;
785 }
786 }
787
788 // If we still have pending updates, wake SurfaceFlinger back up and point
789 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800790 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700791 setTransactionFlags(eTransactionNeeded);
792 mFlinger->setTransactionFlags(eTraversalNeeded);
793 }
794
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800795 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700796 return stateUpdateAvailable;
797}
798
Marissa Wall61c58622018-07-18 10:12:20 -0700799uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000800 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800801
Marissa Wall61c58622018-07-18 10:12:20 -0700802 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
803 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700804
David Sodmaneb085e02017-10-05 18:49:04 -0700805 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700806 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000807 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800808 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
809 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
810 " requested={ wh={%4u,%4u} }}\n"
811 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
812 " requested={ wh={%4u,%4u} }}\n",
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700813 this, getName().c_str(), getBufferTransform(), getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700814 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
815 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
816 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
817 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
818 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700819 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
820 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
821 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800822 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700823
Robert Carre392b552017-09-19 12:16:05 -0700824 // Don't let Layer::doTransaction update the drawing state
825 // if we have a pending resize, unless we are in fixed-size mode.
826 // the drawing state will be updated only once we receive a buffer
827 // with the correct size.
828 //
829 // In particular, we want to make sure the clip (which is part
830 // of the geometry state) is latched together with the size but is
831 // latched immediately when no resizing is involved.
832 //
833 // If a sideband stream is attached, however, we want to skip this
834 // optimization so that transactions aren't missed when a buffer
835 // never arrives
836 //
837 // In the case that we don't have a buffer we ignore other factors
838 // and avoid entering the resizePending state. At a high level the
839 // resizePending state is to avoid applying the state of the new buffer
840 // to the old buffer. However in the state where we don't have an old buffer
841 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700842 const bool resizePending =
843 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
844 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
chaviwd62d3062019-09-04 14:48:02 -0700845 (getBuffer() != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700846 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800847 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700848 flags |= eDontUpdateGeometryState;
849 }
850 }
851
Robert Carr7bf247e2017-05-18 14:02:49 -0700852 // Here we apply various requested geometry states, depending on our
853 // latching configuration. See Layer.h for a detailed discussion of
854 // how geometry latching is controlled.
855 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000856 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700857
Robert Carr7bf247e2017-05-18 14:02:49 -0700858 // There is an awkward asymmetry in the handling of the crop states in the position
859 // states, as can be seen below. Largely this arises from position and transform
860 // being stored in the same data structure while having different latching rules.
861 // b/38182305
862 //
Marissa Wall61c58622018-07-18 10:12:20 -0700863 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700864 // applyPendingStates in the presence of deferred transactions.
chaviw214c89d2019-09-04 16:03:53 -0700865 editCurrentState.active_legacy = editCurrentState.requested_legacy;
866 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Mathias Agopian13127d82013-03-05 17:47:11 -0800867 }
868
Marissa Wall61c58622018-07-18 10:12:20 -0700869 return flags;
870}
871
872uint32_t Layer::doTransaction(uint32_t flags) {
873 ATRACE_CALL();
874
chaviw5aedec92018-10-22 10:40:38 -0700875 if (mLayerDetached) {
Robert Carr321e83c2019-08-19 15:49:30 -0700876 // Ensure BLAST buffer callbacks are processed.
877 // detachChildren and mLayerDetached were implemented to avoid geometry updates
878 // to layers in the cases of animation. For BufferQueue layers buffers are still
879 // consumed as normal. This is useful as otherwise the client could get hung
880 // inevitably waiting on a buffer to return. We recreate this semantic for BufferQueue
881 // even though it is a little consistent. detachChildren is shortly slated for removal
882 // by the hierarchy mirroring work so we don't need to worry about it too much.
Valerie Hau7618b232020-01-09 16:03:08 -0800883 forceSendCallbacks();
Robert Carr321e83c2019-08-19 15:49:30 -0700884 mCurrentState.callbackHandles = {};
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800885 return flags;
886 }
887
888 if (mChildrenChanged) {
889 flags |= eVisibleRegion;
890 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -0700891 }
892
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800893 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +0000894 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -0700895 if (!applyPendingStates(&c)) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800896 return flags;
Marissa Wall61c58622018-07-18 10:12:20 -0700897 }
898
899 flags = doTransactionResize(flags, &c);
900
Alec Mourib416efd2018-09-06 21:01:59 +0000901 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700902
903 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800904 // invalidate and recompute the visible regions if needed
905 flags |= Layer::eVisibleRegion;
906 }
907
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700908 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800909 // invalidate and recompute the visible regions if needed
910 flags |= eVisibleRegion;
911 this->contentDirty = true;
912
913 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -0700914 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -0700915 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -0800916 }
917
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800918 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -0700919 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800920 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -0700921 }
922
Mathias Agopian13127d82013-03-05 17:47:11 -0800923 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -0700924 commitTransaction(c);
Vishnu Nair8406fd72019-07-30 11:29:31 -0700925 mPendingStatesSnapshot = mPendingStates;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800926 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -0800927 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800928}
929
Pablo Ceballos05289c22016-04-14 15:49:55 -0700930void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800931 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700932}
933
Mathias Agopian13127d82013-03-05 17:47:11 -0800934uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800935 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800936}
937
938uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800939 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -0800940}
941
chaviw214c89d2019-09-04 16:03:53 -0700942bool Layer::setPosition(float x, float y) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800943 if (mCurrentState.requested_legacy.transform.tx() == x &&
944 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -0800945 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800946 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -0700947
948 // We update the requested and active position simultaneously because
949 // we want to apply the position portion of the transform matrix immediately,
950 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800951 mCurrentState.requested_legacy.transform.set(x, y);
chaviw214c89d2019-09-04 16:03:53 -0700952 // Here we directly update the active state
953 // unlike other setters, because we store it within
954 // the transform, but use different latching rules.
955 // b/38182305
956 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr69663fb2016-03-27 19:59:19 -0700957
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800958 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800959 setTransactionFlags(eTransactionNeeded);
960 return true;
961}
Robert Carr82364e32016-05-15 11:27:47 -0700962
Robert Carr1f0a16a2016-10-24 16:27:39 -0700963bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
964 ssize_t idx = mCurrentChildren.indexOf(childLayer);
965 if (idx < 0) {
966 return false;
967 }
968 if (childLayer->setLayer(z)) {
969 mCurrentChildren.removeAt(idx);
970 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800971 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700972 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800973 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700974}
975
Robert Carr503c7042017-09-27 15:06:08 -0700976bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
977 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
978 ssize_t idx = mCurrentChildren.indexOf(childLayer);
979 if (idx < 0) {
980 return false;
981 }
982 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
983 mCurrentChildren.removeAt(idx);
984 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800985 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700986 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800987 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700988}
989
Robert Carrae060832016-11-28 10:51:00 -0800990bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800991 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
992 mCurrentState.sequence++;
993 mCurrentState.z = z;
994 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700995
996 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800997 if (mCurrentState.zOrderRelativeOf != nullptr) {
998 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700999 if (strongRelative != nullptr) {
1000 strongRelative->removeZOrderRelative(this);
1001 }
chaviw606e5cf2019-03-01 10:12:10 -08001002 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -07001003 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001004 setTransactionFlags(eTransactionNeeded);
1005 return true;
1006}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001007
Robert Carrdb66e622017-04-10 16:55:57 -07001008void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001009 mCurrentState.zOrderRelatives.remove(relative);
1010 mCurrentState.sequence++;
1011 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001012 setTransactionFlags(eTransactionNeeded);
1013}
1014
1015void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001016 mCurrentState.zOrderRelatives.add(relative);
1017 mCurrentState.modified = true;
1018 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -07001019 setTransactionFlags(eTransactionNeeded);
1020}
1021
chaviw606e5cf2019-03-01 10:12:10 -08001022void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
1023 mCurrentState.zOrderRelativeOf = relativeOf;
1024 mCurrentState.sequence++;
1025 mCurrentState.modified = true;
chaviwbdb8b802019-10-14 09:17:12 -07001026 mCurrentState.isRelativeOf = relativeOf != nullptr;
1027
chaviw606e5cf2019-03-01 10:12:10 -08001028 setTransactionFlags(eTransactionNeeded);
1029}
1030
Robert Carr503d2bd2017-12-04 15:49:47 -08001031bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001032 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1033 if (handle == nullptr) {
1034 return false;
1035 }
1036 sp<Layer> relative = handle->owner.promote();
1037 if (relative == nullptr) {
1038 return false;
1039 }
1040
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001041 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1042 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001043 return false;
1044 }
1045
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001046 mCurrentState.sequence++;
1047 mCurrentState.modified = true;
1048 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001049
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001050 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -07001051 if (oldZOrderRelativeOf != nullptr) {
1052 oldZOrderRelativeOf->removeZOrderRelative(this);
1053 }
chaviw606e5cf2019-03-01 10:12:10 -08001054 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -07001055 relative->addZOrderRelative(this);
1056
1057 setTransactionFlags(eTransactionNeeded);
1058
1059 return true;
1060}
1061
Mathias Agopian13127d82013-03-05 17:47:11 -08001062bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001063 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001064 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001065 mCurrentState.requested_legacy.w = w;
1066 mCurrentState.requested_legacy.h = h;
1067 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001068 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001069
1070 // record the new size, from this point on, when the client request
1071 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001072 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001073 return true;
1074}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001075bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001076 if (mCurrentState.color.a == alpha) return false;
1077 mCurrentState.sequence++;
1078 mCurrentState.color.a = alpha;
1079 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001080 setTransactionFlags(eTransactionNeeded);
1081 return true;
1082}
chaviw13fdc492017-06-27 12:40:18 -07001083
Valerie Haudd0b7572019-01-29 14:59:27 -08001084bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1085 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001086 return false;
Valerie Hauaa194562019-02-05 16:21:38 -08001087 }
1088 mCurrentState.sequence++;
1089 mCurrentState.modified = true;
1090 setTransactionFlags(eTransactionNeeded);
1091
1092 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001093 // create background color layer if one does not yet exist
1094 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001095 std::string name = mName + "BackgroundColorLayer";
Lloyd Pique958be492019-03-28 15:34:59 -07001096 mCurrentState.bgColorLayer = mFlinger->getFactory().createColorLayer(
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001097 LayerCreationArgs(mFlinger.get(), nullptr, std::move(name), 0, 0, flags,
1098 LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -07001099
Valerie Haudd0b7572019-01-29 14:59:27 -08001100 // add to child list
1101 addChild(mCurrentState.bgColorLayer);
1102 mFlinger->mLayersAdded = true;
1103 // set up SF to handle added color layer
1104 if (isRemovedFromCurrentState()) {
1105 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1106 }
1107 mFlinger->setTransactionFlags(eTransactionNeeded);
1108 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1109 mCurrentState.bgColorLayer->reparent(nullptr);
1110 mCurrentState.bgColorLayer = nullptr;
1111 return true;
1112 }
1113
1114 mCurrentState.bgColorLayer->setColor(color);
1115 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1116 mCurrentState.bgColorLayer->setAlpha(alpha);
1117 mCurrentState.bgColorLayer->setDataspace(dataspace);
1118
chaviw13fdc492017-06-27 12:40:18 -07001119 return true;
1120}
1121
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001122bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001123 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001124
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001125 mCurrentState.sequence++;
1126 mCurrentState.cornerRadius = cornerRadius;
1127 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001128 setTransactionFlags(eTransactionNeeded);
1129 return true;
1130}
1131
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001132bool Layer::setBackgroundBlurRadius(int backgroundBlurRadius) {
1133 if (mCurrentState.backgroundBlurRadius == backgroundBlurRadius) return false;
1134
1135 mCurrentState.sequence++;
1136 mCurrentState.backgroundBlurRadius = backgroundBlurRadius;
1137 mCurrentState.modified = true;
1138 setTransactionFlags(eTransactionNeeded);
1139 return true;
1140}
1141
Robert Carrd4ae7f32018-06-07 16:10:57 -07001142bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1143 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001144 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001145 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1146
1147 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1148 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1149 return false;
1150 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001151 mCurrentState.sequence++;
1152 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1153 matrix.dsdy);
1154 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001155 setTransactionFlags(eTransactionNeeded);
1156 return true;
1157}
Marissa Wall61c58622018-07-18 10:12:20 -07001158
Mathias Agopian13127d82013-03-05 17:47:11 -08001159bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001160 mCurrentState.requestedTransparentRegion_legacy = transparent;
1161 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001162 setTransactionFlags(eTransactionNeeded);
1163 return true;
1164}
Ana Krulecc84d09b2019-11-02 23:10:29 +01001165
Mathias Agopian13127d82013-03-05 17:47:11 -08001166bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001167 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1168 if (mCurrentState.flags == newFlags) return false;
1169 mCurrentState.sequence++;
1170 mCurrentState.flags = newFlags;
1171 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001172 setTransactionFlags(eTransactionNeeded);
1173 return true;
1174}
Robert Carr99e27f02016-06-16 15:18:02 -07001175
chaviw214c89d2019-09-04 16:03:53 -07001176bool Layer::setCrop_legacy(const Rect& crop) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001177 if (mCurrentState.requestedCrop_legacy == crop) return false;
1178 mCurrentState.sequence++;
1179 mCurrentState.requestedCrop_legacy = crop;
chaviw214c89d2019-09-04 16:03:53 -07001180 mCurrentState.crop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001181
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001182 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001183 setTransactionFlags(eTransactionNeeded);
1184 return true;
1185}
Robert Carr8d5227b2017-03-16 15:41:03 -07001186
Robert Carrc3574f72016-03-24 12:19:32 -07001187bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001188 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001189 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001190 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001191 return true;
1192}
1193
Evan Roskyef876c92019-01-25 17:46:06 -08001194bool Layer::setMetadata(const LayerMetadata& data) {
1195 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001196 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001197 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001198 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001199 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001200}
1201
Mathias Agopian13127d82013-03-05 17:47:11 -08001202bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001203 if (mCurrentState.layerStack == layerStack) return false;
1204 mCurrentState.sequence++;
1205 mCurrentState.layerStack = layerStack;
1206 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001207 setTransactionFlags(eTransactionNeeded);
1208 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001209}
1210
Peiyong Linc502cb72019-03-01 15:00:23 -08001211bool Layer::setColorSpaceAgnostic(const bool agnostic) {
1212 if (mCurrentState.colorSpaceAgnostic == agnostic) {
1213 return false;
1214 }
1215 mCurrentState.sequence++;
1216 mCurrentState.colorSpaceAgnostic = agnostic;
1217 mCurrentState.modified = true;
1218 setTransactionFlags(eTransactionNeeded);
1219 return true;
1220}
1221
Ana Krulecc84d09b2019-11-02 23:10:29 +01001222bool Layer::setFrameRateSelectionPriority(int32_t priority) {
1223 if (mCurrentState.frameRateSelectionPriority == priority) return false;
1224 mCurrentState.frameRateSelectionPriority = priority;
1225 mCurrentState.sequence++;
1226 mCurrentState.modified = true;
1227 setTransactionFlags(eTransactionNeeded);
1228 return true;
1229}
1230
1231int32_t Layer::getFrameRateSelectionPriority() const {
1232 // Check if layer has priority set.
1233 if (mDrawingState.frameRateSelectionPriority != PRIORITY_UNSET) {
1234 return mDrawingState.frameRateSelectionPriority;
1235 }
1236 // If not, search whether its parents have it set.
1237 sp<Layer> parent = getParent();
1238 if (parent != nullptr) {
1239 return parent->getFrameRateSelectionPriority();
1240 }
1241
1242 return Layer::PRIORITY_UNSET;
1243}
1244
Robert Carr1f0a16a2016-10-24 16:27:39 -07001245uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001246 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001247 if (p == nullptr) {
1248 return getDrawingState().layerStack;
1249 }
1250 return p->getLayerStack();
1251}
1252
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001253bool Layer::setShadowRadius(float shadowRadius) {
1254 if (mCurrentState.shadowRadius == shadowRadius) {
1255 return false;
1256 }
1257
1258 mCurrentState.sequence++;
1259 mCurrentState.shadowRadius = shadowRadius;
1260 mCurrentState.modified = true;
1261 setTransactionFlags(eTransactionNeeded);
1262 return true;
1263}
1264
Ady Abraham71c437d2020-01-31 15:56:57 -08001265bool Layer::setFrameRate(FrameRate frameRate) {
Ana Krulec3803b8d2020-02-03 16:35:46 -08001266 if (!mFlinger->useFrameRateApi) {
1267 return false;
1268 }
Steven Thomas3172e202020-01-06 19:25:30 -08001269 if (mCurrentState.frameRate == frameRate) {
1270 return false;
1271 }
1272
1273 mCurrentState.sequence++;
1274 mCurrentState.frameRate = frameRate;
1275 mCurrentState.modified = true;
1276 setTransactionFlags(eTransactionNeeded);
1277 return true;
1278}
1279
Ady Abraham71c437d2020-01-31 15:56:57 -08001280Layer::FrameRate Layer::getFrameRate() const {
1281 return getDrawingState().frameRate;
Steven Thomas3172e202020-01-06 19:25:30 -08001282}
1283
Marissa Wallf58c14b2018-07-24 10:50:43 -07001284void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Alec Mourie60041e2019-06-14 18:59:51 -07001285 ATRACE_CALL();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001286 mCurrentState.barrierLayer_legacy = barrierLayer;
1287 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001288 // We don't set eTransactionNeeded, because just receiving a deferral
1289 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001290 mCurrentState.modified = true;
1291 pushPendingState();
1292 mCurrentState.barrierLayer_legacy = nullptr;
1293 mCurrentState.frameNumber_legacy = 0;
1294 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001295}
1296
Marissa Wallf58c14b2018-07-24 10:50:43 -07001297void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001298 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001299 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001300}
1301
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001302// ----------------------------------------------------------------------------
1303// pageflip handling...
1304// ----------------------------------------------------------------------------
1305
Robert Carr1f0a16a2016-10-24 16:27:39 -07001306bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001307 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001308 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001309 if (parent != nullptr && parent->isHiddenByPolicy()) {
1310 return true;
1311 }
Robert Carr1c5481e2019-07-01 14:42:27 -07001312 if (usingRelativeZ(LayerVector::StateSet::Drawing)) {
1313 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
1314 if (zOrderRelativeOf != nullptr) {
1315 if (zOrderRelativeOf->isHiddenByPolicy()) {
1316 return true;
1317 }
1318 }
1319 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001320 return s.flags & layer_state_t::eLayerHidden;
1321}
1322
David Sodman41fdfc92017-11-06 16:09:56 -08001323uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001324 // TODO: should we do something special if mSecure is set?
1325 if (mProtectedByApp) {
1326 // need a hardware-protected path to external video sink
1327 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001328 }
Riley Andrews03414a12014-07-01 14:22:59 -07001329 if (mPotentialCursor) {
1330 usage |= GraphicBuffer::USAGE_CURSOR;
1331 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001332 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001333 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001334}
1335
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001336void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001337 uint32_t orientation = 0;
Vishnu Nair5eb3f062019-04-08 08:21:03 -07001338 // Disable setting transform hint if the debug flag is set.
1339 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001340 // The transform hint is used to improve performance, but we can
1341 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001342 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001343 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001344 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001345 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001346 orientation = 0;
1347 }
1348 }
David Sodmaneb085e02017-10-05 18:49:04 -07001349 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001350}
1351
Mathias Agopian13127d82013-03-05 17:47:11 -08001352// ----------------------------------------------------------------------------
1353// debugging
1354// ----------------------------------------------------------------------------
1355
Marissa Wall61c58622018-07-18 10:12:20 -07001356// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001357LayerDebugInfo Layer::getLayerDebugInfo() const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001358 using namespace std::string_literals;
1359
Kalle Raitaa099a242017-01-11 11:17:29 -08001360 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001361 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001362 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001363 sp<Layer> parent = getParent();
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001364 info.mParentName = parent ? parent->getName() : "none"s;
chaviw8a01fa42019-08-19 12:39:31 -07001365 info.mType = getType();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001366 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Lloyd Piquea2468662019-03-07 21:31:06 -08001367
1368 info.mVisibleRegion = debugGetVisibleRegionOnDefaultDisplay();
Kalle Raitaa099a242017-01-11 11:17:29 -08001369 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001370 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001371 info.mX = ds.active_legacy.transform.tx();
1372 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001373 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001374 info.mWidth = ds.active_legacy.w;
1375 info.mHeight = ds.active_legacy.h;
1376 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001377 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001378 info.mFlags = ds.flags;
1379 info.mPixelFormat = getPixelFormat();
chaviw4244e032019-09-04 11:27:49 -07001380 info.mDataSpace = static_cast<android_dataspace>(getDataSpace());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001381 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1382 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1383 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1384 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001385 {
chaviwd62d3062019-09-04 14:48:02 -07001386 sp<const GraphicBuffer> buffer = getBuffer();
David Sodman5b4cffc2017-11-23 13:20:29 -08001387 if (buffer != 0) {
1388 info.mActiveBufferWidth = buffer->getWidth();
1389 info.mActiveBufferHeight = buffer->getHeight();
1390 info.mActiveBufferStride = buffer->getStride();
1391 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001392 } else {
1393 info.mActiveBufferWidth = 0;
1394 info.mActiveBufferHeight = 0;
1395 info.mActiveBufferStride = 0;
1396 info.mActiveBufferFormat = 0;
1397 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001398 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001399 info.mNumQueuedFrames = getQueuedFrameCount();
1400 info.mRefreshPending = isBufferLatched();
1401 info.mIsOpaque = isOpaque(ds);
1402 info.mContentDirty = contentDirty;
1403 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001404}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001405
Yiwei Zhang5434a782018-12-05 18:06:32 -08001406void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001407 result.append("-------------------------------");
1408 result.append("-------------------------------");
1409 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001410 result.append(" Layer name\n");
1411 result.append(" Z | ");
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001412 result.append(" Window Type | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001413 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001414 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001415 result.append(" Disp Frame (LTRB) | ");
1416 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001417 result.append("-------------------------------");
1418 result.append("-------------------------------");
1419 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001420}
1421
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001422void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1423 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1424 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001425 return;
1426 }
1427
Yiwei Zhang5434a782018-12-05 18:06:32 -08001428 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001429 if (mName.length() > 77) {
1430 std::string shortened;
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001431 shortened.append(mName, 0, 36);
Dan Stozae22aec72016-08-01 13:20:59 -07001432 shortened.append("[...]");
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001433 shortened.append(mName, mName.length() - 36);
1434 name = std::move(shortened);
Dan Stozae22aec72016-08-01 13:20:59 -07001435 } else {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001436 name = mName;
Dan Stozae22aec72016-08-01 13:20:59 -07001437 }
1438
Yiwei Zhang5434a782018-12-05 18:06:32 -08001439 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001440
Alec Mourib416efd2018-09-06 21:01:59 +00001441 const State& layerState(getDrawingState());
Lloyd Piquede196652020-01-22 17:29:58 -08001442 const auto& outputLayerState = outputLayer->getState();
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001443
Chia-I Wu1e043612018-03-01 09:45:09 -08001444 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001445 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001446 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001447 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001448 }
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001449 StringAppendF(&result, " %10d | ", mWindowType);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001450 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
Lloyd Piquede196652020-01-22 17:29:58 -08001451 StringAppendF(&result, "%10s | ", toString(outputLayerState.bufferTransform).c_str());
1452 const Rect& frame = outputLayerState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001453 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Piquede196652020-01-22 17:29:58 -08001454 const FloatRect& crop = outputLayerState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001455 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1456 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001457
Yichi Chen6ca35192018-05-29 12:20:43 +08001458 result.append("- - - - - - - - - - - - - - - -");
1459 result.append("- - - - - - - - - - - - - - - -");
1460 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001461}
Dan Stozae22aec72016-08-01 13:20:59 -07001462
Yiwei Zhang5434a782018-12-05 18:06:32 -08001463void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001464 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001465}
1466
Svetoslavd85084b2014-03-20 10:28:31 -07001467void Layer::clearFrameStats() {
1468 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001469}
1470
Jamie Gennis6547ff42013-07-16 20:12:42 -07001471void Layer::logFrameStats() {
1472 mFrameTracker.logAndResetStats(mName);
1473}
1474
Svetoslavd85084b2014-03-20 10:28:31 -07001475void Layer::getFrameStats(FrameStats* outStats) const {
1476 mFrameTracker.getStats(outStats);
1477}
1478
Yiwei Zhang5434a782018-12-05 18:06:32 -08001479void Layer::dumpFrameEvents(std::string& result) {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001480 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().c_str(), getType(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001481 Mutex::Autolock lock(mFrameEventHistoryMutex);
1482 mFrameEventHistory.checkFencesForCompletion();
1483 mFrameEventHistory.dump(result);
1484}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001485
Vishnu Nair0f085c62019-08-30 08:49:12 -07001486void Layer::dumpCallingUidPid(std::string& result) const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001487 StringAppendF(&result, "Layer %s (%s) pid:%d uid:%d\n", getName().c_str(), getType(),
Vishnu Nair0f085c62019-08-30 08:49:12 -07001488 mCallingPid, mCallingUid);
1489}
1490
Brian Anderson5ea5e592016-12-01 16:54:33 -08001491void Layer::onDisconnect() {
1492 Mutex::Autolock lock(mFrameEventHistoryMutex);
1493 mFrameEventHistory.onDisconnect();
Yiwei Zhang1a88c402019-11-18 10:43:58 -08001494 const int32_t layerId = getSequence();
1495 mFlinger->mTimeStats->onDestroy(layerId);
1496 mFlinger->mFrameTracer->onDestroy(layerId);
Brian Anderson5ea5e592016-12-01 16:54:33 -08001497}
1498
Brian Anderson3890c392016-07-25 12:48:08 -07001499void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001500 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001501 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001502 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1503 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhang487340f2019-11-18 17:42:12 -08001504 mFlinger->mTimeStats->setAcquireFence(getSequence(), newTimestamps->frameNumber,
1505 newTimestamps->acquireFence);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001506 }
1507
Brian Andersond6927fb2016-07-23 23:37:30 -07001508 Mutex::Autolock lock(mFrameEventHistoryMutex);
1509 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001510 // If there are any unsignaled fences in the aquire timeline at this
1511 // point, the previously queued frame hasn't been latched yet. Go ahead
1512 // and try to get the signal time here so the syscall is taken out of
1513 // the main thread's critical path.
1514 mAcquireTimeline.updateSignalTimes();
1515 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001516 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001517 mFrameEventHistory.addQueue(*newTimestamps);
1518 }
1519
Brian Anderson3890c392016-07-25 12:48:08 -07001520 if (outDelta) {
1521 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001522 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001523}
Dan Stozae77c7662016-05-13 11:37:28 -07001524
Chia-I Wu98f1c102017-05-30 14:54:08 -07001525size_t Layer::getChildrenCount() const {
1526 size_t count = 0;
1527 for (const sp<Layer>& child : mCurrentChildren) {
1528 count += 1 + child->getChildrenCount();
1529 }
1530 return count;
1531}
1532
Robert Carr1f0a16a2016-10-24 16:27:39 -07001533void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001534 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001535 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001536
Robert Carr1f0a16a2016-10-24 16:27:39 -07001537 mCurrentChildren.add(layer);
1538 layer->setParent(this);
1539}
1540
1541ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001542 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001543 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001544
Robert Carr1323c952019-01-28 18:13:27 -08001545 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001546 return mCurrentChildren.remove(layer);
1547}
1548
Robert Carr1db73f62016-12-21 12:58:51 -08001549bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1550 sp<Handle> handle = nullptr;
1551 sp<Layer> newParent = nullptr;
1552 if (newParentHandle == nullptr) {
1553 return false;
1554 }
1555 handle = static_cast<Handle*>(newParentHandle.get());
1556 newParent = handle->owner.promote();
1557 if (newParent == nullptr) {
1558 ALOGE("Unable to promote Layer handle");
1559 return false;
1560 }
1561
chaviw5aedec92018-10-22 10:40:38 -07001562 if (attachChildren()) {
1563 setTransactionFlags(eTransactionNeeded);
1564 }
Robert Carr1db73f62016-12-21 12:58:51 -08001565 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001566 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001567 }
1568 mCurrentChildren.clear();
1569
1570 return true;
1571}
1572
Robert Carr15eae092018-03-23 13:43:53 -07001573void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001574 for (const sp<Layer>& child : mDrawingChildren) {
1575 child->mDrawingParent = newParent;
Vishnu Nairc652ff82019-03-15 12:48:54 -07001576 child->computeBounds(newParent->mBounds,
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001577 newParent->getTransformWithScale(newParent->getBufferScaleTransform()),
1578 newParent->mEffectiveShadowRadius);
Robert Carr578038f2018-03-09 12:25:24 -08001579 }
1580}
1581
chaviwf1961f72017-09-18 16:41:07 -07001582bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001583 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001584
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001585 // While layers are detached, we allow most operations
1586 // and simply halt performing the actual transaction. However
1587 // for reparent != null we would enter the mRemovedFromCurrentState
1588 // state, regardless of whether doTransaction was called, and
1589 // so we need to prevent the update here.
1590 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001591 return false;
1592 }
1593
Robert Carr54cf5b12019-01-25 14:02:28 -08001594 sp<Layer> newParent;
1595 if (newParentHandle != nullptr) {
1596 auto handle = static_cast<Handle*>(newParentHandle.get());
1597 newParent = handle->owner.promote();
1598 if (newParent == nullptr) {
1599 ALOGE("Unable to promote Layer handle");
1600 return false;
1601 }
1602 if (newParent == this) {
1603 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1604 return false;
1605 }
1606 }
1607
chaviwf1961f72017-09-18 16:41:07 -07001608 sp<Layer> parent = getParent();
1609 if (parent != nullptr) {
1610 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001611 }
1612
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001613 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001614 newParent->addChild(this);
1615 if (!newParent->isRemovedFromCurrentState()) {
1616 addToCurrentState();
1617 } else {
1618 onRemovedFromCurrentState();
1619 }
1620
1621 if (mLayerDetached) {
1622 mLayerDetached = false;
1623 callSetTransactionFlags = true;
1624 }
1625 } else {
1626 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001627 }
1628
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001629 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001630 setTransactionFlags(eTransactionNeeded);
1631 }
chaviw06178942017-07-27 10:25:59 -07001632 return true;
1633}
1634
Robert Carr9524cb32017-02-13 11:32:32 -08001635bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001636 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001637 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001638 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001639 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001640 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001641 child->detachChildren();
chaviw43cb3cb2019-05-31 15:23:41 -07001642 child->removeRemoteSyncPoints();
Robert Carr9524cb32017-02-13 11:32:32 -08001643 }
Robert Carr7f619b22017-11-06 12:56:35 -08001644 }
Robert Carr9524cb32017-02-13 11:32:32 -08001645
1646 return true;
1647}
1648
chaviw5aedec92018-10-22 10:40:38 -07001649bool Layer::attachChildren() {
1650 bool changed = false;
1651 for (const sp<Layer>& child : mCurrentChildren) {
1652 sp<Client> parentClient = mClientRef.promote();
1653 sp<Client> client(child->mClientRef.promote());
1654 if (client != nullptr && parentClient != client) {
1655 if (child->mLayerDetached) {
1656 child->mLayerDetached = false;
1657 changed = true;
1658 }
1659 changed |= child->attachChildren();
1660 }
1661 }
1662
1663 return changed;
1664}
1665
Peiyong Lind3788632018-09-18 16:01:31 -07001666bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001667 static const mat4 identityMatrix = mat4();
1668
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001669 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001670 return false;
1671 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001672 ++mCurrentState.sequence;
1673 mCurrentState.colorTransform = matrix;
1674 mCurrentState.hasColorTransform = matrix != identityMatrix;
1675 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001676 setTransactionFlags(eTransactionNeeded);
1677 return true;
1678}
1679
chaviwf66724d2018-11-28 16:35:21 -08001680mat4 Layer::getColorTransform() const {
1681 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1682 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1683 colorTransform = parent->getColorTransform() * colorTransform;
1684 }
1685 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001686}
1687
1688bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001689 bool hasColorTransform = getDrawingState().hasColorTransform;
1690 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1691 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1692 }
1693 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001694}
1695
Chia-I Wu11481472018-05-04 10:43:19 -07001696bool Layer::isLegacyDataSpace() const {
1697 // return true when no higher bits are set
chaviw4244e032019-09-04 11:27:49 -07001698 return !(getDataSpace() &
1699 (ui::Dataspace::STANDARD_MASK | ui::Dataspace::TRANSFER_MASK |
1700 ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001701}
1702
Robert Carr1f0a16a2016-10-24 16:27:39 -07001703void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001704 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001705}
1706
chaviw301b1d82019-11-06 13:15:09 -08001707int32_t Layer::getZ(LayerVector::StateSet stateSet) const {
1708 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1709 const State& state = useDrawing ? mDrawingState : mCurrentState;
1710 return state.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001711}
1712
Robert Carr1c5481e2019-07-01 14:42:27 -07001713bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) const {
Robert Carr29abff82017-12-04 13:51:20 -08001714 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001715 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviwe5ac40f2019-09-24 16:36:55 -07001716 return state.isRelativeOf;
Robert Carr29abff82017-12-04 13:51:20 -08001717}
1718
David Sodman41fdfc92017-11-06 16:09:56 -08001719__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001720 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001721 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1722 "makeTraversalList received invalid stateSet");
1723 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1724 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001725 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001726
Robert Carr29abff82017-12-04 13:51:20 -08001727 if (state.zOrderRelatives.size() == 0) {
1728 *outSkipRelativeZUsers = true;
1729 return children;
1730 }
1731
chaviwfd462612018-05-31 16:11:27 -07001732 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001733 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001734 sp<Layer> strongRelative = weakRelative.promote();
1735 if (strongRelative != nullptr) {
1736 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001737 }
1738 }
1739
Dan Stoza412903f2017-04-27 13:42:17 -07001740 for (const sp<Layer>& child : children) {
chaviwe5ac40f2019-09-24 16:36:55 -07001741 if (child->usingRelativeZ(stateSet)) {
Robert Carr503c7042017-09-27 15:06:08 -07001742 continue;
1743 }
Robert Carrdb66e622017-04-10 16:55:57 -07001744 traverse.add(child);
1745 }
1746
1747 return traverse;
1748}
1749
Robert Carr1f0a16a2016-10-24 16:27:39 -07001750/**
Robert Carrdb66e622017-04-10 16:55:57 -07001751 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001752 */
Dan Stoza412903f2017-04-27 13:42:17 -07001753void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001754 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1755 // produce a new list for traversing, including our relatives, and not including our children
1756 // who are relatives of another surface. In the case that there are no relative Z,
1757 // makeTraversalList returns our children directly to avoid significant overhead.
1758 // However in this case we need to take the responsibility for filtering children which
1759 // are relatives of another surface here.
1760 bool skipRelativeZUsers = false;
1761 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001762
Robert Carr1f0a16a2016-10-24 16:27:39 -07001763 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001764 for (; i < list.size(); i++) {
1765 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001766 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1767 continue;
1768 }
1769
chaviw301b1d82019-11-06 13:15:09 -08001770 if (relative->getZ(stateSet) >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001771 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001772 }
Dan Stoza412903f2017-04-27 13:42:17 -07001773 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001774 }
Robert Carr29abff82017-12-04 13:51:20 -08001775
Dan Stoza412903f2017-04-27 13:42:17 -07001776 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001777 for (; i < list.size(); i++) {
1778 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001779
Robert Carr29abff82017-12-04 13:51:20 -08001780 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1781 continue;
1782 }
Dan Stoza412903f2017-04-27 13:42:17 -07001783 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001784 }
1785}
1786
1787/**
Robert Carrdb66e622017-04-10 16:55:57 -07001788 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001789 */
Dan Stoza412903f2017-04-27 13:42:17 -07001790void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1791 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001792 // See traverseInZOrder for documentation.
1793 bool skipRelativeZUsers = false;
1794 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001795
Robert Carr1f0a16a2016-10-24 16:27:39 -07001796 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001797 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001798 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001799
1800 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1801 continue;
1802 }
1803
chaviw301b1d82019-11-06 13:15:09 -08001804 if (relative->getZ(stateSet) < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001805 break;
1806 }
Dan Stoza412903f2017-04-27 13:42:17 -07001807 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001808 }
Dan Stoza412903f2017-04-27 13:42:17 -07001809 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001810 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001811 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001812
1813 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1814 continue;
1815 }
1816
Dan Stoza412903f2017-04-27 13:42:17 -07001817 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001818 }
1819}
1820
Edgar Arriaga844fa672020-01-16 14:21:42 -08001821void Layer::traverse(LayerVector::StateSet state, const LayerVector::Visitor& visitor) {
1822 visitor(this);
1823 const LayerVector& children =
1824 state == LayerVector::StateSet::Drawing ? mDrawingChildren : mCurrentChildren;
1825 for (const sp<Layer>& child : children) {
1826 child->traverse(state, visitor);
1827 }
1828}
1829
chaviw4b129c22018-04-09 16:19:43 -07001830LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1831 const std::vector<Layer*>& layersInTree) {
1832 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1833 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001834 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1835 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001836 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001837
chaviwfd462612018-05-31 16:11:27 -07001838 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001839 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1840 sp<Layer> strongRelative = weakRelative.promote();
1841 // Only add relative layers that are also descendents of the top most parent of the tree.
1842 // If a relative layer is not a descendent, then it should be ignored.
1843 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1844 traverse.add(strongRelative);
1845 }
1846 }
1847
1848 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001849 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001850 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1851 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1852 // the child here since it won't be added later as a relative.
1853 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1854 childState.zOrderRelativeOf.promote().get())) {
1855 continue;
1856 }
1857 traverse.add(child);
1858 }
1859
1860 return traverse;
1861}
1862
1863void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1864 LayerVector::StateSet stateSet,
1865 const LayerVector::Visitor& visitor) {
1866 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001867
1868 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001869 for (; i < list.size(); i++) {
1870 const auto& relative = list[i];
chaviw301b1d82019-11-06 13:15:09 -08001871 if (relative->getZ(stateSet) >= 0) {
chaviwa76b2712017-09-20 12:02:26 -07001872 break;
1873 }
chaviw4b129c22018-04-09 16:19:43 -07001874 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001875 }
chaviw4b129c22018-04-09 16:19:43 -07001876
chaviwa76b2712017-09-20 12:02:26 -07001877 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001878 for (; i < list.size(); i++) {
1879 const auto& relative = list[i];
1880 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001881 }
1882}
1883
chaviw4b129c22018-04-09 16:19:43 -07001884std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1885 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1886 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1887
1888 std::vector<Layer*> layersInTree = {this};
1889 for (size_t i = 0; i < children.size(); i++) {
1890 const auto& child = children[i];
1891 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1892 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1893 }
1894
1895 return layersInTree;
1896}
1897
1898void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1899 const LayerVector::Visitor& visitor) {
1900 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1901 std::sort(layersInTree.begin(), layersInTree.end());
1902 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1903}
1904
Peiyong Linefefaac2018-08-17 12:27:51 -07001905ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001906 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001907}
1908
chaviw13fdc492017-06-27 12:40:18 -07001909half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001910 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001911
chaviw13fdc492017-06-27 12:40:18 -07001912 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1913 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001914}
Robert Carr6452f122017-03-21 10:41:29 -07001915
chaviw13fdc492017-06-27 12:40:18 -07001916half4 Layer::getColor() const {
1917 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001918 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001919}
Robert Carr6452f122017-03-21 10:41:29 -07001920
Lucas Dupin19c8f0e2019-11-25 17:55:44 -08001921int32_t Layer::getBackgroundBlurRadius() const {
1922 return getDrawingState().backgroundBlurRadius;
1923}
1924
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001925Layer::RoundedCornerState Layer::getRoundedCornerState() const {
1926 const auto& p = mDrawingParent.promote();
1927 if (p != nullptr) {
Peiyong Lin27016a92019-03-29 17:36:08 +00001928 RoundedCornerState parentState = p->getRoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001929 if (parentState.radius > 0) {
1930 ui::Transform t = getActiveTransform(getDrawingState());
1931 t = t.inverse();
1932 parentState.cropRect = t.transform(parentState.cropRect);
1933 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1934 // but a transform matrix can define horizontal and vertical scales.
1935 // Let's take the average between both of them and pass into the shader, practically we
1936 // never do this type of transformation on windows anyway.
1937 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
1938 return parentState;
1939 }
1940 }
1941 const float radius = getDrawingState().cornerRadius;
Peiyong Linb9ff23e2019-04-08 11:04:59 -07001942 return radius > 0 && getCrop(getDrawingState()).isValid()
1943 ? RoundedCornerState(getCrop(getDrawingState()).toFloatRect(), radius)
1944 : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001945}
1946
Vishnu Nair08f6eae2019-11-26 14:01:39 -08001947renderengine::ShadowSettings Layer::getShadowSettings(const Rect& viewport) const {
1948 renderengine::ShadowSettings state = mFlinger->mDrawingState.globalShadowSettings;
1949
1950 // Shift the spot light x-position to the middle of the display and then
1951 // offset it by casting layer's screen pos.
1952 state.lightPos.x = (viewport.width() / 2.f) - mScreenBounds.left;
1953 state.lightPos.y -= mScreenBounds.top;
1954
1955 state.length = mEffectiveShadowRadius;
1956 return state;
1957}
1958
Robert Carr1f0a16a2016-10-24 16:27:39 -07001959void Layer::commitChildList() {
1960 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1961 const auto& child = mCurrentChildren[i];
1962 child->commitChildList();
1963 }
1964 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001965 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001966}
1967
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001968static wp<Layer> extractLayerFromBinder(const wp<IBinder>& weakBinderHandle) {
1969 if (weakBinderHandle == nullptr) {
1970 return nullptr;
1971 }
1972 sp<IBinder> binderHandle = weakBinderHandle.promote();
1973 if (binderHandle == nullptr) {
1974 return nullptr;
1975 }
1976 sp<Layer::Handle> handle = static_cast<Layer::Handle*>(binderHandle.get());
1977 if (handle == nullptr) {
1978 return nullptr;
1979 }
1980 return handle->owner;
1981}
1982
Robert Carr720e5062018-07-30 17:45:14 -07001983void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001984 mCurrentState.inputInfo = info;
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001985 mCurrentState.touchableRegionCrop = extractLayerFromBinder(info.touchableRegionCropHandle);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001986 mCurrentState.modified = true;
1987 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07001988 setTransactionFlags(eTransactionNeeded);
1989}
1990
chaviw6d89e2d2020-01-14 14:42:01 -08001991LayerProto* Layer::writeToProto(LayersProto& layersProto, uint32_t traceFlags) const {
chaviw08f3cb22020-01-13 13:17:21 -08001992 LayerProto* layerProto = layersProto.add_layers();
1993 writeToProtoDrawingState(layerProto, traceFlags);
1994 writeToProtoCommonState(layerProto, LayerVector::StateSet::Drawing, traceFlags);
1995
1996 for (const sp<Layer>& layer : mDrawingChildren) {
1997 layer->writeToProto(layersProto, traceFlags);
1998 }
chaviw6d89e2d2020-01-14 14:42:01 -08001999
2000 return layerProto;
chaviw08f3cb22020-01-13 13:17:21 -08002001}
2002
Vishnu Nair8406fd72019-07-30 11:29:31 -07002003void Layer::writeToProtoDrawingState(LayerProto* layerInfo, uint32_t traceFlags) const {
2004 ui::Transform transform = getTransform();
2005
2006 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
2007 for (const auto& pendingState : mPendingStatesSnapshot) {
2008 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
2009 if (barrierLayer != nullptr) {
2010 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
2011 barrierLayerProto->set_id(barrierLayer->sequence);
2012 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
2013 }
2014 }
2015
chaviwd62d3062019-09-04 14:48:02 -07002016 auto buffer = getBuffer();
Vishnu Nair8406fd72019-07-30 11:29:31 -07002017 if (buffer != nullptr) {
2018 LayerProtoHelper::writeToProto(buffer,
2019 [&]() { return layerInfo->mutable_active_buffer(); });
chaviw4244e032019-09-04 11:27:49 -07002020 LayerProtoHelper::writeToProto(ui::Transform(getBufferTransform()),
Vishnu Nair8406fd72019-07-30 11:29:31 -07002021 layerInfo->mutable_buffer_transform());
2022 }
2023 layerInfo->set_invalidate(contentDirty);
2024 layerInfo->set_is_protected(isProtected());
chaviw4244e032019-09-04 11:27:49 -07002025 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(getDataSpace())));
Vishnu Nair8406fd72019-07-30 11:29:31 -07002026 layerInfo->set_queued_frames(getQueuedFrameCount());
2027 layerInfo->set_refresh_pending(isBufferLatched());
2028 layerInfo->set_curr_frame(mCurrentFrameNumber);
2029 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
2030
2031 layerInfo->set_corner_radius(getRoundedCornerState().radius);
2032 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
2033 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
2034 [&]() { return layerInfo->mutable_position(); });
2035 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
Lloyd Piquea2468662019-03-07 21:31:06 -08002036 LayerProtoHelper::writeToProto(debugGetVisibleRegionOnDefaultDisplay(),
Vishnu Nair8406fd72019-07-30 11:29:31 -07002037 [&]() { return layerInfo->mutable_visible_region(); });
2038 LayerProtoHelper::writeToProto(surfaceDamageRegion,
2039 [&]() { return layerInfo->mutable_damage_region(); });
chaviwddeae262020-01-06 10:31:23 -08002040
2041 if (hasColorTransform()) {
2042 LayerProtoHelper::writeToProto(getColorTransform(),
2043 layerInfo->mutable_color_transform());
2044 }
Vishnu Nair8406fd72019-07-30 11:29:31 -07002045 }
2046
2047 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
2048 LayerProtoHelper::writeToProto(mSourceBounds,
2049 [&]() { return layerInfo->mutable_source_bounds(); });
2050 LayerProtoHelper::writeToProto(mScreenBounds,
2051 [&]() { return layerInfo->mutable_screen_bounds(); });
Vishnu Nair95a1ed42019-12-06 12:25:11 -08002052 LayerProtoHelper::writeToProto(getRoundedCornerState().cropRect,
2053 [&]() { return layerInfo->mutable_corner_radius_crop(); });
2054 layerInfo->set_shadow_radius(mEffectiveShadowRadius);
Vishnu Nair8406fd72019-07-30 11:29:31 -07002055 }
2056}
2057
2058void Layer::writeToProtoCommonState(LayerProto* layerInfo, LayerVector::StateSet stateSet,
2059 uint32_t traceFlags) const {
chaviw1d044282017-09-27 12:19:28 -07002060 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2061 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002062 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07002063
Peiyong Linefefaac2018-08-17 12:27:51 -07002064 ui::Transform requestedTransform = state.active_legacy.transform;
chaviw1d044282017-09-27 12:19:28 -07002065
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002066 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
2067 layerInfo->set_id(sequence);
2068 layerInfo->set_name(getName().c_str());
chaviw8a01fa42019-08-19 12:39:31 -07002069 layerInfo->set_type(getType());
chaviw1d044282017-09-27 12:19:28 -07002070
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002071 for (const auto& child : children) {
2072 layerInfo->add_children(child->sequence);
chaviw1d044282017-09-27 12:19:28 -07002073 }
chaviw1d044282017-09-27 12:19:28 -07002074
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002075 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
2076 sp<Layer> strongRelative = weakRelative.promote();
2077 if (strongRelative != nullptr) {
2078 layerInfo->add_relatives(strongRelative->sequence);
2079 }
chaviwadc40c22018-07-10 16:57:27 -07002080 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002081
2082 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
2083 [&]() { return layerInfo->mutable_transparent_region(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002084
2085 layerInfo->set_layer_stack(getLayerStack());
2086 layerInfo->set_z(state.z);
2087
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002088 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(),
2089 [&]() {
2090 return layerInfo->mutable_requested_position();
2091 });
2092
2093 LayerProtoHelper::writeSizeToProto(state.active_legacy.w, state.active_legacy.h,
2094 [&]() { return layerInfo->mutable_size(); });
2095
2096 LayerProtoHelper::writeToProto(state.crop_legacy,
2097 [&]() { return layerInfo->mutable_crop(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002098
2099 layerInfo->set_is_opaque(isOpaque(state));
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002100
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002101
2102 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
2103 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
2104 LayerProtoHelper::writeToProto(state.color,
2105 [&]() { return layerInfo->mutable_requested_color(); });
2106 layerInfo->set_flags(state.flags);
2107
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002108 LayerProtoHelper::writeToProto(requestedTransform,
2109 layerInfo->mutable_requested_transform());
2110
2111 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
2112 if (parent != nullptr) {
2113 layerInfo->set_parent(parent->sequence);
2114 } else {
2115 layerInfo->set_parent(-1);
2116 }
2117
2118 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2119 if (zOrderRelativeOf != nullptr) {
2120 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
2121 } else {
2122 layerInfo->set_z_order_relative_of(-1);
2123 }
chaviw08f3cb22020-01-13 13:17:21 -08002124
2125 layerInfo->set_is_relative_of(state.isRelativeOf);
chaviwadc40c22018-07-10 16:57:27 -07002126 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002127
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002128 if (traceFlags & SurfaceTracing::TRACE_INPUT) {
2129 LayerProtoHelper::writeToProto(state.inputInfo, state.touchableRegionCrop,
2130 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002131 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002132
2133 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
2134 auto protoMap = layerInfo->mutable_metadata();
2135 for (const auto& entry : state.metadata.mMap) {
2136 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
2137 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002138 }
chaviw1d044282017-09-27 12:19:28 -07002139}
2140
Robert Carr2e102c92018-10-23 12:11:15 -07002141bool Layer::isRemovedFromCurrentState() const {
2142 return mRemovedFromCurrentState;
2143}
2144
Arthur Hungd20b2702019-01-14 18:16:16 +08002145InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002146 InputWindowInfo info = mDrawingState.inputInfo;
chaviwaf87b3e2019-10-01 16:59:28 -07002147 info.id = sequence;
Robert Carr720e5062018-07-30 17:45:14 -07002148
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002149 if (info.displayId == ADISPLAY_ID_NONE) {
chaviwb15ea8a2019-09-03 12:40:22 -07002150 info.displayId = getLayerStack();
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002151 }
2152
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002153 ui::Transform t = getTransform();
2154 const float xScale = t.sx();
2155 const float yScale = t.sy();
Ady Abraham282f1d72019-07-24 18:05:56 -07002156 int32_t xSurfaceInset = info.surfaceInset;
2157 int32_t ySurfaceInset = info.surfaceInset;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002158 if (xScale != 1.0f || yScale != 1.0f) {
Ady Abraham282f1d72019-07-24 18:05:56 -07002159 info.windowXScale *= (xScale != 0.0f) ? 1.0f / xScale : 0.0f;
2160 info.windowYScale *= (yScale != 0.0f) ? 1.0f / yScale : 0.0f;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002161 info.touchableRegion.scaleSelf(xScale, yScale);
Ady Abraham282f1d72019-07-24 18:05:56 -07002162 xSurfaceInset = std::round(xSurfaceInset * xScale);
2163 ySurfaceInset = std::round(ySurfaceInset * yScale);
Riddle Hsu39d4aa52018-11-30 20:46:53 +08002164 }
Robert Carre07e1032018-11-26 12:55:53 -08002165
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002166 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002167 // If this is a portal window, set the touchableRegion to the layerBounds.
2168 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
2169 ? getBufferSize(getDrawingState())
2170 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002171 if (!layerBounds.isValid()) {
2172 layerBounds = getCroppedBufferSize(getDrawingState());
2173 }
Vishnu Nair8033a492018-12-05 07:27:23 -08002174 layerBounds = t.transform(layerBounds);
Ady Abraham282f1d72019-07-24 18:05:56 -07002175
2176 // clamp inset to layer bounds
2177 xSurfaceInset = (xSurfaceInset >= 0) ? std::min(xSurfaceInset, layerBounds.getWidth() / 2) : 0;
2178 ySurfaceInset = (ySurfaceInset >= 0) ? std::min(ySurfaceInset, layerBounds.getHeight() / 2) : 0;
2179
Arthur Hung118b1142019-05-08 21:25:59 +08002180 layerBounds.inset(xSurfaceInset, ySurfaceInset, xSurfaceInset, ySurfaceInset);
Vishnu Nair8033a492018-12-05 07:27:23 -08002181
Arthur Hungd20b2702019-01-14 18:16:16 +08002182 // Input coordinate should match the layer bounds.
2183 info.frameLeft = layerBounds.left;
2184 info.frameTop = layerBounds.top;
2185 info.frameRight = layerBounds.right;
2186 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08002187
2188 // Position the touchable region relative to frame screen location and restrict it to frame
2189 // bounds.
2190 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08002191 info.visible = canReceiveInput();
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002192
2193 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2194 if (info.replaceTouchableRegionWithCrop) {
2195 if (cropLayer == nullptr) {
2196 info.touchableRegion = Region(Rect{mScreenBounds});
2197 } else {
2198 info.touchableRegion = Region(Rect{cropLayer->mScreenBounds});
2199 }
2200 } else if (cropLayer != nullptr) {
2201 info.touchableRegion = info.touchableRegion.intersect(Rect{cropLayer->mScreenBounds});
2202 }
2203
chaviwaf87b3e2019-10-01 16:59:28 -07002204 // If the layer is a clone, we need to crop the input region to cloned root to prevent
2205 // touches from going outside the cloned area.
2206 if (isClone()) {
2207 sp<Layer> clonedRoot = getClonedRoot();
2208 if (clonedRoot != nullptr) {
2209 Rect rect(clonedRoot->mScreenBounds);
2210 info.touchableRegion = info.touchableRegion.intersect(rect);
2211 }
2212 }
2213
Robert Carr720e5062018-07-30 17:45:14 -07002214 return info;
2215}
2216
chaviwaf87b3e2019-10-01 16:59:28 -07002217sp<Layer> Layer::getClonedRoot() {
2218 if (mClonedChild != nullptr) {
2219 return this;
2220 }
2221 if (mDrawingParent == nullptr || mDrawingParent.promote() == nullptr) {
2222 return nullptr;
2223 }
2224 return mDrawingParent.promote()->getClonedRoot();
2225}
2226
Robert Carr720e5062018-07-30 17:45:14 -07002227bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002228 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002229}
2230
chaviw3e727cd2019-01-31 13:41:05 -08002231bool Layer::canReceiveInput() const {
Vishnu Nair82353e92019-09-12 12:38:47 -07002232 return !isHiddenByPolicy();
chaviw3e727cd2019-01-31 13:41:05 -08002233}
2234
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002235compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
2236 const sp<const DisplayDevice>& display) const {
Lloyd Piquede196652020-01-22 17:29:58 -08002237 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionEngineLayerFE());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002238}
2239
Lloyd Piquea2468662019-03-07 21:31:06 -08002240Region Layer::debugGetVisibleRegionOnDefaultDisplay() const {
2241 sp<DisplayDevice> displayDevice = mFlinger->getDefaultDisplayDeviceLocked();
2242 if (displayDevice == nullptr) {
2243 return {};
2244 }
2245
2246 auto outputLayer = findOutputLayerForDisplay(displayDevice);
2247 if (outputLayer == nullptr) {
2248 return {};
2249 }
2250
2251 return outputLayer->getState().visibleRegion;
2252}
2253
chaviwb4c6e582019-08-16 14:35:07 -07002254void Layer::setInitialValuesForClone(const sp<Layer>& clonedFrom) {
2255 // copy drawing state from cloned layer
2256 mDrawingState = clonedFrom->mDrawingState;
2257 mClonedFrom = clonedFrom;
chaviwb4c6e582019-08-16 14:35:07 -07002258}
chaviw74b03172019-08-19 11:09:03 -07002259
2260void Layer::updateMirrorInfo() {
2261 if (mClonedChild == nullptr || !mClonedChild->isClonedFromAlive()) {
2262 // If mClonedChild is null, there is nothing to mirror. If isClonedFromAlive returns false,
2263 // it means that there is a clone, but the layer it was cloned from has been destroyed. In
2264 // that case, we want to delete the reference to the clone since we want it to get
2265 // destroyed. The root, this layer, will still be around since the client can continue
2266 // to hold a reference, but no cloned layers will be displayed.
2267 mClonedChild = nullptr;
2268 return;
2269 }
2270
2271 std::map<sp<Layer>, sp<Layer>> clonedLayersMap;
2272 // If the real layer exists and is in current state, add the clone as a child of the root.
2273 // There's no need to remove from drawingState when the layer is offscreen since currentState is
2274 // copied to drawingState for the root layer. So the clonedChild is always removed from
2275 // drawingState and then needs to be added back each traversal.
2276 if (!mClonedChild->getClonedFrom()->isRemovedFromCurrentState()) {
2277 addChildToDrawing(mClonedChild);
2278 }
2279
2280 mClonedChild->updateClonedDrawingState(clonedLayersMap);
2281 mClonedChild->updateClonedChildren(this, clonedLayersMap);
2282 mClonedChild->updateClonedRelatives(clonedLayersMap);
2283}
2284
2285void Layer::updateClonedDrawingState(std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2286 // If the layer the clone was cloned from is alive, copy the content of the drawingState
2287 // to the clone. If the real layer is no longer alive, continue traversing the children
2288 // since we may be able to pull out other children that are still alive.
2289 if (isClonedFromAlive()) {
2290 sp<Layer> clonedFrom = getClonedFrom();
2291 mDrawingState = clonedFrom->mDrawingState;
chaviw74b03172019-08-19 11:09:03 -07002292 clonedLayersMap.emplace(clonedFrom, this);
2293 }
2294
2295 // The clone layer may have children in drawingState since they may have been created and
2296 // added from a previous request to updateMirorInfo. This is to ensure we don't recreate clones
2297 // that already exist, since we can just re-use them.
2298 // The drawingChildren will not get overwritten by the currentChildren since the clones are
2299 // not updated in the regular traversal. They are skipped since the root will lose the
2300 // reference to them when it copies its currentChildren to drawing.
2301 for (sp<Layer>& child : mDrawingChildren) {
2302 child->updateClonedDrawingState(clonedLayersMap);
2303 }
2304}
2305
2306void Layer::updateClonedChildren(const sp<Layer>& mirrorRoot,
2307 std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2308 mDrawingChildren.clear();
2309
2310 if (!isClonedFromAlive()) {
2311 return;
2312 }
2313
2314 sp<Layer> clonedFrom = getClonedFrom();
2315 for (sp<Layer>& child : clonedFrom->mDrawingChildren) {
2316 if (child == mirrorRoot) {
2317 // This is to avoid cyclical mirroring.
2318 continue;
2319 }
2320 sp<Layer> clonedChild = clonedLayersMap[child];
2321 if (clonedChild == nullptr) {
2322 clonedChild = child->createClone();
2323 clonedLayersMap[child] = clonedChild;
2324 }
2325 addChildToDrawing(clonedChild);
2326 clonedChild->updateClonedChildren(mirrorRoot, clonedLayersMap);
2327 }
2328}
2329
chaviwaf87b3e2019-10-01 16:59:28 -07002330void Layer::updateClonedInputInfo(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2331 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2332 if (cropLayer != nullptr) {
2333 if (clonedLayersMap.count(cropLayer) == 0) {
2334 // Real layer had a crop layer but it's not in the cloned hierarchy. Just set to
2335 // self as crop layer to avoid going outside bounds.
2336 mDrawingState.touchableRegionCrop = this;
2337 } else {
2338 const sp<Layer>& clonedCropLayer = clonedLayersMap.at(cropLayer);
2339 mDrawingState.touchableRegionCrop = clonedCropLayer;
2340 }
2341 }
2342 // Cloned layers shouldn't handle watch outside since their z order is not determined by
2343 // WM or the client.
2344 mDrawingState.inputInfo.layoutParamsFlags &= ~InputWindowInfo::FLAG_WATCH_OUTSIDE_TOUCH;
2345}
2346
2347void Layer::updateClonedRelatives(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
chaviw74b03172019-08-19 11:09:03 -07002348 mDrawingState.zOrderRelativeOf = nullptr;
2349 mDrawingState.zOrderRelatives.clear();
2350
2351 if (!isClonedFromAlive()) {
2352 return;
2353 }
2354
chaviwaf87b3e2019-10-01 16:59:28 -07002355 const sp<Layer>& clonedFrom = getClonedFrom();
chaviw74b03172019-08-19 11:09:03 -07002356 for (wp<Layer>& relativeWeak : clonedFrom->mDrawingState.zOrderRelatives) {
chaviwaf87b3e2019-10-01 16:59:28 -07002357 const sp<Layer>& relative = relativeWeak.promote();
2358 if (clonedLayersMap.count(relative) > 0) {
2359 auto& clonedRelative = clonedLayersMap.at(relative);
chaviw74b03172019-08-19 11:09:03 -07002360 mDrawingState.zOrderRelatives.add(clonedRelative);
2361 }
2362 }
2363
2364 // Check if the relativeLayer for the real layer is part of the cloned hierarchy.
2365 // It's possible that the layer it's relative to is outside the requested cloned hierarchy.
2366 // In that case, we treat the layer as if the relativeOf has been removed. This way, it will
2367 // still traverse the children, but the layer with the missing relativeOf will not be shown
2368 // on screen.
chaviwaf87b3e2019-10-01 16:59:28 -07002369 const sp<Layer>& relativeOf = clonedFrom->mDrawingState.zOrderRelativeOf.promote();
2370 if (clonedLayersMap.count(relativeOf) > 0) {
2371 const sp<Layer>& clonedRelativeOf = clonedLayersMap.at(relativeOf);
chaviw74b03172019-08-19 11:09:03 -07002372 mDrawingState.zOrderRelativeOf = clonedRelativeOf;
2373 }
2374
chaviwaf87b3e2019-10-01 16:59:28 -07002375 updateClonedInputInfo(clonedLayersMap);
2376
chaviw74b03172019-08-19 11:09:03 -07002377 for (sp<Layer>& child : mDrawingChildren) {
2378 child->updateClonedRelatives(clonedLayersMap);
2379 }
2380}
2381
2382void Layer::addChildToDrawing(const sp<Layer>& layer) {
2383 mDrawingChildren.add(layer);
2384 layer->mDrawingParent = this;
2385}
2386
Mathias Agopian13127d82013-03-05 17:47:11 -08002387// ---------------------------------------------------------------------------
2388
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002389}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002390
2391#if defined(__gl_h_)
2392#error "don't include gl/gl.h in this file"
2393#endif
2394
2395#if defined(__gl2_h_)
2396#error "don't include gl2/gl2.h in this file"
2397#endif
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -08002398
2399// TODO(b/129481165): remove the #pragma below and fix conversion issues
2400#pragma clang diagnostic pop // ignored "-Wconversion"