blob: b0c94a279ef783659aeb59802d82af11f4795a9a [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
20
Dan Stoza9e56aa02015-11-02 13:00:03 -080021//#define LOG_NDEBUG 0
22#undef LOG_TAG
23#define LOG_TAG "Layer"
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080024#define ATRACE_TAG ATRACE_TAG_GRAPHICS
25
Alec Mourie60041e2019-06-14 18:59:51 -070026#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027
Yiwei Zhang5434a782018-12-05 18:06:32 -080028#include <android-base/stringprintf.h>
Vishnu Nair0f085c62019-08-30 08:49:12 -070029#include <binder/IPCThreadState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080030#include <compositionengine/Display.h>
Lloyd Pique0b785d82018-12-04 17:25:27 -080031#include <compositionengine/Layer.h>
Lloyd Piquea83776c2019-01-29 18:42:32 -080032#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080033#include <compositionengine/OutputLayer.h>
34#include <compositionengine/impl/OutputLayerCompositionState.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070035#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070036#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070037#include <cutils/properties.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080038#include <gui/BufferItem.h>
39#include <gui/LayerDebugInfo.h>
40#include <gui/Surface.h>
Alec Mourie60041e2019-06-14 18:59:51 -070041#include <math.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080042#include <renderengine/RenderEngine.h>
Alec Mourie60041e2019-06-14 18:59:51 -070043#include <stdint.h>
44#include <stdlib.h>
45#include <sys/types.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080046#include <ui/DebugUtils.h>
47#include <ui/GraphicBuffer.h>
48#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include <utils/Errors.h>
50#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080051#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080053#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054
Alec Mourie60041e2019-06-14 18:59:51 -070055#include <algorithm>
56#include <mutex>
57#include <sstream>
58
Yiwei Zhang60d1a192018-03-07 14:52:28 -080059#include "BufferLayer.h"
Valerie Haudd0b7572019-01-29 14:59:27 -080060#include "ColorLayer.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020061#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070062#include "DisplayDevice.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080063#include "DisplayHardware/HWComposer.h"
Mikael Pessa90092f42019-08-26 17:22:04 -070064#include "FrameTracer/FrameTracer.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080065#include "LayerProtoHelper.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070066#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070067#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068#include "SurfaceFlinger.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080069#include "TimeStats/TimeStats.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070070
David Sodman41fdfc92017-11-06 16:09:56 -080071#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073namespace android {
74
Yiwei Zhang5434a782018-12-05 18:06:32 -080075using base::StringAppendF;
76
Lloyd Piquef1c675b2018-09-12 20:45:39 -070077std::atomic<int32_t> Layer::sSequence{1};
Mathias Agopian13127d82013-03-05 17:47:11 -080078
Lloyd Pique42ab75e2018-09-12 20:46:03 -070079Layer::Layer(const LayerCreationArgs& args)
Ady Abraham8f1ee7f2019-04-05 10:32:50 -070080 : mFlinger(args.flinger),
81 mName(args.name),
82 mClientRef(args.client),
83 mWindowType(args.metadata.getInt32(METADATA_WINDOW_TYPE, 0)) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -070084
85 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -070086 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
87 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
88 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070089
Lloyd Pique0449b0f2018-12-20 16:23:45 -080090 mCurrentState.active_legacy.w = args.w;
91 mCurrentState.active_legacy.h = args.h;
92 mCurrentState.flags = layerFlags;
93 mCurrentState.active_legacy.transform.set(0, 0);
94 mCurrentState.crop_legacy.makeInvalid();
95 mCurrentState.requestedCrop_legacy = mCurrentState.crop_legacy;
96 mCurrentState.z = 0;
97 mCurrentState.color.a = 1.0f;
98 mCurrentState.layerStack = 0;
99 mCurrentState.sequence = 0;
100 mCurrentState.requested_legacy = mCurrentState.active_legacy;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800101 mCurrentState.active.w = UINT32_MAX;
102 mCurrentState.active.h = UINT32_MAX;
103 mCurrentState.active.transform.set(0, 0);
104 mCurrentState.transform = 0;
105 mCurrentState.transformToDisplayInverse = false;
106 mCurrentState.crop.makeInvalid();
107 mCurrentState.acquireFence = new Fence(-1);
108 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
109 mCurrentState.hdrMetadata.validTypes = 0;
Marissa Wall7e0a01f2019-08-14 14:29:25 -0700110 mCurrentState.surfaceDamageRegion = Region::INVALID_REGION;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800111 mCurrentState.cornerRadius = 0.0f;
112 mCurrentState.api = -1;
113 mCurrentState.hasColorTransform = false;
Peiyong Linc502cb72019-03-01 15:00:23 -0800114 mCurrentState.colorSpaceAgnostic = false;
Evan Roskya1f1e152019-01-24 16:17:46 -0800115 mCurrentState.metadata = args.metadata;
Vishnu Nairf37dcfb2019-11-16 07:37:09 -0800116 mCurrentState.shadowRadius = 0.f;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700117
118 // drawing state & current state are identical
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800119 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700120
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800121 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700122 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800123 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200124 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700125
Vishnu Nair0f085c62019-08-30 08:49:12 -0700126 mCallingPid = args.callingPid;
127 mCallingUid = args.callingUid;
Dominik Laskowski75848362019-11-11 17:57:20 -0800128}
129
130void Layer::onFirstRef() {
131 mFlinger->onLayerFirstRef(this);
Dan Stoza436ccf32018-06-21 12:10:12 -0700132}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700133
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700134Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800135 sp<Client> c(mClientRef.promote());
136 if (c != 0) {
137 c->detachLayer(this);
138 }
139
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000140 mFrameTracker.logAndResetStats(mName);
chaviw74d90ad2019-04-26 14:45:26 -0700141 mFlinger->onLayerDestroyed(this);
Mathias Agopian96f08192010-06-02 23:28:45 -0700142}
143
Vishnu Nair0f085c62019-08-30 08:49:12 -0700144LayerCreationArgs::LayerCreationArgs(SurfaceFlinger* flinger, const sp<Client>& client,
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700145 std::string name, uint32_t w, uint32_t h, uint32_t flags,
Vishnu Nair0f085c62019-08-30 08:49:12 -0700146 LayerMetadata metadata)
147 : flinger(flinger),
148 client(client),
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700149 name(std::move(name)),
Vishnu Nair0f085c62019-08-30 08:49:12 -0700150 w(w),
151 h(h),
152 flags(flags),
153 metadata(std::move(metadata)) {
154 IPCThreadState* ipc = IPCThreadState::self();
155 callingPid = ipc->getCallingPid();
156 callingUid = ipc->getCallingUid();
157}
158
Mathias Agopian13127d82013-03-05 17:47:11 -0800159// ---------------------------------------------------------------------------
160// callbacks
161// ---------------------------------------------------------------------------
162
David Sodmaneb085e02017-10-05 18:49:04 -0700163/*
164 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
165 * Layer. So, the implementation is done in BufferLayer. When called on a
166 * ColorLayer object, it's essentially a NOP.
167 */
David Sodmaneb085e02017-10-05 18:49:04 -0700168void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800169
chaviw43cb3cb2019-05-31 15:23:41 -0700170void Layer::removeRemoteSyncPoints() {
171 for (auto& point : mRemoteSyncPoints) {
172 point->setTransactionApplied();
173 }
174 mRemoteSyncPoints.clear();
175
176 {
chaviw43cb3cb2019-05-31 15:23:41 -0700177 for (State pendingState : mPendingStates) {
178 pendingState.barrierLayer_legacy = nullptr;
179 }
180 }
181}
182
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700183void Layer::removeRelativeZ(const std::vector<Layer*>& layersInTree) {
184 if (mCurrentState.zOrderRelativeOf == nullptr) {
185 return;
186 }
Robert Carr2e102c92018-10-23 12:11:15 -0700187
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700188 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
189 if (strongRelative == nullptr) {
190 setZOrderRelativeOf(nullptr);
191 return;
192 }
193
194 if (!std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
195 strongRelative->removeZOrderRelative(this);
196 mFlinger->setTransactionFlags(eTraversalNeeded);
chaviw606e5cf2019-03-01 10:12:10 -0800197 setZOrderRelativeOf(nullptr);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700198 }
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700199}
200
201void Layer::removeFromCurrentState() {
202 mRemovedFromCurrentState = true;
Rob Carr4bba3702018-10-08 21:53:30 +0000203
Robert Carr2e102c92018-10-23 12:11:15 -0700204 // Since we are no longer reachable from CurrentState SurfaceFlinger
205 // will no longer invoke doTransaction for us, and so we will
206 // never finish applying transactions. We signal the sync point
207 // now so that another layer will not become indefinitely
208 // blocked.
chaviw43cb3cb2019-05-31 15:23:41 -0700209 removeRemoteSyncPoints();
Robert Carr2e102c92018-10-23 12:11:15 -0700210
211 {
212 Mutex::Autolock syncLock(mLocalSyncPointMutex);
213 for (auto& point : mLocalSyncPoints) {
214 point->setFrameAvailable();
215 }
216 mLocalSyncPoints.clear();
217 }
218
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800219 mFlinger->markLayerPendingRemovalLocked(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700220}
Chia-I Wu38512252017-05-17 14:36:16 -0700221
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700222void Layer::onRemovedFromCurrentState() {
223 auto layersInTree = getLayersInTree(LayerVector::StateSet::Current);
224 std::sort(layersInTree.begin(), layersInTree.end());
225 for (const auto& layer : layersInTree) {
226 layer->removeFromCurrentState();
227 layer->removeRelativeZ(layersInTree);
228 }
229}
230
chaviw61626f22018-11-15 16:26:27 -0800231void Layer::addToCurrentState() {
232 mRemovedFromCurrentState = false;
233
234 for (const auto& child : mCurrentChildren) {
235 child->addToCurrentState();
236 }
237}
238
Mathias Agopian13127d82013-03-05 17:47:11 -0800239// ---------------------------------------------------------------------------
240// set-up
241// ---------------------------------------------------------------------------
242
chaviw13fdc492017-06-27 12:40:18 -0700243bool Layer::getPremultipledAlpha() const {
244 return mPremultipliedAlpha;
245}
246
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700247sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800248 Mutex::Autolock _l(mLock);
Robert Carrc0df3122019-04-11 13:18:21 -0700249 if (mGetHandleCalled) {
250 ALOGE("Get handle called twice" );
251 return nullptr;
252 }
253 mGetHandleCalled = true;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700254 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800255}
256
257// ---------------------------------------------------------------------------
258// h/w composer set-up
259// ---------------------------------------------------------------------------
260
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700261static Rect reduce(const Rect& win, const Region& exclude) {
262 if (CC_LIKELY(exclude.isEmpty())) {
263 return win;
264 }
265 if (exclude.isRect()) {
266 return win.reduce(exclude.getBounds());
267 }
268 return Region(win).subtract(exclude).getBounds();
269}
270
Dan Stoza80d61162017-12-20 15:57:52 -0800271static FloatRect reduce(const FloatRect& win, const Region& exclude) {
272 if (CC_LIKELY(exclude.isEmpty())) {
273 return win;
274 }
275 // Convert through Rect (by rounding) for lack of FloatRegion
276 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
277}
278
Vishnu Nair4351ad52019-02-11 14:13:02 -0800279Rect Layer::getScreenBounds(bool reduceTransparentRegion) const {
Vishnu Nairf0c28512019-02-08 12:40:28 -0800280 if (!reduceTransparentRegion) {
281 return Rect{mScreenBounds};
282 }
283
284 FloatRect bounds = getBounds();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800285 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800286 // Transform to screen space.
287 bounds = t.transform(bounds);
288 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700289}
290
Vishnu Nair4351ad52019-02-11 14:13:02 -0800291FloatRect Layer::getBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000292 const State& s(getDrawingState());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800293 return getBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700294}
295
Vishnu Nairf0c28512019-02-08 12:40:28 -0800296FloatRect Layer::getBounds(const Region& activeTransparentRegion) const {
297 // Subtract the transparent region and snap to the bounds.
298 return reduce(mBounds, activeTransparentRegion);
299}
300
Vishnu Nairc652ff82019-03-15 12:48:54 -0700301ui::Transform Layer::getBufferScaleTransform() const {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800302 // If the layer is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
303 // it isFixedSize) then there may be additional scaling not accounted
Vishnu Nairc652ff82019-03-15 12:48:54 -0700304 // for in the layer transform.
chaviwd62d3062019-09-04 14:48:02 -0700305 if (!isFixedSize() || getBuffer() == nullptr) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700306 return {};
Vishnu Nair4351ad52019-02-11 14:13:02 -0800307 }
308
Marissa Wall290ad082019-03-06 13:23:47 -0800309 // If the layer is a buffer state layer, the active width and height
310 // could be infinite. In that case, return the effective transform.
311 const uint32_t activeWidth = getActiveWidth(getDrawingState());
312 const uint32_t activeHeight = getActiveHeight(getDrawingState());
313 if (activeWidth >= UINT32_MAX && activeHeight >= UINT32_MAX) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700314 return {};
Marissa Wall290ad082019-03-06 13:23:47 -0800315 }
316
chaviwd62d3062019-09-04 14:48:02 -0700317 int bufferWidth = getBuffer()->getWidth();
318 int bufferHeight = getBuffer()->getHeight();
Vishnu Nairc652ff82019-03-15 12:48:54 -0700319
chaviw4244e032019-09-04 11:27:49 -0700320 if (getBufferTransform() & NATIVE_WINDOW_TRANSFORM_ROT_90) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700321 std::swap(bufferWidth, bufferHeight);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800322 }
Vishnu Nairc652ff82019-03-15 12:48:54 -0700323
Marissa Wall290ad082019-03-06 13:23:47 -0800324 float sx = activeWidth / static_cast<float>(bufferWidth);
325 float sy = activeHeight / static_cast<float>(bufferHeight);
326
Vishnu Nair4351ad52019-02-11 14:13:02 -0800327 ui::Transform extraParentScaling;
328 extraParentScaling.set(sx, 0, 0, sy);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700329 return extraParentScaling;
330}
331
332ui::Transform Layer::getTransformWithScale(const ui::Transform& bufferScaleTransform) const {
333 // We need to mirror this scaling to child surfaces or we will break the contract where WM can
334 // treat child surfaces as pixels in the parent surface.
chaviwd62d3062019-09-04 14:48:02 -0700335 if (!isFixedSize() || getBuffer() == nullptr) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700336 return mEffectiveTransform;
337 }
338 return mEffectiveTransform * bufferScaleTransform;
339}
340
341FloatRect Layer::getBoundsPreScaling(const ui::Transform& bufferScaleTransform) const {
342 // We need the pre scaled layer bounds when computing child bounds to make sure the child is
343 // cropped to its parent layer after any buffer transform scaling is applied.
chaviwd62d3062019-09-04 14:48:02 -0700344 if (!isFixedSize() || getBuffer() == nullptr) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700345 return mBounds;
346 }
347 return bufferScaleTransform.inverse().transform(mBounds);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800348}
349
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700350void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform,
351 float parentShadowRadius) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800352 const State& s(getDrawingState());
353
354 // Calculate effective layer transform
355 mEffectiveTransform = parentTransform * getActiveTransform(s);
356
357 // Transform parent bounds to layer space
358 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
359
Vishnu Nairc652ff82019-03-15 12:48:54 -0700360 // Calculate source bounds
Vishnu Nair4351ad52019-02-11 14:13:02 -0800361 mSourceBounds = computeSourceBounds(parentBounds);
362
363 // Calculate bounds by croping diplay frame with layer crop and parent bounds
364 FloatRect bounds = mSourceBounds;
365 const Rect layerCrop = getCrop(s);
366 if (!layerCrop.isEmpty()) {
367 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
368 }
369 bounds = bounds.intersect(parentBounds);
370
371 mBounds = bounds;
372 mScreenBounds = mEffectiveTransform.transform(mBounds);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700373
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700374 // Use the layer's own shadow radius if set. Otherwise get the radius from
375 // parent.
376 if (s.shadowRadius > 0.f) {
377 mEffectiveShadowRadius = s.shadowRadius;
378 } else {
379 mEffectiveShadowRadius = parentShadowRadius;
380 }
381
382 // Shadow radius is passed down to only one layer so if the layer can draw shadows,
383 // don't pass it to its children.
384 const float childShadowRadius = canDrawShadows() ? 0.f : mEffectiveShadowRadius;
385
Vishnu Nairc652ff82019-03-15 12:48:54 -0700386 // Add any buffer scaling to the layer's children.
387 ui::Transform bufferScaleTransform = getBufferScaleTransform();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800388 for (const sp<Layer>& child : mDrawingChildren) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700389 child->computeBounds(getBoundsPreScaling(bufferScaleTransform),
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700390 getTransformWithScale(bufferScaleTransform), childShadowRadius);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800391 }
392}
393
Vishnu Nair60356342018-11-13 13:00:45 -0800394Rect Layer::getCroppedBufferSize(const State& s) const {
395 Rect size = getBufferSize(s);
396 Rect crop = getCrop(s);
397 if (!crop.isEmpty() && size.isValid()) {
398 size.intersect(crop, &size);
399 } else if (!crop.isEmpty()) {
400 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700401 }
Vishnu Nair60356342018-11-13 13:00:45 -0800402 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800403}
404
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700405void Layer::setupRoundedCornersCropCoordinates(Rect win,
406 const FloatRect& roundedCornersCrop) const {
407 // Translate win by the rounded corners rect coordinates, to have all values in
408 // layer coordinate space.
409 win.left -= roundedCornersCrop.left;
410 win.right -= roundedCornersCrop.left;
411 win.top -= roundedCornersCrop.top;
412 win.bottom -= roundedCornersCrop.top;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700413}
414
Lloyd Piquec6687342019-03-07 21:34:57 -0800415void Layer::latchBasicGeometry(compositionengine::LayerFECompositionState& compositionState) const {
416 const auto& drawingState{getDrawingState()};
417 const uint32_t layerStack = getLayerStack();
418 const auto alpha = static_cast<float>(getAlpha());
419 const bool opaque = isOpaque(drawingState);
420 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
421
422 auto blendMode = Hwc2::IComposerClient::BlendMode::NONE;
423 if (!opaque || alpha != 1.0f) {
424 blendMode = mPremultipliedAlpha ? Hwc2::IComposerClient::BlendMode::PREMULTIPLIED
425 : Hwc2::IComposerClient::BlendMode::COVERAGE;
426 }
427
428 // TODO(b/121291683): Instead of filling in a passed-in compositionState
429 // structure, switch to Layer owning the structure and have
430 // CompositionEngine be able to get a reference to it.
431
432 compositionState.layerStackId =
433 (layerStack != ~0u) ? std::make_optional(layerStack) : std::nullopt;
434 compositionState.internalOnly = getPrimaryDisplayOnly();
435 compositionState.isVisible = isVisible();
436 compositionState.isOpaque = opaque && !usesRoundedCorners && alpha == 1.f;
Vishnu Naira483b4a2019-12-12 15:07:52 -0800437 compositionState.shadowRadius = mEffectiveShadowRadius;
Lloyd Piquec6687342019-03-07 21:34:57 -0800438
439 compositionState.contentDirty = contentDirty;
440 contentDirty = false;
441
442 compositionState.geomLayerBounds = mBounds;
443 compositionState.geomLayerTransform = getTransform();
444 compositionState.geomInverseLayerTransform = compositionState.geomLayerTransform.inverse();
445 compositionState.transparentRegionHint = getActiveTransparentRegion(drawingState);
446
447 compositionState.blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
448 compositionState.alpha = alpha;
449}
450
Lloyd Piquea83776c2019-01-29 18:42:32 -0800451void Layer::latchGeometry(compositionengine::LayerFECompositionState& compositionState) const {
452 const auto& drawingState{getDrawingState()};
Mathias Agopian13127d82013-03-05 17:47:11 -0800453
Lloyd Piquea83776c2019-01-29 18:42:32 -0800454 int type = drawingState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
455 int appId = drawingState.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700456 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400457 if (parent.get()) {
458 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800459 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
460 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
Jiwen 'Steve' Cai736c74e2019-05-28 18:33:22 -0700461 if (parentType > 0 && parentAppId > 0) {
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800462 type = parentType;
463 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700464 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400465 }
466
Lloyd Piquea83776c2019-01-29 18:42:32 -0800467 compositionState.geomBufferSize = getBufferSize(drawingState);
chaviw4244e032019-09-04 11:27:49 -0700468 compositionState.geomContentCrop = getBufferCrop();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800469 compositionState.geomCrop = getCrop(drawingState);
chaviw4244e032019-09-04 11:27:49 -0700470 compositionState.geomBufferTransform = getBufferTransform();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800471 compositionState.geomBufferUsesDisplayInverseTransform = getTransformToDisplayInverse();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800472 compositionState.geomUsesSourceCrop = usesSourceCrop();
473 compositionState.isSecure = isSecure();
David Sodman15094112018-10-11 09:39:37 -0700474
Lloyd Piquea83776c2019-01-29 18:42:32 -0800475 compositionState.type = type;
476 compositionState.appId = appId;
477}
David Sodmanba340492018-08-05 21:51:33 -0700478
Lloyd Piquef5275482019-01-29 18:42:42 -0800479void Layer::latchPerFrameState(compositionengine::LayerFECompositionState& compositionState) const {
Lloyd Pique688abd42019-02-15 15:42:24 -0800480 const auto& drawingState{getDrawingState()};
Lloyd Piquef5275482019-01-29 18:42:42 -0800481 compositionState.forceClientComposition = false;
482
Lloyd Piquef5275482019-01-29 18:42:42 -0800483 compositionState.isColorspaceAgnostic = isColorSpaceAgnostic();
chaviw4244e032019-09-04 11:27:49 -0700484 compositionState.dataspace = getDataSpace();
Lloyd Piquef5275482019-01-29 18:42:42 -0800485 compositionState.colorTransform = getColorTransform();
486 compositionState.colorTransformIsIdentity = !hasColorTransform();
487 compositionState.surfaceDamage = surfaceDamageRegion;
Lloyd Pique688abd42019-02-15 15:42:24 -0800488 compositionState.hasProtectedContent = isProtected();
489
490 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700491 const bool drawsShadows = mEffectiveShadowRadius != 0.f;
492
Lloyd Pique688abd42019-02-15 15:42:24 -0800493 compositionState.isOpaque =
494 isOpaque(drawingState) && !usesRoundedCorners && getAlpha() == 1.0_hf;
Lloyd Piquef5275482019-01-29 18:42:42 -0800495
496 // Force client composition for special cases known only to the front-end.
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700497 if (isHdrY410() || usesRoundedCorners || drawsShadows) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800498 compositionState.forceClientComposition = true;
499 }
500}
501
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800502void Layer::latchCursorCompositionState(
503 compositionengine::LayerFECompositionState& compositionState) const {
504 // This gives us only the "orientation" component of the transform
505 const State& drawingState{getDrawingState()};
506
507 // Apply the layer's transform, followed by the display's global transform
508 // Here we're guaranteed that the layer's transform preserves rects
509 Rect win = getCroppedBufferSize(drawingState);
510 // Subtract the transparent region and snap to the bounds
511 Rect bounds = reduce(win, getActiveTransparentRegion(drawingState));
512 Rect frame(getTransform().transform(bounds));
513
514 compositionState.cursorFrame = frame;
515}
516
Lloyd Piquef16688f2019-02-19 17:47:57 -0800517bool Layer::onPreComposition(nsecs_t) {
518 return false;
519}
520
Lloyd Piquea83776c2019-01-29 18:42:32 -0800521void Layer::latchCompositionState(compositionengine::LayerFECompositionState& compositionState,
Lloyd Piquec6687342019-03-07 21:34:57 -0800522 compositionengine::LayerFE::StateSubset subset) const {
523 using StateSubset = compositionengine::LayerFE::StateSubset;
Lloyd Piquef5275482019-01-29 18:42:42 -0800524
Lloyd Piquec6687342019-03-07 21:34:57 -0800525 switch (subset) {
526 case StateSubset::BasicGeometry:
527 latchBasicGeometry(compositionState);
528 break;
529
530 case StateSubset::GeometryAndContent:
531 latchBasicGeometry(compositionState);
532 latchGeometry(compositionState);
533 latchPerFrameState(compositionState);
534 break;
535
536 case StateSubset::Content:
537 latchPerFrameState(compositionState);
538 break;
539 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800540}
Mathias Agopian29a367b2011-07-12 14:51:45 -0700541
Lloyd Piquea83776c2019-01-29 18:42:32 -0800542const char* Layer::getDebugName() const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700543 return mName.c_str();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800544}
545
Mathias Agopian13127d82013-03-05 17:47:11 -0800546// ---------------------------------------------------------------------------
547// drawing...
548// ---------------------------------------------------------------------------
549
Lloyd Piquef16688f2019-02-19 17:47:57 -0800550std::optional<renderengine::LayerSettings> Layer::prepareClientComposition(
551 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
552 if (!getCompositionLayer()) {
553 return {};
554 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800555
Vishnu Nair4351ad52019-02-11 14:13:02 -0800556 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000557 half alpha = getAlpha();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800558 renderengine::LayerSettings layerSettings;
559 layerSettings.geometry.boundaries = bounds;
560 if (targetSettings.useIdentityTransform) {
561 layerSettings.geometry.positionTransform = mat4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000562 } else {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800563 layerSettings.geometry.positionTransform = getTransform().asMatrix4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000564 }
565
566 if (hasColorTransform()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800567 layerSettings.colorTransform = getColorTransform();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000568 }
569
570 const auto roundedCornerState = getRoundedCornerState();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800571 layerSettings.geometry.roundedCornersRadius = roundedCornerState.radius;
572 layerSettings.geometry.roundedCornersCrop = roundedCornerState.cropRect;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000573
Lloyd Piquef16688f2019-02-19 17:47:57 -0800574 layerSettings.alpha = alpha;
chaviw4244e032019-09-04 11:27:49 -0700575 layerSettings.sourceDataspace = getDataSpace();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800576 return layerSettings;
Mathias Agopian13127d82013-03-05 17:47:11 -0800577}
578
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800579std::optional<renderengine::LayerSettings> Layer::prepareShadowClientComposition(
580 const renderengine::LayerSettings& casterLayerSettings, const Rect& displayViewport,
581 ui::Dataspace outputDataspace) {
582 renderengine::ShadowSettings shadow = getShadowSettings(displayViewport);
583 if (shadow.length <= 0.f) {
584 return {};
585 }
586
587 const float casterAlpha = casterLayerSettings.alpha;
588 const bool casterIsOpaque = ((casterLayerSettings.source.buffer.buffer != nullptr) &&
589 casterLayerSettings.source.buffer.isOpaque);
590
591 renderengine::LayerSettings shadowLayer = casterLayerSettings;
592 shadowLayer.shadow = shadow;
Vishnu Naira483b4a2019-12-12 15:07:52 -0800593 shadowLayer.geometry.boundaries = mBounds; // ignore transparent region
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800594
595 // If the casting layer is translucent, we need to fill in the shadow underneath the layer.
596 // Otherwise the generated shadow will only be shown around the casting layer.
597 shadowLayer.shadow.casterIsTranslucent = !casterIsOpaque || (casterAlpha < 1.0f);
598 shadowLayer.shadow.ambientColor *= casterAlpha;
599 shadowLayer.shadow.spotColor *= casterAlpha;
600 shadowLayer.sourceDataspace = outputDataspace;
601 shadowLayer.source.buffer.buffer = nullptr;
602
603 if (shadowLayer.shadow.ambientColor.a <= 0.f && shadowLayer.shadow.spotColor.a <= 0.f) {
604 return {};
605 }
606
607 float casterCornerRadius = shadowLayer.geometry.roundedCornersRadius;
608 const FloatRect& cornerRadiusCropRect = casterLayerSettings.geometry.roundedCornersCrop;
609 const FloatRect& casterRect = shadowLayer.geometry.boundaries;
610
611 // crop used to set the corner radius may be larger than the content rect. Adjust the corner
612 // radius accordingly.
613 if (casterCornerRadius > 0.f) {
614 float cropRectOffset = std::max(std::abs(cornerRadiusCropRect.top - casterRect.top),
615 std::abs(cornerRadiusCropRect.left - casterRect.left));
616 if (cropRectOffset > casterCornerRadius) {
617 casterCornerRadius = 0;
618 } else {
619 casterCornerRadius -= cropRectOffset;
620 }
621 shadowLayer.geometry.roundedCornersRadius = casterCornerRadius;
622 }
623
624 return shadowLayer;
625}
626
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800627Hwc2::IComposerClient::Composition Layer::getCompositionType(
628 const sp<const DisplayDevice>& display) const {
629 const auto outputLayer = findOutputLayerForDisplay(display);
630 LOG_FATAL_IF(!outputLayer);
631 return outputLayer->getState().hwc ? (*outputLayer->getState().hwc).hwcCompositionType
632 : Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800633}
634
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800635bool Layer::getClearClientTarget(const sp<const DisplayDevice>& display) const {
636 const auto outputLayer = findOutputLayerForDisplay(display);
637 LOG_FATAL_IF(!outputLayer);
638 return outputLayer->getState().clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800639}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800640
Dan Stozacac35382016-01-27 12:21:06 -0800641bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
642 if (point->getFrameNumber() <= mCurrentFrameNumber) {
643 // Don't bother with a SyncPoint, since we've already latched the
644 // relevant frame
645 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700646 }
Robert Carr2e102c92018-10-23 12:11:15 -0700647 if (isRemovedFromCurrentState()) {
648 return false;
649 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700650
Dan Stozacac35382016-01-27 12:21:06 -0800651 Mutex::Autolock lock(mLocalSyncPointMutex);
652 mLocalSyncPoints.push_back(point);
653 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700654}
655
Mathias Agopian13127d82013-03-05 17:47:11 -0800656// ----------------------------------------------------------------------------
657// local state
658// ----------------------------------------------------------------------------
659
David Sodman41fdfc92017-11-06 16:09:56 -0800660bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800661 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700662 return (s.flags & layer_state_t::eLayerSecure);
663}
664
Mathias Agopian13127d82013-03-05 17:47:11 -0800665// ----------------------------------------------------------------------------
666// transaction
667// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800668
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800669void Layer::pushPendingState() {
670 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700671 return;
672 }
Alec Mourie60041e2019-06-14 18:59:51 -0700673 ATRACE_CALL();
Dan Stoza7dde5992015-05-22 09:51:44 -0700674
Dan Stoza7dde5992015-05-22 09:51:44 -0700675 // If this transaction is waiting on the receipt of a frame, generate a sync
676 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700677 // We don't allow installing sync points after we are removed from the current state
678 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800679 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
680 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800681 if (barrierLayer == nullptr) {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700682 ALOGE("[%s] Unable to promote barrier Layer.", getDebugName());
Dan Stoza7dde5992015-05-22 09:51:44 -0700683 // If we can't promote the layer we are intended to wait on,
684 // then it is expired or otherwise invalid. Allow this transaction
685 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800686 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800687 } else {
chaviw43cb3cb2019-05-31 15:23:41 -0700688 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy, this);
Robert Carr0d480722017-01-10 16:42:54 -0800689 if (barrierLayer->addSyncPoint(syncPoint)) {
Alec Mourie60041e2019-06-14 18:59:51 -0700690 std::stringstream ss;
691 ss << "Adding sync point " << mCurrentState.frameNumber_legacy;
692 ATRACE_NAME(ss.str().c_str());
Dan Stozacac35382016-01-27 12:21:06 -0800693 mRemoteSyncPoints.push_back(std::move(syncPoint));
694 } else {
695 // We already missed the frame we're supposed to synchronize
696 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800697 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800698 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700699 }
700
Dan Stoza7dde5992015-05-22 09:51:44 -0700701 // Wake us up to check if the frame has been received
702 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700703 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700704 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800705 mPendingStates.push_back(mCurrentState);
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700706 ATRACE_INT(mTransactionName.c_str(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700707}
708
Pablo Ceballos05289c22016-04-14 15:49:55 -0700709void Layer::popPendingState(State* stateToCommit) {
Alec Mourie60041e2019-06-14 18:59:51 -0700710 ATRACE_CALL();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800711 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700712
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800713 mPendingStates.removeAt(0);
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700714 ATRACE_INT(mTransactionName.c_str(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700715}
716
Pablo Ceballos05289c22016-04-14 15:49:55 -0700717bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700718 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800719 while (!mPendingStates.empty()) {
720 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700721 if (mRemoteSyncPoints.empty()) {
722 // If we don't have a sync point for this, apply it anyway. It
723 // will be visually wrong, but it should keep us from getting
724 // into too much trouble.
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700725 ALOGE("[%s] No local sync point found", getDebugName());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700726 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700727 stateUpdateAvailable = true;
728 continue;
729 }
730
Marissa Wallf58c14b2018-07-24 10:50:43 -0700731 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800732 mPendingStates[0].frameNumber_legacy) {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700733 ALOGE("[%s] Unexpected sync point frame number found", getDebugName());
Dan Stozacac35382016-01-27 12:21:06 -0800734
735 // Signal our end of the sync point and then dispose of it
736 mRemoteSyncPoints.front()->setTransactionApplied();
737 mRemoteSyncPoints.pop_front();
738 continue;
739 }
740
Dan Stoza7dde5992015-05-22 09:51:44 -0700741 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
Alec Mourie60041e2019-06-14 18:59:51 -0700742 ATRACE_NAME("frameIsAvailable");
Dan Stoza7dde5992015-05-22 09:51:44 -0700743 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700744 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700745 stateUpdateAvailable = true;
746
747 // Signal our end of the sync point and then dispose of it
748 mRemoteSyncPoints.front()->setTransactionApplied();
749 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800750 } else {
Alec Mourie60041e2019-06-14 18:59:51 -0700751 ATRACE_NAME("!frameIsAvailable");
Dan Stoza792e5292016-02-11 11:43:58 -0800752 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700753 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700754 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700755 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700756 stateUpdateAvailable = true;
757 }
758 }
759
760 // If we still have pending updates, wake SurfaceFlinger back up and point
761 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800762 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700763 setTransactionFlags(eTransactionNeeded);
764 mFlinger->setTransactionFlags(eTraversalNeeded);
765 }
766
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800767 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700768 return stateUpdateAvailable;
769}
770
Marissa Wall61c58622018-07-18 10:12:20 -0700771uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000772 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800773
Marissa Wall61c58622018-07-18 10:12:20 -0700774 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
775 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700776
David Sodmaneb085e02017-10-05 18:49:04 -0700777 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700778 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000779 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800780 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
781 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
782 " requested={ wh={%4u,%4u} }}\n"
783 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
784 " requested={ wh={%4u,%4u} }}\n",
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700785 this, getName().c_str(), getBufferTransform(), getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700786 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
787 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
788 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
789 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
790 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700791 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
792 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
793 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800794 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700795
Robert Carre392b552017-09-19 12:16:05 -0700796 // Don't let Layer::doTransaction update the drawing state
797 // if we have a pending resize, unless we are in fixed-size mode.
798 // the drawing state will be updated only once we receive a buffer
799 // with the correct size.
800 //
801 // In particular, we want to make sure the clip (which is part
802 // of the geometry state) is latched together with the size but is
803 // latched immediately when no resizing is involved.
804 //
805 // If a sideband stream is attached, however, we want to skip this
806 // optimization so that transactions aren't missed when a buffer
807 // never arrives
808 //
809 // In the case that we don't have a buffer we ignore other factors
810 // and avoid entering the resizePending state. At a high level the
811 // resizePending state is to avoid applying the state of the new buffer
812 // to the old buffer. However in the state where we don't have an old buffer
813 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700814 const bool resizePending =
815 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
816 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
chaviwd62d3062019-09-04 14:48:02 -0700817 (getBuffer() != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700818 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800819 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700820 flags |= eDontUpdateGeometryState;
821 }
822 }
823
Robert Carr7bf247e2017-05-18 14:02:49 -0700824 // Here we apply various requested geometry states, depending on our
825 // latching configuration. See Layer.h for a detailed discussion of
826 // how geometry latching is controlled.
827 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000828 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700829
Robert Carr7bf247e2017-05-18 14:02:49 -0700830 // There is an awkward asymmetry in the handling of the crop states in the position
831 // states, as can be seen below. Largely this arises from position and transform
832 // being stored in the same data structure while having different latching rules.
833 // b/38182305
834 //
Marissa Wall61c58622018-07-18 10:12:20 -0700835 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700836 // applyPendingStates in the presence of deferred transactions.
chaviw214c89d2019-09-04 16:03:53 -0700837 editCurrentState.active_legacy = editCurrentState.requested_legacy;
838 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Mathias Agopian13127d82013-03-05 17:47:11 -0800839 }
840
Marissa Wall61c58622018-07-18 10:12:20 -0700841 return flags;
842}
843
844uint32_t Layer::doTransaction(uint32_t flags) {
845 ATRACE_CALL();
846
chaviw5aedec92018-10-22 10:40:38 -0700847 if (mLayerDetached) {
Robert Carr321e83c2019-08-19 15:49:30 -0700848 // Ensure BLAST buffer callbacks are processed.
849 // detachChildren and mLayerDetached were implemented to avoid geometry updates
850 // to layers in the cases of animation. For BufferQueue layers buffers are still
851 // consumed as normal. This is useful as otherwise the client could get hung
852 // inevitably waiting on a buffer to return. We recreate this semantic for BufferQueue
853 // even though it is a little consistent. detachChildren is shortly slated for removal
854 // by the hierarchy mirroring work so we don't need to worry about it too much.
Valerie Hau7618b232020-01-09 16:03:08 -0800855 forceSendCallbacks();
Robert Carr321e83c2019-08-19 15:49:30 -0700856 mCurrentState.callbackHandles = {};
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800857 return flags;
858 }
859
860 if (mChildrenChanged) {
861 flags |= eVisibleRegion;
862 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -0700863 }
864
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800865 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +0000866 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -0700867 if (!applyPendingStates(&c)) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800868 return flags;
Marissa Wall61c58622018-07-18 10:12:20 -0700869 }
870
871 flags = doTransactionResize(flags, &c);
872
Alec Mourib416efd2018-09-06 21:01:59 +0000873 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700874
875 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800876 // invalidate and recompute the visible regions if needed
877 flags |= Layer::eVisibleRegion;
878 }
879
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700880 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800881 // invalidate and recompute the visible regions if needed
882 flags |= eVisibleRegion;
883 this->contentDirty = true;
884
885 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -0700886 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -0700887 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -0800888 }
889
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800890 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -0700891 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800892 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -0700893 }
894
Mathias Agopian13127d82013-03-05 17:47:11 -0800895 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -0700896 commitTransaction(c);
Vishnu Nair8406fd72019-07-30 11:29:31 -0700897 mPendingStatesSnapshot = mPendingStates;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800898 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -0800899 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800900}
901
Pablo Ceballos05289c22016-04-14 15:49:55 -0700902void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800903 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700904}
905
Mathias Agopian13127d82013-03-05 17:47:11 -0800906uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800907 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800908}
909
910uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800911 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -0800912}
913
chaviw214c89d2019-09-04 16:03:53 -0700914bool Layer::setPosition(float x, float y) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800915 if (mCurrentState.requested_legacy.transform.tx() == x &&
916 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -0800917 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800918 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -0700919
920 // We update the requested and active position simultaneously because
921 // we want to apply the position portion of the transform matrix immediately,
922 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800923 mCurrentState.requested_legacy.transform.set(x, y);
chaviw214c89d2019-09-04 16:03:53 -0700924 // Here we directly update the active state
925 // unlike other setters, because we store it within
926 // the transform, but use different latching rules.
927 // b/38182305
928 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr69663fb2016-03-27 19:59:19 -0700929
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800930 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800931 setTransactionFlags(eTransactionNeeded);
932 return true;
933}
Robert Carr82364e32016-05-15 11:27:47 -0700934
Robert Carr1f0a16a2016-10-24 16:27:39 -0700935bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
936 ssize_t idx = mCurrentChildren.indexOf(childLayer);
937 if (idx < 0) {
938 return false;
939 }
940 if (childLayer->setLayer(z)) {
941 mCurrentChildren.removeAt(idx);
942 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800943 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700944 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800945 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700946}
947
Robert Carr503c7042017-09-27 15:06:08 -0700948bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
949 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
950 ssize_t idx = mCurrentChildren.indexOf(childLayer);
951 if (idx < 0) {
952 return false;
953 }
954 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
955 mCurrentChildren.removeAt(idx);
956 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800957 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700958 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800959 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700960}
961
Robert Carrae060832016-11-28 10:51:00 -0800962bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800963 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
964 mCurrentState.sequence++;
965 mCurrentState.z = z;
966 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700967
968 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800969 if (mCurrentState.zOrderRelativeOf != nullptr) {
970 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700971 if (strongRelative != nullptr) {
972 strongRelative->removeZOrderRelative(this);
973 }
chaviw606e5cf2019-03-01 10:12:10 -0800974 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -0700975 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800976 setTransactionFlags(eTransactionNeeded);
977 return true;
978}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700979
Robert Carrdb66e622017-04-10 16:55:57 -0700980void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800981 mCurrentState.zOrderRelatives.remove(relative);
982 mCurrentState.sequence++;
983 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700984 setTransactionFlags(eTransactionNeeded);
985}
986
987void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800988 mCurrentState.zOrderRelatives.add(relative);
989 mCurrentState.modified = true;
990 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -0700991 setTransactionFlags(eTransactionNeeded);
992}
993
chaviw606e5cf2019-03-01 10:12:10 -0800994void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
995 mCurrentState.zOrderRelativeOf = relativeOf;
996 mCurrentState.sequence++;
997 mCurrentState.modified = true;
chaviwbdb8b802019-10-14 09:17:12 -0700998 mCurrentState.isRelativeOf = relativeOf != nullptr;
999
chaviw606e5cf2019-03-01 10:12:10 -08001000 setTransactionFlags(eTransactionNeeded);
1001}
1002
Robert Carr503d2bd2017-12-04 15:49:47 -08001003bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001004 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1005 if (handle == nullptr) {
1006 return false;
1007 }
1008 sp<Layer> relative = handle->owner.promote();
1009 if (relative == nullptr) {
1010 return false;
1011 }
1012
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001013 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1014 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001015 return false;
1016 }
1017
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001018 mCurrentState.sequence++;
1019 mCurrentState.modified = true;
1020 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001021
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001022 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -07001023 if (oldZOrderRelativeOf != nullptr) {
1024 oldZOrderRelativeOf->removeZOrderRelative(this);
1025 }
chaviw606e5cf2019-03-01 10:12:10 -08001026 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -07001027 relative->addZOrderRelative(this);
1028
1029 setTransactionFlags(eTransactionNeeded);
1030
1031 return true;
1032}
1033
Mathias Agopian13127d82013-03-05 17:47:11 -08001034bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001035 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001036 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001037 mCurrentState.requested_legacy.w = w;
1038 mCurrentState.requested_legacy.h = h;
1039 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001040 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001041
1042 // record the new size, from this point on, when the client request
1043 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001044 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001045 return true;
1046}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001047bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001048 if (mCurrentState.color.a == alpha) return false;
1049 mCurrentState.sequence++;
1050 mCurrentState.color.a = alpha;
1051 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001052 setTransactionFlags(eTransactionNeeded);
1053 return true;
1054}
chaviw13fdc492017-06-27 12:40:18 -07001055
Valerie Haudd0b7572019-01-29 14:59:27 -08001056bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1057 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001058 return false;
Valerie Hauaa194562019-02-05 16:21:38 -08001059 }
1060 mCurrentState.sequence++;
1061 mCurrentState.modified = true;
1062 setTransactionFlags(eTransactionNeeded);
1063
1064 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001065 // create background color layer if one does not yet exist
1066 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001067 std::string name = mName + "BackgroundColorLayer";
Lloyd Pique958be492019-03-28 15:34:59 -07001068 mCurrentState.bgColorLayer = mFlinger->getFactory().createColorLayer(
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001069 LayerCreationArgs(mFlinger.get(), nullptr, std::move(name), 0, 0, flags,
1070 LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -07001071
Valerie Haudd0b7572019-01-29 14:59:27 -08001072 // add to child list
1073 addChild(mCurrentState.bgColorLayer);
1074 mFlinger->mLayersAdded = true;
1075 // set up SF to handle added color layer
1076 if (isRemovedFromCurrentState()) {
1077 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1078 }
1079 mFlinger->setTransactionFlags(eTransactionNeeded);
1080 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1081 mCurrentState.bgColorLayer->reparent(nullptr);
1082 mCurrentState.bgColorLayer = nullptr;
1083 return true;
1084 }
1085
1086 mCurrentState.bgColorLayer->setColor(color);
1087 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1088 mCurrentState.bgColorLayer->setAlpha(alpha);
1089 mCurrentState.bgColorLayer->setDataspace(dataspace);
1090
chaviw13fdc492017-06-27 12:40:18 -07001091 return true;
1092}
1093
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001094bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001095 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001096
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001097 mCurrentState.sequence++;
1098 mCurrentState.cornerRadius = cornerRadius;
1099 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001100 setTransactionFlags(eTransactionNeeded);
1101 return true;
1102}
1103
Robert Carrd4ae7f32018-06-07 16:10:57 -07001104bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1105 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001106 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001107 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1108
1109 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1110 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1111 return false;
1112 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001113 mCurrentState.sequence++;
1114 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1115 matrix.dsdy);
1116 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001117 setTransactionFlags(eTransactionNeeded);
1118 return true;
1119}
Marissa Wall61c58622018-07-18 10:12:20 -07001120
Mathias Agopian13127d82013-03-05 17:47:11 -08001121bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001122 mCurrentState.requestedTransparentRegion_legacy = transparent;
1123 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001124 setTransactionFlags(eTransactionNeeded);
1125 return true;
1126}
1127bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001128 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1129 if (mCurrentState.flags == newFlags) return false;
1130 mCurrentState.sequence++;
1131 mCurrentState.flags = newFlags;
1132 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001133 setTransactionFlags(eTransactionNeeded);
1134 return true;
1135}
Robert Carr99e27f02016-06-16 15:18:02 -07001136
chaviw214c89d2019-09-04 16:03:53 -07001137bool Layer::setCrop_legacy(const Rect& crop) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001138 if (mCurrentState.requestedCrop_legacy == crop) return false;
1139 mCurrentState.sequence++;
1140 mCurrentState.requestedCrop_legacy = crop;
chaviw214c89d2019-09-04 16:03:53 -07001141 mCurrentState.crop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001142
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001143 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001144 setTransactionFlags(eTransactionNeeded);
1145 return true;
1146}
Robert Carr8d5227b2017-03-16 15:41:03 -07001147
Robert Carrc3574f72016-03-24 12:19:32 -07001148bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001149 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001150 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001151 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001152 return true;
1153}
1154
Evan Roskyef876c92019-01-25 17:46:06 -08001155bool Layer::setMetadata(const LayerMetadata& data) {
1156 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001157 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001158 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001159 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001160 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001161}
1162
Mathias Agopian13127d82013-03-05 17:47:11 -08001163bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001164 if (mCurrentState.layerStack == layerStack) return false;
1165 mCurrentState.sequence++;
1166 mCurrentState.layerStack = layerStack;
1167 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001168 setTransactionFlags(eTransactionNeeded);
1169 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001170}
1171
Peiyong Linc502cb72019-03-01 15:00:23 -08001172bool Layer::setColorSpaceAgnostic(const bool agnostic) {
1173 if (mCurrentState.colorSpaceAgnostic == agnostic) {
1174 return false;
1175 }
1176 mCurrentState.sequence++;
1177 mCurrentState.colorSpaceAgnostic = agnostic;
1178 mCurrentState.modified = true;
1179 setTransactionFlags(eTransactionNeeded);
1180 return true;
1181}
1182
Robert Carr1f0a16a2016-10-24 16:27:39 -07001183uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001184 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001185 if (p == nullptr) {
1186 return getDrawingState().layerStack;
1187 }
1188 return p->getLayerStack();
1189}
1190
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001191bool Layer::setShadowRadius(float shadowRadius) {
1192 if (mCurrentState.shadowRadius == shadowRadius) {
1193 return false;
1194 }
1195
1196 mCurrentState.sequence++;
1197 mCurrentState.shadowRadius = shadowRadius;
1198 mCurrentState.modified = true;
1199 setTransactionFlags(eTransactionNeeded);
1200 return true;
1201}
1202
Marissa Wallf58c14b2018-07-24 10:50:43 -07001203void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Alec Mourie60041e2019-06-14 18:59:51 -07001204 ATRACE_CALL();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001205 mCurrentState.barrierLayer_legacy = barrierLayer;
1206 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001207 // We don't set eTransactionNeeded, because just receiving a deferral
1208 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001209 mCurrentState.modified = true;
1210 pushPendingState();
1211 mCurrentState.barrierLayer_legacy = nullptr;
1212 mCurrentState.frameNumber_legacy = 0;
1213 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001214}
1215
Marissa Wallf58c14b2018-07-24 10:50:43 -07001216void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001217 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001218 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001219}
1220
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001221// ----------------------------------------------------------------------------
1222// pageflip handling...
1223// ----------------------------------------------------------------------------
1224
Robert Carr1f0a16a2016-10-24 16:27:39 -07001225bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001226 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001227 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001228 if (parent != nullptr && parent->isHiddenByPolicy()) {
1229 return true;
1230 }
Robert Carr1c5481e2019-07-01 14:42:27 -07001231 if (usingRelativeZ(LayerVector::StateSet::Drawing)) {
1232 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
1233 if (zOrderRelativeOf != nullptr) {
1234 if (zOrderRelativeOf->isHiddenByPolicy()) {
1235 return true;
1236 }
1237 }
1238 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001239 return s.flags & layer_state_t::eLayerHidden;
1240}
1241
David Sodman41fdfc92017-11-06 16:09:56 -08001242uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001243 // TODO: should we do something special if mSecure is set?
1244 if (mProtectedByApp) {
1245 // need a hardware-protected path to external video sink
1246 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001247 }
Riley Andrews03414a12014-07-01 14:22:59 -07001248 if (mPotentialCursor) {
1249 usage |= GraphicBuffer::USAGE_CURSOR;
1250 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001251 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001252 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001253}
1254
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001255void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001256 uint32_t orientation = 0;
Vishnu Nair5eb3f062019-04-08 08:21:03 -07001257 // Disable setting transform hint if the debug flag is set.
1258 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001259 // The transform hint is used to improve performance, but we can
1260 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001261 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001262 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001263 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001264 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001265 orientation = 0;
1266 }
1267 }
David Sodmaneb085e02017-10-05 18:49:04 -07001268 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001269}
1270
Mathias Agopian13127d82013-03-05 17:47:11 -08001271// ----------------------------------------------------------------------------
1272// debugging
1273// ----------------------------------------------------------------------------
1274
Marissa Wall61c58622018-07-18 10:12:20 -07001275// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001276LayerDebugInfo Layer::getLayerDebugInfo() const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001277 using namespace std::string_literals;
1278
Kalle Raitaa099a242017-01-11 11:17:29 -08001279 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001280 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001281 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001282 sp<Layer> parent = getParent();
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001283 info.mParentName = parent ? parent->getName() : "none"s;
chaviw8a01fa42019-08-19 12:39:31 -07001284 info.mType = getType();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001285 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Lloyd Piquea2468662019-03-07 21:31:06 -08001286
1287 info.mVisibleRegion = debugGetVisibleRegionOnDefaultDisplay();
Kalle Raitaa099a242017-01-11 11:17:29 -08001288 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001289 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001290 info.mX = ds.active_legacy.transform.tx();
1291 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001292 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001293 info.mWidth = ds.active_legacy.w;
1294 info.mHeight = ds.active_legacy.h;
1295 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001296 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001297 info.mFlags = ds.flags;
1298 info.mPixelFormat = getPixelFormat();
chaviw4244e032019-09-04 11:27:49 -07001299 info.mDataSpace = static_cast<android_dataspace>(getDataSpace());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001300 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1301 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1302 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1303 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001304 {
chaviwd62d3062019-09-04 14:48:02 -07001305 sp<const GraphicBuffer> buffer = getBuffer();
David Sodman5b4cffc2017-11-23 13:20:29 -08001306 if (buffer != 0) {
1307 info.mActiveBufferWidth = buffer->getWidth();
1308 info.mActiveBufferHeight = buffer->getHeight();
1309 info.mActiveBufferStride = buffer->getStride();
1310 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001311 } else {
1312 info.mActiveBufferWidth = 0;
1313 info.mActiveBufferHeight = 0;
1314 info.mActiveBufferStride = 0;
1315 info.mActiveBufferFormat = 0;
1316 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001317 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001318 info.mNumQueuedFrames = getQueuedFrameCount();
1319 info.mRefreshPending = isBufferLatched();
1320 info.mIsOpaque = isOpaque(ds);
1321 info.mContentDirty = contentDirty;
1322 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001323}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001324
Yiwei Zhang5434a782018-12-05 18:06:32 -08001325void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001326 result.append("-------------------------------");
1327 result.append("-------------------------------");
1328 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001329 result.append(" Layer name\n");
1330 result.append(" Z | ");
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001331 result.append(" Window Type | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001332 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001333 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001334 result.append(" Disp Frame (LTRB) | ");
1335 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001336 result.append("-------------------------------");
1337 result.append("-------------------------------");
1338 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001339}
1340
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001341void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1342 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1343 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001344 return;
1345 }
1346
Yiwei Zhang5434a782018-12-05 18:06:32 -08001347 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001348 if (mName.length() > 77) {
1349 std::string shortened;
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001350 shortened.append(mName, 0, 36);
Dan Stozae22aec72016-08-01 13:20:59 -07001351 shortened.append("[...]");
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001352 shortened.append(mName, mName.length() - 36);
1353 name = std::move(shortened);
Dan Stozae22aec72016-08-01 13:20:59 -07001354 } else {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001355 name = mName;
Dan Stozae22aec72016-08-01 13:20:59 -07001356 }
1357
Yiwei Zhang5434a782018-12-05 18:06:32 -08001358 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001359
Alec Mourib416efd2018-09-06 21:01:59 +00001360 const State& layerState(getDrawingState());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001361 const auto& compositionState = outputLayer->getState();
1362
Chia-I Wu1e043612018-03-01 09:45:09 -08001363 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001364 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001365 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001366 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001367 }
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001368 StringAppendF(&result, " %10d | ", mWindowType);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001369 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
1370 StringAppendF(&result, "%10s | ",
1371 toString(getCompositionLayer() ? compositionState.bufferTransform
1372 : static_cast<Hwc2::Transform>(0))
1373 .c_str());
1374 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001375 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001376 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001377 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1378 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001379
Yichi Chen6ca35192018-05-29 12:20:43 +08001380 result.append("- - - - - - - - - - - - - - - -");
1381 result.append("- - - - - - - - - - - - - - - -");
1382 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001383}
Dan Stozae22aec72016-08-01 13:20:59 -07001384
Yiwei Zhang5434a782018-12-05 18:06:32 -08001385void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001386 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001387}
1388
Svetoslavd85084b2014-03-20 10:28:31 -07001389void Layer::clearFrameStats() {
1390 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001391}
1392
Jamie Gennis6547ff42013-07-16 20:12:42 -07001393void Layer::logFrameStats() {
1394 mFrameTracker.logAndResetStats(mName);
1395}
1396
Svetoslavd85084b2014-03-20 10:28:31 -07001397void Layer::getFrameStats(FrameStats* outStats) const {
1398 mFrameTracker.getStats(outStats);
1399}
1400
Yiwei Zhang5434a782018-12-05 18:06:32 -08001401void Layer::dumpFrameEvents(std::string& result) {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001402 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().c_str(), getType(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001403 Mutex::Autolock lock(mFrameEventHistoryMutex);
1404 mFrameEventHistory.checkFencesForCompletion();
1405 mFrameEventHistory.dump(result);
1406}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001407
Vishnu Nair0f085c62019-08-30 08:49:12 -07001408void Layer::dumpCallingUidPid(std::string& result) const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001409 StringAppendF(&result, "Layer %s (%s) pid:%d uid:%d\n", getName().c_str(), getType(),
Vishnu Nair0f085c62019-08-30 08:49:12 -07001410 mCallingPid, mCallingUid);
1411}
1412
Brian Anderson5ea5e592016-12-01 16:54:33 -08001413void Layer::onDisconnect() {
1414 Mutex::Autolock lock(mFrameEventHistoryMutex);
1415 mFrameEventHistory.onDisconnect();
Yiwei Zhang1a88c402019-11-18 10:43:58 -08001416 const int32_t layerId = getSequence();
1417 mFlinger->mTimeStats->onDestroy(layerId);
1418 mFlinger->mFrameTracer->onDestroy(layerId);
Brian Anderson5ea5e592016-12-01 16:54:33 -08001419}
1420
Brian Anderson3890c392016-07-25 12:48:08 -07001421void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001422 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001423 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001424 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1425 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhang487340f2019-11-18 17:42:12 -08001426 mFlinger->mTimeStats->setAcquireFence(getSequence(), newTimestamps->frameNumber,
1427 newTimestamps->acquireFence);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001428 }
1429
Brian Andersond6927fb2016-07-23 23:37:30 -07001430 Mutex::Autolock lock(mFrameEventHistoryMutex);
1431 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001432 // If there are any unsignaled fences in the aquire timeline at this
1433 // point, the previously queued frame hasn't been latched yet. Go ahead
1434 // and try to get the signal time here so the syscall is taken out of
1435 // the main thread's critical path.
1436 mAcquireTimeline.updateSignalTimes();
1437 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001438 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001439 mFrameEventHistory.addQueue(*newTimestamps);
1440 }
1441
Brian Anderson3890c392016-07-25 12:48:08 -07001442 if (outDelta) {
1443 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001444 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001445}
Dan Stozae77c7662016-05-13 11:37:28 -07001446
Chia-I Wu98f1c102017-05-30 14:54:08 -07001447size_t Layer::getChildrenCount() const {
1448 size_t count = 0;
1449 for (const sp<Layer>& child : mCurrentChildren) {
1450 count += 1 + child->getChildrenCount();
1451 }
1452 return count;
1453}
1454
Robert Carr1f0a16a2016-10-24 16:27:39 -07001455void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001456 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001457 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001458
Robert Carr1f0a16a2016-10-24 16:27:39 -07001459 mCurrentChildren.add(layer);
1460 layer->setParent(this);
1461}
1462
1463ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001464 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001465 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001466
Robert Carr1323c952019-01-28 18:13:27 -08001467 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001468 return mCurrentChildren.remove(layer);
1469}
1470
Robert Carr1db73f62016-12-21 12:58:51 -08001471bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1472 sp<Handle> handle = nullptr;
1473 sp<Layer> newParent = nullptr;
1474 if (newParentHandle == nullptr) {
1475 return false;
1476 }
1477 handle = static_cast<Handle*>(newParentHandle.get());
1478 newParent = handle->owner.promote();
1479 if (newParent == nullptr) {
1480 ALOGE("Unable to promote Layer handle");
1481 return false;
1482 }
1483
chaviw5aedec92018-10-22 10:40:38 -07001484 if (attachChildren()) {
1485 setTransactionFlags(eTransactionNeeded);
1486 }
Robert Carr1db73f62016-12-21 12:58:51 -08001487 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001488 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001489 }
1490 mCurrentChildren.clear();
1491
1492 return true;
1493}
1494
Robert Carr15eae092018-03-23 13:43:53 -07001495void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001496 for (const sp<Layer>& child : mDrawingChildren) {
1497 child->mDrawingParent = newParent;
Vishnu Nairc652ff82019-03-15 12:48:54 -07001498 child->computeBounds(newParent->mBounds,
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001499 newParent->getTransformWithScale(newParent->getBufferScaleTransform()),
1500 newParent->mEffectiveShadowRadius);
Robert Carr578038f2018-03-09 12:25:24 -08001501 }
1502}
1503
chaviwf1961f72017-09-18 16:41:07 -07001504bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001505 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001506
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001507 // While layers are detached, we allow most operations
1508 // and simply halt performing the actual transaction. However
1509 // for reparent != null we would enter the mRemovedFromCurrentState
1510 // state, regardless of whether doTransaction was called, and
1511 // so we need to prevent the update here.
1512 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001513 return false;
1514 }
1515
Robert Carr54cf5b12019-01-25 14:02:28 -08001516 sp<Layer> newParent;
1517 if (newParentHandle != nullptr) {
1518 auto handle = static_cast<Handle*>(newParentHandle.get());
1519 newParent = handle->owner.promote();
1520 if (newParent == nullptr) {
1521 ALOGE("Unable to promote Layer handle");
1522 return false;
1523 }
1524 if (newParent == this) {
1525 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1526 return false;
1527 }
1528 }
1529
chaviwf1961f72017-09-18 16:41:07 -07001530 sp<Layer> parent = getParent();
1531 if (parent != nullptr) {
1532 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001533 }
1534
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001535 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001536 newParent->addChild(this);
1537 if (!newParent->isRemovedFromCurrentState()) {
1538 addToCurrentState();
1539 } else {
1540 onRemovedFromCurrentState();
1541 }
1542
1543 if (mLayerDetached) {
1544 mLayerDetached = false;
1545 callSetTransactionFlags = true;
1546 }
1547 } else {
1548 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001549 }
1550
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001551 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001552 setTransactionFlags(eTransactionNeeded);
1553 }
chaviw06178942017-07-27 10:25:59 -07001554 return true;
1555}
1556
Robert Carr9524cb32017-02-13 11:32:32 -08001557bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001558 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001559 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001560 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001561 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001562 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001563 child->detachChildren();
chaviw43cb3cb2019-05-31 15:23:41 -07001564 child->removeRemoteSyncPoints();
Robert Carr9524cb32017-02-13 11:32:32 -08001565 }
Robert Carr7f619b22017-11-06 12:56:35 -08001566 }
Robert Carr9524cb32017-02-13 11:32:32 -08001567
1568 return true;
1569}
1570
chaviw5aedec92018-10-22 10:40:38 -07001571bool Layer::attachChildren() {
1572 bool changed = false;
1573 for (const sp<Layer>& child : mCurrentChildren) {
1574 sp<Client> parentClient = mClientRef.promote();
1575 sp<Client> client(child->mClientRef.promote());
1576 if (client != nullptr && parentClient != client) {
1577 if (child->mLayerDetached) {
1578 child->mLayerDetached = false;
1579 changed = true;
1580 }
1581 changed |= child->attachChildren();
1582 }
1583 }
1584
1585 return changed;
1586}
1587
Peiyong Lind3788632018-09-18 16:01:31 -07001588bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001589 static const mat4 identityMatrix = mat4();
1590
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001591 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001592 return false;
1593 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001594 ++mCurrentState.sequence;
1595 mCurrentState.colorTransform = matrix;
1596 mCurrentState.hasColorTransform = matrix != identityMatrix;
1597 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001598 setTransactionFlags(eTransactionNeeded);
1599 return true;
1600}
1601
chaviwf66724d2018-11-28 16:35:21 -08001602mat4 Layer::getColorTransform() const {
1603 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1604 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1605 colorTransform = parent->getColorTransform() * colorTransform;
1606 }
1607 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001608}
1609
1610bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001611 bool hasColorTransform = getDrawingState().hasColorTransform;
1612 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1613 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1614 }
1615 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001616}
1617
Chia-I Wu11481472018-05-04 10:43:19 -07001618bool Layer::isLegacyDataSpace() const {
1619 // return true when no higher bits are set
chaviw4244e032019-09-04 11:27:49 -07001620 return !(getDataSpace() &
1621 (ui::Dataspace::STANDARD_MASK | ui::Dataspace::TRANSFER_MASK |
1622 ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001623}
1624
Robert Carr1f0a16a2016-10-24 16:27:39 -07001625void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001626 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001627}
1628
chaviw301b1d82019-11-06 13:15:09 -08001629int32_t Layer::getZ(LayerVector::StateSet stateSet) const {
1630 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1631 const State& state = useDrawing ? mDrawingState : mCurrentState;
1632 return state.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001633}
1634
Robert Carr1c5481e2019-07-01 14:42:27 -07001635bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) const {
Robert Carr29abff82017-12-04 13:51:20 -08001636 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001637 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviwe5ac40f2019-09-24 16:36:55 -07001638 return state.isRelativeOf;
Robert Carr29abff82017-12-04 13:51:20 -08001639}
1640
David Sodman41fdfc92017-11-06 16:09:56 -08001641__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001642 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001643 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1644 "makeTraversalList received invalid stateSet");
1645 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1646 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001647 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001648
Robert Carr29abff82017-12-04 13:51:20 -08001649 if (state.zOrderRelatives.size() == 0) {
1650 *outSkipRelativeZUsers = true;
1651 return children;
1652 }
1653
chaviwfd462612018-05-31 16:11:27 -07001654 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001655 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001656 sp<Layer> strongRelative = weakRelative.promote();
1657 if (strongRelative != nullptr) {
1658 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001659 }
1660 }
1661
Dan Stoza412903f2017-04-27 13:42:17 -07001662 for (const sp<Layer>& child : children) {
chaviwe5ac40f2019-09-24 16:36:55 -07001663 if (child->usingRelativeZ(stateSet)) {
Robert Carr503c7042017-09-27 15:06:08 -07001664 continue;
1665 }
Robert Carrdb66e622017-04-10 16:55:57 -07001666 traverse.add(child);
1667 }
1668
1669 return traverse;
1670}
1671
Robert Carr1f0a16a2016-10-24 16:27:39 -07001672/**
Robert Carrdb66e622017-04-10 16:55:57 -07001673 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001674 */
Dan Stoza412903f2017-04-27 13:42:17 -07001675void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001676 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1677 // produce a new list for traversing, including our relatives, and not including our children
1678 // who are relatives of another surface. In the case that there are no relative Z,
1679 // makeTraversalList returns our children directly to avoid significant overhead.
1680 // However in this case we need to take the responsibility for filtering children which
1681 // are relatives of another surface here.
1682 bool skipRelativeZUsers = false;
1683 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001684
Robert Carr1f0a16a2016-10-24 16:27:39 -07001685 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001686 for (; i < list.size(); i++) {
1687 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001688 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1689 continue;
1690 }
1691
chaviw301b1d82019-11-06 13:15:09 -08001692 if (relative->getZ(stateSet) >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001693 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001694 }
Dan Stoza412903f2017-04-27 13:42:17 -07001695 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001696 }
Robert Carr29abff82017-12-04 13:51:20 -08001697
Dan Stoza412903f2017-04-27 13:42:17 -07001698 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001699 for (; i < list.size(); i++) {
1700 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001701
Robert Carr29abff82017-12-04 13:51:20 -08001702 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1703 continue;
1704 }
Dan Stoza412903f2017-04-27 13:42:17 -07001705 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001706 }
1707}
1708
1709/**
Robert Carrdb66e622017-04-10 16:55:57 -07001710 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001711 */
Dan Stoza412903f2017-04-27 13:42:17 -07001712void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1713 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001714 // See traverseInZOrder for documentation.
1715 bool skipRelativeZUsers = false;
1716 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001717
Robert Carr1f0a16a2016-10-24 16:27:39 -07001718 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001719 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001720 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001721
1722 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1723 continue;
1724 }
1725
chaviw301b1d82019-11-06 13:15:09 -08001726 if (relative->getZ(stateSet) < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001727 break;
1728 }
Dan Stoza412903f2017-04-27 13:42:17 -07001729 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001730 }
Dan Stoza412903f2017-04-27 13:42:17 -07001731 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001732 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001733 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001734
1735 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1736 continue;
1737 }
1738
Dan Stoza412903f2017-04-27 13:42:17 -07001739 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001740 }
1741}
1742
chaviw4b129c22018-04-09 16:19:43 -07001743LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1744 const std::vector<Layer*>& layersInTree) {
1745 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1746 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001747 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1748 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001749 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001750
chaviwfd462612018-05-31 16:11:27 -07001751 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001752 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1753 sp<Layer> strongRelative = weakRelative.promote();
1754 // Only add relative layers that are also descendents of the top most parent of the tree.
1755 // If a relative layer is not a descendent, then it should be ignored.
1756 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1757 traverse.add(strongRelative);
1758 }
1759 }
1760
1761 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001762 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001763 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1764 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1765 // the child here since it won't be added later as a relative.
1766 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1767 childState.zOrderRelativeOf.promote().get())) {
1768 continue;
1769 }
1770 traverse.add(child);
1771 }
1772
1773 return traverse;
1774}
1775
1776void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1777 LayerVector::StateSet stateSet,
1778 const LayerVector::Visitor& visitor) {
1779 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001780
1781 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001782 for (; i < list.size(); i++) {
1783 const auto& relative = list[i];
chaviw301b1d82019-11-06 13:15:09 -08001784 if (relative->getZ(stateSet) >= 0) {
chaviwa76b2712017-09-20 12:02:26 -07001785 break;
1786 }
chaviw4b129c22018-04-09 16:19:43 -07001787 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001788 }
chaviw4b129c22018-04-09 16:19:43 -07001789
chaviwa76b2712017-09-20 12:02:26 -07001790 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001791 for (; i < list.size(); i++) {
1792 const auto& relative = list[i];
1793 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001794 }
1795}
1796
chaviw4b129c22018-04-09 16:19:43 -07001797std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1798 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1799 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1800
1801 std::vector<Layer*> layersInTree = {this};
1802 for (size_t i = 0; i < children.size(); i++) {
1803 const auto& child = children[i];
1804 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1805 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1806 }
1807
1808 return layersInTree;
1809}
1810
1811void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1812 const LayerVector::Visitor& visitor) {
1813 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1814 std::sort(layersInTree.begin(), layersInTree.end());
1815 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1816}
1817
Peiyong Linefefaac2018-08-17 12:27:51 -07001818ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001819 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001820}
1821
chaviw13fdc492017-06-27 12:40:18 -07001822half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001823 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001824
chaviw13fdc492017-06-27 12:40:18 -07001825 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1826 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001827}
Robert Carr6452f122017-03-21 10:41:29 -07001828
chaviw13fdc492017-06-27 12:40:18 -07001829half4 Layer::getColor() const {
1830 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001831 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001832}
Robert Carr6452f122017-03-21 10:41:29 -07001833
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001834Layer::RoundedCornerState Layer::getRoundedCornerState() const {
1835 const auto& p = mDrawingParent.promote();
1836 if (p != nullptr) {
Peiyong Lin27016a92019-03-29 17:36:08 +00001837 RoundedCornerState parentState = p->getRoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001838 if (parentState.radius > 0) {
1839 ui::Transform t = getActiveTransform(getDrawingState());
1840 t = t.inverse();
1841 parentState.cropRect = t.transform(parentState.cropRect);
1842 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1843 // but a transform matrix can define horizontal and vertical scales.
1844 // Let's take the average between both of them and pass into the shader, practically we
1845 // never do this type of transformation on windows anyway.
1846 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
1847 return parentState;
1848 }
1849 }
1850 const float radius = getDrawingState().cornerRadius;
Peiyong Linb9ff23e2019-04-08 11:04:59 -07001851 return radius > 0 && getCrop(getDrawingState()).isValid()
1852 ? RoundedCornerState(getCrop(getDrawingState()).toFloatRect(), radius)
1853 : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001854}
1855
Vishnu Nair08f6eae2019-11-26 14:01:39 -08001856renderengine::ShadowSettings Layer::getShadowSettings(const Rect& viewport) const {
1857 renderengine::ShadowSettings state = mFlinger->mDrawingState.globalShadowSettings;
1858
1859 // Shift the spot light x-position to the middle of the display and then
1860 // offset it by casting layer's screen pos.
1861 state.lightPos.x = (viewport.width() / 2.f) - mScreenBounds.left;
1862 state.lightPos.y -= mScreenBounds.top;
1863
1864 state.length = mEffectiveShadowRadius;
1865 return state;
1866}
1867
Robert Carr1f0a16a2016-10-24 16:27:39 -07001868void Layer::commitChildList() {
1869 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1870 const auto& child = mCurrentChildren[i];
1871 child->commitChildList();
1872 }
1873 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001874 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001875}
1876
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001877static wp<Layer> extractLayerFromBinder(const wp<IBinder>& weakBinderHandle) {
1878 if (weakBinderHandle == nullptr) {
1879 return nullptr;
1880 }
1881 sp<IBinder> binderHandle = weakBinderHandle.promote();
1882 if (binderHandle == nullptr) {
1883 return nullptr;
1884 }
1885 sp<Layer::Handle> handle = static_cast<Layer::Handle*>(binderHandle.get());
1886 if (handle == nullptr) {
1887 return nullptr;
1888 }
1889 return handle->owner;
1890}
1891
Robert Carr720e5062018-07-30 17:45:14 -07001892void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001893 mCurrentState.inputInfo = info;
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001894 mCurrentState.touchableRegionCrop = extractLayerFromBinder(info.touchableRegionCropHandle);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001895 mCurrentState.modified = true;
1896 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07001897 setTransactionFlags(eTransactionNeeded);
1898}
1899
chaviw6d89e2d2020-01-14 14:42:01 -08001900LayerProto* Layer::writeToProto(LayersProto& layersProto, uint32_t traceFlags) const {
chaviw08f3cb22020-01-13 13:17:21 -08001901 LayerProto* layerProto = layersProto.add_layers();
1902 writeToProtoDrawingState(layerProto, traceFlags);
1903 writeToProtoCommonState(layerProto, LayerVector::StateSet::Drawing, traceFlags);
1904
1905 for (const sp<Layer>& layer : mDrawingChildren) {
1906 layer->writeToProto(layersProto, traceFlags);
1907 }
chaviw6d89e2d2020-01-14 14:42:01 -08001908
1909 return layerProto;
chaviw08f3cb22020-01-13 13:17:21 -08001910}
1911
Vishnu Nair8406fd72019-07-30 11:29:31 -07001912void Layer::writeToProtoDrawingState(LayerProto* layerInfo, uint32_t traceFlags) const {
1913 ui::Transform transform = getTransform();
1914
1915 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
1916 for (const auto& pendingState : mPendingStatesSnapshot) {
1917 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
1918 if (barrierLayer != nullptr) {
1919 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
1920 barrierLayerProto->set_id(barrierLayer->sequence);
1921 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
1922 }
1923 }
1924
chaviwd62d3062019-09-04 14:48:02 -07001925 auto buffer = getBuffer();
Vishnu Nair8406fd72019-07-30 11:29:31 -07001926 if (buffer != nullptr) {
1927 LayerProtoHelper::writeToProto(buffer,
1928 [&]() { return layerInfo->mutable_active_buffer(); });
chaviw4244e032019-09-04 11:27:49 -07001929 LayerProtoHelper::writeToProto(ui::Transform(getBufferTransform()),
Vishnu Nair8406fd72019-07-30 11:29:31 -07001930 layerInfo->mutable_buffer_transform());
1931 }
1932 layerInfo->set_invalidate(contentDirty);
1933 layerInfo->set_is_protected(isProtected());
chaviw4244e032019-09-04 11:27:49 -07001934 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(getDataSpace())));
Vishnu Nair8406fd72019-07-30 11:29:31 -07001935 layerInfo->set_queued_frames(getQueuedFrameCount());
1936 layerInfo->set_refresh_pending(isBufferLatched());
1937 layerInfo->set_curr_frame(mCurrentFrameNumber);
1938 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
1939
1940 layerInfo->set_corner_radius(getRoundedCornerState().radius);
1941 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1942 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
1943 [&]() { return layerInfo->mutable_position(); });
1944 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
Lloyd Piquea2468662019-03-07 21:31:06 -08001945 LayerProtoHelper::writeToProto(debugGetVisibleRegionOnDefaultDisplay(),
Vishnu Nair8406fd72019-07-30 11:29:31 -07001946 [&]() { return layerInfo->mutable_visible_region(); });
1947 LayerProtoHelper::writeToProto(surfaceDamageRegion,
1948 [&]() { return layerInfo->mutable_damage_region(); });
chaviwddeae262020-01-06 10:31:23 -08001949
1950 if (hasColorTransform()) {
1951 LayerProtoHelper::writeToProto(getColorTransform(),
1952 layerInfo->mutable_color_transform());
1953 }
Vishnu Nair8406fd72019-07-30 11:29:31 -07001954 }
1955
1956 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
1957 LayerProtoHelper::writeToProto(mSourceBounds,
1958 [&]() { return layerInfo->mutable_source_bounds(); });
1959 LayerProtoHelper::writeToProto(mScreenBounds,
1960 [&]() { return layerInfo->mutable_screen_bounds(); });
Vishnu Nair95a1ed42019-12-06 12:25:11 -08001961 LayerProtoHelper::writeToProto(getRoundedCornerState().cropRect,
1962 [&]() { return layerInfo->mutable_corner_radius_crop(); });
1963 layerInfo->set_shadow_radius(mEffectiveShadowRadius);
Vishnu Nair8406fd72019-07-30 11:29:31 -07001964 }
1965}
1966
1967void Layer::writeToProtoCommonState(LayerProto* layerInfo, LayerVector::StateSet stateSet,
1968 uint32_t traceFlags) const {
chaviw1d044282017-09-27 12:19:28 -07001969 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1970 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001971 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07001972
Peiyong Linefefaac2018-08-17 12:27:51 -07001973 ui::Transform requestedTransform = state.active_legacy.transform;
chaviw1d044282017-09-27 12:19:28 -07001974
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001975 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
1976 layerInfo->set_id(sequence);
1977 layerInfo->set_name(getName().c_str());
chaviw8a01fa42019-08-19 12:39:31 -07001978 layerInfo->set_type(getType());
chaviw1d044282017-09-27 12:19:28 -07001979
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001980 for (const auto& child : children) {
1981 layerInfo->add_children(child->sequence);
chaviw1d044282017-09-27 12:19:28 -07001982 }
chaviw1d044282017-09-27 12:19:28 -07001983
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001984 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1985 sp<Layer> strongRelative = weakRelative.promote();
1986 if (strongRelative != nullptr) {
1987 layerInfo->add_relatives(strongRelative->sequence);
1988 }
chaviwadc40c22018-07-10 16:57:27 -07001989 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001990
1991 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
1992 [&]() { return layerInfo->mutable_transparent_region(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001993
1994 layerInfo->set_layer_stack(getLayerStack());
1995 layerInfo->set_z(state.z);
1996
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001997 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(),
1998 [&]() {
1999 return layerInfo->mutable_requested_position();
2000 });
2001
2002 LayerProtoHelper::writeSizeToProto(state.active_legacy.w, state.active_legacy.h,
2003 [&]() { return layerInfo->mutable_size(); });
2004
2005 LayerProtoHelper::writeToProto(state.crop_legacy,
2006 [&]() { return layerInfo->mutable_crop(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002007
2008 layerInfo->set_is_opaque(isOpaque(state));
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002009
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002010
2011 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
2012 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
2013 LayerProtoHelper::writeToProto(state.color,
2014 [&]() { return layerInfo->mutable_requested_color(); });
2015 layerInfo->set_flags(state.flags);
2016
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002017 LayerProtoHelper::writeToProto(requestedTransform,
2018 layerInfo->mutable_requested_transform());
2019
2020 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
2021 if (parent != nullptr) {
2022 layerInfo->set_parent(parent->sequence);
2023 } else {
2024 layerInfo->set_parent(-1);
2025 }
2026
2027 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2028 if (zOrderRelativeOf != nullptr) {
2029 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
2030 } else {
2031 layerInfo->set_z_order_relative_of(-1);
2032 }
chaviw08f3cb22020-01-13 13:17:21 -08002033
2034 layerInfo->set_is_relative_of(state.isRelativeOf);
chaviwadc40c22018-07-10 16:57:27 -07002035 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002036
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002037 if (traceFlags & SurfaceTracing::TRACE_INPUT) {
2038 LayerProtoHelper::writeToProto(state.inputInfo, state.touchableRegionCrop,
2039 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002040 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002041
2042 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
2043 auto protoMap = layerInfo->mutable_metadata();
2044 for (const auto& entry : state.metadata.mMap) {
2045 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
2046 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002047 }
chaviw1d044282017-09-27 12:19:28 -07002048}
2049
Robert Carr2e102c92018-10-23 12:11:15 -07002050bool Layer::isRemovedFromCurrentState() const {
2051 return mRemovedFromCurrentState;
2052}
2053
Arthur Hungd20b2702019-01-14 18:16:16 +08002054InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002055 InputWindowInfo info = mDrawingState.inputInfo;
chaviwaf87b3e2019-10-01 16:59:28 -07002056 info.id = sequence;
Robert Carr720e5062018-07-30 17:45:14 -07002057
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002058 if (info.displayId == ADISPLAY_ID_NONE) {
chaviwb15ea8a2019-09-03 12:40:22 -07002059 info.displayId = getLayerStack();
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002060 }
2061
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002062 ui::Transform t = getTransform();
2063 const float xScale = t.sx();
2064 const float yScale = t.sy();
Ady Abraham282f1d72019-07-24 18:05:56 -07002065 int32_t xSurfaceInset = info.surfaceInset;
2066 int32_t ySurfaceInset = info.surfaceInset;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002067 if (xScale != 1.0f || yScale != 1.0f) {
Ady Abraham282f1d72019-07-24 18:05:56 -07002068 info.windowXScale *= (xScale != 0.0f) ? 1.0f / xScale : 0.0f;
2069 info.windowYScale *= (yScale != 0.0f) ? 1.0f / yScale : 0.0f;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002070 info.touchableRegion.scaleSelf(xScale, yScale);
Ady Abraham282f1d72019-07-24 18:05:56 -07002071 xSurfaceInset = std::round(xSurfaceInset * xScale);
2072 ySurfaceInset = std::round(ySurfaceInset * yScale);
Riddle Hsu39d4aa52018-11-30 20:46:53 +08002073 }
Robert Carre07e1032018-11-26 12:55:53 -08002074
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002075 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002076 // If this is a portal window, set the touchableRegion to the layerBounds.
2077 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
2078 ? getBufferSize(getDrawingState())
2079 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002080 if (!layerBounds.isValid()) {
2081 layerBounds = getCroppedBufferSize(getDrawingState());
2082 }
Vishnu Nair8033a492018-12-05 07:27:23 -08002083 layerBounds = t.transform(layerBounds);
Ady Abraham282f1d72019-07-24 18:05:56 -07002084
2085 // clamp inset to layer bounds
2086 xSurfaceInset = (xSurfaceInset >= 0) ? std::min(xSurfaceInset, layerBounds.getWidth() / 2) : 0;
2087 ySurfaceInset = (ySurfaceInset >= 0) ? std::min(ySurfaceInset, layerBounds.getHeight() / 2) : 0;
2088
Arthur Hung118b1142019-05-08 21:25:59 +08002089 layerBounds.inset(xSurfaceInset, ySurfaceInset, xSurfaceInset, ySurfaceInset);
Vishnu Nair8033a492018-12-05 07:27:23 -08002090
Arthur Hungd20b2702019-01-14 18:16:16 +08002091 // Input coordinate should match the layer bounds.
2092 info.frameLeft = layerBounds.left;
2093 info.frameTop = layerBounds.top;
2094 info.frameRight = layerBounds.right;
2095 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08002096
2097 // Position the touchable region relative to frame screen location and restrict it to frame
2098 // bounds.
2099 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08002100 info.visible = canReceiveInput();
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002101
2102 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2103 if (info.replaceTouchableRegionWithCrop) {
2104 if (cropLayer == nullptr) {
2105 info.touchableRegion = Region(Rect{mScreenBounds});
2106 } else {
2107 info.touchableRegion = Region(Rect{cropLayer->mScreenBounds});
2108 }
2109 } else if (cropLayer != nullptr) {
2110 info.touchableRegion = info.touchableRegion.intersect(Rect{cropLayer->mScreenBounds});
2111 }
2112
chaviwaf87b3e2019-10-01 16:59:28 -07002113 // If the layer is a clone, we need to crop the input region to cloned root to prevent
2114 // touches from going outside the cloned area.
2115 if (isClone()) {
2116 sp<Layer> clonedRoot = getClonedRoot();
2117 if (clonedRoot != nullptr) {
2118 Rect rect(clonedRoot->mScreenBounds);
2119 info.touchableRegion = info.touchableRegion.intersect(rect);
2120 }
2121 }
2122
Robert Carr720e5062018-07-30 17:45:14 -07002123 return info;
2124}
2125
chaviwaf87b3e2019-10-01 16:59:28 -07002126sp<Layer> Layer::getClonedRoot() {
2127 if (mClonedChild != nullptr) {
2128 return this;
2129 }
2130 if (mDrawingParent == nullptr || mDrawingParent.promote() == nullptr) {
2131 return nullptr;
2132 }
2133 return mDrawingParent.promote()->getClonedRoot();
2134}
2135
Robert Carr720e5062018-07-30 17:45:14 -07002136bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002137 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002138}
2139
Lloyd Piquefeb73d72018-12-04 17:23:44 -08002140std::shared_ptr<compositionengine::Layer> Layer::getCompositionLayer() const {
2141 return nullptr;
2142}
2143
chaviw3e727cd2019-01-31 13:41:05 -08002144bool Layer::canReceiveInput() const {
Vishnu Nair82353e92019-09-12 12:38:47 -07002145 return !isHiddenByPolicy();
chaviw3e727cd2019-01-31 13:41:05 -08002146}
2147
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002148compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
2149 const sp<const DisplayDevice>& display) const {
2150 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionLayer().get());
2151}
2152
Lloyd Piquea2468662019-03-07 21:31:06 -08002153Region Layer::debugGetVisibleRegionOnDefaultDisplay() const {
2154 sp<DisplayDevice> displayDevice = mFlinger->getDefaultDisplayDeviceLocked();
2155 if (displayDevice == nullptr) {
2156 return {};
2157 }
2158
2159 auto outputLayer = findOutputLayerForDisplay(displayDevice);
2160 if (outputLayer == nullptr) {
2161 return {};
2162 }
2163
2164 return outputLayer->getState().visibleRegion;
2165}
2166
chaviwb4c6e582019-08-16 14:35:07 -07002167void Layer::setInitialValuesForClone(const sp<Layer>& clonedFrom) {
2168 // copy drawing state from cloned layer
2169 mDrawingState = clonedFrom->mDrawingState;
2170 mClonedFrom = clonedFrom;
chaviwb4c6e582019-08-16 14:35:07 -07002171}
chaviw74b03172019-08-19 11:09:03 -07002172
2173void Layer::updateMirrorInfo() {
2174 if (mClonedChild == nullptr || !mClonedChild->isClonedFromAlive()) {
2175 // If mClonedChild is null, there is nothing to mirror. If isClonedFromAlive returns false,
2176 // it means that there is a clone, but the layer it was cloned from has been destroyed. In
2177 // that case, we want to delete the reference to the clone since we want it to get
2178 // destroyed. The root, this layer, will still be around since the client can continue
2179 // to hold a reference, but no cloned layers will be displayed.
2180 mClonedChild = nullptr;
2181 return;
2182 }
2183
2184 std::map<sp<Layer>, sp<Layer>> clonedLayersMap;
2185 // If the real layer exists and is in current state, add the clone as a child of the root.
2186 // There's no need to remove from drawingState when the layer is offscreen since currentState is
2187 // copied to drawingState for the root layer. So the clonedChild is always removed from
2188 // drawingState and then needs to be added back each traversal.
2189 if (!mClonedChild->getClonedFrom()->isRemovedFromCurrentState()) {
2190 addChildToDrawing(mClonedChild);
2191 }
2192
2193 mClonedChild->updateClonedDrawingState(clonedLayersMap);
2194 mClonedChild->updateClonedChildren(this, clonedLayersMap);
2195 mClonedChild->updateClonedRelatives(clonedLayersMap);
2196}
2197
2198void Layer::updateClonedDrawingState(std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2199 // If the layer the clone was cloned from is alive, copy the content of the drawingState
2200 // to the clone. If the real layer is no longer alive, continue traversing the children
2201 // since we may be able to pull out other children that are still alive.
2202 if (isClonedFromAlive()) {
2203 sp<Layer> clonedFrom = getClonedFrom();
2204 mDrawingState = clonedFrom->mDrawingState;
chaviw74b03172019-08-19 11:09:03 -07002205 clonedLayersMap.emplace(clonedFrom, this);
2206 }
2207
2208 // The clone layer may have children in drawingState since they may have been created and
2209 // added from a previous request to updateMirorInfo. This is to ensure we don't recreate clones
2210 // that already exist, since we can just re-use them.
2211 // The drawingChildren will not get overwritten by the currentChildren since the clones are
2212 // not updated in the regular traversal. They are skipped since the root will lose the
2213 // reference to them when it copies its currentChildren to drawing.
2214 for (sp<Layer>& child : mDrawingChildren) {
2215 child->updateClonedDrawingState(clonedLayersMap);
2216 }
2217}
2218
2219void Layer::updateClonedChildren(const sp<Layer>& mirrorRoot,
2220 std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2221 mDrawingChildren.clear();
2222
2223 if (!isClonedFromAlive()) {
2224 return;
2225 }
2226
2227 sp<Layer> clonedFrom = getClonedFrom();
2228 for (sp<Layer>& child : clonedFrom->mDrawingChildren) {
2229 if (child == mirrorRoot) {
2230 // This is to avoid cyclical mirroring.
2231 continue;
2232 }
2233 sp<Layer> clonedChild = clonedLayersMap[child];
2234 if (clonedChild == nullptr) {
2235 clonedChild = child->createClone();
2236 clonedLayersMap[child] = clonedChild;
2237 }
2238 addChildToDrawing(clonedChild);
2239 clonedChild->updateClonedChildren(mirrorRoot, clonedLayersMap);
2240 }
2241}
2242
chaviwaf87b3e2019-10-01 16:59:28 -07002243void Layer::updateClonedInputInfo(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2244 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2245 if (cropLayer != nullptr) {
2246 if (clonedLayersMap.count(cropLayer) == 0) {
2247 // Real layer had a crop layer but it's not in the cloned hierarchy. Just set to
2248 // self as crop layer to avoid going outside bounds.
2249 mDrawingState.touchableRegionCrop = this;
2250 } else {
2251 const sp<Layer>& clonedCropLayer = clonedLayersMap.at(cropLayer);
2252 mDrawingState.touchableRegionCrop = clonedCropLayer;
2253 }
2254 }
2255 // Cloned layers shouldn't handle watch outside since their z order is not determined by
2256 // WM or the client.
2257 mDrawingState.inputInfo.layoutParamsFlags &= ~InputWindowInfo::FLAG_WATCH_OUTSIDE_TOUCH;
2258}
2259
2260void Layer::updateClonedRelatives(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
chaviw74b03172019-08-19 11:09:03 -07002261 mDrawingState.zOrderRelativeOf = nullptr;
2262 mDrawingState.zOrderRelatives.clear();
2263
2264 if (!isClonedFromAlive()) {
2265 return;
2266 }
2267
chaviwaf87b3e2019-10-01 16:59:28 -07002268 const sp<Layer>& clonedFrom = getClonedFrom();
chaviw74b03172019-08-19 11:09:03 -07002269 for (wp<Layer>& relativeWeak : clonedFrom->mDrawingState.zOrderRelatives) {
chaviwaf87b3e2019-10-01 16:59:28 -07002270 const sp<Layer>& relative = relativeWeak.promote();
2271 if (clonedLayersMap.count(relative) > 0) {
2272 auto& clonedRelative = clonedLayersMap.at(relative);
chaviw74b03172019-08-19 11:09:03 -07002273 mDrawingState.zOrderRelatives.add(clonedRelative);
2274 }
2275 }
2276
2277 // Check if the relativeLayer for the real layer is part of the cloned hierarchy.
2278 // It's possible that the layer it's relative to is outside the requested cloned hierarchy.
2279 // In that case, we treat the layer as if the relativeOf has been removed. This way, it will
2280 // still traverse the children, but the layer with the missing relativeOf will not be shown
2281 // on screen.
chaviwaf87b3e2019-10-01 16:59:28 -07002282 const sp<Layer>& relativeOf = clonedFrom->mDrawingState.zOrderRelativeOf.promote();
2283 if (clonedLayersMap.count(relativeOf) > 0) {
2284 const sp<Layer>& clonedRelativeOf = clonedLayersMap.at(relativeOf);
chaviw74b03172019-08-19 11:09:03 -07002285 mDrawingState.zOrderRelativeOf = clonedRelativeOf;
2286 }
2287
chaviwaf87b3e2019-10-01 16:59:28 -07002288 updateClonedInputInfo(clonedLayersMap);
2289
chaviw74b03172019-08-19 11:09:03 -07002290 for (sp<Layer>& child : mDrawingChildren) {
2291 child->updateClonedRelatives(clonedLayersMap);
2292 }
2293}
2294
2295void Layer::addChildToDrawing(const sp<Layer>& layer) {
2296 mDrawingChildren.add(layer);
2297 layer->mDrawingParent = this;
2298}
2299
Mathias Agopian13127d82013-03-05 17:47:11 -08002300// ---------------------------------------------------------------------------
2301
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002302}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002303
2304#if defined(__gl_h_)
2305#error "don't include gl/gl.h in this file"
2306#endif
2307
2308#if defined(__gl2_h_)
2309#error "don't include gl2/gl2.h in this file"
2310#endif
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -08002311
2312// TODO(b/129481165): remove the #pragma below and fix conversion issues
2313#pragma clang diagnostic pop // ignored "-Wconversion"