blob: 00d932d705c09b37679c8829f2a43e3927522241 [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
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700345void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform,
346 float parentShadowRadius) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800347 const State& s(getDrawingState());
348
349 // Calculate effective layer transform
350 mEffectiveTransform = parentTransform * getActiveTransform(s);
351
352 // Transform parent bounds to layer space
353 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
354
Vishnu Nairc652ff82019-03-15 12:48:54 -0700355 // Calculate source bounds
Vishnu Nair4351ad52019-02-11 14:13:02 -0800356 mSourceBounds = computeSourceBounds(parentBounds);
357
358 // Calculate bounds by croping diplay frame with layer crop and parent bounds
359 FloatRect bounds = mSourceBounds;
360 const Rect layerCrop = getCrop(s);
361 if (!layerCrop.isEmpty()) {
362 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
363 }
364 bounds = bounds.intersect(parentBounds);
365
366 mBounds = bounds;
367 mScreenBounds = mEffectiveTransform.transform(mBounds);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700368
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700369 // Use the layer's own shadow radius if set. Otherwise get the radius from
370 // parent.
371 if (s.shadowRadius > 0.f) {
372 mEffectiveShadowRadius = s.shadowRadius;
373 } else {
374 mEffectiveShadowRadius = parentShadowRadius;
375 }
376
377 // Shadow radius is passed down to only one layer so if the layer can draw shadows,
378 // don't pass it to its children.
379 const float childShadowRadius = canDrawShadows() ? 0.f : mEffectiveShadowRadius;
380
Vishnu Nairc652ff82019-03-15 12:48:54 -0700381 // Add any buffer scaling to the layer's children.
382 ui::Transform bufferScaleTransform = getBufferScaleTransform();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800383 for (const sp<Layer>& child : mDrawingChildren) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700384 child->computeBounds(getBoundsPreScaling(bufferScaleTransform),
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700385 getTransformWithScale(bufferScaleTransform), childShadowRadius);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800386 }
387}
388
Vishnu Nair60356342018-11-13 13:00:45 -0800389Rect Layer::getCroppedBufferSize(const State& s) const {
390 Rect size = getBufferSize(s);
391 Rect crop = getCrop(s);
392 if (!crop.isEmpty() && size.isValid()) {
393 size.intersect(crop, &size);
394 } else if (!crop.isEmpty()) {
395 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700396 }
Vishnu Nair60356342018-11-13 13:00:45 -0800397 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800398}
399
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700400void Layer::setupRoundedCornersCropCoordinates(Rect win,
401 const FloatRect& roundedCornersCrop) const {
402 // Translate win by the rounded corners rect coordinates, to have all values in
403 // layer coordinate space.
404 win.left -= roundedCornersCrop.left;
405 win.right -= roundedCornersCrop.left;
406 win.top -= roundedCornersCrop.top;
407 win.bottom -= roundedCornersCrop.top;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700408}
409
Lloyd Piquec6687342019-03-07 21:34:57 -0800410void Layer::latchBasicGeometry(compositionengine::LayerFECompositionState& compositionState) const {
411 const auto& drawingState{getDrawingState()};
412 const uint32_t layerStack = getLayerStack();
413 const auto alpha = static_cast<float>(getAlpha());
414 const bool opaque = isOpaque(drawingState);
415 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
416
417 auto blendMode = Hwc2::IComposerClient::BlendMode::NONE;
418 if (!opaque || alpha != 1.0f) {
419 blendMode = mPremultipliedAlpha ? Hwc2::IComposerClient::BlendMode::PREMULTIPLIED
420 : Hwc2::IComposerClient::BlendMode::COVERAGE;
421 }
422
423 // TODO(b/121291683): Instead of filling in a passed-in compositionState
424 // structure, switch to Layer owning the structure and have
425 // CompositionEngine be able to get a reference to it.
426
427 compositionState.layerStackId =
428 (layerStack != ~0u) ? std::make_optional(layerStack) : std::nullopt;
429 compositionState.internalOnly = getPrimaryDisplayOnly();
430 compositionState.isVisible = isVisible();
431 compositionState.isOpaque = opaque && !usesRoundedCorners && alpha == 1.f;
432
433 compositionState.contentDirty = contentDirty;
434 contentDirty = false;
435
436 compositionState.geomLayerBounds = mBounds;
437 compositionState.geomLayerTransform = getTransform();
438 compositionState.geomInverseLayerTransform = compositionState.geomLayerTransform.inverse();
439 compositionState.transparentRegionHint = getActiveTransparentRegion(drawingState);
440
441 compositionState.blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
442 compositionState.alpha = alpha;
443}
444
Lloyd Piquea83776c2019-01-29 18:42:32 -0800445void Layer::latchGeometry(compositionengine::LayerFECompositionState& compositionState) const {
446 const auto& drawingState{getDrawingState()};
Mathias Agopian13127d82013-03-05 17:47:11 -0800447
Lloyd Piquea83776c2019-01-29 18:42:32 -0800448 int type = drawingState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
449 int appId = drawingState.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700450 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400451 if (parent.get()) {
452 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800453 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
454 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
Jiwen 'Steve' Cai736c74e2019-05-28 18:33:22 -0700455 if (parentType > 0 && parentAppId > 0) {
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800456 type = parentType;
457 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700458 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400459 }
460
Lloyd Piquea83776c2019-01-29 18:42:32 -0800461 compositionState.geomBufferSize = getBufferSize(drawingState);
chaviw4244e032019-09-04 11:27:49 -0700462 compositionState.geomContentCrop = getBufferCrop();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800463 compositionState.geomCrop = getCrop(drawingState);
chaviw4244e032019-09-04 11:27:49 -0700464 compositionState.geomBufferTransform = getBufferTransform();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800465 compositionState.geomBufferUsesDisplayInverseTransform = getTransformToDisplayInverse();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800466 compositionState.geomUsesSourceCrop = usesSourceCrop();
467 compositionState.isSecure = isSecure();
David Sodman15094112018-10-11 09:39:37 -0700468
Lloyd Piquea83776c2019-01-29 18:42:32 -0800469 compositionState.type = type;
470 compositionState.appId = appId;
471}
David Sodmanba340492018-08-05 21:51:33 -0700472
Lloyd Piquef5275482019-01-29 18:42:42 -0800473void Layer::latchPerFrameState(compositionengine::LayerFECompositionState& compositionState) const {
Lloyd Pique688abd42019-02-15 15:42:24 -0800474 const auto& drawingState{getDrawingState()};
Lloyd Piquef5275482019-01-29 18:42:42 -0800475 compositionState.forceClientComposition = false;
476
Lloyd Piquef5275482019-01-29 18:42:42 -0800477 compositionState.isColorspaceAgnostic = isColorSpaceAgnostic();
chaviw4244e032019-09-04 11:27:49 -0700478 compositionState.dataspace = getDataSpace();
Lloyd Piquef5275482019-01-29 18:42:42 -0800479 compositionState.colorTransform = getColorTransform();
480 compositionState.colorTransformIsIdentity = !hasColorTransform();
481 compositionState.surfaceDamage = surfaceDamageRegion;
Lloyd Pique688abd42019-02-15 15:42:24 -0800482 compositionState.hasProtectedContent = isProtected();
483
484 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700485 const bool drawsShadows = mEffectiveShadowRadius != 0.f;
486
Lloyd Pique688abd42019-02-15 15:42:24 -0800487 compositionState.isOpaque =
488 isOpaque(drawingState) && !usesRoundedCorners && getAlpha() == 1.0_hf;
Lloyd Piquef5275482019-01-29 18:42:42 -0800489
490 // Force client composition for special cases known only to the front-end.
Vishnu Nairc97b8db2019-10-29 18:19:35 -0700491 if (isHdrY410() || usesRoundedCorners || drawsShadows) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800492 compositionState.forceClientComposition = true;
493 }
494}
495
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800496void Layer::latchCursorCompositionState(
497 compositionengine::LayerFECompositionState& compositionState) const {
498 // This gives us only the "orientation" component of the transform
499 const State& drawingState{getDrawingState()};
500
501 // Apply the layer's transform, followed by the display's global transform
502 // Here we're guaranteed that the layer's transform preserves rects
503 Rect win = getCroppedBufferSize(drawingState);
504 // Subtract the transparent region and snap to the bounds
505 Rect bounds = reduce(win, getActiveTransparentRegion(drawingState));
506 Rect frame(getTransform().transform(bounds));
507
508 compositionState.cursorFrame = frame;
509}
510
Lloyd Piquef16688f2019-02-19 17:47:57 -0800511bool Layer::onPreComposition(nsecs_t) {
512 return false;
513}
514
Lloyd Piquea83776c2019-01-29 18:42:32 -0800515void Layer::latchCompositionState(compositionengine::LayerFECompositionState& compositionState,
Lloyd Piquec6687342019-03-07 21:34:57 -0800516 compositionengine::LayerFE::StateSubset subset) const {
517 using StateSubset = compositionengine::LayerFE::StateSubset;
Lloyd Piquef5275482019-01-29 18:42:42 -0800518
Lloyd Piquec6687342019-03-07 21:34:57 -0800519 switch (subset) {
520 case StateSubset::BasicGeometry:
521 latchBasicGeometry(compositionState);
522 break;
523
524 case StateSubset::GeometryAndContent:
525 latchBasicGeometry(compositionState);
526 latchGeometry(compositionState);
527 latchPerFrameState(compositionState);
528 break;
529
530 case StateSubset::Content:
531 latchPerFrameState(compositionState);
532 break;
533 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800534}
Mathias Agopian29a367b2011-07-12 14:51:45 -0700535
Lloyd Piquea83776c2019-01-29 18:42:32 -0800536const char* Layer::getDebugName() const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700537 return mName.c_str();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800538}
539
Mathias Agopian13127d82013-03-05 17:47:11 -0800540// ---------------------------------------------------------------------------
541// drawing...
542// ---------------------------------------------------------------------------
543
Lloyd Piquef16688f2019-02-19 17:47:57 -0800544std::optional<renderengine::LayerSettings> Layer::prepareClientComposition(
545 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
546 if (!getCompositionLayer()) {
547 return {};
548 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800549
Vishnu Nair4351ad52019-02-11 14:13:02 -0800550 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000551 half alpha = getAlpha();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800552 renderengine::LayerSettings layerSettings;
553 layerSettings.geometry.boundaries = bounds;
554 if (targetSettings.useIdentityTransform) {
555 layerSettings.geometry.positionTransform = mat4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000556 } else {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800557 layerSettings.geometry.positionTransform = getTransform().asMatrix4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000558 }
559
560 if (hasColorTransform()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800561 layerSettings.colorTransform = getColorTransform();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000562 }
563
564 const auto roundedCornerState = getRoundedCornerState();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800565 layerSettings.geometry.roundedCornersRadius = roundedCornerState.radius;
566 layerSettings.geometry.roundedCornersCrop = roundedCornerState.cropRect;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000567
Lloyd Piquef16688f2019-02-19 17:47:57 -0800568 layerSettings.alpha = alpha;
chaviw4244e032019-09-04 11:27:49 -0700569 layerSettings.sourceDataspace = getDataSpace();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800570 return layerSettings;
Mathias Agopian13127d82013-03-05 17:47:11 -0800571}
572
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800573Hwc2::IComposerClient::Composition Layer::getCompositionType(
574 const sp<const DisplayDevice>& display) const {
575 const auto outputLayer = findOutputLayerForDisplay(display);
576 LOG_FATAL_IF(!outputLayer);
577 return outputLayer->getState().hwc ? (*outputLayer->getState().hwc).hwcCompositionType
578 : Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800579}
580
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800581bool Layer::getClearClientTarget(const sp<const DisplayDevice>& display) const {
582 const auto outputLayer = findOutputLayerForDisplay(display);
583 LOG_FATAL_IF(!outputLayer);
584 return outputLayer->getState().clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800585}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800586
Dan Stozacac35382016-01-27 12:21:06 -0800587bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
588 if (point->getFrameNumber() <= mCurrentFrameNumber) {
589 // Don't bother with a SyncPoint, since we've already latched the
590 // relevant frame
591 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700592 }
Robert Carr2e102c92018-10-23 12:11:15 -0700593 if (isRemovedFromCurrentState()) {
594 return false;
595 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700596
Dan Stozacac35382016-01-27 12:21:06 -0800597 Mutex::Autolock lock(mLocalSyncPointMutex);
598 mLocalSyncPoints.push_back(point);
599 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700600}
601
Mathias Agopian13127d82013-03-05 17:47:11 -0800602// ----------------------------------------------------------------------------
603// local state
604// ----------------------------------------------------------------------------
605
David Sodman41fdfc92017-11-06 16:09:56 -0800606bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800607 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700608 return (s.flags & layer_state_t::eLayerSecure);
609}
610
Mathias Agopian13127d82013-03-05 17:47:11 -0800611// ----------------------------------------------------------------------------
612// transaction
613// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800614
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800615void Layer::pushPendingState() {
616 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700617 return;
618 }
Alec Mourie60041e2019-06-14 18:59:51 -0700619 ATRACE_CALL();
Dan Stoza7dde5992015-05-22 09:51:44 -0700620
Dan Stoza7dde5992015-05-22 09:51:44 -0700621 // If this transaction is waiting on the receipt of a frame, generate a sync
622 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700623 // We don't allow installing sync points after we are removed from the current state
624 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800625 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
626 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800627 if (barrierLayer == nullptr) {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700628 ALOGE("[%s] Unable to promote barrier Layer.", getDebugName());
Dan Stoza7dde5992015-05-22 09:51:44 -0700629 // If we can't promote the layer we are intended to wait on,
630 // then it is expired or otherwise invalid. Allow this transaction
631 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800632 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800633 } else {
chaviw43cb3cb2019-05-31 15:23:41 -0700634 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy, this);
Robert Carr0d480722017-01-10 16:42:54 -0800635 if (barrierLayer->addSyncPoint(syncPoint)) {
Alec Mourie60041e2019-06-14 18:59:51 -0700636 std::stringstream ss;
637 ss << "Adding sync point " << mCurrentState.frameNumber_legacy;
638 ATRACE_NAME(ss.str().c_str());
Dan Stozacac35382016-01-27 12:21:06 -0800639 mRemoteSyncPoints.push_back(std::move(syncPoint));
640 } else {
641 // We already missed the frame we're supposed to synchronize
642 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800643 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800644 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700645 }
646
Dan Stoza7dde5992015-05-22 09:51:44 -0700647 // Wake us up to check if the frame has been received
648 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700649 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700650 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800651 mPendingStates.push_back(mCurrentState);
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700652 ATRACE_INT(mTransactionName.c_str(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700653}
654
Pablo Ceballos05289c22016-04-14 15:49:55 -0700655void Layer::popPendingState(State* stateToCommit) {
Alec Mourie60041e2019-06-14 18:59:51 -0700656 ATRACE_CALL();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800657 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700658
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800659 mPendingStates.removeAt(0);
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700660 ATRACE_INT(mTransactionName.c_str(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700661}
662
Pablo Ceballos05289c22016-04-14 15:49:55 -0700663bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700664 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800665 while (!mPendingStates.empty()) {
666 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700667 if (mRemoteSyncPoints.empty()) {
668 // If we don't have a sync point for this, apply it anyway. It
669 // will be visually wrong, but it should keep us from getting
670 // into too much trouble.
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700671 ALOGE("[%s] No local sync point found", getDebugName());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700672 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700673 stateUpdateAvailable = true;
674 continue;
675 }
676
Marissa Wallf58c14b2018-07-24 10:50:43 -0700677 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800678 mPendingStates[0].frameNumber_legacy) {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700679 ALOGE("[%s] Unexpected sync point frame number found", getDebugName());
Dan Stozacac35382016-01-27 12:21:06 -0800680
681 // Signal our end of the sync point and then dispose of it
682 mRemoteSyncPoints.front()->setTransactionApplied();
683 mRemoteSyncPoints.pop_front();
684 continue;
685 }
686
Dan Stoza7dde5992015-05-22 09:51:44 -0700687 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
Alec Mourie60041e2019-06-14 18:59:51 -0700688 ATRACE_NAME("frameIsAvailable");
Dan Stoza7dde5992015-05-22 09:51:44 -0700689 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700690 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700691 stateUpdateAvailable = true;
692
693 // Signal our end of the sync point and then dispose of it
694 mRemoteSyncPoints.front()->setTransactionApplied();
695 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800696 } else {
Alec Mourie60041e2019-06-14 18:59:51 -0700697 ATRACE_NAME("!frameIsAvailable");
Dan Stoza792e5292016-02-11 11:43:58 -0800698 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700699 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700700 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700701 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700702 stateUpdateAvailable = true;
703 }
704 }
705
706 // If we still have pending updates, wake SurfaceFlinger back up and point
707 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800708 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700709 setTransactionFlags(eTransactionNeeded);
710 mFlinger->setTransactionFlags(eTraversalNeeded);
711 }
712
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800713 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700714 return stateUpdateAvailable;
715}
716
Marissa Wall61c58622018-07-18 10:12:20 -0700717uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000718 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800719
Marissa Wall61c58622018-07-18 10:12:20 -0700720 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
721 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700722
David Sodmaneb085e02017-10-05 18:49:04 -0700723 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700724 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000725 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800726 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
727 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
728 " requested={ wh={%4u,%4u} }}\n"
729 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
730 " requested={ wh={%4u,%4u} }}\n",
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700731 this, getName().c_str(), getBufferTransform(), getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700732 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
733 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
734 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
735 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
736 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700737 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
738 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
739 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800740 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700741
Robert Carre392b552017-09-19 12:16:05 -0700742 // Don't let Layer::doTransaction update the drawing state
743 // if we have a pending resize, unless we are in fixed-size mode.
744 // the drawing state will be updated only once we receive a buffer
745 // with the correct size.
746 //
747 // In particular, we want to make sure the clip (which is part
748 // of the geometry state) is latched together with the size but is
749 // latched immediately when no resizing is involved.
750 //
751 // If a sideband stream is attached, however, we want to skip this
752 // optimization so that transactions aren't missed when a buffer
753 // never arrives
754 //
755 // In the case that we don't have a buffer we ignore other factors
756 // and avoid entering the resizePending state. At a high level the
757 // resizePending state is to avoid applying the state of the new buffer
758 // to the old buffer. However in the state where we don't have an old buffer
759 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700760 const bool resizePending =
761 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
762 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
chaviwd62d3062019-09-04 14:48:02 -0700763 (getBuffer() != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700764 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800765 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700766 flags |= eDontUpdateGeometryState;
767 }
768 }
769
Robert Carr7bf247e2017-05-18 14:02:49 -0700770 // Here we apply various requested geometry states, depending on our
771 // latching configuration. See Layer.h for a detailed discussion of
772 // how geometry latching is controlled.
773 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000774 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700775
Robert Carr7bf247e2017-05-18 14:02:49 -0700776 // There is an awkward asymmetry in the handling of the crop states in the position
777 // states, as can be seen below. Largely this arises from position and transform
778 // being stored in the same data structure while having different latching rules.
779 // b/38182305
780 //
Marissa Wall61c58622018-07-18 10:12:20 -0700781 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700782 // applyPendingStates in the presence of deferred transactions.
chaviw214c89d2019-09-04 16:03:53 -0700783 editCurrentState.active_legacy = editCurrentState.requested_legacy;
784 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Mathias Agopian13127d82013-03-05 17:47:11 -0800785 }
786
Marissa Wall61c58622018-07-18 10:12:20 -0700787 return flags;
788}
789
790uint32_t Layer::doTransaction(uint32_t flags) {
791 ATRACE_CALL();
792
chaviw5aedec92018-10-22 10:40:38 -0700793 if (mLayerDetached) {
Robert Carr321e83c2019-08-19 15:49:30 -0700794 // Ensure BLAST buffer callbacks are processed.
795 // detachChildren and mLayerDetached were implemented to avoid geometry updates
796 // to layers in the cases of animation. For BufferQueue layers buffers are still
797 // consumed as normal. This is useful as otherwise the client could get hung
798 // inevitably waiting on a buffer to return. We recreate this semantic for BufferQueue
799 // even though it is a little consistent. detachChildren is shortly slated for removal
800 // by the hierarchy mirroring work so we don't need to worry about it too much.
801 mDrawingState.callbackHandles = mCurrentState.callbackHandles;
802 mCurrentState.callbackHandles = {};
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800803 return flags;
804 }
805
806 if (mChildrenChanged) {
807 flags |= eVisibleRegion;
808 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -0700809 }
810
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800811 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +0000812 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -0700813 if (!applyPendingStates(&c)) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800814 return flags;
Marissa Wall61c58622018-07-18 10:12:20 -0700815 }
816
817 flags = doTransactionResize(flags, &c);
818
Alec Mourib416efd2018-09-06 21:01:59 +0000819 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700820
821 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800822 // invalidate and recompute the visible regions if needed
823 flags |= Layer::eVisibleRegion;
824 }
825
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700826 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800827 // invalidate and recompute the visible regions if needed
828 flags |= eVisibleRegion;
829 this->contentDirty = true;
830
831 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -0700832 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -0700833 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -0800834 }
835
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800836 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -0700837 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800838 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -0700839 }
840
Mathias Agopian13127d82013-03-05 17:47:11 -0800841 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -0700842 commitTransaction(c);
Vishnu Nair8406fd72019-07-30 11:29:31 -0700843 mPendingStatesSnapshot = mPendingStates;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800844 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -0800845 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800846}
847
Pablo Ceballos05289c22016-04-14 15:49:55 -0700848void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800849 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700850}
851
Mathias Agopian13127d82013-03-05 17:47:11 -0800852uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800853 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800854}
855
856uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800857 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -0800858}
859
chaviw214c89d2019-09-04 16:03:53 -0700860bool Layer::setPosition(float x, float y) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800861 if (mCurrentState.requested_legacy.transform.tx() == x &&
862 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -0800863 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800864 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -0700865
866 // We update the requested and active position simultaneously because
867 // we want to apply the position portion of the transform matrix immediately,
868 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800869 mCurrentState.requested_legacy.transform.set(x, y);
chaviw214c89d2019-09-04 16:03:53 -0700870 // Here we directly update the active state
871 // unlike other setters, because we store it within
872 // the transform, but use different latching rules.
873 // b/38182305
874 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr69663fb2016-03-27 19:59:19 -0700875
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800876 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800877 setTransactionFlags(eTransactionNeeded);
878 return true;
879}
Robert Carr82364e32016-05-15 11:27:47 -0700880
Robert Carr1f0a16a2016-10-24 16:27:39 -0700881bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
882 ssize_t idx = mCurrentChildren.indexOf(childLayer);
883 if (idx < 0) {
884 return false;
885 }
886 if (childLayer->setLayer(z)) {
887 mCurrentChildren.removeAt(idx);
888 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800889 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700890 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800891 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700892}
893
Robert Carr503c7042017-09-27 15:06:08 -0700894bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
895 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
896 ssize_t idx = mCurrentChildren.indexOf(childLayer);
897 if (idx < 0) {
898 return false;
899 }
900 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
901 mCurrentChildren.removeAt(idx);
902 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800903 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700904 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800905 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700906}
907
Robert Carrae060832016-11-28 10:51:00 -0800908bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800909 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
910 mCurrentState.sequence++;
911 mCurrentState.z = z;
912 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700913
914 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800915 if (mCurrentState.zOrderRelativeOf != nullptr) {
916 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700917 if (strongRelative != nullptr) {
918 strongRelative->removeZOrderRelative(this);
919 }
chaviw606e5cf2019-03-01 10:12:10 -0800920 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -0700921 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800922 setTransactionFlags(eTransactionNeeded);
923 return true;
924}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700925
Robert Carrdb66e622017-04-10 16:55:57 -0700926void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800927 mCurrentState.zOrderRelatives.remove(relative);
928 mCurrentState.sequence++;
929 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700930 setTransactionFlags(eTransactionNeeded);
931}
932
933void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800934 mCurrentState.zOrderRelatives.add(relative);
935 mCurrentState.modified = true;
936 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -0700937 setTransactionFlags(eTransactionNeeded);
938}
939
chaviw606e5cf2019-03-01 10:12:10 -0800940void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
941 mCurrentState.zOrderRelativeOf = relativeOf;
942 mCurrentState.sequence++;
943 mCurrentState.modified = true;
chaviwbdb8b802019-10-14 09:17:12 -0700944 mCurrentState.isRelativeOf = relativeOf != nullptr;
945
chaviw606e5cf2019-03-01 10:12:10 -0800946 setTransactionFlags(eTransactionNeeded);
947}
948
Robert Carr503d2bd2017-12-04 15:49:47 -0800949bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -0700950 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
951 if (handle == nullptr) {
952 return false;
953 }
954 sp<Layer> relative = handle->owner.promote();
955 if (relative == nullptr) {
956 return false;
957 }
958
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800959 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
960 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -0800961 return false;
962 }
963
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800964 mCurrentState.sequence++;
965 mCurrentState.modified = true;
966 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -0700967
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800968 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -0700969 if (oldZOrderRelativeOf != nullptr) {
970 oldZOrderRelativeOf->removeZOrderRelative(this);
971 }
chaviw606e5cf2019-03-01 10:12:10 -0800972 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -0700973 relative->addZOrderRelative(this);
974
975 setTransactionFlags(eTransactionNeeded);
976
977 return true;
978}
979
Mathias Agopian13127d82013-03-05 17:47:11 -0800980bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800981 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -0700982 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800983 mCurrentState.requested_legacy.w = w;
984 mCurrentState.requested_legacy.h = h;
985 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800986 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -0700987
988 // record the new size, from this point on, when the client request
989 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800990 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -0800991 return true;
992}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800993bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800994 if (mCurrentState.color.a == alpha) return false;
995 mCurrentState.sequence++;
996 mCurrentState.color.a = alpha;
997 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800998 setTransactionFlags(eTransactionNeeded);
999 return true;
1000}
chaviw13fdc492017-06-27 12:40:18 -07001001
Valerie Haudd0b7572019-01-29 14:59:27 -08001002bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1003 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001004 return false;
Valerie Hauaa194562019-02-05 16:21:38 -08001005 }
1006 mCurrentState.sequence++;
1007 mCurrentState.modified = true;
1008 setTransactionFlags(eTransactionNeeded);
1009
1010 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001011 // create background color layer if one does not yet exist
1012 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001013 std::string name = mName + "BackgroundColorLayer";
Lloyd Pique958be492019-03-28 15:34:59 -07001014 mCurrentState.bgColorLayer = mFlinger->getFactory().createColorLayer(
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001015 LayerCreationArgs(mFlinger.get(), nullptr, std::move(name), 0, 0, flags,
1016 LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -07001017
Valerie Haudd0b7572019-01-29 14:59:27 -08001018 // add to child list
1019 addChild(mCurrentState.bgColorLayer);
1020 mFlinger->mLayersAdded = true;
1021 // set up SF to handle added color layer
1022 if (isRemovedFromCurrentState()) {
1023 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1024 }
1025 mFlinger->setTransactionFlags(eTransactionNeeded);
1026 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1027 mCurrentState.bgColorLayer->reparent(nullptr);
1028 mCurrentState.bgColorLayer = nullptr;
1029 return true;
1030 }
1031
1032 mCurrentState.bgColorLayer->setColor(color);
1033 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1034 mCurrentState.bgColorLayer->setAlpha(alpha);
1035 mCurrentState.bgColorLayer->setDataspace(dataspace);
1036
chaviw13fdc492017-06-27 12:40:18 -07001037 return true;
1038}
1039
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001040bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001041 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001042
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001043 mCurrentState.sequence++;
1044 mCurrentState.cornerRadius = cornerRadius;
1045 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001046 setTransactionFlags(eTransactionNeeded);
1047 return true;
1048}
1049
Robert Carrd4ae7f32018-06-07 16:10:57 -07001050bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1051 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001052 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001053 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1054
1055 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1056 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1057 return false;
1058 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001059 mCurrentState.sequence++;
1060 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1061 matrix.dsdy);
1062 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001063 setTransactionFlags(eTransactionNeeded);
1064 return true;
1065}
Marissa Wall61c58622018-07-18 10:12:20 -07001066
Mathias Agopian13127d82013-03-05 17:47:11 -08001067bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001068 mCurrentState.requestedTransparentRegion_legacy = transparent;
1069 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001070 setTransactionFlags(eTransactionNeeded);
1071 return true;
1072}
1073bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001074 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1075 if (mCurrentState.flags == newFlags) return false;
1076 mCurrentState.sequence++;
1077 mCurrentState.flags = newFlags;
1078 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001079 setTransactionFlags(eTransactionNeeded);
1080 return true;
1081}
Robert Carr99e27f02016-06-16 15:18:02 -07001082
chaviw214c89d2019-09-04 16:03:53 -07001083bool Layer::setCrop_legacy(const Rect& crop) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001084 if (mCurrentState.requestedCrop_legacy == crop) return false;
1085 mCurrentState.sequence++;
1086 mCurrentState.requestedCrop_legacy = crop;
chaviw214c89d2019-09-04 16:03:53 -07001087 mCurrentState.crop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001088
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001089 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001090 setTransactionFlags(eTransactionNeeded);
1091 return true;
1092}
Robert Carr8d5227b2017-03-16 15:41:03 -07001093
Robert Carrc3574f72016-03-24 12:19:32 -07001094bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001095 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001096 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001097 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001098 return true;
1099}
1100
Evan Roskyef876c92019-01-25 17:46:06 -08001101bool Layer::setMetadata(const LayerMetadata& data) {
1102 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001103 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001104 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001105 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001106 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001107}
1108
Mathias Agopian13127d82013-03-05 17:47:11 -08001109bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001110 if (mCurrentState.layerStack == layerStack) return false;
1111 mCurrentState.sequence++;
1112 mCurrentState.layerStack = layerStack;
1113 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001114 setTransactionFlags(eTransactionNeeded);
1115 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001116}
1117
Peiyong Linc502cb72019-03-01 15:00:23 -08001118bool Layer::setColorSpaceAgnostic(const bool agnostic) {
1119 if (mCurrentState.colorSpaceAgnostic == agnostic) {
1120 return false;
1121 }
1122 mCurrentState.sequence++;
1123 mCurrentState.colorSpaceAgnostic = agnostic;
1124 mCurrentState.modified = true;
1125 setTransactionFlags(eTransactionNeeded);
1126 return true;
1127}
1128
Robert Carr1f0a16a2016-10-24 16:27:39 -07001129uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001130 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001131 if (p == nullptr) {
1132 return getDrawingState().layerStack;
1133 }
1134 return p->getLayerStack();
1135}
1136
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001137bool Layer::setShadowRadius(float shadowRadius) {
1138 if (mCurrentState.shadowRadius == shadowRadius) {
1139 return false;
1140 }
1141
1142 mCurrentState.sequence++;
1143 mCurrentState.shadowRadius = shadowRadius;
1144 mCurrentState.modified = true;
1145 setTransactionFlags(eTransactionNeeded);
1146 return true;
1147}
1148
Marissa Wallf58c14b2018-07-24 10:50:43 -07001149void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Alec Mourie60041e2019-06-14 18:59:51 -07001150 ATRACE_CALL();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001151 mCurrentState.barrierLayer_legacy = barrierLayer;
1152 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001153 // We don't set eTransactionNeeded, because just receiving a deferral
1154 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001155 mCurrentState.modified = true;
1156 pushPendingState();
1157 mCurrentState.barrierLayer_legacy = nullptr;
1158 mCurrentState.frameNumber_legacy = 0;
1159 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001160}
1161
Marissa Wallf58c14b2018-07-24 10:50:43 -07001162void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001163 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001164 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001165}
1166
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001167// ----------------------------------------------------------------------------
1168// pageflip handling...
1169// ----------------------------------------------------------------------------
1170
Robert Carr1f0a16a2016-10-24 16:27:39 -07001171bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001172 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001173 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001174 if (parent != nullptr && parent->isHiddenByPolicy()) {
1175 return true;
1176 }
Robert Carr1c5481e2019-07-01 14:42:27 -07001177 if (usingRelativeZ(LayerVector::StateSet::Drawing)) {
1178 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
1179 if (zOrderRelativeOf != nullptr) {
1180 if (zOrderRelativeOf->isHiddenByPolicy()) {
1181 return true;
1182 }
1183 }
1184 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001185 return s.flags & layer_state_t::eLayerHidden;
1186}
1187
David Sodman41fdfc92017-11-06 16:09:56 -08001188uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001189 // TODO: should we do something special if mSecure is set?
1190 if (mProtectedByApp) {
1191 // need a hardware-protected path to external video sink
1192 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001193 }
Riley Andrews03414a12014-07-01 14:22:59 -07001194 if (mPotentialCursor) {
1195 usage |= GraphicBuffer::USAGE_CURSOR;
1196 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001197 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001198 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001199}
1200
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001201void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001202 uint32_t orientation = 0;
Vishnu Nair5eb3f062019-04-08 08:21:03 -07001203 // Disable setting transform hint if the debug flag is set.
1204 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001205 // The transform hint is used to improve performance, but we can
1206 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001207 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001208 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001209 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001210 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001211 orientation = 0;
1212 }
1213 }
David Sodmaneb085e02017-10-05 18:49:04 -07001214 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001215}
1216
Mathias Agopian13127d82013-03-05 17:47:11 -08001217// ----------------------------------------------------------------------------
1218// debugging
1219// ----------------------------------------------------------------------------
1220
Marissa Wall61c58622018-07-18 10:12:20 -07001221// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001222LayerDebugInfo Layer::getLayerDebugInfo() const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001223 using namespace std::string_literals;
1224
Kalle Raitaa099a242017-01-11 11:17:29 -08001225 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001226 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001227 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001228 sp<Layer> parent = getParent();
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001229 info.mParentName = parent ? parent->getName() : "none"s;
chaviw8a01fa42019-08-19 12:39:31 -07001230 info.mType = getType();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001231 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Lloyd Piquea2468662019-03-07 21:31:06 -08001232
1233 info.mVisibleRegion = debugGetVisibleRegionOnDefaultDisplay();
Kalle Raitaa099a242017-01-11 11:17:29 -08001234 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001235 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001236 info.mX = ds.active_legacy.transform.tx();
1237 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001238 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001239 info.mWidth = ds.active_legacy.w;
1240 info.mHeight = ds.active_legacy.h;
1241 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001242 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001243 info.mFlags = ds.flags;
1244 info.mPixelFormat = getPixelFormat();
chaviw4244e032019-09-04 11:27:49 -07001245 info.mDataSpace = static_cast<android_dataspace>(getDataSpace());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001246 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1247 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1248 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1249 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001250 {
chaviwd62d3062019-09-04 14:48:02 -07001251 sp<const GraphicBuffer> buffer = getBuffer();
David Sodman5b4cffc2017-11-23 13:20:29 -08001252 if (buffer != 0) {
1253 info.mActiveBufferWidth = buffer->getWidth();
1254 info.mActiveBufferHeight = buffer->getHeight();
1255 info.mActiveBufferStride = buffer->getStride();
1256 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001257 } else {
1258 info.mActiveBufferWidth = 0;
1259 info.mActiveBufferHeight = 0;
1260 info.mActiveBufferStride = 0;
1261 info.mActiveBufferFormat = 0;
1262 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001263 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001264 info.mNumQueuedFrames = getQueuedFrameCount();
1265 info.mRefreshPending = isBufferLatched();
1266 info.mIsOpaque = isOpaque(ds);
1267 info.mContentDirty = contentDirty;
1268 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001269}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001270
Yiwei Zhang5434a782018-12-05 18:06:32 -08001271void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001272 result.append("-------------------------------");
1273 result.append("-------------------------------");
1274 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001275 result.append(" Layer name\n");
1276 result.append(" Z | ");
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001277 result.append(" Window Type | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001278 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001279 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001280 result.append(" Disp Frame (LTRB) | ");
1281 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001282 result.append("-------------------------------");
1283 result.append("-------------------------------");
1284 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001285}
1286
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001287void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1288 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1289 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001290 return;
1291 }
1292
Yiwei Zhang5434a782018-12-05 18:06:32 -08001293 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001294 if (mName.length() > 77) {
1295 std::string shortened;
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001296 shortened.append(mName, 0, 36);
Dan Stozae22aec72016-08-01 13:20:59 -07001297 shortened.append("[...]");
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001298 shortened.append(mName, mName.length() - 36);
1299 name = std::move(shortened);
Dan Stozae22aec72016-08-01 13:20:59 -07001300 } else {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001301 name = mName;
Dan Stozae22aec72016-08-01 13:20:59 -07001302 }
1303
Yiwei Zhang5434a782018-12-05 18:06:32 -08001304 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001305
Alec Mourib416efd2018-09-06 21:01:59 +00001306 const State& layerState(getDrawingState());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001307 const auto& compositionState = outputLayer->getState();
1308
Chia-I Wu1e043612018-03-01 09:45:09 -08001309 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001310 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001311 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001312 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001313 }
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001314 StringAppendF(&result, " %10d | ", mWindowType);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001315 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
1316 StringAppendF(&result, "%10s | ",
1317 toString(getCompositionLayer() ? compositionState.bufferTransform
1318 : static_cast<Hwc2::Transform>(0))
1319 .c_str());
1320 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001321 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001322 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001323 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1324 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001325
Yichi Chen6ca35192018-05-29 12:20:43 +08001326 result.append("- - - - - - - - - - - - - - - -");
1327 result.append("- - - - - - - - - - - - - - - -");
1328 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001329}
Dan Stozae22aec72016-08-01 13:20:59 -07001330
Yiwei Zhang5434a782018-12-05 18:06:32 -08001331void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001332 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001333}
1334
Svetoslavd85084b2014-03-20 10:28:31 -07001335void Layer::clearFrameStats() {
1336 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001337}
1338
Jamie Gennis6547ff42013-07-16 20:12:42 -07001339void Layer::logFrameStats() {
1340 mFrameTracker.logAndResetStats(mName);
1341}
1342
Svetoslavd85084b2014-03-20 10:28:31 -07001343void Layer::getFrameStats(FrameStats* outStats) const {
1344 mFrameTracker.getStats(outStats);
1345}
1346
Yiwei Zhang5434a782018-12-05 18:06:32 -08001347void Layer::dumpFrameEvents(std::string& result) {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001348 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().c_str(), getType(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001349 Mutex::Autolock lock(mFrameEventHistoryMutex);
1350 mFrameEventHistory.checkFencesForCompletion();
1351 mFrameEventHistory.dump(result);
1352}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001353
Vishnu Nair0f085c62019-08-30 08:49:12 -07001354void Layer::dumpCallingUidPid(std::string& result) const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001355 StringAppendF(&result, "Layer %s (%s) pid:%d uid:%d\n", getName().c_str(), getType(),
Vishnu Nair0f085c62019-08-30 08:49:12 -07001356 mCallingPid, mCallingUid);
1357}
1358
Brian Anderson5ea5e592016-12-01 16:54:33 -08001359void Layer::onDisconnect() {
1360 Mutex::Autolock lock(mFrameEventHistoryMutex);
1361 mFrameEventHistory.onDisconnect();
Mikael Pessa90092f42019-08-26 17:22:04 -07001362 const int32_t layerID = getSequence();
1363 mFlinger->mTimeStats->onDestroy(layerID);
1364 mFlinger->mFrameTracer->onDestroy(layerID);
Brian Anderson5ea5e592016-12-01 16:54:33 -08001365}
1366
Brian Anderson3890c392016-07-25 12:48:08 -07001367void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001368 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001369 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001370 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1371 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001372 }
1373
Brian Andersond6927fb2016-07-23 23:37:30 -07001374 Mutex::Autolock lock(mFrameEventHistoryMutex);
1375 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001376 // If there are any unsignaled fences in the aquire timeline at this
1377 // point, the previously queued frame hasn't been latched yet. Go ahead
1378 // and try to get the signal time here so the syscall is taken out of
1379 // the main thread's critical path.
1380 mAcquireTimeline.updateSignalTimes();
1381 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001382 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001383 mFrameEventHistory.addQueue(*newTimestamps);
1384 }
1385
Brian Anderson3890c392016-07-25 12:48:08 -07001386 if (outDelta) {
1387 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001388 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001389}
Dan Stozae77c7662016-05-13 11:37:28 -07001390
Chia-I Wu98f1c102017-05-30 14:54:08 -07001391size_t Layer::getChildrenCount() const {
1392 size_t count = 0;
1393 for (const sp<Layer>& child : mCurrentChildren) {
1394 count += 1 + child->getChildrenCount();
1395 }
1396 return count;
1397}
1398
Robert Carr1f0a16a2016-10-24 16:27:39 -07001399void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001400 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001401 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001402
Robert Carr1f0a16a2016-10-24 16:27:39 -07001403 mCurrentChildren.add(layer);
1404 layer->setParent(this);
1405}
1406
1407ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001408 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001409 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001410
Robert Carr1323c952019-01-28 18:13:27 -08001411 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001412 return mCurrentChildren.remove(layer);
1413}
1414
Robert Carr1db73f62016-12-21 12:58:51 -08001415bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1416 sp<Handle> handle = nullptr;
1417 sp<Layer> newParent = nullptr;
1418 if (newParentHandle == nullptr) {
1419 return false;
1420 }
1421 handle = static_cast<Handle*>(newParentHandle.get());
1422 newParent = handle->owner.promote();
1423 if (newParent == nullptr) {
1424 ALOGE("Unable to promote Layer handle");
1425 return false;
1426 }
1427
chaviw5aedec92018-10-22 10:40:38 -07001428 if (attachChildren()) {
1429 setTransactionFlags(eTransactionNeeded);
1430 }
Robert Carr1db73f62016-12-21 12:58:51 -08001431 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001432 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001433 }
1434 mCurrentChildren.clear();
1435
1436 return true;
1437}
1438
Robert Carr15eae092018-03-23 13:43:53 -07001439void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001440 for (const sp<Layer>& child : mDrawingChildren) {
1441 child->mDrawingParent = newParent;
Vishnu Nairc652ff82019-03-15 12:48:54 -07001442 child->computeBounds(newParent->mBounds,
Vishnu Nairc97b8db2019-10-29 18:19:35 -07001443 newParent->getTransformWithScale(newParent->getBufferScaleTransform()),
1444 newParent->mEffectiveShadowRadius);
Robert Carr578038f2018-03-09 12:25:24 -08001445 }
1446}
1447
chaviwf1961f72017-09-18 16:41:07 -07001448bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001449 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001450
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001451 // While layers are detached, we allow most operations
1452 // and simply halt performing the actual transaction. However
1453 // for reparent != null we would enter the mRemovedFromCurrentState
1454 // state, regardless of whether doTransaction was called, and
1455 // so we need to prevent the update here.
1456 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001457 return false;
1458 }
1459
Robert Carr54cf5b12019-01-25 14:02:28 -08001460 sp<Layer> newParent;
1461 if (newParentHandle != nullptr) {
1462 auto handle = static_cast<Handle*>(newParentHandle.get());
1463 newParent = handle->owner.promote();
1464 if (newParent == nullptr) {
1465 ALOGE("Unable to promote Layer handle");
1466 return false;
1467 }
1468 if (newParent == this) {
1469 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1470 return false;
1471 }
1472 }
1473
chaviwf1961f72017-09-18 16:41:07 -07001474 sp<Layer> parent = getParent();
1475 if (parent != nullptr) {
1476 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001477 }
1478
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001479 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001480 newParent->addChild(this);
1481 if (!newParent->isRemovedFromCurrentState()) {
1482 addToCurrentState();
1483 } else {
1484 onRemovedFromCurrentState();
1485 }
1486
1487 if (mLayerDetached) {
1488 mLayerDetached = false;
1489 callSetTransactionFlags = true;
1490 }
1491 } else {
1492 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001493 }
1494
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001495 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001496 setTransactionFlags(eTransactionNeeded);
1497 }
chaviw06178942017-07-27 10:25:59 -07001498 return true;
1499}
1500
Robert Carr9524cb32017-02-13 11:32:32 -08001501bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001502 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001503 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001504 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001505 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001506 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001507 child->detachChildren();
chaviw43cb3cb2019-05-31 15:23:41 -07001508 child->removeRemoteSyncPoints();
Robert Carr9524cb32017-02-13 11:32:32 -08001509 }
Robert Carr7f619b22017-11-06 12:56:35 -08001510 }
Robert Carr9524cb32017-02-13 11:32:32 -08001511
1512 return true;
1513}
1514
chaviw5aedec92018-10-22 10:40:38 -07001515bool Layer::attachChildren() {
1516 bool changed = false;
1517 for (const sp<Layer>& child : mCurrentChildren) {
1518 sp<Client> parentClient = mClientRef.promote();
1519 sp<Client> client(child->mClientRef.promote());
1520 if (client != nullptr && parentClient != client) {
1521 if (child->mLayerDetached) {
1522 child->mLayerDetached = false;
1523 changed = true;
1524 }
1525 changed |= child->attachChildren();
1526 }
1527 }
1528
1529 return changed;
1530}
1531
Peiyong Lind3788632018-09-18 16:01:31 -07001532bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001533 static const mat4 identityMatrix = mat4();
1534
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001535 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001536 return false;
1537 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001538 ++mCurrentState.sequence;
1539 mCurrentState.colorTransform = matrix;
1540 mCurrentState.hasColorTransform = matrix != identityMatrix;
1541 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001542 setTransactionFlags(eTransactionNeeded);
1543 return true;
1544}
1545
chaviwf66724d2018-11-28 16:35:21 -08001546mat4 Layer::getColorTransform() const {
1547 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1548 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1549 colorTransform = parent->getColorTransform() * colorTransform;
1550 }
1551 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001552}
1553
1554bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001555 bool hasColorTransform = getDrawingState().hasColorTransform;
1556 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1557 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1558 }
1559 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001560}
1561
Chia-I Wu11481472018-05-04 10:43:19 -07001562bool Layer::isLegacyDataSpace() const {
1563 // return true when no higher bits are set
chaviw4244e032019-09-04 11:27:49 -07001564 return !(getDataSpace() &
1565 (ui::Dataspace::STANDARD_MASK | ui::Dataspace::TRANSFER_MASK |
1566 ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001567}
1568
Robert Carr1f0a16a2016-10-24 16:27:39 -07001569void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001570 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001571}
1572
chaviw301b1d82019-11-06 13:15:09 -08001573int32_t Layer::getZ(LayerVector::StateSet stateSet) const {
1574 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1575 const State& state = useDrawing ? mDrawingState : mCurrentState;
1576 return state.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001577}
1578
Robert Carr1c5481e2019-07-01 14:42:27 -07001579bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) const {
Robert Carr29abff82017-12-04 13:51:20 -08001580 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001581 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviwe5ac40f2019-09-24 16:36:55 -07001582 return state.isRelativeOf;
Robert Carr29abff82017-12-04 13:51:20 -08001583}
1584
David Sodman41fdfc92017-11-06 16:09:56 -08001585__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001586 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001587 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1588 "makeTraversalList received invalid stateSet");
1589 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1590 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001591 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001592
Robert Carr29abff82017-12-04 13:51:20 -08001593 if (state.zOrderRelatives.size() == 0) {
1594 *outSkipRelativeZUsers = true;
1595 return children;
1596 }
1597
chaviwfd462612018-05-31 16:11:27 -07001598 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001599 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001600 sp<Layer> strongRelative = weakRelative.promote();
1601 if (strongRelative != nullptr) {
1602 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001603 }
1604 }
1605
Dan Stoza412903f2017-04-27 13:42:17 -07001606 for (const sp<Layer>& child : children) {
chaviwe5ac40f2019-09-24 16:36:55 -07001607 if (child->usingRelativeZ(stateSet)) {
Robert Carr503c7042017-09-27 15:06:08 -07001608 continue;
1609 }
Robert Carrdb66e622017-04-10 16:55:57 -07001610 traverse.add(child);
1611 }
1612
1613 return traverse;
1614}
1615
Robert Carr1f0a16a2016-10-24 16:27:39 -07001616/**
Robert Carrdb66e622017-04-10 16:55:57 -07001617 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001618 */
Dan Stoza412903f2017-04-27 13:42:17 -07001619void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001620 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1621 // produce a new list for traversing, including our relatives, and not including our children
1622 // who are relatives of another surface. In the case that there are no relative Z,
1623 // makeTraversalList returns our children directly to avoid significant overhead.
1624 // However in this case we need to take the responsibility for filtering children which
1625 // are relatives of another surface here.
1626 bool skipRelativeZUsers = false;
1627 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001628
Robert Carr1f0a16a2016-10-24 16:27:39 -07001629 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001630 for (; i < list.size(); i++) {
1631 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001632 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1633 continue;
1634 }
1635
chaviw301b1d82019-11-06 13:15:09 -08001636 if (relative->getZ(stateSet) >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001637 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001638 }
Dan Stoza412903f2017-04-27 13:42:17 -07001639 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001640 }
Robert Carr29abff82017-12-04 13:51:20 -08001641
Dan Stoza412903f2017-04-27 13:42:17 -07001642 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001643 for (; i < list.size(); i++) {
1644 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001645
Robert Carr29abff82017-12-04 13:51:20 -08001646 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1647 continue;
1648 }
Dan Stoza412903f2017-04-27 13:42:17 -07001649 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001650 }
1651}
1652
1653/**
Robert Carrdb66e622017-04-10 16:55:57 -07001654 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001655 */
Dan Stoza412903f2017-04-27 13:42:17 -07001656void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1657 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001658 // See traverseInZOrder for documentation.
1659 bool skipRelativeZUsers = false;
1660 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001661
Robert Carr1f0a16a2016-10-24 16:27:39 -07001662 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001663 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001664 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001665
1666 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1667 continue;
1668 }
1669
chaviw301b1d82019-11-06 13:15:09 -08001670 if (relative->getZ(stateSet) < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001671 break;
1672 }
Dan Stoza412903f2017-04-27 13:42:17 -07001673 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001674 }
Dan Stoza412903f2017-04-27 13:42:17 -07001675 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001676 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001677 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001678
1679 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1680 continue;
1681 }
1682
Dan Stoza412903f2017-04-27 13:42:17 -07001683 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001684 }
1685}
1686
chaviw4b129c22018-04-09 16:19:43 -07001687LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1688 const std::vector<Layer*>& layersInTree) {
1689 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1690 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001691 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1692 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001693 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001694
chaviwfd462612018-05-31 16:11:27 -07001695 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001696 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1697 sp<Layer> strongRelative = weakRelative.promote();
1698 // Only add relative layers that are also descendents of the top most parent of the tree.
1699 // If a relative layer is not a descendent, then it should be ignored.
1700 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1701 traverse.add(strongRelative);
1702 }
1703 }
1704
1705 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001706 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001707 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1708 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1709 // the child here since it won't be added later as a relative.
1710 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1711 childState.zOrderRelativeOf.promote().get())) {
1712 continue;
1713 }
1714 traverse.add(child);
1715 }
1716
1717 return traverse;
1718}
1719
1720void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1721 LayerVector::StateSet stateSet,
1722 const LayerVector::Visitor& visitor) {
1723 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001724
1725 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001726 for (; i < list.size(); i++) {
1727 const auto& relative = list[i];
chaviw301b1d82019-11-06 13:15:09 -08001728 if (relative->getZ(stateSet) >= 0) {
chaviwa76b2712017-09-20 12:02:26 -07001729 break;
1730 }
chaviw4b129c22018-04-09 16:19:43 -07001731 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001732 }
chaviw4b129c22018-04-09 16:19:43 -07001733
chaviwa76b2712017-09-20 12:02:26 -07001734 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001735 for (; i < list.size(); i++) {
1736 const auto& relative = list[i];
1737 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001738 }
1739}
1740
chaviw4b129c22018-04-09 16:19:43 -07001741std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1742 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1743 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1744
1745 std::vector<Layer*> layersInTree = {this};
1746 for (size_t i = 0; i < children.size(); i++) {
1747 const auto& child = children[i];
1748 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1749 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1750 }
1751
1752 return layersInTree;
1753}
1754
1755void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1756 const LayerVector::Visitor& visitor) {
1757 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1758 std::sort(layersInTree.begin(), layersInTree.end());
1759 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1760}
1761
Peiyong Linefefaac2018-08-17 12:27:51 -07001762ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001763 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001764}
1765
chaviw13fdc492017-06-27 12:40:18 -07001766half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001767 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001768
chaviw13fdc492017-06-27 12:40:18 -07001769 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1770 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001771}
Robert Carr6452f122017-03-21 10:41:29 -07001772
chaviw13fdc492017-06-27 12:40:18 -07001773half4 Layer::getColor() const {
1774 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001775 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001776}
Robert Carr6452f122017-03-21 10:41:29 -07001777
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001778Layer::RoundedCornerState Layer::getRoundedCornerState() const {
1779 const auto& p = mDrawingParent.promote();
1780 if (p != nullptr) {
Peiyong Lin27016a92019-03-29 17:36:08 +00001781 RoundedCornerState parentState = p->getRoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001782 if (parentState.radius > 0) {
1783 ui::Transform t = getActiveTransform(getDrawingState());
1784 t = t.inverse();
1785 parentState.cropRect = t.transform(parentState.cropRect);
1786 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1787 // but a transform matrix can define horizontal and vertical scales.
1788 // Let's take the average between both of them and pass into the shader, practically we
1789 // never do this type of transformation on windows anyway.
1790 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
1791 return parentState;
1792 }
1793 }
1794 const float radius = getDrawingState().cornerRadius;
Peiyong Linb9ff23e2019-04-08 11:04:59 -07001795 return radius > 0 && getCrop(getDrawingState()).isValid()
1796 ? RoundedCornerState(getCrop(getDrawingState()).toFloatRect(), radius)
1797 : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001798}
1799
Robert Carr1f0a16a2016-10-24 16:27:39 -07001800void Layer::commitChildList() {
1801 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1802 const auto& child = mCurrentChildren[i];
1803 child->commitChildList();
1804 }
1805 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001806 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001807}
1808
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001809static wp<Layer> extractLayerFromBinder(const wp<IBinder>& weakBinderHandle) {
1810 if (weakBinderHandle == nullptr) {
1811 return nullptr;
1812 }
1813 sp<IBinder> binderHandle = weakBinderHandle.promote();
1814 if (binderHandle == nullptr) {
1815 return nullptr;
1816 }
1817 sp<Layer::Handle> handle = static_cast<Layer::Handle*>(binderHandle.get());
1818 if (handle == nullptr) {
1819 return nullptr;
1820 }
1821 return handle->owner;
1822}
1823
Robert Carr720e5062018-07-30 17:45:14 -07001824void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001825 mCurrentState.inputInfo = info;
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001826 mCurrentState.touchableRegionCrop = extractLayerFromBinder(info.touchableRegionCropHandle);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001827 mCurrentState.modified = true;
1828 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07001829 setTransactionFlags(eTransactionNeeded);
1830}
1831
Vishnu Nair8406fd72019-07-30 11:29:31 -07001832void Layer::writeToProtoDrawingState(LayerProto* layerInfo, uint32_t traceFlags) const {
1833 ui::Transform transform = getTransform();
1834
1835 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
1836 for (const auto& pendingState : mPendingStatesSnapshot) {
1837 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
1838 if (barrierLayer != nullptr) {
1839 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
1840 barrierLayerProto->set_id(barrierLayer->sequence);
1841 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
1842 }
1843 }
1844
chaviwd62d3062019-09-04 14:48:02 -07001845 auto buffer = getBuffer();
Vishnu Nair8406fd72019-07-30 11:29:31 -07001846 if (buffer != nullptr) {
1847 LayerProtoHelper::writeToProto(buffer,
1848 [&]() { return layerInfo->mutable_active_buffer(); });
chaviw4244e032019-09-04 11:27:49 -07001849 LayerProtoHelper::writeToProto(ui::Transform(getBufferTransform()),
Vishnu Nair8406fd72019-07-30 11:29:31 -07001850 layerInfo->mutable_buffer_transform());
1851 }
1852 layerInfo->set_invalidate(contentDirty);
1853 layerInfo->set_is_protected(isProtected());
chaviw4244e032019-09-04 11:27:49 -07001854 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(getDataSpace())));
Vishnu Nair8406fd72019-07-30 11:29:31 -07001855 layerInfo->set_queued_frames(getQueuedFrameCount());
1856 layerInfo->set_refresh_pending(isBufferLatched());
1857 layerInfo->set_curr_frame(mCurrentFrameNumber);
1858 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
1859
1860 layerInfo->set_corner_radius(getRoundedCornerState().radius);
1861 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1862 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
1863 [&]() { return layerInfo->mutable_position(); });
1864 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
Lloyd Piquea2468662019-03-07 21:31:06 -08001865 LayerProtoHelper::writeToProto(debugGetVisibleRegionOnDefaultDisplay(),
Vishnu Nair8406fd72019-07-30 11:29:31 -07001866 [&]() { return layerInfo->mutable_visible_region(); });
1867 LayerProtoHelper::writeToProto(surfaceDamageRegion,
1868 [&]() { return layerInfo->mutable_damage_region(); });
1869 }
1870
1871 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
1872 LayerProtoHelper::writeToProto(mSourceBounds,
1873 [&]() { return layerInfo->mutable_source_bounds(); });
1874 LayerProtoHelper::writeToProto(mScreenBounds,
1875 [&]() { return layerInfo->mutable_screen_bounds(); });
1876 }
1877}
1878
1879void Layer::writeToProtoCommonState(LayerProto* layerInfo, LayerVector::StateSet stateSet,
1880 uint32_t traceFlags) const {
chaviw1d044282017-09-27 12:19:28 -07001881 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1882 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001883 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07001884
Peiyong Linefefaac2018-08-17 12:27:51 -07001885 ui::Transform requestedTransform = state.active_legacy.transform;
chaviw1d044282017-09-27 12:19:28 -07001886
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001887 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
1888 layerInfo->set_id(sequence);
1889 layerInfo->set_name(getName().c_str());
chaviw8a01fa42019-08-19 12:39:31 -07001890 layerInfo->set_type(getType());
chaviw1d044282017-09-27 12:19:28 -07001891
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001892 for (const auto& child : children) {
1893 layerInfo->add_children(child->sequence);
chaviw1d044282017-09-27 12:19:28 -07001894 }
chaviw1d044282017-09-27 12:19:28 -07001895
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001896 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1897 sp<Layer> strongRelative = weakRelative.promote();
1898 if (strongRelative != nullptr) {
1899 layerInfo->add_relatives(strongRelative->sequence);
1900 }
chaviwadc40c22018-07-10 16:57:27 -07001901 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001902
1903 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
1904 [&]() { return layerInfo->mutable_transparent_region(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001905
1906 layerInfo->set_layer_stack(getLayerStack());
1907 layerInfo->set_z(state.z);
1908
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001909 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(),
1910 [&]() {
1911 return layerInfo->mutable_requested_position();
1912 });
1913
1914 LayerProtoHelper::writeSizeToProto(state.active_legacy.w, state.active_legacy.h,
1915 [&]() { return layerInfo->mutable_size(); });
1916
1917 LayerProtoHelper::writeToProto(state.crop_legacy,
1918 [&]() { return layerInfo->mutable_crop(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001919
1920 layerInfo->set_is_opaque(isOpaque(state));
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001921
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001922
1923 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1924 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
1925 LayerProtoHelper::writeToProto(state.color,
1926 [&]() { return layerInfo->mutable_requested_color(); });
1927 layerInfo->set_flags(state.flags);
1928
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001929 LayerProtoHelper::writeToProto(requestedTransform,
1930 layerInfo->mutable_requested_transform());
1931
1932 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
1933 if (parent != nullptr) {
1934 layerInfo->set_parent(parent->sequence);
1935 } else {
1936 layerInfo->set_parent(-1);
1937 }
1938
1939 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1940 if (zOrderRelativeOf != nullptr) {
1941 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1942 } else {
1943 layerInfo->set_z_order_relative_of(-1);
1944 }
chaviwadc40c22018-07-10 16:57:27 -07001945 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001946
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001947 if (traceFlags & SurfaceTracing::TRACE_INPUT) {
1948 LayerProtoHelper::writeToProto(state.inputInfo, state.touchableRegionCrop,
1949 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001950 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001951
1952 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
1953 auto protoMap = layerInfo->mutable_metadata();
1954 for (const auto& entry : state.metadata.mMap) {
1955 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
1956 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001957 }
chaviw1d044282017-09-27 12:19:28 -07001958}
1959
Robert Carr2e102c92018-10-23 12:11:15 -07001960bool Layer::isRemovedFromCurrentState() const {
1961 return mRemovedFromCurrentState;
1962}
1963
Arthur Hungd20b2702019-01-14 18:16:16 +08001964InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001965 InputWindowInfo info = mDrawingState.inputInfo;
Robert Carr720e5062018-07-30 17:45:14 -07001966
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001967 if (info.displayId == ADISPLAY_ID_NONE) {
chaviwb15ea8a2019-09-03 12:40:22 -07001968 info.displayId = getLayerStack();
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001969 }
1970
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001971 ui::Transform t = getTransform();
1972 const float xScale = t.sx();
1973 const float yScale = t.sy();
Ady Abraham282f1d72019-07-24 18:05:56 -07001974 int32_t xSurfaceInset = info.surfaceInset;
1975 int32_t ySurfaceInset = info.surfaceInset;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001976 if (xScale != 1.0f || yScale != 1.0f) {
Ady Abraham282f1d72019-07-24 18:05:56 -07001977 info.windowXScale *= (xScale != 0.0f) ? 1.0f / xScale : 0.0f;
1978 info.windowYScale *= (yScale != 0.0f) ? 1.0f / yScale : 0.0f;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001979 info.touchableRegion.scaleSelf(xScale, yScale);
Ady Abraham282f1d72019-07-24 18:05:56 -07001980 xSurfaceInset = std::round(xSurfaceInset * xScale);
1981 ySurfaceInset = std::round(ySurfaceInset * yScale);
Riddle Hsu39d4aa52018-11-30 20:46:53 +08001982 }
Robert Carre07e1032018-11-26 12:55:53 -08001983
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001984 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001985 // If this is a portal window, set the touchableRegion to the layerBounds.
1986 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
1987 ? getBufferSize(getDrawingState())
1988 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08001989 if (!layerBounds.isValid()) {
1990 layerBounds = getCroppedBufferSize(getDrawingState());
1991 }
Vishnu Nair8033a492018-12-05 07:27:23 -08001992 layerBounds = t.transform(layerBounds);
Ady Abraham282f1d72019-07-24 18:05:56 -07001993
1994 // clamp inset to layer bounds
1995 xSurfaceInset = (xSurfaceInset >= 0) ? std::min(xSurfaceInset, layerBounds.getWidth() / 2) : 0;
1996 ySurfaceInset = (ySurfaceInset >= 0) ? std::min(ySurfaceInset, layerBounds.getHeight() / 2) : 0;
1997
Arthur Hung118b1142019-05-08 21:25:59 +08001998 layerBounds.inset(xSurfaceInset, ySurfaceInset, xSurfaceInset, ySurfaceInset);
Vishnu Nair8033a492018-12-05 07:27:23 -08001999
Arthur Hungd20b2702019-01-14 18:16:16 +08002000 // Input coordinate should match the layer bounds.
2001 info.frameLeft = layerBounds.left;
2002 info.frameTop = layerBounds.top;
2003 info.frameRight = layerBounds.right;
2004 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08002005
2006 // Position the touchable region relative to frame screen location and restrict it to frame
2007 // bounds.
2008 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08002009 info.visible = canReceiveInput();
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002010
2011 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2012 if (info.replaceTouchableRegionWithCrop) {
2013 if (cropLayer == nullptr) {
2014 info.touchableRegion = Region(Rect{mScreenBounds});
2015 } else {
2016 info.touchableRegion = Region(Rect{cropLayer->mScreenBounds});
2017 }
2018 } else if (cropLayer != nullptr) {
2019 info.touchableRegion = info.touchableRegion.intersect(Rect{cropLayer->mScreenBounds});
2020 }
2021
Robert Carr720e5062018-07-30 17:45:14 -07002022 return info;
2023}
2024
2025bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002026 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002027}
2028
Lloyd Piquefeb73d72018-12-04 17:23:44 -08002029std::shared_ptr<compositionengine::Layer> Layer::getCompositionLayer() const {
2030 return nullptr;
2031}
2032
chaviw3e727cd2019-01-31 13:41:05 -08002033bool Layer::canReceiveInput() const {
Vishnu Nair82353e92019-09-12 12:38:47 -07002034 return !isHiddenByPolicy();
chaviw3e727cd2019-01-31 13:41:05 -08002035}
2036
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002037compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
2038 const sp<const DisplayDevice>& display) const {
2039 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionLayer().get());
2040}
2041
Lloyd Piquea2468662019-03-07 21:31:06 -08002042Region Layer::debugGetVisibleRegionOnDefaultDisplay() const {
2043 sp<DisplayDevice> displayDevice = mFlinger->getDefaultDisplayDeviceLocked();
2044 if (displayDevice == nullptr) {
2045 return {};
2046 }
2047
2048 auto outputLayer = findOutputLayerForDisplay(displayDevice);
2049 if (outputLayer == nullptr) {
2050 return {};
2051 }
2052
2053 return outputLayer->getState().visibleRegion;
2054}
2055
chaviwb4c6e582019-08-16 14:35:07 -07002056void Layer::setInitialValuesForClone(const sp<Layer>& clonedFrom) {
2057 // copy drawing state from cloned layer
2058 mDrawingState = clonedFrom->mDrawingState;
2059 mClonedFrom = clonedFrom;
2060
2061 // TODO: (b/140756730) Ignore input for now since InputDispatcher doesn't support multiple
2062 // InputWindows per client token yet.
2063 mDrawingState.inputInfo.token = nullptr;
2064}
chaviw74b03172019-08-19 11:09:03 -07002065
2066void Layer::updateMirrorInfo() {
2067 if (mClonedChild == nullptr || !mClonedChild->isClonedFromAlive()) {
2068 // If mClonedChild is null, there is nothing to mirror. If isClonedFromAlive returns false,
2069 // it means that there is a clone, but the layer it was cloned from has been destroyed. In
2070 // that case, we want to delete the reference to the clone since we want it to get
2071 // destroyed. The root, this layer, will still be around since the client can continue
2072 // to hold a reference, but no cloned layers will be displayed.
2073 mClonedChild = nullptr;
2074 return;
2075 }
2076
2077 std::map<sp<Layer>, sp<Layer>> clonedLayersMap;
2078 // If the real layer exists and is in current state, add the clone as a child of the root.
2079 // There's no need to remove from drawingState when the layer is offscreen since currentState is
2080 // copied to drawingState for the root layer. So the clonedChild is always removed from
2081 // drawingState and then needs to be added back each traversal.
2082 if (!mClonedChild->getClonedFrom()->isRemovedFromCurrentState()) {
2083 addChildToDrawing(mClonedChild);
2084 }
2085
2086 mClonedChild->updateClonedDrawingState(clonedLayersMap);
2087 mClonedChild->updateClonedChildren(this, clonedLayersMap);
2088 mClonedChild->updateClonedRelatives(clonedLayersMap);
2089}
2090
2091void Layer::updateClonedDrawingState(std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2092 // If the layer the clone was cloned from is alive, copy the content of the drawingState
2093 // to the clone. If the real layer is no longer alive, continue traversing the children
2094 // since we may be able to pull out other children that are still alive.
2095 if (isClonedFromAlive()) {
2096 sp<Layer> clonedFrom = getClonedFrom();
2097 mDrawingState = clonedFrom->mDrawingState;
2098 // TODO: (b/140756730) Ignore input for now since InputDispatcher doesn't support multiple
2099 // InputWindows per client token yet.
2100 mDrawingState.inputInfo.token = nullptr;
2101 clonedLayersMap.emplace(clonedFrom, this);
2102 }
2103
2104 // The clone layer may have children in drawingState since they may have been created and
2105 // added from a previous request to updateMirorInfo. This is to ensure we don't recreate clones
2106 // that already exist, since we can just re-use them.
2107 // The drawingChildren will not get overwritten by the currentChildren since the clones are
2108 // not updated in the regular traversal. They are skipped since the root will lose the
2109 // reference to them when it copies its currentChildren to drawing.
2110 for (sp<Layer>& child : mDrawingChildren) {
2111 child->updateClonedDrawingState(clonedLayersMap);
2112 }
2113}
2114
2115void Layer::updateClonedChildren(const sp<Layer>& mirrorRoot,
2116 std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2117 mDrawingChildren.clear();
2118
2119 if (!isClonedFromAlive()) {
2120 return;
2121 }
2122
2123 sp<Layer> clonedFrom = getClonedFrom();
2124 for (sp<Layer>& child : clonedFrom->mDrawingChildren) {
2125 if (child == mirrorRoot) {
2126 // This is to avoid cyclical mirroring.
2127 continue;
2128 }
2129 sp<Layer> clonedChild = clonedLayersMap[child];
2130 if (clonedChild == nullptr) {
2131 clonedChild = child->createClone();
2132 clonedLayersMap[child] = clonedChild;
2133 }
2134 addChildToDrawing(clonedChild);
2135 clonedChild->updateClonedChildren(mirrorRoot, clonedLayersMap);
2136 }
2137}
2138
2139void Layer::updateClonedRelatives(std::map<sp<Layer>, sp<Layer>> clonedLayersMap) {
2140 mDrawingState.zOrderRelativeOf = nullptr;
2141 mDrawingState.zOrderRelatives.clear();
2142
2143 if (!isClonedFromAlive()) {
2144 return;
2145 }
2146
2147 sp<Layer> clonedFrom = getClonedFrom();
2148 for (wp<Layer>& relativeWeak : clonedFrom->mDrawingState.zOrderRelatives) {
2149 sp<Layer> relative = relativeWeak.promote();
2150 auto clonedRelative = clonedLayersMap[relative];
2151 if (clonedRelative != nullptr) {
2152 mDrawingState.zOrderRelatives.add(clonedRelative);
2153 }
2154 }
2155
2156 // Check if the relativeLayer for the real layer is part of the cloned hierarchy.
2157 // It's possible that the layer it's relative to is outside the requested cloned hierarchy.
2158 // In that case, we treat the layer as if the relativeOf has been removed. This way, it will
2159 // still traverse the children, but the layer with the missing relativeOf will not be shown
2160 // on screen.
2161 sp<Layer> relativeOf = clonedFrom->mDrawingState.zOrderRelativeOf.promote();
2162 sp<Layer> clonedRelativeOf = clonedLayersMap[relativeOf];
2163 if (clonedRelativeOf != nullptr) {
2164 mDrawingState.zOrderRelativeOf = clonedRelativeOf;
2165 }
2166
2167 for (sp<Layer>& child : mDrawingChildren) {
2168 child->updateClonedRelatives(clonedLayersMap);
2169 }
2170}
2171
2172void Layer::addChildToDrawing(const sp<Layer>& layer) {
2173 mDrawingChildren.add(layer);
2174 layer->mDrawingParent = this;
2175}
2176
Mathias Agopian13127d82013-03-05 17:47:11 -08002177// ---------------------------------------------------------------------------
2178
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002179}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002180
2181#if defined(__gl_h_)
2182#error "don't include gl/gl.h in this file"
2183#endif
2184
2185#if defined(__gl2_h_)
2186#error "don't include gl2/gl2.h in this file"
2187#endif