blob: 2e2d7450b48afd4b947593a16e99ee5d249ee98f [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dan Stoza9e56aa02015-11-02 13:00:03 -080017//#define LOG_NDEBUG 0
18#undef LOG_TAG
19#define LOG_TAG "Layer"
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080020#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21
Alec Mourie60041e2019-06-14 18:59:51 -070022#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023
Yiwei Zhang5434a782018-12-05 18:06:32 -080024#include <android-base/stringprintf.h>
Vishnu Nair0f085c62019-08-30 08:49:12 -070025#include <binder/IPCThreadState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080026#include <compositionengine/Display.h>
Lloyd Pique0b785d82018-12-04 17:25:27 -080027#include <compositionengine/Layer.h>
Lloyd Piquea83776c2019-01-29 18:42:32 -080028#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080029#include <compositionengine/OutputLayer.h>
30#include <compositionengine/impl/OutputLayerCompositionState.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070031#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070032#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070033#include <cutils/properties.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080034#include <gui/BufferItem.h>
35#include <gui/LayerDebugInfo.h>
36#include <gui/Surface.h>
Alec Mourie60041e2019-06-14 18:59:51 -070037#include <math.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080038#include <renderengine/RenderEngine.h>
Alec Mourie60041e2019-06-14 18:59:51 -070039#include <stdint.h>
40#include <stdlib.h>
41#include <sys/types.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080042#include <ui/DebugUtils.h>
43#include <ui/GraphicBuffer.h>
44#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045#include <utils/Errors.h>
46#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080047#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080049#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050
Alec Mourie60041e2019-06-14 18:59:51 -070051#include <algorithm>
52#include <mutex>
53#include <sstream>
54
Yiwei Zhang60d1a192018-03-07 14:52:28 -080055#include "BufferLayer.h"
Valerie Haudd0b7572019-01-29 14:59:27 -080056#include "ColorLayer.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020057#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070058#include "DisplayDevice.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080059#include "DisplayHardware/HWComposer.h"
Mikael Pessa90092f42019-08-26 17:22:04 -070060#include "FrameTracer/FrameTracer.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080061#include "LayerProtoHelper.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070062#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070063#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064#include "SurfaceFlinger.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080065#include "TimeStats/TimeStats.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070066
David Sodman41fdfc92017-11-06 16:09:56 -080067#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069namespace android {
70
Yiwei Zhang5434a782018-12-05 18:06:32 -080071using base::StringAppendF;
72
Lloyd Piquef1c675b2018-09-12 20:45:39 -070073std::atomic<int32_t> Layer::sSequence{1};
Mathias Agopian13127d82013-03-05 17:47:11 -080074
Lloyd Pique42ab75e2018-09-12 20:46:03 -070075Layer::Layer(const LayerCreationArgs& args)
Ady Abraham8f1ee7f2019-04-05 10:32:50 -070076 : mFlinger(args.flinger),
77 mName(args.name),
78 mClientRef(args.client),
79 mWindowType(args.metadata.getInt32(METADATA_WINDOW_TYPE, 0)) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -070080
81 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -070082 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
83 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
84 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070085
Lloyd Pique0449b0f2018-12-20 16:23:45 -080086 mCurrentState.active_legacy.w = args.w;
87 mCurrentState.active_legacy.h = args.h;
88 mCurrentState.flags = layerFlags;
89 mCurrentState.active_legacy.transform.set(0, 0);
90 mCurrentState.crop_legacy.makeInvalid();
91 mCurrentState.requestedCrop_legacy = mCurrentState.crop_legacy;
92 mCurrentState.z = 0;
93 mCurrentState.color.a = 1.0f;
94 mCurrentState.layerStack = 0;
95 mCurrentState.sequence = 0;
96 mCurrentState.requested_legacy = mCurrentState.active_legacy;
Lloyd Pique0449b0f2018-12-20 16:23:45 -080097 mCurrentState.active.w = UINT32_MAX;
98 mCurrentState.active.h = UINT32_MAX;
99 mCurrentState.active.transform.set(0, 0);
100 mCurrentState.transform = 0;
101 mCurrentState.transformToDisplayInverse = false;
102 mCurrentState.crop.makeInvalid();
103 mCurrentState.acquireFence = new Fence(-1);
104 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
105 mCurrentState.hdrMetadata.validTypes = 0;
Marissa Wall7e0a01f2019-08-14 14:29:25 -0700106 mCurrentState.surfaceDamageRegion = Region::INVALID_REGION;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800107 mCurrentState.cornerRadius = 0.0f;
108 mCurrentState.api = -1;
109 mCurrentState.hasColorTransform = false;
Peiyong Linc502cb72019-03-01 15:00:23 -0800110 mCurrentState.colorSpaceAgnostic = false;
Ana Krulecc84d09b2019-11-02 23:10:29 +0100111 mCurrentState.frameRateSelectionPriority = PRIORITY_UNSET;
Evan Roskya1f1e152019-01-24 16:17:46 -0800112 mCurrentState.metadata = args.metadata;
Vishnu Nairf37dcfb2019-11-16 07:37:09 -0800113 mCurrentState.shadowRadius = 0.f;
Steven Thomas3172e202020-01-06 19:25:30 -0800114 mCurrentState.frameRate = 0.f;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700115
116 // drawing state & current state are identical
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800117 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700118
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800119 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700120 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800121 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200122 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700123
Vishnu Nair0f085c62019-08-30 08:49:12 -0700124 mCallingPid = args.callingPid;
125 mCallingUid = args.callingUid;
Dominik Laskowski75848362019-11-11 17:57:20 -0800126}
127
128void Layer::onFirstRef() {
129 mFlinger->onLayerFirstRef(this);
Dan Stoza436ccf32018-06-21 12:10:12 -0700130}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700131
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700132Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800133 sp<Client> c(mClientRef.promote());
134 if (c != 0) {
135 c->detachLayer(this);
136 }
137
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000138 mFrameTracker.logAndResetStats(mName);
chaviw74d90ad2019-04-26 14:45:26 -0700139 mFlinger->onLayerDestroyed(this);
Mathias Agopian96f08192010-06-02 23:28:45 -0700140}
141
Vishnu Nair0f085c62019-08-30 08:49:12 -0700142LayerCreationArgs::LayerCreationArgs(SurfaceFlinger* flinger, const sp<Client>& client,
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700143 std::string name, uint32_t w, uint32_t h, uint32_t flags,
Vishnu Nair0f085c62019-08-30 08:49:12 -0700144 LayerMetadata metadata)
145 : flinger(flinger),
146 client(client),
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700147 name(std::move(name)),
Vishnu Nair0f085c62019-08-30 08:49:12 -0700148 w(w),
149 h(h),
150 flags(flags),
151 metadata(std::move(metadata)) {
152 IPCThreadState* ipc = IPCThreadState::self();
153 callingPid = ipc->getCallingPid();
154 callingUid = ipc->getCallingUid();
155}
156
Mathias Agopian13127d82013-03-05 17:47:11 -0800157// ---------------------------------------------------------------------------
158// callbacks
159// ---------------------------------------------------------------------------
160
David Sodmaneb085e02017-10-05 18:49:04 -0700161/*
162 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
163 * Layer. So, the implementation is done in BufferLayer. When called on a
164 * ColorLayer object, it's essentially a NOP.
165 */
David Sodmaneb085e02017-10-05 18:49:04 -0700166void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800167
chaviw43cb3cb2019-05-31 15:23:41 -0700168void Layer::removeRemoteSyncPoints() {
169 for (auto& point : mRemoteSyncPoints) {
170 point->setTransactionApplied();
171 }
172 mRemoteSyncPoints.clear();
173
174 {
chaviw43cb3cb2019-05-31 15:23:41 -0700175 for (State pendingState : mPendingStates) {
176 pendingState.barrierLayer_legacy = nullptr;
177 }
178 }
179}
180
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700181void Layer::removeRelativeZ(const std::vector<Layer*>& layersInTree) {
182 if (mCurrentState.zOrderRelativeOf == nullptr) {
183 return;
184 }
Robert Carr2e102c92018-10-23 12:11:15 -0700185
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700186 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
187 if (strongRelative == nullptr) {
188 setZOrderRelativeOf(nullptr);
189 return;
190 }
191
192 if (!std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
193 strongRelative->removeZOrderRelative(this);
194 mFlinger->setTransactionFlags(eTraversalNeeded);
chaviw606e5cf2019-03-01 10:12:10 -0800195 setZOrderRelativeOf(nullptr);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700196 }
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700197}
198
199void Layer::removeFromCurrentState() {
200 mRemovedFromCurrentState = true;
Rob Carr4bba3702018-10-08 21:53:30 +0000201
Robert Carr2e102c92018-10-23 12:11:15 -0700202 // Since we are no longer reachable from CurrentState SurfaceFlinger
203 // will no longer invoke doTransaction for us, and so we will
204 // never finish applying transactions. We signal the sync point
205 // now so that another layer will not become indefinitely
206 // blocked.
chaviw43cb3cb2019-05-31 15:23:41 -0700207 removeRemoteSyncPoints();
Robert Carr2e102c92018-10-23 12:11:15 -0700208
209 {
210 Mutex::Autolock syncLock(mLocalSyncPointMutex);
211 for (auto& point : mLocalSyncPoints) {
212 point->setFrameAvailable();
213 }
214 mLocalSyncPoints.clear();
215 }
216
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800217 mFlinger->markLayerPendingRemovalLocked(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700218}
Chia-I Wu38512252017-05-17 14:36:16 -0700219
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700220void Layer::onRemovedFromCurrentState() {
221 auto layersInTree = getLayersInTree(LayerVector::StateSet::Current);
222 std::sort(layersInTree.begin(), layersInTree.end());
223 for (const auto& layer : layersInTree) {
224 layer->removeFromCurrentState();
225 layer->removeRelativeZ(layersInTree);
226 }
227}
228
chaviw61626f22018-11-15 16:26:27 -0800229void Layer::addToCurrentState() {
230 mRemovedFromCurrentState = false;
231
232 for (const auto& child : mCurrentChildren) {
233 child->addToCurrentState();
234 }
235}
236
Mathias Agopian13127d82013-03-05 17:47:11 -0800237// ---------------------------------------------------------------------------
238// set-up
239// ---------------------------------------------------------------------------
240
chaviw13fdc492017-06-27 12:40:18 -0700241bool Layer::getPremultipledAlpha() const {
242 return mPremultipliedAlpha;
243}
244
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700245sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800246 Mutex::Autolock _l(mLock);
Robert Carrc0df3122019-04-11 13:18:21 -0700247 if (mGetHandleCalled) {
248 ALOGE("Get handle called twice" );
249 return nullptr;
250 }
251 mGetHandleCalled = true;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700252 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800253}
254
255// ---------------------------------------------------------------------------
256// h/w composer set-up
257// ---------------------------------------------------------------------------
258
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700259static Rect reduce(const Rect& win, const Region& exclude) {
260 if (CC_LIKELY(exclude.isEmpty())) {
261 return win;
262 }
263 if (exclude.isRect()) {
264 return win.reduce(exclude.getBounds());
265 }
266 return Region(win).subtract(exclude).getBounds();
267}
268
Dan Stoza80d61162017-12-20 15:57:52 -0800269static FloatRect reduce(const FloatRect& win, const Region& exclude) {
270 if (CC_LIKELY(exclude.isEmpty())) {
271 return win;
272 }
273 // Convert through Rect (by rounding) for lack of FloatRegion
274 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
275}
276
Vishnu Nair4351ad52019-02-11 14:13:02 -0800277Rect Layer::getScreenBounds(bool reduceTransparentRegion) const {
Vishnu Nairf0c28512019-02-08 12:40:28 -0800278 if (!reduceTransparentRegion) {
279 return Rect{mScreenBounds};
280 }
281
282 FloatRect bounds = getBounds();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800283 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800284 // Transform to screen space.
285 bounds = t.transform(bounds);
286 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700287}
288
Vishnu Nair4351ad52019-02-11 14:13:02 -0800289FloatRect Layer::getBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000290 const State& s(getDrawingState());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800291 return getBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700292}
293
Vishnu Nairf0c28512019-02-08 12:40:28 -0800294FloatRect Layer::getBounds(const Region& activeTransparentRegion) const {
295 // Subtract the transparent region and snap to the bounds.
296 return reduce(mBounds, activeTransparentRegion);
297}
298
Vishnu Nairc652ff82019-03-15 12:48:54 -0700299ui::Transform Layer::getBufferScaleTransform() const {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800300 // If the layer is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
301 // it isFixedSize) then there may be additional scaling not accounted
Vishnu Nairc652ff82019-03-15 12:48:54 -0700302 // for in the layer transform.
chaviwd62d3062019-09-04 14:48:02 -0700303 if (!isFixedSize() || getBuffer() == nullptr) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700304 return {};
Vishnu Nair4351ad52019-02-11 14:13:02 -0800305 }
306
Marissa Wall290ad082019-03-06 13:23:47 -0800307 // If the layer is a buffer state layer, the active width and height
308 // could be infinite. In that case, return the effective transform.
309 const uint32_t activeWidth = getActiveWidth(getDrawingState());
310 const uint32_t activeHeight = getActiveHeight(getDrawingState());
311 if (activeWidth >= UINT32_MAX && activeHeight >= UINT32_MAX) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700312 return {};
Marissa Wall290ad082019-03-06 13:23:47 -0800313 }
314
chaviwd62d3062019-09-04 14:48:02 -0700315 int bufferWidth = getBuffer()->getWidth();
316 int bufferHeight = getBuffer()->getHeight();
Vishnu Nairc652ff82019-03-15 12:48:54 -0700317
chaviw4244e032019-09-04 11:27:49 -0700318 if (getBufferTransform() & NATIVE_WINDOW_TRANSFORM_ROT_90) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700319 std::swap(bufferWidth, bufferHeight);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800320 }
Vishnu Nairc652ff82019-03-15 12:48:54 -0700321
Marissa Wall290ad082019-03-06 13:23:47 -0800322 float sx = activeWidth / static_cast<float>(bufferWidth);
323 float sy = activeHeight / static_cast<float>(bufferHeight);
324
Vishnu Nair4351ad52019-02-11 14:13:02 -0800325 ui::Transform extraParentScaling;
326 extraParentScaling.set(sx, 0, 0, sy);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700327 return extraParentScaling;
328}
329
330ui::Transform Layer::getTransformWithScale(const ui::Transform& bufferScaleTransform) const {
331 // We need to mirror this scaling to child surfaces or we will break the contract where WM can
332 // treat child surfaces as pixels in the parent surface.
chaviwd62d3062019-09-04 14:48:02 -0700333 if (!isFixedSize() || getBuffer() == nullptr) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700334 return mEffectiveTransform;
335 }
336 return mEffectiveTransform * bufferScaleTransform;
337}
338
339FloatRect Layer::getBoundsPreScaling(const ui::Transform& bufferScaleTransform) const {
340 // We need the pre scaled layer bounds when computing child bounds to make sure the child is
341 // cropped to its parent layer after any buffer transform scaling is applied.
chaviwd62d3062019-09-04 14:48:02 -0700342 if (!isFixedSize() || getBuffer() == nullptr) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700343 return mBounds;
344 }
345 return bufferScaleTransform.inverse().transform(mBounds);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800346}
347
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700348void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform,
349 float parentShadowRadius) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800350 const State& s(getDrawingState());
351
352 // Calculate effective layer transform
353 mEffectiveTransform = parentTransform * getActiveTransform(s);
354
355 // Transform parent bounds to layer space
356 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
357
Vishnu Nairc652ff82019-03-15 12:48:54 -0700358 // Calculate source bounds
Vishnu Nair4351ad52019-02-11 14:13:02 -0800359 mSourceBounds = computeSourceBounds(parentBounds);
360
361 // Calculate bounds by croping diplay frame with layer crop and parent bounds
362 FloatRect bounds = mSourceBounds;
363 const Rect layerCrop = getCrop(s);
364 if (!layerCrop.isEmpty()) {
365 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
366 }
367 bounds = bounds.intersect(parentBounds);
368
369 mBounds = bounds;
370 mScreenBounds = mEffectiveTransform.transform(mBounds);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700371
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700372 // Use the layer's own shadow radius if set. Otherwise get the radius from
373 // parent.
374 if (s.shadowRadius > 0.f) {
375 mEffectiveShadowRadius = s.shadowRadius;
376 } else {
377 mEffectiveShadowRadius = parentShadowRadius;
378 }
379
380 // Shadow radius is passed down to only one layer so if the layer can draw shadows,
381 // don't pass it to its children.
382 const float childShadowRadius = canDrawShadows() ? 0.f : mEffectiveShadowRadius;
383
Vishnu Nairc652ff82019-03-15 12:48:54 -0700384 // Add any buffer scaling to the layer's children.
385 ui::Transform bufferScaleTransform = getBufferScaleTransform();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800386 for (const sp<Layer>& child : mDrawingChildren) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700387 child->computeBounds(getBoundsPreScaling(bufferScaleTransform),
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700388 getTransformWithScale(bufferScaleTransform), childShadowRadius);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800389 }
390}
391
Vishnu Nair60356342018-11-13 13:00:45 -0800392Rect Layer::getCroppedBufferSize(const State& s) const {
393 Rect size = getBufferSize(s);
394 Rect crop = getCrop(s);
395 if (!crop.isEmpty() && size.isValid()) {
396 size.intersect(crop, &size);
397 } else if (!crop.isEmpty()) {
398 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700399 }
Vishnu Nair60356342018-11-13 13:00:45 -0800400 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800401}
402
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700403void Layer::setupRoundedCornersCropCoordinates(Rect win,
404 const FloatRect& roundedCornersCrop) const {
405 // Translate win by the rounded corners rect coordinates, to have all values in
406 // layer coordinate space.
407 win.left -= roundedCornersCrop.left;
408 win.right -= roundedCornersCrop.left;
409 win.top -= roundedCornersCrop.top;
410 win.bottom -= roundedCornersCrop.top;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700411}
412
Lloyd Piquec6687342019-03-07 21:34:57 -0800413void Layer::latchBasicGeometry(compositionengine::LayerFECompositionState& compositionState) const {
414 const auto& drawingState{getDrawingState()};
415 const uint32_t layerStack = getLayerStack();
416 const auto alpha = static_cast<float>(getAlpha());
417 const bool opaque = isOpaque(drawingState);
418 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
419
420 auto blendMode = Hwc2::IComposerClient::BlendMode::NONE;
421 if (!opaque || alpha != 1.0f) {
422 blendMode = mPremultipliedAlpha ? Hwc2::IComposerClient::BlendMode::PREMULTIPLIED
423 : Hwc2::IComposerClient::BlendMode::COVERAGE;
424 }
425
426 // TODO(b/121291683): Instead of filling in a passed-in compositionState
427 // structure, switch to Layer owning the structure and have
428 // CompositionEngine be able to get a reference to it.
429
430 compositionState.layerStackId =
431 (layerStack != ~0u) ? std::make_optional(layerStack) : std::nullopt;
432 compositionState.internalOnly = getPrimaryDisplayOnly();
433 compositionState.isVisible = isVisible();
434 compositionState.isOpaque = opaque && !usesRoundedCorners && alpha == 1.f;
Vishnu Naira483b4a2019-12-12 15:07:52 -0800435 compositionState.shadowRadius = mEffectiveShadowRadius;
Lloyd Piquec6687342019-03-07 21:34:57 -0800436
437 compositionState.contentDirty = contentDirty;
438 contentDirty = false;
439
440 compositionState.geomLayerBounds = mBounds;
441 compositionState.geomLayerTransform = getTransform();
442 compositionState.geomInverseLayerTransform = compositionState.geomLayerTransform.inverse();
443 compositionState.transparentRegionHint = getActiveTransparentRegion(drawingState);
444
445 compositionState.blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
446 compositionState.alpha = alpha;
447}
448
Lloyd Piquea83776c2019-01-29 18:42:32 -0800449void Layer::latchGeometry(compositionengine::LayerFECompositionState& compositionState) const {
450 const auto& drawingState{getDrawingState()};
Mathias Agopian13127d82013-03-05 17:47:11 -0800451
Lloyd Piquea83776c2019-01-29 18:42:32 -0800452 int type = drawingState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
453 int appId = drawingState.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700454 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400455 if (parent.get()) {
456 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800457 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
458 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
Jiwen 'Steve' Cai736c74e2019-05-28 18:33:22 -0700459 if (parentType > 0 && parentAppId > 0) {
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800460 type = parentType;
461 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700462 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400463 }
464
Lloyd Piquea83776c2019-01-29 18:42:32 -0800465 compositionState.geomBufferSize = getBufferSize(drawingState);
chaviw4244e032019-09-04 11:27:49 -0700466 compositionState.geomContentCrop = getBufferCrop();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800467 compositionState.geomCrop = getCrop(drawingState);
chaviw4244e032019-09-04 11:27:49 -0700468 compositionState.geomBufferTransform = getBufferTransform();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800469 compositionState.geomBufferUsesDisplayInverseTransform = getTransformToDisplayInverse();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800470 compositionState.geomUsesSourceCrop = usesSourceCrop();
471 compositionState.isSecure = isSecure();
David Sodman15094112018-10-11 09:39:37 -0700472
Lloyd Piquea83776c2019-01-29 18:42:32 -0800473 compositionState.type = type;
474 compositionState.appId = appId;
475}
David Sodmanba340492018-08-05 21:51:33 -0700476
Lloyd Piquef5275482019-01-29 18:42:42 -0800477void Layer::latchPerFrameState(compositionengine::LayerFECompositionState& compositionState) const {
Lloyd Pique688abd42019-02-15 15:42:24 -0800478 const auto& drawingState{getDrawingState()};
Lloyd Piquef5275482019-01-29 18:42:42 -0800479 compositionState.forceClientComposition = false;
480
Lloyd Piquef5275482019-01-29 18:42:42 -0800481 compositionState.isColorspaceAgnostic = isColorSpaceAgnostic();
chaviw4244e032019-09-04 11:27:49 -0700482 compositionState.dataspace = getDataSpace();
Lloyd Piquef5275482019-01-29 18:42:42 -0800483 compositionState.colorTransform = getColorTransform();
484 compositionState.colorTransformIsIdentity = !hasColorTransform();
485 compositionState.surfaceDamage = surfaceDamageRegion;
Lloyd Pique688abd42019-02-15 15:42:24 -0800486 compositionState.hasProtectedContent = isProtected();
487
488 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700489 const bool drawsShadows = mEffectiveShadowRadius != 0.f;
490
Lloyd Pique688abd42019-02-15 15:42:24 -0800491 compositionState.isOpaque =
492 isOpaque(drawingState) && !usesRoundedCorners && getAlpha() == 1.0_hf;
Lloyd Piquef5275482019-01-29 18:42:42 -0800493
494 // Force client composition for special cases known only to the front-end.
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700495 if (isHdrY410() || usesRoundedCorners || drawsShadows) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800496 compositionState.forceClientComposition = true;
497 }
498}
499
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800500void Layer::latchCursorCompositionState(
501 compositionengine::LayerFECompositionState& compositionState) const {
502 // This gives us only the "orientation" component of the transform
503 const State& drawingState{getDrawingState()};
504
505 // Apply the layer's transform, followed by the display's global transform
506 // Here we're guaranteed that the layer's transform preserves rects
507 Rect win = getCroppedBufferSize(drawingState);
508 // Subtract the transparent region and snap to the bounds
509 Rect bounds = reduce(win, getActiveTransparentRegion(drawingState));
510 Rect frame(getTransform().transform(bounds));
511
512 compositionState.cursorFrame = frame;
513}
514
Lloyd Piquef16688f2019-02-19 17:47:57 -0800515bool Layer::onPreComposition(nsecs_t) {
516 return false;
517}
518
Lloyd Piquea83776c2019-01-29 18:42:32 -0800519void Layer::latchCompositionState(compositionengine::LayerFECompositionState& compositionState,
Lloyd Piquec6687342019-03-07 21:34:57 -0800520 compositionengine::LayerFE::StateSubset subset) const {
521 using StateSubset = compositionengine::LayerFE::StateSubset;
Lloyd Piquef5275482019-01-29 18:42:42 -0800522
Lloyd Piquec6687342019-03-07 21:34:57 -0800523 switch (subset) {
524 case StateSubset::BasicGeometry:
525 latchBasicGeometry(compositionState);
526 break;
527
528 case StateSubset::GeometryAndContent:
529 latchBasicGeometry(compositionState);
530 latchGeometry(compositionState);
531 latchPerFrameState(compositionState);
532 break;
533
534 case StateSubset::Content:
535 latchPerFrameState(compositionState);
536 break;
537 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800538}
Mathias Agopian29a367b2011-07-12 14:51:45 -0700539
Lloyd Piquea83776c2019-01-29 18:42:32 -0800540const char* Layer::getDebugName() const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700541 return mName.c_str();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800542}
543
Mathias Agopian13127d82013-03-05 17:47:11 -0800544// ---------------------------------------------------------------------------
545// drawing...
546// ---------------------------------------------------------------------------
547
Lloyd Piquef16688f2019-02-19 17:47:57 -0800548std::optional<renderengine::LayerSettings> Layer::prepareClientComposition(
549 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
550 if (!getCompositionLayer()) {
551 return {};
552 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800553
Vishnu Nair4351ad52019-02-11 14:13:02 -0800554 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000555 half alpha = getAlpha();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800556 renderengine::LayerSettings layerSettings;
557 layerSettings.geometry.boundaries = bounds;
558 if (targetSettings.useIdentityTransform) {
559 layerSettings.geometry.positionTransform = mat4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000560 } else {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800561 layerSettings.geometry.positionTransform = getTransform().asMatrix4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000562 }
563
564 if (hasColorTransform()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800565 layerSettings.colorTransform = getColorTransform();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000566 }
567
568 const auto roundedCornerState = getRoundedCornerState();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800569 layerSettings.geometry.roundedCornersRadius = roundedCornerState.radius;
570 layerSettings.geometry.roundedCornersCrop = roundedCornerState.cropRect;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000571
Lloyd Piquef16688f2019-02-19 17:47:57 -0800572 layerSettings.alpha = alpha;
chaviw4244e032019-09-04 11:27:49 -0700573 layerSettings.sourceDataspace = getDataSpace();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800574 return layerSettings;
Mathias Agopian13127d82013-03-05 17:47:11 -0800575}
576
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800577std::optional<renderengine::LayerSettings> Layer::prepareShadowClientComposition(
578 const renderengine::LayerSettings& casterLayerSettings, const Rect& displayViewport,
579 ui::Dataspace outputDataspace) {
580 renderengine::ShadowSettings shadow = getShadowSettings(displayViewport);
581 if (shadow.length <= 0.f) {
582 return {};
583 }
584
585 const float casterAlpha = casterLayerSettings.alpha;
586 const bool casterIsOpaque = ((casterLayerSettings.source.buffer.buffer != nullptr) &&
587 casterLayerSettings.source.buffer.isOpaque);
588
589 renderengine::LayerSettings shadowLayer = casterLayerSettings;
590 shadowLayer.shadow = shadow;
Vishnu Naira483b4a2019-12-12 15:07:52 -0800591 shadowLayer.geometry.boundaries = mBounds; // ignore transparent region
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800592
593 // If the casting layer is translucent, we need to fill in the shadow underneath the layer.
594 // Otherwise the generated shadow will only be shown around the casting layer.
595 shadowLayer.shadow.casterIsTranslucent = !casterIsOpaque || (casterAlpha < 1.0f);
596 shadowLayer.shadow.ambientColor *= casterAlpha;
597 shadowLayer.shadow.spotColor *= casterAlpha;
598 shadowLayer.sourceDataspace = outputDataspace;
599 shadowLayer.source.buffer.buffer = nullptr;
600
601 if (shadowLayer.shadow.ambientColor.a <= 0.f && shadowLayer.shadow.spotColor.a <= 0.f) {
602 return {};
603 }
604
605 float casterCornerRadius = shadowLayer.geometry.roundedCornersRadius;
606 const FloatRect& cornerRadiusCropRect = casterLayerSettings.geometry.roundedCornersCrop;
607 const FloatRect& casterRect = shadowLayer.geometry.boundaries;
608
609 // crop used to set the corner radius may be larger than the content rect. Adjust the corner
610 // radius accordingly.
611 if (casterCornerRadius > 0.f) {
612 float cropRectOffset = std::max(std::abs(cornerRadiusCropRect.top - casterRect.top),
613 std::abs(cornerRadiusCropRect.left - casterRect.left));
614 if (cropRectOffset > casterCornerRadius) {
615 casterCornerRadius = 0;
616 } else {
617 casterCornerRadius -= cropRectOffset;
618 }
619 shadowLayer.geometry.roundedCornersRadius = casterCornerRadius;
620 }
621
622 return shadowLayer;
623}
624
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800625Hwc2::IComposerClient::Composition Layer::getCompositionType(
626 const sp<const DisplayDevice>& display) const {
627 const auto outputLayer = findOutputLayerForDisplay(display);
628 LOG_FATAL_IF(!outputLayer);
629 return outputLayer->getState().hwc ? (*outputLayer->getState().hwc).hwcCompositionType
630 : Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800631}
632
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800633bool Layer::getClearClientTarget(const sp<const DisplayDevice>& display) const {
634 const auto outputLayer = findOutputLayerForDisplay(display);
635 LOG_FATAL_IF(!outputLayer);
636 return outputLayer->getState().clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800637}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800638
Dan Stozacac35382016-01-27 12:21:06 -0800639bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
640 if (point->getFrameNumber() <= mCurrentFrameNumber) {
641 // Don't bother with a SyncPoint, since we've already latched the
642 // relevant frame
643 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700644 }
Robert Carr2e102c92018-10-23 12:11:15 -0700645 if (isRemovedFromCurrentState()) {
646 return false;
647 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700648
Dan Stozacac35382016-01-27 12:21:06 -0800649 Mutex::Autolock lock(mLocalSyncPointMutex);
650 mLocalSyncPoints.push_back(point);
651 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700652}
653
Mathias Agopian13127d82013-03-05 17:47:11 -0800654// ----------------------------------------------------------------------------
655// local state
656// ----------------------------------------------------------------------------
657
David Sodman41fdfc92017-11-06 16:09:56 -0800658bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800659 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700660 return (s.flags & layer_state_t::eLayerSecure);
661}
662
Mathias Agopian13127d82013-03-05 17:47:11 -0800663// ----------------------------------------------------------------------------
664// transaction
665// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800666
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800667void Layer::pushPendingState() {
668 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700669 return;
670 }
Alec Mourie60041e2019-06-14 18:59:51 -0700671 ATRACE_CALL();
Dan Stoza7dde5992015-05-22 09:51:44 -0700672
Dan Stoza7dde5992015-05-22 09:51:44 -0700673 // If this transaction is waiting on the receipt of a frame, generate a sync
674 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700675 // We don't allow installing sync points after we are removed from the current state
676 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800677 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
678 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800679 if (barrierLayer == nullptr) {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700680 ALOGE("[%s] Unable to promote barrier Layer.", getDebugName());
Dan Stoza7dde5992015-05-22 09:51:44 -0700681 // If we can't promote the layer we are intended to wait on,
682 // then it is expired or otherwise invalid. Allow this transaction
683 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800684 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800685 } else {
chaviw43cb3cb2019-05-31 15:23:41 -0700686 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy, this);
Robert Carr0d480722017-01-10 16:42:54 -0800687 if (barrierLayer->addSyncPoint(syncPoint)) {
Alec Mourie60041e2019-06-14 18:59:51 -0700688 std::stringstream ss;
689 ss << "Adding sync point " << mCurrentState.frameNumber_legacy;
690 ATRACE_NAME(ss.str().c_str());
Dan Stozacac35382016-01-27 12:21:06 -0800691 mRemoteSyncPoints.push_back(std::move(syncPoint));
692 } else {
693 // We already missed the frame we're supposed to synchronize
694 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800695 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800696 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700697 }
698
Dan Stoza7dde5992015-05-22 09:51:44 -0700699 // Wake us up to check if the frame has been received
700 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700701 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700702 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800703 mPendingStates.push_back(mCurrentState);
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700704 ATRACE_INT(mTransactionName.c_str(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700705}
706
Pablo Ceballos05289c22016-04-14 15:49:55 -0700707void Layer::popPendingState(State* stateToCommit) {
Alec Mourie60041e2019-06-14 18:59:51 -0700708 ATRACE_CALL();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800709 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700710
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800711 mPendingStates.removeAt(0);
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700712 ATRACE_INT(mTransactionName.c_str(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700713}
714
Pablo Ceballos05289c22016-04-14 15:49:55 -0700715bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700716 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800717 while (!mPendingStates.empty()) {
718 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700719 if (mRemoteSyncPoints.empty()) {
720 // If we don't have a sync point for this, apply it anyway. It
721 // will be visually wrong, but it should keep us from getting
722 // into too much trouble.
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700723 ALOGE("[%s] No local sync point found", getDebugName());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700724 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700725 stateUpdateAvailable = true;
726 continue;
727 }
728
Marissa Wallf58c14b2018-07-24 10:50:43 -0700729 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800730 mPendingStates[0].frameNumber_legacy) {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700731 ALOGE("[%s] Unexpected sync point frame number found", getDebugName());
Dan Stozacac35382016-01-27 12:21:06 -0800732
733 // Signal our end of the sync point and then dispose of it
734 mRemoteSyncPoints.front()->setTransactionApplied();
735 mRemoteSyncPoints.pop_front();
736 continue;
737 }
738
Dan Stoza7dde5992015-05-22 09:51:44 -0700739 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
Alec Mourie60041e2019-06-14 18:59:51 -0700740 ATRACE_NAME("frameIsAvailable");
Dan Stoza7dde5992015-05-22 09:51:44 -0700741 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700742 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700743 stateUpdateAvailable = true;
744
745 // Signal our end of the sync point and then dispose of it
746 mRemoteSyncPoints.front()->setTransactionApplied();
747 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800748 } else {
Alec Mourie60041e2019-06-14 18:59:51 -0700749 ATRACE_NAME("!frameIsAvailable");
Dan Stoza792e5292016-02-11 11:43:58 -0800750 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700751 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700752 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700753 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700754 stateUpdateAvailable = true;
755 }
756 }
757
758 // If we still have pending updates, wake SurfaceFlinger back up and point
759 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800760 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700761 setTransactionFlags(eTransactionNeeded);
762 mFlinger->setTransactionFlags(eTraversalNeeded);
763 }
764
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800765 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700766 return stateUpdateAvailable;
767}
768
Marissa Wall61c58622018-07-18 10:12:20 -0700769uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000770 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800771
Marissa Wall61c58622018-07-18 10:12:20 -0700772 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
773 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700774
David Sodmaneb085e02017-10-05 18:49:04 -0700775 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700776 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000777 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800778 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
779 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
780 " requested={ wh={%4u,%4u} }}\n"
781 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
782 " requested={ wh={%4u,%4u} }}\n",
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700783 this, getName().c_str(), getBufferTransform(), getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700784 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
785 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
786 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
787 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
788 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700789 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
790 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
791 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800792 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700793
Robert Carre392b552017-09-19 12:16:05 -0700794 // Don't let Layer::doTransaction update the drawing state
795 // if we have a pending resize, unless we are in fixed-size mode.
796 // the drawing state will be updated only once we receive a buffer
797 // with the correct size.
798 //
799 // In particular, we want to make sure the clip (which is part
800 // of the geometry state) is latched together with the size but is
801 // latched immediately when no resizing is involved.
802 //
803 // If a sideband stream is attached, however, we want to skip this
804 // optimization so that transactions aren't missed when a buffer
805 // never arrives
806 //
807 // In the case that we don't have a buffer we ignore other factors
808 // and avoid entering the resizePending state. At a high level the
809 // resizePending state is to avoid applying the state of the new buffer
810 // to the old buffer. However in the state where we don't have an old buffer
811 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700812 const bool resizePending =
813 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
814 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
chaviwd62d3062019-09-04 14:48:02 -0700815 (getBuffer() != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700816 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800817 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700818 flags |= eDontUpdateGeometryState;
819 }
820 }
821
Robert Carr7bf247e2017-05-18 14:02:49 -0700822 // Here we apply various requested geometry states, depending on our
823 // latching configuration. See Layer.h for a detailed discussion of
824 // how geometry latching is controlled.
825 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000826 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700827
Robert Carr7bf247e2017-05-18 14:02:49 -0700828 // There is an awkward asymmetry in the handling of the crop states in the position
829 // states, as can be seen below. Largely this arises from position and transform
830 // being stored in the same data structure while having different latching rules.
831 // b/38182305
832 //
Marissa Wall61c58622018-07-18 10:12:20 -0700833 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700834 // applyPendingStates in the presence of deferred transactions.
chaviw214c89d2019-09-04 16:03:53 -0700835 editCurrentState.active_legacy = editCurrentState.requested_legacy;
836 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Mathias Agopian13127d82013-03-05 17:47:11 -0800837 }
838
Marissa Wall61c58622018-07-18 10:12:20 -0700839 return flags;
840}
841
842uint32_t Layer::doTransaction(uint32_t flags) {
843 ATRACE_CALL();
844
chaviw5aedec92018-10-22 10:40:38 -0700845 if (mLayerDetached) {
Robert Carr321e83c2019-08-19 15:49:30 -0700846 // Ensure BLAST buffer callbacks are processed.
847 // detachChildren and mLayerDetached were implemented to avoid geometry updates
848 // to layers in the cases of animation. For BufferQueue layers buffers are still
849 // consumed as normal. This is useful as otherwise the client could get hung
850 // inevitably waiting on a buffer to return. We recreate this semantic for BufferQueue
851 // even though it is a little consistent. detachChildren is shortly slated for removal
852 // by the hierarchy mirroring work so we don't need to worry about it too much.
Valerie Hau7618b232020-01-09 16:03:08 -0800853 forceSendCallbacks();
Robert Carr321e83c2019-08-19 15:49:30 -0700854 mCurrentState.callbackHandles = {};
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800855 return flags;
856 }
857
858 if (mChildrenChanged) {
859 flags |= eVisibleRegion;
860 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -0700861 }
862
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800863 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +0000864 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -0700865 if (!applyPendingStates(&c)) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800866 return flags;
Marissa Wall61c58622018-07-18 10:12:20 -0700867 }
868
869 flags = doTransactionResize(flags, &c);
870
Alec Mourib416efd2018-09-06 21:01:59 +0000871 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700872
873 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800874 // invalidate and recompute the visible regions if needed
875 flags |= Layer::eVisibleRegion;
876 }
877
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700878 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800879 // invalidate and recompute the visible regions if needed
880 flags |= eVisibleRegion;
881 this->contentDirty = true;
882
883 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -0700884 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -0700885 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -0800886 }
887
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800888 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -0700889 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800890 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -0700891 }
892
Mathias Agopian13127d82013-03-05 17:47:11 -0800893 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -0700894 commitTransaction(c);
Vishnu Nair8406fd72019-07-30 11:29:31 -0700895 mPendingStatesSnapshot = mPendingStates;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800896 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -0800897 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800898}
899
Pablo Ceballos05289c22016-04-14 15:49:55 -0700900void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800901 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700902}
903
Mathias Agopian13127d82013-03-05 17:47:11 -0800904uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800905 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800906}
907
908uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800909 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -0800910}
911
chaviw214c89d2019-09-04 16:03:53 -0700912bool Layer::setPosition(float x, float y) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800913 if (mCurrentState.requested_legacy.transform.tx() == x &&
914 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -0800915 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800916 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -0700917
918 // We update the requested and active position simultaneously because
919 // we want to apply the position portion of the transform matrix immediately,
920 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800921 mCurrentState.requested_legacy.transform.set(x, y);
chaviw214c89d2019-09-04 16:03:53 -0700922 // Here we directly update the active state
923 // unlike other setters, because we store it within
924 // the transform, but use different latching rules.
925 // b/38182305
926 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr69663fb2016-03-27 19:59:19 -0700927
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800928 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800929 setTransactionFlags(eTransactionNeeded);
930 return true;
931}
Robert Carr82364e32016-05-15 11:27:47 -0700932
Robert Carr1f0a16a2016-10-24 16:27:39 -0700933bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
934 ssize_t idx = mCurrentChildren.indexOf(childLayer);
935 if (idx < 0) {
936 return false;
937 }
938 if (childLayer->setLayer(z)) {
939 mCurrentChildren.removeAt(idx);
940 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800941 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700942 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800943 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700944}
945
Robert Carr503c7042017-09-27 15:06:08 -0700946bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
947 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
948 ssize_t idx = mCurrentChildren.indexOf(childLayer);
949 if (idx < 0) {
950 return false;
951 }
952 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
953 mCurrentChildren.removeAt(idx);
954 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800955 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700956 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800957 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700958}
959
Robert Carrae060832016-11-28 10:51:00 -0800960bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800961 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
962 mCurrentState.sequence++;
963 mCurrentState.z = z;
964 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700965
966 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800967 if (mCurrentState.zOrderRelativeOf != nullptr) {
968 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700969 if (strongRelative != nullptr) {
970 strongRelative->removeZOrderRelative(this);
971 }
chaviw606e5cf2019-03-01 10:12:10 -0800972 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -0700973 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800974 setTransactionFlags(eTransactionNeeded);
975 return true;
976}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700977
Robert Carrdb66e622017-04-10 16:55:57 -0700978void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800979 mCurrentState.zOrderRelatives.remove(relative);
980 mCurrentState.sequence++;
981 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700982 setTransactionFlags(eTransactionNeeded);
983}
984
985void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800986 mCurrentState.zOrderRelatives.add(relative);
987 mCurrentState.modified = true;
988 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -0700989 setTransactionFlags(eTransactionNeeded);
990}
991
chaviw606e5cf2019-03-01 10:12:10 -0800992void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
993 mCurrentState.zOrderRelativeOf = relativeOf;
994 mCurrentState.sequence++;
995 mCurrentState.modified = true;
chaviwbdb8b802019-10-14 09:17:12 -0700996 mCurrentState.isRelativeOf = relativeOf != nullptr;
997
chaviw606e5cf2019-03-01 10:12:10 -0800998 setTransactionFlags(eTransactionNeeded);
999}
1000
Robert Carr503d2bd2017-12-04 15:49:47 -08001001bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001002 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1003 if (handle == nullptr) {
1004 return false;
1005 }
1006 sp<Layer> relative = handle->owner.promote();
1007 if (relative == nullptr) {
1008 return false;
1009 }
1010
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001011 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1012 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001013 return false;
1014 }
1015
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001016 mCurrentState.sequence++;
1017 mCurrentState.modified = true;
1018 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001019
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001020 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -07001021 if (oldZOrderRelativeOf != nullptr) {
1022 oldZOrderRelativeOf->removeZOrderRelative(this);
1023 }
chaviw606e5cf2019-03-01 10:12:10 -08001024 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -07001025 relative->addZOrderRelative(this);
1026
1027 setTransactionFlags(eTransactionNeeded);
1028
1029 return true;
1030}
1031
Mathias Agopian13127d82013-03-05 17:47:11 -08001032bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001033 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001034 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001035 mCurrentState.requested_legacy.w = w;
1036 mCurrentState.requested_legacy.h = h;
1037 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001038 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001039
1040 // record the new size, from this point on, when the client request
1041 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001042 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001043 return true;
1044}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001045bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001046 if (mCurrentState.color.a == alpha) return false;
1047 mCurrentState.sequence++;
1048 mCurrentState.color.a = alpha;
1049 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001050 setTransactionFlags(eTransactionNeeded);
1051 return true;
1052}
chaviw13fdc492017-06-27 12:40:18 -07001053
Valerie Haudd0b7572019-01-29 14:59:27 -08001054bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1055 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001056 return false;
Valerie Hauaa194562019-02-05 16:21:38 -08001057 }
1058 mCurrentState.sequence++;
1059 mCurrentState.modified = true;
1060 setTransactionFlags(eTransactionNeeded);
1061
1062 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001063 // create background color layer if one does not yet exist
1064 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001065 std::string name = mName + "BackgroundColorLayer";
Lloyd Pique958be492019-03-28 15:34:59 -07001066 mCurrentState.bgColorLayer = mFlinger->getFactory().createColorLayer(
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001067 LayerCreationArgs(mFlinger.get(), nullptr, std::move(name), 0, 0, flags,
1068 LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -07001069
Valerie Haudd0b7572019-01-29 14:59:27 -08001070 // add to child list
1071 addChild(mCurrentState.bgColorLayer);
1072 mFlinger->mLayersAdded = true;
1073 // set up SF to handle added color layer
1074 if (isRemovedFromCurrentState()) {
1075 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1076 }
1077 mFlinger->setTransactionFlags(eTransactionNeeded);
1078 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1079 mCurrentState.bgColorLayer->reparent(nullptr);
1080 mCurrentState.bgColorLayer = nullptr;
1081 return true;
1082 }
1083
1084 mCurrentState.bgColorLayer->setColor(color);
1085 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1086 mCurrentState.bgColorLayer->setAlpha(alpha);
1087 mCurrentState.bgColorLayer->setDataspace(dataspace);
1088
chaviw13fdc492017-06-27 12:40:18 -07001089 return true;
1090}
1091
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001092bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001093 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001094
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001095 mCurrentState.sequence++;
1096 mCurrentState.cornerRadius = cornerRadius;
1097 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001098 setTransactionFlags(eTransactionNeeded);
1099 return true;
1100}
1101
Robert Carrd4ae7f32018-06-07 16:10:57 -07001102bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1103 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001104 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001105 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1106
1107 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1108 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1109 return false;
1110 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001111 mCurrentState.sequence++;
1112 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1113 matrix.dsdy);
1114 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001115 setTransactionFlags(eTransactionNeeded);
1116 return true;
1117}
Marissa Wall61c58622018-07-18 10:12:20 -07001118
Mathias Agopian13127d82013-03-05 17:47:11 -08001119bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001120 mCurrentState.requestedTransparentRegion_legacy = transparent;
1121 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001122 setTransactionFlags(eTransactionNeeded);
1123 return true;
1124}
Ana Krulecc84d09b2019-11-02 23:10:29 +01001125
Mathias Agopian13127d82013-03-05 17:47:11 -08001126bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001127 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1128 if (mCurrentState.flags == newFlags) return false;
1129 mCurrentState.sequence++;
1130 mCurrentState.flags = newFlags;
1131 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001132 setTransactionFlags(eTransactionNeeded);
1133 return true;
1134}
Robert Carr99e27f02016-06-16 15:18:02 -07001135
chaviw214c89d2019-09-04 16:03:53 -07001136bool Layer::setCrop_legacy(const Rect& crop) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001137 if (mCurrentState.requestedCrop_legacy == crop) return false;
1138 mCurrentState.sequence++;
1139 mCurrentState.requestedCrop_legacy = crop;
chaviw214c89d2019-09-04 16:03:53 -07001140 mCurrentState.crop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001141
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001142 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001143 setTransactionFlags(eTransactionNeeded);
1144 return true;
1145}
Robert Carr8d5227b2017-03-16 15:41:03 -07001146
Robert Carrc3574f72016-03-24 12:19:32 -07001147bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001148 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001149 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001150 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001151 return true;
1152}
1153
Evan Roskyef876c92019-01-25 17:46:06 -08001154bool Layer::setMetadata(const LayerMetadata& data) {
1155 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001156 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001157 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001158 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001159 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001160}
1161
Mathias Agopian13127d82013-03-05 17:47:11 -08001162bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001163 if (mCurrentState.layerStack == layerStack) return false;
1164 mCurrentState.sequence++;
1165 mCurrentState.layerStack = layerStack;
1166 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001167 setTransactionFlags(eTransactionNeeded);
1168 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001169}
1170
Peiyong Linc502cb72019-03-01 15:00:23 -08001171bool Layer::setColorSpaceAgnostic(const bool agnostic) {
1172 if (mCurrentState.colorSpaceAgnostic == agnostic) {
1173 return false;
1174 }
1175 mCurrentState.sequence++;
1176 mCurrentState.colorSpaceAgnostic = agnostic;
1177 mCurrentState.modified = true;
1178 setTransactionFlags(eTransactionNeeded);
1179 return true;
1180}
1181
Ana Krulecc84d09b2019-11-02 23:10:29 +01001182bool Layer::setFrameRateSelectionPriority(int32_t priority) {
1183 if (mCurrentState.frameRateSelectionPriority == priority) return false;
1184 mCurrentState.frameRateSelectionPriority = priority;
1185 mCurrentState.sequence++;
1186 mCurrentState.modified = true;
1187 setTransactionFlags(eTransactionNeeded);
1188 return true;
1189}
1190
1191int32_t Layer::getFrameRateSelectionPriority() const {
1192 // Check if layer has priority set.
1193 if (mDrawingState.frameRateSelectionPriority != PRIORITY_UNSET) {
1194 return mDrawingState.frameRateSelectionPriority;
1195 }
1196 // If not, search whether its parents have it set.
1197 sp<Layer> parent = getParent();
1198 if (parent != nullptr) {
1199 return parent->getFrameRateSelectionPriority();
1200 }
1201
1202 return Layer::PRIORITY_UNSET;
1203}
1204
Robert Carr1f0a16a2016-10-24 16:27:39 -07001205uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001206 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001207 if (p == nullptr) {
1208 return getDrawingState().layerStack;
1209 }
1210 return p->getLayerStack();
1211}
1212
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001213bool Layer::setShadowRadius(float shadowRadius) {
1214 if (mCurrentState.shadowRadius == shadowRadius) {
1215 return false;
1216 }
1217
1218 mCurrentState.sequence++;
1219 mCurrentState.shadowRadius = shadowRadius;
1220 mCurrentState.modified = true;
1221 setTransactionFlags(eTransactionNeeded);
1222 return true;
1223}
1224
Steven Thomas3172e202020-01-06 19:25:30 -08001225bool Layer::setFrameRate(float frameRate) {
1226 if (mCurrentState.frameRate == frameRate) {
1227 return false;
1228 }
1229
1230 mCurrentState.sequence++;
1231 mCurrentState.frameRate = frameRate;
1232 mCurrentState.modified = true;
1233 setTransactionFlags(eTransactionNeeded);
1234 return true;
1235}
1236
1237float Layer::getFrameRate() const {
1238 return getDrawingState().frameRate;
1239}
1240
Marissa Wallf58c14b2018-07-24 10:50:43 -07001241void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Alec Mourie60041e2019-06-14 18:59:51 -07001242 ATRACE_CALL();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001243 mCurrentState.barrierLayer_legacy = barrierLayer;
1244 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001245 // We don't set eTransactionNeeded, because just receiving a deferral
1246 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001247 mCurrentState.modified = true;
1248 pushPendingState();
1249 mCurrentState.barrierLayer_legacy = nullptr;
1250 mCurrentState.frameNumber_legacy = 0;
1251 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001252}
1253
Marissa Wallf58c14b2018-07-24 10:50:43 -07001254void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001255 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001256 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001257}
1258
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001259// ----------------------------------------------------------------------------
1260// pageflip handling...
1261// ----------------------------------------------------------------------------
1262
Robert Carr1f0a16a2016-10-24 16:27:39 -07001263bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001264 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001265 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001266 if (parent != nullptr && parent->isHiddenByPolicy()) {
1267 return true;
1268 }
Robert Carr1c5481e2019-07-01 14:42:27 -07001269 if (usingRelativeZ(LayerVector::StateSet::Drawing)) {
1270 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
1271 if (zOrderRelativeOf != nullptr) {
1272 if (zOrderRelativeOf->isHiddenByPolicy()) {
1273 return true;
1274 }
1275 }
1276 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001277 return s.flags & layer_state_t::eLayerHidden;
1278}
1279
David Sodman41fdfc92017-11-06 16:09:56 -08001280uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001281 // TODO: should we do something special if mSecure is set?
1282 if (mProtectedByApp) {
1283 // need a hardware-protected path to external video sink
1284 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001285 }
Riley Andrews03414a12014-07-01 14:22:59 -07001286 if (mPotentialCursor) {
1287 usage |= GraphicBuffer::USAGE_CURSOR;
1288 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001289 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001290 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001291}
1292
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001293void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001294 uint32_t orientation = 0;
Vishnu Nair5eb3f062019-04-08 08:21:03 -07001295 // Disable setting transform hint if the debug flag is set.
1296 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001297 // The transform hint is used to improve performance, but we can
1298 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001299 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001300 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001301 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001302 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001303 orientation = 0;
1304 }
1305 }
David Sodmaneb085e02017-10-05 18:49:04 -07001306 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001307}
1308
Mathias Agopian13127d82013-03-05 17:47:11 -08001309// ----------------------------------------------------------------------------
1310// debugging
1311// ----------------------------------------------------------------------------
1312
Marissa Wall61c58622018-07-18 10:12:20 -07001313// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001314LayerDebugInfo Layer::getLayerDebugInfo() const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001315 using namespace std::string_literals;
1316
Kalle Raitaa099a242017-01-11 11:17:29 -08001317 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001318 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001319 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001320 sp<Layer> parent = getParent();
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001321 info.mParentName = parent ? parent->getName() : "none"s;
chaviw8a01fa42019-08-19 12:39:31 -07001322 info.mType = getType();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001323 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Lloyd Piquea2468662019-03-07 21:31:06 -08001324
1325 info.mVisibleRegion = debugGetVisibleRegionOnDefaultDisplay();
Kalle Raitaa099a242017-01-11 11:17:29 -08001326 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001327 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001328 info.mX = ds.active_legacy.transform.tx();
1329 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001330 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001331 info.mWidth = ds.active_legacy.w;
1332 info.mHeight = ds.active_legacy.h;
1333 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001334 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001335 info.mFlags = ds.flags;
1336 info.mPixelFormat = getPixelFormat();
chaviw4244e032019-09-04 11:27:49 -07001337 info.mDataSpace = static_cast<android_dataspace>(getDataSpace());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001338 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1339 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1340 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1341 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001342 {
chaviwd62d3062019-09-04 14:48:02 -07001343 sp<const GraphicBuffer> buffer = getBuffer();
David Sodman5b4cffc2017-11-23 13:20:29 -08001344 if (buffer != 0) {
1345 info.mActiveBufferWidth = buffer->getWidth();
1346 info.mActiveBufferHeight = buffer->getHeight();
1347 info.mActiveBufferStride = buffer->getStride();
1348 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001349 } else {
1350 info.mActiveBufferWidth = 0;
1351 info.mActiveBufferHeight = 0;
1352 info.mActiveBufferStride = 0;
1353 info.mActiveBufferFormat = 0;
1354 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001355 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001356 info.mNumQueuedFrames = getQueuedFrameCount();
1357 info.mRefreshPending = isBufferLatched();
1358 info.mIsOpaque = isOpaque(ds);
1359 info.mContentDirty = contentDirty;
1360 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001361}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001362
Yiwei Zhang5434a782018-12-05 18:06:32 -08001363void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001364 result.append("-------------------------------");
1365 result.append("-------------------------------");
1366 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001367 result.append(" Layer name\n");
1368 result.append(" Z | ");
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001369 result.append(" Window Type | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001370 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001371 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001372 result.append(" Disp Frame (LTRB) | ");
1373 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001374 result.append("-------------------------------");
1375 result.append("-------------------------------");
1376 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001377}
1378
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001379void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1380 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1381 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001382 return;
1383 }
1384
Yiwei Zhang5434a782018-12-05 18:06:32 -08001385 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001386 if (mName.length() > 77) {
1387 std::string shortened;
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001388 shortened.append(mName, 0, 36);
Dan Stozae22aec72016-08-01 13:20:59 -07001389 shortened.append("[...]");
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001390 shortened.append(mName, mName.length() - 36);
1391 name = std::move(shortened);
Dan Stozae22aec72016-08-01 13:20:59 -07001392 } else {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001393 name = mName;
Dan Stozae22aec72016-08-01 13:20:59 -07001394 }
1395
Yiwei Zhang5434a782018-12-05 18:06:32 -08001396 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001397
Alec Mourib416efd2018-09-06 21:01:59 +00001398 const State& layerState(getDrawingState());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001399 const auto& compositionState = outputLayer->getState();
1400
Chia-I Wu1e043612018-03-01 09:45:09 -08001401 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001402 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001403 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001404 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001405 }
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001406 StringAppendF(&result, " %10d | ", mWindowType);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001407 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
1408 StringAppendF(&result, "%10s | ",
1409 toString(getCompositionLayer() ? compositionState.bufferTransform
1410 : static_cast<Hwc2::Transform>(0))
1411 .c_str());
1412 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001413 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001414 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001415 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1416 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001417
Yichi Chen6ca35192018-05-29 12:20:43 +08001418 result.append("- - - - - - - - - - - - - - - -");
1419 result.append("- - - - - - - - - - - - - - - -");
1420 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001421}
Dan Stozae22aec72016-08-01 13:20:59 -07001422
Yiwei Zhang5434a782018-12-05 18:06:32 -08001423void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001424 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001425}
1426
Svetoslavd85084b2014-03-20 10:28:31 -07001427void Layer::clearFrameStats() {
1428 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001429}
1430
Jamie Gennis6547ff42013-07-16 20:12:42 -07001431void Layer::logFrameStats() {
1432 mFrameTracker.logAndResetStats(mName);
1433}
1434
Svetoslavd85084b2014-03-20 10:28:31 -07001435void Layer::getFrameStats(FrameStats* outStats) const {
1436 mFrameTracker.getStats(outStats);
1437}
1438
Yiwei Zhang5434a782018-12-05 18:06:32 -08001439void Layer::dumpFrameEvents(std::string& result) {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001440 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().c_str(), getType(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001441 Mutex::Autolock lock(mFrameEventHistoryMutex);
1442 mFrameEventHistory.checkFencesForCompletion();
1443 mFrameEventHistory.dump(result);
1444}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001445
Vishnu Nair0f085c62019-08-30 08:49:12 -07001446void Layer::dumpCallingUidPid(std::string& result) const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001447 StringAppendF(&result, "Layer %s (%s) pid:%d uid:%d\n", getName().c_str(), getType(),
Vishnu Nair0f085c62019-08-30 08:49:12 -07001448 mCallingPid, mCallingUid);
1449}
1450
Brian Anderson5ea5e592016-12-01 16:54:33 -08001451void Layer::onDisconnect() {
1452 Mutex::Autolock lock(mFrameEventHistoryMutex);
1453 mFrameEventHistory.onDisconnect();
Yiwei Zhang1a88c402019-11-18 10:43:58 -08001454 const int32_t layerId = getSequence();
1455 mFlinger->mTimeStats->onDestroy(layerId);
1456 mFlinger->mFrameTracer->onDestroy(layerId);
Brian Anderson5ea5e592016-12-01 16:54:33 -08001457}
1458
Brian Anderson3890c392016-07-25 12:48:08 -07001459void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001460 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001461 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001462 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1463 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhang487340f2019-11-18 17:42:12 -08001464 mFlinger->mTimeStats->setAcquireFence(getSequence(), newTimestamps->frameNumber,
1465 newTimestamps->acquireFence);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001466 }
1467
Brian Andersond6927fb2016-07-23 23:37:30 -07001468 Mutex::Autolock lock(mFrameEventHistoryMutex);
1469 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001470 // If there are any unsignaled fences in the aquire timeline at this
1471 // point, the previously queued frame hasn't been latched yet. Go ahead
1472 // and try to get the signal time here so the syscall is taken out of
1473 // the main thread's critical path.
1474 mAcquireTimeline.updateSignalTimes();
1475 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001476 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001477 mFrameEventHistory.addQueue(*newTimestamps);
1478 }
1479
Brian Anderson3890c392016-07-25 12:48:08 -07001480 if (outDelta) {
1481 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001482 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001483}
Dan Stozae77c7662016-05-13 11:37:28 -07001484
Chia-I Wu98f1c102017-05-30 14:54:08 -07001485size_t Layer::getChildrenCount() const {
1486 size_t count = 0;
1487 for (const sp<Layer>& child : mCurrentChildren) {
1488 count += 1 + child->getChildrenCount();
1489 }
1490 return count;
1491}
1492
Robert Carr1f0a16a2016-10-24 16:27:39 -07001493void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001494 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001495 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001496
Robert Carr1f0a16a2016-10-24 16:27:39 -07001497 mCurrentChildren.add(layer);
1498 layer->setParent(this);
1499}
1500
1501ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001502 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001503 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001504
Robert Carr1323c952019-01-28 18:13:27 -08001505 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001506 return mCurrentChildren.remove(layer);
1507}
1508
Robert Carr1db73f62016-12-21 12:58:51 -08001509bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1510 sp<Handle> handle = nullptr;
1511 sp<Layer> newParent = nullptr;
1512 if (newParentHandle == nullptr) {
1513 return false;
1514 }
1515 handle = static_cast<Handle*>(newParentHandle.get());
1516 newParent = handle->owner.promote();
1517 if (newParent == nullptr) {
1518 ALOGE("Unable to promote Layer handle");
1519 return false;
1520 }
1521
chaviw5aedec92018-10-22 10:40:38 -07001522 if (attachChildren()) {
1523 setTransactionFlags(eTransactionNeeded);
1524 }
Robert Carr1db73f62016-12-21 12:58:51 -08001525 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001526 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001527 }
1528 mCurrentChildren.clear();
1529
1530 return true;
1531}
1532
Robert Carr15eae092018-03-23 13:43:53 -07001533void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001534 for (const sp<Layer>& child : mDrawingChildren) {
1535 child->mDrawingParent = newParent;
Vishnu Nairc652ff82019-03-15 12:48:54 -07001536 child->computeBounds(newParent->mBounds,
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001537 newParent->getTransformWithScale(newParent->getBufferScaleTransform()),
1538 newParent->mEffectiveShadowRadius);
Robert Carr578038f2018-03-09 12:25:24 -08001539 }
1540}
1541
chaviwf1961f72017-09-18 16:41:07 -07001542bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001543 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001544
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001545 // While layers are detached, we allow most operations
1546 // and simply halt performing the actual transaction. However
1547 // for reparent != null we would enter the mRemovedFromCurrentState
1548 // state, regardless of whether doTransaction was called, and
1549 // so we need to prevent the update here.
1550 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001551 return false;
1552 }
1553
Robert Carr54cf5b12019-01-25 14:02:28 -08001554 sp<Layer> newParent;
1555 if (newParentHandle != nullptr) {
1556 auto handle = static_cast<Handle*>(newParentHandle.get());
1557 newParent = handle->owner.promote();
1558 if (newParent == nullptr) {
1559 ALOGE("Unable to promote Layer handle");
1560 return false;
1561 }
1562 if (newParent == this) {
1563 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1564 return false;
1565 }
1566 }
1567
chaviwf1961f72017-09-18 16:41:07 -07001568 sp<Layer> parent = getParent();
1569 if (parent != nullptr) {
1570 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001571 }
1572
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001573 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001574 newParent->addChild(this);
1575 if (!newParent->isRemovedFromCurrentState()) {
1576 addToCurrentState();
1577 } else {
1578 onRemovedFromCurrentState();
1579 }
1580
1581 if (mLayerDetached) {
1582 mLayerDetached = false;
1583 callSetTransactionFlags = true;
1584 }
1585 } else {
1586 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001587 }
1588
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001589 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001590 setTransactionFlags(eTransactionNeeded);
1591 }
chaviw06178942017-07-27 10:25:59 -07001592 return true;
1593}
1594
Robert Carr9524cb32017-02-13 11:32:32 -08001595bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001596 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001597 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001598 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001599 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001600 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001601 child->detachChildren();
chaviw43cb3cb2019-05-31 15:23:41 -07001602 child->removeRemoteSyncPoints();
Robert Carr9524cb32017-02-13 11:32:32 -08001603 }
Robert Carr7f619b22017-11-06 12:56:35 -08001604 }
Robert Carr9524cb32017-02-13 11:32:32 -08001605
1606 return true;
1607}
1608
chaviw5aedec92018-10-22 10:40:38 -07001609bool Layer::attachChildren() {
1610 bool changed = false;
1611 for (const sp<Layer>& child : mCurrentChildren) {
1612 sp<Client> parentClient = mClientRef.promote();
1613 sp<Client> client(child->mClientRef.promote());
1614 if (client != nullptr && parentClient != client) {
1615 if (child->mLayerDetached) {
1616 child->mLayerDetached = false;
1617 changed = true;
1618 }
1619 changed |= child->attachChildren();
1620 }
1621 }
1622
1623 return changed;
1624}
1625
Peiyong Lind3788632018-09-18 16:01:31 -07001626bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001627 static const mat4 identityMatrix = mat4();
1628
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001629 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001630 return false;
1631 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001632 ++mCurrentState.sequence;
1633 mCurrentState.colorTransform = matrix;
1634 mCurrentState.hasColorTransform = matrix != identityMatrix;
1635 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001636 setTransactionFlags(eTransactionNeeded);
1637 return true;
1638}
1639
chaviwf66724d2018-11-28 16:35:21 -08001640mat4 Layer::getColorTransform() const {
1641 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1642 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1643 colorTransform = parent->getColorTransform() * colorTransform;
1644 }
1645 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001646}
1647
1648bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001649 bool hasColorTransform = getDrawingState().hasColorTransform;
1650 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1651 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1652 }
1653 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001654}
1655
Chia-I Wu11481472018-05-04 10:43:19 -07001656bool Layer::isLegacyDataSpace() const {
1657 // return true when no higher bits are set
chaviw4244e032019-09-04 11:27:49 -07001658 return !(getDataSpace() &
1659 (ui::Dataspace::STANDARD_MASK | ui::Dataspace::TRANSFER_MASK |
1660 ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001661}
1662
Robert Carr1f0a16a2016-10-24 16:27:39 -07001663void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001664 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001665}
1666
chaviw301b1d82019-11-06 13:15:09 -08001667int32_t Layer::getZ(LayerVector::StateSet stateSet) const {
1668 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1669 const State& state = useDrawing ? mDrawingState : mCurrentState;
1670 return state.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001671}
1672
Robert Carr1c5481e2019-07-01 14:42:27 -07001673bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) const {
Robert Carr29abff82017-12-04 13:51:20 -08001674 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001675 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviwe5ac40f2019-09-24 16:36:55 -07001676 return state.isRelativeOf;
Robert Carr29abff82017-12-04 13:51:20 -08001677}
1678
David Sodman41fdfc92017-11-06 16:09:56 -08001679__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001680 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001681 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1682 "makeTraversalList received invalid stateSet");
1683 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1684 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001685 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001686
Robert Carr29abff82017-12-04 13:51:20 -08001687 if (state.zOrderRelatives.size() == 0) {
1688 *outSkipRelativeZUsers = true;
1689 return children;
1690 }
1691
chaviwfd462612018-05-31 16:11:27 -07001692 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001693 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001694 sp<Layer> strongRelative = weakRelative.promote();
1695 if (strongRelative != nullptr) {
1696 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001697 }
1698 }
1699
Dan Stoza412903f2017-04-27 13:42:17 -07001700 for (const sp<Layer>& child : children) {
chaviwe5ac40f2019-09-24 16:36:55 -07001701 if (child->usingRelativeZ(stateSet)) {
Robert Carr503c7042017-09-27 15:06:08 -07001702 continue;
1703 }
Robert Carrdb66e622017-04-10 16:55:57 -07001704 traverse.add(child);
1705 }
1706
1707 return traverse;
1708}
1709
Robert Carr1f0a16a2016-10-24 16:27:39 -07001710/**
Robert Carrdb66e622017-04-10 16:55:57 -07001711 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001712 */
Dan Stoza412903f2017-04-27 13:42:17 -07001713void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001714 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1715 // produce a new list for traversing, including our relatives, and not including our children
1716 // who are relatives of another surface. In the case that there are no relative Z,
1717 // makeTraversalList returns our children directly to avoid significant overhead.
1718 // However in this case we need to take the responsibility for filtering children which
1719 // are relatives of another surface here.
1720 bool skipRelativeZUsers = false;
1721 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001722
Robert Carr1f0a16a2016-10-24 16:27:39 -07001723 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001724 for (; i < list.size(); i++) {
1725 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001726 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1727 continue;
1728 }
1729
chaviw301b1d82019-11-06 13:15:09 -08001730 if (relative->getZ(stateSet) >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001731 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001732 }
Dan Stoza412903f2017-04-27 13:42:17 -07001733 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001734 }
Robert Carr29abff82017-12-04 13:51:20 -08001735
Dan Stoza412903f2017-04-27 13:42:17 -07001736 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001737 for (; i < list.size(); i++) {
1738 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001739
Robert Carr29abff82017-12-04 13:51:20 -08001740 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1741 continue;
1742 }
Dan Stoza412903f2017-04-27 13:42:17 -07001743 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001744 }
1745}
1746
1747/**
Robert Carrdb66e622017-04-10 16:55:57 -07001748 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001749 */
Dan Stoza412903f2017-04-27 13:42:17 -07001750void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1751 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001752 // See traverseInZOrder for documentation.
1753 bool skipRelativeZUsers = false;
1754 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001755
Robert Carr1f0a16a2016-10-24 16:27:39 -07001756 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001757 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001758 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001759
1760 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1761 continue;
1762 }
1763
chaviw301b1d82019-11-06 13:15:09 -08001764 if (relative->getZ(stateSet) < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001765 break;
1766 }
Dan Stoza412903f2017-04-27 13:42:17 -07001767 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001768 }
Dan Stoza412903f2017-04-27 13:42:17 -07001769 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001770 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001771 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001772
1773 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1774 continue;
1775 }
1776
Dan Stoza412903f2017-04-27 13:42:17 -07001777 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001778 }
1779}
1780
chaviw4b129c22018-04-09 16:19:43 -07001781LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1782 const std::vector<Layer*>& layersInTree) {
1783 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1784 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001785 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1786 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001787 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001788
chaviwfd462612018-05-31 16:11:27 -07001789 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001790 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1791 sp<Layer> strongRelative = weakRelative.promote();
1792 // Only add relative layers that are also descendents of the top most parent of the tree.
1793 // If a relative layer is not a descendent, then it should be ignored.
1794 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1795 traverse.add(strongRelative);
1796 }
1797 }
1798
1799 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001800 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001801 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1802 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1803 // the child here since it won't be added later as a relative.
1804 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1805 childState.zOrderRelativeOf.promote().get())) {
1806 continue;
1807 }
1808 traverse.add(child);
1809 }
1810
1811 return traverse;
1812}
1813
1814void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1815 LayerVector::StateSet stateSet,
1816 const LayerVector::Visitor& visitor) {
1817 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001818
1819 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001820 for (; i < list.size(); i++) {
1821 const auto& relative = list[i];
chaviw301b1d82019-11-06 13:15:09 -08001822 if (relative->getZ(stateSet) >= 0) {
chaviwa76b2712017-09-20 12:02:26 -07001823 break;
1824 }
chaviw4b129c22018-04-09 16:19:43 -07001825 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001826 }
chaviw4b129c22018-04-09 16:19:43 -07001827
chaviwa76b2712017-09-20 12:02:26 -07001828 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001829 for (; i < list.size(); i++) {
1830 const auto& relative = list[i];
1831 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001832 }
1833}
1834
chaviw4b129c22018-04-09 16:19:43 -07001835std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1836 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1837 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1838
1839 std::vector<Layer*> layersInTree = {this};
1840 for (size_t i = 0; i < children.size(); i++) {
1841 const auto& child = children[i];
1842 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1843 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1844 }
1845
1846 return layersInTree;
1847}
1848
1849void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1850 const LayerVector::Visitor& visitor) {
1851 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1852 std::sort(layersInTree.begin(), layersInTree.end());
1853 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1854}
1855
Peiyong Linefefaac2018-08-17 12:27:51 -07001856ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001857 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001858}
1859
chaviw13fdc492017-06-27 12:40:18 -07001860half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001861 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001862
chaviw13fdc492017-06-27 12:40:18 -07001863 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1864 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001865}
Robert Carr6452f122017-03-21 10:41:29 -07001866
chaviw13fdc492017-06-27 12:40:18 -07001867half4 Layer::getColor() const {
1868 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001869 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001870}
Robert Carr6452f122017-03-21 10:41:29 -07001871
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001872Layer::RoundedCornerState Layer::getRoundedCornerState() const {
1873 const auto& p = mDrawingParent.promote();
1874 if (p != nullptr) {
Peiyong Lin27016a92019-03-29 17:36:08 +00001875 RoundedCornerState parentState = p->getRoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001876 if (parentState.radius > 0) {
1877 ui::Transform t = getActiveTransform(getDrawingState());
1878 t = t.inverse();
1879 parentState.cropRect = t.transform(parentState.cropRect);
1880 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1881 // but a transform matrix can define horizontal and vertical scales.
1882 // Let's take the average between both of them and pass into the shader, practically we
1883 // never do this type of transformation on windows anyway.
1884 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
1885 return parentState;
1886 }
1887 }
1888 const float radius = getDrawingState().cornerRadius;
Peiyong Linb9ff23e2019-04-08 11:04:59 -07001889 return radius > 0 && getCrop(getDrawingState()).isValid()
1890 ? RoundedCornerState(getCrop(getDrawingState()).toFloatRect(), radius)
1891 : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001892}
1893
Vishnu Nair08f6eae2019-11-26 14:01:39 -08001894renderengine::ShadowSettings Layer::getShadowSettings(const Rect& viewport) const {
1895 renderengine::ShadowSettings state = mFlinger->mDrawingState.globalShadowSettings;
1896
1897 // Shift the spot light x-position to the middle of the display and then
1898 // offset it by casting layer's screen pos.
1899 state.lightPos.x = (viewport.width() / 2.f) - mScreenBounds.left;
1900 state.lightPos.y -= mScreenBounds.top;
1901
1902 state.length = mEffectiveShadowRadius;
1903 return state;
1904}
1905
Robert Carr1f0a16a2016-10-24 16:27:39 -07001906void Layer::commitChildList() {
1907 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1908 const auto& child = mCurrentChildren[i];
1909 child->commitChildList();
1910 }
1911 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001912 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001913}
1914
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001915static wp<Layer> extractLayerFromBinder(const wp<IBinder>& weakBinderHandle) {
1916 if (weakBinderHandle == nullptr) {
1917 return nullptr;
1918 }
1919 sp<IBinder> binderHandle = weakBinderHandle.promote();
1920 if (binderHandle == nullptr) {
1921 return nullptr;
1922 }
1923 sp<Layer::Handle> handle = static_cast<Layer::Handle*>(binderHandle.get());
1924 if (handle == nullptr) {
1925 return nullptr;
1926 }
1927 return handle->owner;
1928}
1929
Robert Carr720e5062018-07-30 17:45:14 -07001930void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001931 mCurrentState.inputInfo = info;
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001932 mCurrentState.touchableRegionCrop = extractLayerFromBinder(info.touchableRegionCropHandle);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001933 mCurrentState.modified = true;
1934 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07001935 setTransactionFlags(eTransactionNeeded);
1936}
1937
chaviw6d89e2d2020-01-14 14:42:01 -08001938LayerProto* Layer::writeToProto(LayersProto& layersProto, uint32_t traceFlags) const {
chaviw08f3cb22020-01-13 13:17:21 -08001939 LayerProto* layerProto = layersProto.add_layers();
1940 writeToProtoDrawingState(layerProto, traceFlags);
1941 writeToProtoCommonState(layerProto, LayerVector::StateSet::Drawing, traceFlags);
1942
1943 for (const sp<Layer>& layer : mDrawingChildren) {
1944 layer->writeToProto(layersProto, traceFlags);
1945 }
chaviw6d89e2d2020-01-14 14:42:01 -08001946
1947 return layerProto;
chaviw08f3cb22020-01-13 13:17:21 -08001948}
1949
Vishnu Nair8406fd72019-07-30 11:29:31 -07001950void Layer::writeToProtoDrawingState(LayerProto* layerInfo, uint32_t traceFlags) const {
1951 ui::Transform transform = getTransform();
1952
1953 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
1954 for (const auto& pendingState : mPendingStatesSnapshot) {
1955 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
1956 if (barrierLayer != nullptr) {
1957 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
1958 barrierLayerProto->set_id(barrierLayer->sequence);
1959 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
1960 }
1961 }
1962
chaviwd62d3062019-09-04 14:48:02 -07001963 auto buffer = getBuffer();
Vishnu Nair8406fd72019-07-30 11:29:31 -07001964 if (buffer != nullptr) {
1965 LayerProtoHelper::writeToProto(buffer,
1966 [&]() { return layerInfo->mutable_active_buffer(); });
chaviw4244e032019-09-04 11:27:49 -07001967 LayerProtoHelper::writeToProto(ui::Transform(getBufferTransform()),
Vishnu Nair8406fd72019-07-30 11:29:31 -07001968 layerInfo->mutable_buffer_transform());
1969 }
1970 layerInfo->set_invalidate(contentDirty);
1971 layerInfo->set_is_protected(isProtected());
chaviw4244e032019-09-04 11:27:49 -07001972 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(getDataSpace())));
Vishnu Nair8406fd72019-07-30 11:29:31 -07001973 layerInfo->set_queued_frames(getQueuedFrameCount());
1974 layerInfo->set_refresh_pending(isBufferLatched());
1975 layerInfo->set_curr_frame(mCurrentFrameNumber);
1976 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
1977
1978 layerInfo->set_corner_radius(getRoundedCornerState().radius);
1979 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1980 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
1981 [&]() { return layerInfo->mutable_position(); });
1982 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
Lloyd Piquea2468662019-03-07 21:31:06 -08001983 LayerProtoHelper::writeToProto(debugGetVisibleRegionOnDefaultDisplay(),
Vishnu Nair8406fd72019-07-30 11:29:31 -07001984 [&]() { return layerInfo->mutable_visible_region(); });
1985 LayerProtoHelper::writeToProto(surfaceDamageRegion,
1986 [&]() { return layerInfo->mutable_damage_region(); });
chaviwddeae262020-01-06 10:31:23 -08001987
1988 if (hasColorTransform()) {
1989 LayerProtoHelper::writeToProto(getColorTransform(),
1990 layerInfo->mutable_color_transform());
1991 }
Vishnu Nair8406fd72019-07-30 11:29:31 -07001992 }
1993
1994 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
1995 LayerProtoHelper::writeToProto(mSourceBounds,
1996 [&]() { return layerInfo->mutable_source_bounds(); });
1997 LayerProtoHelper::writeToProto(mScreenBounds,
1998 [&]() { return layerInfo->mutable_screen_bounds(); });
Vishnu Nair95a1ed42019-12-06 12:25:11 -08001999 LayerProtoHelper::writeToProto(getRoundedCornerState().cropRect,
2000 [&]() { return layerInfo->mutable_corner_radius_crop(); });
2001 layerInfo->set_shadow_radius(mEffectiveShadowRadius);
Vishnu Nair8406fd72019-07-30 11:29:31 -07002002 }
2003}
2004
2005void Layer::writeToProtoCommonState(LayerProto* layerInfo, LayerVector::StateSet stateSet,
2006 uint32_t traceFlags) const {
chaviw1d044282017-09-27 12:19:28 -07002007 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2008 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002009 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07002010
Peiyong Linefefaac2018-08-17 12:27:51 -07002011 ui::Transform requestedTransform = state.active_legacy.transform;
chaviw1d044282017-09-27 12:19:28 -07002012
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002013 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
2014 layerInfo->set_id(sequence);
2015 layerInfo->set_name(getName().c_str());
chaviw8a01fa42019-08-19 12:39:31 -07002016 layerInfo->set_type(getType());
chaviw1d044282017-09-27 12:19:28 -07002017
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002018 for (const auto& child : children) {
2019 layerInfo->add_children(child->sequence);
chaviw1d044282017-09-27 12:19:28 -07002020 }
chaviw1d044282017-09-27 12:19:28 -07002021
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002022 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
2023 sp<Layer> strongRelative = weakRelative.promote();
2024 if (strongRelative != nullptr) {
2025 layerInfo->add_relatives(strongRelative->sequence);
2026 }
chaviwadc40c22018-07-10 16:57:27 -07002027 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002028
2029 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
2030 [&]() { return layerInfo->mutable_transparent_region(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002031
2032 layerInfo->set_layer_stack(getLayerStack());
2033 layerInfo->set_z(state.z);
2034
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002035 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(),
2036 [&]() {
2037 return layerInfo->mutable_requested_position();
2038 });
2039
2040 LayerProtoHelper::writeSizeToProto(state.active_legacy.w, state.active_legacy.h,
2041 [&]() { return layerInfo->mutable_size(); });
2042
2043 LayerProtoHelper::writeToProto(state.crop_legacy,
2044 [&]() { return layerInfo->mutable_crop(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002045
2046 layerInfo->set_is_opaque(isOpaque(state));
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002047
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002048
2049 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
2050 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
2051 LayerProtoHelper::writeToProto(state.color,
2052 [&]() { return layerInfo->mutable_requested_color(); });
2053 layerInfo->set_flags(state.flags);
2054
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002055 LayerProtoHelper::writeToProto(requestedTransform,
2056 layerInfo->mutable_requested_transform());
2057
2058 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
2059 if (parent != nullptr) {
2060 layerInfo->set_parent(parent->sequence);
2061 } else {
2062 layerInfo->set_parent(-1);
2063 }
2064
2065 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2066 if (zOrderRelativeOf != nullptr) {
2067 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
2068 } else {
2069 layerInfo->set_z_order_relative_of(-1);
2070 }
chaviw08f3cb22020-01-13 13:17:21 -08002071
2072 layerInfo->set_is_relative_of(state.isRelativeOf);
chaviwadc40c22018-07-10 16:57:27 -07002073 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002074
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002075 if (traceFlags & SurfaceTracing::TRACE_INPUT) {
2076 LayerProtoHelper::writeToProto(state.inputInfo, state.touchableRegionCrop,
2077 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002078 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002079
2080 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
2081 auto protoMap = layerInfo->mutable_metadata();
2082 for (const auto& entry : state.metadata.mMap) {
2083 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
2084 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07002085 }
chaviw1d044282017-09-27 12:19:28 -07002086}
2087
Robert Carr2e102c92018-10-23 12:11:15 -07002088bool Layer::isRemovedFromCurrentState() const {
2089 return mRemovedFromCurrentState;
2090}
2091
Arthur Hungd20b2702019-01-14 18:16:16 +08002092InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002093 InputWindowInfo info = mDrawingState.inputInfo;
chaviwaf87b3e2019-10-01 16:59:28 -07002094 info.id = sequence;
Robert Carr720e5062018-07-30 17:45:14 -07002095
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002096 if (info.displayId == ADISPLAY_ID_NONE) {
chaviwb15ea8a2019-09-03 12:40:22 -07002097 info.displayId = getLayerStack();
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002098 }
2099
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002100 ui::Transform t = getTransform();
2101 const float xScale = t.sx();
2102 const float yScale = t.sy();
Ady Abraham282f1d72019-07-24 18:05:56 -07002103 int32_t xSurfaceInset = info.surfaceInset;
2104 int32_t ySurfaceInset = info.surfaceInset;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002105 if (xScale != 1.0f || yScale != 1.0f) {
Ady Abraham282f1d72019-07-24 18:05:56 -07002106 info.windowXScale *= (xScale != 0.0f) ? 1.0f / xScale : 0.0f;
2107 info.windowYScale *= (yScale != 0.0f) ? 1.0f / yScale : 0.0f;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002108 info.touchableRegion.scaleSelf(xScale, yScale);
Ady Abraham282f1d72019-07-24 18:05:56 -07002109 xSurfaceInset = std::round(xSurfaceInset * xScale);
2110 ySurfaceInset = std::round(ySurfaceInset * yScale);
Riddle Hsu39d4aa52018-11-30 20:46:53 +08002111 }
Robert Carre07e1032018-11-26 12:55:53 -08002112
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002113 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002114 // If this is a portal window, set the touchableRegion to the layerBounds.
2115 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
2116 ? getBufferSize(getDrawingState())
2117 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002118 if (!layerBounds.isValid()) {
2119 layerBounds = getCroppedBufferSize(getDrawingState());
2120 }
Vishnu Nair8033a492018-12-05 07:27:23 -08002121 layerBounds = t.transform(layerBounds);
Ady Abraham282f1d72019-07-24 18:05:56 -07002122
2123 // clamp inset to layer bounds
2124 xSurfaceInset = (xSurfaceInset >= 0) ? std::min(xSurfaceInset, layerBounds.getWidth() / 2) : 0;
2125 ySurfaceInset = (ySurfaceInset >= 0) ? std::min(ySurfaceInset, layerBounds.getHeight() / 2) : 0;
2126
Arthur Hung118b1142019-05-08 21:25:59 +08002127 layerBounds.inset(xSurfaceInset, ySurfaceInset, xSurfaceInset, ySurfaceInset);
Vishnu Nair8033a492018-12-05 07:27:23 -08002128
Arthur Hungd20b2702019-01-14 18:16:16 +08002129 // Input coordinate should match the layer bounds.
2130 info.frameLeft = layerBounds.left;
2131 info.frameTop = layerBounds.top;
2132 info.frameRight = layerBounds.right;
2133 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08002134
2135 // Position the touchable region relative to frame screen location and restrict it to frame
2136 // bounds.
2137 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08002138 info.visible = canReceiveInput();
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002139
2140 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2141 if (info.replaceTouchableRegionWithCrop) {
2142 if (cropLayer == nullptr) {
2143 info.touchableRegion = Region(Rect{mScreenBounds});
2144 } else {
2145 info.touchableRegion = Region(Rect{cropLayer->mScreenBounds});
2146 }
2147 } else if (cropLayer != nullptr) {
2148 info.touchableRegion = info.touchableRegion.intersect(Rect{cropLayer->mScreenBounds});
2149 }
2150
chaviwaf87b3e2019-10-01 16:59:28 -07002151 // If the layer is a clone, we need to crop the input region to cloned root to prevent
2152 // touches from going outside the cloned area.
2153 if (isClone()) {
2154 sp<Layer> clonedRoot = getClonedRoot();
2155 if (clonedRoot != nullptr) {
2156 Rect rect(clonedRoot->mScreenBounds);
2157 info.touchableRegion = info.touchableRegion.intersect(rect);
2158 }
2159 }
2160
Robert Carr720e5062018-07-30 17:45:14 -07002161 return info;
2162}
2163
chaviwaf87b3e2019-10-01 16:59:28 -07002164sp<Layer> Layer::getClonedRoot() {
2165 if (mClonedChild != nullptr) {
2166 return this;
2167 }
2168 if (mDrawingParent == nullptr || mDrawingParent.promote() == nullptr) {
2169 return nullptr;
2170 }
2171 return mDrawingParent.promote()->getClonedRoot();
2172}
2173
Robert Carr720e5062018-07-30 17:45:14 -07002174bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002175 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002176}
2177
Lloyd Piquefeb73d72018-12-04 17:23:44 -08002178std::shared_ptr<compositionengine::Layer> Layer::getCompositionLayer() const {
2179 return nullptr;
2180}
2181
chaviw3e727cd2019-01-31 13:41:05 -08002182bool Layer::canReceiveInput() const {
Vishnu Nair82353e92019-09-12 12:38:47 -07002183 return !isHiddenByPolicy();
chaviw3e727cd2019-01-31 13:41:05 -08002184}
2185
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002186compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
2187 const sp<const DisplayDevice>& display) const {
2188 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionLayer().get());
2189}
2190
Lloyd Piquea2468662019-03-07 21:31:06 -08002191Region Layer::debugGetVisibleRegionOnDefaultDisplay() const {
2192 sp<DisplayDevice> displayDevice = mFlinger->getDefaultDisplayDeviceLocked();
2193 if (displayDevice == nullptr) {
2194 return {};
2195 }
2196
2197 auto outputLayer = findOutputLayerForDisplay(displayDevice);
2198 if (outputLayer == nullptr) {
2199 return {};
2200 }
2201
2202 return outputLayer->getState().visibleRegion;
2203}
2204
chaviwb4c6e582019-08-16 14:35:07 -07002205void Layer::setInitialValuesForClone(const sp<Layer>& clonedFrom) {
2206 // copy drawing state from cloned layer
2207 mDrawingState = clonedFrom->mDrawingState;
2208 mClonedFrom = clonedFrom;
chaviwb4c6e582019-08-16 14:35:07 -07002209}
chaviw74b03172019-08-19 11:09:03 -07002210
2211void Layer::updateMirrorInfo() {
2212 if (mClonedChild == nullptr || !mClonedChild->isClonedFromAlive()) {
2213 // If mClonedChild is null, there is nothing to mirror. If isClonedFromAlive returns false,
2214 // it means that there is a clone, but the layer it was cloned from has been destroyed. In
2215 // that case, we want to delete the reference to the clone since we want it to get
2216 // destroyed. The root, this layer, will still be around since the client can continue
2217 // to hold a reference, but no cloned layers will be displayed.
2218 mClonedChild = nullptr;
2219 return;
2220 }
2221
2222 std::map<sp<Layer>, sp<Layer>> clonedLayersMap;
2223 // If the real layer exists and is in current state, add the clone as a child of the root.
2224 // There's no need to remove from drawingState when the layer is offscreen since currentState is
2225 // copied to drawingState for the root layer. So the clonedChild is always removed from
2226 // drawingState and then needs to be added back each traversal.
2227 if (!mClonedChild->getClonedFrom()->isRemovedFromCurrentState()) {
2228 addChildToDrawing(mClonedChild);
2229 }
2230
2231 mClonedChild->updateClonedDrawingState(clonedLayersMap);
2232 mClonedChild->updateClonedChildren(this, clonedLayersMap);
2233 mClonedChild->updateClonedRelatives(clonedLayersMap);
2234}
2235
2236void Layer::updateClonedDrawingState(std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2237 // If the layer the clone was cloned from is alive, copy the content of the drawingState
2238 // to the clone. If the real layer is no longer alive, continue traversing the children
2239 // since we may be able to pull out other children that are still alive.
2240 if (isClonedFromAlive()) {
2241 sp<Layer> clonedFrom = getClonedFrom();
2242 mDrawingState = clonedFrom->mDrawingState;
chaviw74b03172019-08-19 11:09:03 -07002243 clonedLayersMap.emplace(clonedFrom, this);
2244 }
2245
2246 // The clone layer may have children in drawingState since they may have been created and
2247 // added from a previous request to updateMirorInfo. This is to ensure we don't recreate clones
2248 // that already exist, since we can just re-use them.
2249 // The drawingChildren will not get overwritten by the currentChildren since the clones are
2250 // not updated in the regular traversal. They are skipped since the root will lose the
2251 // reference to them when it copies its currentChildren to drawing.
2252 for (sp<Layer>& child : mDrawingChildren) {
2253 child->updateClonedDrawingState(clonedLayersMap);
2254 }
2255}
2256
2257void Layer::updateClonedChildren(const sp<Layer>& mirrorRoot,
2258 std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2259 mDrawingChildren.clear();
2260
2261 if (!isClonedFromAlive()) {
2262 return;
2263 }
2264
2265 sp<Layer> clonedFrom = getClonedFrom();
2266 for (sp<Layer>& child : clonedFrom->mDrawingChildren) {
2267 if (child == mirrorRoot) {
2268 // This is to avoid cyclical mirroring.
2269 continue;
2270 }
2271 sp<Layer> clonedChild = clonedLayersMap[child];
2272 if (clonedChild == nullptr) {
2273 clonedChild = child->createClone();
2274 clonedLayersMap[child] = clonedChild;
2275 }
2276 addChildToDrawing(clonedChild);
2277 clonedChild->updateClonedChildren(mirrorRoot, clonedLayersMap);
2278 }
2279}
2280
chaviwaf87b3e2019-10-01 16:59:28 -07002281void Layer::updateClonedInputInfo(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2282 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2283 if (cropLayer != nullptr) {
2284 if (clonedLayersMap.count(cropLayer) == 0) {
2285 // Real layer had a crop layer but it's not in the cloned hierarchy. Just set to
2286 // self as crop layer to avoid going outside bounds.
2287 mDrawingState.touchableRegionCrop = this;
2288 } else {
2289 const sp<Layer>& clonedCropLayer = clonedLayersMap.at(cropLayer);
2290 mDrawingState.touchableRegionCrop = clonedCropLayer;
2291 }
2292 }
2293 // Cloned layers shouldn't handle watch outside since their z order is not determined by
2294 // WM or the client.
2295 mDrawingState.inputInfo.layoutParamsFlags &= ~InputWindowInfo::FLAG_WATCH_OUTSIDE_TOUCH;
2296}
2297
2298void Layer::updateClonedRelatives(const std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
chaviw74b03172019-08-19 11:09:03 -07002299 mDrawingState.zOrderRelativeOf = nullptr;
2300 mDrawingState.zOrderRelatives.clear();
2301
2302 if (!isClonedFromAlive()) {
2303 return;
2304 }
2305
chaviwaf87b3e2019-10-01 16:59:28 -07002306 const sp<Layer>& clonedFrom = getClonedFrom();
chaviw74b03172019-08-19 11:09:03 -07002307 for (wp<Layer>& relativeWeak : clonedFrom->mDrawingState.zOrderRelatives) {
chaviwaf87b3e2019-10-01 16:59:28 -07002308 const sp<Layer>& relative = relativeWeak.promote();
2309 if (clonedLayersMap.count(relative) > 0) {
2310 auto& clonedRelative = clonedLayersMap.at(relative);
chaviw74b03172019-08-19 11:09:03 -07002311 mDrawingState.zOrderRelatives.add(clonedRelative);
2312 }
2313 }
2314
2315 // Check if the relativeLayer for the real layer is part of the cloned hierarchy.
2316 // It's possible that the layer it's relative to is outside the requested cloned hierarchy.
2317 // In that case, we treat the layer as if the relativeOf has been removed. This way, it will
2318 // still traverse the children, but the layer with the missing relativeOf will not be shown
2319 // on screen.
chaviwaf87b3e2019-10-01 16:59:28 -07002320 const sp<Layer>& relativeOf = clonedFrom->mDrawingState.zOrderRelativeOf.promote();
2321 if (clonedLayersMap.count(relativeOf) > 0) {
2322 const sp<Layer>& clonedRelativeOf = clonedLayersMap.at(relativeOf);
chaviw74b03172019-08-19 11:09:03 -07002323 mDrawingState.zOrderRelativeOf = clonedRelativeOf;
2324 }
2325
chaviwaf87b3e2019-10-01 16:59:28 -07002326 updateClonedInputInfo(clonedLayersMap);
2327
chaviw74b03172019-08-19 11:09:03 -07002328 for (sp<Layer>& child : mDrawingChildren) {
2329 child->updateClonedRelatives(clonedLayersMap);
2330 }
2331}
2332
2333void Layer::addChildToDrawing(const sp<Layer>& layer) {
2334 mDrawingChildren.add(layer);
2335 layer->mDrawingParent = this;
2336}
2337
Mathias Agopian13127d82013-03-05 17:47:11 -08002338// ---------------------------------------------------------------------------
2339
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002340}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002341
2342#if defined(__gl_h_)
2343#error "don't include gl/gl.h in this file"
2344#endif
2345
2346#if defined(__gl2_h_)
2347#error "don't include gl2/gl2.h in this file"
2348#endif