blob: 215e2403601bfdd14d5799bc5736a7639fab8bd1 [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
342void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform) {
343 const State& s(getDrawingState());
344
345 // Calculate effective layer transform
346 mEffectiveTransform = parentTransform * getActiveTransform(s);
347
348 // Transform parent bounds to layer space
349 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
350
Vishnu Nairc652ff82019-03-15 12:48:54 -0700351 // Calculate source bounds
Vishnu Nair4351ad52019-02-11 14:13:02 -0800352 mSourceBounds = computeSourceBounds(parentBounds);
353
354 // Calculate bounds by croping diplay frame with layer crop and parent bounds
355 FloatRect bounds = mSourceBounds;
356 const Rect layerCrop = getCrop(s);
357 if (!layerCrop.isEmpty()) {
358 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
359 }
360 bounds = bounds.intersect(parentBounds);
361
362 mBounds = bounds;
363 mScreenBounds = mEffectiveTransform.transform(mBounds);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700364
365 // Add any buffer scaling to the layer's children.
366 ui::Transform bufferScaleTransform = getBufferScaleTransform();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800367 for (const sp<Layer>& child : mDrawingChildren) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700368 child->computeBounds(getBoundsPreScaling(bufferScaleTransform),
369 getTransformWithScale(bufferScaleTransform));
Vishnu Nair4351ad52019-02-11 14:13:02 -0800370 }
371}
372
Vishnu Nair60356342018-11-13 13:00:45 -0800373Rect Layer::getCroppedBufferSize(const State& s) const {
374 Rect size = getBufferSize(s);
375 Rect crop = getCrop(s);
376 if (!crop.isEmpty() && size.isValid()) {
377 size.intersect(crop, &size);
378 } else if (!crop.isEmpty()) {
379 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700380 }
Vishnu Nair60356342018-11-13 13:00:45 -0800381 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800382}
383
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700384void Layer::setupRoundedCornersCropCoordinates(Rect win,
385 const FloatRect& roundedCornersCrop) const {
386 // Translate win by the rounded corners rect coordinates, to have all values in
387 // layer coordinate space.
388 win.left -= roundedCornersCrop.left;
389 win.right -= roundedCornersCrop.left;
390 win.top -= roundedCornersCrop.top;
391 win.bottom -= roundedCornersCrop.top;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700392}
393
Lloyd Piquec6687342019-03-07 21:34:57 -0800394void Layer::latchBasicGeometry(compositionengine::LayerFECompositionState& compositionState) const {
395 const auto& drawingState{getDrawingState()};
396 const uint32_t layerStack = getLayerStack();
397 const auto alpha = static_cast<float>(getAlpha());
398 const bool opaque = isOpaque(drawingState);
399 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
400
401 auto blendMode = Hwc2::IComposerClient::BlendMode::NONE;
402 if (!opaque || alpha != 1.0f) {
403 blendMode = mPremultipliedAlpha ? Hwc2::IComposerClient::BlendMode::PREMULTIPLIED
404 : Hwc2::IComposerClient::BlendMode::COVERAGE;
405 }
406
407 // TODO(b/121291683): Instead of filling in a passed-in compositionState
408 // structure, switch to Layer owning the structure and have
409 // CompositionEngine be able to get a reference to it.
410
411 compositionState.layerStackId =
412 (layerStack != ~0u) ? std::make_optional(layerStack) : std::nullopt;
413 compositionState.internalOnly = getPrimaryDisplayOnly();
414 compositionState.isVisible = isVisible();
415 compositionState.isOpaque = opaque && !usesRoundedCorners && alpha == 1.f;
416
417 compositionState.contentDirty = contentDirty;
418 contentDirty = false;
419
420 compositionState.geomLayerBounds = mBounds;
421 compositionState.geomLayerTransform = getTransform();
422 compositionState.geomInverseLayerTransform = compositionState.geomLayerTransform.inverse();
423 compositionState.transparentRegionHint = getActiveTransparentRegion(drawingState);
424
425 compositionState.blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
426 compositionState.alpha = alpha;
427}
428
Lloyd Piquea83776c2019-01-29 18:42:32 -0800429void Layer::latchGeometry(compositionengine::LayerFECompositionState& compositionState) const {
430 const auto& drawingState{getDrawingState()};
Mathias Agopian13127d82013-03-05 17:47:11 -0800431
Lloyd Piquea83776c2019-01-29 18:42:32 -0800432 int type = drawingState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
433 int appId = drawingState.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700434 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400435 if (parent.get()) {
436 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800437 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
438 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
Jiwen 'Steve' Cai736c74e2019-05-28 18:33:22 -0700439 if (parentType > 0 && parentAppId > 0) {
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800440 type = parentType;
441 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700442 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400443 }
444
Lloyd Piquea83776c2019-01-29 18:42:32 -0800445 compositionState.geomBufferSize = getBufferSize(drawingState);
chaviw4244e032019-09-04 11:27:49 -0700446 compositionState.geomContentCrop = getBufferCrop();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800447 compositionState.geomCrop = getCrop(drawingState);
chaviw4244e032019-09-04 11:27:49 -0700448 compositionState.geomBufferTransform = getBufferTransform();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800449 compositionState.geomBufferUsesDisplayInverseTransform = getTransformToDisplayInverse();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800450 compositionState.geomUsesSourceCrop = usesSourceCrop();
451 compositionState.isSecure = isSecure();
David Sodman15094112018-10-11 09:39:37 -0700452
Lloyd Piquea83776c2019-01-29 18:42:32 -0800453 compositionState.type = type;
454 compositionState.appId = appId;
455}
David Sodmanba340492018-08-05 21:51:33 -0700456
Lloyd Piquef5275482019-01-29 18:42:42 -0800457void Layer::latchPerFrameState(compositionengine::LayerFECompositionState& compositionState) const {
Lloyd Pique688abd42019-02-15 15:42:24 -0800458 const auto& drawingState{getDrawingState()};
Lloyd Piquef5275482019-01-29 18:42:42 -0800459 compositionState.forceClientComposition = false;
460
Lloyd Piquef5275482019-01-29 18:42:42 -0800461 compositionState.isColorspaceAgnostic = isColorSpaceAgnostic();
chaviw4244e032019-09-04 11:27:49 -0700462 compositionState.dataspace = getDataSpace();
Lloyd Piquef5275482019-01-29 18:42:42 -0800463 compositionState.colorTransform = getColorTransform();
464 compositionState.colorTransformIsIdentity = !hasColorTransform();
465 compositionState.surfaceDamage = surfaceDamageRegion;
Lloyd Pique688abd42019-02-15 15:42:24 -0800466 compositionState.hasProtectedContent = isProtected();
467
468 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
469 compositionState.isOpaque =
470 isOpaque(drawingState) && !usesRoundedCorners && getAlpha() == 1.0_hf;
Lloyd Piquef5275482019-01-29 18:42:42 -0800471
472 // Force client composition for special cases known only to the front-end.
Lloyd Pique688abd42019-02-15 15:42:24 -0800473 if (isHdrY410() || usesRoundedCorners) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800474 compositionState.forceClientComposition = true;
475 }
476}
477
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800478void Layer::latchCursorCompositionState(
479 compositionengine::LayerFECompositionState& compositionState) const {
480 // This gives us only the "orientation" component of the transform
481 const State& drawingState{getDrawingState()};
482
483 // Apply the layer's transform, followed by the display's global transform
484 // Here we're guaranteed that the layer's transform preserves rects
485 Rect win = getCroppedBufferSize(drawingState);
486 // Subtract the transparent region and snap to the bounds
487 Rect bounds = reduce(win, getActiveTransparentRegion(drawingState));
488 Rect frame(getTransform().transform(bounds));
489
490 compositionState.cursorFrame = frame;
491}
492
Lloyd Piquef16688f2019-02-19 17:47:57 -0800493bool Layer::onPreComposition(nsecs_t) {
494 return false;
495}
496
Lloyd Piquea83776c2019-01-29 18:42:32 -0800497void Layer::latchCompositionState(compositionengine::LayerFECompositionState& compositionState,
Lloyd Piquec6687342019-03-07 21:34:57 -0800498 compositionengine::LayerFE::StateSubset subset) const {
499 using StateSubset = compositionengine::LayerFE::StateSubset;
Lloyd Piquef5275482019-01-29 18:42:42 -0800500
Lloyd Piquec6687342019-03-07 21:34:57 -0800501 switch (subset) {
502 case StateSubset::BasicGeometry:
503 latchBasicGeometry(compositionState);
504 break;
505
506 case StateSubset::GeometryAndContent:
507 latchBasicGeometry(compositionState);
508 latchGeometry(compositionState);
509 latchPerFrameState(compositionState);
510 break;
511
512 case StateSubset::Content:
513 latchPerFrameState(compositionState);
514 break;
515 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800516}
Mathias Agopian29a367b2011-07-12 14:51:45 -0700517
Lloyd Piquea83776c2019-01-29 18:42:32 -0800518const char* Layer::getDebugName() const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700519 return mName.c_str();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800520}
521
Mathias Agopian13127d82013-03-05 17:47:11 -0800522// ---------------------------------------------------------------------------
523// drawing...
524// ---------------------------------------------------------------------------
525
Lloyd Piquef16688f2019-02-19 17:47:57 -0800526std::optional<renderengine::LayerSettings> Layer::prepareClientComposition(
527 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
528 if (!getCompositionLayer()) {
529 return {};
530 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800531
Vishnu Nair4351ad52019-02-11 14:13:02 -0800532 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000533 half alpha = getAlpha();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800534 renderengine::LayerSettings layerSettings;
535 layerSettings.geometry.boundaries = bounds;
536 if (targetSettings.useIdentityTransform) {
537 layerSettings.geometry.positionTransform = mat4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000538 } else {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800539 layerSettings.geometry.positionTransform = getTransform().asMatrix4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000540 }
541
542 if (hasColorTransform()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800543 layerSettings.colorTransform = getColorTransform();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000544 }
545
546 const auto roundedCornerState = getRoundedCornerState();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800547 layerSettings.geometry.roundedCornersRadius = roundedCornerState.radius;
548 layerSettings.geometry.roundedCornersCrop = roundedCornerState.cropRect;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000549
Lloyd Piquef16688f2019-02-19 17:47:57 -0800550 layerSettings.alpha = alpha;
chaviw4244e032019-09-04 11:27:49 -0700551 layerSettings.sourceDataspace = getDataSpace();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800552 return layerSettings;
Mathias Agopian13127d82013-03-05 17:47:11 -0800553}
554
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800555Hwc2::IComposerClient::Composition Layer::getCompositionType(
556 const sp<const DisplayDevice>& display) const {
557 const auto outputLayer = findOutputLayerForDisplay(display);
558 LOG_FATAL_IF(!outputLayer);
559 return outputLayer->getState().hwc ? (*outputLayer->getState().hwc).hwcCompositionType
560 : Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800561}
562
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800563bool Layer::getClearClientTarget(const sp<const DisplayDevice>& display) const {
564 const auto outputLayer = findOutputLayerForDisplay(display);
565 LOG_FATAL_IF(!outputLayer);
566 return outputLayer->getState().clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800567}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800568
Dan Stozacac35382016-01-27 12:21:06 -0800569bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
570 if (point->getFrameNumber() <= mCurrentFrameNumber) {
571 // Don't bother with a SyncPoint, since we've already latched the
572 // relevant frame
573 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700574 }
Robert Carr2e102c92018-10-23 12:11:15 -0700575 if (isRemovedFromCurrentState()) {
576 return false;
577 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700578
Dan Stozacac35382016-01-27 12:21:06 -0800579 Mutex::Autolock lock(mLocalSyncPointMutex);
580 mLocalSyncPoints.push_back(point);
581 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700582}
583
Mathias Agopian13127d82013-03-05 17:47:11 -0800584// ----------------------------------------------------------------------------
585// local state
586// ----------------------------------------------------------------------------
587
David Sodman41fdfc92017-11-06 16:09:56 -0800588bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800589 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700590 return (s.flags & layer_state_t::eLayerSecure);
591}
592
Mathias Agopian13127d82013-03-05 17:47:11 -0800593// ----------------------------------------------------------------------------
594// transaction
595// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800596
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800597void Layer::pushPendingState() {
598 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700599 return;
600 }
Alec Mourie60041e2019-06-14 18:59:51 -0700601 ATRACE_CALL();
Dan Stoza7dde5992015-05-22 09:51:44 -0700602
Dan Stoza7dde5992015-05-22 09:51:44 -0700603 // If this transaction is waiting on the receipt of a frame, generate a sync
604 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700605 // We don't allow installing sync points after we are removed from the current state
606 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800607 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
608 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800609 if (barrierLayer == nullptr) {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700610 ALOGE("[%s] Unable to promote barrier Layer.", getDebugName());
Dan Stoza7dde5992015-05-22 09:51:44 -0700611 // If we can't promote the layer we are intended to wait on,
612 // then it is expired or otherwise invalid. Allow this transaction
613 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800614 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800615 } else {
chaviw43cb3cb2019-05-31 15:23:41 -0700616 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy, this);
Robert Carr0d480722017-01-10 16:42:54 -0800617 if (barrierLayer->addSyncPoint(syncPoint)) {
Alec Mourie60041e2019-06-14 18:59:51 -0700618 std::stringstream ss;
619 ss << "Adding sync point " << mCurrentState.frameNumber_legacy;
620 ATRACE_NAME(ss.str().c_str());
Dan Stozacac35382016-01-27 12:21:06 -0800621 mRemoteSyncPoints.push_back(std::move(syncPoint));
622 } else {
623 // We already missed the frame we're supposed to synchronize
624 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800625 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800626 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700627 }
628
Dan Stoza7dde5992015-05-22 09:51:44 -0700629 // Wake us up to check if the frame has been received
630 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700631 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700632 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800633 mPendingStates.push_back(mCurrentState);
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700634 ATRACE_INT(mTransactionName.c_str(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700635}
636
Pablo Ceballos05289c22016-04-14 15:49:55 -0700637void Layer::popPendingState(State* stateToCommit) {
Alec Mourie60041e2019-06-14 18:59:51 -0700638 ATRACE_CALL();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800639 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700640
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800641 mPendingStates.removeAt(0);
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700642 ATRACE_INT(mTransactionName.c_str(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700643}
644
Pablo Ceballos05289c22016-04-14 15:49:55 -0700645bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700646 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800647 while (!mPendingStates.empty()) {
648 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700649 if (mRemoteSyncPoints.empty()) {
650 // If we don't have a sync point for this, apply it anyway. It
651 // will be visually wrong, but it should keep us from getting
652 // into too much trouble.
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700653 ALOGE("[%s] No local sync point found", getDebugName());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700654 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700655 stateUpdateAvailable = true;
656 continue;
657 }
658
Marissa Wallf58c14b2018-07-24 10:50:43 -0700659 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800660 mPendingStates[0].frameNumber_legacy) {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700661 ALOGE("[%s] Unexpected sync point frame number found", getDebugName());
Dan Stozacac35382016-01-27 12:21:06 -0800662
663 // Signal our end of the sync point and then dispose of it
664 mRemoteSyncPoints.front()->setTransactionApplied();
665 mRemoteSyncPoints.pop_front();
666 continue;
667 }
668
Dan Stoza7dde5992015-05-22 09:51:44 -0700669 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
Alec Mourie60041e2019-06-14 18:59:51 -0700670 ATRACE_NAME("frameIsAvailable");
Dan Stoza7dde5992015-05-22 09:51:44 -0700671 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700672 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700673 stateUpdateAvailable = true;
674
675 // Signal our end of the sync point and then dispose of it
676 mRemoteSyncPoints.front()->setTransactionApplied();
677 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800678 } else {
Alec Mourie60041e2019-06-14 18:59:51 -0700679 ATRACE_NAME("!frameIsAvailable");
Dan Stoza792e5292016-02-11 11:43:58 -0800680 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700681 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700682 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700683 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700684 stateUpdateAvailable = true;
685 }
686 }
687
688 // If we still have pending updates, wake SurfaceFlinger back up and point
689 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800690 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700691 setTransactionFlags(eTransactionNeeded);
692 mFlinger->setTransactionFlags(eTraversalNeeded);
693 }
694
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800695 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700696 return stateUpdateAvailable;
697}
698
Marissa Wall61c58622018-07-18 10:12:20 -0700699uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000700 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800701
Marissa Wall61c58622018-07-18 10:12:20 -0700702 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
703 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700704
David Sodmaneb085e02017-10-05 18:49:04 -0700705 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700706 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000707 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800708 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
709 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
710 " requested={ wh={%4u,%4u} }}\n"
711 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
712 " requested={ wh={%4u,%4u} }}\n",
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700713 this, getName().c_str(), getBufferTransform(), getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700714 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
715 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
716 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
717 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
718 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700719 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
720 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
721 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800722 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700723
Robert Carre392b552017-09-19 12:16:05 -0700724 // Don't let Layer::doTransaction update the drawing state
725 // if we have a pending resize, unless we are in fixed-size mode.
726 // the drawing state will be updated only once we receive a buffer
727 // with the correct size.
728 //
729 // In particular, we want to make sure the clip (which is part
730 // of the geometry state) is latched together with the size but is
731 // latched immediately when no resizing is involved.
732 //
733 // If a sideband stream is attached, however, we want to skip this
734 // optimization so that transactions aren't missed when a buffer
735 // never arrives
736 //
737 // In the case that we don't have a buffer we ignore other factors
738 // and avoid entering the resizePending state. At a high level the
739 // resizePending state is to avoid applying the state of the new buffer
740 // to the old buffer. However in the state where we don't have an old buffer
741 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700742 const bool resizePending =
743 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
744 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
chaviwd62d3062019-09-04 14:48:02 -0700745 (getBuffer() != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700746 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800747 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700748 flags |= eDontUpdateGeometryState;
749 }
750 }
751
Robert Carr7bf247e2017-05-18 14:02:49 -0700752 // Here we apply various requested geometry states, depending on our
753 // latching configuration. See Layer.h for a detailed discussion of
754 // how geometry latching is controlled.
755 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000756 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700757
Robert Carr7bf247e2017-05-18 14:02:49 -0700758 // There is an awkward asymmetry in the handling of the crop states in the position
759 // states, as can be seen below. Largely this arises from position and transform
760 // being stored in the same data structure while having different latching rules.
761 // b/38182305
762 //
Marissa Wall61c58622018-07-18 10:12:20 -0700763 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700764 // applyPendingStates in the presence of deferred transactions.
chaviw214c89d2019-09-04 16:03:53 -0700765 editCurrentState.active_legacy = editCurrentState.requested_legacy;
766 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Mathias Agopian13127d82013-03-05 17:47:11 -0800767 }
768
Marissa Wall61c58622018-07-18 10:12:20 -0700769 return flags;
770}
771
772uint32_t Layer::doTransaction(uint32_t flags) {
773 ATRACE_CALL();
774
chaviw5aedec92018-10-22 10:40:38 -0700775 if (mLayerDetached) {
Robert Carr321e83c2019-08-19 15:49:30 -0700776 // Ensure BLAST buffer callbacks are processed.
777 // detachChildren and mLayerDetached were implemented to avoid geometry updates
778 // to layers in the cases of animation. For BufferQueue layers buffers are still
779 // consumed as normal. This is useful as otherwise the client could get hung
780 // inevitably waiting on a buffer to return. We recreate this semantic for BufferQueue
781 // even though it is a little consistent. detachChildren is shortly slated for removal
782 // by the hierarchy mirroring work so we don't need to worry about it too much.
783 mDrawingState.callbackHandles = mCurrentState.callbackHandles;
784 mCurrentState.callbackHandles = {};
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800785 return flags;
786 }
787
788 if (mChildrenChanged) {
789 flags |= eVisibleRegion;
790 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -0700791 }
792
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800793 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +0000794 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -0700795 if (!applyPendingStates(&c)) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800796 return flags;
Marissa Wall61c58622018-07-18 10:12:20 -0700797 }
798
799 flags = doTransactionResize(flags, &c);
800
Alec Mourib416efd2018-09-06 21:01:59 +0000801 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700802
803 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800804 // invalidate and recompute the visible regions if needed
805 flags |= Layer::eVisibleRegion;
806 }
807
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700808 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800809 // invalidate and recompute the visible regions if needed
810 flags |= eVisibleRegion;
811 this->contentDirty = true;
812
813 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -0700814 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -0700815 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -0800816 }
817
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800818 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -0700819 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800820 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -0700821 }
822
Mathias Agopian13127d82013-03-05 17:47:11 -0800823 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -0700824 commitTransaction(c);
Vishnu Nair8406fd72019-07-30 11:29:31 -0700825 mPendingStatesSnapshot = mPendingStates;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800826 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -0800827 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800828}
829
Pablo Ceballos05289c22016-04-14 15:49:55 -0700830void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800831 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700832}
833
Mathias Agopian13127d82013-03-05 17:47:11 -0800834uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800835 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800836}
837
838uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800839 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -0800840}
841
chaviw214c89d2019-09-04 16:03:53 -0700842bool Layer::setPosition(float x, float y) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800843 if (mCurrentState.requested_legacy.transform.tx() == x &&
844 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -0800845 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800846 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -0700847
848 // We update the requested and active position simultaneously because
849 // we want to apply the position portion of the transform matrix immediately,
850 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800851 mCurrentState.requested_legacy.transform.set(x, y);
chaviw214c89d2019-09-04 16:03:53 -0700852 // Here we directly update the active state
853 // unlike other setters, because we store it within
854 // the transform, but use different latching rules.
855 // b/38182305
856 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr69663fb2016-03-27 19:59:19 -0700857
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800858 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800859 setTransactionFlags(eTransactionNeeded);
860 return true;
861}
Robert Carr82364e32016-05-15 11:27:47 -0700862
Robert Carr1f0a16a2016-10-24 16:27:39 -0700863bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
864 ssize_t idx = mCurrentChildren.indexOf(childLayer);
865 if (idx < 0) {
866 return false;
867 }
868 if (childLayer->setLayer(z)) {
869 mCurrentChildren.removeAt(idx);
870 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800871 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700872 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800873 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700874}
875
Robert Carr503c7042017-09-27 15:06:08 -0700876bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
877 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
878 ssize_t idx = mCurrentChildren.indexOf(childLayer);
879 if (idx < 0) {
880 return false;
881 }
882 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
883 mCurrentChildren.removeAt(idx);
884 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800885 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700886 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800887 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700888}
889
Robert Carrae060832016-11-28 10:51:00 -0800890bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800891 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
892 mCurrentState.sequence++;
893 mCurrentState.z = z;
894 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700895
896 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800897 if (mCurrentState.zOrderRelativeOf != nullptr) {
898 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700899 if (strongRelative != nullptr) {
900 strongRelative->removeZOrderRelative(this);
901 }
chaviw606e5cf2019-03-01 10:12:10 -0800902 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -0700903 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800904 setTransactionFlags(eTransactionNeeded);
905 return true;
906}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700907
Robert Carrdb66e622017-04-10 16:55:57 -0700908void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800909 mCurrentState.zOrderRelatives.remove(relative);
910 mCurrentState.sequence++;
911 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700912 setTransactionFlags(eTransactionNeeded);
913}
914
915void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800916 mCurrentState.zOrderRelatives.add(relative);
917 mCurrentState.modified = true;
918 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -0700919 setTransactionFlags(eTransactionNeeded);
920}
921
chaviw606e5cf2019-03-01 10:12:10 -0800922void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
923 mCurrentState.zOrderRelativeOf = relativeOf;
924 mCurrentState.sequence++;
925 mCurrentState.modified = true;
chaviwbdb8b802019-10-14 09:17:12 -0700926 mCurrentState.isRelativeOf = relativeOf != nullptr;
927
chaviw606e5cf2019-03-01 10:12:10 -0800928 setTransactionFlags(eTransactionNeeded);
929}
930
Robert Carr503d2bd2017-12-04 15:49:47 -0800931bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -0700932 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
933 if (handle == nullptr) {
934 return false;
935 }
936 sp<Layer> relative = handle->owner.promote();
937 if (relative == nullptr) {
938 return false;
939 }
940
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800941 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
942 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -0800943 return false;
944 }
945
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800946 mCurrentState.sequence++;
947 mCurrentState.modified = true;
948 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -0700949
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800950 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -0700951 if (oldZOrderRelativeOf != nullptr) {
952 oldZOrderRelativeOf->removeZOrderRelative(this);
953 }
chaviw606e5cf2019-03-01 10:12:10 -0800954 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -0700955 relative->addZOrderRelative(this);
956
957 setTransactionFlags(eTransactionNeeded);
958
959 return true;
960}
961
Mathias Agopian13127d82013-03-05 17:47:11 -0800962bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800963 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -0700964 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800965 mCurrentState.requested_legacy.w = w;
966 mCurrentState.requested_legacy.h = h;
967 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800968 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -0700969
970 // record the new size, from this point on, when the client request
971 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800972 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -0800973 return true;
974}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800975bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800976 if (mCurrentState.color.a == alpha) return false;
977 mCurrentState.sequence++;
978 mCurrentState.color.a = alpha;
979 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800980 setTransactionFlags(eTransactionNeeded);
981 return true;
982}
chaviw13fdc492017-06-27 12:40:18 -0700983
Valerie Haudd0b7572019-01-29 14:59:27 -0800984bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
985 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -0700986 return false;
Valerie Hauaa194562019-02-05 16:21:38 -0800987 }
988 mCurrentState.sequence++;
989 mCurrentState.modified = true;
990 setTransactionFlags(eTransactionNeeded);
991
992 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -0800993 // create background color layer if one does not yet exist
994 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700995 std::string name = mName + "BackgroundColorLayer";
Lloyd Pique958be492019-03-28 15:34:59 -0700996 mCurrentState.bgColorLayer = mFlinger->getFactory().createColorLayer(
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700997 LayerCreationArgs(mFlinger.get(), nullptr, std::move(name), 0, 0, flags,
998 LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -0700999
Valerie Haudd0b7572019-01-29 14:59:27 -08001000 // add to child list
1001 addChild(mCurrentState.bgColorLayer);
1002 mFlinger->mLayersAdded = true;
1003 // set up SF to handle added color layer
1004 if (isRemovedFromCurrentState()) {
1005 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1006 }
1007 mFlinger->setTransactionFlags(eTransactionNeeded);
1008 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1009 mCurrentState.bgColorLayer->reparent(nullptr);
1010 mCurrentState.bgColorLayer = nullptr;
1011 return true;
1012 }
1013
1014 mCurrentState.bgColorLayer->setColor(color);
1015 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1016 mCurrentState.bgColorLayer->setAlpha(alpha);
1017 mCurrentState.bgColorLayer->setDataspace(dataspace);
1018
chaviw13fdc492017-06-27 12:40:18 -07001019 return true;
1020}
1021
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001022bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001023 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001024
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001025 mCurrentState.sequence++;
1026 mCurrentState.cornerRadius = cornerRadius;
1027 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001028 setTransactionFlags(eTransactionNeeded);
1029 return true;
1030}
1031
Robert Carrd4ae7f32018-06-07 16:10:57 -07001032bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1033 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001034 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001035 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1036
1037 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1038 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1039 return false;
1040 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001041 mCurrentState.sequence++;
1042 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1043 matrix.dsdy);
1044 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001045 setTransactionFlags(eTransactionNeeded);
1046 return true;
1047}
Marissa Wall61c58622018-07-18 10:12:20 -07001048
Mathias Agopian13127d82013-03-05 17:47:11 -08001049bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001050 mCurrentState.requestedTransparentRegion_legacy = transparent;
1051 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001052 setTransactionFlags(eTransactionNeeded);
1053 return true;
1054}
1055bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001056 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1057 if (mCurrentState.flags == newFlags) return false;
1058 mCurrentState.sequence++;
1059 mCurrentState.flags = newFlags;
1060 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001061 setTransactionFlags(eTransactionNeeded);
1062 return true;
1063}
Robert Carr99e27f02016-06-16 15:18:02 -07001064
chaviw214c89d2019-09-04 16:03:53 -07001065bool Layer::setCrop_legacy(const Rect& crop) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001066 if (mCurrentState.requestedCrop_legacy == crop) return false;
1067 mCurrentState.sequence++;
1068 mCurrentState.requestedCrop_legacy = crop;
chaviw214c89d2019-09-04 16:03:53 -07001069 mCurrentState.crop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001070
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001071 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001072 setTransactionFlags(eTransactionNeeded);
1073 return true;
1074}
Robert Carr8d5227b2017-03-16 15:41:03 -07001075
Robert Carrc3574f72016-03-24 12:19:32 -07001076bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001077 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001078 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001079 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001080 return true;
1081}
1082
Evan Roskyef876c92019-01-25 17:46:06 -08001083bool Layer::setMetadata(const LayerMetadata& data) {
1084 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001085 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001086 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001087 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001088 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001089}
1090
Mathias Agopian13127d82013-03-05 17:47:11 -08001091bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001092 if (mCurrentState.layerStack == layerStack) return false;
1093 mCurrentState.sequence++;
1094 mCurrentState.layerStack = layerStack;
1095 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001096 setTransactionFlags(eTransactionNeeded);
1097 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001098}
1099
Peiyong Linc502cb72019-03-01 15:00:23 -08001100bool Layer::setColorSpaceAgnostic(const bool agnostic) {
1101 if (mCurrentState.colorSpaceAgnostic == agnostic) {
1102 return false;
1103 }
1104 mCurrentState.sequence++;
1105 mCurrentState.colorSpaceAgnostic = agnostic;
1106 mCurrentState.modified = true;
1107 setTransactionFlags(eTransactionNeeded);
1108 return true;
1109}
1110
Robert Carr1f0a16a2016-10-24 16:27:39 -07001111uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001112 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001113 if (p == nullptr) {
1114 return getDrawingState().layerStack;
1115 }
1116 return p->getLayerStack();
1117}
1118
Marissa Wallf58c14b2018-07-24 10:50:43 -07001119void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Alec Mourie60041e2019-06-14 18:59:51 -07001120 ATRACE_CALL();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001121 mCurrentState.barrierLayer_legacy = barrierLayer;
1122 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001123 // We don't set eTransactionNeeded, because just receiving a deferral
1124 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001125 mCurrentState.modified = true;
1126 pushPendingState();
1127 mCurrentState.barrierLayer_legacy = nullptr;
1128 mCurrentState.frameNumber_legacy = 0;
1129 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001130}
1131
Marissa Wallf58c14b2018-07-24 10:50:43 -07001132void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001133 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001134 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001135}
1136
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001137// ----------------------------------------------------------------------------
1138// pageflip handling...
1139// ----------------------------------------------------------------------------
1140
Robert Carr1f0a16a2016-10-24 16:27:39 -07001141bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001142 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001143 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001144 if (parent != nullptr && parent->isHiddenByPolicy()) {
1145 return true;
1146 }
Robert Carr1c5481e2019-07-01 14:42:27 -07001147 if (usingRelativeZ(LayerVector::StateSet::Drawing)) {
1148 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
1149 if (zOrderRelativeOf != nullptr) {
1150 if (zOrderRelativeOf->isHiddenByPolicy()) {
1151 return true;
1152 }
1153 }
1154 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001155 return s.flags & layer_state_t::eLayerHidden;
1156}
1157
David Sodman41fdfc92017-11-06 16:09:56 -08001158uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001159 // TODO: should we do something special if mSecure is set?
1160 if (mProtectedByApp) {
1161 // need a hardware-protected path to external video sink
1162 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001163 }
Riley Andrews03414a12014-07-01 14:22:59 -07001164 if (mPotentialCursor) {
1165 usage |= GraphicBuffer::USAGE_CURSOR;
1166 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001167 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001168 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001169}
1170
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001171void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001172 uint32_t orientation = 0;
Vishnu Nair5eb3f062019-04-08 08:21:03 -07001173 // Disable setting transform hint if the debug flag is set.
1174 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001175 // The transform hint is used to improve performance, but we can
1176 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001177 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001178 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001179 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001180 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001181 orientation = 0;
1182 }
1183 }
David Sodmaneb085e02017-10-05 18:49:04 -07001184 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001185}
1186
Mathias Agopian13127d82013-03-05 17:47:11 -08001187// ----------------------------------------------------------------------------
1188// debugging
1189// ----------------------------------------------------------------------------
1190
Marissa Wall61c58622018-07-18 10:12:20 -07001191// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001192LayerDebugInfo Layer::getLayerDebugInfo() const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001193 using namespace std::string_literals;
1194
Kalle Raitaa099a242017-01-11 11:17:29 -08001195 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001196 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001197 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001198 sp<Layer> parent = getParent();
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001199 info.mParentName = parent ? parent->getName() : "none"s;
chaviw8a01fa42019-08-19 12:39:31 -07001200 info.mType = getType();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001201 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Lloyd Piquea2468662019-03-07 21:31:06 -08001202
1203 info.mVisibleRegion = debugGetVisibleRegionOnDefaultDisplay();
Kalle Raitaa099a242017-01-11 11:17:29 -08001204 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001205 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001206 info.mX = ds.active_legacy.transform.tx();
1207 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001208 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001209 info.mWidth = ds.active_legacy.w;
1210 info.mHeight = ds.active_legacy.h;
1211 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001212 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001213 info.mFlags = ds.flags;
1214 info.mPixelFormat = getPixelFormat();
chaviw4244e032019-09-04 11:27:49 -07001215 info.mDataSpace = static_cast<android_dataspace>(getDataSpace());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001216 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1217 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1218 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1219 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001220 {
chaviwd62d3062019-09-04 14:48:02 -07001221 sp<const GraphicBuffer> buffer = getBuffer();
David Sodman5b4cffc2017-11-23 13:20:29 -08001222 if (buffer != 0) {
1223 info.mActiveBufferWidth = buffer->getWidth();
1224 info.mActiveBufferHeight = buffer->getHeight();
1225 info.mActiveBufferStride = buffer->getStride();
1226 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001227 } else {
1228 info.mActiveBufferWidth = 0;
1229 info.mActiveBufferHeight = 0;
1230 info.mActiveBufferStride = 0;
1231 info.mActiveBufferFormat = 0;
1232 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001233 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001234 info.mNumQueuedFrames = getQueuedFrameCount();
1235 info.mRefreshPending = isBufferLatched();
1236 info.mIsOpaque = isOpaque(ds);
1237 info.mContentDirty = contentDirty;
1238 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001239}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001240
Yiwei Zhang5434a782018-12-05 18:06:32 -08001241void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001242 result.append("-------------------------------");
1243 result.append("-------------------------------");
1244 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001245 result.append(" Layer name\n");
1246 result.append(" Z | ");
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001247 result.append(" Window Type | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001248 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001249 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001250 result.append(" Disp Frame (LTRB) | ");
1251 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001252 result.append("-------------------------------");
1253 result.append("-------------------------------");
1254 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001255}
1256
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001257void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1258 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1259 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001260 return;
1261 }
1262
Yiwei Zhang5434a782018-12-05 18:06:32 -08001263 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001264 if (mName.length() > 77) {
1265 std::string shortened;
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001266 shortened.append(mName, 0, 36);
Dan Stozae22aec72016-08-01 13:20:59 -07001267 shortened.append("[...]");
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001268 shortened.append(mName, mName.length() - 36);
1269 name = std::move(shortened);
Dan Stozae22aec72016-08-01 13:20:59 -07001270 } else {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001271 name = mName;
Dan Stozae22aec72016-08-01 13:20:59 -07001272 }
1273
Yiwei Zhang5434a782018-12-05 18:06:32 -08001274 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001275
Alec Mourib416efd2018-09-06 21:01:59 +00001276 const State& layerState(getDrawingState());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001277 const auto& compositionState = outputLayer->getState();
1278
Chia-I Wu1e043612018-03-01 09:45:09 -08001279 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001280 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001281 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001282 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001283 }
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001284 StringAppendF(&result, " %10d | ", mWindowType);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001285 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
1286 StringAppendF(&result, "%10s | ",
1287 toString(getCompositionLayer() ? compositionState.bufferTransform
1288 : static_cast<Hwc2::Transform>(0))
1289 .c_str());
1290 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001291 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001292 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001293 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1294 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001295
Yichi Chen6ca35192018-05-29 12:20:43 +08001296 result.append("- - - - - - - - - - - - - - - -");
1297 result.append("- - - - - - - - - - - - - - - -");
1298 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001299}
Dan Stozae22aec72016-08-01 13:20:59 -07001300
Yiwei Zhang5434a782018-12-05 18:06:32 -08001301void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001302 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001303}
1304
Svetoslavd85084b2014-03-20 10:28:31 -07001305void Layer::clearFrameStats() {
1306 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001307}
1308
Jamie Gennis6547ff42013-07-16 20:12:42 -07001309void Layer::logFrameStats() {
1310 mFrameTracker.logAndResetStats(mName);
1311}
1312
Svetoslavd85084b2014-03-20 10:28:31 -07001313void Layer::getFrameStats(FrameStats* outStats) const {
1314 mFrameTracker.getStats(outStats);
1315}
1316
Yiwei Zhang5434a782018-12-05 18:06:32 -08001317void Layer::dumpFrameEvents(std::string& result) {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001318 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().c_str(), getType(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001319 Mutex::Autolock lock(mFrameEventHistoryMutex);
1320 mFrameEventHistory.checkFencesForCompletion();
1321 mFrameEventHistory.dump(result);
1322}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001323
Vishnu Nair0f085c62019-08-30 08:49:12 -07001324void Layer::dumpCallingUidPid(std::string& result) const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001325 StringAppendF(&result, "Layer %s (%s) pid:%d uid:%d\n", getName().c_str(), getType(),
Vishnu Nair0f085c62019-08-30 08:49:12 -07001326 mCallingPid, mCallingUid);
1327}
1328
Brian Anderson5ea5e592016-12-01 16:54:33 -08001329void Layer::onDisconnect() {
1330 Mutex::Autolock lock(mFrameEventHistoryMutex);
1331 mFrameEventHistory.onDisconnect();
Mikael Pessa90092f42019-08-26 17:22:04 -07001332 const int32_t layerID = getSequence();
1333 mFlinger->mTimeStats->onDestroy(layerID);
1334 mFlinger->mFrameTracer->onDestroy(layerID);
Brian Anderson5ea5e592016-12-01 16:54:33 -08001335}
1336
Brian Anderson3890c392016-07-25 12:48:08 -07001337void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001338 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001339 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001340 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1341 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001342 }
1343
Brian Andersond6927fb2016-07-23 23:37:30 -07001344 Mutex::Autolock lock(mFrameEventHistoryMutex);
1345 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001346 // If there are any unsignaled fences in the aquire timeline at this
1347 // point, the previously queued frame hasn't been latched yet. Go ahead
1348 // and try to get the signal time here so the syscall is taken out of
1349 // the main thread's critical path.
1350 mAcquireTimeline.updateSignalTimes();
1351 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001352 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001353 mFrameEventHistory.addQueue(*newTimestamps);
1354 }
1355
Brian Anderson3890c392016-07-25 12:48:08 -07001356 if (outDelta) {
1357 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001358 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001359}
Dan Stozae77c7662016-05-13 11:37:28 -07001360
Chia-I Wu98f1c102017-05-30 14:54:08 -07001361size_t Layer::getChildrenCount() const {
1362 size_t count = 0;
1363 for (const sp<Layer>& child : mCurrentChildren) {
1364 count += 1 + child->getChildrenCount();
1365 }
1366 return count;
1367}
1368
Robert Carr1f0a16a2016-10-24 16:27:39 -07001369void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001370 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001371 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001372
Robert Carr1f0a16a2016-10-24 16:27:39 -07001373 mCurrentChildren.add(layer);
1374 layer->setParent(this);
1375}
1376
1377ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001378 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001379 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001380
Robert Carr1323c952019-01-28 18:13:27 -08001381 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001382 return mCurrentChildren.remove(layer);
1383}
1384
Robert Carr1db73f62016-12-21 12:58:51 -08001385bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1386 sp<Handle> handle = nullptr;
1387 sp<Layer> newParent = nullptr;
1388 if (newParentHandle == nullptr) {
1389 return false;
1390 }
1391 handle = static_cast<Handle*>(newParentHandle.get());
1392 newParent = handle->owner.promote();
1393 if (newParent == nullptr) {
1394 ALOGE("Unable to promote Layer handle");
1395 return false;
1396 }
1397
chaviw5aedec92018-10-22 10:40:38 -07001398 if (attachChildren()) {
1399 setTransactionFlags(eTransactionNeeded);
1400 }
Robert Carr1db73f62016-12-21 12:58:51 -08001401 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001402 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001403 }
1404 mCurrentChildren.clear();
1405
1406 return true;
1407}
1408
Robert Carr15eae092018-03-23 13:43:53 -07001409void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001410 for (const sp<Layer>& child : mDrawingChildren) {
1411 child->mDrawingParent = newParent;
Vishnu Nairc652ff82019-03-15 12:48:54 -07001412 child->computeBounds(newParent->mBounds,
1413 newParent->getTransformWithScale(
1414 newParent->getBufferScaleTransform()));
Robert Carr578038f2018-03-09 12:25:24 -08001415 }
1416}
1417
chaviwf1961f72017-09-18 16:41:07 -07001418bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001419 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001420
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001421 // While layers are detached, we allow most operations
1422 // and simply halt performing the actual transaction. However
1423 // for reparent != null we would enter the mRemovedFromCurrentState
1424 // state, regardless of whether doTransaction was called, and
1425 // so we need to prevent the update here.
1426 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001427 return false;
1428 }
1429
Robert Carr54cf5b12019-01-25 14:02:28 -08001430 sp<Layer> newParent;
1431 if (newParentHandle != nullptr) {
1432 auto handle = static_cast<Handle*>(newParentHandle.get());
1433 newParent = handle->owner.promote();
1434 if (newParent == nullptr) {
1435 ALOGE("Unable to promote Layer handle");
1436 return false;
1437 }
1438 if (newParent == this) {
1439 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1440 return false;
1441 }
1442 }
1443
chaviwf1961f72017-09-18 16:41:07 -07001444 sp<Layer> parent = getParent();
1445 if (parent != nullptr) {
1446 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001447 }
1448
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001449 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001450 newParent->addChild(this);
1451 if (!newParent->isRemovedFromCurrentState()) {
1452 addToCurrentState();
1453 } else {
1454 onRemovedFromCurrentState();
1455 }
1456
1457 if (mLayerDetached) {
1458 mLayerDetached = false;
1459 callSetTransactionFlags = true;
1460 }
1461 } else {
1462 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001463 }
1464
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001465 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001466 setTransactionFlags(eTransactionNeeded);
1467 }
chaviw06178942017-07-27 10:25:59 -07001468 return true;
1469}
1470
Robert Carr9524cb32017-02-13 11:32:32 -08001471bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001472 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001473 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001474 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001475 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001476 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001477 child->detachChildren();
chaviw43cb3cb2019-05-31 15:23:41 -07001478 child->removeRemoteSyncPoints();
Robert Carr9524cb32017-02-13 11:32:32 -08001479 }
Robert Carr7f619b22017-11-06 12:56:35 -08001480 }
Robert Carr9524cb32017-02-13 11:32:32 -08001481
1482 return true;
1483}
1484
chaviw5aedec92018-10-22 10:40:38 -07001485bool Layer::attachChildren() {
1486 bool changed = false;
1487 for (const sp<Layer>& child : mCurrentChildren) {
1488 sp<Client> parentClient = mClientRef.promote();
1489 sp<Client> client(child->mClientRef.promote());
1490 if (client != nullptr && parentClient != client) {
1491 if (child->mLayerDetached) {
1492 child->mLayerDetached = false;
1493 changed = true;
1494 }
1495 changed |= child->attachChildren();
1496 }
1497 }
1498
1499 return changed;
1500}
1501
Peiyong Lind3788632018-09-18 16:01:31 -07001502bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001503 static const mat4 identityMatrix = mat4();
1504
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001505 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001506 return false;
1507 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001508 ++mCurrentState.sequence;
1509 mCurrentState.colorTransform = matrix;
1510 mCurrentState.hasColorTransform = matrix != identityMatrix;
1511 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001512 setTransactionFlags(eTransactionNeeded);
1513 return true;
1514}
1515
chaviwf66724d2018-11-28 16:35:21 -08001516mat4 Layer::getColorTransform() const {
1517 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1518 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1519 colorTransform = parent->getColorTransform() * colorTransform;
1520 }
1521 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001522}
1523
1524bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001525 bool hasColorTransform = getDrawingState().hasColorTransform;
1526 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1527 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1528 }
1529 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001530}
1531
Chia-I Wu11481472018-05-04 10:43:19 -07001532bool Layer::isLegacyDataSpace() const {
1533 // return true when no higher bits are set
chaviw4244e032019-09-04 11:27:49 -07001534 return !(getDataSpace() &
1535 (ui::Dataspace::STANDARD_MASK | ui::Dataspace::TRANSFER_MASK |
1536 ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001537}
1538
Robert Carr1f0a16a2016-10-24 16:27:39 -07001539void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001540 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001541}
1542
chaviw301b1d82019-11-06 13:15:09 -08001543int32_t Layer::getZ(LayerVector::StateSet stateSet) const {
1544 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1545 const State& state = useDrawing ? mDrawingState : mCurrentState;
1546 return state.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001547}
1548
Robert Carr1c5481e2019-07-01 14:42:27 -07001549bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) const {
Robert Carr29abff82017-12-04 13:51:20 -08001550 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001551 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviwe5ac40f2019-09-24 16:36:55 -07001552 return state.isRelativeOf;
Robert Carr29abff82017-12-04 13:51:20 -08001553}
1554
David Sodman41fdfc92017-11-06 16:09:56 -08001555__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001556 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001557 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1558 "makeTraversalList received invalid stateSet");
1559 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1560 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001561 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001562
Robert Carr29abff82017-12-04 13:51:20 -08001563 if (state.zOrderRelatives.size() == 0) {
1564 *outSkipRelativeZUsers = true;
1565 return children;
1566 }
1567
chaviwfd462612018-05-31 16:11:27 -07001568 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001569 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001570 sp<Layer> strongRelative = weakRelative.promote();
1571 if (strongRelative != nullptr) {
1572 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001573 }
1574 }
1575
Dan Stoza412903f2017-04-27 13:42:17 -07001576 for (const sp<Layer>& child : children) {
chaviwe5ac40f2019-09-24 16:36:55 -07001577 if (child->usingRelativeZ(stateSet)) {
Robert Carr503c7042017-09-27 15:06:08 -07001578 continue;
1579 }
Robert Carrdb66e622017-04-10 16:55:57 -07001580 traverse.add(child);
1581 }
1582
1583 return traverse;
1584}
1585
Robert Carr1f0a16a2016-10-24 16:27:39 -07001586/**
Robert Carrdb66e622017-04-10 16:55:57 -07001587 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001588 */
Dan Stoza412903f2017-04-27 13:42:17 -07001589void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001590 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1591 // produce a new list for traversing, including our relatives, and not including our children
1592 // who are relatives of another surface. In the case that there are no relative Z,
1593 // makeTraversalList returns our children directly to avoid significant overhead.
1594 // However in this case we need to take the responsibility for filtering children which
1595 // are relatives of another surface here.
1596 bool skipRelativeZUsers = false;
1597 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001598
Robert Carr1f0a16a2016-10-24 16:27:39 -07001599 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001600 for (; i < list.size(); i++) {
1601 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001602 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1603 continue;
1604 }
1605
chaviw301b1d82019-11-06 13:15:09 -08001606 if (relative->getZ(stateSet) >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001607 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001608 }
Dan Stoza412903f2017-04-27 13:42:17 -07001609 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001610 }
Robert Carr29abff82017-12-04 13:51:20 -08001611
Dan Stoza412903f2017-04-27 13:42:17 -07001612 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001613 for (; i < list.size(); i++) {
1614 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001615
Robert Carr29abff82017-12-04 13:51:20 -08001616 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1617 continue;
1618 }
Dan Stoza412903f2017-04-27 13:42:17 -07001619 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001620 }
1621}
1622
1623/**
Robert Carrdb66e622017-04-10 16:55:57 -07001624 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001625 */
Dan Stoza412903f2017-04-27 13:42:17 -07001626void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1627 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001628 // See traverseInZOrder for documentation.
1629 bool skipRelativeZUsers = false;
1630 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001631
Robert Carr1f0a16a2016-10-24 16:27:39 -07001632 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001633 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001634 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001635
1636 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1637 continue;
1638 }
1639
chaviw301b1d82019-11-06 13:15:09 -08001640 if (relative->getZ(stateSet) < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001641 break;
1642 }
Dan Stoza412903f2017-04-27 13:42:17 -07001643 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001644 }
Dan Stoza412903f2017-04-27 13:42:17 -07001645 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001646 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001647 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001648
1649 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1650 continue;
1651 }
1652
Dan Stoza412903f2017-04-27 13:42:17 -07001653 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001654 }
1655}
1656
chaviw4b129c22018-04-09 16:19:43 -07001657LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1658 const std::vector<Layer*>& layersInTree) {
1659 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1660 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001661 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1662 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001663 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001664
chaviwfd462612018-05-31 16:11:27 -07001665 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001666 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1667 sp<Layer> strongRelative = weakRelative.promote();
1668 // Only add relative layers that are also descendents of the top most parent of the tree.
1669 // If a relative layer is not a descendent, then it should be ignored.
1670 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1671 traverse.add(strongRelative);
1672 }
1673 }
1674
1675 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001676 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001677 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1678 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1679 // the child here since it won't be added later as a relative.
1680 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1681 childState.zOrderRelativeOf.promote().get())) {
1682 continue;
1683 }
1684 traverse.add(child);
1685 }
1686
1687 return traverse;
1688}
1689
1690void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1691 LayerVector::StateSet stateSet,
1692 const LayerVector::Visitor& visitor) {
1693 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001694
1695 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001696 for (; i < list.size(); i++) {
1697 const auto& relative = list[i];
chaviw301b1d82019-11-06 13:15:09 -08001698 if (relative->getZ(stateSet) >= 0) {
chaviwa76b2712017-09-20 12:02:26 -07001699 break;
1700 }
chaviw4b129c22018-04-09 16:19:43 -07001701 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001702 }
chaviw4b129c22018-04-09 16:19:43 -07001703
chaviwa76b2712017-09-20 12:02:26 -07001704 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001705 for (; i < list.size(); i++) {
1706 const auto& relative = list[i];
1707 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001708 }
1709}
1710
chaviw4b129c22018-04-09 16:19:43 -07001711std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1712 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1713 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1714
1715 std::vector<Layer*> layersInTree = {this};
1716 for (size_t i = 0; i < children.size(); i++) {
1717 const auto& child = children[i];
1718 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1719 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1720 }
1721
1722 return layersInTree;
1723}
1724
1725void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1726 const LayerVector::Visitor& visitor) {
1727 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1728 std::sort(layersInTree.begin(), layersInTree.end());
1729 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1730}
1731
Peiyong Linefefaac2018-08-17 12:27:51 -07001732ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001733 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001734}
1735
chaviw13fdc492017-06-27 12:40:18 -07001736half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001737 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001738
chaviw13fdc492017-06-27 12:40:18 -07001739 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1740 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001741}
Robert Carr6452f122017-03-21 10:41:29 -07001742
chaviw13fdc492017-06-27 12:40:18 -07001743half4 Layer::getColor() const {
1744 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001745 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001746}
Robert Carr6452f122017-03-21 10:41:29 -07001747
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001748Layer::RoundedCornerState Layer::getRoundedCornerState() const {
1749 const auto& p = mDrawingParent.promote();
1750 if (p != nullptr) {
Peiyong Lin27016a92019-03-29 17:36:08 +00001751 RoundedCornerState parentState = p->getRoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001752 if (parentState.radius > 0) {
1753 ui::Transform t = getActiveTransform(getDrawingState());
1754 t = t.inverse();
1755 parentState.cropRect = t.transform(parentState.cropRect);
1756 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1757 // but a transform matrix can define horizontal and vertical scales.
1758 // Let's take the average between both of them and pass into the shader, practically we
1759 // never do this type of transformation on windows anyway.
1760 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
1761 return parentState;
1762 }
1763 }
1764 const float radius = getDrawingState().cornerRadius;
Peiyong Linb9ff23e2019-04-08 11:04:59 -07001765 return radius > 0 && getCrop(getDrawingState()).isValid()
1766 ? RoundedCornerState(getCrop(getDrawingState()).toFloatRect(), radius)
1767 : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001768}
1769
Robert Carr1f0a16a2016-10-24 16:27:39 -07001770void Layer::commitChildList() {
1771 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1772 const auto& child = mCurrentChildren[i];
1773 child->commitChildList();
1774 }
1775 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001776 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001777}
1778
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001779static wp<Layer> extractLayerFromBinder(const wp<IBinder>& weakBinderHandle) {
1780 if (weakBinderHandle == nullptr) {
1781 return nullptr;
1782 }
1783 sp<IBinder> binderHandle = weakBinderHandle.promote();
1784 if (binderHandle == nullptr) {
1785 return nullptr;
1786 }
1787 sp<Layer::Handle> handle = static_cast<Layer::Handle*>(binderHandle.get());
1788 if (handle == nullptr) {
1789 return nullptr;
1790 }
1791 return handle->owner;
1792}
1793
Robert Carr720e5062018-07-30 17:45:14 -07001794void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001795 mCurrentState.inputInfo = info;
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001796 mCurrentState.touchableRegionCrop = extractLayerFromBinder(info.touchableRegionCropHandle);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001797 mCurrentState.modified = true;
1798 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07001799 setTransactionFlags(eTransactionNeeded);
1800}
1801
Vishnu Nair8406fd72019-07-30 11:29:31 -07001802void Layer::writeToProtoDrawingState(LayerProto* layerInfo, uint32_t traceFlags) const {
1803 ui::Transform transform = getTransform();
1804
1805 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
1806 for (const auto& pendingState : mPendingStatesSnapshot) {
1807 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
1808 if (barrierLayer != nullptr) {
1809 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
1810 barrierLayerProto->set_id(barrierLayer->sequence);
1811 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
1812 }
1813 }
1814
chaviwd62d3062019-09-04 14:48:02 -07001815 auto buffer = getBuffer();
Vishnu Nair8406fd72019-07-30 11:29:31 -07001816 if (buffer != nullptr) {
1817 LayerProtoHelper::writeToProto(buffer,
1818 [&]() { return layerInfo->mutable_active_buffer(); });
chaviw4244e032019-09-04 11:27:49 -07001819 LayerProtoHelper::writeToProto(ui::Transform(getBufferTransform()),
Vishnu Nair8406fd72019-07-30 11:29:31 -07001820 layerInfo->mutable_buffer_transform());
1821 }
1822 layerInfo->set_invalidate(contentDirty);
1823 layerInfo->set_is_protected(isProtected());
chaviw4244e032019-09-04 11:27:49 -07001824 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(getDataSpace())));
Vishnu Nair8406fd72019-07-30 11:29:31 -07001825 layerInfo->set_queued_frames(getQueuedFrameCount());
1826 layerInfo->set_refresh_pending(isBufferLatched());
1827 layerInfo->set_curr_frame(mCurrentFrameNumber);
1828 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
1829
1830 layerInfo->set_corner_radius(getRoundedCornerState().radius);
1831 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1832 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
1833 [&]() { return layerInfo->mutable_position(); });
1834 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
Lloyd Piquea2468662019-03-07 21:31:06 -08001835 LayerProtoHelper::writeToProto(debugGetVisibleRegionOnDefaultDisplay(),
Vishnu Nair8406fd72019-07-30 11:29:31 -07001836 [&]() { return layerInfo->mutable_visible_region(); });
1837 LayerProtoHelper::writeToProto(surfaceDamageRegion,
1838 [&]() { return layerInfo->mutable_damage_region(); });
1839 }
1840
1841 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
1842 LayerProtoHelper::writeToProto(mSourceBounds,
1843 [&]() { return layerInfo->mutable_source_bounds(); });
1844 LayerProtoHelper::writeToProto(mScreenBounds,
1845 [&]() { return layerInfo->mutable_screen_bounds(); });
1846 }
1847}
1848
1849void Layer::writeToProtoCommonState(LayerProto* layerInfo, LayerVector::StateSet stateSet,
1850 uint32_t traceFlags) const {
chaviw1d044282017-09-27 12:19:28 -07001851 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1852 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001853 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07001854
Peiyong Linefefaac2018-08-17 12:27:51 -07001855 ui::Transform requestedTransform = state.active_legacy.transform;
chaviw1d044282017-09-27 12:19:28 -07001856
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001857 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
1858 layerInfo->set_id(sequence);
1859 layerInfo->set_name(getName().c_str());
chaviw8a01fa42019-08-19 12:39:31 -07001860 layerInfo->set_type(getType());
chaviw1d044282017-09-27 12:19:28 -07001861
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001862 for (const auto& child : children) {
1863 layerInfo->add_children(child->sequence);
chaviw1d044282017-09-27 12:19:28 -07001864 }
chaviw1d044282017-09-27 12:19:28 -07001865
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001866 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1867 sp<Layer> strongRelative = weakRelative.promote();
1868 if (strongRelative != nullptr) {
1869 layerInfo->add_relatives(strongRelative->sequence);
1870 }
chaviwadc40c22018-07-10 16:57:27 -07001871 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001872
1873 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
1874 [&]() { return layerInfo->mutable_transparent_region(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001875
1876 layerInfo->set_layer_stack(getLayerStack());
1877 layerInfo->set_z(state.z);
1878
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001879 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(),
1880 [&]() {
1881 return layerInfo->mutable_requested_position();
1882 });
1883
1884 LayerProtoHelper::writeSizeToProto(state.active_legacy.w, state.active_legacy.h,
1885 [&]() { return layerInfo->mutable_size(); });
1886
1887 LayerProtoHelper::writeToProto(state.crop_legacy,
1888 [&]() { return layerInfo->mutable_crop(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001889
1890 layerInfo->set_is_opaque(isOpaque(state));
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001891
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001892
1893 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1894 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
1895 LayerProtoHelper::writeToProto(state.color,
1896 [&]() { return layerInfo->mutable_requested_color(); });
1897 layerInfo->set_flags(state.flags);
1898
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001899 LayerProtoHelper::writeToProto(requestedTransform,
1900 layerInfo->mutable_requested_transform());
1901
1902 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
1903 if (parent != nullptr) {
1904 layerInfo->set_parent(parent->sequence);
1905 } else {
1906 layerInfo->set_parent(-1);
1907 }
1908
1909 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1910 if (zOrderRelativeOf != nullptr) {
1911 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1912 } else {
1913 layerInfo->set_z_order_relative_of(-1);
1914 }
chaviwadc40c22018-07-10 16:57:27 -07001915 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001916
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001917 if (traceFlags & SurfaceTracing::TRACE_INPUT) {
1918 LayerProtoHelper::writeToProto(state.inputInfo, state.touchableRegionCrop,
1919 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001920 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001921
1922 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
1923 auto protoMap = layerInfo->mutable_metadata();
1924 for (const auto& entry : state.metadata.mMap) {
1925 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
1926 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001927 }
chaviw1d044282017-09-27 12:19:28 -07001928}
1929
Robert Carr2e102c92018-10-23 12:11:15 -07001930bool Layer::isRemovedFromCurrentState() const {
1931 return mRemovedFromCurrentState;
1932}
1933
Arthur Hungd20b2702019-01-14 18:16:16 +08001934InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001935 InputWindowInfo info = mDrawingState.inputInfo;
Robert Carr720e5062018-07-30 17:45:14 -07001936
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001937 if (info.displayId == ADISPLAY_ID_NONE) {
chaviwb15ea8a2019-09-03 12:40:22 -07001938 info.displayId = getLayerStack();
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001939 }
1940
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001941 ui::Transform t = getTransform();
1942 const float xScale = t.sx();
1943 const float yScale = t.sy();
Ady Abraham282f1d72019-07-24 18:05:56 -07001944 int32_t xSurfaceInset = info.surfaceInset;
1945 int32_t ySurfaceInset = info.surfaceInset;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001946 if (xScale != 1.0f || yScale != 1.0f) {
Ady Abraham282f1d72019-07-24 18:05:56 -07001947 info.windowXScale *= (xScale != 0.0f) ? 1.0f / xScale : 0.0f;
1948 info.windowYScale *= (yScale != 0.0f) ? 1.0f / yScale : 0.0f;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001949 info.touchableRegion.scaleSelf(xScale, yScale);
Ady Abraham282f1d72019-07-24 18:05:56 -07001950 xSurfaceInset = std::round(xSurfaceInset * xScale);
1951 ySurfaceInset = std::round(ySurfaceInset * yScale);
Riddle Hsu39d4aa52018-11-30 20:46:53 +08001952 }
Robert Carre07e1032018-11-26 12:55:53 -08001953
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001954 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001955 // If this is a portal window, set the touchableRegion to the layerBounds.
1956 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
1957 ? getBufferSize(getDrawingState())
1958 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08001959 if (!layerBounds.isValid()) {
1960 layerBounds = getCroppedBufferSize(getDrawingState());
1961 }
Vishnu Nair8033a492018-12-05 07:27:23 -08001962 layerBounds = t.transform(layerBounds);
Ady Abraham282f1d72019-07-24 18:05:56 -07001963
1964 // clamp inset to layer bounds
1965 xSurfaceInset = (xSurfaceInset >= 0) ? std::min(xSurfaceInset, layerBounds.getWidth() / 2) : 0;
1966 ySurfaceInset = (ySurfaceInset >= 0) ? std::min(ySurfaceInset, layerBounds.getHeight() / 2) : 0;
1967
Arthur Hung118b1142019-05-08 21:25:59 +08001968 layerBounds.inset(xSurfaceInset, ySurfaceInset, xSurfaceInset, ySurfaceInset);
Vishnu Nair8033a492018-12-05 07:27:23 -08001969
Arthur Hungd20b2702019-01-14 18:16:16 +08001970 // Input coordinate should match the layer bounds.
1971 info.frameLeft = layerBounds.left;
1972 info.frameTop = layerBounds.top;
1973 info.frameRight = layerBounds.right;
1974 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08001975
1976 // Position the touchable region relative to frame screen location and restrict it to frame
1977 // bounds.
1978 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08001979 info.visible = canReceiveInput();
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001980
1981 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
1982 if (info.replaceTouchableRegionWithCrop) {
1983 if (cropLayer == nullptr) {
1984 info.touchableRegion = Region(Rect{mScreenBounds});
1985 } else {
1986 info.touchableRegion = Region(Rect{cropLayer->mScreenBounds});
1987 }
1988 } else if (cropLayer != nullptr) {
1989 info.touchableRegion = info.touchableRegion.intersect(Rect{cropLayer->mScreenBounds});
1990 }
1991
Robert Carr720e5062018-07-30 17:45:14 -07001992 return info;
1993}
1994
1995bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001996 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07001997}
1998
Lloyd Piquefeb73d72018-12-04 17:23:44 -08001999std::shared_ptr<compositionengine::Layer> Layer::getCompositionLayer() const {
2000 return nullptr;
2001}
2002
chaviw3e727cd2019-01-31 13:41:05 -08002003bool Layer::canReceiveInput() const {
Vishnu Nair82353e92019-09-12 12:38:47 -07002004 return !isHiddenByPolicy();
chaviw3e727cd2019-01-31 13:41:05 -08002005}
2006
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002007compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
2008 const sp<const DisplayDevice>& display) const {
2009 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionLayer().get());
2010}
2011
Lloyd Piquea2468662019-03-07 21:31:06 -08002012Region Layer::debugGetVisibleRegionOnDefaultDisplay() const {
2013 sp<DisplayDevice> displayDevice = mFlinger->getDefaultDisplayDeviceLocked();
2014 if (displayDevice == nullptr) {
2015 return {};
2016 }
2017
2018 auto outputLayer = findOutputLayerForDisplay(displayDevice);
2019 if (outputLayer == nullptr) {
2020 return {};
2021 }
2022
2023 return outputLayer->getState().visibleRegion;
2024}
2025
chaviwb4c6e582019-08-16 14:35:07 -07002026void Layer::setInitialValuesForClone(const sp<Layer>& clonedFrom) {
2027 // copy drawing state from cloned layer
2028 mDrawingState = clonedFrom->mDrawingState;
2029 mClonedFrom = clonedFrom;
2030
2031 // TODO: (b/140756730) Ignore input for now since InputDispatcher doesn't support multiple
2032 // InputWindows per client token yet.
2033 mDrawingState.inputInfo.token = nullptr;
2034}
chaviw74b03172019-08-19 11:09:03 -07002035
2036void Layer::updateMirrorInfo() {
2037 if (mClonedChild == nullptr || !mClonedChild->isClonedFromAlive()) {
2038 // If mClonedChild is null, there is nothing to mirror. If isClonedFromAlive returns false,
2039 // it means that there is a clone, but the layer it was cloned from has been destroyed. In
2040 // that case, we want to delete the reference to the clone since we want it to get
2041 // destroyed. The root, this layer, will still be around since the client can continue
2042 // to hold a reference, but no cloned layers will be displayed.
2043 mClonedChild = nullptr;
2044 return;
2045 }
2046
2047 std::map<sp<Layer>, sp<Layer>> clonedLayersMap;
2048 // If the real layer exists and is in current state, add the clone as a child of the root.
2049 // There's no need to remove from drawingState when the layer is offscreen since currentState is
2050 // copied to drawingState for the root layer. So the clonedChild is always removed from
2051 // drawingState and then needs to be added back each traversal.
2052 if (!mClonedChild->getClonedFrom()->isRemovedFromCurrentState()) {
2053 addChildToDrawing(mClonedChild);
2054 }
2055
2056 mClonedChild->updateClonedDrawingState(clonedLayersMap);
2057 mClonedChild->updateClonedChildren(this, clonedLayersMap);
2058 mClonedChild->updateClonedRelatives(clonedLayersMap);
2059}
2060
2061void Layer::updateClonedDrawingState(std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2062 // If the layer the clone was cloned from is alive, copy the content of the drawingState
2063 // to the clone. If the real layer is no longer alive, continue traversing the children
2064 // since we may be able to pull out other children that are still alive.
2065 if (isClonedFromAlive()) {
2066 sp<Layer> clonedFrom = getClonedFrom();
2067 mDrawingState = clonedFrom->mDrawingState;
2068 // TODO: (b/140756730) Ignore input for now since InputDispatcher doesn't support multiple
2069 // InputWindows per client token yet.
2070 mDrawingState.inputInfo.token = nullptr;
2071 clonedLayersMap.emplace(clonedFrom, this);
2072 }
2073
2074 // The clone layer may have children in drawingState since they may have been created and
2075 // added from a previous request to updateMirorInfo. This is to ensure we don't recreate clones
2076 // that already exist, since we can just re-use them.
2077 // The drawingChildren will not get overwritten by the currentChildren since the clones are
2078 // not updated in the regular traversal. They are skipped since the root will lose the
2079 // reference to them when it copies its currentChildren to drawing.
2080 for (sp<Layer>& child : mDrawingChildren) {
2081 child->updateClonedDrawingState(clonedLayersMap);
2082 }
2083}
2084
2085void Layer::updateClonedChildren(const sp<Layer>& mirrorRoot,
2086 std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2087 mDrawingChildren.clear();
2088
2089 if (!isClonedFromAlive()) {
2090 return;
2091 }
2092
2093 sp<Layer> clonedFrom = getClonedFrom();
2094 for (sp<Layer>& child : clonedFrom->mDrawingChildren) {
2095 if (child == mirrorRoot) {
2096 // This is to avoid cyclical mirroring.
2097 continue;
2098 }
2099 sp<Layer> clonedChild = clonedLayersMap[child];
2100 if (clonedChild == nullptr) {
2101 clonedChild = child->createClone();
2102 clonedLayersMap[child] = clonedChild;
2103 }
2104 addChildToDrawing(clonedChild);
2105 clonedChild->updateClonedChildren(mirrorRoot, clonedLayersMap);
2106 }
2107}
2108
2109void Layer::updateClonedRelatives(std::map<sp<Layer>, sp<Layer>> clonedLayersMap) {
2110 mDrawingState.zOrderRelativeOf = nullptr;
2111 mDrawingState.zOrderRelatives.clear();
2112
2113 if (!isClonedFromAlive()) {
2114 return;
2115 }
2116
2117 sp<Layer> clonedFrom = getClonedFrom();
2118 for (wp<Layer>& relativeWeak : clonedFrom->mDrawingState.zOrderRelatives) {
2119 sp<Layer> relative = relativeWeak.promote();
2120 auto clonedRelative = clonedLayersMap[relative];
2121 if (clonedRelative != nullptr) {
2122 mDrawingState.zOrderRelatives.add(clonedRelative);
2123 }
2124 }
2125
2126 // Check if the relativeLayer for the real layer is part of the cloned hierarchy.
2127 // It's possible that the layer it's relative to is outside the requested cloned hierarchy.
2128 // In that case, we treat the layer as if the relativeOf has been removed. This way, it will
2129 // still traverse the children, but the layer with the missing relativeOf will not be shown
2130 // on screen.
2131 sp<Layer> relativeOf = clonedFrom->mDrawingState.zOrderRelativeOf.promote();
2132 sp<Layer> clonedRelativeOf = clonedLayersMap[relativeOf];
2133 if (clonedRelativeOf != nullptr) {
2134 mDrawingState.zOrderRelativeOf = clonedRelativeOf;
2135 }
2136
2137 for (sp<Layer>& child : mDrawingChildren) {
2138 child->updateClonedRelatives(clonedLayersMap);
2139 }
2140}
2141
2142void Layer::addChildToDrawing(const sp<Layer>& layer) {
2143 mDrawingChildren.add(layer);
2144 layer->mDrawingParent = this;
2145}
2146
Mathias Agopian13127d82013-03-05 17:47:11 -08002147// ---------------------------------------------------------------------------
2148
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002149}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002150
2151#if defined(__gl_h_)
2152#error "don't include gl/gl.h in this file"
2153#endif
2154
2155#if defined(__gl2_h_)
2156#error "don't include gl2/gl2.h in this file"
2157#endif