blob: 3757fc7c00a78321c325ad9b48b33e6ff9e1b64b [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dan Stoza9e56aa02015-11-02 13:00:03 -080017//#define LOG_NDEBUG 0
18#undef LOG_TAG
19#define LOG_TAG "Layer"
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080020#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21
Alec Mourie60041e2019-06-14 18:59:51 -070022#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023
Yiwei Zhang5434a782018-12-05 18:06:32 -080024#include <android-base/stringprintf.h>
Vishnu Nair0f085c62019-08-30 08:49:12 -070025#include <binder/IPCThreadState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080026#include <compositionengine/Display.h>
Lloyd Pique0b785d82018-12-04 17:25:27 -080027#include <compositionengine/Layer.h>
Lloyd Piquea83776c2019-01-29 18:42:32 -080028#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080029#include <compositionengine/OutputLayer.h>
30#include <compositionengine/impl/OutputLayerCompositionState.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070031#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070032#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070033#include <cutils/properties.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080034#include <gui/BufferItem.h>
35#include <gui/LayerDebugInfo.h>
36#include <gui/Surface.h>
Alec Mourie60041e2019-06-14 18:59:51 -070037#include <math.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080038#include <renderengine/RenderEngine.h>
Alec Mourie60041e2019-06-14 18:59:51 -070039#include <stdint.h>
40#include <stdlib.h>
41#include <sys/types.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080042#include <ui/DebugUtils.h>
43#include <ui/GraphicBuffer.h>
44#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045#include <utils/Errors.h>
46#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080047#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080049#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050
Alec Mourie60041e2019-06-14 18:59:51 -070051#include <algorithm>
52#include <mutex>
53#include <sstream>
54
Yiwei Zhang60d1a192018-03-07 14:52:28 -080055#include "BufferLayer.h"
Valerie Haudd0b7572019-01-29 14:59:27 -080056#include "ColorLayer.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020057#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070058#include "DisplayDevice.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080059#include "DisplayHardware/HWComposer.h"
Mikael Pessa90092f42019-08-26 17:22:04 -070060#include "FrameTracer/FrameTracer.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080061#include "LayerProtoHelper.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070062#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070063#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064#include "SurfaceFlinger.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080065#include "TimeStats/TimeStats.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070066
David Sodman41fdfc92017-11-06 16:09:56 -080067#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069namespace android {
70
Yiwei Zhang5434a782018-12-05 18:06:32 -080071using base::StringAppendF;
72
Lloyd Piquef1c675b2018-09-12 20:45:39 -070073std::atomic<int32_t> Layer::sSequence{1};
Mathias Agopian13127d82013-03-05 17:47:11 -080074
Lloyd Pique42ab75e2018-09-12 20:46:03 -070075Layer::Layer(const LayerCreationArgs& args)
Ady Abraham8f1ee7f2019-04-05 10:32:50 -070076 : mFlinger(args.flinger),
77 mName(args.name),
78 mClientRef(args.client),
79 mWindowType(args.metadata.getInt32(METADATA_WINDOW_TYPE, 0)) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -070080
81 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -070082 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
83 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
84 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070085
Lloyd Pique0449b0f2018-12-20 16:23:45 -080086 mCurrentState.active_legacy.w = args.w;
87 mCurrentState.active_legacy.h = args.h;
88 mCurrentState.flags = layerFlags;
89 mCurrentState.active_legacy.transform.set(0, 0);
90 mCurrentState.crop_legacy.makeInvalid();
91 mCurrentState.requestedCrop_legacy = mCurrentState.crop_legacy;
92 mCurrentState.z = 0;
93 mCurrentState.color.a = 1.0f;
94 mCurrentState.layerStack = 0;
95 mCurrentState.sequence = 0;
96 mCurrentState.requested_legacy = mCurrentState.active_legacy;
Lloyd Pique0449b0f2018-12-20 16:23:45 -080097 mCurrentState.active.w = UINT32_MAX;
98 mCurrentState.active.h = UINT32_MAX;
99 mCurrentState.active.transform.set(0, 0);
100 mCurrentState.transform = 0;
101 mCurrentState.transformToDisplayInverse = false;
102 mCurrentState.crop.makeInvalid();
103 mCurrentState.acquireFence = new Fence(-1);
104 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
105 mCurrentState.hdrMetadata.validTypes = 0;
Marissa Wall7e0a01f2019-08-14 14:29:25 -0700106 mCurrentState.surfaceDamageRegion = Region::INVALID_REGION;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800107 mCurrentState.cornerRadius = 0.0f;
108 mCurrentState.api = -1;
109 mCurrentState.hasColorTransform = false;
Peiyong Linc502cb72019-03-01 15:00:23 -0800110 mCurrentState.colorSpaceAgnostic = false;
Evan Roskya1f1e152019-01-24 16:17:46 -0800111 mCurrentState.metadata = args.metadata;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700112
113 // drawing state & current state are identical
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800114 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700115
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800116 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700117 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800118 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200119 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700120
Vishnu Nair0f085c62019-08-30 08:49:12 -0700121 mCallingPid = args.callingPid;
122 mCallingUid = args.callingUid;
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700123 mFlinger->onLayerCreated(this);
Dan Stoza436ccf32018-06-21 12:10:12 -0700124}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700125
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700126Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800127 sp<Client> c(mClientRef.promote());
128 if (c != 0) {
129 c->detachLayer(this);
130 }
131
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000132 mFrameTracker.logAndResetStats(mName);
chaviw74d90ad2019-04-26 14:45:26 -0700133 mFlinger->onLayerDestroyed(this);
Mathias Agopian96f08192010-06-02 23:28:45 -0700134}
135
Vishnu Nair0f085c62019-08-30 08:49:12 -0700136LayerCreationArgs::LayerCreationArgs(SurfaceFlinger* flinger, const sp<Client>& client,
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700137 std::string name, uint32_t w, uint32_t h, uint32_t flags,
Vishnu Nair0f085c62019-08-30 08:49:12 -0700138 LayerMetadata metadata)
139 : flinger(flinger),
140 client(client),
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700141 name(std::move(name)),
Vishnu Nair0f085c62019-08-30 08:49:12 -0700142 w(w),
143 h(h),
144 flags(flags),
145 metadata(std::move(metadata)) {
146 IPCThreadState* ipc = IPCThreadState::self();
147 callingPid = ipc->getCallingPid();
148 callingUid = ipc->getCallingUid();
149}
150
Mathias Agopian13127d82013-03-05 17:47:11 -0800151// ---------------------------------------------------------------------------
152// callbacks
153// ---------------------------------------------------------------------------
154
David Sodmaneb085e02017-10-05 18:49:04 -0700155/*
156 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
157 * Layer. So, the implementation is done in BufferLayer. When called on a
158 * ColorLayer object, it's essentially a NOP.
159 */
David Sodmaneb085e02017-10-05 18:49:04 -0700160void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800161
chaviw43cb3cb2019-05-31 15:23:41 -0700162void Layer::removeRemoteSyncPoints() {
163 for (auto& point : mRemoteSyncPoints) {
164 point->setTransactionApplied();
165 }
166 mRemoteSyncPoints.clear();
167
168 {
chaviw43cb3cb2019-05-31 15:23:41 -0700169 for (State pendingState : mPendingStates) {
170 pendingState.barrierLayer_legacy = nullptr;
171 }
172 }
173}
174
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700175void Layer::removeRelativeZ(const std::vector<Layer*>& layersInTree) {
176 if (mCurrentState.zOrderRelativeOf == nullptr) {
177 return;
178 }
Robert Carr2e102c92018-10-23 12:11:15 -0700179
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700180 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
181 if (strongRelative == nullptr) {
182 setZOrderRelativeOf(nullptr);
183 return;
184 }
185
186 if (!std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
187 strongRelative->removeZOrderRelative(this);
188 mFlinger->setTransactionFlags(eTraversalNeeded);
chaviw606e5cf2019-03-01 10:12:10 -0800189 setZOrderRelativeOf(nullptr);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700190 }
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700191}
192
193void Layer::removeFromCurrentState() {
194 mRemovedFromCurrentState = true;
Rob Carr4bba3702018-10-08 21:53:30 +0000195
Robert Carr2e102c92018-10-23 12:11:15 -0700196 // Since we are no longer reachable from CurrentState SurfaceFlinger
197 // will no longer invoke doTransaction for us, and so we will
198 // never finish applying transactions. We signal the sync point
199 // now so that another layer will not become indefinitely
200 // blocked.
chaviw43cb3cb2019-05-31 15:23:41 -0700201 removeRemoteSyncPoints();
Robert Carr2e102c92018-10-23 12:11:15 -0700202
203 {
204 Mutex::Autolock syncLock(mLocalSyncPointMutex);
205 for (auto& point : mLocalSyncPoints) {
206 point->setFrameAvailable();
207 }
208 mLocalSyncPoints.clear();
209 }
210
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800211 mFlinger->markLayerPendingRemovalLocked(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700212}
Chia-I Wu38512252017-05-17 14:36:16 -0700213
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700214void Layer::onRemovedFromCurrentState() {
215 auto layersInTree = getLayersInTree(LayerVector::StateSet::Current);
216 std::sort(layersInTree.begin(), layersInTree.end());
217 for (const auto& layer : layersInTree) {
218 layer->removeFromCurrentState();
219 layer->removeRelativeZ(layersInTree);
220 }
221}
222
chaviw61626f22018-11-15 16:26:27 -0800223void Layer::addToCurrentState() {
224 mRemovedFromCurrentState = false;
225
226 for (const auto& child : mCurrentChildren) {
227 child->addToCurrentState();
228 }
229}
230
Mathias Agopian13127d82013-03-05 17:47:11 -0800231// ---------------------------------------------------------------------------
232// set-up
233// ---------------------------------------------------------------------------
234
chaviw13fdc492017-06-27 12:40:18 -0700235bool Layer::getPremultipledAlpha() const {
236 return mPremultipliedAlpha;
237}
238
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700239sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800240 Mutex::Autolock _l(mLock);
Robert Carrc0df3122019-04-11 13:18:21 -0700241 if (mGetHandleCalled) {
242 ALOGE("Get handle called twice" );
243 return nullptr;
244 }
245 mGetHandleCalled = true;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700246 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800247}
248
249// ---------------------------------------------------------------------------
250// h/w composer set-up
251// ---------------------------------------------------------------------------
252
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700253static Rect reduce(const Rect& win, const Region& exclude) {
254 if (CC_LIKELY(exclude.isEmpty())) {
255 return win;
256 }
257 if (exclude.isRect()) {
258 return win.reduce(exclude.getBounds());
259 }
260 return Region(win).subtract(exclude).getBounds();
261}
262
Dan Stoza80d61162017-12-20 15:57:52 -0800263static FloatRect reduce(const FloatRect& win, const Region& exclude) {
264 if (CC_LIKELY(exclude.isEmpty())) {
265 return win;
266 }
267 // Convert through Rect (by rounding) for lack of FloatRegion
268 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
269}
270
Vishnu Nair4351ad52019-02-11 14:13:02 -0800271Rect Layer::getScreenBounds(bool reduceTransparentRegion) const {
Vishnu Nairf0c28512019-02-08 12:40:28 -0800272 if (!reduceTransparentRegion) {
273 return Rect{mScreenBounds};
274 }
275
276 FloatRect bounds = getBounds();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800277 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800278 // Transform to screen space.
279 bounds = t.transform(bounds);
280 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700281}
282
Vishnu Nair4351ad52019-02-11 14:13:02 -0800283FloatRect Layer::getBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000284 const State& s(getDrawingState());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800285 return getBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700286}
287
Vishnu Nairf0c28512019-02-08 12:40:28 -0800288FloatRect Layer::getBounds(const Region& activeTransparentRegion) const {
289 // Subtract the transparent region and snap to the bounds.
290 return reduce(mBounds, activeTransparentRegion);
291}
292
Vishnu Nairc652ff82019-03-15 12:48:54 -0700293ui::Transform Layer::getBufferScaleTransform() const {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800294 // If the layer is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
295 // it isFixedSize) then there may be additional scaling not accounted
Vishnu Nairc652ff82019-03-15 12:48:54 -0700296 // for in the layer transform.
chaviwd62d3062019-09-04 14:48:02 -0700297 if (!isFixedSize() || getBuffer() == nullptr) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700298 return {};
Vishnu Nair4351ad52019-02-11 14:13:02 -0800299 }
300
Marissa Wall290ad082019-03-06 13:23:47 -0800301 // If the layer is a buffer state layer, the active width and height
302 // could be infinite. In that case, return the effective transform.
303 const uint32_t activeWidth = getActiveWidth(getDrawingState());
304 const uint32_t activeHeight = getActiveHeight(getDrawingState());
305 if (activeWidth >= UINT32_MAX && activeHeight >= UINT32_MAX) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700306 return {};
Marissa Wall290ad082019-03-06 13:23:47 -0800307 }
308
chaviwd62d3062019-09-04 14:48:02 -0700309 int bufferWidth = getBuffer()->getWidth();
310 int bufferHeight = getBuffer()->getHeight();
Vishnu Nairc652ff82019-03-15 12:48:54 -0700311
chaviw4244e032019-09-04 11:27:49 -0700312 if (getBufferTransform() & NATIVE_WINDOW_TRANSFORM_ROT_90) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700313 std::swap(bufferWidth, bufferHeight);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800314 }
Vishnu Nairc652ff82019-03-15 12:48:54 -0700315
Marissa Wall290ad082019-03-06 13:23:47 -0800316 float sx = activeWidth / static_cast<float>(bufferWidth);
317 float sy = activeHeight / static_cast<float>(bufferHeight);
318
Vishnu Nair4351ad52019-02-11 14:13:02 -0800319 ui::Transform extraParentScaling;
320 extraParentScaling.set(sx, 0, 0, sy);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700321 return extraParentScaling;
322}
323
324ui::Transform Layer::getTransformWithScale(const ui::Transform& bufferScaleTransform) const {
325 // We need to mirror this scaling to child surfaces or we will break the contract where WM can
326 // treat child surfaces as pixels in the parent surface.
chaviwd62d3062019-09-04 14:48:02 -0700327 if (!isFixedSize() || getBuffer() == nullptr) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700328 return mEffectiveTransform;
329 }
330 return mEffectiveTransform * bufferScaleTransform;
331}
332
333FloatRect Layer::getBoundsPreScaling(const ui::Transform& bufferScaleTransform) const {
334 // We need the pre scaled layer bounds when computing child bounds to make sure the child is
335 // cropped to its parent layer after any buffer transform scaling is applied.
chaviwd62d3062019-09-04 14:48:02 -0700336 if (!isFixedSize() || getBuffer() == nullptr) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700337 return mBounds;
338 }
339 return bufferScaleTransform.inverse().transform(mBounds);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800340}
341
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700342void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform,
343 float parentShadowRadius) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800344 const State& s(getDrawingState());
345
346 // Calculate effective layer transform
347 mEffectiveTransform = parentTransform * getActiveTransform(s);
348
349 // Transform parent bounds to layer space
350 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
351
Vishnu Nairc652ff82019-03-15 12:48:54 -0700352 // Calculate source bounds
Vishnu Nair4351ad52019-02-11 14:13:02 -0800353 mSourceBounds = computeSourceBounds(parentBounds);
354
355 // Calculate bounds by croping diplay frame with layer crop and parent bounds
356 FloatRect bounds = mSourceBounds;
357 const Rect layerCrop = getCrop(s);
358 if (!layerCrop.isEmpty()) {
359 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
360 }
361 bounds = bounds.intersect(parentBounds);
362
363 mBounds = bounds;
364 mScreenBounds = mEffectiveTransform.transform(mBounds);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700365
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700366 // Use the layer's own shadow radius if set. Otherwise get the radius from
367 // parent.
368 if (s.shadowRadius > 0.f) {
369 mEffectiveShadowRadius = s.shadowRadius;
370 } else {
371 mEffectiveShadowRadius = parentShadowRadius;
372 }
373
374 // Shadow radius is passed down to only one layer so if the layer can draw shadows,
375 // don't pass it to its children.
376 const float childShadowRadius = canDrawShadows() ? 0.f : mEffectiveShadowRadius;
377
Vishnu Nairc652ff82019-03-15 12:48:54 -0700378 // Add any buffer scaling to the layer's children.
379 ui::Transform bufferScaleTransform = getBufferScaleTransform();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800380 for (const sp<Layer>& child : mDrawingChildren) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700381 child->computeBounds(getBoundsPreScaling(bufferScaleTransform),
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700382 getTransformWithScale(bufferScaleTransform), childShadowRadius);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800383 }
384}
385
Vishnu Nair60356342018-11-13 13:00:45 -0800386Rect Layer::getCroppedBufferSize(const State& s) const {
387 Rect size = getBufferSize(s);
388 Rect crop = getCrop(s);
389 if (!crop.isEmpty() && size.isValid()) {
390 size.intersect(crop, &size);
391 } else if (!crop.isEmpty()) {
392 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700393 }
Vishnu Nair60356342018-11-13 13:00:45 -0800394 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800395}
396
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700397void Layer::setupRoundedCornersCropCoordinates(Rect win,
398 const FloatRect& roundedCornersCrop) const {
399 // Translate win by the rounded corners rect coordinates, to have all values in
400 // layer coordinate space.
401 win.left -= roundedCornersCrop.left;
402 win.right -= roundedCornersCrop.left;
403 win.top -= roundedCornersCrop.top;
404 win.bottom -= roundedCornersCrop.top;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700405}
406
Lloyd Piquec6687342019-03-07 21:34:57 -0800407void Layer::latchBasicGeometry(compositionengine::LayerFECompositionState& compositionState) const {
408 const auto& drawingState{getDrawingState()};
409 const uint32_t layerStack = getLayerStack();
410 const auto alpha = static_cast<float>(getAlpha());
411 const bool opaque = isOpaque(drawingState);
412 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
413
414 auto blendMode = Hwc2::IComposerClient::BlendMode::NONE;
415 if (!opaque || alpha != 1.0f) {
416 blendMode = mPremultipliedAlpha ? Hwc2::IComposerClient::BlendMode::PREMULTIPLIED
417 : Hwc2::IComposerClient::BlendMode::COVERAGE;
418 }
419
420 // TODO(b/121291683): Instead of filling in a passed-in compositionState
421 // structure, switch to Layer owning the structure and have
422 // CompositionEngine be able to get a reference to it.
423
424 compositionState.layerStackId =
425 (layerStack != ~0u) ? std::make_optional(layerStack) : std::nullopt;
426 compositionState.internalOnly = getPrimaryDisplayOnly();
427 compositionState.isVisible = isVisible();
428 compositionState.isOpaque = opaque && !usesRoundedCorners && alpha == 1.f;
429
430 compositionState.contentDirty = contentDirty;
431 contentDirty = false;
432
433 compositionState.geomLayerBounds = mBounds;
434 compositionState.geomLayerTransform = getTransform();
435 compositionState.geomInverseLayerTransform = compositionState.geomLayerTransform.inverse();
436 compositionState.transparentRegionHint = getActiveTransparentRegion(drawingState);
437
438 compositionState.blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
439 compositionState.alpha = alpha;
440}
441
Lloyd Piquea83776c2019-01-29 18:42:32 -0800442void Layer::latchGeometry(compositionengine::LayerFECompositionState& compositionState) const {
443 const auto& drawingState{getDrawingState()};
Mathias Agopian13127d82013-03-05 17:47:11 -0800444
Lloyd Piquea83776c2019-01-29 18:42:32 -0800445 int type = drawingState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
446 int appId = drawingState.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700447 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400448 if (parent.get()) {
449 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800450 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
451 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
Jiwen 'Steve' Cai736c74e2019-05-28 18:33:22 -0700452 if (parentType > 0 && parentAppId > 0) {
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800453 type = parentType;
454 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700455 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400456 }
457
Lloyd Piquea83776c2019-01-29 18:42:32 -0800458 compositionState.geomBufferSize = getBufferSize(drawingState);
chaviw4244e032019-09-04 11:27:49 -0700459 compositionState.geomContentCrop = getBufferCrop();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800460 compositionState.geomCrop = getCrop(drawingState);
chaviw4244e032019-09-04 11:27:49 -0700461 compositionState.geomBufferTransform = getBufferTransform();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800462 compositionState.geomBufferUsesDisplayInverseTransform = getTransformToDisplayInverse();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800463 compositionState.geomUsesSourceCrop = usesSourceCrop();
464 compositionState.isSecure = isSecure();
David Sodman15094112018-10-11 09:39:37 -0700465
Lloyd Piquea83776c2019-01-29 18:42:32 -0800466 compositionState.type = type;
467 compositionState.appId = appId;
468}
David Sodmanba340492018-08-05 21:51:33 -0700469
Lloyd Piquef5275482019-01-29 18:42:42 -0800470void Layer::latchPerFrameState(compositionengine::LayerFECompositionState& compositionState) const {
Lloyd Pique688abd42019-02-15 15:42:24 -0800471 const auto& drawingState{getDrawingState()};
Lloyd Piquef5275482019-01-29 18:42:42 -0800472 compositionState.forceClientComposition = false;
473
Lloyd Piquef5275482019-01-29 18:42:42 -0800474 compositionState.isColorspaceAgnostic = isColorSpaceAgnostic();
chaviw4244e032019-09-04 11:27:49 -0700475 compositionState.dataspace = getDataSpace();
Lloyd Piquef5275482019-01-29 18:42:42 -0800476 compositionState.colorTransform = getColorTransform();
477 compositionState.colorTransformIsIdentity = !hasColorTransform();
478 compositionState.surfaceDamage = surfaceDamageRegion;
Lloyd Pique688abd42019-02-15 15:42:24 -0800479 compositionState.hasProtectedContent = isProtected();
480
481 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700482 const bool drawsShadows = mEffectiveShadowRadius != 0.f;
483
Lloyd Pique688abd42019-02-15 15:42:24 -0800484 compositionState.isOpaque =
485 isOpaque(drawingState) && !usesRoundedCorners && getAlpha() == 1.0_hf;
Lloyd Piquef5275482019-01-29 18:42:42 -0800486
487 // Force client composition for special cases known only to the front-end.
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700488 if (isHdrY410() || usesRoundedCorners || drawsShadows) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800489 compositionState.forceClientComposition = true;
490 }
491}
492
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800493void Layer::latchCursorCompositionState(
494 compositionengine::LayerFECompositionState& compositionState) const {
495 // This gives us only the "orientation" component of the transform
496 const State& drawingState{getDrawingState()};
497
498 // Apply the layer's transform, followed by the display's global transform
499 // Here we're guaranteed that the layer's transform preserves rects
500 Rect win = getCroppedBufferSize(drawingState);
501 // Subtract the transparent region and snap to the bounds
502 Rect bounds = reduce(win, getActiveTransparentRegion(drawingState));
503 Rect frame(getTransform().transform(bounds));
504
505 compositionState.cursorFrame = frame;
506}
507
Lloyd Piquef16688f2019-02-19 17:47:57 -0800508bool Layer::onPreComposition(nsecs_t) {
509 return false;
510}
511
Lloyd Piquea83776c2019-01-29 18:42:32 -0800512void Layer::latchCompositionState(compositionengine::LayerFECompositionState& compositionState,
Lloyd Piquec6687342019-03-07 21:34:57 -0800513 compositionengine::LayerFE::StateSubset subset) const {
514 using StateSubset = compositionengine::LayerFE::StateSubset;
Lloyd Piquef5275482019-01-29 18:42:42 -0800515
Lloyd Piquec6687342019-03-07 21:34:57 -0800516 switch (subset) {
517 case StateSubset::BasicGeometry:
518 latchBasicGeometry(compositionState);
519 break;
520
521 case StateSubset::GeometryAndContent:
522 latchBasicGeometry(compositionState);
523 latchGeometry(compositionState);
524 latchPerFrameState(compositionState);
525 break;
526
527 case StateSubset::Content:
528 latchPerFrameState(compositionState);
529 break;
530 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800531}
Mathias Agopian29a367b2011-07-12 14:51:45 -0700532
Lloyd Piquea83776c2019-01-29 18:42:32 -0800533const char* Layer::getDebugName() const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700534 return mName.c_str();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800535}
536
Mathias Agopian13127d82013-03-05 17:47:11 -0800537// ---------------------------------------------------------------------------
538// drawing...
539// ---------------------------------------------------------------------------
540
Lloyd Piquef16688f2019-02-19 17:47:57 -0800541std::optional<renderengine::LayerSettings> Layer::prepareClientComposition(
542 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
543 if (!getCompositionLayer()) {
544 return {};
545 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800546
Vishnu Nair4351ad52019-02-11 14:13:02 -0800547 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000548 half alpha = getAlpha();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800549 renderengine::LayerSettings layerSettings;
550 layerSettings.geometry.boundaries = bounds;
551 if (targetSettings.useIdentityTransform) {
552 layerSettings.geometry.positionTransform = mat4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000553 } else {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800554 layerSettings.geometry.positionTransform = getTransform().asMatrix4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000555 }
556
557 if (hasColorTransform()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800558 layerSettings.colorTransform = getColorTransform();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000559 }
560
561 const auto roundedCornerState = getRoundedCornerState();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800562 layerSettings.geometry.roundedCornersRadius = roundedCornerState.radius;
563 layerSettings.geometry.roundedCornersCrop = roundedCornerState.cropRect;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000564
Lloyd Piquef16688f2019-02-19 17:47:57 -0800565 layerSettings.alpha = alpha;
chaviw4244e032019-09-04 11:27:49 -0700566 layerSettings.sourceDataspace = getDataSpace();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800567 return layerSettings;
Mathias Agopian13127d82013-03-05 17:47:11 -0800568}
569
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800570Hwc2::IComposerClient::Composition Layer::getCompositionType(
571 const sp<const DisplayDevice>& display) const {
572 const auto outputLayer = findOutputLayerForDisplay(display);
573 LOG_FATAL_IF(!outputLayer);
574 return outputLayer->getState().hwc ? (*outputLayer->getState().hwc).hwcCompositionType
575 : Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800576}
577
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800578bool Layer::getClearClientTarget(const sp<const DisplayDevice>& display) const {
579 const auto outputLayer = findOutputLayerForDisplay(display);
580 LOG_FATAL_IF(!outputLayer);
581 return outputLayer->getState().clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800582}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800583
Dan Stozacac35382016-01-27 12:21:06 -0800584bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
585 if (point->getFrameNumber() <= mCurrentFrameNumber) {
586 // Don't bother with a SyncPoint, since we've already latched the
587 // relevant frame
588 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700589 }
Robert Carr2e102c92018-10-23 12:11:15 -0700590 if (isRemovedFromCurrentState()) {
591 return false;
592 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700593
Dan Stozacac35382016-01-27 12:21:06 -0800594 Mutex::Autolock lock(mLocalSyncPointMutex);
595 mLocalSyncPoints.push_back(point);
596 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700597}
598
Mathias Agopian13127d82013-03-05 17:47:11 -0800599// ----------------------------------------------------------------------------
600// local state
601// ----------------------------------------------------------------------------
602
David Sodman41fdfc92017-11-06 16:09:56 -0800603bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800604 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700605 return (s.flags & layer_state_t::eLayerSecure);
606}
607
Mathias Agopian13127d82013-03-05 17:47:11 -0800608// ----------------------------------------------------------------------------
609// transaction
610// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800611
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800612void Layer::pushPendingState() {
613 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700614 return;
615 }
Alec Mourie60041e2019-06-14 18:59:51 -0700616 ATRACE_CALL();
Dan Stoza7dde5992015-05-22 09:51:44 -0700617
Dan Stoza7dde5992015-05-22 09:51:44 -0700618 // If this transaction is waiting on the receipt of a frame, generate a sync
619 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700620 // We don't allow installing sync points after we are removed from the current state
621 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800622 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
623 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800624 if (barrierLayer == nullptr) {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700625 ALOGE("[%s] Unable to promote barrier Layer.", getDebugName());
Dan Stoza7dde5992015-05-22 09:51:44 -0700626 // If we can't promote the layer we are intended to wait on,
627 // then it is expired or otherwise invalid. Allow this transaction
628 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800629 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800630 } else {
chaviw43cb3cb2019-05-31 15:23:41 -0700631 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy, this);
Robert Carr0d480722017-01-10 16:42:54 -0800632 if (barrierLayer->addSyncPoint(syncPoint)) {
Alec Mourie60041e2019-06-14 18:59:51 -0700633 std::stringstream ss;
634 ss << "Adding sync point " << mCurrentState.frameNumber_legacy;
635 ATRACE_NAME(ss.str().c_str());
Dan Stozacac35382016-01-27 12:21:06 -0800636 mRemoteSyncPoints.push_back(std::move(syncPoint));
637 } else {
638 // We already missed the frame we're supposed to synchronize
639 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800640 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800641 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700642 }
643
Dan Stoza7dde5992015-05-22 09:51:44 -0700644 // Wake us up to check if the frame has been received
645 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700646 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700647 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800648 mPendingStates.push_back(mCurrentState);
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700649 ATRACE_INT(mTransactionName.c_str(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700650}
651
Pablo Ceballos05289c22016-04-14 15:49:55 -0700652void Layer::popPendingState(State* stateToCommit) {
Alec Mourie60041e2019-06-14 18:59:51 -0700653 ATRACE_CALL();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800654 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700655
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800656 mPendingStates.removeAt(0);
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700657 ATRACE_INT(mTransactionName.c_str(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700658}
659
Pablo Ceballos05289c22016-04-14 15:49:55 -0700660bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700661 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800662 while (!mPendingStates.empty()) {
663 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700664 if (mRemoteSyncPoints.empty()) {
665 // If we don't have a sync point for this, apply it anyway. It
666 // will be visually wrong, but it should keep us from getting
667 // into too much trouble.
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700668 ALOGE("[%s] No local sync point found", getDebugName());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700669 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700670 stateUpdateAvailable = true;
671 continue;
672 }
673
Marissa Wallf58c14b2018-07-24 10:50:43 -0700674 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800675 mPendingStates[0].frameNumber_legacy) {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700676 ALOGE("[%s] Unexpected sync point frame number found", getDebugName());
Dan Stozacac35382016-01-27 12:21:06 -0800677
678 // Signal our end of the sync point and then dispose of it
679 mRemoteSyncPoints.front()->setTransactionApplied();
680 mRemoteSyncPoints.pop_front();
681 continue;
682 }
683
Dan Stoza7dde5992015-05-22 09:51:44 -0700684 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
Alec Mourie60041e2019-06-14 18:59:51 -0700685 ATRACE_NAME("frameIsAvailable");
Dan Stoza7dde5992015-05-22 09:51:44 -0700686 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700687 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700688 stateUpdateAvailable = true;
689
690 // Signal our end of the sync point and then dispose of it
691 mRemoteSyncPoints.front()->setTransactionApplied();
692 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800693 } else {
Alec Mourie60041e2019-06-14 18:59:51 -0700694 ATRACE_NAME("!frameIsAvailable");
Dan Stoza792e5292016-02-11 11:43:58 -0800695 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700696 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700697 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700698 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700699 stateUpdateAvailable = true;
700 }
701 }
702
703 // If we still have pending updates, wake SurfaceFlinger back up and point
704 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800705 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700706 setTransactionFlags(eTransactionNeeded);
707 mFlinger->setTransactionFlags(eTraversalNeeded);
708 }
709
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800710 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700711 return stateUpdateAvailable;
712}
713
Marissa Wall61c58622018-07-18 10:12:20 -0700714uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000715 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800716
Marissa Wall61c58622018-07-18 10:12:20 -0700717 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
718 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700719
David Sodmaneb085e02017-10-05 18:49:04 -0700720 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700721 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000722 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800723 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
724 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
725 " requested={ wh={%4u,%4u} }}\n"
726 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
727 " requested={ wh={%4u,%4u} }}\n",
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700728 this, getName().c_str(), getBufferTransform(), getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700729 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
730 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
731 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
732 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
733 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700734 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
735 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
736 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800737 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700738
Robert Carre392b552017-09-19 12:16:05 -0700739 // Don't let Layer::doTransaction update the drawing state
740 // if we have a pending resize, unless we are in fixed-size mode.
741 // the drawing state will be updated only once we receive a buffer
742 // with the correct size.
743 //
744 // In particular, we want to make sure the clip (which is part
745 // of the geometry state) is latched together with the size but is
746 // latched immediately when no resizing is involved.
747 //
748 // If a sideband stream is attached, however, we want to skip this
749 // optimization so that transactions aren't missed when a buffer
750 // never arrives
751 //
752 // In the case that we don't have a buffer we ignore other factors
753 // and avoid entering the resizePending state. At a high level the
754 // resizePending state is to avoid applying the state of the new buffer
755 // to the old buffer. However in the state where we don't have an old buffer
756 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700757 const bool resizePending =
758 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
759 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
chaviwd62d3062019-09-04 14:48:02 -0700760 (getBuffer() != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700761 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800762 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700763 flags |= eDontUpdateGeometryState;
764 }
765 }
766
Robert Carr7bf247e2017-05-18 14:02:49 -0700767 // Here we apply various requested geometry states, depending on our
768 // latching configuration. See Layer.h for a detailed discussion of
769 // how geometry latching is controlled.
770 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000771 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700772
Robert Carr7bf247e2017-05-18 14:02:49 -0700773 // There is an awkward asymmetry in the handling of the crop states in the position
774 // states, as can be seen below. Largely this arises from position and transform
775 // being stored in the same data structure while having different latching rules.
776 // b/38182305
777 //
Marissa Wall61c58622018-07-18 10:12:20 -0700778 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700779 // applyPendingStates in the presence of deferred transactions.
chaviw214c89d2019-09-04 16:03:53 -0700780 editCurrentState.active_legacy = editCurrentState.requested_legacy;
781 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Mathias Agopian13127d82013-03-05 17:47:11 -0800782 }
783
Marissa Wall61c58622018-07-18 10:12:20 -0700784 return flags;
785}
786
787uint32_t Layer::doTransaction(uint32_t flags) {
788 ATRACE_CALL();
789
chaviw5aedec92018-10-22 10:40:38 -0700790 if (mLayerDetached) {
Robert Carr321e83c2019-08-19 15:49:30 -0700791 // Ensure BLAST buffer callbacks are processed.
792 // detachChildren and mLayerDetached were implemented to avoid geometry updates
793 // to layers in the cases of animation. For BufferQueue layers buffers are still
794 // consumed as normal. This is useful as otherwise the client could get hung
795 // inevitably waiting on a buffer to return. We recreate this semantic for BufferQueue
796 // even though it is a little consistent. detachChildren is shortly slated for removal
797 // by the hierarchy mirroring work so we don't need to worry about it too much.
798 mDrawingState.callbackHandles = mCurrentState.callbackHandles;
799 mCurrentState.callbackHandles = {};
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800800 return flags;
801 }
802
803 if (mChildrenChanged) {
804 flags |= eVisibleRegion;
805 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -0700806 }
807
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800808 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +0000809 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -0700810 if (!applyPendingStates(&c)) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800811 return flags;
Marissa Wall61c58622018-07-18 10:12:20 -0700812 }
813
814 flags = doTransactionResize(flags, &c);
815
Alec Mourib416efd2018-09-06 21:01:59 +0000816 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700817
818 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800819 // invalidate and recompute the visible regions if needed
820 flags |= Layer::eVisibleRegion;
821 }
822
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700823 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800824 // invalidate and recompute the visible regions if needed
825 flags |= eVisibleRegion;
826 this->contentDirty = true;
827
828 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -0700829 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -0700830 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -0800831 }
832
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800833 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -0700834 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800835 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -0700836 }
837
Mathias Agopian13127d82013-03-05 17:47:11 -0800838 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -0700839 commitTransaction(c);
Vishnu Nair8406fd72019-07-30 11:29:31 -0700840 mPendingStatesSnapshot = mPendingStates;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800841 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -0800842 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800843}
844
Pablo Ceballos05289c22016-04-14 15:49:55 -0700845void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800846 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700847}
848
Mathias Agopian13127d82013-03-05 17:47:11 -0800849uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800850 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800851}
852
853uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800854 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -0800855}
856
chaviw214c89d2019-09-04 16:03:53 -0700857bool Layer::setPosition(float x, float y) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800858 if (mCurrentState.requested_legacy.transform.tx() == x &&
859 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -0800860 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800861 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -0700862
863 // We update the requested and active position simultaneously because
864 // we want to apply the position portion of the transform matrix immediately,
865 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800866 mCurrentState.requested_legacy.transform.set(x, y);
chaviw214c89d2019-09-04 16:03:53 -0700867 // Here we directly update the active state
868 // unlike other setters, because we store it within
869 // the transform, but use different latching rules.
870 // b/38182305
871 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr69663fb2016-03-27 19:59:19 -0700872
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800873 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800874 setTransactionFlags(eTransactionNeeded);
875 return true;
876}
Robert Carr82364e32016-05-15 11:27:47 -0700877
Robert Carr1f0a16a2016-10-24 16:27:39 -0700878bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
879 ssize_t idx = mCurrentChildren.indexOf(childLayer);
880 if (idx < 0) {
881 return false;
882 }
883 if (childLayer->setLayer(z)) {
884 mCurrentChildren.removeAt(idx);
885 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800886 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700887 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800888 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700889}
890
Robert Carr503c7042017-09-27 15:06:08 -0700891bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
892 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
893 ssize_t idx = mCurrentChildren.indexOf(childLayer);
894 if (idx < 0) {
895 return false;
896 }
897 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
898 mCurrentChildren.removeAt(idx);
899 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800900 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700901 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800902 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700903}
904
Robert Carrae060832016-11-28 10:51:00 -0800905bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800906 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
907 mCurrentState.sequence++;
908 mCurrentState.z = z;
909 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700910
911 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800912 if (mCurrentState.zOrderRelativeOf != nullptr) {
913 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700914 if (strongRelative != nullptr) {
915 strongRelative->removeZOrderRelative(this);
916 }
chaviw606e5cf2019-03-01 10:12:10 -0800917 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -0700918 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800919 setTransactionFlags(eTransactionNeeded);
920 return true;
921}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700922
Robert Carrdb66e622017-04-10 16:55:57 -0700923void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800924 mCurrentState.zOrderRelatives.remove(relative);
925 mCurrentState.sequence++;
926 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700927 setTransactionFlags(eTransactionNeeded);
928}
929
930void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800931 mCurrentState.zOrderRelatives.add(relative);
932 mCurrentState.modified = true;
933 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -0700934 setTransactionFlags(eTransactionNeeded);
935}
936
chaviw606e5cf2019-03-01 10:12:10 -0800937void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
938 mCurrentState.zOrderRelativeOf = relativeOf;
939 mCurrentState.sequence++;
940 mCurrentState.modified = true;
chaviwbdb8b802019-10-14 09:17:12 -0700941 mCurrentState.isRelativeOf = relativeOf != nullptr;
942
chaviw606e5cf2019-03-01 10:12:10 -0800943 setTransactionFlags(eTransactionNeeded);
944}
945
Robert Carr503d2bd2017-12-04 15:49:47 -0800946bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -0700947 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
948 if (handle == nullptr) {
949 return false;
950 }
951 sp<Layer> relative = handle->owner.promote();
952 if (relative == nullptr) {
953 return false;
954 }
955
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800956 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
957 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -0800958 return false;
959 }
960
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800961 mCurrentState.sequence++;
962 mCurrentState.modified = true;
963 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -0700964
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800965 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -0700966 if (oldZOrderRelativeOf != nullptr) {
967 oldZOrderRelativeOf->removeZOrderRelative(this);
968 }
chaviw606e5cf2019-03-01 10:12:10 -0800969 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -0700970 relative->addZOrderRelative(this);
971
972 setTransactionFlags(eTransactionNeeded);
973
974 return true;
975}
976
Mathias Agopian13127d82013-03-05 17:47:11 -0800977bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800978 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -0700979 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800980 mCurrentState.requested_legacy.w = w;
981 mCurrentState.requested_legacy.h = h;
982 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800983 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -0700984
985 // record the new size, from this point on, when the client request
986 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800987 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -0800988 return true;
989}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800990bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800991 if (mCurrentState.color.a == alpha) return false;
992 mCurrentState.sequence++;
993 mCurrentState.color.a = alpha;
994 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800995 setTransactionFlags(eTransactionNeeded);
996 return true;
997}
chaviw13fdc492017-06-27 12:40:18 -0700998
Valerie Haudd0b7572019-01-29 14:59:27 -0800999bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1000 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001001 return false;
Valerie Hauaa194562019-02-05 16:21:38 -08001002 }
1003 mCurrentState.sequence++;
1004 mCurrentState.modified = true;
1005 setTransactionFlags(eTransactionNeeded);
1006
1007 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001008 // create background color layer if one does not yet exist
1009 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001010 std::string name = mName + "BackgroundColorLayer";
Lloyd Pique958be492019-03-28 15:34:59 -07001011 mCurrentState.bgColorLayer = mFlinger->getFactory().createColorLayer(
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001012 LayerCreationArgs(mFlinger.get(), nullptr, std::move(name), 0, 0, flags,
1013 LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -07001014
Valerie Haudd0b7572019-01-29 14:59:27 -08001015 // add to child list
1016 addChild(mCurrentState.bgColorLayer);
1017 mFlinger->mLayersAdded = true;
1018 // set up SF to handle added color layer
1019 if (isRemovedFromCurrentState()) {
1020 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1021 }
1022 mFlinger->setTransactionFlags(eTransactionNeeded);
1023 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1024 mCurrentState.bgColorLayer->reparent(nullptr);
1025 mCurrentState.bgColorLayer = nullptr;
1026 return true;
1027 }
1028
1029 mCurrentState.bgColorLayer->setColor(color);
1030 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1031 mCurrentState.bgColorLayer->setAlpha(alpha);
1032 mCurrentState.bgColorLayer->setDataspace(dataspace);
1033
chaviw13fdc492017-06-27 12:40:18 -07001034 return true;
1035}
1036
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001037bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001038 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001039
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001040 mCurrentState.sequence++;
1041 mCurrentState.cornerRadius = cornerRadius;
1042 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001043 setTransactionFlags(eTransactionNeeded);
1044 return true;
1045}
1046
Robert Carrd4ae7f32018-06-07 16:10:57 -07001047bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1048 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001049 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001050 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1051
1052 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1053 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1054 return false;
1055 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001056 mCurrentState.sequence++;
1057 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1058 matrix.dsdy);
1059 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001060 setTransactionFlags(eTransactionNeeded);
1061 return true;
1062}
Marissa Wall61c58622018-07-18 10:12:20 -07001063
Mathias Agopian13127d82013-03-05 17:47:11 -08001064bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001065 mCurrentState.requestedTransparentRegion_legacy = transparent;
1066 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001067 setTransactionFlags(eTransactionNeeded);
1068 return true;
1069}
1070bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001071 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1072 if (mCurrentState.flags == newFlags) return false;
1073 mCurrentState.sequence++;
1074 mCurrentState.flags = newFlags;
1075 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001076 setTransactionFlags(eTransactionNeeded);
1077 return true;
1078}
Robert Carr99e27f02016-06-16 15:18:02 -07001079
chaviw214c89d2019-09-04 16:03:53 -07001080bool Layer::setCrop_legacy(const Rect& crop) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001081 if (mCurrentState.requestedCrop_legacy == crop) return false;
1082 mCurrentState.sequence++;
1083 mCurrentState.requestedCrop_legacy = crop;
chaviw214c89d2019-09-04 16:03:53 -07001084 mCurrentState.crop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001085
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001086 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001087 setTransactionFlags(eTransactionNeeded);
1088 return true;
1089}
Robert Carr8d5227b2017-03-16 15:41:03 -07001090
Robert Carrc3574f72016-03-24 12:19:32 -07001091bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001092 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001093 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001094 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001095 return true;
1096}
1097
Evan Roskyef876c92019-01-25 17:46:06 -08001098bool Layer::setMetadata(const LayerMetadata& data) {
1099 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001100 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001101 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001102 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001103 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001104}
1105
Mathias Agopian13127d82013-03-05 17:47:11 -08001106bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001107 if (mCurrentState.layerStack == layerStack) return false;
1108 mCurrentState.sequence++;
1109 mCurrentState.layerStack = layerStack;
1110 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001111 setTransactionFlags(eTransactionNeeded);
1112 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001113}
1114
Peiyong Linc502cb72019-03-01 15:00:23 -08001115bool Layer::setColorSpaceAgnostic(const bool agnostic) {
1116 if (mCurrentState.colorSpaceAgnostic == agnostic) {
1117 return false;
1118 }
1119 mCurrentState.sequence++;
1120 mCurrentState.colorSpaceAgnostic = agnostic;
1121 mCurrentState.modified = true;
1122 setTransactionFlags(eTransactionNeeded);
1123 return true;
1124}
1125
Robert Carr1f0a16a2016-10-24 16:27:39 -07001126uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001127 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001128 if (p == nullptr) {
1129 return getDrawingState().layerStack;
1130 }
1131 return p->getLayerStack();
1132}
1133
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001134bool Layer::setShadowRadius(float shadowRadius) {
1135 if (mCurrentState.shadowRadius == shadowRadius) {
1136 return false;
1137 }
1138
1139 mCurrentState.sequence++;
1140 mCurrentState.shadowRadius = shadowRadius;
1141 mCurrentState.modified = true;
1142 setTransactionFlags(eTransactionNeeded);
1143 return true;
1144}
1145
Marissa Wallf58c14b2018-07-24 10:50:43 -07001146void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Alec Mourie60041e2019-06-14 18:59:51 -07001147 ATRACE_CALL();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001148 mCurrentState.barrierLayer_legacy = barrierLayer;
1149 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001150 // We don't set eTransactionNeeded, because just receiving a deferral
1151 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001152 mCurrentState.modified = true;
1153 pushPendingState();
1154 mCurrentState.barrierLayer_legacy = nullptr;
1155 mCurrentState.frameNumber_legacy = 0;
1156 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001157}
1158
Marissa Wallf58c14b2018-07-24 10:50:43 -07001159void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001160 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001161 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001162}
1163
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001164// ----------------------------------------------------------------------------
1165// pageflip handling...
1166// ----------------------------------------------------------------------------
1167
Robert Carr1f0a16a2016-10-24 16:27:39 -07001168bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001169 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001170 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001171 if (parent != nullptr && parent->isHiddenByPolicy()) {
1172 return true;
1173 }
Robert Carr1c5481e2019-07-01 14:42:27 -07001174 if (usingRelativeZ(LayerVector::StateSet::Drawing)) {
1175 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
1176 if (zOrderRelativeOf != nullptr) {
1177 if (zOrderRelativeOf->isHiddenByPolicy()) {
1178 return true;
1179 }
1180 }
1181 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001182 return s.flags & layer_state_t::eLayerHidden;
1183}
1184
David Sodman41fdfc92017-11-06 16:09:56 -08001185uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001186 // TODO: should we do something special if mSecure is set?
1187 if (mProtectedByApp) {
1188 // need a hardware-protected path to external video sink
1189 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001190 }
Riley Andrews03414a12014-07-01 14:22:59 -07001191 if (mPotentialCursor) {
1192 usage |= GraphicBuffer::USAGE_CURSOR;
1193 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001194 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001195 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001196}
1197
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001198void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001199 uint32_t orientation = 0;
Vishnu Nair5eb3f062019-04-08 08:21:03 -07001200 // Disable setting transform hint if the debug flag is set.
1201 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001202 // The transform hint is used to improve performance, but we can
1203 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001204 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001205 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001206 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001207 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001208 orientation = 0;
1209 }
1210 }
David Sodmaneb085e02017-10-05 18:49:04 -07001211 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001212}
1213
Mathias Agopian13127d82013-03-05 17:47:11 -08001214// ----------------------------------------------------------------------------
1215// debugging
1216// ----------------------------------------------------------------------------
1217
Marissa Wall61c58622018-07-18 10:12:20 -07001218// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001219LayerDebugInfo Layer::getLayerDebugInfo() const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001220 using namespace std::string_literals;
1221
Kalle Raitaa099a242017-01-11 11:17:29 -08001222 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001223 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001224 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001225 sp<Layer> parent = getParent();
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001226 info.mParentName = parent ? parent->getName() : "none"s;
chaviw8a01fa42019-08-19 12:39:31 -07001227 info.mType = getType();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001228 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Lloyd Piquea2468662019-03-07 21:31:06 -08001229
1230 info.mVisibleRegion = debugGetVisibleRegionOnDefaultDisplay();
Kalle Raitaa099a242017-01-11 11:17:29 -08001231 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001232 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001233 info.mX = ds.active_legacy.transform.tx();
1234 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001235 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001236 info.mWidth = ds.active_legacy.w;
1237 info.mHeight = ds.active_legacy.h;
1238 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001239 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001240 info.mFlags = ds.flags;
1241 info.mPixelFormat = getPixelFormat();
chaviw4244e032019-09-04 11:27:49 -07001242 info.mDataSpace = static_cast<android_dataspace>(getDataSpace());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001243 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1244 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1245 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1246 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001247 {
chaviwd62d3062019-09-04 14:48:02 -07001248 sp<const GraphicBuffer> buffer = getBuffer();
David Sodman5b4cffc2017-11-23 13:20:29 -08001249 if (buffer != 0) {
1250 info.mActiveBufferWidth = buffer->getWidth();
1251 info.mActiveBufferHeight = buffer->getHeight();
1252 info.mActiveBufferStride = buffer->getStride();
1253 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001254 } else {
1255 info.mActiveBufferWidth = 0;
1256 info.mActiveBufferHeight = 0;
1257 info.mActiveBufferStride = 0;
1258 info.mActiveBufferFormat = 0;
1259 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001260 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001261 info.mNumQueuedFrames = getQueuedFrameCount();
1262 info.mRefreshPending = isBufferLatched();
1263 info.mIsOpaque = isOpaque(ds);
1264 info.mContentDirty = contentDirty;
1265 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001266}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001267
Yiwei Zhang5434a782018-12-05 18:06:32 -08001268void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001269 result.append("-------------------------------");
1270 result.append("-------------------------------");
1271 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001272 result.append(" Layer name\n");
1273 result.append(" Z | ");
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001274 result.append(" Window Type | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001275 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001276 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001277 result.append(" Disp Frame (LTRB) | ");
1278 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001279 result.append("-------------------------------");
1280 result.append("-------------------------------");
1281 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001282}
1283
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001284void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1285 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1286 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001287 return;
1288 }
1289
Yiwei Zhang5434a782018-12-05 18:06:32 -08001290 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001291 if (mName.length() > 77) {
1292 std::string shortened;
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001293 shortened.append(mName, 0, 36);
Dan Stozae22aec72016-08-01 13:20:59 -07001294 shortened.append("[...]");
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001295 shortened.append(mName, mName.length() - 36);
1296 name = std::move(shortened);
Dan Stozae22aec72016-08-01 13:20:59 -07001297 } else {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001298 name = mName;
Dan Stozae22aec72016-08-01 13:20:59 -07001299 }
1300
Yiwei Zhang5434a782018-12-05 18:06:32 -08001301 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001302
Alec Mourib416efd2018-09-06 21:01:59 +00001303 const State& layerState(getDrawingState());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001304 const auto& compositionState = outputLayer->getState();
1305
Chia-I Wu1e043612018-03-01 09:45:09 -08001306 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001307 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001308 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001309 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001310 }
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001311 StringAppendF(&result, " %10d | ", mWindowType);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001312 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
1313 StringAppendF(&result, "%10s | ",
1314 toString(getCompositionLayer() ? compositionState.bufferTransform
1315 : static_cast<Hwc2::Transform>(0))
1316 .c_str());
1317 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001318 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001319 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001320 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1321 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001322
Yichi Chen6ca35192018-05-29 12:20:43 +08001323 result.append("- - - - - - - - - - - - - - - -");
1324 result.append("- - - - - - - - - - - - - - - -");
1325 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001326}
Dan Stozae22aec72016-08-01 13:20:59 -07001327
Yiwei Zhang5434a782018-12-05 18:06:32 -08001328void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001329 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001330}
1331
Svetoslavd85084b2014-03-20 10:28:31 -07001332void Layer::clearFrameStats() {
1333 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001334}
1335
Jamie Gennis6547ff42013-07-16 20:12:42 -07001336void Layer::logFrameStats() {
1337 mFrameTracker.logAndResetStats(mName);
1338}
1339
Svetoslavd85084b2014-03-20 10:28:31 -07001340void Layer::getFrameStats(FrameStats* outStats) const {
1341 mFrameTracker.getStats(outStats);
1342}
1343
Yiwei Zhang5434a782018-12-05 18:06:32 -08001344void Layer::dumpFrameEvents(std::string& result) {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001345 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().c_str(), getType(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001346 Mutex::Autolock lock(mFrameEventHistoryMutex);
1347 mFrameEventHistory.checkFencesForCompletion();
1348 mFrameEventHistory.dump(result);
1349}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001350
Vishnu Nair0f085c62019-08-30 08:49:12 -07001351void Layer::dumpCallingUidPid(std::string& result) const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001352 StringAppendF(&result, "Layer %s (%s) pid:%d uid:%d\n", getName().c_str(), getType(),
Vishnu Nair0f085c62019-08-30 08:49:12 -07001353 mCallingPid, mCallingUid);
1354}
1355
Brian Anderson5ea5e592016-12-01 16:54:33 -08001356void Layer::onDisconnect() {
1357 Mutex::Autolock lock(mFrameEventHistoryMutex);
1358 mFrameEventHistory.onDisconnect();
Yiwei Zhang1a88c402019-11-18 10:43:58 -08001359 const int32_t layerId = getSequence();
1360 mFlinger->mTimeStats->onDestroy(layerId);
1361 mFlinger->mFrameTracer->onDestroy(layerId);
Brian Anderson5ea5e592016-12-01 16:54:33 -08001362}
1363
Brian Anderson3890c392016-07-25 12:48:08 -07001364void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001365 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001366 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001367 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1368 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001369 }
1370
Brian Andersond6927fb2016-07-23 23:37:30 -07001371 Mutex::Autolock lock(mFrameEventHistoryMutex);
1372 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001373 // If there are any unsignaled fences in the aquire timeline at this
1374 // point, the previously queued frame hasn't been latched yet. Go ahead
1375 // and try to get the signal time here so the syscall is taken out of
1376 // the main thread's critical path.
1377 mAcquireTimeline.updateSignalTimes();
1378 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001379 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001380 mFrameEventHistory.addQueue(*newTimestamps);
1381 }
1382
Brian Anderson3890c392016-07-25 12:48:08 -07001383 if (outDelta) {
1384 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001385 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001386}
Dan Stozae77c7662016-05-13 11:37:28 -07001387
Chia-I Wu98f1c102017-05-30 14:54:08 -07001388size_t Layer::getChildrenCount() const {
1389 size_t count = 0;
1390 for (const sp<Layer>& child : mCurrentChildren) {
1391 count += 1 + child->getChildrenCount();
1392 }
1393 return count;
1394}
1395
Robert Carr1f0a16a2016-10-24 16:27:39 -07001396void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001397 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001398 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001399
Robert Carr1f0a16a2016-10-24 16:27:39 -07001400 mCurrentChildren.add(layer);
1401 layer->setParent(this);
1402}
1403
1404ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001405 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001406 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001407
Robert Carr1323c952019-01-28 18:13:27 -08001408 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001409 return mCurrentChildren.remove(layer);
1410}
1411
Robert Carr1db73f62016-12-21 12:58:51 -08001412bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1413 sp<Handle> handle = nullptr;
1414 sp<Layer> newParent = nullptr;
1415 if (newParentHandle == nullptr) {
1416 return false;
1417 }
1418 handle = static_cast<Handle*>(newParentHandle.get());
1419 newParent = handle->owner.promote();
1420 if (newParent == nullptr) {
1421 ALOGE("Unable to promote Layer handle");
1422 return false;
1423 }
1424
chaviw5aedec92018-10-22 10:40:38 -07001425 if (attachChildren()) {
1426 setTransactionFlags(eTransactionNeeded);
1427 }
Robert Carr1db73f62016-12-21 12:58:51 -08001428 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001429 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001430 }
1431 mCurrentChildren.clear();
1432
1433 return true;
1434}
1435
Robert Carr15eae092018-03-23 13:43:53 -07001436void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001437 for (const sp<Layer>& child : mDrawingChildren) {
1438 child->mDrawingParent = newParent;
Vishnu Nairc652ff82019-03-15 12:48:54 -07001439 child->computeBounds(newParent->mBounds,
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001440 newParent->getTransformWithScale(newParent->getBufferScaleTransform()),
1441 newParent->mEffectiveShadowRadius);
Robert Carr578038f2018-03-09 12:25:24 -08001442 }
1443}
1444
chaviwf1961f72017-09-18 16:41:07 -07001445bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001446 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001447
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001448 // While layers are detached, we allow most operations
1449 // and simply halt performing the actual transaction. However
1450 // for reparent != null we would enter the mRemovedFromCurrentState
1451 // state, regardless of whether doTransaction was called, and
1452 // so we need to prevent the update here.
1453 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001454 return false;
1455 }
1456
Robert Carr54cf5b12019-01-25 14:02:28 -08001457 sp<Layer> newParent;
1458 if (newParentHandle != nullptr) {
1459 auto handle = static_cast<Handle*>(newParentHandle.get());
1460 newParent = handle->owner.promote();
1461 if (newParent == nullptr) {
1462 ALOGE("Unable to promote Layer handle");
1463 return false;
1464 }
1465 if (newParent == this) {
1466 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1467 return false;
1468 }
1469 }
1470
chaviwf1961f72017-09-18 16:41:07 -07001471 sp<Layer> parent = getParent();
1472 if (parent != nullptr) {
1473 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001474 }
1475
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001476 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001477 newParent->addChild(this);
1478 if (!newParent->isRemovedFromCurrentState()) {
1479 addToCurrentState();
1480 } else {
1481 onRemovedFromCurrentState();
1482 }
1483
1484 if (mLayerDetached) {
1485 mLayerDetached = false;
1486 callSetTransactionFlags = true;
1487 }
1488 } else {
1489 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001490 }
1491
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001492 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001493 setTransactionFlags(eTransactionNeeded);
1494 }
chaviw06178942017-07-27 10:25:59 -07001495 return true;
1496}
1497
Robert Carr9524cb32017-02-13 11:32:32 -08001498bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001499 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001500 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001501 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001502 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001503 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001504 child->detachChildren();
chaviw43cb3cb2019-05-31 15:23:41 -07001505 child->removeRemoteSyncPoints();
Robert Carr9524cb32017-02-13 11:32:32 -08001506 }
Robert Carr7f619b22017-11-06 12:56:35 -08001507 }
Robert Carr9524cb32017-02-13 11:32:32 -08001508
1509 return true;
1510}
1511
chaviw5aedec92018-10-22 10:40:38 -07001512bool Layer::attachChildren() {
1513 bool changed = false;
1514 for (const sp<Layer>& child : mCurrentChildren) {
1515 sp<Client> parentClient = mClientRef.promote();
1516 sp<Client> client(child->mClientRef.promote());
1517 if (client != nullptr && parentClient != client) {
1518 if (child->mLayerDetached) {
1519 child->mLayerDetached = false;
1520 changed = true;
1521 }
1522 changed |= child->attachChildren();
1523 }
1524 }
1525
1526 return changed;
1527}
1528
Peiyong Lind3788632018-09-18 16:01:31 -07001529bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001530 static const mat4 identityMatrix = mat4();
1531
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001532 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001533 return false;
1534 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001535 ++mCurrentState.sequence;
1536 mCurrentState.colorTransform = matrix;
1537 mCurrentState.hasColorTransform = matrix != identityMatrix;
1538 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001539 setTransactionFlags(eTransactionNeeded);
1540 return true;
1541}
1542
chaviwf66724d2018-11-28 16:35:21 -08001543mat4 Layer::getColorTransform() const {
1544 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1545 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1546 colorTransform = parent->getColorTransform() * colorTransform;
1547 }
1548 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001549}
1550
1551bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001552 bool hasColorTransform = getDrawingState().hasColorTransform;
1553 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1554 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1555 }
1556 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001557}
1558
Chia-I Wu11481472018-05-04 10:43:19 -07001559bool Layer::isLegacyDataSpace() const {
1560 // return true when no higher bits are set
chaviw4244e032019-09-04 11:27:49 -07001561 return !(getDataSpace() &
1562 (ui::Dataspace::STANDARD_MASK | ui::Dataspace::TRANSFER_MASK |
1563 ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001564}
1565
Robert Carr1f0a16a2016-10-24 16:27:39 -07001566void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001567 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001568}
1569
chaviw301b1d82019-11-06 13:15:09 -08001570int32_t Layer::getZ(LayerVector::StateSet stateSet) const {
1571 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1572 const State& state = useDrawing ? mDrawingState : mCurrentState;
1573 return state.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001574}
1575
Robert Carr1c5481e2019-07-01 14:42:27 -07001576bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) const {
Robert Carr29abff82017-12-04 13:51:20 -08001577 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001578 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviwe5ac40f2019-09-24 16:36:55 -07001579 return state.isRelativeOf;
Robert Carr29abff82017-12-04 13:51:20 -08001580}
1581
David Sodman41fdfc92017-11-06 16:09:56 -08001582__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001583 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001584 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1585 "makeTraversalList received invalid stateSet");
1586 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1587 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001588 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001589
Robert Carr29abff82017-12-04 13:51:20 -08001590 if (state.zOrderRelatives.size() == 0) {
1591 *outSkipRelativeZUsers = true;
1592 return children;
1593 }
1594
chaviwfd462612018-05-31 16:11:27 -07001595 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001596 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001597 sp<Layer> strongRelative = weakRelative.promote();
1598 if (strongRelative != nullptr) {
1599 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001600 }
1601 }
1602
Dan Stoza412903f2017-04-27 13:42:17 -07001603 for (const sp<Layer>& child : children) {
chaviwe5ac40f2019-09-24 16:36:55 -07001604 if (child->usingRelativeZ(stateSet)) {
Robert Carr503c7042017-09-27 15:06:08 -07001605 continue;
1606 }
Robert Carrdb66e622017-04-10 16:55:57 -07001607 traverse.add(child);
1608 }
1609
1610 return traverse;
1611}
1612
Robert Carr1f0a16a2016-10-24 16:27:39 -07001613/**
Robert Carrdb66e622017-04-10 16:55:57 -07001614 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001615 */
Dan Stoza412903f2017-04-27 13:42:17 -07001616void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001617 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1618 // produce a new list for traversing, including our relatives, and not including our children
1619 // who are relatives of another surface. In the case that there are no relative Z,
1620 // makeTraversalList returns our children directly to avoid significant overhead.
1621 // However in this case we need to take the responsibility for filtering children which
1622 // are relatives of another surface here.
1623 bool skipRelativeZUsers = false;
1624 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001625
Robert Carr1f0a16a2016-10-24 16:27:39 -07001626 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001627 for (; i < list.size(); i++) {
1628 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001629 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1630 continue;
1631 }
1632
chaviw301b1d82019-11-06 13:15:09 -08001633 if (relative->getZ(stateSet) >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001634 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001635 }
Dan Stoza412903f2017-04-27 13:42:17 -07001636 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001637 }
Robert Carr29abff82017-12-04 13:51:20 -08001638
Dan Stoza412903f2017-04-27 13:42:17 -07001639 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001640 for (; i < list.size(); i++) {
1641 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001642
Robert Carr29abff82017-12-04 13:51:20 -08001643 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1644 continue;
1645 }
Dan Stoza412903f2017-04-27 13:42:17 -07001646 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001647 }
1648}
1649
1650/**
Robert Carrdb66e622017-04-10 16:55:57 -07001651 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001652 */
Dan Stoza412903f2017-04-27 13:42:17 -07001653void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1654 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001655 // See traverseInZOrder for documentation.
1656 bool skipRelativeZUsers = false;
1657 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001658
Robert Carr1f0a16a2016-10-24 16:27:39 -07001659 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001660 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001661 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001662
1663 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1664 continue;
1665 }
1666
chaviw301b1d82019-11-06 13:15:09 -08001667 if (relative->getZ(stateSet) < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001668 break;
1669 }
Dan Stoza412903f2017-04-27 13:42:17 -07001670 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001671 }
Dan Stoza412903f2017-04-27 13:42:17 -07001672 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001673 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001674 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001675
1676 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1677 continue;
1678 }
1679
Dan Stoza412903f2017-04-27 13:42:17 -07001680 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001681 }
1682}
1683
chaviw4b129c22018-04-09 16:19:43 -07001684LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1685 const std::vector<Layer*>& layersInTree) {
1686 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1687 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001688 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1689 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001690 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001691
chaviwfd462612018-05-31 16:11:27 -07001692 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001693 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1694 sp<Layer> strongRelative = weakRelative.promote();
1695 // Only add relative layers that are also descendents of the top most parent of the tree.
1696 // If a relative layer is not a descendent, then it should be ignored.
1697 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1698 traverse.add(strongRelative);
1699 }
1700 }
1701
1702 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001703 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001704 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1705 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1706 // the child here since it won't be added later as a relative.
1707 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1708 childState.zOrderRelativeOf.promote().get())) {
1709 continue;
1710 }
1711 traverse.add(child);
1712 }
1713
1714 return traverse;
1715}
1716
1717void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1718 LayerVector::StateSet stateSet,
1719 const LayerVector::Visitor& visitor) {
1720 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001721
1722 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001723 for (; i < list.size(); i++) {
1724 const auto& relative = list[i];
chaviw301b1d82019-11-06 13:15:09 -08001725 if (relative->getZ(stateSet) >= 0) {
chaviwa76b2712017-09-20 12:02:26 -07001726 break;
1727 }
chaviw4b129c22018-04-09 16:19:43 -07001728 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001729 }
chaviw4b129c22018-04-09 16:19:43 -07001730
chaviwa76b2712017-09-20 12:02:26 -07001731 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001732 for (; i < list.size(); i++) {
1733 const auto& relative = list[i];
1734 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001735 }
1736}
1737
chaviw4b129c22018-04-09 16:19:43 -07001738std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1739 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1740 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1741
1742 std::vector<Layer*> layersInTree = {this};
1743 for (size_t i = 0; i < children.size(); i++) {
1744 const auto& child = children[i];
1745 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1746 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1747 }
1748
1749 return layersInTree;
1750}
1751
1752void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1753 const LayerVector::Visitor& visitor) {
1754 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1755 std::sort(layersInTree.begin(), layersInTree.end());
1756 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1757}
1758
Peiyong Linefefaac2018-08-17 12:27:51 -07001759ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001760 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001761}
1762
chaviw13fdc492017-06-27 12:40:18 -07001763half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001764 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001765
chaviw13fdc492017-06-27 12:40:18 -07001766 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1767 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001768}
Robert Carr6452f122017-03-21 10:41:29 -07001769
chaviw13fdc492017-06-27 12:40:18 -07001770half4 Layer::getColor() const {
1771 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001772 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001773}
Robert Carr6452f122017-03-21 10:41:29 -07001774
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001775Layer::RoundedCornerState Layer::getRoundedCornerState() const {
1776 const auto& p = mDrawingParent.promote();
1777 if (p != nullptr) {
Peiyong Lin27016a92019-03-29 17:36:08 +00001778 RoundedCornerState parentState = p->getRoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001779 if (parentState.radius > 0) {
1780 ui::Transform t = getActiveTransform(getDrawingState());
1781 t = t.inverse();
1782 parentState.cropRect = t.transform(parentState.cropRect);
1783 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1784 // but a transform matrix can define horizontal and vertical scales.
1785 // Let's take the average between both of them and pass into the shader, practically we
1786 // never do this type of transformation on windows anyway.
1787 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
1788 return parentState;
1789 }
1790 }
1791 const float radius = getDrawingState().cornerRadius;
Peiyong Linb9ff23e2019-04-08 11:04:59 -07001792 return radius > 0 && getCrop(getDrawingState()).isValid()
1793 ? RoundedCornerState(getCrop(getDrawingState()).toFloatRect(), radius)
1794 : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001795}
1796
Robert Carr1f0a16a2016-10-24 16:27:39 -07001797void Layer::commitChildList() {
1798 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1799 const auto& child = mCurrentChildren[i];
1800 child->commitChildList();
1801 }
1802 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001803 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001804}
1805
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001806static wp<Layer> extractLayerFromBinder(const wp<IBinder>& weakBinderHandle) {
1807 if (weakBinderHandle == nullptr) {
1808 return nullptr;
1809 }
1810 sp<IBinder> binderHandle = weakBinderHandle.promote();
1811 if (binderHandle == nullptr) {
1812 return nullptr;
1813 }
1814 sp<Layer::Handle> handle = static_cast<Layer::Handle*>(binderHandle.get());
1815 if (handle == nullptr) {
1816 return nullptr;
1817 }
1818 return handle->owner;
1819}
1820
Robert Carr720e5062018-07-30 17:45:14 -07001821void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001822 mCurrentState.inputInfo = info;
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001823 mCurrentState.touchableRegionCrop = extractLayerFromBinder(info.touchableRegionCropHandle);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001824 mCurrentState.modified = true;
1825 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07001826 setTransactionFlags(eTransactionNeeded);
1827}
1828
Vishnu Nair8406fd72019-07-30 11:29:31 -07001829void Layer::writeToProtoDrawingState(LayerProto* layerInfo, uint32_t traceFlags) const {
1830 ui::Transform transform = getTransform();
1831
1832 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
1833 for (const auto& pendingState : mPendingStatesSnapshot) {
1834 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
1835 if (barrierLayer != nullptr) {
1836 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
1837 barrierLayerProto->set_id(barrierLayer->sequence);
1838 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
1839 }
1840 }
1841
chaviwd62d3062019-09-04 14:48:02 -07001842 auto buffer = getBuffer();
Vishnu Nair8406fd72019-07-30 11:29:31 -07001843 if (buffer != nullptr) {
1844 LayerProtoHelper::writeToProto(buffer,
1845 [&]() { return layerInfo->mutable_active_buffer(); });
chaviw4244e032019-09-04 11:27:49 -07001846 LayerProtoHelper::writeToProto(ui::Transform(getBufferTransform()),
Vishnu Nair8406fd72019-07-30 11:29:31 -07001847 layerInfo->mutable_buffer_transform());
1848 }
1849 layerInfo->set_invalidate(contentDirty);
1850 layerInfo->set_is_protected(isProtected());
chaviw4244e032019-09-04 11:27:49 -07001851 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(getDataSpace())));
Vishnu Nair8406fd72019-07-30 11:29:31 -07001852 layerInfo->set_queued_frames(getQueuedFrameCount());
1853 layerInfo->set_refresh_pending(isBufferLatched());
1854 layerInfo->set_curr_frame(mCurrentFrameNumber);
1855 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
1856
1857 layerInfo->set_corner_radius(getRoundedCornerState().radius);
1858 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1859 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
1860 [&]() { return layerInfo->mutable_position(); });
1861 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
Lloyd Piquea2468662019-03-07 21:31:06 -08001862 LayerProtoHelper::writeToProto(debugGetVisibleRegionOnDefaultDisplay(),
Vishnu Nair8406fd72019-07-30 11:29:31 -07001863 [&]() { return layerInfo->mutable_visible_region(); });
1864 LayerProtoHelper::writeToProto(surfaceDamageRegion,
1865 [&]() { return layerInfo->mutable_damage_region(); });
1866 }
1867
1868 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
1869 LayerProtoHelper::writeToProto(mSourceBounds,
1870 [&]() { return layerInfo->mutable_source_bounds(); });
1871 LayerProtoHelper::writeToProto(mScreenBounds,
1872 [&]() { return layerInfo->mutable_screen_bounds(); });
1873 }
1874}
1875
1876void Layer::writeToProtoCommonState(LayerProto* layerInfo, LayerVector::StateSet stateSet,
1877 uint32_t traceFlags) const {
chaviw1d044282017-09-27 12:19:28 -07001878 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1879 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001880 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07001881
Peiyong Linefefaac2018-08-17 12:27:51 -07001882 ui::Transform requestedTransform = state.active_legacy.transform;
chaviw1d044282017-09-27 12:19:28 -07001883
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001884 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
1885 layerInfo->set_id(sequence);
1886 layerInfo->set_name(getName().c_str());
chaviw8a01fa42019-08-19 12:39:31 -07001887 layerInfo->set_type(getType());
chaviw1d044282017-09-27 12:19:28 -07001888
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001889 for (const auto& child : children) {
1890 layerInfo->add_children(child->sequence);
chaviw1d044282017-09-27 12:19:28 -07001891 }
chaviw1d044282017-09-27 12:19:28 -07001892
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001893 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1894 sp<Layer> strongRelative = weakRelative.promote();
1895 if (strongRelative != nullptr) {
1896 layerInfo->add_relatives(strongRelative->sequence);
1897 }
chaviwadc40c22018-07-10 16:57:27 -07001898 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001899
1900 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
1901 [&]() { return layerInfo->mutable_transparent_region(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001902
1903 layerInfo->set_layer_stack(getLayerStack());
1904 layerInfo->set_z(state.z);
1905
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001906 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(),
1907 [&]() {
1908 return layerInfo->mutable_requested_position();
1909 });
1910
1911 LayerProtoHelper::writeSizeToProto(state.active_legacy.w, state.active_legacy.h,
1912 [&]() { return layerInfo->mutable_size(); });
1913
1914 LayerProtoHelper::writeToProto(state.crop_legacy,
1915 [&]() { return layerInfo->mutable_crop(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001916
1917 layerInfo->set_is_opaque(isOpaque(state));
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001918
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001919
1920 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1921 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
1922 LayerProtoHelper::writeToProto(state.color,
1923 [&]() { return layerInfo->mutable_requested_color(); });
1924 layerInfo->set_flags(state.flags);
1925
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001926 LayerProtoHelper::writeToProto(requestedTransform,
1927 layerInfo->mutable_requested_transform());
1928
1929 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
1930 if (parent != nullptr) {
1931 layerInfo->set_parent(parent->sequence);
1932 } else {
1933 layerInfo->set_parent(-1);
1934 }
1935
1936 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1937 if (zOrderRelativeOf != nullptr) {
1938 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1939 } else {
1940 layerInfo->set_z_order_relative_of(-1);
1941 }
chaviwadc40c22018-07-10 16:57:27 -07001942 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001943
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001944 if (traceFlags & SurfaceTracing::TRACE_INPUT) {
1945 LayerProtoHelper::writeToProto(state.inputInfo, state.touchableRegionCrop,
1946 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001947 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001948
1949 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
1950 auto protoMap = layerInfo->mutable_metadata();
1951 for (const auto& entry : state.metadata.mMap) {
1952 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
1953 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001954 }
chaviw1d044282017-09-27 12:19:28 -07001955}
1956
Robert Carr2e102c92018-10-23 12:11:15 -07001957bool Layer::isRemovedFromCurrentState() const {
1958 return mRemovedFromCurrentState;
1959}
1960
Arthur Hungd20b2702019-01-14 18:16:16 +08001961InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001962 InputWindowInfo info = mDrawingState.inputInfo;
Robert Carr720e5062018-07-30 17:45:14 -07001963
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001964 if (info.displayId == ADISPLAY_ID_NONE) {
chaviwb15ea8a2019-09-03 12:40:22 -07001965 info.displayId = getLayerStack();
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001966 }
1967
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001968 ui::Transform t = getTransform();
1969 const float xScale = t.sx();
1970 const float yScale = t.sy();
Ady Abraham282f1d72019-07-24 18:05:56 -07001971 int32_t xSurfaceInset = info.surfaceInset;
1972 int32_t ySurfaceInset = info.surfaceInset;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001973 if (xScale != 1.0f || yScale != 1.0f) {
Ady Abraham282f1d72019-07-24 18:05:56 -07001974 info.windowXScale *= (xScale != 0.0f) ? 1.0f / xScale : 0.0f;
1975 info.windowYScale *= (yScale != 0.0f) ? 1.0f / yScale : 0.0f;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001976 info.touchableRegion.scaleSelf(xScale, yScale);
Ady Abraham282f1d72019-07-24 18:05:56 -07001977 xSurfaceInset = std::round(xSurfaceInset * xScale);
1978 ySurfaceInset = std::round(ySurfaceInset * yScale);
Riddle Hsu39d4aa52018-11-30 20:46:53 +08001979 }
Robert Carre07e1032018-11-26 12:55:53 -08001980
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001981 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001982 // If this is a portal window, set the touchableRegion to the layerBounds.
1983 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
1984 ? getBufferSize(getDrawingState())
1985 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08001986 if (!layerBounds.isValid()) {
1987 layerBounds = getCroppedBufferSize(getDrawingState());
1988 }
Vishnu Nair8033a492018-12-05 07:27:23 -08001989 layerBounds = t.transform(layerBounds);
Ady Abraham282f1d72019-07-24 18:05:56 -07001990
1991 // clamp inset to layer bounds
1992 xSurfaceInset = (xSurfaceInset >= 0) ? std::min(xSurfaceInset, layerBounds.getWidth() / 2) : 0;
1993 ySurfaceInset = (ySurfaceInset >= 0) ? std::min(ySurfaceInset, layerBounds.getHeight() / 2) : 0;
1994
Arthur Hung118b1142019-05-08 21:25:59 +08001995 layerBounds.inset(xSurfaceInset, ySurfaceInset, xSurfaceInset, ySurfaceInset);
Vishnu Nair8033a492018-12-05 07:27:23 -08001996
Arthur Hungd20b2702019-01-14 18:16:16 +08001997 // Input coordinate should match the layer bounds.
1998 info.frameLeft = layerBounds.left;
1999 info.frameTop = layerBounds.top;
2000 info.frameRight = layerBounds.right;
2001 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08002002
2003 // Position the touchable region relative to frame screen location and restrict it to frame
2004 // bounds.
2005 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08002006 info.visible = canReceiveInput();
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002007
2008 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2009 if (info.replaceTouchableRegionWithCrop) {
2010 if (cropLayer == nullptr) {
2011 info.touchableRegion = Region(Rect{mScreenBounds});
2012 } else {
2013 info.touchableRegion = Region(Rect{cropLayer->mScreenBounds});
2014 }
2015 } else if (cropLayer != nullptr) {
2016 info.touchableRegion = info.touchableRegion.intersect(Rect{cropLayer->mScreenBounds});
2017 }
2018
Robert Carr720e5062018-07-30 17:45:14 -07002019 return info;
2020}
2021
2022bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002023 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002024}
2025
Lloyd Piquefeb73d72018-12-04 17:23:44 -08002026std::shared_ptr<compositionengine::Layer> Layer::getCompositionLayer() const {
2027 return nullptr;
2028}
2029
chaviw3e727cd2019-01-31 13:41:05 -08002030bool Layer::canReceiveInput() const {
Vishnu Nair82353e92019-09-12 12:38:47 -07002031 return !isHiddenByPolicy();
chaviw3e727cd2019-01-31 13:41:05 -08002032}
2033
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002034compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
2035 const sp<const DisplayDevice>& display) const {
2036 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionLayer().get());
2037}
2038
Lloyd Piquea2468662019-03-07 21:31:06 -08002039Region Layer::debugGetVisibleRegionOnDefaultDisplay() const {
2040 sp<DisplayDevice> displayDevice = mFlinger->getDefaultDisplayDeviceLocked();
2041 if (displayDevice == nullptr) {
2042 return {};
2043 }
2044
2045 auto outputLayer = findOutputLayerForDisplay(displayDevice);
2046 if (outputLayer == nullptr) {
2047 return {};
2048 }
2049
2050 return outputLayer->getState().visibleRegion;
2051}
2052
chaviwb4c6e582019-08-16 14:35:07 -07002053void Layer::setInitialValuesForClone(const sp<Layer>& clonedFrom) {
2054 // copy drawing state from cloned layer
2055 mDrawingState = clonedFrom->mDrawingState;
2056 mClonedFrom = clonedFrom;
2057
2058 // TODO: (b/140756730) Ignore input for now since InputDispatcher doesn't support multiple
2059 // InputWindows per client token yet.
2060 mDrawingState.inputInfo.token = nullptr;
2061}
chaviw74b03172019-08-19 11:09:03 -07002062
2063void Layer::updateMirrorInfo() {
2064 if (mClonedChild == nullptr || !mClonedChild->isClonedFromAlive()) {
2065 // If mClonedChild is null, there is nothing to mirror. If isClonedFromAlive returns false,
2066 // it means that there is a clone, but the layer it was cloned from has been destroyed. In
2067 // that case, we want to delete the reference to the clone since we want it to get
2068 // destroyed. The root, this layer, will still be around since the client can continue
2069 // to hold a reference, but no cloned layers will be displayed.
2070 mClonedChild = nullptr;
2071 return;
2072 }
2073
2074 std::map<sp<Layer>, sp<Layer>> clonedLayersMap;
2075 // If the real layer exists and is in current state, add the clone as a child of the root.
2076 // There's no need to remove from drawingState when the layer is offscreen since currentState is
2077 // copied to drawingState for the root layer. So the clonedChild is always removed from
2078 // drawingState and then needs to be added back each traversal.
2079 if (!mClonedChild->getClonedFrom()->isRemovedFromCurrentState()) {
2080 addChildToDrawing(mClonedChild);
2081 }
2082
2083 mClonedChild->updateClonedDrawingState(clonedLayersMap);
2084 mClonedChild->updateClonedChildren(this, clonedLayersMap);
2085 mClonedChild->updateClonedRelatives(clonedLayersMap);
2086}
2087
2088void Layer::updateClonedDrawingState(std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2089 // If the layer the clone was cloned from is alive, copy the content of the drawingState
2090 // to the clone. If the real layer is no longer alive, continue traversing the children
2091 // since we may be able to pull out other children that are still alive.
2092 if (isClonedFromAlive()) {
2093 sp<Layer> clonedFrom = getClonedFrom();
2094 mDrawingState = clonedFrom->mDrawingState;
2095 // TODO: (b/140756730) Ignore input for now since InputDispatcher doesn't support multiple
2096 // InputWindows per client token yet.
2097 mDrawingState.inputInfo.token = nullptr;
2098 clonedLayersMap.emplace(clonedFrom, this);
2099 }
2100
2101 // The clone layer may have children in drawingState since they may have been created and
2102 // added from a previous request to updateMirorInfo. This is to ensure we don't recreate clones
2103 // that already exist, since we can just re-use them.
2104 // The drawingChildren will not get overwritten by the currentChildren since the clones are
2105 // not updated in the regular traversal. They are skipped since the root will lose the
2106 // reference to them when it copies its currentChildren to drawing.
2107 for (sp<Layer>& child : mDrawingChildren) {
2108 child->updateClonedDrawingState(clonedLayersMap);
2109 }
2110}
2111
2112void Layer::updateClonedChildren(const sp<Layer>& mirrorRoot,
2113 std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2114 mDrawingChildren.clear();
2115
2116 if (!isClonedFromAlive()) {
2117 return;
2118 }
2119
2120 sp<Layer> clonedFrom = getClonedFrom();
2121 for (sp<Layer>& child : clonedFrom->mDrawingChildren) {
2122 if (child == mirrorRoot) {
2123 // This is to avoid cyclical mirroring.
2124 continue;
2125 }
2126 sp<Layer> clonedChild = clonedLayersMap[child];
2127 if (clonedChild == nullptr) {
2128 clonedChild = child->createClone();
2129 clonedLayersMap[child] = clonedChild;
2130 }
2131 addChildToDrawing(clonedChild);
2132 clonedChild->updateClonedChildren(mirrorRoot, clonedLayersMap);
2133 }
2134}
2135
2136void Layer::updateClonedRelatives(std::map<sp<Layer>, sp<Layer>> clonedLayersMap) {
2137 mDrawingState.zOrderRelativeOf = nullptr;
2138 mDrawingState.zOrderRelatives.clear();
2139
2140 if (!isClonedFromAlive()) {
2141 return;
2142 }
2143
2144 sp<Layer> clonedFrom = getClonedFrom();
2145 for (wp<Layer>& relativeWeak : clonedFrom->mDrawingState.zOrderRelatives) {
2146 sp<Layer> relative = relativeWeak.promote();
2147 auto clonedRelative = clonedLayersMap[relative];
2148 if (clonedRelative != nullptr) {
2149 mDrawingState.zOrderRelatives.add(clonedRelative);
2150 }
2151 }
2152
2153 // Check if the relativeLayer for the real layer is part of the cloned hierarchy.
2154 // It's possible that the layer it's relative to is outside the requested cloned hierarchy.
2155 // In that case, we treat the layer as if the relativeOf has been removed. This way, it will
2156 // still traverse the children, but the layer with the missing relativeOf will not be shown
2157 // on screen.
2158 sp<Layer> relativeOf = clonedFrom->mDrawingState.zOrderRelativeOf.promote();
2159 sp<Layer> clonedRelativeOf = clonedLayersMap[relativeOf];
2160 if (clonedRelativeOf != nullptr) {
2161 mDrawingState.zOrderRelativeOf = clonedRelativeOf;
2162 }
2163
2164 for (sp<Layer>& child : mDrawingChildren) {
2165 child->updateClonedRelatives(clonedLayersMap);
2166 }
2167}
2168
2169void Layer::addChildToDrawing(const sp<Layer>& layer) {
2170 mDrawingChildren.add(layer);
2171 layer->mDrawingParent = this;
2172}
2173
Mathias Agopian13127d82013-03-05 17:47:11 -08002174// ---------------------------------------------------------------------------
2175
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002176}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002177
2178#if defined(__gl_h_)
2179#error "don't include gl/gl.h in this file"
2180#endif
2181
2182#if defined(__gl2_h_)
2183#error "don't include gl2/gl2.h in this file"
2184#endif