blob: 4b9f55595bd996f32d09841c03c878fbb1eb9c77 [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;
Ana Krulecc84d09b2019-11-02 23:10:29 +0100111 mCurrentState.frameRateSelectionPriority = PRIORITY_UNSET;
Evan Roskya1f1e152019-01-24 16:17:46 -0800112 mCurrentState.metadata = args.metadata;
Vishnu Nairf37dcfb2019-11-16 07:37:09 -0800113 mCurrentState.shadowRadius = 0.f;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700114
115 // drawing state & current state are identical
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800116 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700117
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800118 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700119 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800120 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200121 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700122
Vishnu Nair0f085c62019-08-30 08:49:12 -0700123 mCallingPid = args.callingPid;
124 mCallingUid = args.callingUid;
Dominik Laskowski75848362019-11-11 17:57:20 -0800125}
126
127void Layer::onFirstRef() {
128 mFlinger->onLayerFirstRef(this);
Dan Stoza436ccf32018-06-21 12:10:12 -0700129}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700130
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700131Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800132 sp<Client> c(mClientRef.promote());
133 if (c != 0) {
134 c->detachLayer(this);
135 }
136
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000137 mFrameTracker.logAndResetStats(mName);
chaviw74d90ad2019-04-26 14:45:26 -0700138 mFlinger->onLayerDestroyed(this);
Mathias Agopian96f08192010-06-02 23:28:45 -0700139}
140
Vishnu Nair0f085c62019-08-30 08:49:12 -0700141LayerCreationArgs::LayerCreationArgs(SurfaceFlinger* flinger, const sp<Client>& client,
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700142 std::string name, uint32_t w, uint32_t h, uint32_t flags,
Vishnu Nair0f085c62019-08-30 08:49:12 -0700143 LayerMetadata metadata)
144 : flinger(flinger),
145 client(client),
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700146 name(std::move(name)),
Vishnu Nair0f085c62019-08-30 08:49:12 -0700147 w(w),
148 h(h),
149 flags(flags),
150 metadata(std::move(metadata)) {
151 IPCThreadState* ipc = IPCThreadState::self();
152 callingPid = ipc->getCallingPid();
153 callingUid = ipc->getCallingUid();
154}
155
Mathias Agopian13127d82013-03-05 17:47:11 -0800156// ---------------------------------------------------------------------------
157// callbacks
158// ---------------------------------------------------------------------------
159
David Sodmaneb085e02017-10-05 18:49:04 -0700160/*
161 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
162 * Layer. So, the implementation is done in BufferLayer. When called on a
163 * ColorLayer object, it's essentially a NOP.
164 */
David Sodmaneb085e02017-10-05 18:49:04 -0700165void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800166
chaviw43cb3cb2019-05-31 15:23:41 -0700167void Layer::removeRemoteSyncPoints() {
168 for (auto& point : mRemoteSyncPoints) {
169 point->setTransactionApplied();
170 }
171 mRemoteSyncPoints.clear();
172
173 {
chaviw43cb3cb2019-05-31 15:23:41 -0700174 for (State pendingState : mPendingStates) {
175 pendingState.barrierLayer_legacy = nullptr;
176 }
177 }
178}
179
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700180void Layer::removeRelativeZ(const std::vector<Layer*>& layersInTree) {
181 if (mCurrentState.zOrderRelativeOf == nullptr) {
182 return;
183 }
Robert Carr2e102c92018-10-23 12:11:15 -0700184
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700185 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
186 if (strongRelative == nullptr) {
187 setZOrderRelativeOf(nullptr);
188 return;
189 }
190
191 if (!std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
192 strongRelative->removeZOrderRelative(this);
193 mFlinger->setTransactionFlags(eTraversalNeeded);
chaviw606e5cf2019-03-01 10:12:10 -0800194 setZOrderRelativeOf(nullptr);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700195 }
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700196}
197
198void Layer::removeFromCurrentState() {
199 mRemovedFromCurrentState = true;
Rob Carr4bba3702018-10-08 21:53:30 +0000200
Robert Carr2e102c92018-10-23 12:11:15 -0700201 // Since we are no longer reachable from CurrentState SurfaceFlinger
202 // will no longer invoke doTransaction for us, and so we will
203 // never finish applying transactions. We signal the sync point
204 // now so that another layer will not become indefinitely
205 // blocked.
chaviw43cb3cb2019-05-31 15:23:41 -0700206 removeRemoteSyncPoints();
Robert Carr2e102c92018-10-23 12:11:15 -0700207
208 {
209 Mutex::Autolock syncLock(mLocalSyncPointMutex);
210 for (auto& point : mLocalSyncPoints) {
211 point->setFrameAvailable();
212 }
213 mLocalSyncPoints.clear();
214 }
215
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800216 mFlinger->markLayerPendingRemovalLocked(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700217}
Chia-I Wu38512252017-05-17 14:36:16 -0700218
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700219void Layer::onRemovedFromCurrentState() {
220 auto layersInTree = getLayersInTree(LayerVector::StateSet::Current);
221 std::sort(layersInTree.begin(), layersInTree.end());
222 for (const auto& layer : layersInTree) {
223 layer->removeFromCurrentState();
224 layer->removeRelativeZ(layersInTree);
225 }
226}
227
chaviw61626f22018-11-15 16:26:27 -0800228void Layer::addToCurrentState() {
229 mRemovedFromCurrentState = false;
230
231 for (const auto& child : mCurrentChildren) {
232 child->addToCurrentState();
233 }
234}
235
Mathias Agopian13127d82013-03-05 17:47:11 -0800236// ---------------------------------------------------------------------------
237// set-up
238// ---------------------------------------------------------------------------
239
chaviw13fdc492017-06-27 12:40:18 -0700240bool Layer::getPremultipledAlpha() const {
241 return mPremultipliedAlpha;
242}
243
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700244sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800245 Mutex::Autolock _l(mLock);
Robert Carrc0df3122019-04-11 13:18:21 -0700246 if (mGetHandleCalled) {
247 ALOGE("Get handle called twice" );
248 return nullptr;
249 }
250 mGetHandleCalled = true;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700251 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800252}
253
254// ---------------------------------------------------------------------------
255// h/w composer set-up
256// ---------------------------------------------------------------------------
257
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700258static Rect reduce(const Rect& win, const Region& exclude) {
259 if (CC_LIKELY(exclude.isEmpty())) {
260 return win;
261 }
262 if (exclude.isRect()) {
263 return win.reduce(exclude.getBounds());
264 }
265 return Region(win).subtract(exclude).getBounds();
266}
267
Dan Stoza80d61162017-12-20 15:57:52 -0800268static FloatRect reduce(const FloatRect& win, const Region& exclude) {
269 if (CC_LIKELY(exclude.isEmpty())) {
270 return win;
271 }
272 // Convert through Rect (by rounding) for lack of FloatRegion
273 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
274}
275
Vishnu Nair4351ad52019-02-11 14:13:02 -0800276Rect Layer::getScreenBounds(bool reduceTransparentRegion) const {
Vishnu Nairf0c28512019-02-08 12:40:28 -0800277 if (!reduceTransparentRegion) {
278 return Rect{mScreenBounds};
279 }
280
281 FloatRect bounds = getBounds();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800282 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800283 // Transform to screen space.
284 bounds = t.transform(bounds);
285 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700286}
287
Vishnu Nair4351ad52019-02-11 14:13:02 -0800288FloatRect Layer::getBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000289 const State& s(getDrawingState());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800290 return getBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700291}
292
Vishnu Nairf0c28512019-02-08 12:40:28 -0800293FloatRect Layer::getBounds(const Region& activeTransparentRegion) const {
294 // Subtract the transparent region and snap to the bounds.
295 return reduce(mBounds, activeTransparentRegion);
296}
297
Vishnu Nairc652ff82019-03-15 12:48:54 -0700298ui::Transform Layer::getBufferScaleTransform() const {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800299 // If the layer is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
300 // it isFixedSize) then there may be additional scaling not accounted
Vishnu Nairc652ff82019-03-15 12:48:54 -0700301 // for in the layer transform.
chaviwd62d3062019-09-04 14:48:02 -0700302 if (!isFixedSize() || getBuffer() == nullptr) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700303 return {};
Vishnu Nair4351ad52019-02-11 14:13:02 -0800304 }
305
Marissa Wall290ad082019-03-06 13:23:47 -0800306 // If the layer is a buffer state layer, the active width and height
307 // could be infinite. In that case, return the effective transform.
308 const uint32_t activeWidth = getActiveWidth(getDrawingState());
309 const uint32_t activeHeight = getActiveHeight(getDrawingState());
310 if (activeWidth >= UINT32_MAX && activeHeight >= UINT32_MAX) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700311 return {};
Marissa Wall290ad082019-03-06 13:23:47 -0800312 }
313
chaviwd62d3062019-09-04 14:48:02 -0700314 int bufferWidth = getBuffer()->getWidth();
315 int bufferHeight = getBuffer()->getHeight();
Vishnu Nairc652ff82019-03-15 12:48:54 -0700316
chaviw4244e032019-09-04 11:27:49 -0700317 if (getBufferTransform() & NATIVE_WINDOW_TRANSFORM_ROT_90) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700318 std::swap(bufferWidth, bufferHeight);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800319 }
Vishnu Nairc652ff82019-03-15 12:48:54 -0700320
Marissa Wall290ad082019-03-06 13:23:47 -0800321 float sx = activeWidth / static_cast<float>(bufferWidth);
322 float sy = activeHeight / static_cast<float>(bufferHeight);
323
Vishnu Nair4351ad52019-02-11 14:13:02 -0800324 ui::Transform extraParentScaling;
325 extraParentScaling.set(sx, 0, 0, sy);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700326 return extraParentScaling;
327}
328
329ui::Transform Layer::getTransformWithScale(const ui::Transform& bufferScaleTransform) const {
330 // We need to mirror this scaling to child surfaces or we will break the contract where WM can
331 // treat child surfaces as pixels in the parent surface.
chaviwd62d3062019-09-04 14:48:02 -0700332 if (!isFixedSize() || getBuffer() == nullptr) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700333 return mEffectiveTransform;
334 }
335 return mEffectiveTransform * bufferScaleTransform;
336}
337
338FloatRect Layer::getBoundsPreScaling(const ui::Transform& bufferScaleTransform) const {
339 // We need the pre scaled layer bounds when computing child bounds to make sure the child is
340 // cropped to its parent layer after any buffer transform scaling is applied.
chaviwd62d3062019-09-04 14:48:02 -0700341 if (!isFixedSize() || getBuffer() == nullptr) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700342 return mBounds;
343 }
344 return bufferScaleTransform.inverse().transform(mBounds);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800345}
346
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700347void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform,
348 float parentShadowRadius) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800349 const State& s(getDrawingState());
350
351 // Calculate effective layer transform
352 mEffectiveTransform = parentTransform * getActiveTransform(s);
353
354 // Transform parent bounds to layer space
355 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
356
Vishnu Nairc652ff82019-03-15 12:48:54 -0700357 // Calculate source bounds
Vishnu Nair4351ad52019-02-11 14:13:02 -0800358 mSourceBounds = computeSourceBounds(parentBounds);
359
360 // Calculate bounds by croping diplay frame with layer crop and parent bounds
361 FloatRect bounds = mSourceBounds;
362 const Rect layerCrop = getCrop(s);
363 if (!layerCrop.isEmpty()) {
364 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
365 }
366 bounds = bounds.intersect(parentBounds);
367
368 mBounds = bounds;
369 mScreenBounds = mEffectiveTransform.transform(mBounds);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700370
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700371 // Use the layer's own shadow radius if set. Otherwise get the radius from
372 // parent.
373 if (s.shadowRadius > 0.f) {
374 mEffectiveShadowRadius = s.shadowRadius;
375 } else {
376 mEffectiveShadowRadius = parentShadowRadius;
377 }
378
379 // Shadow radius is passed down to only one layer so if the layer can draw shadows,
380 // don't pass it to its children.
381 const float childShadowRadius = canDrawShadows() ? 0.f : mEffectiveShadowRadius;
382
Vishnu Nairc652ff82019-03-15 12:48:54 -0700383 // Add any buffer scaling to the layer's children.
384 ui::Transform bufferScaleTransform = getBufferScaleTransform();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800385 for (const sp<Layer>& child : mDrawingChildren) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700386 child->computeBounds(getBoundsPreScaling(bufferScaleTransform),
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700387 getTransformWithScale(bufferScaleTransform), childShadowRadius);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800388 }
389}
390
Vishnu Nair60356342018-11-13 13:00:45 -0800391Rect Layer::getCroppedBufferSize(const State& s) const {
392 Rect size = getBufferSize(s);
393 Rect crop = getCrop(s);
394 if (!crop.isEmpty() && size.isValid()) {
395 size.intersect(crop, &size);
396 } else if (!crop.isEmpty()) {
397 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700398 }
Vishnu Nair60356342018-11-13 13:00:45 -0800399 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800400}
401
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700402void Layer::setupRoundedCornersCropCoordinates(Rect win,
403 const FloatRect& roundedCornersCrop) const {
404 // Translate win by the rounded corners rect coordinates, to have all values in
405 // layer coordinate space.
406 win.left -= roundedCornersCrop.left;
407 win.right -= roundedCornersCrop.left;
408 win.top -= roundedCornersCrop.top;
409 win.bottom -= roundedCornersCrop.top;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700410}
411
Lloyd Piquec6687342019-03-07 21:34:57 -0800412void Layer::latchBasicGeometry(compositionengine::LayerFECompositionState& compositionState) const {
413 const auto& drawingState{getDrawingState()};
414 const uint32_t layerStack = getLayerStack();
415 const auto alpha = static_cast<float>(getAlpha());
416 const bool opaque = isOpaque(drawingState);
417 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
418
419 auto blendMode = Hwc2::IComposerClient::BlendMode::NONE;
420 if (!opaque || alpha != 1.0f) {
421 blendMode = mPremultipliedAlpha ? Hwc2::IComposerClient::BlendMode::PREMULTIPLIED
422 : Hwc2::IComposerClient::BlendMode::COVERAGE;
423 }
424
425 // TODO(b/121291683): Instead of filling in a passed-in compositionState
426 // structure, switch to Layer owning the structure and have
427 // CompositionEngine be able to get a reference to it.
428
429 compositionState.layerStackId =
430 (layerStack != ~0u) ? std::make_optional(layerStack) : std::nullopt;
431 compositionState.internalOnly = getPrimaryDisplayOnly();
432 compositionState.isVisible = isVisible();
433 compositionState.isOpaque = opaque && !usesRoundedCorners && alpha == 1.f;
Vishnu Naira483b4a2019-12-12 15:07:52 -0800434 compositionState.shadowRadius = mEffectiveShadowRadius;
Lloyd Piquec6687342019-03-07 21:34:57 -0800435
436 compositionState.contentDirty = contentDirty;
437 contentDirty = false;
438
439 compositionState.geomLayerBounds = mBounds;
440 compositionState.geomLayerTransform = getTransform();
441 compositionState.geomInverseLayerTransform = compositionState.geomLayerTransform.inverse();
442 compositionState.transparentRegionHint = getActiveTransparentRegion(drawingState);
443
444 compositionState.blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
445 compositionState.alpha = alpha;
446}
447
Lloyd Piquea83776c2019-01-29 18:42:32 -0800448void Layer::latchGeometry(compositionengine::LayerFECompositionState& compositionState) const {
449 const auto& drawingState{getDrawingState()};
Mathias Agopian13127d82013-03-05 17:47:11 -0800450
Lloyd Piquea83776c2019-01-29 18:42:32 -0800451 int type = drawingState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
452 int appId = drawingState.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700453 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400454 if (parent.get()) {
455 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800456 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
457 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
Jiwen 'Steve' Cai736c74e2019-05-28 18:33:22 -0700458 if (parentType > 0 && parentAppId > 0) {
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800459 type = parentType;
460 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700461 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400462 }
463
Lloyd Piquea83776c2019-01-29 18:42:32 -0800464 compositionState.geomBufferSize = getBufferSize(drawingState);
chaviw4244e032019-09-04 11:27:49 -0700465 compositionState.geomContentCrop = getBufferCrop();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800466 compositionState.geomCrop = getCrop(drawingState);
chaviw4244e032019-09-04 11:27:49 -0700467 compositionState.geomBufferTransform = getBufferTransform();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800468 compositionState.geomBufferUsesDisplayInverseTransform = getTransformToDisplayInverse();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800469 compositionState.geomUsesSourceCrop = usesSourceCrop();
470 compositionState.isSecure = isSecure();
David Sodman15094112018-10-11 09:39:37 -0700471
Lloyd Piquea83776c2019-01-29 18:42:32 -0800472 compositionState.type = type;
473 compositionState.appId = appId;
474}
David Sodmanba340492018-08-05 21:51:33 -0700475
Lloyd Piquef5275482019-01-29 18:42:42 -0800476void Layer::latchPerFrameState(compositionengine::LayerFECompositionState& compositionState) const {
Lloyd Pique688abd42019-02-15 15:42:24 -0800477 const auto& drawingState{getDrawingState()};
Lloyd Piquef5275482019-01-29 18:42:42 -0800478 compositionState.forceClientComposition = false;
479
Lloyd Piquef5275482019-01-29 18:42:42 -0800480 compositionState.isColorspaceAgnostic = isColorSpaceAgnostic();
chaviw4244e032019-09-04 11:27:49 -0700481 compositionState.dataspace = getDataSpace();
Lloyd Piquef5275482019-01-29 18:42:42 -0800482 compositionState.colorTransform = getColorTransform();
483 compositionState.colorTransformIsIdentity = !hasColorTransform();
484 compositionState.surfaceDamage = surfaceDamageRegion;
Lloyd Pique688abd42019-02-15 15:42:24 -0800485 compositionState.hasProtectedContent = isProtected();
486
487 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700488 const bool drawsShadows = mEffectiveShadowRadius != 0.f;
489
Lloyd Pique688abd42019-02-15 15:42:24 -0800490 compositionState.isOpaque =
491 isOpaque(drawingState) && !usesRoundedCorners && getAlpha() == 1.0_hf;
Lloyd Piquef5275482019-01-29 18:42:42 -0800492
493 // Force client composition for special cases known only to the front-end.
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700494 if (isHdrY410() || usesRoundedCorners || drawsShadows) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800495 compositionState.forceClientComposition = true;
496 }
497}
498
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800499void Layer::latchCursorCompositionState(
500 compositionengine::LayerFECompositionState& compositionState) const {
501 // This gives us only the "orientation" component of the transform
502 const State& drawingState{getDrawingState()};
503
504 // Apply the layer's transform, followed by the display's global transform
505 // Here we're guaranteed that the layer's transform preserves rects
506 Rect win = getCroppedBufferSize(drawingState);
507 // Subtract the transparent region and snap to the bounds
508 Rect bounds = reduce(win, getActiveTransparentRegion(drawingState));
509 Rect frame(getTransform().transform(bounds));
510
511 compositionState.cursorFrame = frame;
512}
513
Lloyd Piquef16688f2019-02-19 17:47:57 -0800514bool Layer::onPreComposition(nsecs_t) {
515 return false;
516}
517
Lloyd Piquea83776c2019-01-29 18:42:32 -0800518void Layer::latchCompositionState(compositionengine::LayerFECompositionState& compositionState,
Lloyd Piquec6687342019-03-07 21:34:57 -0800519 compositionengine::LayerFE::StateSubset subset) const {
520 using StateSubset = compositionengine::LayerFE::StateSubset;
Lloyd Piquef5275482019-01-29 18:42:42 -0800521
Lloyd Piquec6687342019-03-07 21:34:57 -0800522 switch (subset) {
523 case StateSubset::BasicGeometry:
524 latchBasicGeometry(compositionState);
525 break;
526
527 case StateSubset::GeometryAndContent:
528 latchBasicGeometry(compositionState);
529 latchGeometry(compositionState);
530 latchPerFrameState(compositionState);
531 break;
532
533 case StateSubset::Content:
534 latchPerFrameState(compositionState);
535 break;
536 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800537}
Mathias Agopian29a367b2011-07-12 14:51:45 -0700538
Lloyd Piquea83776c2019-01-29 18:42:32 -0800539const char* Layer::getDebugName() const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700540 return mName.c_str();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800541}
542
Mathias Agopian13127d82013-03-05 17:47:11 -0800543// ---------------------------------------------------------------------------
544// drawing...
545// ---------------------------------------------------------------------------
546
Lloyd Piquef16688f2019-02-19 17:47:57 -0800547std::optional<renderengine::LayerSettings> Layer::prepareClientComposition(
548 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
549 if (!getCompositionLayer()) {
550 return {};
551 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800552
Vishnu Nair4351ad52019-02-11 14:13:02 -0800553 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000554 half alpha = getAlpha();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800555 renderengine::LayerSettings layerSettings;
556 layerSettings.geometry.boundaries = bounds;
557 if (targetSettings.useIdentityTransform) {
558 layerSettings.geometry.positionTransform = mat4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000559 } else {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800560 layerSettings.geometry.positionTransform = getTransform().asMatrix4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000561 }
562
563 if (hasColorTransform()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800564 layerSettings.colorTransform = getColorTransform();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000565 }
566
567 const auto roundedCornerState = getRoundedCornerState();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800568 layerSettings.geometry.roundedCornersRadius = roundedCornerState.radius;
569 layerSettings.geometry.roundedCornersCrop = roundedCornerState.cropRect;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000570
Lloyd Piquef16688f2019-02-19 17:47:57 -0800571 layerSettings.alpha = alpha;
chaviw4244e032019-09-04 11:27:49 -0700572 layerSettings.sourceDataspace = getDataSpace();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800573 return layerSettings;
Mathias Agopian13127d82013-03-05 17:47:11 -0800574}
575
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800576std::optional<renderengine::LayerSettings> Layer::prepareShadowClientComposition(
577 const renderengine::LayerSettings& casterLayerSettings, const Rect& displayViewport,
578 ui::Dataspace outputDataspace) {
579 renderengine::ShadowSettings shadow = getShadowSettings(displayViewport);
580 if (shadow.length <= 0.f) {
581 return {};
582 }
583
584 const float casterAlpha = casterLayerSettings.alpha;
585 const bool casterIsOpaque = ((casterLayerSettings.source.buffer.buffer != nullptr) &&
586 casterLayerSettings.source.buffer.isOpaque);
587
588 renderengine::LayerSettings shadowLayer = casterLayerSettings;
589 shadowLayer.shadow = shadow;
Vishnu Naira483b4a2019-12-12 15:07:52 -0800590 shadowLayer.geometry.boundaries = mBounds; // ignore transparent region
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800591
592 // If the casting layer is translucent, we need to fill in the shadow underneath the layer.
593 // Otherwise the generated shadow will only be shown around the casting layer.
594 shadowLayer.shadow.casterIsTranslucent = !casterIsOpaque || (casterAlpha < 1.0f);
595 shadowLayer.shadow.ambientColor *= casterAlpha;
596 shadowLayer.shadow.spotColor *= casterAlpha;
597 shadowLayer.sourceDataspace = outputDataspace;
598 shadowLayer.source.buffer.buffer = nullptr;
599
600 if (shadowLayer.shadow.ambientColor.a <= 0.f && shadowLayer.shadow.spotColor.a <= 0.f) {
601 return {};
602 }
603
604 float casterCornerRadius = shadowLayer.geometry.roundedCornersRadius;
605 const FloatRect& cornerRadiusCropRect = casterLayerSettings.geometry.roundedCornersCrop;
606 const FloatRect& casterRect = shadowLayer.geometry.boundaries;
607
608 // crop used to set the corner radius may be larger than the content rect. Adjust the corner
609 // radius accordingly.
610 if (casterCornerRadius > 0.f) {
611 float cropRectOffset = std::max(std::abs(cornerRadiusCropRect.top - casterRect.top),
612 std::abs(cornerRadiusCropRect.left - casterRect.left));
613 if (cropRectOffset > casterCornerRadius) {
614 casterCornerRadius = 0;
615 } else {
616 casterCornerRadius -= cropRectOffset;
617 }
618 shadowLayer.geometry.roundedCornersRadius = casterCornerRadius;
619 }
620
621 return shadowLayer;
622}
623
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800624Hwc2::IComposerClient::Composition Layer::getCompositionType(
625 const sp<const DisplayDevice>& display) const {
626 const auto outputLayer = findOutputLayerForDisplay(display);
627 LOG_FATAL_IF(!outputLayer);
628 return outputLayer->getState().hwc ? (*outputLayer->getState().hwc).hwcCompositionType
629 : Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800630}
631
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800632bool Layer::getClearClientTarget(const sp<const DisplayDevice>& display) const {
633 const auto outputLayer = findOutputLayerForDisplay(display);
634 LOG_FATAL_IF(!outputLayer);
635 return outputLayer->getState().clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800636}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800637
Dan Stozacac35382016-01-27 12:21:06 -0800638bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
639 if (point->getFrameNumber() <= mCurrentFrameNumber) {
640 // Don't bother with a SyncPoint, since we've already latched the
641 // relevant frame
642 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700643 }
Robert Carr2e102c92018-10-23 12:11:15 -0700644 if (isRemovedFromCurrentState()) {
645 return false;
646 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700647
Dan Stozacac35382016-01-27 12:21:06 -0800648 Mutex::Autolock lock(mLocalSyncPointMutex);
649 mLocalSyncPoints.push_back(point);
650 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700651}
652
Mathias Agopian13127d82013-03-05 17:47:11 -0800653// ----------------------------------------------------------------------------
654// local state
655// ----------------------------------------------------------------------------
656
David Sodman41fdfc92017-11-06 16:09:56 -0800657bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800658 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700659 return (s.flags & layer_state_t::eLayerSecure);
660}
661
Mathias Agopian13127d82013-03-05 17:47:11 -0800662// ----------------------------------------------------------------------------
663// transaction
664// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800665
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800666void Layer::pushPendingState() {
667 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700668 return;
669 }
Alec Mourie60041e2019-06-14 18:59:51 -0700670 ATRACE_CALL();
Dan Stoza7dde5992015-05-22 09:51:44 -0700671
Dan Stoza7dde5992015-05-22 09:51:44 -0700672 // If this transaction is waiting on the receipt of a frame, generate a sync
673 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700674 // We don't allow installing sync points after we are removed from the current state
675 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800676 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
677 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800678 if (barrierLayer == nullptr) {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700679 ALOGE("[%s] Unable to promote barrier Layer.", getDebugName());
Dan Stoza7dde5992015-05-22 09:51:44 -0700680 // If we can't promote the layer we are intended to wait on,
681 // then it is expired or otherwise invalid. Allow this transaction
682 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800683 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800684 } else {
chaviw43cb3cb2019-05-31 15:23:41 -0700685 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy, this);
Robert Carr0d480722017-01-10 16:42:54 -0800686 if (barrierLayer->addSyncPoint(syncPoint)) {
Alec Mourie60041e2019-06-14 18:59:51 -0700687 std::stringstream ss;
688 ss << "Adding sync point " << mCurrentState.frameNumber_legacy;
689 ATRACE_NAME(ss.str().c_str());
Dan Stozacac35382016-01-27 12:21:06 -0800690 mRemoteSyncPoints.push_back(std::move(syncPoint));
691 } else {
692 // We already missed the frame we're supposed to synchronize
693 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800694 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800695 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700696 }
697
Dan Stoza7dde5992015-05-22 09:51:44 -0700698 // Wake us up to check if the frame has been received
699 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700700 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700701 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800702 mPendingStates.push_back(mCurrentState);
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700703 ATRACE_INT(mTransactionName.c_str(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700704}
705
Pablo Ceballos05289c22016-04-14 15:49:55 -0700706void Layer::popPendingState(State* stateToCommit) {
Alec Mourie60041e2019-06-14 18:59:51 -0700707 ATRACE_CALL();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800708 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700709
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800710 mPendingStates.removeAt(0);
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700711 ATRACE_INT(mTransactionName.c_str(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700712}
713
Pablo Ceballos05289c22016-04-14 15:49:55 -0700714bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700715 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800716 while (!mPendingStates.empty()) {
717 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700718 if (mRemoteSyncPoints.empty()) {
719 // If we don't have a sync point for this, apply it anyway. It
720 // will be visually wrong, but it should keep us from getting
721 // into too much trouble.
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700722 ALOGE("[%s] No local sync point found", getDebugName());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700723 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700724 stateUpdateAvailable = true;
725 continue;
726 }
727
Marissa Wallf58c14b2018-07-24 10:50:43 -0700728 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800729 mPendingStates[0].frameNumber_legacy) {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700730 ALOGE("[%s] Unexpected sync point frame number found", getDebugName());
Dan Stozacac35382016-01-27 12:21:06 -0800731
732 // Signal our end of the sync point and then dispose of it
733 mRemoteSyncPoints.front()->setTransactionApplied();
734 mRemoteSyncPoints.pop_front();
735 continue;
736 }
737
Dan Stoza7dde5992015-05-22 09:51:44 -0700738 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
Alec Mourie60041e2019-06-14 18:59:51 -0700739 ATRACE_NAME("frameIsAvailable");
Dan Stoza7dde5992015-05-22 09:51:44 -0700740 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700741 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700742 stateUpdateAvailable = true;
743
744 // Signal our end of the sync point and then dispose of it
745 mRemoteSyncPoints.front()->setTransactionApplied();
746 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800747 } else {
Alec Mourie60041e2019-06-14 18:59:51 -0700748 ATRACE_NAME("!frameIsAvailable");
Dan Stoza792e5292016-02-11 11:43:58 -0800749 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700750 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700751 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700752 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700753 stateUpdateAvailable = true;
754 }
755 }
756
757 // If we still have pending updates, wake SurfaceFlinger back up and point
758 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800759 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700760 setTransactionFlags(eTransactionNeeded);
761 mFlinger->setTransactionFlags(eTraversalNeeded);
762 }
763
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800764 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700765 return stateUpdateAvailable;
766}
767
Marissa Wall61c58622018-07-18 10:12:20 -0700768uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000769 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800770
Marissa Wall61c58622018-07-18 10:12:20 -0700771 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
772 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700773
David Sodmaneb085e02017-10-05 18:49:04 -0700774 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700775 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000776 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800777 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
778 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
779 " requested={ wh={%4u,%4u} }}\n"
780 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
781 " requested={ wh={%4u,%4u} }}\n",
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700782 this, getName().c_str(), getBufferTransform(), getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700783 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
784 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
785 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
786 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
787 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700788 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
789 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
790 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800791 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700792
Robert Carre392b552017-09-19 12:16:05 -0700793 // Don't let Layer::doTransaction update the drawing state
794 // if we have a pending resize, unless we are in fixed-size mode.
795 // the drawing state will be updated only once we receive a buffer
796 // with the correct size.
797 //
798 // In particular, we want to make sure the clip (which is part
799 // of the geometry state) is latched together with the size but is
800 // latched immediately when no resizing is involved.
801 //
802 // If a sideband stream is attached, however, we want to skip this
803 // optimization so that transactions aren't missed when a buffer
804 // never arrives
805 //
806 // In the case that we don't have a buffer we ignore other factors
807 // and avoid entering the resizePending state. At a high level the
808 // resizePending state is to avoid applying the state of the new buffer
809 // to the old buffer. However in the state where we don't have an old buffer
810 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700811 const bool resizePending =
812 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
813 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
chaviwd62d3062019-09-04 14:48:02 -0700814 (getBuffer() != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700815 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800816 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700817 flags |= eDontUpdateGeometryState;
818 }
819 }
820
Robert Carr7bf247e2017-05-18 14:02:49 -0700821 // Here we apply various requested geometry states, depending on our
822 // latching configuration. See Layer.h for a detailed discussion of
823 // how geometry latching is controlled.
824 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000825 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700826
Robert Carr7bf247e2017-05-18 14:02:49 -0700827 // There is an awkward asymmetry in the handling of the crop states in the position
828 // states, as can be seen below. Largely this arises from position and transform
829 // being stored in the same data structure while having different latching rules.
830 // b/38182305
831 //
Marissa Wall61c58622018-07-18 10:12:20 -0700832 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700833 // applyPendingStates in the presence of deferred transactions.
chaviw214c89d2019-09-04 16:03:53 -0700834 editCurrentState.active_legacy = editCurrentState.requested_legacy;
835 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Mathias Agopian13127d82013-03-05 17:47:11 -0800836 }
837
Marissa Wall61c58622018-07-18 10:12:20 -0700838 return flags;
839}
840
841uint32_t Layer::doTransaction(uint32_t flags) {
842 ATRACE_CALL();
843
chaviw5aedec92018-10-22 10:40:38 -0700844 if (mLayerDetached) {
Robert Carr321e83c2019-08-19 15:49:30 -0700845 // Ensure BLAST buffer callbacks are processed.
846 // detachChildren and mLayerDetached were implemented to avoid geometry updates
847 // to layers in the cases of animation. For BufferQueue layers buffers are still
848 // consumed as normal. This is useful as otherwise the client could get hung
849 // inevitably waiting on a buffer to return. We recreate this semantic for BufferQueue
850 // even though it is a little consistent. detachChildren is shortly slated for removal
851 // by the hierarchy mirroring work so we don't need to worry about it too much.
Valerie Hau7618b232020-01-09 16:03:08 -0800852 forceSendCallbacks();
Robert Carr321e83c2019-08-19 15:49:30 -0700853 mCurrentState.callbackHandles = {};
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800854 return flags;
855 }
856
857 if (mChildrenChanged) {
858 flags |= eVisibleRegion;
859 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -0700860 }
861
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800862 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +0000863 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -0700864 if (!applyPendingStates(&c)) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800865 return flags;
Marissa Wall61c58622018-07-18 10:12:20 -0700866 }
867
868 flags = doTransactionResize(flags, &c);
869
Alec Mourib416efd2018-09-06 21:01:59 +0000870 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700871
872 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800873 // invalidate and recompute the visible regions if needed
874 flags |= Layer::eVisibleRegion;
875 }
876
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700877 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800878 // invalidate and recompute the visible regions if needed
879 flags |= eVisibleRegion;
880 this->contentDirty = true;
881
882 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -0700883 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -0700884 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -0800885 }
886
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800887 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -0700888 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800889 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -0700890 }
891
Mathias Agopian13127d82013-03-05 17:47:11 -0800892 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -0700893 commitTransaction(c);
Vishnu Nair8406fd72019-07-30 11:29:31 -0700894 mPendingStatesSnapshot = mPendingStates;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800895 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -0800896 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800897}
898
Pablo Ceballos05289c22016-04-14 15:49:55 -0700899void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800900 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700901}
902
Mathias Agopian13127d82013-03-05 17:47:11 -0800903uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800904 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800905}
906
907uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800908 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -0800909}
910
chaviw214c89d2019-09-04 16:03:53 -0700911bool Layer::setPosition(float x, float y) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800912 if (mCurrentState.requested_legacy.transform.tx() == x &&
913 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -0800914 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800915 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -0700916
917 // We update the requested and active position simultaneously because
918 // we want to apply the position portion of the transform matrix immediately,
919 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800920 mCurrentState.requested_legacy.transform.set(x, y);
chaviw214c89d2019-09-04 16:03:53 -0700921 // Here we directly update the active state
922 // unlike other setters, because we store it within
923 // the transform, but use different latching rules.
924 // b/38182305
925 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr69663fb2016-03-27 19:59:19 -0700926
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800927 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800928 setTransactionFlags(eTransactionNeeded);
929 return true;
930}
Robert Carr82364e32016-05-15 11:27:47 -0700931
Robert Carr1f0a16a2016-10-24 16:27:39 -0700932bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
933 ssize_t idx = mCurrentChildren.indexOf(childLayer);
934 if (idx < 0) {
935 return false;
936 }
937 if (childLayer->setLayer(z)) {
938 mCurrentChildren.removeAt(idx);
939 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800940 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700941 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800942 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700943}
944
Robert Carr503c7042017-09-27 15:06:08 -0700945bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
946 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
947 ssize_t idx = mCurrentChildren.indexOf(childLayer);
948 if (idx < 0) {
949 return false;
950 }
951 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
952 mCurrentChildren.removeAt(idx);
953 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800954 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700955 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800956 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700957}
958
Robert Carrae060832016-11-28 10:51:00 -0800959bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800960 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
961 mCurrentState.sequence++;
962 mCurrentState.z = z;
963 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700964
965 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800966 if (mCurrentState.zOrderRelativeOf != nullptr) {
967 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700968 if (strongRelative != nullptr) {
969 strongRelative->removeZOrderRelative(this);
970 }
chaviw606e5cf2019-03-01 10:12:10 -0800971 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -0700972 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800973 setTransactionFlags(eTransactionNeeded);
974 return true;
975}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700976
Robert Carrdb66e622017-04-10 16:55:57 -0700977void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800978 mCurrentState.zOrderRelatives.remove(relative);
979 mCurrentState.sequence++;
980 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700981 setTransactionFlags(eTransactionNeeded);
982}
983
984void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800985 mCurrentState.zOrderRelatives.add(relative);
986 mCurrentState.modified = true;
987 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -0700988 setTransactionFlags(eTransactionNeeded);
989}
990
chaviw606e5cf2019-03-01 10:12:10 -0800991void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
992 mCurrentState.zOrderRelativeOf = relativeOf;
993 mCurrentState.sequence++;
994 mCurrentState.modified = true;
chaviwbdb8b802019-10-14 09:17:12 -0700995 mCurrentState.isRelativeOf = relativeOf != nullptr;
996
chaviw606e5cf2019-03-01 10:12:10 -0800997 setTransactionFlags(eTransactionNeeded);
998}
999
Robert Carr503d2bd2017-12-04 15:49:47 -08001000bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001001 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1002 if (handle == nullptr) {
1003 return false;
1004 }
1005 sp<Layer> relative = handle->owner.promote();
1006 if (relative == nullptr) {
1007 return false;
1008 }
1009
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001010 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1011 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001012 return false;
1013 }
1014
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001015 mCurrentState.sequence++;
1016 mCurrentState.modified = true;
1017 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001018
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001019 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -07001020 if (oldZOrderRelativeOf != nullptr) {
1021 oldZOrderRelativeOf->removeZOrderRelative(this);
1022 }
chaviw606e5cf2019-03-01 10:12:10 -08001023 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -07001024 relative->addZOrderRelative(this);
1025
1026 setTransactionFlags(eTransactionNeeded);
1027
1028 return true;
1029}
1030
Mathias Agopian13127d82013-03-05 17:47:11 -08001031bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001032 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001033 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001034 mCurrentState.requested_legacy.w = w;
1035 mCurrentState.requested_legacy.h = h;
1036 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001037 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001038
1039 // record the new size, from this point on, when the client request
1040 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001041 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001042 return true;
1043}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001044bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001045 if (mCurrentState.color.a == alpha) return false;
1046 mCurrentState.sequence++;
1047 mCurrentState.color.a = alpha;
1048 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001049 setTransactionFlags(eTransactionNeeded);
1050 return true;
1051}
chaviw13fdc492017-06-27 12:40:18 -07001052
Valerie Haudd0b7572019-01-29 14:59:27 -08001053bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1054 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001055 return false;
Valerie Hauaa194562019-02-05 16:21:38 -08001056 }
1057 mCurrentState.sequence++;
1058 mCurrentState.modified = true;
1059 setTransactionFlags(eTransactionNeeded);
1060
1061 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001062 // create background color layer if one does not yet exist
1063 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001064 std::string name = mName + "BackgroundColorLayer";
Lloyd Pique958be492019-03-28 15:34:59 -07001065 mCurrentState.bgColorLayer = mFlinger->getFactory().createColorLayer(
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001066 LayerCreationArgs(mFlinger.get(), nullptr, std::move(name), 0, 0, flags,
1067 LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -07001068
Valerie Haudd0b7572019-01-29 14:59:27 -08001069 // add to child list
1070 addChild(mCurrentState.bgColorLayer);
1071 mFlinger->mLayersAdded = true;
1072 // set up SF to handle added color layer
1073 if (isRemovedFromCurrentState()) {
1074 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1075 }
1076 mFlinger->setTransactionFlags(eTransactionNeeded);
1077 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1078 mCurrentState.bgColorLayer->reparent(nullptr);
1079 mCurrentState.bgColorLayer = nullptr;
1080 return true;
1081 }
1082
1083 mCurrentState.bgColorLayer->setColor(color);
1084 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1085 mCurrentState.bgColorLayer->setAlpha(alpha);
1086 mCurrentState.bgColorLayer->setDataspace(dataspace);
1087
chaviw13fdc492017-06-27 12:40:18 -07001088 return true;
1089}
1090
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001091bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001092 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001093
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001094 mCurrentState.sequence++;
1095 mCurrentState.cornerRadius = cornerRadius;
1096 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001097 setTransactionFlags(eTransactionNeeded);
1098 return true;
1099}
1100
Robert Carrd4ae7f32018-06-07 16:10:57 -07001101bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1102 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001103 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001104 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1105
1106 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1107 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1108 return false;
1109 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001110 mCurrentState.sequence++;
1111 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1112 matrix.dsdy);
1113 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001114 setTransactionFlags(eTransactionNeeded);
1115 return true;
1116}
Marissa Wall61c58622018-07-18 10:12:20 -07001117
Mathias Agopian13127d82013-03-05 17:47:11 -08001118bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001119 mCurrentState.requestedTransparentRegion_legacy = transparent;
1120 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001121 setTransactionFlags(eTransactionNeeded);
1122 return true;
1123}
Ana Krulecc84d09b2019-11-02 23:10:29 +01001124
Mathias Agopian13127d82013-03-05 17:47:11 -08001125bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001126 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1127 if (mCurrentState.flags == newFlags) return false;
1128 mCurrentState.sequence++;
1129 mCurrentState.flags = newFlags;
1130 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001131 setTransactionFlags(eTransactionNeeded);
1132 return true;
1133}
Robert Carr99e27f02016-06-16 15:18:02 -07001134
chaviw214c89d2019-09-04 16:03:53 -07001135bool Layer::setCrop_legacy(const Rect& crop) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001136 if (mCurrentState.requestedCrop_legacy == crop) return false;
1137 mCurrentState.sequence++;
1138 mCurrentState.requestedCrop_legacy = crop;
chaviw214c89d2019-09-04 16:03:53 -07001139 mCurrentState.crop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001140
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001141 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001142 setTransactionFlags(eTransactionNeeded);
1143 return true;
1144}
Robert Carr8d5227b2017-03-16 15:41:03 -07001145
Robert Carrc3574f72016-03-24 12:19:32 -07001146bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001147 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001148 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001149 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001150 return true;
1151}
1152
Evan Roskyef876c92019-01-25 17:46:06 -08001153bool Layer::setMetadata(const LayerMetadata& data) {
1154 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001155 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001156 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001157 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001158 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001159}
1160
Mathias Agopian13127d82013-03-05 17:47:11 -08001161bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001162 if (mCurrentState.layerStack == layerStack) return false;
1163 mCurrentState.sequence++;
1164 mCurrentState.layerStack = layerStack;
1165 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001166 setTransactionFlags(eTransactionNeeded);
1167 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001168}
1169
Peiyong Linc502cb72019-03-01 15:00:23 -08001170bool Layer::setColorSpaceAgnostic(const bool agnostic) {
1171 if (mCurrentState.colorSpaceAgnostic == agnostic) {
1172 return false;
1173 }
1174 mCurrentState.sequence++;
1175 mCurrentState.colorSpaceAgnostic = agnostic;
1176 mCurrentState.modified = true;
1177 setTransactionFlags(eTransactionNeeded);
1178 return true;
1179}
1180
Ana Krulecc84d09b2019-11-02 23:10:29 +01001181bool Layer::setFrameRateSelectionPriority(int32_t priority) {
1182 if (mCurrentState.frameRateSelectionPriority == priority) return false;
1183 mCurrentState.frameRateSelectionPriority = priority;
1184 mCurrentState.sequence++;
1185 mCurrentState.modified = true;
1186 setTransactionFlags(eTransactionNeeded);
1187 return true;
1188}
1189
1190int32_t Layer::getFrameRateSelectionPriority() const {
1191 // Check if layer has priority set.
1192 if (mDrawingState.frameRateSelectionPriority != PRIORITY_UNSET) {
1193 return mDrawingState.frameRateSelectionPriority;
1194 }
1195 // If not, search whether its parents have it set.
1196 sp<Layer> parent = getParent();
1197 if (parent != nullptr) {
1198 return parent->getFrameRateSelectionPriority();
1199 }
1200
1201 return Layer::PRIORITY_UNSET;
1202}
1203
Robert Carr1f0a16a2016-10-24 16:27:39 -07001204uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001205 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001206 if (p == nullptr) {
1207 return getDrawingState().layerStack;
1208 }
1209 return p->getLayerStack();
1210}
1211
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001212bool Layer::setShadowRadius(float shadowRadius) {
1213 if (mCurrentState.shadowRadius == shadowRadius) {
1214 return false;
1215 }
1216
1217 mCurrentState.sequence++;
1218 mCurrentState.shadowRadius = shadowRadius;
1219 mCurrentState.modified = true;
1220 setTransactionFlags(eTransactionNeeded);
1221 return true;
1222}
1223
Marissa Wallf58c14b2018-07-24 10:50:43 -07001224void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Alec Mourie60041e2019-06-14 18:59:51 -07001225 ATRACE_CALL();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001226 mCurrentState.barrierLayer_legacy = barrierLayer;
1227 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001228 // We don't set eTransactionNeeded, because just receiving a deferral
1229 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001230 mCurrentState.modified = true;
1231 pushPendingState();
1232 mCurrentState.barrierLayer_legacy = nullptr;
1233 mCurrentState.frameNumber_legacy = 0;
1234 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001235}
1236
Marissa Wallf58c14b2018-07-24 10:50:43 -07001237void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001238 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001239 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001240}
1241
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001242// ----------------------------------------------------------------------------
1243// pageflip handling...
1244// ----------------------------------------------------------------------------
1245
Robert Carr1f0a16a2016-10-24 16:27:39 -07001246bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001247 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001248 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001249 if (parent != nullptr && parent->isHiddenByPolicy()) {
1250 return true;
1251 }
Robert Carr1c5481e2019-07-01 14:42:27 -07001252 if (usingRelativeZ(LayerVector::StateSet::Drawing)) {
1253 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
1254 if (zOrderRelativeOf != nullptr) {
1255 if (zOrderRelativeOf->isHiddenByPolicy()) {
1256 return true;
1257 }
1258 }
1259 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001260 return s.flags & layer_state_t::eLayerHidden;
1261}
1262
David Sodman41fdfc92017-11-06 16:09:56 -08001263uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001264 // TODO: should we do something special if mSecure is set?
1265 if (mProtectedByApp) {
1266 // need a hardware-protected path to external video sink
1267 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001268 }
Riley Andrews03414a12014-07-01 14:22:59 -07001269 if (mPotentialCursor) {
1270 usage |= GraphicBuffer::USAGE_CURSOR;
1271 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001272 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001273 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001274}
1275
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001276void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001277 uint32_t orientation = 0;
Vishnu Nair5eb3f062019-04-08 08:21:03 -07001278 // Disable setting transform hint if the debug flag is set.
1279 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001280 // The transform hint is used to improve performance, but we can
1281 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001282 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001283 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001284 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001285 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001286 orientation = 0;
1287 }
1288 }
David Sodmaneb085e02017-10-05 18:49:04 -07001289 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001290}
1291
Mathias Agopian13127d82013-03-05 17:47:11 -08001292// ----------------------------------------------------------------------------
1293// debugging
1294// ----------------------------------------------------------------------------
1295
Marissa Wall61c58622018-07-18 10:12:20 -07001296// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001297LayerDebugInfo Layer::getLayerDebugInfo() const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001298 using namespace std::string_literals;
1299
Kalle Raitaa099a242017-01-11 11:17:29 -08001300 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001301 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001302 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001303 sp<Layer> parent = getParent();
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001304 info.mParentName = parent ? parent->getName() : "none"s;
chaviw8a01fa42019-08-19 12:39:31 -07001305 info.mType = getType();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001306 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Lloyd Piquea2468662019-03-07 21:31:06 -08001307
1308 info.mVisibleRegion = debugGetVisibleRegionOnDefaultDisplay();
Kalle Raitaa099a242017-01-11 11:17:29 -08001309 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001310 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001311 info.mX = ds.active_legacy.transform.tx();
1312 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001313 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001314 info.mWidth = ds.active_legacy.w;
1315 info.mHeight = ds.active_legacy.h;
1316 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001317 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001318 info.mFlags = ds.flags;
1319 info.mPixelFormat = getPixelFormat();
chaviw4244e032019-09-04 11:27:49 -07001320 info.mDataSpace = static_cast<android_dataspace>(getDataSpace());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001321 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1322 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1323 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1324 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001325 {
chaviwd62d3062019-09-04 14:48:02 -07001326 sp<const GraphicBuffer> buffer = getBuffer();
David Sodman5b4cffc2017-11-23 13:20:29 -08001327 if (buffer != 0) {
1328 info.mActiveBufferWidth = buffer->getWidth();
1329 info.mActiveBufferHeight = buffer->getHeight();
1330 info.mActiveBufferStride = buffer->getStride();
1331 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001332 } else {
1333 info.mActiveBufferWidth = 0;
1334 info.mActiveBufferHeight = 0;
1335 info.mActiveBufferStride = 0;
1336 info.mActiveBufferFormat = 0;
1337 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001338 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001339 info.mNumQueuedFrames = getQueuedFrameCount();
1340 info.mRefreshPending = isBufferLatched();
1341 info.mIsOpaque = isOpaque(ds);
1342 info.mContentDirty = contentDirty;
1343 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001344}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001345
Yiwei Zhang5434a782018-12-05 18:06:32 -08001346void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001347 result.append("-------------------------------");
1348 result.append("-------------------------------");
1349 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001350 result.append(" Layer name\n");
1351 result.append(" Z | ");
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001352 result.append(" Window Type | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001353 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001354 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001355 result.append(" Disp Frame (LTRB) | ");
1356 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001357 result.append("-------------------------------");
1358 result.append("-------------------------------");
1359 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001360}
1361
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001362void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1363 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1364 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001365 return;
1366 }
1367
Yiwei Zhang5434a782018-12-05 18:06:32 -08001368 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001369 if (mName.length() > 77) {
1370 std::string shortened;
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001371 shortened.append(mName, 0, 36);
Dan Stozae22aec72016-08-01 13:20:59 -07001372 shortened.append("[...]");
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001373 shortened.append(mName, mName.length() - 36);
1374 name = std::move(shortened);
Dan Stozae22aec72016-08-01 13:20:59 -07001375 } else {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001376 name = mName;
Dan Stozae22aec72016-08-01 13:20:59 -07001377 }
1378
Yiwei Zhang5434a782018-12-05 18:06:32 -08001379 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001380
Alec Mourib416efd2018-09-06 21:01:59 +00001381 const State& layerState(getDrawingState());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001382 const auto& compositionState = outputLayer->getState();
1383
Chia-I Wu1e043612018-03-01 09:45:09 -08001384 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001385 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001386 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001387 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001388 }
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001389 StringAppendF(&result, " %10d | ", mWindowType);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001390 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
1391 StringAppendF(&result, "%10s | ",
1392 toString(getCompositionLayer() ? compositionState.bufferTransform
1393 : static_cast<Hwc2::Transform>(0))
1394 .c_str());
1395 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001396 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001397 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001398 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1399 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001400
Yichi Chen6ca35192018-05-29 12:20:43 +08001401 result.append("- - - - - - - - - - - - - - - -");
1402 result.append("- - - - - - - - - - - - - - - -");
1403 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001404}
Dan Stozae22aec72016-08-01 13:20:59 -07001405
Yiwei Zhang5434a782018-12-05 18:06:32 -08001406void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001407 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001408}
1409
Svetoslavd85084b2014-03-20 10:28:31 -07001410void Layer::clearFrameStats() {
1411 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001412}
1413
Jamie Gennis6547ff42013-07-16 20:12:42 -07001414void Layer::logFrameStats() {
1415 mFrameTracker.logAndResetStats(mName);
1416}
1417
Svetoslavd85084b2014-03-20 10:28:31 -07001418void Layer::getFrameStats(FrameStats* outStats) const {
1419 mFrameTracker.getStats(outStats);
1420}
1421
Yiwei Zhang5434a782018-12-05 18:06:32 -08001422void Layer::dumpFrameEvents(std::string& result) {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001423 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().c_str(), getType(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001424 Mutex::Autolock lock(mFrameEventHistoryMutex);
1425 mFrameEventHistory.checkFencesForCompletion();
1426 mFrameEventHistory.dump(result);
1427}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001428
Vishnu Nair0f085c62019-08-30 08:49:12 -07001429void Layer::dumpCallingUidPid(std::string& result) const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001430 StringAppendF(&result, "Layer %s (%s) pid:%d uid:%d\n", getName().c_str(), getType(),
Vishnu Nair0f085c62019-08-30 08:49:12 -07001431 mCallingPid, mCallingUid);
1432}
1433
Brian Anderson5ea5e592016-12-01 16:54:33 -08001434void Layer::onDisconnect() {
1435 Mutex::Autolock lock(mFrameEventHistoryMutex);
1436 mFrameEventHistory.onDisconnect();
Yiwei Zhang1a88c402019-11-18 10:43:58 -08001437 const int32_t layerId = getSequence();
1438 mFlinger->mTimeStats->onDestroy(layerId);
1439 mFlinger->mFrameTracer->onDestroy(layerId);
Brian Anderson5ea5e592016-12-01 16:54:33 -08001440}
1441
Brian Anderson3890c392016-07-25 12:48:08 -07001442void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001443 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001444 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001445 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1446 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhang487340f2019-11-18 17:42:12 -08001447 mFlinger->mTimeStats->setAcquireFence(getSequence(), newTimestamps->frameNumber,
1448 newTimestamps->acquireFence);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001449 }
1450
Brian Andersond6927fb2016-07-23 23:37:30 -07001451 Mutex::Autolock lock(mFrameEventHistoryMutex);
1452 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001453 // If there are any unsignaled fences in the aquire timeline at this
1454 // point, the previously queued frame hasn't been latched yet. Go ahead
1455 // and try to get the signal time here so the syscall is taken out of
1456 // the main thread's critical path.
1457 mAcquireTimeline.updateSignalTimes();
1458 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001459 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001460 mFrameEventHistory.addQueue(*newTimestamps);
1461 }
1462
Brian Anderson3890c392016-07-25 12:48:08 -07001463 if (outDelta) {
1464 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001465 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001466}
Dan Stozae77c7662016-05-13 11:37:28 -07001467
Chia-I Wu98f1c102017-05-30 14:54:08 -07001468size_t Layer::getChildrenCount() const {
1469 size_t count = 0;
1470 for (const sp<Layer>& child : mCurrentChildren) {
1471 count += 1 + child->getChildrenCount();
1472 }
1473 return count;
1474}
1475
Robert Carr1f0a16a2016-10-24 16:27:39 -07001476void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001477 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001478 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001479
Robert Carr1f0a16a2016-10-24 16:27:39 -07001480 mCurrentChildren.add(layer);
1481 layer->setParent(this);
1482}
1483
1484ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001485 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001486 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001487
Robert Carr1323c952019-01-28 18:13:27 -08001488 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001489 return mCurrentChildren.remove(layer);
1490}
1491
Robert Carr1db73f62016-12-21 12:58:51 -08001492bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1493 sp<Handle> handle = nullptr;
1494 sp<Layer> newParent = nullptr;
1495 if (newParentHandle == nullptr) {
1496 return false;
1497 }
1498 handle = static_cast<Handle*>(newParentHandle.get());
1499 newParent = handle->owner.promote();
1500 if (newParent == nullptr) {
1501 ALOGE("Unable to promote Layer handle");
1502 return false;
1503 }
1504
chaviw5aedec92018-10-22 10:40:38 -07001505 if (attachChildren()) {
1506 setTransactionFlags(eTransactionNeeded);
1507 }
Robert Carr1db73f62016-12-21 12:58:51 -08001508 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001509 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001510 }
1511 mCurrentChildren.clear();
1512
1513 return true;
1514}
1515
Robert Carr15eae092018-03-23 13:43:53 -07001516void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001517 for (const sp<Layer>& child : mDrawingChildren) {
1518 child->mDrawingParent = newParent;
Vishnu Nairc652ff82019-03-15 12:48:54 -07001519 child->computeBounds(newParent->mBounds,
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001520 newParent->getTransformWithScale(newParent->getBufferScaleTransform()),
1521 newParent->mEffectiveShadowRadius);
Robert Carr578038f2018-03-09 12:25:24 -08001522 }
1523}
1524
chaviwf1961f72017-09-18 16:41:07 -07001525bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001526 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001527
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001528 // While layers are detached, we allow most operations
1529 // and simply halt performing the actual transaction. However
1530 // for reparent != null we would enter the mRemovedFromCurrentState
1531 // state, regardless of whether doTransaction was called, and
1532 // so we need to prevent the update here.
1533 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001534 return false;
1535 }
1536
Robert Carr54cf5b12019-01-25 14:02:28 -08001537 sp<Layer> newParent;
1538 if (newParentHandle != nullptr) {
1539 auto handle = static_cast<Handle*>(newParentHandle.get());
1540 newParent = handle->owner.promote();
1541 if (newParent == nullptr) {
1542 ALOGE("Unable to promote Layer handle");
1543 return false;
1544 }
1545 if (newParent == this) {
1546 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1547 return false;
1548 }
1549 }
1550
chaviwf1961f72017-09-18 16:41:07 -07001551 sp<Layer> parent = getParent();
1552 if (parent != nullptr) {
1553 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001554 }
1555
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001556 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001557 newParent->addChild(this);
1558 if (!newParent->isRemovedFromCurrentState()) {
1559 addToCurrentState();
1560 } else {
1561 onRemovedFromCurrentState();
1562 }
1563
1564 if (mLayerDetached) {
1565 mLayerDetached = false;
1566 callSetTransactionFlags = true;
1567 }
1568 } else {
1569 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001570 }
1571
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001572 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001573 setTransactionFlags(eTransactionNeeded);
1574 }
chaviw06178942017-07-27 10:25:59 -07001575 return true;
1576}
1577
Robert Carr9524cb32017-02-13 11:32:32 -08001578bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001579 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001580 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001581 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001582 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001583 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001584 child->detachChildren();
chaviw43cb3cb2019-05-31 15:23:41 -07001585 child->removeRemoteSyncPoints();
Robert Carr9524cb32017-02-13 11:32:32 -08001586 }
Robert Carr7f619b22017-11-06 12:56:35 -08001587 }
Robert Carr9524cb32017-02-13 11:32:32 -08001588
1589 return true;
1590}
1591
chaviw5aedec92018-10-22 10:40:38 -07001592bool Layer::attachChildren() {
1593 bool changed = false;
1594 for (const sp<Layer>& child : mCurrentChildren) {
1595 sp<Client> parentClient = mClientRef.promote();
1596 sp<Client> client(child->mClientRef.promote());
1597 if (client != nullptr && parentClient != client) {
1598 if (child->mLayerDetached) {
1599 child->mLayerDetached = false;
1600 changed = true;
1601 }
1602 changed |= child->attachChildren();
1603 }
1604 }
1605
1606 return changed;
1607}
1608
Peiyong Lind3788632018-09-18 16:01:31 -07001609bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001610 static const mat4 identityMatrix = mat4();
1611
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001612 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001613 return false;
1614 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001615 ++mCurrentState.sequence;
1616 mCurrentState.colorTransform = matrix;
1617 mCurrentState.hasColorTransform = matrix != identityMatrix;
1618 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001619 setTransactionFlags(eTransactionNeeded);
1620 return true;
1621}
1622
chaviwf66724d2018-11-28 16:35:21 -08001623mat4 Layer::getColorTransform() const {
1624 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1625 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1626 colorTransform = parent->getColorTransform() * colorTransform;
1627 }
1628 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001629}
1630
1631bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001632 bool hasColorTransform = getDrawingState().hasColorTransform;
1633 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1634 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1635 }
1636 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001637}
1638
Chia-I Wu11481472018-05-04 10:43:19 -07001639bool Layer::isLegacyDataSpace() const {
1640 // return true when no higher bits are set
chaviw4244e032019-09-04 11:27:49 -07001641 return !(getDataSpace() &
1642 (ui::Dataspace::STANDARD_MASK | ui::Dataspace::TRANSFER_MASK |
1643 ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001644}
1645
Robert Carr1f0a16a2016-10-24 16:27:39 -07001646void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001647 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001648}
1649
chaviw301b1d82019-11-06 13:15:09 -08001650int32_t Layer::getZ(LayerVector::StateSet stateSet) const {
1651 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1652 const State& state = useDrawing ? mDrawingState : mCurrentState;
1653 return state.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001654}
1655
Robert Carr1c5481e2019-07-01 14:42:27 -07001656bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) const {
Robert Carr29abff82017-12-04 13:51:20 -08001657 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001658 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviwe5ac40f2019-09-24 16:36:55 -07001659 return state.isRelativeOf;
Robert Carr29abff82017-12-04 13:51:20 -08001660}
1661
David Sodman41fdfc92017-11-06 16:09:56 -08001662__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001663 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001664 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1665 "makeTraversalList received invalid stateSet");
1666 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1667 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001668 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001669
Robert Carr29abff82017-12-04 13:51:20 -08001670 if (state.zOrderRelatives.size() == 0) {
1671 *outSkipRelativeZUsers = true;
1672 return children;
1673 }
1674
chaviwfd462612018-05-31 16:11:27 -07001675 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001676 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001677 sp<Layer> strongRelative = weakRelative.promote();
1678 if (strongRelative != nullptr) {
1679 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001680 }
1681 }
1682
Dan Stoza412903f2017-04-27 13:42:17 -07001683 for (const sp<Layer>& child : children) {
chaviwe5ac40f2019-09-24 16:36:55 -07001684 if (child->usingRelativeZ(stateSet)) {
Robert Carr503c7042017-09-27 15:06:08 -07001685 continue;
1686 }
Robert Carrdb66e622017-04-10 16:55:57 -07001687 traverse.add(child);
1688 }
1689
1690 return traverse;
1691}
1692
Robert Carr1f0a16a2016-10-24 16:27:39 -07001693/**
Robert Carrdb66e622017-04-10 16:55:57 -07001694 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001695 */
Dan Stoza412903f2017-04-27 13:42:17 -07001696void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001697 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1698 // produce a new list for traversing, including our relatives, and not including our children
1699 // who are relatives of another surface. In the case that there are no relative Z,
1700 // makeTraversalList returns our children directly to avoid significant overhead.
1701 // However in this case we need to take the responsibility for filtering children which
1702 // are relatives of another surface here.
1703 bool skipRelativeZUsers = false;
1704 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001705
Robert Carr1f0a16a2016-10-24 16:27:39 -07001706 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001707 for (; i < list.size(); i++) {
1708 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001709 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1710 continue;
1711 }
1712
chaviw301b1d82019-11-06 13:15:09 -08001713 if (relative->getZ(stateSet) >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001714 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001715 }
Dan Stoza412903f2017-04-27 13:42:17 -07001716 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001717 }
Robert Carr29abff82017-12-04 13:51:20 -08001718
Dan Stoza412903f2017-04-27 13:42:17 -07001719 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001720 for (; i < list.size(); i++) {
1721 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001722
Robert Carr29abff82017-12-04 13:51:20 -08001723 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1724 continue;
1725 }
Dan Stoza412903f2017-04-27 13:42:17 -07001726 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001727 }
1728}
1729
1730/**
Robert Carrdb66e622017-04-10 16:55:57 -07001731 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001732 */
Dan Stoza412903f2017-04-27 13:42:17 -07001733void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1734 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001735 // See traverseInZOrder for documentation.
1736 bool skipRelativeZUsers = false;
1737 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001738
Robert Carr1f0a16a2016-10-24 16:27:39 -07001739 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001740 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001741 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001742
1743 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1744 continue;
1745 }
1746
chaviw301b1d82019-11-06 13:15:09 -08001747 if (relative->getZ(stateSet) < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001748 break;
1749 }
Dan Stoza412903f2017-04-27 13:42:17 -07001750 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001751 }
Dan Stoza412903f2017-04-27 13:42:17 -07001752 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001753 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001754 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001755
1756 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1757 continue;
1758 }
1759
Dan Stoza412903f2017-04-27 13:42:17 -07001760 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001761 }
1762}
1763
chaviw4b129c22018-04-09 16:19:43 -07001764LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1765 const std::vector<Layer*>& layersInTree) {
1766 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1767 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001768 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1769 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001770 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001771
chaviwfd462612018-05-31 16:11:27 -07001772 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001773 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1774 sp<Layer> strongRelative = weakRelative.promote();
1775 // Only add relative layers that are also descendents of the top most parent of the tree.
1776 // If a relative layer is not a descendent, then it should be ignored.
1777 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1778 traverse.add(strongRelative);
1779 }
1780 }
1781
1782 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001783 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001784 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1785 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1786 // the child here since it won't be added later as a relative.
1787 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1788 childState.zOrderRelativeOf.promote().get())) {
1789 continue;
1790 }
1791 traverse.add(child);
1792 }
1793
1794 return traverse;
1795}
1796
1797void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1798 LayerVector::StateSet stateSet,
1799 const LayerVector::Visitor& visitor) {
1800 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001801
1802 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001803 for (; i < list.size(); i++) {
1804 const auto& relative = list[i];
chaviw301b1d82019-11-06 13:15:09 -08001805 if (relative->getZ(stateSet) >= 0) {
chaviwa76b2712017-09-20 12:02:26 -07001806 break;
1807 }
chaviw4b129c22018-04-09 16:19:43 -07001808 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001809 }
chaviw4b129c22018-04-09 16:19:43 -07001810
chaviwa76b2712017-09-20 12:02:26 -07001811 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001812 for (; i < list.size(); i++) {
1813 const auto& relative = list[i];
1814 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001815 }
1816}
1817
chaviw4b129c22018-04-09 16:19:43 -07001818std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1819 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1820 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1821
1822 std::vector<Layer*> layersInTree = {this};
1823 for (size_t i = 0; i < children.size(); i++) {
1824 const auto& child = children[i];
1825 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1826 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1827 }
1828
1829 return layersInTree;
1830}
1831
1832void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1833 const LayerVector::Visitor& visitor) {
1834 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1835 std::sort(layersInTree.begin(), layersInTree.end());
1836 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1837}
1838
Peiyong Linefefaac2018-08-17 12:27:51 -07001839ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001840 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001841}
1842
chaviw13fdc492017-06-27 12:40:18 -07001843half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001844 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001845
chaviw13fdc492017-06-27 12:40:18 -07001846 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1847 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001848}
Robert Carr6452f122017-03-21 10:41:29 -07001849
chaviw13fdc492017-06-27 12:40:18 -07001850half4 Layer::getColor() const {
1851 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001852 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001853}
Robert Carr6452f122017-03-21 10:41:29 -07001854
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001855Layer::RoundedCornerState Layer::getRoundedCornerState() const {
1856 const auto& p = mDrawingParent.promote();
1857 if (p != nullptr) {
Peiyong Lin27016a92019-03-29 17:36:08 +00001858 RoundedCornerState parentState = p->getRoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001859 if (parentState.radius > 0) {
1860 ui::Transform t = getActiveTransform(getDrawingState());
1861 t = t.inverse();
1862 parentState.cropRect = t.transform(parentState.cropRect);
1863 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1864 // but a transform matrix can define horizontal and vertical scales.
1865 // Let's take the average between both of them and pass into the shader, practically we
1866 // never do this type of transformation on windows anyway.
1867 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
1868 return parentState;
1869 }
1870 }
1871 const float radius = getDrawingState().cornerRadius;
Peiyong Linb9ff23e2019-04-08 11:04:59 -07001872 return radius > 0 && getCrop(getDrawingState()).isValid()
1873 ? RoundedCornerState(getCrop(getDrawingState()).toFloatRect(), radius)
1874 : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001875}
1876
Vishnu Nair08f6eae2019-11-26 14:01:39 -08001877renderengine::ShadowSettings Layer::getShadowSettings(const Rect& viewport) const {
1878 renderengine::ShadowSettings state = mFlinger->mDrawingState.globalShadowSettings;
1879
1880 // Shift the spot light x-position to the middle of the display and then
1881 // offset it by casting layer's screen pos.
1882 state.lightPos.x = (viewport.width() / 2.f) - mScreenBounds.left;
1883 state.lightPos.y -= mScreenBounds.top;
1884
1885 state.length = mEffectiveShadowRadius;
1886 return state;
1887}
1888
Robert Carr1f0a16a2016-10-24 16:27:39 -07001889void Layer::commitChildList() {
1890 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1891 const auto& child = mCurrentChildren[i];
1892 child->commitChildList();
1893 }
1894 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001895 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001896}
1897
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001898static wp<Layer> extractLayerFromBinder(const wp<IBinder>& weakBinderHandle) {
1899 if (weakBinderHandle == nullptr) {
1900 return nullptr;
1901 }
1902 sp<IBinder> binderHandle = weakBinderHandle.promote();
1903 if (binderHandle == nullptr) {
1904 return nullptr;
1905 }
1906 sp<Layer::Handle> handle = static_cast<Layer::Handle*>(binderHandle.get());
1907 if (handle == nullptr) {
1908 return nullptr;
1909 }
1910 return handle->owner;
1911}
1912
Robert Carr720e5062018-07-30 17:45:14 -07001913void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001914 mCurrentState.inputInfo = info;
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001915 mCurrentState.touchableRegionCrop = extractLayerFromBinder(info.touchableRegionCropHandle);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001916 mCurrentState.modified = true;
1917 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07001918 setTransactionFlags(eTransactionNeeded);
1919}
1920
chaviw08f3cb22020-01-13 13:17:21 -08001921void Layer::writeToProto(LayersProto& layersProto, uint32_t traceFlags) const {
1922 LayerProto* layerProto = layersProto.add_layers();
1923 writeToProtoDrawingState(layerProto, traceFlags);
1924 writeToProtoCommonState(layerProto, LayerVector::StateSet::Drawing, traceFlags);
1925
1926 for (const sp<Layer>& layer : mDrawingChildren) {
1927 layer->writeToProto(layersProto, traceFlags);
1928 }
1929}
1930
Vishnu Nair8406fd72019-07-30 11:29:31 -07001931void Layer::writeToProtoDrawingState(LayerProto* layerInfo, uint32_t traceFlags) const {
1932 ui::Transform transform = getTransform();
1933
1934 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
1935 for (const auto& pendingState : mPendingStatesSnapshot) {
1936 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
1937 if (barrierLayer != nullptr) {
1938 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
1939 barrierLayerProto->set_id(barrierLayer->sequence);
1940 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
1941 }
1942 }
1943
chaviwd62d3062019-09-04 14:48:02 -07001944 auto buffer = getBuffer();
Vishnu Nair8406fd72019-07-30 11:29:31 -07001945 if (buffer != nullptr) {
1946 LayerProtoHelper::writeToProto(buffer,
1947 [&]() { return layerInfo->mutable_active_buffer(); });
chaviw4244e032019-09-04 11:27:49 -07001948 LayerProtoHelper::writeToProto(ui::Transform(getBufferTransform()),
Vishnu Nair8406fd72019-07-30 11:29:31 -07001949 layerInfo->mutable_buffer_transform());
1950 }
1951 layerInfo->set_invalidate(contentDirty);
1952 layerInfo->set_is_protected(isProtected());
chaviw4244e032019-09-04 11:27:49 -07001953 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(getDataSpace())));
Vishnu Nair8406fd72019-07-30 11:29:31 -07001954 layerInfo->set_queued_frames(getQueuedFrameCount());
1955 layerInfo->set_refresh_pending(isBufferLatched());
1956 layerInfo->set_curr_frame(mCurrentFrameNumber);
1957 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
1958
1959 layerInfo->set_corner_radius(getRoundedCornerState().radius);
1960 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1961 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
1962 [&]() { return layerInfo->mutable_position(); });
1963 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
Lloyd Piquea2468662019-03-07 21:31:06 -08001964 LayerProtoHelper::writeToProto(debugGetVisibleRegionOnDefaultDisplay(),
Vishnu Nair8406fd72019-07-30 11:29:31 -07001965 [&]() { return layerInfo->mutable_visible_region(); });
1966 LayerProtoHelper::writeToProto(surfaceDamageRegion,
1967 [&]() { return layerInfo->mutable_damage_region(); });
chaviwddeae262020-01-06 10:31:23 -08001968
1969 if (hasColorTransform()) {
1970 LayerProtoHelper::writeToProto(getColorTransform(),
1971 layerInfo->mutable_color_transform());
1972 }
Vishnu Nair8406fd72019-07-30 11:29:31 -07001973 }
1974
1975 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
1976 LayerProtoHelper::writeToProto(mSourceBounds,
1977 [&]() { return layerInfo->mutable_source_bounds(); });
1978 LayerProtoHelper::writeToProto(mScreenBounds,
1979 [&]() { return layerInfo->mutable_screen_bounds(); });
Vishnu Nair95a1ed42019-12-06 12:25:11 -08001980 LayerProtoHelper::writeToProto(getRoundedCornerState().cropRect,
1981 [&]() { return layerInfo->mutable_corner_radius_crop(); });
1982 layerInfo->set_shadow_radius(mEffectiveShadowRadius);
Vishnu Nair8406fd72019-07-30 11:29:31 -07001983 }
1984}
1985
1986void Layer::writeToProtoCommonState(LayerProto* layerInfo, LayerVector::StateSet stateSet,
1987 uint32_t traceFlags) const {
chaviw1d044282017-09-27 12:19:28 -07001988 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1989 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001990 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07001991
Peiyong Linefefaac2018-08-17 12:27:51 -07001992 ui::Transform requestedTransform = state.active_legacy.transform;
chaviw1d044282017-09-27 12:19:28 -07001993
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001994 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
1995 layerInfo->set_id(sequence);
1996 layerInfo->set_name(getName().c_str());
chaviw8a01fa42019-08-19 12:39:31 -07001997 layerInfo->set_type(getType());
chaviw1d044282017-09-27 12:19:28 -07001998
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001999 for (const auto& child : children) {
2000 layerInfo->add_children(child->sequence);
chaviw1d044282017-09-27 12:19:28 -07002001 }
chaviw1d044282017-09-27 12:19:28 -07002002
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002003 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
2004 sp<Layer> strongRelative = weakRelative.promote();
2005 if (strongRelative != nullptr) {
2006 layerInfo->add_relatives(strongRelative->sequence);
2007 }
chaviwadc40c22018-07-10 16:57:27 -07002008 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002009
2010 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
2011 [&]() { return layerInfo->mutable_transparent_region(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002012
2013 layerInfo->set_layer_stack(getLayerStack());
2014 layerInfo->set_z(state.z);
2015
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002016 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(),
2017 [&]() {
2018 return layerInfo->mutable_requested_position();
2019 });
2020
2021 LayerProtoHelper::writeSizeToProto(state.active_legacy.w, state.active_legacy.h,
2022 [&]() { return layerInfo->mutable_size(); });
2023
2024 LayerProtoHelper::writeToProto(state.crop_legacy,
2025 [&]() { return layerInfo->mutable_crop(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002026
2027 layerInfo->set_is_opaque(isOpaque(state));
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002028
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002029
2030 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
2031 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
2032 LayerProtoHelper::writeToProto(state.color,
2033 [&]() { return layerInfo->mutable_requested_color(); });
2034 layerInfo->set_flags(state.flags);
2035
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002036 LayerProtoHelper::writeToProto(requestedTransform,
2037 layerInfo->mutable_requested_transform());
2038
2039 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
2040 if (parent != nullptr) {
2041 layerInfo->set_parent(parent->sequence);
2042 } else {
2043 layerInfo->set_parent(-1);
2044 }
2045
2046 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2047 if (zOrderRelativeOf != nullptr) {
2048 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
2049 } else {
2050 layerInfo->set_z_order_relative_of(-1);
2051 }
chaviw08f3cb22020-01-13 13:17:21 -08002052
2053 layerInfo->set_is_relative_of(state.isRelativeOf);
chaviwadc40c22018-07-10 16:57:27 -07002054 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002055
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002056 if (traceFlags & SurfaceTracing::TRACE_INPUT) {
2057 LayerProtoHelper::writeToProto(state.inputInfo, state.touchableRegionCrop,
2058 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002059 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002060
2061 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
2062 auto protoMap = layerInfo->mutable_metadata();
2063 for (const auto& entry : state.metadata.mMap) {
2064 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
2065 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002066 }
chaviw1d044282017-09-27 12:19:28 -07002067}
2068
Robert Carr2e102c92018-10-23 12:11:15 -07002069bool Layer::isRemovedFromCurrentState() const {
2070 return mRemovedFromCurrentState;
2071}
2072
Arthur Hungd20b2702019-01-14 18:16:16 +08002073InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002074 InputWindowInfo info = mDrawingState.inputInfo;
chaviwaf87b3e2019-10-01 16:59:28 -07002075 info.id = sequence;
Robert Carr720e5062018-07-30 17:45:14 -07002076
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002077 if (info.displayId == ADISPLAY_ID_NONE) {
chaviwb15ea8a2019-09-03 12:40:22 -07002078 info.displayId = getLayerStack();
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002079 }
2080
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002081 ui::Transform t = getTransform();
2082 const float xScale = t.sx();
2083 const float yScale = t.sy();
Ady Abraham282f1d72019-07-24 18:05:56 -07002084 int32_t xSurfaceInset = info.surfaceInset;
2085 int32_t ySurfaceInset = info.surfaceInset;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002086 if (xScale != 1.0f || yScale != 1.0f) {
Ady Abraham282f1d72019-07-24 18:05:56 -07002087 info.windowXScale *= (xScale != 0.0f) ? 1.0f / xScale : 0.0f;
2088 info.windowYScale *= (yScale != 0.0f) ? 1.0f / yScale : 0.0f;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002089 info.touchableRegion.scaleSelf(xScale, yScale);
Ady Abraham282f1d72019-07-24 18:05:56 -07002090 xSurfaceInset = std::round(xSurfaceInset * xScale);
2091 ySurfaceInset = std::round(ySurfaceInset * yScale);
Riddle Hsu39d4aa52018-11-30 20:46:53 +08002092 }
Robert Carre07e1032018-11-26 12:55:53 -08002093
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002094 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002095 // If this is a portal window, set the touchableRegion to the layerBounds.
2096 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
2097 ? getBufferSize(getDrawingState())
2098 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002099 if (!layerBounds.isValid()) {
2100 layerBounds = getCroppedBufferSize(getDrawingState());
2101 }
Vishnu Nair8033a492018-12-05 07:27:23 -08002102 layerBounds = t.transform(layerBounds);
Ady Abraham282f1d72019-07-24 18:05:56 -07002103
2104 // clamp inset to layer bounds
2105 xSurfaceInset = (xSurfaceInset >= 0) ? std::min(xSurfaceInset, layerBounds.getWidth() / 2) : 0;
2106 ySurfaceInset = (ySurfaceInset >= 0) ? std::min(ySurfaceInset, layerBounds.getHeight() / 2) : 0;
2107
Arthur Hung118b1142019-05-08 21:25:59 +08002108 layerBounds.inset(xSurfaceInset, ySurfaceInset, xSurfaceInset, ySurfaceInset);
Vishnu Nair8033a492018-12-05 07:27:23 -08002109
Arthur Hungd20b2702019-01-14 18:16:16 +08002110 // Input coordinate should match the layer bounds.
2111 info.frameLeft = layerBounds.left;
2112 info.frameTop = layerBounds.top;
2113 info.frameRight = layerBounds.right;
2114 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08002115
2116 // Position the touchable region relative to frame screen location and restrict it to frame
2117 // bounds.
2118 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08002119 info.visible = canReceiveInput();
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002120
2121 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2122 if (info.replaceTouchableRegionWithCrop) {
2123 if (cropLayer == nullptr) {
2124 info.touchableRegion = Region(Rect{mScreenBounds});
2125 } else {
2126 info.touchableRegion = Region(Rect{cropLayer->mScreenBounds});
2127 }
2128 } else if (cropLayer != nullptr) {
2129 info.touchableRegion = info.touchableRegion.intersect(Rect{cropLayer->mScreenBounds});
2130 }
2131
chaviwaf87b3e2019-10-01 16:59:28 -07002132 // If the layer is a clone, we need to crop the input region to cloned root to prevent
2133 // touches from going outside the cloned area.
2134 if (isClone()) {
2135 sp<Layer> clonedRoot = getClonedRoot();
2136 if (clonedRoot != nullptr) {
2137 Rect rect(clonedRoot->mScreenBounds);
2138 info.touchableRegion = info.touchableRegion.intersect(rect);
2139 }
2140 }
2141
Robert Carr720e5062018-07-30 17:45:14 -07002142 return info;
2143}
2144
chaviwaf87b3e2019-10-01 16:59:28 -07002145sp<Layer> Layer::getClonedRoot() {
2146 if (mClonedChild != nullptr) {
2147 return this;
2148 }
2149 if (mDrawingParent == nullptr || mDrawingParent.promote() == nullptr) {
2150 return nullptr;
2151 }
2152 return mDrawingParent.promote()->getClonedRoot();
2153}
2154
Robert Carr720e5062018-07-30 17:45:14 -07002155bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002156 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002157}
2158
Lloyd Piquefeb73d72018-12-04 17:23:44 -08002159std::shared_ptr<compositionengine::Layer> Layer::getCompositionLayer() const {
2160 return nullptr;
2161}
2162
chaviw3e727cd2019-01-31 13:41:05 -08002163bool Layer::canReceiveInput() const {
Vishnu Nair82353e92019-09-12 12:38:47 -07002164 return !isHiddenByPolicy();
chaviw3e727cd2019-01-31 13:41:05 -08002165}
2166
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002167compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
2168 const sp<const DisplayDevice>& display) const {
2169 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionLayer().get());
2170}
2171
Lloyd Piquea2468662019-03-07 21:31:06 -08002172Region Layer::debugGetVisibleRegionOnDefaultDisplay() const {
2173 sp<DisplayDevice> displayDevice = mFlinger->getDefaultDisplayDeviceLocked();
2174 if (displayDevice == nullptr) {
2175 return {};
2176 }
2177
2178 auto outputLayer = findOutputLayerForDisplay(displayDevice);
2179 if (outputLayer == nullptr) {
2180 return {};
2181 }
2182
2183 return outputLayer->getState().visibleRegion;
2184}
2185
chaviwb4c6e582019-08-16 14:35:07 -07002186void Layer::setInitialValuesForClone(const sp<Layer>& clonedFrom) {
2187 // copy drawing state from cloned layer
2188 mDrawingState = clonedFrom->mDrawingState;
2189 mClonedFrom = clonedFrom;
chaviwb4c6e582019-08-16 14:35:07 -07002190}
chaviw74b03172019-08-19 11:09:03 -07002191
2192void Layer::updateMirrorInfo() {
2193 if (mClonedChild == nullptr || !mClonedChild->isClonedFromAlive()) {
2194 // If mClonedChild is null, there is nothing to mirror. If isClonedFromAlive returns false,
2195 // it means that there is a clone, but the layer it was cloned from has been destroyed. In
2196 // that case, we want to delete the reference to the clone since we want it to get
2197 // destroyed. The root, this layer, will still be around since the client can continue
2198 // to hold a reference, but no cloned layers will be displayed.
2199 mClonedChild = nullptr;
2200 return;
2201 }
2202
2203 std::map<sp<Layer>, sp<Layer>> clonedLayersMap;
2204 // If the real layer exists and is in current state, add the clone as a child of the root.
2205 // There's no need to remove from drawingState when the layer is offscreen since currentState is
2206 // copied to drawingState for the root layer. So the clonedChild is always removed from
2207 // drawingState and then needs to be added back each traversal.
2208 if (!mClonedChild->getClonedFrom()->isRemovedFromCurrentState()) {
2209 addChildToDrawing(mClonedChild);
2210 }
2211
2212 mClonedChild->updateClonedDrawingState(clonedLayersMap);
2213 mClonedChild->updateClonedChildren(this, clonedLayersMap);
2214 mClonedChild->updateClonedRelatives(clonedLayersMap);
2215}
2216
2217void Layer::updateClonedDrawingState(std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2218 // If the layer the clone was cloned from is alive, copy the content of the drawingState
2219 // to the clone. If the real layer is no longer alive, continue traversing the children
2220 // since we may be able to pull out other children that are still alive.
2221 if (isClonedFromAlive()) {
2222 sp<Layer> clonedFrom = getClonedFrom();
2223 mDrawingState = clonedFrom->mDrawingState;
chaviw74b03172019-08-19 11:09:03 -07002224 clonedLayersMap.emplace(clonedFrom, this);
2225 }
2226
2227 // The clone layer may have children in drawingState since they may have been created and
2228 // added from a previous request to updateMirorInfo. This is to ensure we don't recreate clones
2229 // that already exist, since we can just re-use them.
2230 // The drawingChildren will not get overwritten by the currentChildren since the clones are
2231 // not updated in the regular traversal. They are skipped since the root will lose the
2232 // reference to them when it copies its currentChildren to drawing.
2233 for (sp<Layer>& child : mDrawingChildren) {
2234 child->updateClonedDrawingState(clonedLayersMap);
2235 }
2236}
2237
2238void Layer::updateClonedChildren(const sp<Layer>& mirrorRoot,
2239 std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2240 mDrawingChildren.clear();
2241
2242 if (!isClonedFromAlive()) {
2243 return;
2244 }
2245
2246 sp<Layer> clonedFrom = getClonedFrom();
2247 for (sp<Layer>& child : clonedFrom->mDrawingChildren) {
2248 if (child == mirrorRoot) {
2249 // This is to avoid cyclical mirroring.
2250 continue;
2251 }
2252 sp<Layer> clonedChild = clonedLayersMap[child];
2253 if (clonedChild == nullptr) {
2254 clonedChild = child->createClone();
2255 clonedLayersMap[child] = clonedChild;
2256 }
2257 addChildToDrawing(clonedChild);
2258 clonedChild->updateClonedChildren(mirrorRoot, clonedLayersMap);
2259 }
2260}
2261
chaviwaf87b3e2019-10-01 16:59:28 -07002262void Layer::updateClonedInputInfo(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2263 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2264 if (cropLayer != nullptr) {
2265 if (clonedLayersMap.count(cropLayer) == 0) {
2266 // Real layer had a crop layer but it's not in the cloned hierarchy. Just set to
2267 // self as crop layer to avoid going outside bounds.
2268 mDrawingState.touchableRegionCrop = this;
2269 } else {
2270 const sp<Layer>& clonedCropLayer = clonedLayersMap.at(cropLayer);
2271 mDrawingState.touchableRegionCrop = clonedCropLayer;
2272 }
2273 }
2274 // Cloned layers shouldn't handle watch outside since their z order is not determined by
2275 // WM or the client.
2276 mDrawingState.inputInfo.layoutParamsFlags &= ~InputWindowInfo::FLAG_WATCH_OUTSIDE_TOUCH;
2277}
2278
2279void Layer::updateClonedRelatives(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
chaviw74b03172019-08-19 11:09:03 -07002280 mDrawingState.zOrderRelativeOf = nullptr;
2281 mDrawingState.zOrderRelatives.clear();
2282
2283 if (!isClonedFromAlive()) {
2284 return;
2285 }
2286
chaviwaf87b3e2019-10-01 16:59:28 -07002287 const sp<Layer>& clonedFrom = getClonedFrom();
chaviw74b03172019-08-19 11:09:03 -07002288 for (wp<Layer>& relativeWeak : clonedFrom->mDrawingState.zOrderRelatives) {
chaviwaf87b3e2019-10-01 16:59:28 -07002289 const sp<Layer>& relative = relativeWeak.promote();
2290 if (clonedLayersMap.count(relative) > 0) {
2291 auto& clonedRelative = clonedLayersMap.at(relative);
chaviw74b03172019-08-19 11:09:03 -07002292 mDrawingState.zOrderRelatives.add(clonedRelative);
2293 }
2294 }
2295
2296 // Check if the relativeLayer for the real layer is part of the cloned hierarchy.
2297 // It's possible that the layer it's relative to is outside the requested cloned hierarchy.
2298 // In that case, we treat the layer as if the relativeOf has been removed. This way, it will
2299 // still traverse the children, but the layer with the missing relativeOf will not be shown
2300 // on screen.
chaviwaf87b3e2019-10-01 16:59:28 -07002301 const sp<Layer>& relativeOf = clonedFrom->mDrawingState.zOrderRelativeOf.promote();
2302 if (clonedLayersMap.count(relativeOf) > 0) {
2303 const sp<Layer>& clonedRelativeOf = clonedLayersMap.at(relativeOf);
chaviw74b03172019-08-19 11:09:03 -07002304 mDrawingState.zOrderRelativeOf = clonedRelativeOf;
2305 }
2306
chaviwaf87b3e2019-10-01 16:59:28 -07002307 updateClonedInputInfo(clonedLayersMap);
2308
chaviw74b03172019-08-19 11:09:03 -07002309 for (sp<Layer>& child : mDrawingChildren) {
2310 child->updateClonedRelatives(clonedLayersMap);
2311 }
2312}
2313
2314void Layer::addChildToDrawing(const sp<Layer>& layer) {
2315 mDrawingChildren.add(layer);
2316 layer->mDrawingParent = this;
2317}
2318
Mathias Agopian13127d82013-03-05 17:47:11 -08002319// ---------------------------------------------------------------------------
2320
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002321}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002322
2323#if defined(__gl_h_)
2324#error "don't include gl/gl.h in this file"
2325#endif
2326
2327#if defined(__gl2_h_)
2328#error "don't include gl2/gl2.h in this file"
2329#endif