blob: 53866445527bc49aab7c164fa2982473470377ea [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
Dan Stoza9e56aa02015-11-02 13:00:03 -080017//#define LOG_NDEBUG 0
18#undef LOG_TAG
19#define LOG_TAG "Layer"
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080020#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21
Alec Mourie60041e2019-06-14 18:59:51 -070022#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023
Yiwei Zhang5434a782018-12-05 18:06:32 -080024#include <android-base/stringprintf.h>
Vishnu Nair0f085c62019-08-30 08:49:12 -070025#include <binder/IPCThreadState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080026#include <compositionengine/Display.h>
Lloyd Pique0b785d82018-12-04 17:25:27 -080027#include <compositionengine/Layer.h>
Lloyd Piquea83776c2019-01-29 18:42:32 -080028#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080029#include <compositionengine/OutputLayer.h>
30#include <compositionengine/impl/OutputLayerCompositionState.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070031#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070032#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070033#include <cutils/properties.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080034#include <gui/BufferItem.h>
35#include <gui/LayerDebugInfo.h>
36#include <gui/Surface.h>
Alec Mourie60041e2019-06-14 18:59:51 -070037#include <math.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080038#include <renderengine/RenderEngine.h>
Alec Mourie60041e2019-06-14 18:59:51 -070039#include <stdint.h>
40#include <stdlib.h>
41#include <sys/types.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080042#include <ui/DebugUtils.h>
43#include <ui/GraphicBuffer.h>
44#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045#include <utils/Errors.h>
46#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080047#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080049#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050
Alec Mourie60041e2019-06-14 18:59:51 -070051#include <algorithm>
52#include <mutex>
53#include <sstream>
54
Yiwei Zhang60d1a192018-03-07 14:52:28 -080055#include "BufferLayer.h"
Valerie Haudd0b7572019-01-29 14:59:27 -080056#include "ColorLayer.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020057#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070058#include "DisplayDevice.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080059#include "DisplayHardware/HWComposer.h"
Mikael Pessa90092f42019-08-26 17:22:04 -070060#include "FrameTracer/FrameTracer.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080061#include "LayerProtoHelper.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070062#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070063#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064#include "SurfaceFlinger.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080065#include "TimeStats/TimeStats.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070066
David Sodman41fdfc92017-11-06 16:09:56 -080067#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069namespace android {
70
Yiwei Zhang5434a782018-12-05 18:06:32 -080071using base::StringAppendF;
72
Lloyd Piquef1c675b2018-09-12 20:45:39 -070073std::atomic<int32_t> Layer::sSequence{1};
Mathias Agopian13127d82013-03-05 17:47:11 -080074
Lloyd Pique42ab75e2018-09-12 20:46:03 -070075Layer::Layer(const LayerCreationArgs& args)
Ady Abraham8f1ee7f2019-04-05 10:32:50 -070076 : mFlinger(args.flinger),
77 mName(args.name),
78 mClientRef(args.client),
79 mWindowType(args.metadata.getInt32(METADATA_WINDOW_TYPE, 0)) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -070080
81 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -070082 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
83 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
84 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070085
Lloyd Pique0449b0f2018-12-20 16:23:45 -080086 mCurrentState.active_legacy.w = args.w;
87 mCurrentState.active_legacy.h = args.h;
88 mCurrentState.flags = layerFlags;
89 mCurrentState.active_legacy.transform.set(0, 0);
90 mCurrentState.crop_legacy.makeInvalid();
91 mCurrentState.requestedCrop_legacy = mCurrentState.crop_legacy;
92 mCurrentState.z = 0;
93 mCurrentState.color.a = 1.0f;
94 mCurrentState.layerStack = 0;
95 mCurrentState.sequence = 0;
96 mCurrentState.requested_legacy = mCurrentState.active_legacy;
Lloyd Pique0449b0f2018-12-20 16:23:45 -080097 mCurrentState.active.w = UINT32_MAX;
98 mCurrentState.active.h = UINT32_MAX;
99 mCurrentState.active.transform.set(0, 0);
100 mCurrentState.transform = 0;
101 mCurrentState.transformToDisplayInverse = false;
102 mCurrentState.crop.makeInvalid();
103 mCurrentState.acquireFence = new Fence(-1);
104 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
105 mCurrentState.hdrMetadata.validTypes = 0;
Marissa Wall7e0a01f2019-08-14 14:29:25 -0700106 mCurrentState.surfaceDamageRegion = Region::INVALID_REGION;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800107 mCurrentState.cornerRadius = 0.0f;
108 mCurrentState.api = -1;
109 mCurrentState.hasColorTransform = false;
Peiyong Linc502cb72019-03-01 15:00:23 -0800110 mCurrentState.colorSpaceAgnostic = false;
Evan Roskya1f1e152019-01-24 16:17:46 -0800111 mCurrentState.metadata = args.metadata;
Vishnu Nairf37dcfb2019-11-16 07:37:09 -0800112 mCurrentState.shadowRadius = 0.f;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700113
114 // drawing state & current state are identical
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800115 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700116
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800117 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700118 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800119 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200120 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700121
Vishnu Nair0f085c62019-08-30 08:49:12 -0700122 mCallingPid = args.callingPid;
123 mCallingUid = args.callingUid;
Dominik Laskowski75848362019-11-11 17:57:20 -0800124}
125
126void Layer::onFirstRef() {
127 mFlinger->onLayerFirstRef(this);
Dan Stoza436ccf32018-06-21 12:10:12 -0700128}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700129
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700130Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800131 sp<Client> c(mClientRef.promote());
132 if (c != 0) {
133 c->detachLayer(this);
134 }
135
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000136 mFrameTracker.logAndResetStats(mName);
chaviw74d90ad2019-04-26 14:45:26 -0700137 mFlinger->onLayerDestroyed(this);
Mathias Agopian96f08192010-06-02 23:28:45 -0700138}
139
Vishnu Nair0f085c62019-08-30 08:49:12 -0700140LayerCreationArgs::LayerCreationArgs(SurfaceFlinger* flinger, const sp<Client>& client,
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700141 std::string name, uint32_t w, uint32_t h, uint32_t flags,
Vishnu Nair0f085c62019-08-30 08:49:12 -0700142 LayerMetadata metadata)
143 : flinger(flinger),
144 client(client),
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700145 name(std::move(name)),
Vishnu Nair0f085c62019-08-30 08:49:12 -0700146 w(w),
147 h(h),
148 flags(flags),
149 metadata(std::move(metadata)) {
150 IPCThreadState* ipc = IPCThreadState::self();
151 callingPid = ipc->getCallingPid();
152 callingUid = ipc->getCallingUid();
153}
154
Mathias Agopian13127d82013-03-05 17:47:11 -0800155// ---------------------------------------------------------------------------
156// callbacks
157// ---------------------------------------------------------------------------
158
David Sodmaneb085e02017-10-05 18:49:04 -0700159/*
160 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
161 * Layer. So, the implementation is done in BufferLayer. When called on a
162 * ColorLayer object, it's essentially a NOP.
163 */
David Sodmaneb085e02017-10-05 18:49:04 -0700164void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800165
chaviw43cb3cb2019-05-31 15:23:41 -0700166void Layer::removeRemoteSyncPoints() {
167 for (auto& point : mRemoteSyncPoints) {
168 point->setTransactionApplied();
169 }
170 mRemoteSyncPoints.clear();
171
172 {
chaviw43cb3cb2019-05-31 15:23:41 -0700173 for (State pendingState : mPendingStates) {
174 pendingState.barrierLayer_legacy = nullptr;
175 }
176 }
177}
178
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700179void Layer::removeRelativeZ(const std::vector<Layer*>& layersInTree) {
180 if (mCurrentState.zOrderRelativeOf == nullptr) {
181 return;
182 }
Robert Carr2e102c92018-10-23 12:11:15 -0700183
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700184 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
185 if (strongRelative == nullptr) {
186 setZOrderRelativeOf(nullptr);
187 return;
188 }
189
190 if (!std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
191 strongRelative->removeZOrderRelative(this);
192 mFlinger->setTransactionFlags(eTraversalNeeded);
chaviw606e5cf2019-03-01 10:12:10 -0800193 setZOrderRelativeOf(nullptr);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700194 }
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700195}
196
197void Layer::removeFromCurrentState() {
198 mRemovedFromCurrentState = true;
Rob Carr4bba3702018-10-08 21:53:30 +0000199
Robert Carr2e102c92018-10-23 12:11:15 -0700200 // Since we are no longer reachable from CurrentState SurfaceFlinger
201 // will no longer invoke doTransaction for us, and so we will
202 // never finish applying transactions. We signal the sync point
203 // now so that another layer will not become indefinitely
204 // blocked.
chaviw43cb3cb2019-05-31 15:23:41 -0700205 removeRemoteSyncPoints();
Robert Carr2e102c92018-10-23 12:11:15 -0700206
207 {
208 Mutex::Autolock syncLock(mLocalSyncPointMutex);
209 for (auto& point : mLocalSyncPoints) {
210 point->setFrameAvailable();
211 }
212 mLocalSyncPoints.clear();
213 }
214
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800215 mFlinger->markLayerPendingRemovalLocked(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700216}
Chia-I Wu38512252017-05-17 14:36:16 -0700217
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700218void Layer::onRemovedFromCurrentState() {
219 auto layersInTree = getLayersInTree(LayerVector::StateSet::Current);
220 std::sort(layersInTree.begin(), layersInTree.end());
221 for (const auto& layer : layersInTree) {
222 layer->removeFromCurrentState();
223 layer->removeRelativeZ(layersInTree);
224 }
225}
226
chaviw61626f22018-11-15 16:26:27 -0800227void Layer::addToCurrentState() {
228 mRemovedFromCurrentState = false;
229
230 for (const auto& child : mCurrentChildren) {
231 child->addToCurrentState();
232 }
233}
234
Mathias Agopian13127d82013-03-05 17:47:11 -0800235// ---------------------------------------------------------------------------
236// set-up
237// ---------------------------------------------------------------------------
238
chaviw13fdc492017-06-27 12:40:18 -0700239bool Layer::getPremultipledAlpha() const {
240 return mPremultipliedAlpha;
241}
242
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700243sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800244 Mutex::Autolock _l(mLock);
Robert Carrc0df3122019-04-11 13:18:21 -0700245 if (mGetHandleCalled) {
246 ALOGE("Get handle called twice" );
247 return nullptr;
248 }
249 mGetHandleCalled = true;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700250 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800251}
252
253// ---------------------------------------------------------------------------
254// h/w composer set-up
255// ---------------------------------------------------------------------------
256
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700257static Rect reduce(const Rect& win, const Region& exclude) {
258 if (CC_LIKELY(exclude.isEmpty())) {
259 return win;
260 }
261 if (exclude.isRect()) {
262 return win.reduce(exclude.getBounds());
263 }
264 return Region(win).subtract(exclude).getBounds();
265}
266
Dan Stoza80d61162017-12-20 15:57:52 -0800267static FloatRect reduce(const FloatRect& win, const Region& exclude) {
268 if (CC_LIKELY(exclude.isEmpty())) {
269 return win;
270 }
271 // Convert through Rect (by rounding) for lack of FloatRegion
272 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
273}
274
Vishnu Nair4351ad52019-02-11 14:13:02 -0800275Rect Layer::getScreenBounds(bool reduceTransparentRegion) const {
Vishnu Nairf0c28512019-02-08 12:40:28 -0800276 if (!reduceTransparentRegion) {
277 return Rect{mScreenBounds};
278 }
279
280 FloatRect bounds = getBounds();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800281 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800282 // Transform to screen space.
283 bounds = t.transform(bounds);
284 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700285}
286
Vishnu Nair4351ad52019-02-11 14:13:02 -0800287FloatRect Layer::getBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000288 const State& s(getDrawingState());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800289 return getBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700290}
291
Vishnu Nairf0c28512019-02-08 12:40:28 -0800292FloatRect Layer::getBounds(const Region& activeTransparentRegion) const {
293 // Subtract the transparent region and snap to the bounds.
294 return reduce(mBounds, activeTransparentRegion);
295}
296
Vishnu Nairc652ff82019-03-15 12:48:54 -0700297ui::Transform Layer::getBufferScaleTransform() const {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800298 // If the layer is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
299 // it isFixedSize) then there may be additional scaling not accounted
Vishnu Nairc652ff82019-03-15 12:48:54 -0700300 // for in the layer transform.
chaviwd62d3062019-09-04 14:48:02 -0700301 if (!isFixedSize() || getBuffer() == nullptr) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700302 return {};
Vishnu Nair4351ad52019-02-11 14:13:02 -0800303 }
304
Marissa Wall290ad082019-03-06 13:23:47 -0800305 // If the layer is a buffer state layer, the active width and height
306 // could be infinite. In that case, return the effective transform.
307 const uint32_t activeWidth = getActiveWidth(getDrawingState());
308 const uint32_t activeHeight = getActiveHeight(getDrawingState());
309 if (activeWidth >= UINT32_MAX && activeHeight >= UINT32_MAX) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700310 return {};
Marissa Wall290ad082019-03-06 13:23:47 -0800311 }
312
chaviwd62d3062019-09-04 14:48:02 -0700313 int bufferWidth = getBuffer()->getWidth();
314 int bufferHeight = getBuffer()->getHeight();
Vishnu Nairc652ff82019-03-15 12:48:54 -0700315
chaviw4244e032019-09-04 11:27:49 -0700316 if (getBufferTransform() & NATIVE_WINDOW_TRANSFORM_ROT_90) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700317 std::swap(bufferWidth, bufferHeight);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800318 }
Vishnu Nairc652ff82019-03-15 12:48:54 -0700319
Marissa Wall290ad082019-03-06 13:23:47 -0800320 float sx = activeWidth / static_cast<float>(bufferWidth);
321 float sy = activeHeight / static_cast<float>(bufferHeight);
322
Vishnu Nair4351ad52019-02-11 14:13:02 -0800323 ui::Transform extraParentScaling;
324 extraParentScaling.set(sx, 0, 0, sy);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700325 return extraParentScaling;
326}
327
328ui::Transform Layer::getTransformWithScale(const ui::Transform& bufferScaleTransform) const {
329 // We need to mirror this scaling to child surfaces or we will break the contract where WM can
330 // treat child surfaces as pixels in the parent surface.
chaviwd62d3062019-09-04 14:48:02 -0700331 if (!isFixedSize() || getBuffer() == nullptr) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700332 return mEffectiveTransform;
333 }
334 return mEffectiveTransform * bufferScaleTransform;
335}
336
337FloatRect Layer::getBoundsPreScaling(const ui::Transform& bufferScaleTransform) const {
338 // We need the pre scaled layer bounds when computing child bounds to make sure the child is
339 // cropped to its parent layer after any buffer transform scaling is applied.
chaviwd62d3062019-09-04 14:48:02 -0700340 if (!isFixedSize() || getBuffer() == nullptr) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700341 return mBounds;
342 }
343 return bufferScaleTransform.inverse().transform(mBounds);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800344}
345
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700346void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform,
347 float parentShadowRadius) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800348 const State& s(getDrawingState());
349
350 // Calculate effective layer transform
351 mEffectiveTransform = parentTransform * getActiveTransform(s);
352
353 // Transform parent bounds to layer space
354 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
355
Vishnu Nairc652ff82019-03-15 12:48:54 -0700356 // Calculate source bounds
Vishnu Nair4351ad52019-02-11 14:13:02 -0800357 mSourceBounds = computeSourceBounds(parentBounds);
358
359 // Calculate bounds by croping diplay frame with layer crop and parent bounds
360 FloatRect bounds = mSourceBounds;
361 const Rect layerCrop = getCrop(s);
362 if (!layerCrop.isEmpty()) {
363 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
364 }
365 bounds = bounds.intersect(parentBounds);
366
367 mBounds = bounds;
368 mScreenBounds = mEffectiveTransform.transform(mBounds);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700369
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700370 // Use the layer's own shadow radius if set. Otherwise get the radius from
371 // parent.
372 if (s.shadowRadius > 0.f) {
373 mEffectiveShadowRadius = s.shadowRadius;
374 } else {
375 mEffectiveShadowRadius = parentShadowRadius;
376 }
377
378 // Shadow radius is passed down to only one layer so if the layer can draw shadows,
379 // don't pass it to its children.
380 const float childShadowRadius = canDrawShadows() ? 0.f : mEffectiveShadowRadius;
381
Vishnu Nairc652ff82019-03-15 12:48:54 -0700382 // Add any buffer scaling to the layer's children.
383 ui::Transform bufferScaleTransform = getBufferScaleTransform();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800384 for (const sp<Layer>& child : mDrawingChildren) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700385 child->computeBounds(getBoundsPreScaling(bufferScaleTransform),
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700386 getTransformWithScale(bufferScaleTransform), childShadowRadius);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800387 }
388}
389
Vishnu Nair60356342018-11-13 13:00:45 -0800390Rect Layer::getCroppedBufferSize(const State& s) const {
391 Rect size = getBufferSize(s);
392 Rect crop = getCrop(s);
393 if (!crop.isEmpty() && size.isValid()) {
394 size.intersect(crop, &size);
395 } else if (!crop.isEmpty()) {
396 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700397 }
Vishnu Nair60356342018-11-13 13:00:45 -0800398 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800399}
400
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700401void Layer::setupRoundedCornersCropCoordinates(Rect win,
402 const FloatRect& roundedCornersCrop) const {
403 // Translate win by the rounded corners rect coordinates, to have all values in
404 // layer coordinate space.
405 win.left -= roundedCornersCrop.left;
406 win.right -= roundedCornersCrop.left;
407 win.top -= roundedCornersCrop.top;
408 win.bottom -= roundedCornersCrop.top;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700409}
410
Lloyd Piquec6687342019-03-07 21:34:57 -0800411void Layer::latchBasicGeometry(compositionengine::LayerFECompositionState& compositionState) const {
412 const auto& drawingState{getDrawingState()};
413 const uint32_t layerStack = getLayerStack();
414 const auto alpha = static_cast<float>(getAlpha());
415 const bool opaque = isOpaque(drawingState);
416 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
417
418 auto blendMode = Hwc2::IComposerClient::BlendMode::NONE;
419 if (!opaque || alpha != 1.0f) {
420 blendMode = mPremultipliedAlpha ? Hwc2::IComposerClient::BlendMode::PREMULTIPLIED
421 : Hwc2::IComposerClient::BlendMode::COVERAGE;
422 }
423
424 // TODO(b/121291683): Instead of filling in a passed-in compositionState
425 // structure, switch to Layer owning the structure and have
426 // CompositionEngine be able to get a reference to it.
427
428 compositionState.layerStackId =
429 (layerStack != ~0u) ? std::make_optional(layerStack) : std::nullopt;
430 compositionState.internalOnly = getPrimaryDisplayOnly();
431 compositionState.isVisible = isVisible();
432 compositionState.isOpaque = opaque && !usesRoundedCorners && alpha == 1.f;
Vishnu Naira483b4a2019-12-12 15:07:52 -0800433 compositionState.shadowRadius = mEffectiveShadowRadius;
Lloyd Piquec6687342019-03-07 21:34:57 -0800434
435 compositionState.contentDirty = contentDirty;
436 contentDirty = false;
437
438 compositionState.geomLayerBounds = mBounds;
439 compositionState.geomLayerTransform = getTransform();
440 compositionState.geomInverseLayerTransform = compositionState.geomLayerTransform.inverse();
441 compositionState.transparentRegionHint = getActiveTransparentRegion(drawingState);
442
443 compositionState.blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
444 compositionState.alpha = alpha;
445}
446
Lloyd Piquea83776c2019-01-29 18:42:32 -0800447void Layer::latchGeometry(compositionengine::LayerFECompositionState& compositionState) const {
448 const auto& drawingState{getDrawingState()};
Mathias Agopian13127d82013-03-05 17:47:11 -0800449
Lloyd Piquea83776c2019-01-29 18:42:32 -0800450 int type = drawingState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
451 int appId = drawingState.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700452 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400453 if (parent.get()) {
454 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800455 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
456 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
Jiwen 'Steve' Cai736c74e2019-05-28 18:33:22 -0700457 if (parentType > 0 && parentAppId > 0) {
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800458 type = parentType;
459 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700460 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400461 }
462
Lloyd Piquea83776c2019-01-29 18:42:32 -0800463 compositionState.geomBufferSize = getBufferSize(drawingState);
chaviw4244e032019-09-04 11:27:49 -0700464 compositionState.geomContentCrop = getBufferCrop();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800465 compositionState.geomCrop = getCrop(drawingState);
chaviw4244e032019-09-04 11:27:49 -0700466 compositionState.geomBufferTransform = getBufferTransform();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800467 compositionState.geomBufferUsesDisplayInverseTransform = getTransformToDisplayInverse();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800468 compositionState.geomUsesSourceCrop = usesSourceCrop();
469 compositionState.isSecure = isSecure();
David Sodman15094112018-10-11 09:39:37 -0700470
Lloyd Piquea83776c2019-01-29 18:42:32 -0800471 compositionState.type = type;
472 compositionState.appId = appId;
473}
David Sodmanba340492018-08-05 21:51:33 -0700474
Lloyd Piquef5275482019-01-29 18:42:42 -0800475void Layer::latchPerFrameState(compositionengine::LayerFECompositionState& compositionState) const {
Lloyd Pique688abd42019-02-15 15:42:24 -0800476 const auto& drawingState{getDrawingState()};
Lloyd Piquef5275482019-01-29 18:42:42 -0800477 compositionState.forceClientComposition = false;
478
Lloyd Piquef5275482019-01-29 18:42:42 -0800479 compositionState.isColorspaceAgnostic = isColorSpaceAgnostic();
chaviw4244e032019-09-04 11:27:49 -0700480 compositionState.dataspace = getDataSpace();
Lloyd Piquef5275482019-01-29 18:42:42 -0800481 compositionState.colorTransform = getColorTransform();
482 compositionState.colorTransformIsIdentity = !hasColorTransform();
483 compositionState.surfaceDamage = surfaceDamageRegion;
Lloyd Pique688abd42019-02-15 15:42:24 -0800484 compositionState.hasProtectedContent = isProtected();
485
486 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700487 const bool drawsShadows = mEffectiveShadowRadius != 0.f;
488
Lloyd Pique688abd42019-02-15 15:42:24 -0800489 compositionState.isOpaque =
490 isOpaque(drawingState) && !usesRoundedCorners && getAlpha() == 1.0_hf;
Lloyd Piquef5275482019-01-29 18:42:42 -0800491
492 // Force client composition for special cases known only to the front-end.
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700493 if (isHdrY410() || usesRoundedCorners || drawsShadows) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800494 compositionState.forceClientComposition = true;
495 }
496}
497
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800498void Layer::latchCursorCompositionState(
499 compositionengine::LayerFECompositionState& compositionState) const {
500 // This gives us only the "orientation" component of the transform
501 const State& drawingState{getDrawingState()};
502
503 // Apply the layer's transform, followed by the display's global transform
504 // Here we're guaranteed that the layer's transform preserves rects
505 Rect win = getCroppedBufferSize(drawingState);
506 // Subtract the transparent region and snap to the bounds
507 Rect bounds = reduce(win, getActiveTransparentRegion(drawingState));
508 Rect frame(getTransform().transform(bounds));
509
510 compositionState.cursorFrame = frame;
511}
512
Lloyd Piquef16688f2019-02-19 17:47:57 -0800513bool Layer::onPreComposition(nsecs_t) {
514 return false;
515}
516
Lloyd Piquea83776c2019-01-29 18:42:32 -0800517void Layer::latchCompositionState(compositionengine::LayerFECompositionState& compositionState,
Lloyd Piquec6687342019-03-07 21:34:57 -0800518 compositionengine::LayerFE::StateSubset subset) const {
519 using StateSubset = compositionengine::LayerFE::StateSubset;
Lloyd Piquef5275482019-01-29 18:42:42 -0800520
Lloyd Piquec6687342019-03-07 21:34:57 -0800521 switch (subset) {
522 case StateSubset::BasicGeometry:
523 latchBasicGeometry(compositionState);
524 break;
525
526 case StateSubset::GeometryAndContent:
527 latchBasicGeometry(compositionState);
528 latchGeometry(compositionState);
529 latchPerFrameState(compositionState);
530 break;
531
532 case StateSubset::Content:
533 latchPerFrameState(compositionState);
534 break;
535 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800536}
Mathias Agopian29a367b2011-07-12 14:51:45 -0700537
Lloyd Piquea83776c2019-01-29 18:42:32 -0800538const char* Layer::getDebugName() const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700539 return mName.c_str();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800540}
541
Mathias Agopian13127d82013-03-05 17:47:11 -0800542// ---------------------------------------------------------------------------
543// drawing...
544// ---------------------------------------------------------------------------
545
Lloyd Piquef16688f2019-02-19 17:47:57 -0800546std::optional<renderengine::LayerSettings> Layer::prepareClientComposition(
547 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
548 if (!getCompositionLayer()) {
549 return {};
550 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800551
Vishnu Nair4351ad52019-02-11 14:13:02 -0800552 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000553 half alpha = getAlpha();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800554 renderengine::LayerSettings layerSettings;
555 layerSettings.geometry.boundaries = bounds;
556 if (targetSettings.useIdentityTransform) {
557 layerSettings.geometry.positionTransform = mat4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000558 } else {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800559 layerSettings.geometry.positionTransform = getTransform().asMatrix4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000560 }
561
562 if (hasColorTransform()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800563 layerSettings.colorTransform = getColorTransform();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000564 }
565
566 const auto roundedCornerState = getRoundedCornerState();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800567 layerSettings.geometry.roundedCornersRadius = roundedCornerState.radius;
568 layerSettings.geometry.roundedCornersCrop = roundedCornerState.cropRect;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000569
Lloyd Piquef16688f2019-02-19 17:47:57 -0800570 layerSettings.alpha = alpha;
chaviw4244e032019-09-04 11:27:49 -0700571 layerSettings.sourceDataspace = getDataSpace();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800572 return layerSettings;
Mathias Agopian13127d82013-03-05 17:47:11 -0800573}
574
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800575std::optional<renderengine::LayerSettings> Layer::prepareShadowClientComposition(
576 const renderengine::LayerSettings& casterLayerSettings, const Rect& displayViewport,
577 ui::Dataspace outputDataspace) {
578 renderengine::ShadowSettings shadow = getShadowSettings(displayViewport);
579 if (shadow.length <= 0.f) {
580 return {};
581 }
582
583 const float casterAlpha = casterLayerSettings.alpha;
584 const bool casterIsOpaque = ((casterLayerSettings.source.buffer.buffer != nullptr) &&
585 casterLayerSettings.source.buffer.isOpaque);
586
587 renderengine::LayerSettings shadowLayer = casterLayerSettings;
588 shadowLayer.shadow = shadow;
Vishnu Naira483b4a2019-12-12 15:07:52 -0800589 shadowLayer.geometry.boundaries = mBounds; // ignore transparent region
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800590
591 // If the casting layer is translucent, we need to fill in the shadow underneath the layer.
592 // Otherwise the generated shadow will only be shown around the casting layer.
593 shadowLayer.shadow.casterIsTranslucent = !casterIsOpaque || (casterAlpha < 1.0f);
594 shadowLayer.shadow.ambientColor *= casterAlpha;
595 shadowLayer.shadow.spotColor *= casterAlpha;
596 shadowLayer.sourceDataspace = outputDataspace;
597 shadowLayer.source.buffer.buffer = nullptr;
598
599 if (shadowLayer.shadow.ambientColor.a <= 0.f && shadowLayer.shadow.spotColor.a <= 0.f) {
600 return {};
601 }
602
603 float casterCornerRadius = shadowLayer.geometry.roundedCornersRadius;
604 const FloatRect& cornerRadiusCropRect = casterLayerSettings.geometry.roundedCornersCrop;
605 const FloatRect& casterRect = shadowLayer.geometry.boundaries;
606
607 // crop used to set the corner radius may be larger than the content rect. Adjust the corner
608 // radius accordingly.
609 if (casterCornerRadius > 0.f) {
610 float cropRectOffset = std::max(std::abs(cornerRadiusCropRect.top - casterRect.top),
611 std::abs(cornerRadiusCropRect.left - casterRect.left));
612 if (cropRectOffset > casterCornerRadius) {
613 casterCornerRadius = 0;
614 } else {
615 casterCornerRadius -= cropRectOffset;
616 }
617 shadowLayer.geometry.roundedCornersRadius = casterCornerRadius;
618 }
619
620 return shadowLayer;
621}
622
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800623Hwc2::IComposerClient::Composition Layer::getCompositionType(
624 const sp<const DisplayDevice>& display) const {
625 const auto outputLayer = findOutputLayerForDisplay(display);
626 LOG_FATAL_IF(!outputLayer);
627 return outputLayer->getState().hwc ? (*outputLayer->getState().hwc).hwcCompositionType
628 : Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800629}
630
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800631bool Layer::getClearClientTarget(const sp<const DisplayDevice>& display) const {
632 const auto outputLayer = findOutputLayerForDisplay(display);
633 LOG_FATAL_IF(!outputLayer);
634 return outputLayer->getState().clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800635}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800636
Dan Stozacac35382016-01-27 12:21:06 -0800637bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
638 if (point->getFrameNumber() <= mCurrentFrameNumber) {
639 // Don't bother with a SyncPoint, since we've already latched the
640 // relevant frame
641 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700642 }
Robert Carr2e102c92018-10-23 12:11:15 -0700643 if (isRemovedFromCurrentState()) {
644 return false;
645 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700646
Dan Stozacac35382016-01-27 12:21:06 -0800647 Mutex::Autolock lock(mLocalSyncPointMutex);
648 mLocalSyncPoints.push_back(point);
649 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700650}
651
Mathias Agopian13127d82013-03-05 17:47:11 -0800652// ----------------------------------------------------------------------------
653// local state
654// ----------------------------------------------------------------------------
655
David Sodman41fdfc92017-11-06 16:09:56 -0800656bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800657 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700658 return (s.flags & layer_state_t::eLayerSecure);
659}
660
Mathias Agopian13127d82013-03-05 17:47:11 -0800661// ----------------------------------------------------------------------------
662// transaction
663// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800664
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800665void Layer::pushPendingState() {
666 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700667 return;
668 }
Alec Mourie60041e2019-06-14 18:59:51 -0700669 ATRACE_CALL();
Dan Stoza7dde5992015-05-22 09:51:44 -0700670
Dan Stoza7dde5992015-05-22 09:51:44 -0700671 // If this transaction is waiting on the receipt of a frame, generate a sync
672 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700673 // We don't allow installing sync points after we are removed from the current state
674 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800675 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
676 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800677 if (barrierLayer == nullptr) {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700678 ALOGE("[%s] Unable to promote barrier Layer.", getDebugName());
Dan Stoza7dde5992015-05-22 09:51:44 -0700679 // If we can't promote the layer we are intended to wait on,
680 // then it is expired or otherwise invalid. Allow this transaction
681 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800682 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800683 } else {
chaviw43cb3cb2019-05-31 15:23:41 -0700684 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy, this);
Robert Carr0d480722017-01-10 16:42:54 -0800685 if (barrierLayer->addSyncPoint(syncPoint)) {
Alec Mourie60041e2019-06-14 18:59:51 -0700686 std::stringstream ss;
687 ss << "Adding sync point " << mCurrentState.frameNumber_legacy;
688 ATRACE_NAME(ss.str().c_str());
Dan Stozacac35382016-01-27 12:21:06 -0800689 mRemoteSyncPoints.push_back(std::move(syncPoint));
690 } else {
691 // We already missed the frame we're supposed to synchronize
692 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800693 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800694 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700695 }
696
Dan Stoza7dde5992015-05-22 09:51:44 -0700697 // Wake us up to check if the frame has been received
698 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700699 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700700 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800701 mPendingStates.push_back(mCurrentState);
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700702 ATRACE_INT(mTransactionName.c_str(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700703}
704
Pablo Ceballos05289c22016-04-14 15:49:55 -0700705void Layer::popPendingState(State* stateToCommit) {
Alec Mourie60041e2019-06-14 18:59:51 -0700706 ATRACE_CALL();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800707 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700708
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800709 mPendingStates.removeAt(0);
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700710 ATRACE_INT(mTransactionName.c_str(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700711}
712
Pablo Ceballos05289c22016-04-14 15:49:55 -0700713bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700714 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800715 while (!mPendingStates.empty()) {
716 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700717 if (mRemoteSyncPoints.empty()) {
718 // If we don't have a sync point for this, apply it anyway. It
719 // will be visually wrong, but it should keep us from getting
720 // into too much trouble.
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700721 ALOGE("[%s] No local sync point found", getDebugName());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700722 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700723 stateUpdateAvailable = true;
724 continue;
725 }
726
Marissa Wallf58c14b2018-07-24 10:50:43 -0700727 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800728 mPendingStates[0].frameNumber_legacy) {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700729 ALOGE("[%s] Unexpected sync point frame number found", getDebugName());
Dan Stozacac35382016-01-27 12:21:06 -0800730
731 // Signal our end of the sync point and then dispose of it
732 mRemoteSyncPoints.front()->setTransactionApplied();
733 mRemoteSyncPoints.pop_front();
734 continue;
735 }
736
Dan Stoza7dde5992015-05-22 09:51:44 -0700737 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
Alec Mourie60041e2019-06-14 18:59:51 -0700738 ATRACE_NAME("frameIsAvailable");
Dan Stoza7dde5992015-05-22 09:51:44 -0700739 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700740 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700741 stateUpdateAvailable = true;
742
743 // Signal our end of the sync point and then dispose of it
744 mRemoteSyncPoints.front()->setTransactionApplied();
745 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800746 } else {
Alec Mourie60041e2019-06-14 18:59:51 -0700747 ATRACE_NAME("!frameIsAvailable");
Dan Stoza792e5292016-02-11 11:43:58 -0800748 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700749 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700750 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700751 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700752 stateUpdateAvailable = true;
753 }
754 }
755
756 // If we still have pending updates, wake SurfaceFlinger back up and point
757 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800758 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700759 setTransactionFlags(eTransactionNeeded);
760 mFlinger->setTransactionFlags(eTraversalNeeded);
761 }
762
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800763 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700764 return stateUpdateAvailable;
765}
766
Marissa Wall61c58622018-07-18 10:12:20 -0700767uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000768 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800769
Marissa Wall61c58622018-07-18 10:12:20 -0700770 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
771 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700772
David Sodmaneb085e02017-10-05 18:49:04 -0700773 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700774 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000775 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800776 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
777 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
778 " requested={ wh={%4u,%4u} }}\n"
779 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
780 " requested={ wh={%4u,%4u} }}\n",
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700781 this, getName().c_str(), getBufferTransform(), getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700782 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
783 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
784 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
785 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
786 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700787 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
788 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
789 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800790 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700791
Robert Carre392b552017-09-19 12:16:05 -0700792 // Don't let Layer::doTransaction update the drawing state
793 // if we have a pending resize, unless we are in fixed-size mode.
794 // the drawing state will be updated only once we receive a buffer
795 // with the correct size.
796 //
797 // In particular, we want to make sure the clip (which is part
798 // of the geometry state) is latched together with the size but is
799 // latched immediately when no resizing is involved.
800 //
801 // If a sideband stream is attached, however, we want to skip this
802 // optimization so that transactions aren't missed when a buffer
803 // never arrives
804 //
805 // In the case that we don't have a buffer we ignore other factors
806 // and avoid entering the resizePending state. At a high level the
807 // resizePending state is to avoid applying the state of the new buffer
808 // to the old buffer. However in the state where we don't have an old buffer
809 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700810 const bool resizePending =
811 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
812 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
chaviwd62d3062019-09-04 14:48:02 -0700813 (getBuffer() != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700814 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800815 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700816 flags |= eDontUpdateGeometryState;
817 }
818 }
819
Robert Carr7bf247e2017-05-18 14:02:49 -0700820 // Here we apply various requested geometry states, depending on our
821 // latching configuration. See Layer.h for a detailed discussion of
822 // how geometry latching is controlled.
823 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000824 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700825
Robert Carr7bf247e2017-05-18 14:02:49 -0700826 // There is an awkward asymmetry in the handling of the crop states in the position
827 // states, as can be seen below. Largely this arises from position and transform
828 // being stored in the same data structure while having different latching rules.
829 // b/38182305
830 //
Marissa Wall61c58622018-07-18 10:12:20 -0700831 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700832 // applyPendingStates in the presence of deferred transactions.
chaviw214c89d2019-09-04 16:03:53 -0700833 editCurrentState.active_legacy = editCurrentState.requested_legacy;
834 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Mathias Agopian13127d82013-03-05 17:47:11 -0800835 }
836
Marissa Wall61c58622018-07-18 10:12:20 -0700837 return flags;
838}
839
840uint32_t Layer::doTransaction(uint32_t flags) {
841 ATRACE_CALL();
842
chaviw5aedec92018-10-22 10:40:38 -0700843 if (mLayerDetached) {
Robert Carr321e83c2019-08-19 15:49:30 -0700844 // Ensure BLAST buffer callbacks are processed.
845 // detachChildren and mLayerDetached were implemented to avoid geometry updates
846 // to layers in the cases of animation. For BufferQueue layers buffers are still
847 // consumed as normal. This is useful as otherwise the client could get hung
848 // inevitably waiting on a buffer to return. We recreate this semantic for BufferQueue
849 // even though it is a little consistent. detachChildren is shortly slated for removal
850 // by the hierarchy mirroring work so we don't need to worry about it too much.
851 mDrawingState.callbackHandles = mCurrentState.callbackHandles;
852 mCurrentState.callbackHandles = {};
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800853 return flags;
854 }
855
856 if (mChildrenChanged) {
857 flags |= eVisibleRegion;
858 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -0700859 }
860
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800861 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +0000862 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -0700863 if (!applyPendingStates(&c)) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800864 return flags;
Marissa Wall61c58622018-07-18 10:12:20 -0700865 }
866
867 flags = doTransactionResize(flags, &c);
868
Alec Mourib416efd2018-09-06 21:01:59 +0000869 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700870
871 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800872 // invalidate and recompute the visible regions if needed
873 flags |= Layer::eVisibleRegion;
874 }
875
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700876 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800877 // invalidate and recompute the visible regions if needed
878 flags |= eVisibleRegion;
879 this->contentDirty = true;
880
881 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -0700882 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -0700883 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -0800884 }
885
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800886 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -0700887 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800888 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -0700889 }
890
Mathias Agopian13127d82013-03-05 17:47:11 -0800891 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -0700892 commitTransaction(c);
Vishnu Nair8406fd72019-07-30 11:29:31 -0700893 mPendingStatesSnapshot = mPendingStates;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800894 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -0800895 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800896}
897
Pablo Ceballos05289c22016-04-14 15:49:55 -0700898void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800899 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700900}
901
Mathias Agopian13127d82013-03-05 17:47:11 -0800902uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800903 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800904}
905
906uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800907 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -0800908}
909
chaviw214c89d2019-09-04 16:03:53 -0700910bool Layer::setPosition(float x, float y) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800911 if (mCurrentState.requested_legacy.transform.tx() == x &&
912 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -0800913 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800914 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -0700915
916 // We update the requested and active position simultaneously because
917 // we want to apply the position portion of the transform matrix immediately,
918 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800919 mCurrentState.requested_legacy.transform.set(x, y);
chaviw214c89d2019-09-04 16:03:53 -0700920 // Here we directly update the active state
921 // unlike other setters, because we store it within
922 // the transform, but use different latching rules.
923 // b/38182305
924 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr69663fb2016-03-27 19:59:19 -0700925
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800926 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800927 setTransactionFlags(eTransactionNeeded);
928 return true;
929}
Robert Carr82364e32016-05-15 11:27:47 -0700930
Robert Carr1f0a16a2016-10-24 16:27:39 -0700931bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
932 ssize_t idx = mCurrentChildren.indexOf(childLayer);
933 if (idx < 0) {
934 return false;
935 }
936 if (childLayer->setLayer(z)) {
937 mCurrentChildren.removeAt(idx);
938 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800939 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700940 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800941 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700942}
943
Robert Carr503c7042017-09-27 15:06:08 -0700944bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
945 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
946 ssize_t idx = mCurrentChildren.indexOf(childLayer);
947 if (idx < 0) {
948 return false;
949 }
950 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
951 mCurrentChildren.removeAt(idx);
952 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800953 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700954 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800955 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700956}
957
Robert Carrae060832016-11-28 10:51:00 -0800958bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800959 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
960 mCurrentState.sequence++;
961 mCurrentState.z = z;
962 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700963
964 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800965 if (mCurrentState.zOrderRelativeOf != nullptr) {
966 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700967 if (strongRelative != nullptr) {
968 strongRelative->removeZOrderRelative(this);
969 }
chaviw606e5cf2019-03-01 10:12:10 -0800970 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -0700971 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800972 setTransactionFlags(eTransactionNeeded);
973 return true;
974}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700975
Robert Carrdb66e622017-04-10 16:55:57 -0700976void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800977 mCurrentState.zOrderRelatives.remove(relative);
978 mCurrentState.sequence++;
979 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700980 setTransactionFlags(eTransactionNeeded);
981}
982
983void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800984 mCurrentState.zOrderRelatives.add(relative);
985 mCurrentState.modified = true;
986 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -0700987 setTransactionFlags(eTransactionNeeded);
988}
989
chaviw606e5cf2019-03-01 10:12:10 -0800990void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
991 mCurrentState.zOrderRelativeOf = relativeOf;
992 mCurrentState.sequence++;
993 mCurrentState.modified = true;
chaviwbdb8b802019-10-14 09:17:12 -0700994 mCurrentState.isRelativeOf = relativeOf != nullptr;
995
chaviw606e5cf2019-03-01 10:12:10 -0800996 setTransactionFlags(eTransactionNeeded);
997}
998
Robert Carr503d2bd2017-12-04 15:49:47 -0800999bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001000 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1001 if (handle == nullptr) {
1002 return false;
1003 }
1004 sp<Layer> relative = handle->owner.promote();
1005 if (relative == nullptr) {
1006 return false;
1007 }
1008
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001009 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1010 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001011 return false;
1012 }
1013
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001014 mCurrentState.sequence++;
1015 mCurrentState.modified = true;
1016 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001017
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001018 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -07001019 if (oldZOrderRelativeOf != nullptr) {
1020 oldZOrderRelativeOf->removeZOrderRelative(this);
1021 }
chaviw606e5cf2019-03-01 10:12:10 -08001022 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -07001023 relative->addZOrderRelative(this);
1024
1025 setTransactionFlags(eTransactionNeeded);
1026
1027 return true;
1028}
1029
Mathias Agopian13127d82013-03-05 17:47:11 -08001030bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001031 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001032 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001033 mCurrentState.requested_legacy.w = w;
1034 mCurrentState.requested_legacy.h = h;
1035 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001036 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001037
1038 // record the new size, from this point on, when the client request
1039 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001040 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001041 return true;
1042}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001043bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001044 if (mCurrentState.color.a == alpha) return false;
1045 mCurrentState.sequence++;
1046 mCurrentState.color.a = alpha;
1047 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001048 setTransactionFlags(eTransactionNeeded);
1049 return true;
1050}
chaviw13fdc492017-06-27 12:40:18 -07001051
Valerie Haudd0b7572019-01-29 14:59:27 -08001052bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1053 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001054 return false;
Valerie Hauaa194562019-02-05 16:21:38 -08001055 }
1056 mCurrentState.sequence++;
1057 mCurrentState.modified = true;
1058 setTransactionFlags(eTransactionNeeded);
1059
1060 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001061 // create background color layer if one does not yet exist
1062 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001063 std::string name = mName + "BackgroundColorLayer";
Lloyd Pique958be492019-03-28 15:34:59 -07001064 mCurrentState.bgColorLayer = mFlinger->getFactory().createColorLayer(
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001065 LayerCreationArgs(mFlinger.get(), nullptr, std::move(name), 0, 0, flags,
1066 LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -07001067
Valerie Haudd0b7572019-01-29 14:59:27 -08001068 // add to child list
1069 addChild(mCurrentState.bgColorLayer);
1070 mFlinger->mLayersAdded = true;
1071 // set up SF to handle added color layer
1072 if (isRemovedFromCurrentState()) {
1073 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1074 }
1075 mFlinger->setTransactionFlags(eTransactionNeeded);
1076 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1077 mCurrentState.bgColorLayer->reparent(nullptr);
1078 mCurrentState.bgColorLayer = nullptr;
1079 return true;
1080 }
1081
1082 mCurrentState.bgColorLayer->setColor(color);
1083 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1084 mCurrentState.bgColorLayer->setAlpha(alpha);
1085 mCurrentState.bgColorLayer->setDataspace(dataspace);
1086
chaviw13fdc492017-06-27 12:40:18 -07001087 return true;
1088}
1089
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001090bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001091 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001092
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001093 mCurrentState.sequence++;
1094 mCurrentState.cornerRadius = cornerRadius;
1095 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001096 setTransactionFlags(eTransactionNeeded);
1097 return true;
1098}
1099
Robert Carrd4ae7f32018-06-07 16:10:57 -07001100bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1101 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001102 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001103 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1104
1105 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1106 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1107 return false;
1108 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001109 mCurrentState.sequence++;
1110 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1111 matrix.dsdy);
1112 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001113 setTransactionFlags(eTransactionNeeded);
1114 return true;
1115}
Marissa Wall61c58622018-07-18 10:12:20 -07001116
Mathias Agopian13127d82013-03-05 17:47:11 -08001117bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001118 mCurrentState.requestedTransparentRegion_legacy = transparent;
1119 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001120 setTransactionFlags(eTransactionNeeded);
1121 return true;
1122}
1123bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001124 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1125 if (mCurrentState.flags == newFlags) return false;
1126 mCurrentState.sequence++;
1127 mCurrentState.flags = newFlags;
1128 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001129 setTransactionFlags(eTransactionNeeded);
1130 return true;
1131}
Robert Carr99e27f02016-06-16 15:18:02 -07001132
chaviw214c89d2019-09-04 16:03:53 -07001133bool Layer::setCrop_legacy(const Rect& crop) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001134 if (mCurrentState.requestedCrop_legacy == crop) return false;
1135 mCurrentState.sequence++;
1136 mCurrentState.requestedCrop_legacy = crop;
chaviw214c89d2019-09-04 16:03:53 -07001137 mCurrentState.crop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001138
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001139 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001140 setTransactionFlags(eTransactionNeeded);
1141 return true;
1142}
Robert Carr8d5227b2017-03-16 15:41:03 -07001143
Robert Carrc3574f72016-03-24 12:19:32 -07001144bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001145 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001146 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001147 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001148 return true;
1149}
1150
Evan Roskyef876c92019-01-25 17:46:06 -08001151bool Layer::setMetadata(const LayerMetadata& data) {
1152 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001153 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001154 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001155 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001156 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001157}
1158
Mathias Agopian13127d82013-03-05 17:47:11 -08001159bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001160 if (mCurrentState.layerStack == layerStack) return false;
1161 mCurrentState.sequence++;
1162 mCurrentState.layerStack = layerStack;
1163 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001164 setTransactionFlags(eTransactionNeeded);
1165 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001166}
1167
Peiyong Linc502cb72019-03-01 15:00:23 -08001168bool Layer::setColorSpaceAgnostic(const bool agnostic) {
1169 if (mCurrentState.colorSpaceAgnostic == agnostic) {
1170 return false;
1171 }
1172 mCurrentState.sequence++;
1173 mCurrentState.colorSpaceAgnostic = agnostic;
1174 mCurrentState.modified = true;
1175 setTransactionFlags(eTransactionNeeded);
1176 return true;
1177}
1178
Robert Carr1f0a16a2016-10-24 16:27:39 -07001179uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001180 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001181 if (p == nullptr) {
1182 return getDrawingState().layerStack;
1183 }
1184 return p->getLayerStack();
1185}
1186
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001187bool Layer::setShadowRadius(float shadowRadius) {
1188 if (mCurrentState.shadowRadius == shadowRadius) {
1189 return false;
1190 }
1191
1192 mCurrentState.sequence++;
1193 mCurrentState.shadowRadius = shadowRadius;
1194 mCurrentState.modified = true;
1195 setTransactionFlags(eTransactionNeeded);
1196 return true;
1197}
1198
Marissa Wallf58c14b2018-07-24 10:50:43 -07001199void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Alec Mourie60041e2019-06-14 18:59:51 -07001200 ATRACE_CALL();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001201 mCurrentState.barrierLayer_legacy = barrierLayer;
1202 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001203 // We don't set eTransactionNeeded, because just receiving a deferral
1204 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001205 mCurrentState.modified = true;
1206 pushPendingState();
1207 mCurrentState.barrierLayer_legacy = nullptr;
1208 mCurrentState.frameNumber_legacy = 0;
1209 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001210}
1211
Marissa Wallf58c14b2018-07-24 10:50:43 -07001212void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001213 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001214 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001215}
1216
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001217// ----------------------------------------------------------------------------
1218// pageflip handling...
1219// ----------------------------------------------------------------------------
1220
Robert Carr1f0a16a2016-10-24 16:27:39 -07001221bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001222 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001223 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001224 if (parent != nullptr && parent->isHiddenByPolicy()) {
1225 return true;
1226 }
Robert Carr1c5481e2019-07-01 14:42:27 -07001227 if (usingRelativeZ(LayerVector::StateSet::Drawing)) {
1228 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
1229 if (zOrderRelativeOf != nullptr) {
1230 if (zOrderRelativeOf->isHiddenByPolicy()) {
1231 return true;
1232 }
1233 }
1234 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001235 return s.flags & layer_state_t::eLayerHidden;
1236}
1237
David Sodman41fdfc92017-11-06 16:09:56 -08001238uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001239 // TODO: should we do something special if mSecure is set?
1240 if (mProtectedByApp) {
1241 // need a hardware-protected path to external video sink
1242 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001243 }
Riley Andrews03414a12014-07-01 14:22:59 -07001244 if (mPotentialCursor) {
1245 usage |= GraphicBuffer::USAGE_CURSOR;
1246 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001247 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001248 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001249}
1250
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001251void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001252 uint32_t orientation = 0;
Vishnu Nair5eb3f062019-04-08 08:21:03 -07001253 // Disable setting transform hint if the debug flag is set.
1254 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001255 // The transform hint is used to improve performance, but we can
1256 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001257 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001258 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001259 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001260 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001261 orientation = 0;
1262 }
1263 }
David Sodmaneb085e02017-10-05 18:49:04 -07001264 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001265}
1266
Mathias Agopian13127d82013-03-05 17:47:11 -08001267// ----------------------------------------------------------------------------
1268// debugging
1269// ----------------------------------------------------------------------------
1270
Marissa Wall61c58622018-07-18 10:12:20 -07001271// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001272LayerDebugInfo Layer::getLayerDebugInfo() const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001273 using namespace std::string_literals;
1274
Kalle Raitaa099a242017-01-11 11:17:29 -08001275 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001276 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001277 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001278 sp<Layer> parent = getParent();
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001279 info.mParentName = parent ? parent->getName() : "none"s;
chaviw8a01fa42019-08-19 12:39:31 -07001280 info.mType = getType();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001281 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Lloyd Piquea2468662019-03-07 21:31:06 -08001282
1283 info.mVisibleRegion = debugGetVisibleRegionOnDefaultDisplay();
Kalle Raitaa099a242017-01-11 11:17:29 -08001284 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001285 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001286 info.mX = ds.active_legacy.transform.tx();
1287 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001288 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001289 info.mWidth = ds.active_legacy.w;
1290 info.mHeight = ds.active_legacy.h;
1291 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001292 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001293 info.mFlags = ds.flags;
1294 info.mPixelFormat = getPixelFormat();
chaviw4244e032019-09-04 11:27:49 -07001295 info.mDataSpace = static_cast<android_dataspace>(getDataSpace());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001296 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1297 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1298 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1299 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001300 {
chaviwd62d3062019-09-04 14:48:02 -07001301 sp<const GraphicBuffer> buffer = getBuffer();
David Sodman5b4cffc2017-11-23 13:20:29 -08001302 if (buffer != 0) {
1303 info.mActiveBufferWidth = buffer->getWidth();
1304 info.mActiveBufferHeight = buffer->getHeight();
1305 info.mActiveBufferStride = buffer->getStride();
1306 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001307 } else {
1308 info.mActiveBufferWidth = 0;
1309 info.mActiveBufferHeight = 0;
1310 info.mActiveBufferStride = 0;
1311 info.mActiveBufferFormat = 0;
1312 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001313 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001314 info.mNumQueuedFrames = getQueuedFrameCount();
1315 info.mRefreshPending = isBufferLatched();
1316 info.mIsOpaque = isOpaque(ds);
1317 info.mContentDirty = contentDirty;
1318 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001319}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001320
Yiwei Zhang5434a782018-12-05 18:06:32 -08001321void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001322 result.append("-------------------------------");
1323 result.append("-------------------------------");
1324 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001325 result.append(" Layer name\n");
1326 result.append(" Z | ");
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001327 result.append(" Window Type | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001328 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001329 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001330 result.append(" Disp Frame (LTRB) | ");
1331 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001332 result.append("-------------------------------");
1333 result.append("-------------------------------");
1334 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001335}
1336
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001337void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1338 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1339 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001340 return;
1341 }
1342
Yiwei Zhang5434a782018-12-05 18:06:32 -08001343 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001344 if (mName.length() > 77) {
1345 std::string shortened;
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001346 shortened.append(mName, 0, 36);
Dan Stozae22aec72016-08-01 13:20:59 -07001347 shortened.append("[...]");
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001348 shortened.append(mName, mName.length() - 36);
1349 name = std::move(shortened);
Dan Stozae22aec72016-08-01 13:20:59 -07001350 } else {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001351 name = mName;
Dan Stozae22aec72016-08-01 13:20:59 -07001352 }
1353
Yiwei Zhang5434a782018-12-05 18:06:32 -08001354 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001355
Alec Mourib416efd2018-09-06 21:01:59 +00001356 const State& layerState(getDrawingState());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001357 const auto& compositionState = outputLayer->getState();
1358
Chia-I Wu1e043612018-03-01 09:45:09 -08001359 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001360 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001361 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001362 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001363 }
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001364 StringAppendF(&result, " %10d | ", mWindowType);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001365 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
1366 StringAppendF(&result, "%10s | ",
1367 toString(getCompositionLayer() ? compositionState.bufferTransform
1368 : static_cast<Hwc2::Transform>(0))
1369 .c_str());
1370 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001371 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001372 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001373 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1374 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001375
Yichi Chen6ca35192018-05-29 12:20:43 +08001376 result.append("- - - - - - - - - - - - - - - -");
1377 result.append("- - - - - - - - - - - - - - - -");
1378 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001379}
Dan Stozae22aec72016-08-01 13:20:59 -07001380
Yiwei Zhang5434a782018-12-05 18:06:32 -08001381void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001382 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001383}
1384
Svetoslavd85084b2014-03-20 10:28:31 -07001385void Layer::clearFrameStats() {
1386 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001387}
1388
Jamie Gennis6547ff42013-07-16 20:12:42 -07001389void Layer::logFrameStats() {
1390 mFrameTracker.logAndResetStats(mName);
1391}
1392
Svetoslavd85084b2014-03-20 10:28:31 -07001393void Layer::getFrameStats(FrameStats* outStats) const {
1394 mFrameTracker.getStats(outStats);
1395}
1396
Yiwei Zhang5434a782018-12-05 18:06:32 -08001397void Layer::dumpFrameEvents(std::string& result) {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001398 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().c_str(), getType(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001399 Mutex::Autolock lock(mFrameEventHistoryMutex);
1400 mFrameEventHistory.checkFencesForCompletion();
1401 mFrameEventHistory.dump(result);
1402}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001403
Vishnu Nair0f085c62019-08-30 08:49:12 -07001404void Layer::dumpCallingUidPid(std::string& result) const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001405 StringAppendF(&result, "Layer %s (%s) pid:%d uid:%d\n", getName().c_str(), getType(),
Vishnu Nair0f085c62019-08-30 08:49:12 -07001406 mCallingPid, mCallingUid);
1407}
1408
Brian Anderson5ea5e592016-12-01 16:54:33 -08001409void Layer::onDisconnect() {
1410 Mutex::Autolock lock(mFrameEventHistoryMutex);
1411 mFrameEventHistory.onDisconnect();
Yiwei Zhang1a88c402019-11-18 10:43:58 -08001412 const int32_t layerId = getSequence();
1413 mFlinger->mTimeStats->onDestroy(layerId);
1414 mFlinger->mFrameTracer->onDestroy(layerId);
Brian Anderson5ea5e592016-12-01 16:54:33 -08001415}
1416
Brian Anderson3890c392016-07-25 12:48:08 -07001417void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001418 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001419 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001420 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1421 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhang487340f2019-11-18 17:42:12 -08001422 mFlinger->mTimeStats->setAcquireFence(getSequence(), newTimestamps->frameNumber,
1423 newTimestamps->acquireFence);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001424 }
1425
Brian Andersond6927fb2016-07-23 23:37:30 -07001426 Mutex::Autolock lock(mFrameEventHistoryMutex);
1427 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001428 // If there are any unsignaled fences in the aquire timeline at this
1429 // point, the previously queued frame hasn't been latched yet. Go ahead
1430 // and try to get the signal time here so the syscall is taken out of
1431 // the main thread's critical path.
1432 mAcquireTimeline.updateSignalTimes();
1433 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001434 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001435 mFrameEventHistory.addQueue(*newTimestamps);
1436 }
1437
Brian Anderson3890c392016-07-25 12:48:08 -07001438 if (outDelta) {
1439 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001440 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001441}
Dan Stozae77c7662016-05-13 11:37:28 -07001442
Chia-I Wu98f1c102017-05-30 14:54:08 -07001443size_t Layer::getChildrenCount() const {
1444 size_t count = 0;
1445 for (const sp<Layer>& child : mCurrentChildren) {
1446 count += 1 + child->getChildrenCount();
1447 }
1448 return count;
1449}
1450
Robert Carr1f0a16a2016-10-24 16:27:39 -07001451void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001452 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001453 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001454
Robert Carr1f0a16a2016-10-24 16:27:39 -07001455 mCurrentChildren.add(layer);
1456 layer->setParent(this);
1457}
1458
1459ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001460 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001461 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001462
Robert Carr1323c952019-01-28 18:13:27 -08001463 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001464 return mCurrentChildren.remove(layer);
1465}
1466
Robert Carr1db73f62016-12-21 12:58:51 -08001467bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1468 sp<Handle> handle = nullptr;
1469 sp<Layer> newParent = nullptr;
1470 if (newParentHandle == nullptr) {
1471 return false;
1472 }
1473 handle = static_cast<Handle*>(newParentHandle.get());
1474 newParent = handle->owner.promote();
1475 if (newParent == nullptr) {
1476 ALOGE("Unable to promote Layer handle");
1477 return false;
1478 }
1479
chaviw5aedec92018-10-22 10:40:38 -07001480 if (attachChildren()) {
1481 setTransactionFlags(eTransactionNeeded);
1482 }
Robert Carr1db73f62016-12-21 12:58:51 -08001483 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001484 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001485 }
1486 mCurrentChildren.clear();
1487
1488 return true;
1489}
1490
Robert Carr15eae092018-03-23 13:43:53 -07001491void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001492 for (const sp<Layer>& child : mDrawingChildren) {
1493 child->mDrawingParent = newParent;
Vishnu Nairc652ff82019-03-15 12:48:54 -07001494 child->computeBounds(newParent->mBounds,
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001495 newParent->getTransformWithScale(newParent->getBufferScaleTransform()),
1496 newParent->mEffectiveShadowRadius);
Robert Carr578038f2018-03-09 12:25:24 -08001497 }
1498}
1499
chaviwf1961f72017-09-18 16:41:07 -07001500bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001501 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001502
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001503 // While layers are detached, we allow most operations
1504 // and simply halt performing the actual transaction. However
1505 // for reparent != null we would enter the mRemovedFromCurrentState
1506 // state, regardless of whether doTransaction was called, and
1507 // so we need to prevent the update here.
1508 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001509 return false;
1510 }
1511
Robert Carr54cf5b12019-01-25 14:02:28 -08001512 sp<Layer> newParent;
1513 if (newParentHandle != nullptr) {
1514 auto handle = static_cast<Handle*>(newParentHandle.get());
1515 newParent = handle->owner.promote();
1516 if (newParent == nullptr) {
1517 ALOGE("Unable to promote Layer handle");
1518 return false;
1519 }
1520 if (newParent == this) {
1521 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1522 return false;
1523 }
1524 }
1525
chaviwf1961f72017-09-18 16:41:07 -07001526 sp<Layer> parent = getParent();
1527 if (parent != nullptr) {
1528 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001529 }
1530
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001531 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001532 newParent->addChild(this);
1533 if (!newParent->isRemovedFromCurrentState()) {
1534 addToCurrentState();
1535 } else {
1536 onRemovedFromCurrentState();
1537 }
1538
1539 if (mLayerDetached) {
1540 mLayerDetached = false;
1541 callSetTransactionFlags = true;
1542 }
1543 } else {
1544 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001545 }
1546
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001547 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001548 setTransactionFlags(eTransactionNeeded);
1549 }
chaviw06178942017-07-27 10:25:59 -07001550 return true;
1551}
1552
Robert Carr9524cb32017-02-13 11:32:32 -08001553bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001554 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001555 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001556 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001557 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001558 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001559 child->detachChildren();
chaviw43cb3cb2019-05-31 15:23:41 -07001560 child->removeRemoteSyncPoints();
Robert Carr9524cb32017-02-13 11:32:32 -08001561 }
Robert Carr7f619b22017-11-06 12:56:35 -08001562 }
Robert Carr9524cb32017-02-13 11:32:32 -08001563
1564 return true;
1565}
1566
chaviw5aedec92018-10-22 10:40:38 -07001567bool Layer::attachChildren() {
1568 bool changed = false;
1569 for (const sp<Layer>& child : mCurrentChildren) {
1570 sp<Client> parentClient = mClientRef.promote();
1571 sp<Client> client(child->mClientRef.promote());
1572 if (client != nullptr && parentClient != client) {
1573 if (child->mLayerDetached) {
1574 child->mLayerDetached = false;
1575 changed = true;
1576 }
1577 changed |= child->attachChildren();
1578 }
1579 }
1580
1581 return changed;
1582}
1583
Peiyong Lind3788632018-09-18 16:01:31 -07001584bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001585 static const mat4 identityMatrix = mat4();
1586
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001587 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001588 return false;
1589 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001590 ++mCurrentState.sequence;
1591 mCurrentState.colorTransform = matrix;
1592 mCurrentState.hasColorTransform = matrix != identityMatrix;
1593 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001594 setTransactionFlags(eTransactionNeeded);
1595 return true;
1596}
1597
chaviwf66724d2018-11-28 16:35:21 -08001598mat4 Layer::getColorTransform() const {
1599 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1600 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1601 colorTransform = parent->getColorTransform() * colorTransform;
1602 }
1603 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001604}
1605
1606bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001607 bool hasColorTransform = getDrawingState().hasColorTransform;
1608 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1609 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1610 }
1611 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001612}
1613
Chia-I Wu11481472018-05-04 10:43:19 -07001614bool Layer::isLegacyDataSpace() const {
1615 // return true when no higher bits are set
chaviw4244e032019-09-04 11:27:49 -07001616 return !(getDataSpace() &
1617 (ui::Dataspace::STANDARD_MASK | ui::Dataspace::TRANSFER_MASK |
1618 ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001619}
1620
Robert Carr1f0a16a2016-10-24 16:27:39 -07001621void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001622 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001623}
1624
chaviw301b1d82019-11-06 13:15:09 -08001625int32_t Layer::getZ(LayerVector::StateSet stateSet) const {
1626 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1627 const State& state = useDrawing ? mDrawingState : mCurrentState;
1628 return state.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001629}
1630
Robert Carr1c5481e2019-07-01 14:42:27 -07001631bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) const {
Robert Carr29abff82017-12-04 13:51:20 -08001632 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001633 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviwe5ac40f2019-09-24 16:36:55 -07001634 return state.isRelativeOf;
Robert Carr29abff82017-12-04 13:51:20 -08001635}
1636
David Sodman41fdfc92017-11-06 16:09:56 -08001637__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001638 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001639 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1640 "makeTraversalList received invalid stateSet");
1641 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1642 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001643 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001644
Robert Carr29abff82017-12-04 13:51:20 -08001645 if (state.zOrderRelatives.size() == 0) {
1646 *outSkipRelativeZUsers = true;
1647 return children;
1648 }
1649
chaviwfd462612018-05-31 16:11:27 -07001650 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001651 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001652 sp<Layer> strongRelative = weakRelative.promote();
1653 if (strongRelative != nullptr) {
1654 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001655 }
1656 }
1657
Dan Stoza412903f2017-04-27 13:42:17 -07001658 for (const sp<Layer>& child : children) {
chaviwe5ac40f2019-09-24 16:36:55 -07001659 if (child->usingRelativeZ(stateSet)) {
Robert Carr503c7042017-09-27 15:06:08 -07001660 continue;
1661 }
Robert Carrdb66e622017-04-10 16:55:57 -07001662 traverse.add(child);
1663 }
1664
1665 return traverse;
1666}
1667
Robert Carr1f0a16a2016-10-24 16:27:39 -07001668/**
Robert Carrdb66e622017-04-10 16:55:57 -07001669 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001670 */
Dan Stoza412903f2017-04-27 13:42:17 -07001671void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001672 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1673 // produce a new list for traversing, including our relatives, and not including our children
1674 // who are relatives of another surface. In the case that there are no relative Z,
1675 // makeTraversalList returns our children directly to avoid significant overhead.
1676 // However in this case we need to take the responsibility for filtering children which
1677 // are relatives of another surface here.
1678 bool skipRelativeZUsers = false;
1679 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001680
Robert Carr1f0a16a2016-10-24 16:27:39 -07001681 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001682 for (; i < list.size(); i++) {
1683 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001684 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1685 continue;
1686 }
1687
chaviw301b1d82019-11-06 13:15:09 -08001688 if (relative->getZ(stateSet) >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001689 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001690 }
Dan Stoza412903f2017-04-27 13:42:17 -07001691 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001692 }
Robert Carr29abff82017-12-04 13:51:20 -08001693
Dan Stoza412903f2017-04-27 13:42:17 -07001694 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001695 for (; i < list.size(); i++) {
1696 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001697
Robert Carr29abff82017-12-04 13:51:20 -08001698 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1699 continue;
1700 }
Dan Stoza412903f2017-04-27 13:42:17 -07001701 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001702 }
1703}
1704
1705/**
Robert Carrdb66e622017-04-10 16:55:57 -07001706 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001707 */
Dan Stoza412903f2017-04-27 13:42:17 -07001708void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1709 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001710 // See traverseInZOrder for documentation.
1711 bool skipRelativeZUsers = false;
1712 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001713
Robert Carr1f0a16a2016-10-24 16:27:39 -07001714 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001715 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001716 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001717
1718 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1719 continue;
1720 }
1721
chaviw301b1d82019-11-06 13:15:09 -08001722 if (relative->getZ(stateSet) < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001723 break;
1724 }
Dan Stoza412903f2017-04-27 13:42:17 -07001725 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001726 }
Dan Stoza412903f2017-04-27 13:42:17 -07001727 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001728 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001729 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001730
1731 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1732 continue;
1733 }
1734
Dan Stoza412903f2017-04-27 13:42:17 -07001735 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001736 }
1737}
1738
chaviw4b129c22018-04-09 16:19:43 -07001739LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1740 const std::vector<Layer*>& layersInTree) {
1741 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1742 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001743 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1744 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001745 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001746
chaviwfd462612018-05-31 16:11:27 -07001747 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001748 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1749 sp<Layer> strongRelative = weakRelative.promote();
1750 // Only add relative layers that are also descendents of the top most parent of the tree.
1751 // If a relative layer is not a descendent, then it should be ignored.
1752 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1753 traverse.add(strongRelative);
1754 }
1755 }
1756
1757 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001758 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001759 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1760 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1761 // the child here since it won't be added later as a relative.
1762 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1763 childState.zOrderRelativeOf.promote().get())) {
1764 continue;
1765 }
1766 traverse.add(child);
1767 }
1768
1769 return traverse;
1770}
1771
1772void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1773 LayerVector::StateSet stateSet,
1774 const LayerVector::Visitor& visitor) {
1775 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001776
1777 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001778 for (; i < list.size(); i++) {
1779 const auto& relative = list[i];
chaviw301b1d82019-11-06 13:15:09 -08001780 if (relative->getZ(stateSet) >= 0) {
chaviwa76b2712017-09-20 12:02:26 -07001781 break;
1782 }
chaviw4b129c22018-04-09 16:19:43 -07001783 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001784 }
chaviw4b129c22018-04-09 16:19:43 -07001785
chaviwa76b2712017-09-20 12:02:26 -07001786 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001787 for (; i < list.size(); i++) {
1788 const auto& relative = list[i];
1789 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001790 }
1791}
1792
chaviw4b129c22018-04-09 16:19:43 -07001793std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1794 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1795 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1796
1797 std::vector<Layer*> layersInTree = {this};
1798 for (size_t i = 0; i < children.size(); i++) {
1799 const auto& child = children[i];
1800 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1801 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1802 }
1803
1804 return layersInTree;
1805}
1806
1807void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1808 const LayerVector::Visitor& visitor) {
1809 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1810 std::sort(layersInTree.begin(), layersInTree.end());
1811 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1812}
1813
Peiyong Linefefaac2018-08-17 12:27:51 -07001814ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001815 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001816}
1817
chaviw13fdc492017-06-27 12:40:18 -07001818half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001819 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001820
chaviw13fdc492017-06-27 12:40:18 -07001821 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1822 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001823}
Robert Carr6452f122017-03-21 10:41:29 -07001824
chaviw13fdc492017-06-27 12:40:18 -07001825half4 Layer::getColor() const {
1826 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001827 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001828}
Robert Carr6452f122017-03-21 10:41:29 -07001829
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001830Layer::RoundedCornerState Layer::getRoundedCornerState() const {
1831 const auto& p = mDrawingParent.promote();
1832 if (p != nullptr) {
Peiyong Lin27016a92019-03-29 17:36:08 +00001833 RoundedCornerState parentState = p->getRoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001834 if (parentState.radius > 0) {
1835 ui::Transform t = getActiveTransform(getDrawingState());
1836 t = t.inverse();
1837 parentState.cropRect = t.transform(parentState.cropRect);
1838 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1839 // but a transform matrix can define horizontal and vertical scales.
1840 // Let's take the average between both of them and pass into the shader, practically we
1841 // never do this type of transformation on windows anyway.
1842 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
1843 return parentState;
1844 }
1845 }
1846 const float radius = getDrawingState().cornerRadius;
Peiyong Linb9ff23e2019-04-08 11:04:59 -07001847 return radius > 0 && getCrop(getDrawingState()).isValid()
1848 ? RoundedCornerState(getCrop(getDrawingState()).toFloatRect(), radius)
1849 : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001850}
1851
Vishnu Nair08f6eae2019-11-26 14:01:39 -08001852renderengine::ShadowSettings Layer::getShadowSettings(const Rect& viewport) const {
1853 renderengine::ShadowSettings state = mFlinger->mDrawingState.globalShadowSettings;
1854
1855 // Shift the spot light x-position to the middle of the display and then
1856 // offset it by casting layer's screen pos.
1857 state.lightPos.x = (viewport.width() / 2.f) - mScreenBounds.left;
1858 state.lightPos.y -= mScreenBounds.top;
1859
1860 state.length = mEffectiveShadowRadius;
1861 return state;
1862}
1863
Robert Carr1f0a16a2016-10-24 16:27:39 -07001864void Layer::commitChildList() {
1865 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1866 const auto& child = mCurrentChildren[i];
1867 child->commitChildList();
1868 }
1869 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001870 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001871}
1872
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001873static wp<Layer> extractLayerFromBinder(const wp<IBinder>& weakBinderHandle) {
1874 if (weakBinderHandle == nullptr) {
1875 return nullptr;
1876 }
1877 sp<IBinder> binderHandle = weakBinderHandle.promote();
1878 if (binderHandle == nullptr) {
1879 return nullptr;
1880 }
1881 sp<Layer::Handle> handle = static_cast<Layer::Handle*>(binderHandle.get());
1882 if (handle == nullptr) {
1883 return nullptr;
1884 }
1885 return handle->owner;
1886}
1887
Robert Carr720e5062018-07-30 17:45:14 -07001888void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001889 mCurrentState.inputInfo = info;
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001890 mCurrentState.touchableRegionCrop = extractLayerFromBinder(info.touchableRegionCropHandle);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001891 mCurrentState.modified = true;
1892 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07001893 setTransactionFlags(eTransactionNeeded);
1894}
1895
chaviw08f3cb22020-01-13 13:17:21 -08001896void Layer::writeToProto(LayersProto& layersProto, uint32_t traceFlags) const {
1897 LayerProto* layerProto = layersProto.add_layers();
1898 writeToProtoDrawingState(layerProto, traceFlags);
1899 writeToProtoCommonState(layerProto, LayerVector::StateSet::Drawing, traceFlags);
1900
1901 for (const sp<Layer>& layer : mDrawingChildren) {
1902 layer->writeToProto(layersProto, traceFlags);
1903 }
1904}
1905
Vishnu Nair8406fd72019-07-30 11:29:31 -07001906void Layer::writeToProtoDrawingState(LayerProto* layerInfo, uint32_t traceFlags) const {
1907 ui::Transform transform = getTransform();
1908
1909 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
1910 for (const auto& pendingState : mPendingStatesSnapshot) {
1911 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
1912 if (barrierLayer != nullptr) {
1913 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
1914 barrierLayerProto->set_id(barrierLayer->sequence);
1915 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
1916 }
1917 }
1918
chaviwd62d3062019-09-04 14:48:02 -07001919 auto buffer = getBuffer();
Vishnu Nair8406fd72019-07-30 11:29:31 -07001920 if (buffer != nullptr) {
1921 LayerProtoHelper::writeToProto(buffer,
1922 [&]() { return layerInfo->mutable_active_buffer(); });
chaviw4244e032019-09-04 11:27:49 -07001923 LayerProtoHelper::writeToProto(ui::Transform(getBufferTransform()),
Vishnu Nair8406fd72019-07-30 11:29:31 -07001924 layerInfo->mutable_buffer_transform());
1925 }
1926 layerInfo->set_invalidate(contentDirty);
1927 layerInfo->set_is_protected(isProtected());
chaviw4244e032019-09-04 11:27:49 -07001928 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(getDataSpace())));
Vishnu Nair8406fd72019-07-30 11:29:31 -07001929 layerInfo->set_queued_frames(getQueuedFrameCount());
1930 layerInfo->set_refresh_pending(isBufferLatched());
1931 layerInfo->set_curr_frame(mCurrentFrameNumber);
1932 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
1933
1934 layerInfo->set_corner_radius(getRoundedCornerState().radius);
1935 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1936 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
1937 [&]() { return layerInfo->mutable_position(); });
1938 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
Lloyd Piquea2468662019-03-07 21:31:06 -08001939 LayerProtoHelper::writeToProto(debugGetVisibleRegionOnDefaultDisplay(),
Vishnu Nair8406fd72019-07-30 11:29:31 -07001940 [&]() { return layerInfo->mutable_visible_region(); });
1941 LayerProtoHelper::writeToProto(surfaceDamageRegion,
1942 [&]() { return layerInfo->mutable_damage_region(); });
chaviwddeae262020-01-06 10:31:23 -08001943
1944 if (hasColorTransform()) {
1945 LayerProtoHelper::writeToProto(getColorTransform(),
1946 layerInfo->mutable_color_transform());
1947 }
Vishnu Nair8406fd72019-07-30 11:29:31 -07001948 }
1949
1950 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
1951 LayerProtoHelper::writeToProto(mSourceBounds,
1952 [&]() { return layerInfo->mutable_source_bounds(); });
1953 LayerProtoHelper::writeToProto(mScreenBounds,
1954 [&]() { return layerInfo->mutable_screen_bounds(); });
Vishnu Nair95a1ed42019-12-06 12:25:11 -08001955 LayerProtoHelper::writeToProto(getRoundedCornerState().cropRect,
1956 [&]() { return layerInfo->mutable_corner_radius_crop(); });
1957 layerInfo->set_shadow_radius(mEffectiveShadowRadius);
Vishnu Nair8406fd72019-07-30 11:29:31 -07001958 }
1959}
1960
1961void Layer::writeToProtoCommonState(LayerProto* layerInfo, LayerVector::StateSet stateSet,
1962 uint32_t traceFlags) const {
chaviw1d044282017-09-27 12:19:28 -07001963 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1964 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001965 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07001966
Peiyong Linefefaac2018-08-17 12:27:51 -07001967 ui::Transform requestedTransform = state.active_legacy.transform;
chaviw1d044282017-09-27 12:19:28 -07001968
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001969 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
1970 layerInfo->set_id(sequence);
1971 layerInfo->set_name(getName().c_str());
chaviw8a01fa42019-08-19 12:39:31 -07001972 layerInfo->set_type(getType());
chaviw1d044282017-09-27 12:19:28 -07001973
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001974 for (const auto& child : children) {
1975 layerInfo->add_children(child->sequence);
chaviw1d044282017-09-27 12:19:28 -07001976 }
chaviw1d044282017-09-27 12:19:28 -07001977
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001978 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1979 sp<Layer> strongRelative = weakRelative.promote();
1980 if (strongRelative != nullptr) {
1981 layerInfo->add_relatives(strongRelative->sequence);
1982 }
chaviwadc40c22018-07-10 16:57:27 -07001983 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001984
1985 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
1986 [&]() { return layerInfo->mutable_transparent_region(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001987
1988 layerInfo->set_layer_stack(getLayerStack());
1989 layerInfo->set_z(state.z);
1990
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001991 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(),
1992 [&]() {
1993 return layerInfo->mutable_requested_position();
1994 });
1995
1996 LayerProtoHelper::writeSizeToProto(state.active_legacy.w, state.active_legacy.h,
1997 [&]() { return layerInfo->mutable_size(); });
1998
1999 LayerProtoHelper::writeToProto(state.crop_legacy,
2000 [&]() { return layerInfo->mutable_crop(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002001
2002 layerInfo->set_is_opaque(isOpaque(state));
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002003
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002004
2005 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
2006 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
2007 LayerProtoHelper::writeToProto(state.color,
2008 [&]() { return layerInfo->mutable_requested_color(); });
2009 layerInfo->set_flags(state.flags);
2010
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002011 LayerProtoHelper::writeToProto(requestedTransform,
2012 layerInfo->mutable_requested_transform());
2013
2014 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
2015 if (parent != nullptr) {
2016 layerInfo->set_parent(parent->sequence);
2017 } else {
2018 layerInfo->set_parent(-1);
2019 }
2020
2021 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2022 if (zOrderRelativeOf != nullptr) {
2023 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
2024 } else {
2025 layerInfo->set_z_order_relative_of(-1);
2026 }
chaviw08f3cb22020-01-13 13:17:21 -08002027
2028 layerInfo->set_is_relative_of(state.isRelativeOf);
chaviwadc40c22018-07-10 16:57:27 -07002029 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002030
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002031 if (traceFlags & SurfaceTracing::TRACE_INPUT) {
2032 LayerProtoHelper::writeToProto(state.inputInfo, state.touchableRegionCrop,
2033 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002034 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002035
2036 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
2037 auto protoMap = layerInfo->mutable_metadata();
2038 for (const auto& entry : state.metadata.mMap) {
2039 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
2040 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002041 }
chaviw1d044282017-09-27 12:19:28 -07002042}
2043
Robert Carr2e102c92018-10-23 12:11:15 -07002044bool Layer::isRemovedFromCurrentState() const {
2045 return mRemovedFromCurrentState;
2046}
2047
Arthur Hungd20b2702019-01-14 18:16:16 +08002048InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002049 InputWindowInfo info = mDrawingState.inputInfo;
chaviwaf87b3e2019-10-01 16:59:28 -07002050 info.id = sequence;
Robert Carr720e5062018-07-30 17:45:14 -07002051
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002052 if (info.displayId == ADISPLAY_ID_NONE) {
chaviwb15ea8a2019-09-03 12:40:22 -07002053 info.displayId = getLayerStack();
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002054 }
2055
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002056 ui::Transform t = getTransform();
2057 const float xScale = t.sx();
2058 const float yScale = t.sy();
Ady Abraham282f1d72019-07-24 18:05:56 -07002059 int32_t xSurfaceInset = info.surfaceInset;
2060 int32_t ySurfaceInset = info.surfaceInset;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002061 if (xScale != 1.0f || yScale != 1.0f) {
Ady Abraham282f1d72019-07-24 18:05:56 -07002062 info.windowXScale *= (xScale != 0.0f) ? 1.0f / xScale : 0.0f;
2063 info.windowYScale *= (yScale != 0.0f) ? 1.0f / yScale : 0.0f;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002064 info.touchableRegion.scaleSelf(xScale, yScale);
Ady Abraham282f1d72019-07-24 18:05:56 -07002065 xSurfaceInset = std::round(xSurfaceInset * xScale);
2066 ySurfaceInset = std::round(ySurfaceInset * yScale);
Riddle Hsu39d4aa52018-11-30 20:46:53 +08002067 }
Robert Carre07e1032018-11-26 12:55:53 -08002068
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002069 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002070 // If this is a portal window, set the touchableRegion to the layerBounds.
2071 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
2072 ? getBufferSize(getDrawingState())
2073 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002074 if (!layerBounds.isValid()) {
2075 layerBounds = getCroppedBufferSize(getDrawingState());
2076 }
Vishnu Nair8033a492018-12-05 07:27:23 -08002077 layerBounds = t.transform(layerBounds);
Ady Abraham282f1d72019-07-24 18:05:56 -07002078
2079 // clamp inset to layer bounds
2080 xSurfaceInset = (xSurfaceInset >= 0) ? std::min(xSurfaceInset, layerBounds.getWidth() / 2) : 0;
2081 ySurfaceInset = (ySurfaceInset >= 0) ? std::min(ySurfaceInset, layerBounds.getHeight() / 2) : 0;
2082
Arthur Hung118b1142019-05-08 21:25:59 +08002083 layerBounds.inset(xSurfaceInset, ySurfaceInset, xSurfaceInset, ySurfaceInset);
Vishnu Nair8033a492018-12-05 07:27:23 -08002084
Arthur Hungd20b2702019-01-14 18:16:16 +08002085 // Input coordinate should match the layer bounds.
2086 info.frameLeft = layerBounds.left;
2087 info.frameTop = layerBounds.top;
2088 info.frameRight = layerBounds.right;
2089 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08002090
2091 // Position the touchable region relative to frame screen location and restrict it to frame
2092 // bounds.
2093 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08002094 info.visible = canReceiveInput();
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002095
2096 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2097 if (info.replaceTouchableRegionWithCrop) {
2098 if (cropLayer == nullptr) {
2099 info.touchableRegion = Region(Rect{mScreenBounds});
2100 } else {
2101 info.touchableRegion = Region(Rect{cropLayer->mScreenBounds});
2102 }
2103 } else if (cropLayer != nullptr) {
2104 info.touchableRegion = info.touchableRegion.intersect(Rect{cropLayer->mScreenBounds});
2105 }
2106
chaviwaf87b3e2019-10-01 16:59:28 -07002107 // If the layer is a clone, we need to crop the input region to cloned root to prevent
2108 // touches from going outside the cloned area.
2109 if (isClone()) {
2110 sp<Layer> clonedRoot = getClonedRoot();
2111 if (clonedRoot != nullptr) {
2112 Rect rect(clonedRoot->mScreenBounds);
2113 info.touchableRegion = info.touchableRegion.intersect(rect);
2114 }
2115 }
2116
Robert Carr720e5062018-07-30 17:45:14 -07002117 return info;
2118}
2119
chaviwaf87b3e2019-10-01 16:59:28 -07002120sp<Layer> Layer::getClonedRoot() {
2121 if (mClonedChild != nullptr) {
2122 return this;
2123 }
2124 if (mDrawingParent == nullptr || mDrawingParent.promote() == nullptr) {
2125 return nullptr;
2126 }
2127 return mDrawingParent.promote()->getClonedRoot();
2128}
2129
Robert Carr720e5062018-07-30 17:45:14 -07002130bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002131 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002132}
2133
Lloyd Piquefeb73d72018-12-04 17:23:44 -08002134std::shared_ptr<compositionengine::Layer> Layer::getCompositionLayer() const {
2135 return nullptr;
2136}
2137
chaviw3e727cd2019-01-31 13:41:05 -08002138bool Layer::canReceiveInput() const {
Vishnu Nair82353e92019-09-12 12:38:47 -07002139 return !isHiddenByPolicy();
chaviw3e727cd2019-01-31 13:41:05 -08002140}
2141
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002142compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
2143 const sp<const DisplayDevice>& display) const {
2144 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionLayer().get());
2145}
2146
Lloyd Piquea2468662019-03-07 21:31:06 -08002147Region Layer::debugGetVisibleRegionOnDefaultDisplay() const {
2148 sp<DisplayDevice> displayDevice = mFlinger->getDefaultDisplayDeviceLocked();
2149 if (displayDevice == nullptr) {
2150 return {};
2151 }
2152
2153 auto outputLayer = findOutputLayerForDisplay(displayDevice);
2154 if (outputLayer == nullptr) {
2155 return {};
2156 }
2157
2158 return outputLayer->getState().visibleRegion;
2159}
2160
chaviwb4c6e582019-08-16 14:35:07 -07002161void Layer::setInitialValuesForClone(const sp<Layer>& clonedFrom) {
2162 // copy drawing state from cloned layer
2163 mDrawingState = clonedFrom->mDrawingState;
2164 mClonedFrom = clonedFrom;
chaviwb4c6e582019-08-16 14:35:07 -07002165}
chaviw74b03172019-08-19 11:09:03 -07002166
2167void Layer::updateMirrorInfo() {
2168 if (mClonedChild == nullptr || !mClonedChild->isClonedFromAlive()) {
2169 // If mClonedChild is null, there is nothing to mirror. If isClonedFromAlive returns false,
2170 // it means that there is a clone, but the layer it was cloned from has been destroyed. In
2171 // that case, we want to delete the reference to the clone since we want it to get
2172 // destroyed. The root, this layer, will still be around since the client can continue
2173 // to hold a reference, but no cloned layers will be displayed.
2174 mClonedChild = nullptr;
2175 return;
2176 }
2177
2178 std::map<sp<Layer>, sp<Layer>> clonedLayersMap;
2179 // If the real layer exists and is in current state, add the clone as a child of the root.
2180 // There's no need to remove from drawingState when the layer is offscreen since currentState is
2181 // copied to drawingState for the root layer. So the clonedChild is always removed from
2182 // drawingState and then needs to be added back each traversal.
2183 if (!mClonedChild->getClonedFrom()->isRemovedFromCurrentState()) {
2184 addChildToDrawing(mClonedChild);
2185 }
2186
2187 mClonedChild->updateClonedDrawingState(clonedLayersMap);
2188 mClonedChild->updateClonedChildren(this, clonedLayersMap);
2189 mClonedChild->updateClonedRelatives(clonedLayersMap);
2190}
2191
2192void Layer::updateClonedDrawingState(std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2193 // If the layer the clone was cloned from is alive, copy the content of the drawingState
2194 // to the clone. If the real layer is no longer alive, continue traversing the children
2195 // since we may be able to pull out other children that are still alive.
2196 if (isClonedFromAlive()) {
2197 sp<Layer> clonedFrom = getClonedFrom();
2198 mDrawingState = clonedFrom->mDrawingState;
chaviw74b03172019-08-19 11:09:03 -07002199 clonedLayersMap.emplace(clonedFrom, this);
2200 }
2201
2202 // The clone layer may have children in drawingState since they may have been created and
2203 // added from a previous request to updateMirorInfo. This is to ensure we don't recreate clones
2204 // that already exist, since we can just re-use them.
2205 // The drawingChildren will not get overwritten by the currentChildren since the clones are
2206 // not updated in the regular traversal. They are skipped since the root will lose the
2207 // reference to them when it copies its currentChildren to drawing.
2208 for (sp<Layer>& child : mDrawingChildren) {
2209 child->updateClonedDrawingState(clonedLayersMap);
2210 }
2211}
2212
2213void Layer::updateClonedChildren(const sp<Layer>& mirrorRoot,
2214 std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2215 mDrawingChildren.clear();
2216
2217 if (!isClonedFromAlive()) {
2218 return;
2219 }
2220
2221 sp<Layer> clonedFrom = getClonedFrom();
2222 for (sp<Layer>& child : clonedFrom->mDrawingChildren) {
2223 if (child == mirrorRoot) {
2224 // This is to avoid cyclical mirroring.
2225 continue;
2226 }
2227 sp<Layer> clonedChild = clonedLayersMap[child];
2228 if (clonedChild == nullptr) {
2229 clonedChild = child->createClone();
2230 clonedLayersMap[child] = clonedChild;
2231 }
2232 addChildToDrawing(clonedChild);
2233 clonedChild->updateClonedChildren(mirrorRoot, clonedLayersMap);
2234 }
2235}
2236
chaviwaf87b3e2019-10-01 16:59:28 -07002237void Layer::updateClonedInputInfo(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2238 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2239 if (cropLayer != nullptr) {
2240 if (clonedLayersMap.count(cropLayer) == 0) {
2241 // Real layer had a crop layer but it's not in the cloned hierarchy. Just set to
2242 // self as crop layer to avoid going outside bounds.
2243 mDrawingState.touchableRegionCrop = this;
2244 } else {
2245 const sp<Layer>& clonedCropLayer = clonedLayersMap.at(cropLayer);
2246 mDrawingState.touchableRegionCrop = clonedCropLayer;
2247 }
2248 }
2249 // Cloned layers shouldn't handle watch outside since their z order is not determined by
2250 // WM or the client.
2251 mDrawingState.inputInfo.layoutParamsFlags &= ~InputWindowInfo::FLAG_WATCH_OUTSIDE_TOUCH;
2252}
2253
2254void Layer::updateClonedRelatives(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
chaviw74b03172019-08-19 11:09:03 -07002255 mDrawingState.zOrderRelativeOf = nullptr;
2256 mDrawingState.zOrderRelatives.clear();
2257
2258 if (!isClonedFromAlive()) {
2259 return;
2260 }
2261
chaviwaf87b3e2019-10-01 16:59:28 -07002262 const sp<Layer>& clonedFrom = getClonedFrom();
chaviw74b03172019-08-19 11:09:03 -07002263 for (wp<Layer>& relativeWeak : clonedFrom->mDrawingState.zOrderRelatives) {
chaviwaf87b3e2019-10-01 16:59:28 -07002264 const sp<Layer>& relative = relativeWeak.promote();
2265 if (clonedLayersMap.count(relative) > 0) {
2266 auto& clonedRelative = clonedLayersMap.at(relative);
chaviw74b03172019-08-19 11:09:03 -07002267 mDrawingState.zOrderRelatives.add(clonedRelative);
2268 }
2269 }
2270
2271 // Check if the relativeLayer for the real layer is part of the cloned hierarchy.
2272 // It's possible that the layer it's relative to is outside the requested cloned hierarchy.
2273 // In that case, we treat the layer as if the relativeOf has been removed. This way, it will
2274 // still traverse the children, but the layer with the missing relativeOf will not be shown
2275 // on screen.
chaviwaf87b3e2019-10-01 16:59:28 -07002276 const sp<Layer>& relativeOf = clonedFrom->mDrawingState.zOrderRelativeOf.promote();
2277 if (clonedLayersMap.count(relativeOf) > 0) {
2278 const sp<Layer>& clonedRelativeOf = clonedLayersMap.at(relativeOf);
chaviw74b03172019-08-19 11:09:03 -07002279 mDrawingState.zOrderRelativeOf = clonedRelativeOf;
2280 }
2281
chaviwaf87b3e2019-10-01 16:59:28 -07002282 updateClonedInputInfo(clonedLayersMap);
2283
chaviw74b03172019-08-19 11:09:03 -07002284 for (sp<Layer>& child : mDrawingChildren) {
2285 child->updateClonedRelatives(clonedLayersMap);
2286 }
2287}
2288
2289void Layer::addChildToDrawing(const sp<Layer>& layer) {
2290 mDrawingChildren.add(layer);
2291 layer->mDrawingParent = this;
2292}
2293
Mathias Agopian13127d82013-03-05 17:47:11 -08002294// ---------------------------------------------------------------------------
2295
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002296}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002297
2298#if defined(__gl_h_)
2299#error "don't include gl/gl.h in this file"
2300#endif
2301
2302#if defined(__gl2_h_)
2303#error "don't include gl2/gl2.h in this file"
2304#endif