blob: 93c0b52e8bf529950f52fb02f5a5c3ca9df8feed [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 Laskowski75848362019-11-11 17:57:20 -0800123}
124
125void Layer::onFirstRef() {
126 mFlinger->onLayerFirstRef(this);
Dan Stoza436ccf32018-06-21 12:10:12 -0700127}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700128
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700129Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800130 sp<Client> c(mClientRef.promote());
131 if (c != 0) {
132 c->detachLayer(this);
133 }
134
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000135 mFrameTracker.logAndResetStats(mName);
chaviw74d90ad2019-04-26 14:45:26 -0700136 mFlinger->onLayerDestroyed(this);
Mathias Agopian96f08192010-06-02 23:28:45 -0700137}
138
Vishnu Nair0f085c62019-08-30 08:49:12 -0700139LayerCreationArgs::LayerCreationArgs(SurfaceFlinger* flinger, const sp<Client>& client,
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700140 std::string name, uint32_t w, uint32_t h, uint32_t flags,
Vishnu Nair0f085c62019-08-30 08:49:12 -0700141 LayerMetadata metadata)
142 : flinger(flinger),
143 client(client),
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700144 name(std::move(name)),
Vishnu Nair0f085c62019-08-30 08:49:12 -0700145 w(w),
146 h(h),
147 flags(flags),
148 metadata(std::move(metadata)) {
149 IPCThreadState* ipc = IPCThreadState::self();
150 callingPid = ipc->getCallingPid();
151 callingUid = ipc->getCallingUid();
152}
153
Mathias Agopian13127d82013-03-05 17:47:11 -0800154// ---------------------------------------------------------------------------
155// callbacks
156// ---------------------------------------------------------------------------
157
David Sodmaneb085e02017-10-05 18:49:04 -0700158/*
159 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
160 * Layer. So, the implementation is done in BufferLayer. When called on a
161 * ColorLayer object, it's essentially a NOP.
162 */
David Sodmaneb085e02017-10-05 18:49:04 -0700163void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800164
chaviw43cb3cb2019-05-31 15:23:41 -0700165void Layer::removeRemoteSyncPoints() {
166 for (auto& point : mRemoteSyncPoints) {
167 point->setTransactionApplied();
168 }
169 mRemoteSyncPoints.clear();
170
171 {
chaviw43cb3cb2019-05-31 15:23:41 -0700172 for (State pendingState : mPendingStates) {
173 pendingState.barrierLayer_legacy = nullptr;
174 }
175 }
176}
177
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700178void Layer::removeRelativeZ(const std::vector<Layer*>& layersInTree) {
179 if (mCurrentState.zOrderRelativeOf == nullptr) {
180 return;
181 }
Robert Carr2e102c92018-10-23 12:11:15 -0700182
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700183 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
184 if (strongRelative == nullptr) {
185 setZOrderRelativeOf(nullptr);
186 return;
187 }
188
189 if (!std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
190 strongRelative->removeZOrderRelative(this);
191 mFlinger->setTransactionFlags(eTraversalNeeded);
chaviw606e5cf2019-03-01 10:12:10 -0800192 setZOrderRelativeOf(nullptr);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700193 }
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700194}
195
196void Layer::removeFromCurrentState() {
197 mRemovedFromCurrentState = true;
Rob Carr4bba3702018-10-08 21:53:30 +0000198
Robert Carr2e102c92018-10-23 12:11:15 -0700199 // Since we are no longer reachable from CurrentState SurfaceFlinger
200 // will no longer invoke doTransaction for us, and so we will
201 // never finish applying transactions. We signal the sync point
202 // now so that another layer will not become indefinitely
203 // blocked.
chaviw43cb3cb2019-05-31 15:23:41 -0700204 removeRemoteSyncPoints();
Robert Carr2e102c92018-10-23 12:11:15 -0700205
206 {
207 Mutex::Autolock syncLock(mLocalSyncPointMutex);
208 for (auto& point : mLocalSyncPoints) {
209 point->setFrameAvailable();
210 }
211 mLocalSyncPoints.clear();
212 }
213
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800214 mFlinger->markLayerPendingRemovalLocked(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700215}
Chia-I Wu38512252017-05-17 14:36:16 -0700216
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700217void Layer::onRemovedFromCurrentState() {
218 auto layersInTree = getLayersInTree(LayerVector::StateSet::Current);
219 std::sort(layersInTree.begin(), layersInTree.end());
220 for (const auto& layer : layersInTree) {
221 layer->removeFromCurrentState();
222 layer->removeRelativeZ(layersInTree);
223 }
224}
225
chaviw61626f22018-11-15 16:26:27 -0800226void Layer::addToCurrentState() {
227 mRemovedFromCurrentState = false;
228
229 for (const auto& child : mCurrentChildren) {
230 child->addToCurrentState();
231 }
232}
233
Mathias Agopian13127d82013-03-05 17:47:11 -0800234// ---------------------------------------------------------------------------
235// set-up
236// ---------------------------------------------------------------------------
237
chaviw13fdc492017-06-27 12:40:18 -0700238bool Layer::getPremultipledAlpha() const {
239 return mPremultipliedAlpha;
240}
241
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700242sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800243 Mutex::Autolock _l(mLock);
Robert Carrc0df3122019-04-11 13:18:21 -0700244 if (mGetHandleCalled) {
245 ALOGE("Get handle called twice" );
246 return nullptr;
247 }
248 mGetHandleCalled = true;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700249 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800250}
251
252// ---------------------------------------------------------------------------
253// h/w composer set-up
254// ---------------------------------------------------------------------------
255
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700256static Rect reduce(const Rect& win, const Region& exclude) {
257 if (CC_LIKELY(exclude.isEmpty())) {
258 return win;
259 }
260 if (exclude.isRect()) {
261 return win.reduce(exclude.getBounds());
262 }
263 return Region(win).subtract(exclude).getBounds();
264}
265
Dan Stoza80d61162017-12-20 15:57:52 -0800266static FloatRect reduce(const FloatRect& win, const Region& exclude) {
267 if (CC_LIKELY(exclude.isEmpty())) {
268 return win;
269 }
270 // Convert through Rect (by rounding) for lack of FloatRegion
271 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
272}
273
Vishnu Nair4351ad52019-02-11 14:13:02 -0800274Rect Layer::getScreenBounds(bool reduceTransparentRegion) const {
Vishnu Nairf0c28512019-02-08 12:40:28 -0800275 if (!reduceTransparentRegion) {
276 return Rect{mScreenBounds};
277 }
278
279 FloatRect bounds = getBounds();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800280 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800281 // Transform to screen space.
282 bounds = t.transform(bounds);
283 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700284}
285
Vishnu Nair4351ad52019-02-11 14:13:02 -0800286FloatRect Layer::getBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000287 const State& s(getDrawingState());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800288 return getBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700289}
290
Vishnu Nairf0c28512019-02-08 12:40:28 -0800291FloatRect Layer::getBounds(const Region& activeTransparentRegion) const {
292 // Subtract the transparent region and snap to the bounds.
293 return reduce(mBounds, activeTransparentRegion);
294}
295
Vishnu Nairc652ff82019-03-15 12:48:54 -0700296ui::Transform Layer::getBufferScaleTransform() const {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800297 // If the layer is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
298 // it isFixedSize) then there may be additional scaling not accounted
Vishnu Nairc652ff82019-03-15 12:48:54 -0700299 // for in the layer transform.
chaviwd62d3062019-09-04 14:48:02 -0700300 if (!isFixedSize() || getBuffer() == nullptr) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700301 return {};
Vishnu Nair4351ad52019-02-11 14:13:02 -0800302 }
303
Marissa Wall290ad082019-03-06 13:23:47 -0800304 // If the layer is a buffer state layer, the active width and height
305 // could be infinite. In that case, return the effective transform.
306 const uint32_t activeWidth = getActiveWidth(getDrawingState());
307 const uint32_t activeHeight = getActiveHeight(getDrawingState());
308 if (activeWidth >= UINT32_MAX && activeHeight >= UINT32_MAX) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700309 return {};
Marissa Wall290ad082019-03-06 13:23:47 -0800310 }
311
chaviwd62d3062019-09-04 14:48:02 -0700312 int bufferWidth = getBuffer()->getWidth();
313 int bufferHeight = getBuffer()->getHeight();
Vishnu Nairc652ff82019-03-15 12:48:54 -0700314
chaviw4244e032019-09-04 11:27:49 -0700315 if (getBufferTransform() & NATIVE_WINDOW_TRANSFORM_ROT_90) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700316 std::swap(bufferWidth, bufferHeight);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800317 }
Vishnu Nairc652ff82019-03-15 12:48:54 -0700318
Marissa Wall290ad082019-03-06 13:23:47 -0800319 float sx = activeWidth / static_cast<float>(bufferWidth);
320 float sy = activeHeight / static_cast<float>(bufferHeight);
321
Vishnu Nair4351ad52019-02-11 14:13:02 -0800322 ui::Transform extraParentScaling;
323 extraParentScaling.set(sx, 0, 0, sy);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700324 return extraParentScaling;
325}
326
327ui::Transform Layer::getTransformWithScale(const ui::Transform& bufferScaleTransform) const {
328 // We need to mirror this scaling to child surfaces or we will break the contract where WM can
329 // treat child surfaces as pixels in the parent surface.
chaviwd62d3062019-09-04 14:48:02 -0700330 if (!isFixedSize() || getBuffer() == nullptr) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700331 return mEffectiveTransform;
332 }
333 return mEffectiveTransform * bufferScaleTransform;
334}
335
336FloatRect Layer::getBoundsPreScaling(const ui::Transform& bufferScaleTransform) const {
337 // We need the pre scaled layer bounds when computing child bounds to make sure the child is
338 // cropped to its parent layer after any buffer transform scaling is applied.
chaviwd62d3062019-09-04 14:48:02 -0700339 if (!isFixedSize() || getBuffer() == nullptr) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700340 return mBounds;
341 }
342 return bufferScaleTransform.inverse().transform(mBounds);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800343}
344
345void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform) {
346 const State& s(getDrawingState());
347
348 // Calculate effective layer transform
349 mEffectiveTransform = parentTransform * getActiveTransform(s);
350
351 // Transform parent bounds to layer space
352 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
353
Vishnu Nairc652ff82019-03-15 12:48:54 -0700354 // Calculate source bounds
Vishnu Nair4351ad52019-02-11 14:13:02 -0800355 mSourceBounds = computeSourceBounds(parentBounds);
356
357 // Calculate bounds by croping diplay frame with layer crop and parent bounds
358 FloatRect bounds = mSourceBounds;
359 const Rect layerCrop = getCrop(s);
360 if (!layerCrop.isEmpty()) {
361 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
362 }
363 bounds = bounds.intersect(parentBounds);
364
365 mBounds = bounds;
366 mScreenBounds = mEffectiveTransform.transform(mBounds);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700367
368 // Add any buffer scaling to the layer's children.
369 ui::Transform bufferScaleTransform = getBufferScaleTransform();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800370 for (const sp<Layer>& child : mDrawingChildren) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700371 child->computeBounds(getBoundsPreScaling(bufferScaleTransform),
372 getTransformWithScale(bufferScaleTransform));
Vishnu Nair4351ad52019-02-11 14:13:02 -0800373 }
374}
375
Vishnu Nair60356342018-11-13 13:00:45 -0800376Rect Layer::getCroppedBufferSize(const State& s) const {
377 Rect size = getBufferSize(s);
378 Rect crop = getCrop(s);
379 if (!crop.isEmpty() && size.isValid()) {
380 size.intersect(crop, &size);
381 } else if (!crop.isEmpty()) {
382 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700383 }
Vishnu Nair60356342018-11-13 13:00:45 -0800384 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800385}
386
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700387void Layer::setupRoundedCornersCropCoordinates(Rect win,
388 const FloatRect& roundedCornersCrop) const {
389 // Translate win by the rounded corners rect coordinates, to have all values in
390 // layer coordinate space.
391 win.left -= roundedCornersCrop.left;
392 win.right -= roundedCornersCrop.left;
393 win.top -= roundedCornersCrop.top;
394 win.bottom -= roundedCornersCrop.top;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700395}
396
Lloyd Piquec6687342019-03-07 21:34:57 -0800397void Layer::latchBasicGeometry(compositionengine::LayerFECompositionState& compositionState) const {
398 const auto& drawingState{getDrawingState()};
399 const uint32_t layerStack = getLayerStack();
400 const auto alpha = static_cast<float>(getAlpha());
401 const bool opaque = isOpaque(drawingState);
402 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
403
404 auto blendMode = Hwc2::IComposerClient::BlendMode::NONE;
405 if (!opaque || alpha != 1.0f) {
406 blendMode = mPremultipliedAlpha ? Hwc2::IComposerClient::BlendMode::PREMULTIPLIED
407 : Hwc2::IComposerClient::BlendMode::COVERAGE;
408 }
409
410 // TODO(b/121291683): Instead of filling in a passed-in compositionState
411 // structure, switch to Layer owning the structure and have
412 // CompositionEngine be able to get a reference to it.
413
414 compositionState.layerStackId =
415 (layerStack != ~0u) ? std::make_optional(layerStack) : std::nullopt;
416 compositionState.internalOnly = getPrimaryDisplayOnly();
417 compositionState.isVisible = isVisible();
418 compositionState.isOpaque = opaque && !usesRoundedCorners && alpha == 1.f;
419
420 compositionState.contentDirty = contentDirty;
421 contentDirty = false;
422
423 compositionState.geomLayerBounds = mBounds;
424 compositionState.geomLayerTransform = getTransform();
425 compositionState.geomInverseLayerTransform = compositionState.geomLayerTransform.inverse();
426 compositionState.transparentRegionHint = getActiveTransparentRegion(drawingState);
427
428 compositionState.blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
429 compositionState.alpha = alpha;
430}
431
Lloyd Piquea83776c2019-01-29 18:42:32 -0800432void Layer::latchGeometry(compositionengine::LayerFECompositionState& compositionState) const {
433 const auto& drawingState{getDrawingState()};
Mathias Agopian13127d82013-03-05 17:47:11 -0800434
Lloyd Piquea83776c2019-01-29 18:42:32 -0800435 int type = drawingState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
436 int appId = drawingState.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700437 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400438 if (parent.get()) {
439 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800440 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
441 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
Jiwen 'Steve' Cai736c74e2019-05-28 18:33:22 -0700442 if (parentType > 0 && parentAppId > 0) {
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800443 type = parentType;
444 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700445 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400446 }
447
Lloyd Piquea83776c2019-01-29 18:42:32 -0800448 compositionState.geomBufferSize = getBufferSize(drawingState);
chaviw4244e032019-09-04 11:27:49 -0700449 compositionState.geomContentCrop = getBufferCrop();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800450 compositionState.geomCrop = getCrop(drawingState);
chaviw4244e032019-09-04 11:27:49 -0700451 compositionState.geomBufferTransform = getBufferTransform();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800452 compositionState.geomBufferUsesDisplayInverseTransform = getTransformToDisplayInverse();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800453 compositionState.geomUsesSourceCrop = usesSourceCrop();
454 compositionState.isSecure = isSecure();
David Sodman15094112018-10-11 09:39:37 -0700455
Lloyd Piquea83776c2019-01-29 18:42:32 -0800456 compositionState.type = type;
457 compositionState.appId = appId;
458}
David Sodmanba340492018-08-05 21:51:33 -0700459
Lloyd Piquef5275482019-01-29 18:42:42 -0800460void Layer::latchPerFrameState(compositionengine::LayerFECompositionState& compositionState) const {
Lloyd Pique688abd42019-02-15 15:42:24 -0800461 const auto& drawingState{getDrawingState()};
Lloyd Piquef5275482019-01-29 18:42:42 -0800462 compositionState.forceClientComposition = false;
463
Lloyd Piquef5275482019-01-29 18:42:42 -0800464 compositionState.isColorspaceAgnostic = isColorSpaceAgnostic();
chaviw4244e032019-09-04 11:27:49 -0700465 compositionState.dataspace = getDataSpace();
Lloyd Piquef5275482019-01-29 18:42:42 -0800466 compositionState.colorTransform = getColorTransform();
467 compositionState.colorTransformIsIdentity = !hasColorTransform();
468 compositionState.surfaceDamage = surfaceDamageRegion;
Lloyd Pique688abd42019-02-15 15:42:24 -0800469 compositionState.hasProtectedContent = isProtected();
470
471 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
472 compositionState.isOpaque =
473 isOpaque(drawingState) && !usesRoundedCorners && getAlpha() == 1.0_hf;
Lloyd Piquef5275482019-01-29 18:42:42 -0800474
475 // Force client composition for special cases known only to the front-end.
Lloyd Pique688abd42019-02-15 15:42:24 -0800476 if (isHdrY410() || usesRoundedCorners) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800477 compositionState.forceClientComposition = true;
478 }
479}
480
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800481void Layer::latchCursorCompositionState(
482 compositionengine::LayerFECompositionState& compositionState) const {
483 // This gives us only the "orientation" component of the transform
484 const State& drawingState{getDrawingState()};
485
486 // Apply the layer's transform, followed by the display's global transform
487 // Here we're guaranteed that the layer's transform preserves rects
488 Rect win = getCroppedBufferSize(drawingState);
489 // Subtract the transparent region and snap to the bounds
490 Rect bounds = reduce(win, getActiveTransparentRegion(drawingState));
491 Rect frame(getTransform().transform(bounds));
492
493 compositionState.cursorFrame = frame;
494}
495
Lloyd Piquef16688f2019-02-19 17:47:57 -0800496bool Layer::onPreComposition(nsecs_t) {
497 return false;
498}
499
Lloyd Piquea83776c2019-01-29 18:42:32 -0800500void Layer::latchCompositionState(compositionengine::LayerFECompositionState& compositionState,
Lloyd Piquec6687342019-03-07 21:34:57 -0800501 compositionengine::LayerFE::StateSubset subset) const {
502 using StateSubset = compositionengine::LayerFE::StateSubset;
Lloyd Piquef5275482019-01-29 18:42:42 -0800503
Lloyd Piquec6687342019-03-07 21:34:57 -0800504 switch (subset) {
505 case StateSubset::BasicGeometry:
506 latchBasicGeometry(compositionState);
507 break;
508
509 case StateSubset::GeometryAndContent:
510 latchBasicGeometry(compositionState);
511 latchGeometry(compositionState);
512 latchPerFrameState(compositionState);
513 break;
514
515 case StateSubset::Content:
516 latchPerFrameState(compositionState);
517 break;
518 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800519}
Mathias Agopian29a367b2011-07-12 14:51:45 -0700520
Lloyd Piquea83776c2019-01-29 18:42:32 -0800521const char* Layer::getDebugName() const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700522 return mName.c_str();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800523}
524
Mathias Agopian13127d82013-03-05 17:47:11 -0800525// ---------------------------------------------------------------------------
526// drawing...
527// ---------------------------------------------------------------------------
528
Lloyd Piquef16688f2019-02-19 17:47:57 -0800529std::optional<renderengine::LayerSettings> Layer::prepareClientComposition(
530 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
531 if (!getCompositionLayer()) {
532 return {};
533 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800534
Vishnu Nair4351ad52019-02-11 14:13:02 -0800535 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000536 half alpha = getAlpha();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800537 renderengine::LayerSettings layerSettings;
538 layerSettings.geometry.boundaries = bounds;
539 if (targetSettings.useIdentityTransform) {
540 layerSettings.geometry.positionTransform = mat4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000541 } else {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800542 layerSettings.geometry.positionTransform = getTransform().asMatrix4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000543 }
544
545 if (hasColorTransform()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800546 layerSettings.colorTransform = getColorTransform();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000547 }
548
549 const auto roundedCornerState = getRoundedCornerState();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800550 layerSettings.geometry.roundedCornersRadius = roundedCornerState.radius;
551 layerSettings.geometry.roundedCornersCrop = roundedCornerState.cropRect;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000552
Lloyd Piquef16688f2019-02-19 17:47:57 -0800553 layerSettings.alpha = alpha;
chaviw4244e032019-09-04 11:27:49 -0700554 layerSettings.sourceDataspace = getDataSpace();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800555 return layerSettings;
Mathias Agopian13127d82013-03-05 17:47:11 -0800556}
557
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800558Hwc2::IComposerClient::Composition Layer::getCompositionType(
559 const sp<const DisplayDevice>& display) const {
560 const auto outputLayer = findOutputLayerForDisplay(display);
561 LOG_FATAL_IF(!outputLayer);
562 return outputLayer->getState().hwc ? (*outputLayer->getState().hwc).hwcCompositionType
563 : Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800564}
565
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800566bool Layer::getClearClientTarget(const sp<const DisplayDevice>& display) const {
567 const auto outputLayer = findOutputLayerForDisplay(display);
568 LOG_FATAL_IF(!outputLayer);
569 return outputLayer->getState().clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800570}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800571
Dan Stozacac35382016-01-27 12:21:06 -0800572bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
573 if (point->getFrameNumber() <= mCurrentFrameNumber) {
574 // Don't bother with a SyncPoint, since we've already latched the
575 // relevant frame
576 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700577 }
Robert Carr2e102c92018-10-23 12:11:15 -0700578 if (isRemovedFromCurrentState()) {
579 return false;
580 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700581
Dan Stozacac35382016-01-27 12:21:06 -0800582 Mutex::Autolock lock(mLocalSyncPointMutex);
583 mLocalSyncPoints.push_back(point);
584 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700585}
586
Mathias Agopian13127d82013-03-05 17:47:11 -0800587// ----------------------------------------------------------------------------
588// local state
589// ----------------------------------------------------------------------------
590
David Sodman41fdfc92017-11-06 16:09:56 -0800591bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800592 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700593 return (s.flags & layer_state_t::eLayerSecure);
594}
595
Mathias Agopian13127d82013-03-05 17:47:11 -0800596// ----------------------------------------------------------------------------
597// transaction
598// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800599
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800600void Layer::pushPendingState() {
601 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700602 return;
603 }
Alec Mourie60041e2019-06-14 18:59:51 -0700604 ATRACE_CALL();
Dan Stoza7dde5992015-05-22 09:51:44 -0700605
Dan Stoza7dde5992015-05-22 09:51:44 -0700606 // If this transaction is waiting on the receipt of a frame, generate a sync
607 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700608 // We don't allow installing sync points after we are removed from the current state
609 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800610 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
611 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800612 if (barrierLayer == nullptr) {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700613 ALOGE("[%s] Unable to promote barrier Layer.", getDebugName());
Dan Stoza7dde5992015-05-22 09:51:44 -0700614 // If we can't promote the layer we are intended to wait on,
615 // then it is expired or otherwise invalid. Allow this transaction
616 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800617 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800618 } else {
chaviw43cb3cb2019-05-31 15:23:41 -0700619 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy, this);
Robert Carr0d480722017-01-10 16:42:54 -0800620 if (barrierLayer->addSyncPoint(syncPoint)) {
Alec Mourie60041e2019-06-14 18:59:51 -0700621 std::stringstream ss;
622 ss << "Adding sync point " << mCurrentState.frameNumber_legacy;
623 ATRACE_NAME(ss.str().c_str());
Dan Stozacac35382016-01-27 12:21:06 -0800624 mRemoteSyncPoints.push_back(std::move(syncPoint));
625 } else {
626 // We already missed the frame we're supposed to synchronize
627 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800628 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800629 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700630 }
631
Dan Stoza7dde5992015-05-22 09:51:44 -0700632 // Wake us up to check if the frame has been received
633 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700634 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700635 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800636 mPendingStates.push_back(mCurrentState);
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700637 ATRACE_INT(mTransactionName.c_str(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700638}
639
Pablo Ceballos05289c22016-04-14 15:49:55 -0700640void Layer::popPendingState(State* stateToCommit) {
Alec Mourie60041e2019-06-14 18:59:51 -0700641 ATRACE_CALL();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800642 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700643
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800644 mPendingStates.removeAt(0);
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700645 ATRACE_INT(mTransactionName.c_str(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700646}
647
Pablo Ceballos05289c22016-04-14 15:49:55 -0700648bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700649 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800650 while (!mPendingStates.empty()) {
651 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700652 if (mRemoteSyncPoints.empty()) {
653 // If we don't have a sync point for this, apply it anyway. It
654 // will be visually wrong, but it should keep us from getting
655 // into too much trouble.
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700656 ALOGE("[%s] No local sync point found", getDebugName());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700657 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700658 stateUpdateAvailable = true;
659 continue;
660 }
661
Marissa Wallf58c14b2018-07-24 10:50:43 -0700662 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800663 mPendingStates[0].frameNumber_legacy) {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700664 ALOGE("[%s] Unexpected sync point frame number found", getDebugName());
Dan Stozacac35382016-01-27 12:21:06 -0800665
666 // Signal our end of the sync point and then dispose of it
667 mRemoteSyncPoints.front()->setTransactionApplied();
668 mRemoteSyncPoints.pop_front();
669 continue;
670 }
671
Dan Stoza7dde5992015-05-22 09:51:44 -0700672 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
Alec Mourie60041e2019-06-14 18:59:51 -0700673 ATRACE_NAME("frameIsAvailable");
Dan Stoza7dde5992015-05-22 09:51:44 -0700674 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700675 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700676 stateUpdateAvailable = true;
677
678 // Signal our end of the sync point and then dispose of it
679 mRemoteSyncPoints.front()->setTransactionApplied();
680 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800681 } else {
Alec Mourie60041e2019-06-14 18:59:51 -0700682 ATRACE_NAME("!frameIsAvailable");
Dan Stoza792e5292016-02-11 11:43:58 -0800683 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700684 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700685 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700686 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700687 stateUpdateAvailable = true;
688 }
689 }
690
691 // If we still have pending updates, wake SurfaceFlinger back up and point
692 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800693 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700694 setTransactionFlags(eTransactionNeeded);
695 mFlinger->setTransactionFlags(eTraversalNeeded);
696 }
697
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800698 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700699 return stateUpdateAvailable;
700}
701
Marissa Wall61c58622018-07-18 10:12:20 -0700702uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000703 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800704
Marissa Wall61c58622018-07-18 10:12:20 -0700705 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
706 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700707
David Sodmaneb085e02017-10-05 18:49:04 -0700708 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700709 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000710 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800711 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
712 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
713 " requested={ wh={%4u,%4u} }}\n"
714 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
715 " requested={ wh={%4u,%4u} }}\n",
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700716 this, getName().c_str(), getBufferTransform(), getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700717 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
718 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
719 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
720 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
721 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700722 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
723 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
724 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800725 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700726
Robert Carre392b552017-09-19 12:16:05 -0700727 // Don't let Layer::doTransaction update the drawing state
728 // if we have a pending resize, unless we are in fixed-size mode.
729 // the drawing state will be updated only once we receive a buffer
730 // with the correct size.
731 //
732 // In particular, we want to make sure the clip (which is part
733 // of the geometry state) is latched together with the size but is
734 // latched immediately when no resizing is involved.
735 //
736 // If a sideband stream is attached, however, we want to skip this
737 // optimization so that transactions aren't missed when a buffer
738 // never arrives
739 //
740 // In the case that we don't have a buffer we ignore other factors
741 // and avoid entering the resizePending state. At a high level the
742 // resizePending state is to avoid applying the state of the new buffer
743 // to the old buffer. However in the state where we don't have an old buffer
744 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700745 const bool resizePending =
746 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
747 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
chaviwd62d3062019-09-04 14:48:02 -0700748 (getBuffer() != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700749 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800750 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700751 flags |= eDontUpdateGeometryState;
752 }
753 }
754
Robert Carr7bf247e2017-05-18 14:02:49 -0700755 // Here we apply various requested geometry states, depending on our
756 // latching configuration. See Layer.h for a detailed discussion of
757 // how geometry latching is controlled.
758 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000759 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700760
Robert Carr7bf247e2017-05-18 14:02:49 -0700761 // There is an awkward asymmetry in the handling of the crop states in the position
762 // states, as can be seen below. Largely this arises from position and transform
763 // being stored in the same data structure while having different latching rules.
764 // b/38182305
765 //
Marissa Wall61c58622018-07-18 10:12:20 -0700766 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700767 // applyPendingStates in the presence of deferred transactions.
chaviw214c89d2019-09-04 16:03:53 -0700768 editCurrentState.active_legacy = editCurrentState.requested_legacy;
769 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Mathias Agopian13127d82013-03-05 17:47:11 -0800770 }
771
Marissa Wall61c58622018-07-18 10:12:20 -0700772 return flags;
773}
774
775uint32_t Layer::doTransaction(uint32_t flags) {
776 ATRACE_CALL();
777
chaviw5aedec92018-10-22 10:40:38 -0700778 if (mLayerDetached) {
Robert Carr321e83c2019-08-19 15:49:30 -0700779 // Ensure BLAST buffer callbacks are processed.
780 // detachChildren and mLayerDetached were implemented to avoid geometry updates
781 // to layers in the cases of animation. For BufferQueue layers buffers are still
782 // consumed as normal. This is useful as otherwise the client could get hung
783 // inevitably waiting on a buffer to return. We recreate this semantic for BufferQueue
784 // even though it is a little consistent. detachChildren is shortly slated for removal
785 // by the hierarchy mirroring work so we don't need to worry about it too much.
786 mDrawingState.callbackHandles = mCurrentState.callbackHandles;
787 mCurrentState.callbackHandles = {};
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800788 return flags;
789 }
790
791 if (mChildrenChanged) {
792 flags |= eVisibleRegion;
793 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -0700794 }
795
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800796 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +0000797 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -0700798 if (!applyPendingStates(&c)) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800799 return flags;
Marissa Wall61c58622018-07-18 10:12:20 -0700800 }
801
802 flags = doTransactionResize(flags, &c);
803
Alec Mourib416efd2018-09-06 21:01:59 +0000804 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700805
806 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800807 // invalidate and recompute the visible regions if needed
808 flags |= Layer::eVisibleRegion;
809 }
810
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700811 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800812 // invalidate and recompute the visible regions if needed
813 flags |= eVisibleRegion;
814 this->contentDirty = true;
815
816 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -0700817 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -0700818 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -0800819 }
820
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800821 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -0700822 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800823 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -0700824 }
825
Mathias Agopian13127d82013-03-05 17:47:11 -0800826 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -0700827 commitTransaction(c);
Vishnu Nair8406fd72019-07-30 11:29:31 -0700828 mPendingStatesSnapshot = mPendingStates;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800829 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -0800830 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800831}
832
Pablo Ceballos05289c22016-04-14 15:49:55 -0700833void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800834 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700835}
836
Mathias Agopian13127d82013-03-05 17:47:11 -0800837uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800838 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800839}
840
841uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800842 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -0800843}
844
chaviw214c89d2019-09-04 16:03:53 -0700845bool Layer::setPosition(float x, float y) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800846 if (mCurrentState.requested_legacy.transform.tx() == x &&
847 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -0800848 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800849 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -0700850
851 // We update the requested and active position simultaneously because
852 // we want to apply the position portion of the transform matrix immediately,
853 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800854 mCurrentState.requested_legacy.transform.set(x, y);
chaviw214c89d2019-09-04 16:03:53 -0700855 // Here we directly update the active state
856 // unlike other setters, because we store it within
857 // the transform, but use different latching rules.
858 // b/38182305
859 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr69663fb2016-03-27 19:59:19 -0700860
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800861 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800862 setTransactionFlags(eTransactionNeeded);
863 return true;
864}
Robert Carr82364e32016-05-15 11:27:47 -0700865
Robert Carr1f0a16a2016-10-24 16:27:39 -0700866bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
867 ssize_t idx = mCurrentChildren.indexOf(childLayer);
868 if (idx < 0) {
869 return false;
870 }
871 if (childLayer->setLayer(z)) {
872 mCurrentChildren.removeAt(idx);
873 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800874 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700875 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800876 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700877}
878
Robert Carr503c7042017-09-27 15:06:08 -0700879bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
880 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
881 ssize_t idx = mCurrentChildren.indexOf(childLayer);
882 if (idx < 0) {
883 return false;
884 }
885 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
886 mCurrentChildren.removeAt(idx);
887 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800888 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700889 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800890 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700891}
892
Robert Carrae060832016-11-28 10:51:00 -0800893bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800894 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
895 mCurrentState.sequence++;
896 mCurrentState.z = z;
897 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700898
899 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800900 if (mCurrentState.zOrderRelativeOf != nullptr) {
901 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700902 if (strongRelative != nullptr) {
903 strongRelative->removeZOrderRelative(this);
904 }
chaviw606e5cf2019-03-01 10:12:10 -0800905 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -0700906 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800907 setTransactionFlags(eTransactionNeeded);
908 return true;
909}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700910
Robert Carrdb66e622017-04-10 16:55:57 -0700911void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800912 mCurrentState.zOrderRelatives.remove(relative);
913 mCurrentState.sequence++;
914 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700915 setTransactionFlags(eTransactionNeeded);
916}
917
918void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800919 mCurrentState.zOrderRelatives.add(relative);
920 mCurrentState.modified = true;
921 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -0700922 setTransactionFlags(eTransactionNeeded);
923}
924
chaviw606e5cf2019-03-01 10:12:10 -0800925void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
926 mCurrentState.zOrderRelativeOf = relativeOf;
927 mCurrentState.sequence++;
928 mCurrentState.modified = true;
chaviwbdb8b802019-10-14 09:17:12 -0700929 mCurrentState.isRelativeOf = relativeOf != nullptr;
930
chaviw606e5cf2019-03-01 10:12:10 -0800931 setTransactionFlags(eTransactionNeeded);
932}
933
Robert Carr503d2bd2017-12-04 15:49:47 -0800934bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -0700935 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
936 if (handle == nullptr) {
937 return false;
938 }
939 sp<Layer> relative = handle->owner.promote();
940 if (relative == nullptr) {
941 return false;
942 }
943
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800944 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
945 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -0800946 return false;
947 }
948
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800949 mCurrentState.sequence++;
950 mCurrentState.modified = true;
951 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -0700952
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800953 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -0700954 if (oldZOrderRelativeOf != nullptr) {
955 oldZOrderRelativeOf->removeZOrderRelative(this);
956 }
chaviw606e5cf2019-03-01 10:12:10 -0800957 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -0700958 relative->addZOrderRelative(this);
959
960 setTransactionFlags(eTransactionNeeded);
961
962 return true;
963}
964
Mathias Agopian13127d82013-03-05 17:47:11 -0800965bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800966 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -0700967 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800968 mCurrentState.requested_legacy.w = w;
969 mCurrentState.requested_legacy.h = h;
970 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800971 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -0700972
973 // record the new size, from this point on, when the client request
974 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800975 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -0800976 return true;
977}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800978bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800979 if (mCurrentState.color.a == alpha) return false;
980 mCurrentState.sequence++;
981 mCurrentState.color.a = alpha;
982 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800983 setTransactionFlags(eTransactionNeeded);
984 return true;
985}
chaviw13fdc492017-06-27 12:40:18 -0700986
Valerie Haudd0b7572019-01-29 14:59:27 -0800987bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
988 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -0700989 return false;
Valerie Hauaa194562019-02-05 16:21:38 -0800990 }
991 mCurrentState.sequence++;
992 mCurrentState.modified = true;
993 setTransactionFlags(eTransactionNeeded);
994
995 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -0800996 // create background color layer if one does not yet exist
997 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700998 std::string name = mName + "BackgroundColorLayer";
Lloyd Pique958be492019-03-28 15:34:59 -0700999 mCurrentState.bgColorLayer = mFlinger->getFactory().createColorLayer(
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001000 LayerCreationArgs(mFlinger.get(), nullptr, std::move(name), 0, 0, flags,
1001 LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -07001002
Valerie Haudd0b7572019-01-29 14:59:27 -08001003 // add to child list
1004 addChild(mCurrentState.bgColorLayer);
1005 mFlinger->mLayersAdded = true;
1006 // set up SF to handle added color layer
1007 if (isRemovedFromCurrentState()) {
1008 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1009 }
1010 mFlinger->setTransactionFlags(eTransactionNeeded);
1011 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1012 mCurrentState.bgColorLayer->reparent(nullptr);
1013 mCurrentState.bgColorLayer = nullptr;
1014 return true;
1015 }
1016
1017 mCurrentState.bgColorLayer->setColor(color);
1018 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1019 mCurrentState.bgColorLayer->setAlpha(alpha);
1020 mCurrentState.bgColorLayer->setDataspace(dataspace);
1021
chaviw13fdc492017-06-27 12:40:18 -07001022 return true;
1023}
1024
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001025bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001026 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001027
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001028 mCurrentState.sequence++;
1029 mCurrentState.cornerRadius = cornerRadius;
1030 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001031 setTransactionFlags(eTransactionNeeded);
1032 return true;
1033}
1034
Robert Carrd4ae7f32018-06-07 16:10:57 -07001035bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1036 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001037 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001038 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1039
1040 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1041 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1042 return false;
1043 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001044 mCurrentState.sequence++;
1045 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1046 matrix.dsdy);
1047 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001048 setTransactionFlags(eTransactionNeeded);
1049 return true;
1050}
Marissa Wall61c58622018-07-18 10:12:20 -07001051
Mathias Agopian13127d82013-03-05 17:47:11 -08001052bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001053 mCurrentState.requestedTransparentRegion_legacy = transparent;
1054 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001055 setTransactionFlags(eTransactionNeeded);
1056 return true;
1057}
1058bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001059 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1060 if (mCurrentState.flags == newFlags) return false;
1061 mCurrentState.sequence++;
1062 mCurrentState.flags = newFlags;
1063 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001064 setTransactionFlags(eTransactionNeeded);
1065 return true;
1066}
Robert Carr99e27f02016-06-16 15:18:02 -07001067
chaviw214c89d2019-09-04 16:03:53 -07001068bool Layer::setCrop_legacy(const Rect& crop) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001069 if (mCurrentState.requestedCrop_legacy == crop) return false;
1070 mCurrentState.sequence++;
1071 mCurrentState.requestedCrop_legacy = crop;
chaviw214c89d2019-09-04 16:03:53 -07001072 mCurrentState.crop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001073
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001074 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001075 setTransactionFlags(eTransactionNeeded);
1076 return true;
1077}
Robert Carr8d5227b2017-03-16 15:41:03 -07001078
Robert Carrc3574f72016-03-24 12:19:32 -07001079bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001080 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001081 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001082 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001083 return true;
1084}
1085
Evan Roskyef876c92019-01-25 17:46:06 -08001086bool Layer::setMetadata(const LayerMetadata& data) {
1087 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001088 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001089 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001090 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001091 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001092}
1093
Mathias Agopian13127d82013-03-05 17:47:11 -08001094bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001095 if (mCurrentState.layerStack == layerStack) return false;
1096 mCurrentState.sequence++;
1097 mCurrentState.layerStack = layerStack;
1098 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001099 setTransactionFlags(eTransactionNeeded);
1100 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001101}
1102
Peiyong Linc502cb72019-03-01 15:00:23 -08001103bool Layer::setColorSpaceAgnostic(const bool agnostic) {
1104 if (mCurrentState.colorSpaceAgnostic == agnostic) {
1105 return false;
1106 }
1107 mCurrentState.sequence++;
1108 mCurrentState.colorSpaceAgnostic = agnostic;
1109 mCurrentState.modified = true;
1110 setTransactionFlags(eTransactionNeeded);
1111 return true;
1112}
1113
Robert Carr1f0a16a2016-10-24 16:27:39 -07001114uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001115 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001116 if (p == nullptr) {
1117 return getDrawingState().layerStack;
1118 }
1119 return p->getLayerStack();
1120}
1121
Marissa Wallf58c14b2018-07-24 10:50:43 -07001122void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Alec Mourie60041e2019-06-14 18:59:51 -07001123 ATRACE_CALL();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001124 mCurrentState.barrierLayer_legacy = barrierLayer;
1125 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001126 // We don't set eTransactionNeeded, because just receiving a deferral
1127 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001128 mCurrentState.modified = true;
1129 pushPendingState();
1130 mCurrentState.barrierLayer_legacy = nullptr;
1131 mCurrentState.frameNumber_legacy = 0;
1132 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001133}
1134
Marissa Wallf58c14b2018-07-24 10:50:43 -07001135void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001136 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001137 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001138}
1139
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001140// ----------------------------------------------------------------------------
1141// pageflip handling...
1142// ----------------------------------------------------------------------------
1143
Robert Carr1f0a16a2016-10-24 16:27:39 -07001144bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001145 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001146 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001147 if (parent != nullptr && parent->isHiddenByPolicy()) {
1148 return true;
1149 }
Robert Carr1c5481e2019-07-01 14:42:27 -07001150 if (usingRelativeZ(LayerVector::StateSet::Drawing)) {
1151 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
1152 if (zOrderRelativeOf != nullptr) {
1153 if (zOrderRelativeOf->isHiddenByPolicy()) {
1154 return true;
1155 }
1156 }
1157 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001158 return s.flags & layer_state_t::eLayerHidden;
1159}
1160
David Sodman41fdfc92017-11-06 16:09:56 -08001161uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001162 // TODO: should we do something special if mSecure is set?
1163 if (mProtectedByApp) {
1164 // need a hardware-protected path to external video sink
1165 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001166 }
Riley Andrews03414a12014-07-01 14:22:59 -07001167 if (mPotentialCursor) {
1168 usage |= GraphicBuffer::USAGE_CURSOR;
1169 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001170 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001171 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001172}
1173
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001174void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001175 uint32_t orientation = 0;
Vishnu Nair5eb3f062019-04-08 08:21:03 -07001176 // Disable setting transform hint if the debug flag is set.
1177 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001178 // The transform hint is used to improve performance, but we can
1179 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001180 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001181 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001182 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001183 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001184 orientation = 0;
1185 }
1186 }
David Sodmaneb085e02017-10-05 18:49:04 -07001187 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001188}
1189
Mathias Agopian13127d82013-03-05 17:47:11 -08001190// ----------------------------------------------------------------------------
1191// debugging
1192// ----------------------------------------------------------------------------
1193
Marissa Wall61c58622018-07-18 10:12:20 -07001194// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001195LayerDebugInfo Layer::getLayerDebugInfo() const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001196 using namespace std::string_literals;
1197
Kalle Raitaa099a242017-01-11 11:17:29 -08001198 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001199 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001200 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001201 sp<Layer> parent = getParent();
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001202 info.mParentName = parent ? parent->getName() : "none"s;
chaviw8a01fa42019-08-19 12:39:31 -07001203 info.mType = getType();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001204 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Lloyd Piquea2468662019-03-07 21:31:06 -08001205
1206 info.mVisibleRegion = debugGetVisibleRegionOnDefaultDisplay();
Kalle Raitaa099a242017-01-11 11:17:29 -08001207 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001208 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001209 info.mX = ds.active_legacy.transform.tx();
1210 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001211 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001212 info.mWidth = ds.active_legacy.w;
1213 info.mHeight = ds.active_legacy.h;
1214 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001215 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001216 info.mFlags = ds.flags;
1217 info.mPixelFormat = getPixelFormat();
chaviw4244e032019-09-04 11:27:49 -07001218 info.mDataSpace = static_cast<android_dataspace>(getDataSpace());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001219 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1220 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1221 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1222 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001223 {
chaviwd62d3062019-09-04 14:48:02 -07001224 sp<const GraphicBuffer> buffer = getBuffer();
David Sodman5b4cffc2017-11-23 13:20:29 -08001225 if (buffer != 0) {
1226 info.mActiveBufferWidth = buffer->getWidth();
1227 info.mActiveBufferHeight = buffer->getHeight();
1228 info.mActiveBufferStride = buffer->getStride();
1229 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001230 } else {
1231 info.mActiveBufferWidth = 0;
1232 info.mActiveBufferHeight = 0;
1233 info.mActiveBufferStride = 0;
1234 info.mActiveBufferFormat = 0;
1235 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001236 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001237 info.mNumQueuedFrames = getQueuedFrameCount();
1238 info.mRefreshPending = isBufferLatched();
1239 info.mIsOpaque = isOpaque(ds);
1240 info.mContentDirty = contentDirty;
1241 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001242}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001243
Yiwei Zhang5434a782018-12-05 18:06:32 -08001244void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001245 result.append("-------------------------------");
1246 result.append("-------------------------------");
1247 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001248 result.append(" Layer name\n");
1249 result.append(" Z | ");
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001250 result.append(" Window Type | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001251 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001252 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001253 result.append(" Disp Frame (LTRB) | ");
1254 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001255 result.append("-------------------------------");
1256 result.append("-------------------------------");
1257 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001258}
1259
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001260void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1261 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1262 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001263 return;
1264 }
1265
Yiwei Zhang5434a782018-12-05 18:06:32 -08001266 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001267 if (mName.length() > 77) {
1268 std::string shortened;
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001269 shortened.append(mName, 0, 36);
Dan Stozae22aec72016-08-01 13:20:59 -07001270 shortened.append("[...]");
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001271 shortened.append(mName, mName.length() - 36);
1272 name = std::move(shortened);
Dan Stozae22aec72016-08-01 13:20:59 -07001273 } else {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001274 name = mName;
Dan Stozae22aec72016-08-01 13:20:59 -07001275 }
1276
Yiwei Zhang5434a782018-12-05 18:06:32 -08001277 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001278
Alec Mourib416efd2018-09-06 21:01:59 +00001279 const State& layerState(getDrawingState());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001280 const auto& compositionState = outputLayer->getState();
1281
Chia-I Wu1e043612018-03-01 09:45:09 -08001282 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001283 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001284 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001285 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001286 }
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001287 StringAppendF(&result, " %10d | ", mWindowType);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001288 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
1289 StringAppendF(&result, "%10s | ",
1290 toString(getCompositionLayer() ? compositionState.bufferTransform
1291 : static_cast<Hwc2::Transform>(0))
1292 .c_str());
1293 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001294 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001295 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001296 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1297 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001298
Yichi Chen6ca35192018-05-29 12:20:43 +08001299 result.append("- - - - - - - - - - - - - - - -");
1300 result.append("- - - - - - - - - - - - - - - -");
1301 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001302}
Dan Stozae22aec72016-08-01 13:20:59 -07001303
Yiwei Zhang5434a782018-12-05 18:06:32 -08001304void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001305 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001306}
1307
Svetoslavd85084b2014-03-20 10:28:31 -07001308void Layer::clearFrameStats() {
1309 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001310}
1311
Jamie Gennis6547ff42013-07-16 20:12:42 -07001312void Layer::logFrameStats() {
1313 mFrameTracker.logAndResetStats(mName);
1314}
1315
Svetoslavd85084b2014-03-20 10:28:31 -07001316void Layer::getFrameStats(FrameStats* outStats) const {
1317 mFrameTracker.getStats(outStats);
1318}
1319
Yiwei Zhang5434a782018-12-05 18:06:32 -08001320void Layer::dumpFrameEvents(std::string& result) {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001321 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().c_str(), getType(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001322 Mutex::Autolock lock(mFrameEventHistoryMutex);
1323 mFrameEventHistory.checkFencesForCompletion();
1324 mFrameEventHistory.dump(result);
1325}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001326
Vishnu Nair0f085c62019-08-30 08:49:12 -07001327void Layer::dumpCallingUidPid(std::string& result) const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001328 StringAppendF(&result, "Layer %s (%s) pid:%d uid:%d\n", getName().c_str(), getType(),
Vishnu Nair0f085c62019-08-30 08:49:12 -07001329 mCallingPid, mCallingUid);
1330}
1331
Brian Anderson5ea5e592016-12-01 16:54:33 -08001332void Layer::onDisconnect() {
1333 Mutex::Autolock lock(mFrameEventHistoryMutex);
1334 mFrameEventHistory.onDisconnect();
Mikael Pessa90092f42019-08-26 17:22:04 -07001335 const int32_t layerID = getSequence();
1336 mFlinger->mTimeStats->onDestroy(layerID);
1337 mFlinger->mFrameTracer->onDestroy(layerID);
Brian Anderson5ea5e592016-12-01 16:54:33 -08001338}
1339
Brian Anderson3890c392016-07-25 12:48:08 -07001340void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001341 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001342 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001343 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1344 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001345 }
1346
Brian Andersond6927fb2016-07-23 23:37:30 -07001347 Mutex::Autolock lock(mFrameEventHistoryMutex);
1348 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001349 // If there are any unsignaled fences in the aquire timeline at this
1350 // point, the previously queued frame hasn't been latched yet. Go ahead
1351 // and try to get the signal time here so the syscall is taken out of
1352 // the main thread's critical path.
1353 mAcquireTimeline.updateSignalTimes();
1354 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001355 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001356 mFrameEventHistory.addQueue(*newTimestamps);
1357 }
1358
Brian Anderson3890c392016-07-25 12:48:08 -07001359 if (outDelta) {
1360 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001361 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001362}
Dan Stozae77c7662016-05-13 11:37:28 -07001363
Chia-I Wu98f1c102017-05-30 14:54:08 -07001364size_t Layer::getChildrenCount() const {
1365 size_t count = 0;
1366 for (const sp<Layer>& child : mCurrentChildren) {
1367 count += 1 + child->getChildrenCount();
1368 }
1369 return count;
1370}
1371
Robert Carr1f0a16a2016-10-24 16:27:39 -07001372void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001373 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001374 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001375
Robert Carr1f0a16a2016-10-24 16:27:39 -07001376 mCurrentChildren.add(layer);
1377 layer->setParent(this);
1378}
1379
1380ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001381 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001382 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001383
Robert Carr1323c952019-01-28 18:13:27 -08001384 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001385 return mCurrentChildren.remove(layer);
1386}
1387
Robert Carr1db73f62016-12-21 12:58:51 -08001388bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1389 sp<Handle> handle = nullptr;
1390 sp<Layer> newParent = nullptr;
1391 if (newParentHandle == nullptr) {
1392 return false;
1393 }
1394 handle = static_cast<Handle*>(newParentHandle.get());
1395 newParent = handle->owner.promote();
1396 if (newParent == nullptr) {
1397 ALOGE("Unable to promote Layer handle");
1398 return false;
1399 }
1400
chaviw5aedec92018-10-22 10:40:38 -07001401 if (attachChildren()) {
1402 setTransactionFlags(eTransactionNeeded);
1403 }
Robert Carr1db73f62016-12-21 12:58:51 -08001404 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001405 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001406 }
1407 mCurrentChildren.clear();
1408
1409 return true;
1410}
1411
Robert Carr15eae092018-03-23 13:43:53 -07001412void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001413 for (const sp<Layer>& child : mDrawingChildren) {
1414 child->mDrawingParent = newParent;
Vishnu Nairc652ff82019-03-15 12:48:54 -07001415 child->computeBounds(newParent->mBounds,
1416 newParent->getTransformWithScale(
1417 newParent->getBufferScaleTransform()));
Robert Carr578038f2018-03-09 12:25:24 -08001418 }
1419}
1420
chaviwf1961f72017-09-18 16:41:07 -07001421bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001422 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001423
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001424 // While layers are detached, we allow most operations
1425 // and simply halt performing the actual transaction. However
1426 // for reparent != null we would enter the mRemovedFromCurrentState
1427 // state, regardless of whether doTransaction was called, and
1428 // so we need to prevent the update here.
1429 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001430 return false;
1431 }
1432
Robert Carr54cf5b12019-01-25 14:02:28 -08001433 sp<Layer> newParent;
1434 if (newParentHandle != nullptr) {
1435 auto handle = static_cast<Handle*>(newParentHandle.get());
1436 newParent = handle->owner.promote();
1437 if (newParent == nullptr) {
1438 ALOGE("Unable to promote Layer handle");
1439 return false;
1440 }
1441 if (newParent == this) {
1442 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1443 return false;
1444 }
1445 }
1446
chaviwf1961f72017-09-18 16:41:07 -07001447 sp<Layer> parent = getParent();
1448 if (parent != nullptr) {
1449 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001450 }
1451
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001452 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001453 newParent->addChild(this);
1454 if (!newParent->isRemovedFromCurrentState()) {
1455 addToCurrentState();
1456 } else {
1457 onRemovedFromCurrentState();
1458 }
1459
1460 if (mLayerDetached) {
1461 mLayerDetached = false;
1462 callSetTransactionFlags = true;
1463 }
1464 } else {
1465 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001466 }
1467
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001468 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001469 setTransactionFlags(eTransactionNeeded);
1470 }
chaviw06178942017-07-27 10:25:59 -07001471 return true;
1472}
1473
Robert Carr9524cb32017-02-13 11:32:32 -08001474bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001475 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001476 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001477 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001478 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001479 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001480 child->detachChildren();
chaviw43cb3cb2019-05-31 15:23:41 -07001481 child->removeRemoteSyncPoints();
Robert Carr9524cb32017-02-13 11:32:32 -08001482 }
Robert Carr7f619b22017-11-06 12:56:35 -08001483 }
Robert Carr9524cb32017-02-13 11:32:32 -08001484
1485 return true;
1486}
1487
chaviw5aedec92018-10-22 10:40:38 -07001488bool Layer::attachChildren() {
1489 bool changed = false;
1490 for (const sp<Layer>& child : mCurrentChildren) {
1491 sp<Client> parentClient = mClientRef.promote();
1492 sp<Client> client(child->mClientRef.promote());
1493 if (client != nullptr && parentClient != client) {
1494 if (child->mLayerDetached) {
1495 child->mLayerDetached = false;
1496 changed = true;
1497 }
1498 changed |= child->attachChildren();
1499 }
1500 }
1501
1502 return changed;
1503}
1504
Peiyong Lind3788632018-09-18 16:01:31 -07001505bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001506 static const mat4 identityMatrix = mat4();
1507
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001508 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001509 return false;
1510 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001511 ++mCurrentState.sequence;
1512 mCurrentState.colorTransform = matrix;
1513 mCurrentState.hasColorTransform = matrix != identityMatrix;
1514 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001515 setTransactionFlags(eTransactionNeeded);
1516 return true;
1517}
1518
chaviwf66724d2018-11-28 16:35:21 -08001519mat4 Layer::getColorTransform() const {
1520 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1521 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1522 colorTransform = parent->getColorTransform() * colorTransform;
1523 }
1524 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001525}
1526
1527bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001528 bool hasColorTransform = getDrawingState().hasColorTransform;
1529 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1530 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1531 }
1532 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001533}
1534
Chia-I Wu11481472018-05-04 10:43:19 -07001535bool Layer::isLegacyDataSpace() const {
1536 // return true when no higher bits are set
chaviw4244e032019-09-04 11:27:49 -07001537 return !(getDataSpace() &
1538 (ui::Dataspace::STANDARD_MASK | ui::Dataspace::TRANSFER_MASK |
1539 ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001540}
1541
Robert Carr1f0a16a2016-10-24 16:27:39 -07001542void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001543 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001544}
1545
chaviw301b1d82019-11-06 13:15:09 -08001546int32_t Layer::getZ(LayerVector::StateSet stateSet) const {
1547 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1548 const State& state = useDrawing ? mDrawingState : mCurrentState;
1549 return state.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001550}
1551
Robert Carr1c5481e2019-07-01 14:42:27 -07001552bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) const {
Robert Carr29abff82017-12-04 13:51:20 -08001553 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001554 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviwe5ac40f2019-09-24 16:36:55 -07001555 return state.isRelativeOf;
Robert Carr29abff82017-12-04 13:51:20 -08001556}
1557
David Sodman41fdfc92017-11-06 16:09:56 -08001558__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001559 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001560 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1561 "makeTraversalList received invalid stateSet");
1562 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1563 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001564 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001565
Robert Carr29abff82017-12-04 13:51:20 -08001566 if (state.zOrderRelatives.size() == 0) {
1567 *outSkipRelativeZUsers = true;
1568 return children;
1569 }
1570
chaviwfd462612018-05-31 16:11:27 -07001571 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001572 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001573 sp<Layer> strongRelative = weakRelative.promote();
1574 if (strongRelative != nullptr) {
1575 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001576 }
1577 }
1578
Dan Stoza412903f2017-04-27 13:42:17 -07001579 for (const sp<Layer>& child : children) {
chaviwe5ac40f2019-09-24 16:36:55 -07001580 if (child->usingRelativeZ(stateSet)) {
Robert Carr503c7042017-09-27 15:06:08 -07001581 continue;
1582 }
Robert Carrdb66e622017-04-10 16:55:57 -07001583 traverse.add(child);
1584 }
1585
1586 return traverse;
1587}
1588
Robert Carr1f0a16a2016-10-24 16:27:39 -07001589/**
Robert Carrdb66e622017-04-10 16:55:57 -07001590 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001591 */
Dan Stoza412903f2017-04-27 13:42:17 -07001592void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001593 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1594 // produce a new list for traversing, including our relatives, and not including our children
1595 // who are relatives of another surface. In the case that there are no relative Z,
1596 // makeTraversalList returns our children directly to avoid significant overhead.
1597 // However in this case we need to take the responsibility for filtering children which
1598 // are relatives of another surface here.
1599 bool skipRelativeZUsers = false;
1600 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001601
Robert Carr1f0a16a2016-10-24 16:27:39 -07001602 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001603 for (; i < list.size(); i++) {
1604 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001605 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1606 continue;
1607 }
1608
chaviw301b1d82019-11-06 13:15:09 -08001609 if (relative->getZ(stateSet) >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001610 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001611 }
Dan Stoza412903f2017-04-27 13:42:17 -07001612 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001613 }
Robert Carr29abff82017-12-04 13:51:20 -08001614
Dan Stoza412903f2017-04-27 13:42:17 -07001615 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001616 for (; i < list.size(); i++) {
1617 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001618
Robert Carr29abff82017-12-04 13:51:20 -08001619 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1620 continue;
1621 }
Dan Stoza412903f2017-04-27 13:42:17 -07001622 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001623 }
1624}
1625
1626/**
Robert Carrdb66e622017-04-10 16:55:57 -07001627 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001628 */
Dan Stoza412903f2017-04-27 13:42:17 -07001629void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1630 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001631 // See traverseInZOrder for documentation.
1632 bool skipRelativeZUsers = false;
1633 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001634
Robert Carr1f0a16a2016-10-24 16:27:39 -07001635 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001636 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001637 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001638
1639 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1640 continue;
1641 }
1642
chaviw301b1d82019-11-06 13:15:09 -08001643 if (relative->getZ(stateSet) < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001644 break;
1645 }
Dan Stoza412903f2017-04-27 13:42:17 -07001646 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001647 }
Dan Stoza412903f2017-04-27 13:42:17 -07001648 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001649 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001650 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001651
1652 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1653 continue;
1654 }
1655
Dan Stoza412903f2017-04-27 13:42:17 -07001656 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001657 }
1658}
1659
chaviw4b129c22018-04-09 16:19:43 -07001660LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1661 const std::vector<Layer*>& layersInTree) {
1662 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1663 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001664 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1665 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001666 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001667
chaviwfd462612018-05-31 16:11:27 -07001668 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001669 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1670 sp<Layer> strongRelative = weakRelative.promote();
1671 // Only add relative layers that are also descendents of the top most parent of the tree.
1672 // If a relative layer is not a descendent, then it should be ignored.
1673 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1674 traverse.add(strongRelative);
1675 }
1676 }
1677
1678 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001679 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001680 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1681 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1682 // the child here since it won't be added later as a relative.
1683 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1684 childState.zOrderRelativeOf.promote().get())) {
1685 continue;
1686 }
1687 traverse.add(child);
1688 }
1689
1690 return traverse;
1691}
1692
1693void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1694 LayerVector::StateSet stateSet,
1695 const LayerVector::Visitor& visitor) {
1696 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001697
1698 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001699 for (; i < list.size(); i++) {
1700 const auto& relative = list[i];
chaviw301b1d82019-11-06 13:15:09 -08001701 if (relative->getZ(stateSet) >= 0) {
chaviwa76b2712017-09-20 12:02:26 -07001702 break;
1703 }
chaviw4b129c22018-04-09 16:19:43 -07001704 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001705 }
chaviw4b129c22018-04-09 16:19:43 -07001706
chaviwa76b2712017-09-20 12:02:26 -07001707 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001708 for (; i < list.size(); i++) {
1709 const auto& relative = list[i];
1710 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001711 }
1712}
1713
chaviw4b129c22018-04-09 16:19:43 -07001714std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1715 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1716 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1717
1718 std::vector<Layer*> layersInTree = {this};
1719 for (size_t i = 0; i < children.size(); i++) {
1720 const auto& child = children[i];
1721 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1722 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1723 }
1724
1725 return layersInTree;
1726}
1727
1728void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1729 const LayerVector::Visitor& visitor) {
1730 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1731 std::sort(layersInTree.begin(), layersInTree.end());
1732 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1733}
1734
Peiyong Linefefaac2018-08-17 12:27:51 -07001735ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001736 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001737}
1738
chaviw13fdc492017-06-27 12:40:18 -07001739half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001740 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001741
chaviw13fdc492017-06-27 12:40:18 -07001742 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1743 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001744}
Robert Carr6452f122017-03-21 10:41:29 -07001745
chaviw13fdc492017-06-27 12:40:18 -07001746half4 Layer::getColor() const {
1747 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001748 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001749}
Robert Carr6452f122017-03-21 10:41:29 -07001750
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001751Layer::RoundedCornerState Layer::getRoundedCornerState() const {
1752 const auto& p = mDrawingParent.promote();
1753 if (p != nullptr) {
Peiyong Lin27016a92019-03-29 17:36:08 +00001754 RoundedCornerState parentState = p->getRoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001755 if (parentState.radius > 0) {
1756 ui::Transform t = getActiveTransform(getDrawingState());
1757 t = t.inverse();
1758 parentState.cropRect = t.transform(parentState.cropRect);
1759 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1760 // but a transform matrix can define horizontal and vertical scales.
1761 // Let's take the average between both of them and pass into the shader, practically we
1762 // never do this type of transformation on windows anyway.
1763 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
1764 return parentState;
1765 }
1766 }
1767 const float radius = getDrawingState().cornerRadius;
Peiyong Linb9ff23e2019-04-08 11:04:59 -07001768 return radius > 0 && getCrop(getDrawingState()).isValid()
1769 ? RoundedCornerState(getCrop(getDrawingState()).toFloatRect(), radius)
1770 : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001771}
1772
Robert Carr1f0a16a2016-10-24 16:27:39 -07001773void Layer::commitChildList() {
1774 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1775 const auto& child = mCurrentChildren[i];
1776 child->commitChildList();
1777 }
1778 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001779 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001780}
1781
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001782static wp<Layer> extractLayerFromBinder(const wp<IBinder>& weakBinderHandle) {
1783 if (weakBinderHandle == nullptr) {
1784 return nullptr;
1785 }
1786 sp<IBinder> binderHandle = weakBinderHandle.promote();
1787 if (binderHandle == nullptr) {
1788 return nullptr;
1789 }
1790 sp<Layer::Handle> handle = static_cast<Layer::Handle*>(binderHandle.get());
1791 if (handle == nullptr) {
1792 return nullptr;
1793 }
1794 return handle->owner;
1795}
1796
Robert Carr720e5062018-07-30 17:45:14 -07001797void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001798 mCurrentState.inputInfo = info;
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001799 mCurrentState.touchableRegionCrop = extractLayerFromBinder(info.touchableRegionCropHandle);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001800 mCurrentState.modified = true;
1801 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07001802 setTransactionFlags(eTransactionNeeded);
1803}
1804
Vishnu Nair8406fd72019-07-30 11:29:31 -07001805void Layer::writeToProtoDrawingState(LayerProto* layerInfo, uint32_t traceFlags) const {
1806 ui::Transform transform = getTransform();
1807
1808 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
1809 for (const auto& pendingState : mPendingStatesSnapshot) {
1810 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
1811 if (barrierLayer != nullptr) {
1812 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
1813 barrierLayerProto->set_id(barrierLayer->sequence);
1814 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
1815 }
1816 }
1817
chaviwd62d3062019-09-04 14:48:02 -07001818 auto buffer = getBuffer();
Vishnu Nair8406fd72019-07-30 11:29:31 -07001819 if (buffer != nullptr) {
1820 LayerProtoHelper::writeToProto(buffer,
1821 [&]() { return layerInfo->mutable_active_buffer(); });
chaviw4244e032019-09-04 11:27:49 -07001822 LayerProtoHelper::writeToProto(ui::Transform(getBufferTransform()),
Vishnu Nair8406fd72019-07-30 11:29:31 -07001823 layerInfo->mutable_buffer_transform());
1824 }
1825 layerInfo->set_invalidate(contentDirty);
1826 layerInfo->set_is_protected(isProtected());
chaviw4244e032019-09-04 11:27:49 -07001827 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(getDataSpace())));
Vishnu Nair8406fd72019-07-30 11:29:31 -07001828 layerInfo->set_queued_frames(getQueuedFrameCount());
1829 layerInfo->set_refresh_pending(isBufferLatched());
1830 layerInfo->set_curr_frame(mCurrentFrameNumber);
1831 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
1832
1833 layerInfo->set_corner_radius(getRoundedCornerState().radius);
1834 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1835 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
1836 [&]() { return layerInfo->mutable_position(); });
1837 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
Lloyd Piquea2468662019-03-07 21:31:06 -08001838 LayerProtoHelper::writeToProto(debugGetVisibleRegionOnDefaultDisplay(),
Vishnu Nair8406fd72019-07-30 11:29:31 -07001839 [&]() { return layerInfo->mutable_visible_region(); });
1840 LayerProtoHelper::writeToProto(surfaceDamageRegion,
1841 [&]() { return layerInfo->mutable_damage_region(); });
1842 }
1843
1844 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
1845 LayerProtoHelper::writeToProto(mSourceBounds,
1846 [&]() { return layerInfo->mutable_source_bounds(); });
1847 LayerProtoHelper::writeToProto(mScreenBounds,
1848 [&]() { return layerInfo->mutable_screen_bounds(); });
1849 }
1850}
1851
1852void Layer::writeToProtoCommonState(LayerProto* layerInfo, LayerVector::StateSet stateSet,
1853 uint32_t traceFlags) const {
chaviw1d044282017-09-27 12:19:28 -07001854 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1855 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001856 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07001857
Peiyong Linefefaac2018-08-17 12:27:51 -07001858 ui::Transform requestedTransform = state.active_legacy.transform;
chaviw1d044282017-09-27 12:19:28 -07001859
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001860 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
1861 layerInfo->set_id(sequence);
1862 layerInfo->set_name(getName().c_str());
chaviw8a01fa42019-08-19 12:39:31 -07001863 layerInfo->set_type(getType());
chaviw1d044282017-09-27 12:19:28 -07001864
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001865 for (const auto& child : children) {
1866 layerInfo->add_children(child->sequence);
chaviw1d044282017-09-27 12:19:28 -07001867 }
chaviw1d044282017-09-27 12:19:28 -07001868
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001869 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1870 sp<Layer> strongRelative = weakRelative.promote();
1871 if (strongRelative != nullptr) {
1872 layerInfo->add_relatives(strongRelative->sequence);
1873 }
chaviwadc40c22018-07-10 16:57:27 -07001874 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001875
1876 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
1877 [&]() { return layerInfo->mutable_transparent_region(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001878
1879 layerInfo->set_layer_stack(getLayerStack());
1880 layerInfo->set_z(state.z);
1881
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001882 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(),
1883 [&]() {
1884 return layerInfo->mutable_requested_position();
1885 });
1886
1887 LayerProtoHelper::writeSizeToProto(state.active_legacy.w, state.active_legacy.h,
1888 [&]() { return layerInfo->mutable_size(); });
1889
1890 LayerProtoHelper::writeToProto(state.crop_legacy,
1891 [&]() { return layerInfo->mutable_crop(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001892
1893 layerInfo->set_is_opaque(isOpaque(state));
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001894
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001895
1896 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1897 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
1898 LayerProtoHelper::writeToProto(state.color,
1899 [&]() { return layerInfo->mutable_requested_color(); });
1900 layerInfo->set_flags(state.flags);
1901
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001902 LayerProtoHelper::writeToProto(requestedTransform,
1903 layerInfo->mutable_requested_transform());
1904
1905 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
1906 if (parent != nullptr) {
1907 layerInfo->set_parent(parent->sequence);
1908 } else {
1909 layerInfo->set_parent(-1);
1910 }
1911
1912 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1913 if (zOrderRelativeOf != nullptr) {
1914 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1915 } else {
1916 layerInfo->set_z_order_relative_of(-1);
1917 }
chaviwadc40c22018-07-10 16:57:27 -07001918 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001919
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001920 if (traceFlags & SurfaceTracing::TRACE_INPUT) {
1921 LayerProtoHelper::writeToProto(state.inputInfo, state.touchableRegionCrop,
1922 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001923 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001924
1925 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
1926 auto protoMap = layerInfo->mutable_metadata();
1927 for (const auto& entry : state.metadata.mMap) {
1928 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
1929 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001930 }
chaviw1d044282017-09-27 12:19:28 -07001931}
1932
Robert Carr2e102c92018-10-23 12:11:15 -07001933bool Layer::isRemovedFromCurrentState() const {
1934 return mRemovedFromCurrentState;
1935}
1936
Arthur Hungd20b2702019-01-14 18:16:16 +08001937InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001938 InputWindowInfo info = mDrawingState.inputInfo;
Robert Carr720e5062018-07-30 17:45:14 -07001939
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001940 if (info.displayId == ADISPLAY_ID_NONE) {
chaviwb15ea8a2019-09-03 12:40:22 -07001941 info.displayId = getLayerStack();
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001942 }
1943
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001944 ui::Transform t = getTransform();
1945 const float xScale = t.sx();
1946 const float yScale = t.sy();
Ady Abraham282f1d72019-07-24 18:05:56 -07001947 int32_t xSurfaceInset = info.surfaceInset;
1948 int32_t ySurfaceInset = info.surfaceInset;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001949 if (xScale != 1.0f || yScale != 1.0f) {
Ady Abraham282f1d72019-07-24 18:05:56 -07001950 info.windowXScale *= (xScale != 0.0f) ? 1.0f / xScale : 0.0f;
1951 info.windowYScale *= (yScale != 0.0f) ? 1.0f / yScale : 0.0f;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001952 info.touchableRegion.scaleSelf(xScale, yScale);
Ady Abraham282f1d72019-07-24 18:05:56 -07001953 xSurfaceInset = std::round(xSurfaceInset * xScale);
1954 ySurfaceInset = std::round(ySurfaceInset * yScale);
Riddle Hsu39d4aa52018-11-30 20:46:53 +08001955 }
Robert Carre07e1032018-11-26 12:55:53 -08001956
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001957 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001958 // If this is a portal window, set the touchableRegion to the layerBounds.
1959 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
1960 ? getBufferSize(getDrawingState())
1961 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08001962 if (!layerBounds.isValid()) {
1963 layerBounds = getCroppedBufferSize(getDrawingState());
1964 }
Vishnu Nair8033a492018-12-05 07:27:23 -08001965 layerBounds = t.transform(layerBounds);
Ady Abraham282f1d72019-07-24 18:05:56 -07001966
1967 // clamp inset to layer bounds
1968 xSurfaceInset = (xSurfaceInset >= 0) ? std::min(xSurfaceInset, layerBounds.getWidth() / 2) : 0;
1969 ySurfaceInset = (ySurfaceInset >= 0) ? std::min(ySurfaceInset, layerBounds.getHeight() / 2) : 0;
1970
Arthur Hung118b1142019-05-08 21:25:59 +08001971 layerBounds.inset(xSurfaceInset, ySurfaceInset, xSurfaceInset, ySurfaceInset);
Vishnu Nair8033a492018-12-05 07:27:23 -08001972
Arthur Hungd20b2702019-01-14 18:16:16 +08001973 // Input coordinate should match the layer bounds.
1974 info.frameLeft = layerBounds.left;
1975 info.frameTop = layerBounds.top;
1976 info.frameRight = layerBounds.right;
1977 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08001978
1979 // Position the touchable region relative to frame screen location and restrict it to frame
1980 // bounds.
1981 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08001982 info.visible = canReceiveInput();
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001983
1984 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
1985 if (info.replaceTouchableRegionWithCrop) {
1986 if (cropLayer == nullptr) {
1987 info.touchableRegion = Region(Rect{mScreenBounds});
1988 } else {
1989 info.touchableRegion = Region(Rect{cropLayer->mScreenBounds});
1990 }
1991 } else if (cropLayer != nullptr) {
1992 info.touchableRegion = info.touchableRegion.intersect(Rect{cropLayer->mScreenBounds});
1993 }
1994
Robert Carr720e5062018-07-30 17:45:14 -07001995 return info;
1996}
1997
1998bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001999 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002000}
2001
Lloyd Piquefeb73d72018-12-04 17:23:44 -08002002std::shared_ptr<compositionengine::Layer> Layer::getCompositionLayer() const {
2003 return nullptr;
2004}
2005
chaviw3e727cd2019-01-31 13:41:05 -08002006bool Layer::canReceiveInput() const {
Vishnu Nair82353e92019-09-12 12:38:47 -07002007 return !isHiddenByPolicy();
chaviw3e727cd2019-01-31 13:41:05 -08002008}
2009
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002010compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
2011 const sp<const DisplayDevice>& display) const {
2012 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionLayer().get());
2013}
2014
Lloyd Piquea2468662019-03-07 21:31:06 -08002015Region Layer::debugGetVisibleRegionOnDefaultDisplay() const {
2016 sp<DisplayDevice> displayDevice = mFlinger->getDefaultDisplayDeviceLocked();
2017 if (displayDevice == nullptr) {
2018 return {};
2019 }
2020
2021 auto outputLayer = findOutputLayerForDisplay(displayDevice);
2022 if (outputLayer == nullptr) {
2023 return {};
2024 }
2025
2026 return outputLayer->getState().visibleRegion;
2027}
2028
chaviwb4c6e582019-08-16 14:35:07 -07002029void Layer::setInitialValuesForClone(const sp<Layer>& clonedFrom) {
2030 // copy drawing state from cloned layer
2031 mDrawingState = clonedFrom->mDrawingState;
2032 mClonedFrom = clonedFrom;
2033
2034 // TODO: (b/140756730) Ignore input for now since InputDispatcher doesn't support multiple
2035 // InputWindows per client token yet.
2036 mDrawingState.inputInfo.token = nullptr;
2037}
chaviw74b03172019-08-19 11:09:03 -07002038
2039void Layer::updateMirrorInfo() {
2040 if (mClonedChild == nullptr || !mClonedChild->isClonedFromAlive()) {
2041 // If mClonedChild is null, there is nothing to mirror. If isClonedFromAlive returns false,
2042 // it means that there is a clone, but the layer it was cloned from has been destroyed. In
2043 // that case, we want to delete the reference to the clone since we want it to get
2044 // destroyed. The root, this layer, will still be around since the client can continue
2045 // to hold a reference, but no cloned layers will be displayed.
2046 mClonedChild = nullptr;
2047 return;
2048 }
2049
2050 std::map<sp<Layer>, sp<Layer>> clonedLayersMap;
2051 // If the real layer exists and is in current state, add the clone as a child of the root.
2052 // There's no need to remove from drawingState when the layer is offscreen since currentState is
2053 // copied to drawingState for the root layer. So the clonedChild is always removed from
2054 // drawingState and then needs to be added back each traversal.
2055 if (!mClonedChild->getClonedFrom()->isRemovedFromCurrentState()) {
2056 addChildToDrawing(mClonedChild);
2057 }
2058
2059 mClonedChild->updateClonedDrawingState(clonedLayersMap);
2060 mClonedChild->updateClonedChildren(this, clonedLayersMap);
2061 mClonedChild->updateClonedRelatives(clonedLayersMap);
2062}
2063
2064void Layer::updateClonedDrawingState(std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2065 // If the layer the clone was cloned from is alive, copy the content of the drawingState
2066 // to the clone. If the real layer is no longer alive, continue traversing the children
2067 // since we may be able to pull out other children that are still alive.
2068 if (isClonedFromAlive()) {
2069 sp<Layer> clonedFrom = getClonedFrom();
2070 mDrawingState = clonedFrom->mDrawingState;
2071 // TODO: (b/140756730) Ignore input for now since InputDispatcher doesn't support multiple
2072 // InputWindows per client token yet.
2073 mDrawingState.inputInfo.token = nullptr;
2074 clonedLayersMap.emplace(clonedFrom, this);
2075 }
2076
2077 // The clone layer may have children in drawingState since they may have been created and
2078 // added from a previous request to updateMirorInfo. This is to ensure we don't recreate clones
2079 // that already exist, since we can just re-use them.
2080 // The drawingChildren will not get overwritten by the currentChildren since the clones are
2081 // not updated in the regular traversal. They are skipped since the root will lose the
2082 // reference to them when it copies its currentChildren to drawing.
2083 for (sp<Layer>& child : mDrawingChildren) {
2084 child->updateClonedDrawingState(clonedLayersMap);
2085 }
2086}
2087
2088void Layer::updateClonedChildren(const sp<Layer>& mirrorRoot,
2089 std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2090 mDrawingChildren.clear();
2091
2092 if (!isClonedFromAlive()) {
2093 return;
2094 }
2095
2096 sp<Layer> clonedFrom = getClonedFrom();
2097 for (sp<Layer>& child : clonedFrom->mDrawingChildren) {
2098 if (child == mirrorRoot) {
2099 // This is to avoid cyclical mirroring.
2100 continue;
2101 }
2102 sp<Layer> clonedChild = clonedLayersMap[child];
2103 if (clonedChild == nullptr) {
2104 clonedChild = child->createClone();
2105 clonedLayersMap[child] = clonedChild;
2106 }
2107 addChildToDrawing(clonedChild);
2108 clonedChild->updateClonedChildren(mirrorRoot, clonedLayersMap);
2109 }
2110}
2111
2112void Layer::updateClonedRelatives(std::map<sp<Layer>, sp<Layer>> clonedLayersMap) {
2113 mDrawingState.zOrderRelativeOf = nullptr;
2114 mDrawingState.zOrderRelatives.clear();
2115
2116 if (!isClonedFromAlive()) {
2117 return;
2118 }
2119
2120 sp<Layer> clonedFrom = getClonedFrom();
2121 for (wp<Layer>& relativeWeak : clonedFrom->mDrawingState.zOrderRelatives) {
2122 sp<Layer> relative = relativeWeak.promote();
2123 auto clonedRelative = clonedLayersMap[relative];
2124 if (clonedRelative != nullptr) {
2125 mDrawingState.zOrderRelatives.add(clonedRelative);
2126 }
2127 }
2128
2129 // Check if the relativeLayer for the real layer is part of the cloned hierarchy.
2130 // It's possible that the layer it's relative to is outside the requested cloned hierarchy.
2131 // In that case, we treat the layer as if the relativeOf has been removed. This way, it will
2132 // still traverse the children, but the layer with the missing relativeOf will not be shown
2133 // on screen.
2134 sp<Layer> relativeOf = clonedFrom->mDrawingState.zOrderRelativeOf.promote();
2135 sp<Layer> clonedRelativeOf = clonedLayersMap[relativeOf];
2136 if (clonedRelativeOf != nullptr) {
2137 mDrawingState.zOrderRelativeOf = clonedRelativeOf;
2138 }
2139
2140 for (sp<Layer>& child : mDrawingChildren) {
2141 child->updateClonedRelatives(clonedLayersMap);
2142 }
2143}
2144
2145void Layer::addChildToDrawing(const sp<Layer>& layer) {
2146 mDrawingChildren.add(layer);
2147 layer->mDrawingParent = this;
2148}
2149
Mathias Agopian13127d82013-03-05 17:47:11 -08002150// ---------------------------------------------------------------------------
2151
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002152}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002153
2154#if defined(__gl_h_)
2155#error "don't include gl/gl.h in this file"
2156#endif
2157
2158#if defined(__gl2_h_)
2159#error "don't include gl2/gl2.h in this file"
2160#endif