blob: 5121835cae68fa52a1f0e0187e6014b745d3e60d [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>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080025#include <compositionengine/Display.h>
Lloyd Pique0b785d82018-12-04 17:25:27 -080026#include <compositionengine/Layer.h>
Lloyd Piquea83776c2019-01-29 18:42:32 -080027#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080028#include <compositionengine/OutputLayer.h>
Lloyd Pique0b785d82018-12-04 17:25:27 -080029#include <compositionengine/impl/LayerCompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080030#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"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080060#include "LayerProtoHelper.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070061#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070062#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063#include "SurfaceFlinger.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080064#include "TimeStats/TimeStats.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070065
David Sodman41fdfc92017-11-06 16:09:56 -080066#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068namespace android {
69
Yiwei Zhang5434a782018-12-05 18:06:32 -080070using base::StringAppendF;
71
Lloyd Piquef1c675b2018-09-12 20:45:39 -070072std::atomic<int32_t> Layer::sSequence{1};
Mathias Agopian13127d82013-03-05 17:47:11 -080073
Lloyd Pique42ab75e2018-09-12 20:46:03 -070074Layer::Layer(const LayerCreationArgs& args)
Ady Abraham8f1ee7f2019-04-05 10:32:50 -070075 : mFlinger(args.flinger),
76 mName(args.name),
77 mClientRef(args.client),
78 mWindowType(args.metadata.getInt32(METADATA_WINDOW_TYPE, 0)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -070079 mCurrentCrop.makeInvalid();
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
Dan Stozaf7ba41a2017-05-10 15:11:11 -070086 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070087
Lloyd Pique0449b0f2018-12-20 16:23:45 -080088 mCurrentState.active_legacy.w = args.w;
89 mCurrentState.active_legacy.h = args.h;
90 mCurrentState.flags = layerFlags;
91 mCurrentState.active_legacy.transform.set(0, 0);
92 mCurrentState.crop_legacy.makeInvalid();
93 mCurrentState.requestedCrop_legacy = mCurrentState.crop_legacy;
94 mCurrentState.z = 0;
95 mCurrentState.color.a = 1.0f;
96 mCurrentState.layerStack = 0;
97 mCurrentState.sequence = 0;
98 mCurrentState.requested_legacy = mCurrentState.active_legacy;
Lloyd Pique0449b0f2018-12-20 16:23:45 -080099 mCurrentState.active.w = UINT32_MAX;
100 mCurrentState.active.h = UINT32_MAX;
101 mCurrentState.active.transform.set(0, 0);
102 mCurrentState.transform = 0;
103 mCurrentState.transformToDisplayInverse = false;
104 mCurrentState.crop.makeInvalid();
105 mCurrentState.acquireFence = new Fence(-1);
106 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
107 mCurrentState.hdrMetadata.validTypes = 0;
Marissa Wall7e0a01f2019-08-14 14:29:25 -0700108 mCurrentState.surfaceDamageRegion = Region::INVALID_REGION;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800109 mCurrentState.cornerRadius = 0.0f;
110 mCurrentState.api = -1;
111 mCurrentState.hasColorTransform = false;
Peiyong Linc502cb72019-03-01 15:00:23 -0800112 mCurrentState.colorSpaceAgnostic = false;
Evan Roskya1f1e152019-01-24 16:17:46 -0800113 mCurrentState.metadata = args.metadata;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700114
115 // drawing state & current state are identical
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800116 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700117
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800118 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700119 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800120 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200121 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700122
Ady Abraham8f1ee7f2019-04-05 10:32:50 -0700123 mSchedulerLayerHandle = mFlinger->mScheduler->registerLayer(mName.c_str(), mWindowType);
Ady Abraham09bd3922019-04-08 10:44:56 -0700124
Robert Carr2e102c92018-10-23 12:11:15 -0700125 mFlinger->onLayerCreated();
Dan Stoza436ccf32018-06-21 12:10:12 -0700126}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700127
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700128Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800129 sp<Client> c(mClientRef.promote());
130 if (c != 0) {
131 c->detachLayer(this);
132 }
133
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000134 mFrameTracker.logAndResetStats(mName);
chaviw74d90ad2019-04-26 14:45:26 -0700135 mFlinger->onLayerDestroyed(this);
Mathias Agopian96f08192010-06-02 23:28:45 -0700136}
137
Mathias Agopian13127d82013-03-05 17:47:11 -0800138// ---------------------------------------------------------------------------
139// callbacks
140// ---------------------------------------------------------------------------
141
David Sodmaneb085e02017-10-05 18:49:04 -0700142/*
143 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
144 * Layer. So, the implementation is done in BufferLayer. When called on a
145 * ColorLayer object, it's essentially a NOP.
146 */
David Sodmaneb085e02017-10-05 18:49:04 -0700147void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800148
chaviw43cb3cb2019-05-31 15:23:41 -0700149void Layer::removeRemoteSyncPoints() {
150 for (auto& point : mRemoteSyncPoints) {
151 point->setTransactionApplied();
152 }
153 mRemoteSyncPoints.clear();
154
155 {
chaviw43cb3cb2019-05-31 15:23:41 -0700156 for (State pendingState : mPendingStates) {
157 pendingState.barrierLayer_legacy = nullptr;
158 }
159 }
160}
161
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700162void Layer::removeRelativeZ(const std::vector<Layer*>& layersInTree) {
163 if (mCurrentState.zOrderRelativeOf == nullptr) {
164 return;
165 }
Robert Carr2e102c92018-10-23 12:11:15 -0700166
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700167 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
168 if (strongRelative == nullptr) {
169 setZOrderRelativeOf(nullptr);
170 return;
171 }
172
173 if (!std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
174 strongRelative->removeZOrderRelative(this);
175 mFlinger->setTransactionFlags(eTraversalNeeded);
chaviw606e5cf2019-03-01 10:12:10 -0800176 setZOrderRelativeOf(nullptr);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700177 }
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700178}
179
180void Layer::removeFromCurrentState() {
181 mRemovedFromCurrentState = true;
Rob Carr4bba3702018-10-08 21:53:30 +0000182
Robert Carr2e102c92018-10-23 12:11:15 -0700183 // Since we are no longer reachable from CurrentState SurfaceFlinger
184 // will no longer invoke doTransaction for us, and so we will
185 // never finish applying transactions. We signal the sync point
186 // now so that another layer will not become indefinitely
187 // blocked.
chaviw43cb3cb2019-05-31 15:23:41 -0700188 removeRemoteSyncPoints();
Robert Carr2e102c92018-10-23 12:11:15 -0700189
190 {
191 Mutex::Autolock syncLock(mLocalSyncPointMutex);
192 for (auto& point : mLocalSyncPoints) {
193 point->setFrameAvailable();
194 }
195 mLocalSyncPoints.clear();
196 }
197
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800198 mFlinger->markLayerPendingRemovalLocked(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700199}
Chia-I Wu38512252017-05-17 14:36:16 -0700200
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700201void Layer::onRemovedFromCurrentState() {
202 auto layersInTree = getLayersInTree(LayerVector::StateSet::Current);
203 std::sort(layersInTree.begin(), layersInTree.end());
204 for (const auto& layer : layersInTree) {
205 layer->removeFromCurrentState();
206 layer->removeRelativeZ(layersInTree);
207 }
208}
209
chaviw61626f22018-11-15 16:26:27 -0800210void Layer::addToCurrentState() {
211 mRemovedFromCurrentState = false;
212
213 for (const auto& child : mCurrentChildren) {
214 child->addToCurrentState();
215 }
216}
217
Mathias Agopian13127d82013-03-05 17:47:11 -0800218// ---------------------------------------------------------------------------
219// set-up
220// ---------------------------------------------------------------------------
221
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700222const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800223 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800224}
225
chaviw13fdc492017-06-27 12:40:18 -0700226bool Layer::getPremultipledAlpha() const {
227 return mPremultipliedAlpha;
228}
229
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700230sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800231 Mutex::Autolock _l(mLock);
Robert Carrc0df3122019-04-11 13:18:21 -0700232 if (mGetHandleCalled) {
233 ALOGE("Get handle called twice" );
234 return nullptr;
235 }
236 mGetHandleCalled = true;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700237 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800238}
239
240// ---------------------------------------------------------------------------
241// h/w composer set-up
242// ---------------------------------------------------------------------------
243
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800244Rect Layer::getContentCrop() const {
245 // this is the crop rectangle that applies to the buffer
246 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700247 Rect crop;
248 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800249 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700250 crop = mCurrentCrop;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800251 } else if (mActiveBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800252 // otherwise we use the whole buffer
Lloyd Pique0b785d82018-12-04 17:25:27 -0800253 crop = mActiveBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700254 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800255 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700256 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700257 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700258 return crop;
259}
260
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700261static Rect reduce(const Rect& win, const Region& exclude) {
262 if (CC_LIKELY(exclude.isEmpty())) {
263 return win;
264 }
265 if (exclude.isRect()) {
266 return win.reduce(exclude.getBounds());
267 }
268 return Region(win).subtract(exclude).getBounds();
269}
270
Dan Stoza80d61162017-12-20 15:57:52 -0800271static FloatRect reduce(const FloatRect& win, const Region& exclude) {
272 if (CC_LIKELY(exclude.isEmpty())) {
273 return win;
274 }
275 // Convert through Rect (by rounding) for lack of FloatRegion
276 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
277}
278
Vishnu Nair4351ad52019-02-11 14:13:02 -0800279Rect Layer::getScreenBounds(bool reduceTransparentRegion) const {
Vishnu Nairf0c28512019-02-08 12:40:28 -0800280 if (!reduceTransparentRegion) {
281 return Rect{mScreenBounds};
282 }
283
284 FloatRect bounds = getBounds();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800285 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800286 // Transform to screen space.
287 bounds = t.transform(bounds);
288 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700289}
290
Vishnu Nair4351ad52019-02-11 14:13:02 -0800291FloatRect Layer::getBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000292 const State& s(getDrawingState());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800293 return getBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700294}
295
Vishnu Nairf0c28512019-02-08 12:40:28 -0800296FloatRect Layer::getBounds(const Region& activeTransparentRegion) const {
297 // Subtract the transparent region and snap to the bounds.
298 return reduce(mBounds, activeTransparentRegion);
299}
300
Vishnu Nairc652ff82019-03-15 12:48:54 -0700301ui::Transform Layer::getBufferScaleTransform() const {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800302 // If the layer is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
303 // it isFixedSize) then there may be additional scaling not accounted
Vishnu Nairc652ff82019-03-15 12:48:54 -0700304 // for in the layer transform.
Lloyd Pique0b785d82018-12-04 17:25:27 -0800305 if (!isFixedSize() || !mActiveBuffer) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700306 return {};
Vishnu Nair4351ad52019-02-11 14:13:02 -0800307 }
308
Marissa Wall290ad082019-03-06 13:23:47 -0800309 // If the layer is a buffer state layer, the active width and height
310 // could be infinite. In that case, return the effective transform.
311 const uint32_t activeWidth = getActiveWidth(getDrawingState());
312 const uint32_t activeHeight = getActiveHeight(getDrawingState());
313 if (activeWidth >= UINT32_MAX && activeHeight >= UINT32_MAX) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700314 return {};
Marissa Wall290ad082019-03-06 13:23:47 -0800315 }
316
Vishnu Nairc652ff82019-03-15 12:48:54 -0700317 int bufferWidth = mActiveBuffer->getWidth();
318 int bufferHeight = mActiveBuffer->getHeight();
319
320 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
321 std::swap(bufferWidth, bufferHeight);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800322 }
Vishnu Nairc652ff82019-03-15 12:48:54 -0700323
Marissa Wall290ad082019-03-06 13:23:47 -0800324 float sx = activeWidth / static_cast<float>(bufferWidth);
325 float sy = activeHeight / static_cast<float>(bufferHeight);
326
Vishnu Nair4351ad52019-02-11 14:13:02 -0800327 ui::Transform extraParentScaling;
328 extraParentScaling.set(sx, 0, 0, sy);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700329 return extraParentScaling;
330}
331
332ui::Transform Layer::getTransformWithScale(const ui::Transform& bufferScaleTransform) const {
333 // We need to mirror this scaling to child surfaces or we will break the contract where WM can
334 // treat child surfaces as pixels in the parent surface.
335 if (!isFixedSize() || !mActiveBuffer) {
336 return mEffectiveTransform;
337 }
338 return mEffectiveTransform * bufferScaleTransform;
339}
340
341FloatRect Layer::getBoundsPreScaling(const ui::Transform& bufferScaleTransform) const {
342 // We need the pre scaled layer bounds when computing child bounds to make sure the child is
343 // cropped to its parent layer after any buffer transform scaling is applied.
344 if (!isFixedSize() || !mActiveBuffer) {
345 return mBounds;
346 }
347 return bufferScaleTransform.inverse().transform(mBounds);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800348}
349
350void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform) {
351 const State& s(getDrawingState());
352
353 // Calculate effective layer transform
354 mEffectiveTransform = parentTransform * getActiveTransform(s);
355
356 // Transform parent bounds to layer space
357 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
358
Vishnu Nairc652ff82019-03-15 12:48:54 -0700359 // Calculate source bounds
Vishnu Nair4351ad52019-02-11 14:13:02 -0800360 mSourceBounds = computeSourceBounds(parentBounds);
361
362 // Calculate bounds by croping diplay frame with layer crop and parent bounds
363 FloatRect bounds = mSourceBounds;
364 const Rect layerCrop = getCrop(s);
365 if (!layerCrop.isEmpty()) {
366 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
367 }
368 bounds = bounds.intersect(parentBounds);
369
370 mBounds = bounds;
371 mScreenBounds = mEffectiveTransform.transform(mBounds);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700372
373 // Add any buffer scaling to the layer's children.
374 ui::Transform bufferScaleTransform = getBufferScaleTransform();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800375 for (const sp<Layer>& child : mDrawingChildren) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700376 child->computeBounds(getBoundsPreScaling(bufferScaleTransform),
377 getTransformWithScale(bufferScaleTransform));
Vishnu Nair4351ad52019-02-11 14:13:02 -0800378 }
379}
380
Vishnu Nair60356342018-11-13 13:00:45 -0800381Rect Layer::getCroppedBufferSize(const State& s) const {
382 Rect size = getBufferSize(s);
383 Rect crop = getCrop(s);
384 if (!crop.isEmpty() && size.isValid()) {
385 size.intersect(crop, &size);
386 } else if (!crop.isEmpty()) {
387 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700388 }
Vishnu Nair60356342018-11-13 13:00:45 -0800389 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800390}
391
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700392void Layer::setupRoundedCornersCropCoordinates(Rect win,
393 const FloatRect& roundedCornersCrop) const {
394 // Translate win by the rounded corners rect coordinates, to have all values in
395 // layer coordinate space.
396 win.left -= roundedCornersCrop.left;
397 win.right -= roundedCornersCrop.left;
398 win.top -= roundedCornersCrop.top;
399 win.bottom -= roundedCornersCrop.top;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700400}
401
Lloyd Piquea83776c2019-01-29 18:42:32 -0800402void Layer::latchGeometry(compositionengine::LayerFECompositionState& compositionState) const {
403 const auto& drawingState{getDrawingState()};
404 auto alpha = static_cast<float>(getAlpha());
David Revemanecf0fa52017-03-03 11:32:44 -0500405 auto blendMode = HWC2::BlendMode::None;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800406 if (!isOpaque(drawingState) || alpha != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800407 blendMode =
408 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800409 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800410
Lloyd Piquea83776c2019-01-29 18:42:32 -0800411 int type = drawingState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
412 int appId = drawingState.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700413 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400414 if (parent.get()) {
415 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800416 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
417 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
Jiwen 'Steve' Cai736c74e2019-05-28 18:33:22 -0700418 if (parentType > 0 && parentAppId > 0) {
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800419 type = parentType;
420 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700421 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400422 }
423
Lloyd Piquea83776c2019-01-29 18:42:32 -0800424 compositionState.geomLayerTransform = getTransform();
425 compositionState.geomInverseLayerTransform = compositionState.geomLayerTransform.inverse();
426 compositionState.geomBufferSize = getBufferSize(drawingState);
427 compositionState.geomContentCrop = getContentCrop();
428 compositionState.geomCrop = getCrop(drawingState);
429 compositionState.geomBufferTransform = mCurrentTransform;
430 compositionState.geomBufferUsesDisplayInverseTransform = getTransformToDisplayInverse();
431 compositionState.geomActiveTransparentRegion = getActiveTransparentRegion(drawingState);
432 compositionState.geomLayerBounds = mBounds;
433 compositionState.geomUsesSourceCrop = usesSourceCrop();
434 compositionState.isSecure = isSecure();
David Sodman15094112018-10-11 09:39:37 -0700435
Lloyd Piquea83776c2019-01-29 18:42:32 -0800436 compositionState.blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
437 compositionState.alpha = alpha;
438 compositionState.type = type;
439 compositionState.appId = appId;
440}
David Sodmanba340492018-08-05 21:51:33 -0700441
Lloyd Piquef5275482019-01-29 18:42:42 -0800442void Layer::latchPerFrameState(compositionengine::LayerFECompositionState& compositionState) const {
Lloyd Pique688abd42019-02-15 15:42:24 -0800443 const auto& drawingState{getDrawingState()};
Lloyd Piquef5275482019-01-29 18:42:42 -0800444 compositionState.forceClientComposition = false;
445
446 // TODO(lpique): b/121291683 Remove this one we are sure we don't need the
447 // value recomputed / set every frame.
448 compositionState.geomVisibleRegion = visibleRegion;
449
450 compositionState.isColorspaceAgnostic = isColorSpaceAgnostic();
451 compositionState.dataspace = mCurrentDataSpace;
452 compositionState.colorTransform = getColorTransform();
453 compositionState.colorTransformIsIdentity = !hasColorTransform();
454 compositionState.surfaceDamage = surfaceDamageRegion;
Lloyd Pique688abd42019-02-15 15:42:24 -0800455 compositionState.hasProtectedContent = isProtected();
456
457 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
458 compositionState.isOpaque =
459 isOpaque(drawingState) && !usesRoundedCorners && getAlpha() == 1.0_hf;
Lloyd Piquef5275482019-01-29 18:42:42 -0800460
461 // Force client composition for special cases known only to the front-end.
Lloyd Pique688abd42019-02-15 15:42:24 -0800462 if (isHdrY410() || usesRoundedCorners) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800463 compositionState.forceClientComposition = true;
464 }
465}
466
Lloyd Piquef16688f2019-02-19 17:47:57 -0800467bool Layer::onPreComposition(nsecs_t) {
468 return false;
469}
470
Lloyd Piquea83776c2019-01-29 18:42:32 -0800471void Layer::latchCompositionState(compositionengine::LayerFECompositionState& compositionState,
472 bool includeGeometry) const {
473 if (includeGeometry) {
474 latchGeometry(compositionState);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700475 }
Lloyd Piquef5275482019-01-29 18:42:42 -0800476
477 latchPerFrameState(compositionState);
Lloyd Piquea83776c2019-01-29 18:42:32 -0800478}
Mathias Agopian29a367b2011-07-12 14:51:45 -0700479
Lloyd Piquea83776c2019-01-29 18:42:32 -0800480const char* Layer::getDebugName() const {
481 return mName.string();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800482}
483
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700484void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800485 const auto outputLayer = findOutputLayerForDisplay(display);
486 LOG_FATAL_IF(!outputLayer);
487
488 if (!outputLayer->getState().hwc ||
489 (*outputLayer->getState().hwc).hwcCompositionType !=
490 Hwc2::IComposerClient::Composition::CURSOR) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800491 return;
492 }
493
494 // This gives us only the "orientation" component of the transform
Vishnu Nair33a6eee2019-02-06 13:48:06 -0800495 const State& s(getDrawingState());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800496
497 // Apply the layer's transform, followed by the display's global transform
498 // Here we're guaranteed that the layer's transform preserves rects
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800499 Rect win = getCroppedBufferSize(s);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800500 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700501 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800502 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700503 frame.intersect(display->getViewport(), &frame);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700504 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800505 auto position = displayTransform.transform(frame);
506
Dominik Laskowski7e045462018-05-30 13:02:02 -0700507 auto error =
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800508 (*outputLayer->getState().hwc).hwcLayer->setCursorPosition(position.left, position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800509 ALOGE_IF(error != HWC2::Error::None,
510 "[%s] Failed to set cursor position "
511 "to (%d, %d): %s (%d)",
512 mName.string(), position.left, position.top, to_string(error).c_str(),
513 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800514}
Riley Andrews03414a12014-07-01 14:22:59 -0700515
Mathias Agopian13127d82013-03-05 17:47:11 -0800516// ---------------------------------------------------------------------------
517// drawing...
518// ---------------------------------------------------------------------------
519
Lloyd Piquef16688f2019-02-19 17:47:57 -0800520std::optional<renderengine::LayerSettings> Layer::prepareClientComposition(
521 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
522 if (!getCompositionLayer()) {
523 return {};
524 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800525
Vishnu Nair4351ad52019-02-11 14:13:02 -0800526 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000527 half alpha = getAlpha();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800528 renderengine::LayerSettings layerSettings;
529 layerSettings.geometry.boundaries = bounds;
530 if (targetSettings.useIdentityTransform) {
531 layerSettings.geometry.positionTransform = mat4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000532 } else {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800533 layerSettings.geometry.positionTransform = getTransform().asMatrix4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000534 }
535
536 if (hasColorTransform()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800537 layerSettings.colorTransform = getColorTransform();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000538 }
539
540 const auto roundedCornerState = getRoundedCornerState();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800541 layerSettings.geometry.roundedCornersRadius = roundedCornerState.radius;
542 layerSettings.geometry.roundedCornersCrop = roundedCornerState.cropRect;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000543
Lloyd Piquef16688f2019-02-19 17:47:57 -0800544 layerSettings.alpha = alpha;
545 layerSettings.sourceDataspace = mCurrentDataSpace;
546 return layerSettings;
Mathias Agopian13127d82013-03-05 17:47:11 -0800547}
548
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800549Hwc2::IComposerClient::Composition Layer::getCompositionType(
550 const sp<const DisplayDevice>& display) const {
551 const auto outputLayer = findOutputLayerForDisplay(display);
552 LOG_FATAL_IF(!outputLayer);
553 return outputLayer->getState().hwc ? (*outputLayer->getState().hwc).hwcCompositionType
554 : Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800555}
556
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800557bool Layer::getClearClientTarget(const sp<const DisplayDevice>& display) const {
558 const auto outputLayer = findOutputLayerForDisplay(display);
559 LOG_FATAL_IF(!outputLayer);
560 return outputLayer->getState().clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800561}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800562
Dan Stozacac35382016-01-27 12:21:06 -0800563bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
564 if (point->getFrameNumber() <= mCurrentFrameNumber) {
565 // Don't bother with a SyncPoint, since we've already latched the
566 // relevant frame
567 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700568 }
Robert Carr2e102c92018-10-23 12:11:15 -0700569 if (isRemovedFromCurrentState()) {
570 return false;
571 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700572
Dan Stozacac35382016-01-27 12:21:06 -0800573 Mutex::Autolock lock(mLocalSyncPointMutex);
574 mLocalSyncPoints.push_back(point);
575 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700576}
577
Mathias Agopian13127d82013-03-05 17:47:11 -0800578// ----------------------------------------------------------------------------
579// local state
580// ----------------------------------------------------------------------------
581
David Sodman41fdfc92017-11-06 16:09:56 -0800582bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800583 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700584 return (s.flags & layer_state_t::eLayerSecure);
585}
586
Mathias Agopian13127d82013-03-05 17:47:11 -0800587void Layer::setVisibleRegion(const Region& visibleRegion) {
588 // always called from main thread
589 this->visibleRegion = visibleRegion;
590}
591
592void Layer::setCoveredRegion(const Region& coveredRegion) {
593 // always called from main thread
594 this->coveredRegion = coveredRegion;
595}
596
David Sodman41fdfc92017-11-06 16:09:56 -0800597void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800598 // always called from main thread
599 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
600}
601
Robert Carre5f4f692018-01-12 13:12:28 -0800602void Layer::clearVisibilityRegions() {
603 visibleRegion.clear();
604 visibleNonTransparentRegion.clear();
605 coveredRegion.clear();
606}
607
Mathias Agopian13127d82013-03-05 17:47:11 -0800608// ----------------------------------------------------------------------------
609// transaction
610// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800611
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800612void Layer::pushPendingState() {
613 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700614 return;
615 }
Alec Mourie60041e2019-06-14 18:59:51 -0700616 ATRACE_CALL();
Dan Stoza7dde5992015-05-22 09:51:44 -0700617
Dan Stoza7dde5992015-05-22 09:51:44 -0700618 // If this transaction is waiting on the receipt of a frame, generate a sync
619 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700620 // We don't allow installing sync points after we are removed from the current state
621 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800622 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
623 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800624 if (barrierLayer == nullptr) {
625 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700626 // If we can't promote the layer we are intended to wait on,
627 // then it is expired or otherwise invalid. Allow this transaction
628 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800629 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800630 } else {
chaviw43cb3cb2019-05-31 15:23:41 -0700631 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy, this);
Robert Carr0d480722017-01-10 16:42:54 -0800632 if (barrierLayer->addSyncPoint(syncPoint)) {
Alec Mourie60041e2019-06-14 18:59:51 -0700633 std::stringstream ss;
634 ss << "Adding sync point " << mCurrentState.frameNumber_legacy;
635 ATRACE_NAME(ss.str().c_str());
Dan Stozacac35382016-01-27 12:21:06 -0800636 mRemoteSyncPoints.push_back(std::move(syncPoint));
637 } else {
638 // We already missed the frame we're supposed to synchronize
639 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800640 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800641 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700642 }
643
Dan Stoza7dde5992015-05-22 09:51:44 -0700644 // Wake us up to check if the frame has been received
645 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700646 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700647 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800648 mPendingStates.push_back(mCurrentState);
649 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700650}
651
Pablo Ceballos05289c22016-04-14 15:49:55 -0700652void Layer::popPendingState(State* stateToCommit) {
Alec Mourie60041e2019-06-14 18:59:51 -0700653 ATRACE_CALL();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800654 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700655
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800656 mPendingStates.removeAt(0);
657 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700658}
659
Pablo Ceballos05289c22016-04-14 15:49:55 -0700660bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700661 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800662 while (!mPendingStates.empty()) {
663 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700664 if (mRemoteSyncPoints.empty()) {
665 // If we don't have a sync point for this, apply it anyway. It
666 // will be visually wrong, but it should keep us from getting
667 // into too much trouble.
668 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700669 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700670 stateUpdateAvailable = true;
671 continue;
672 }
673
Marissa Wallf58c14b2018-07-24 10:50:43 -0700674 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800675 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800676 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800677
678 // Signal our end of the sync point and then dispose of it
679 mRemoteSyncPoints.front()->setTransactionApplied();
680 mRemoteSyncPoints.pop_front();
681 continue;
682 }
683
Dan Stoza7dde5992015-05-22 09:51:44 -0700684 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
Alec Mourie60041e2019-06-14 18:59:51 -0700685 ATRACE_NAME("frameIsAvailable");
Dan Stoza7dde5992015-05-22 09:51:44 -0700686 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700687 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700688 stateUpdateAvailable = true;
689
690 // Signal our end of the sync point and then dispose of it
691 mRemoteSyncPoints.front()->setTransactionApplied();
692 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800693 } else {
Alec Mourie60041e2019-06-14 18:59:51 -0700694 ATRACE_NAME("!frameIsAvailable");
Dan Stoza792e5292016-02-11 11:43:58 -0800695 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700696 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700697 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700698 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700699 stateUpdateAvailable = true;
700 }
701 }
702
703 // If we still have pending updates, wake SurfaceFlinger back up and point
704 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800705 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700706 setTransactionFlags(eTransactionNeeded);
707 mFlinger->setTransactionFlags(eTraversalNeeded);
708 }
709
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800710 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700711 return stateUpdateAvailable;
712}
713
Marissa Wall61c58622018-07-18 10:12:20 -0700714uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000715 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800716
Marissa Wall61c58622018-07-18 10:12:20 -0700717 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
718 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700719
David Sodmaneb085e02017-10-05 18:49:04 -0700720 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700721 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000722 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800723 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
724 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
725 " requested={ wh={%4u,%4u} }}\n"
726 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
727 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700728 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700729 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
730 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
731 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
732 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
733 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700734 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
735 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
736 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800737 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700738
Robert Carre392b552017-09-19 12:16:05 -0700739 // Don't let Layer::doTransaction update the drawing state
740 // if we have a pending resize, unless we are in fixed-size mode.
741 // the drawing state will be updated only once we receive a buffer
742 // with the correct size.
743 //
744 // In particular, we want to make sure the clip (which is part
745 // of the geometry state) is latched together with the size but is
746 // latched immediately when no resizing is involved.
747 //
748 // If a sideband stream is attached, however, we want to skip this
749 // optimization so that transactions aren't missed when a buffer
750 // never arrives
751 //
752 // In the case that we don't have a buffer we ignore other factors
753 // and avoid entering the resizePending state. At a high level the
754 // resizePending state is to avoid applying the state of the new buffer
755 // to the old buffer. However in the state where we don't have an old buffer
756 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700757 const bool resizePending =
758 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
759 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
Lloyd Pique0b785d82018-12-04 17:25:27 -0800760 (mActiveBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700761 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800762 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700763 flags |= eDontUpdateGeometryState;
764 }
765 }
766
Robert Carr7bf247e2017-05-18 14:02:49 -0700767 // Here we apply various requested geometry states, depending on our
768 // latching configuration. See Layer.h for a detailed discussion of
769 // how geometry latching is controlled.
770 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000771 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700772
773 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
774 // mode, which causes attributes which normally latch regardless of scaling mode,
775 // to be delayed. We copy the requested state to the active state making sure
776 // to respect these rules (again see Layer.h for a detailed discussion).
777 //
778 // There is an awkward asymmetry in the handling of the crop states in the position
779 // states, as can be seen below. Largely this arises from position and transform
780 // being stored in the same data structure while having different latching rules.
781 // b/38182305
782 //
Marissa Wall61c58622018-07-18 10:12:20 -0700783 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700784 // applyPendingStates in the presence of deferred transactions.
785 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -0700786 float tx = stateToCommit->active_legacy.transform.tx();
787 float ty = stateToCommit->active_legacy.transform.ty();
788 stateToCommit->active_legacy = stateToCommit->requested_legacy;
789 stateToCommit->active_legacy.transform.set(tx, ty);
790 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700791 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700792 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -0700793 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700794 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800795 }
796
Marissa Wall61c58622018-07-18 10:12:20 -0700797 return flags;
798}
799
800uint32_t Layer::doTransaction(uint32_t flags) {
801 ATRACE_CALL();
802
chaviw5aedec92018-10-22 10:40:38 -0700803 if (mLayerDetached) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800804 return flags;
805 }
806
807 if (mChildrenChanged) {
808 flags |= eVisibleRegion;
809 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -0700810 }
811
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800812 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +0000813 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -0700814 if (!applyPendingStates(&c)) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800815 return flags;
Marissa Wall61c58622018-07-18 10:12:20 -0700816 }
817
818 flags = doTransactionResize(flags, &c);
819
Alec Mourib416efd2018-09-06 21:01:59 +0000820 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700821
822 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800823 // invalidate and recompute the visible regions if needed
824 flags |= Layer::eVisibleRegion;
825 }
826
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700827 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800828 // invalidate and recompute the visible regions if needed
829 flags |= eVisibleRegion;
830 this->contentDirty = true;
831
832 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -0700833 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -0700834 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -0800835 }
836
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800837 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -0700838 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800839 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -0700840 }
841
Mathias Agopian13127d82013-03-05 17:47:11 -0800842 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -0700843 commitTransaction(c);
Vishnu Nair8406fd72019-07-30 11:29:31 -0700844 mPendingStatesSnapshot = mPendingStates;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800845 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -0800846 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800847}
848
Pablo Ceballos05289c22016-04-14 15:49:55 -0700849void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800850 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700851}
852
Mathias Agopian13127d82013-03-05 17:47:11 -0800853uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800854 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800855}
856
857uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800858 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -0800859}
860
Robert Carr82364e32016-05-15 11:27:47 -0700861bool Layer::setPosition(float x, float y, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800862 if (mCurrentState.requested_legacy.transform.tx() == x &&
863 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -0800864 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800865 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -0700866
867 // We update the requested and active position simultaneously because
868 // we want to apply the position portion of the transform matrix immediately,
869 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800870 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -0700871 if (immediate && !mFreezeGeometryUpdates) {
872 // Here we directly update the active state
873 // unlike other setters, because we store it within
874 // the transform, but use different latching rules.
875 // b/38182305
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800876 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -0700877 }
Robert Carr7bf247e2017-05-18 14:02:49 -0700878 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -0700879
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800880 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800881 setTransactionFlags(eTransactionNeeded);
882 return true;
883}
Robert Carr82364e32016-05-15 11:27:47 -0700884
Robert Carr1f0a16a2016-10-24 16:27:39 -0700885bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
886 ssize_t idx = mCurrentChildren.indexOf(childLayer);
887 if (idx < 0) {
888 return false;
889 }
890 if (childLayer->setLayer(z)) {
891 mCurrentChildren.removeAt(idx);
892 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800893 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700894 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800895 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700896}
897
Robert Carr503c7042017-09-27 15:06:08 -0700898bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
899 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
900 ssize_t idx = mCurrentChildren.indexOf(childLayer);
901 if (idx < 0) {
902 return false;
903 }
904 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
905 mCurrentChildren.removeAt(idx);
906 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800907 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700908 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800909 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700910}
911
Robert Carrae060832016-11-28 10:51:00 -0800912bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800913 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
914 mCurrentState.sequence++;
915 mCurrentState.z = z;
916 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700917
918 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800919 if (mCurrentState.zOrderRelativeOf != nullptr) {
920 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700921 if (strongRelative != nullptr) {
922 strongRelative->removeZOrderRelative(this);
923 }
chaviw606e5cf2019-03-01 10:12:10 -0800924 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -0700925 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800926 setTransactionFlags(eTransactionNeeded);
927 return true;
928}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700929
Robert Carrdb66e622017-04-10 16:55:57 -0700930void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800931 mCurrentState.zOrderRelatives.remove(relative);
932 mCurrentState.sequence++;
933 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700934 setTransactionFlags(eTransactionNeeded);
935}
936
937void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800938 mCurrentState.zOrderRelatives.add(relative);
939 mCurrentState.modified = true;
940 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -0700941 setTransactionFlags(eTransactionNeeded);
942}
943
chaviw606e5cf2019-03-01 10:12:10 -0800944void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
945 mCurrentState.zOrderRelativeOf = relativeOf;
946 mCurrentState.sequence++;
947 mCurrentState.modified = true;
948 setTransactionFlags(eTransactionNeeded);
949}
950
Robert Carr503d2bd2017-12-04 15:49:47 -0800951bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -0700952 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
953 if (handle == nullptr) {
954 return false;
955 }
956 sp<Layer> relative = handle->owner.promote();
957 if (relative == nullptr) {
958 return false;
959 }
960
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800961 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
962 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -0800963 return false;
964 }
965
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800966 mCurrentState.sequence++;
967 mCurrentState.modified = true;
968 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -0700969
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800970 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -0700971 if (oldZOrderRelativeOf != nullptr) {
972 oldZOrderRelativeOf->removeZOrderRelative(this);
973 }
chaviw606e5cf2019-03-01 10:12:10 -0800974 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -0700975 relative->addZOrderRelative(this);
976
977 setTransactionFlags(eTransactionNeeded);
978
979 return true;
980}
981
Mathias Agopian13127d82013-03-05 17:47:11 -0800982bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800983 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -0700984 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800985 mCurrentState.requested_legacy.w = w;
986 mCurrentState.requested_legacy.h = h;
987 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800988 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -0700989
990 // record the new size, from this point on, when the client request
991 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800992 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -0800993 return true;
994}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800995bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800996 if (mCurrentState.color.a == alpha) return false;
997 mCurrentState.sequence++;
998 mCurrentState.color.a = alpha;
999 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001000 setTransactionFlags(eTransactionNeeded);
1001 return true;
1002}
chaviw13fdc492017-06-27 12:40:18 -07001003
Valerie Haudd0b7572019-01-29 14:59:27 -08001004bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1005 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001006 return false;
Valerie Hauaa194562019-02-05 16:21:38 -08001007 }
1008 mCurrentState.sequence++;
1009 mCurrentState.modified = true;
1010 setTransactionFlags(eTransactionNeeded);
1011
1012 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001013 // create background color layer if one does not yet exist
1014 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
1015 const String8& name = mName + "BackgroundColorLayer";
Evan Roskya1f1e152019-01-24 16:17:46 -08001016 mCurrentState.bgColorLayer = new ColorLayer(
1017 LayerCreationArgs(mFlinger.get(), nullptr, name, 0, 0, flags, LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -07001018
Valerie Haudd0b7572019-01-29 14:59:27 -08001019 // add to child list
1020 addChild(mCurrentState.bgColorLayer);
1021 mFlinger->mLayersAdded = true;
1022 // set up SF to handle added color layer
1023 if (isRemovedFromCurrentState()) {
1024 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1025 }
1026 mFlinger->setTransactionFlags(eTransactionNeeded);
1027 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1028 mCurrentState.bgColorLayer->reparent(nullptr);
1029 mCurrentState.bgColorLayer = nullptr;
1030 return true;
1031 }
1032
1033 mCurrentState.bgColorLayer->setColor(color);
1034 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1035 mCurrentState.bgColorLayer->setAlpha(alpha);
1036 mCurrentState.bgColorLayer->setDataspace(dataspace);
1037
chaviw13fdc492017-06-27 12:40:18 -07001038 return true;
1039}
1040
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001041bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001042 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001043
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001044 mCurrentState.sequence++;
1045 mCurrentState.cornerRadius = cornerRadius;
1046 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001047 setTransactionFlags(eTransactionNeeded);
1048 return true;
1049}
1050
Robert Carrd4ae7f32018-06-07 16:10:57 -07001051bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1052 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001053 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001054 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1055
1056 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1057 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1058 return false;
1059 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001060 mCurrentState.sequence++;
1061 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1062 matrix.dsdy);
1063 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001064 setTransactionFlags(eTransactionNeeded);
1065 return true;
1066}
Marissa Wall61c58622018-07-18 10:12:20 -07001067
Mathias Agopian13127d82013-03-05 17:47:11 -08001068bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001069 mCurrentState.requestedTransparentRegion_legacy = transparent;
1070 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001071 setTransactionFlags(eTransactionNeeded);
1072 return true;
1073}
1074bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001075 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1076 if (mCurrentState.flags == newFlags) return false;
1077 mCurrentState.sequence++;
1078 mCurrentState.flags = newFlags;
1079 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001080 setTransactionFlags(eTransactionNeeded);
1081 return true;
1082}
Robert Carr99e27f02016-06-16 15:18:02 -07001083
Marissa Wallf58c14b2018-07-24 10:50:43 -07001084bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001085 if (mCurrentState.requestedCrop_legacy == crop) return false;
1086 mCurrentState.sequence++;
1087 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001088 if (immediate && !mFreezeGeometryUpdates) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001089 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001090 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001091 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1092
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001093 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001094 setTransactionFlags(eTransactionNeeded);
1095 return true;
1096}
Robert Carr8d5227b2017-03-16 15:41:03 -07001097
Robert Carrc3574f72016-03-24 12:19:32 -07001098bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001099 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001100 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001101 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001102 return true;
1103}
1104
Evan Roskyef876c92019-01-25 17:46:06 -08001105bool Layer::setMetadata(const LayerMetadata& data) {
1106 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001107 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001108 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001109 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001110 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001111}
1112
Mathias Agopian13127d82013-03-05 17:47:11 -08001113bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001114 if (mCurrentState.layerStack == layerStack) return false;
1115 mCurrentState.sequence++;
1116 mCurrentState.layerStack = layerStack;
1117 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001118 setTransactionFlags(eTransactionNeeded);
1119 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001120}
1121
Peiyong Linc502cb72019-03-01 15:00:23 -08001122bool Layer::setColorSpaceAgnostic(const bool agnostic) {
1123 if (mCurrentState.colorSpaceAgnostic == agnostic) {
1124 return false;
1125 }
1126 mCurrentState.sequence++;
1127 mCurrentState.colorSpaceAgnostic = agnostic;
1128 mCurrentState.modified = true;
1129 setTransactionFlags(eTransactionNeeded);
1130 return true;
1131}
1132
Robert Carr1f0a16a2016-10-24 16:27:39 -07001133uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001134 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001135 if (p == nullptr) {
1136 return getDrawingState().layerStack;
1137 }
1138 return p->getLayerStack();
1139}
1140
Marissa Wallf58c14b2018-07-24 10:50:43 -07001141void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Alec Mourie60041e2019-06-14 18:59:51 -07001142 ATRACE_CALL();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001143 mCurrentState.barrierLayer_legacy = barrierLayer;
1144 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001145 // We don't set eTransactionNeeded, because just receiving a deferral
1146 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001147 mCurrentState.modified = true;
1148 pushPendingState();
1149 mCurrentState.barrierLayer_legacy = nullptr;
1150 mCurrentState.frameNumber_legacy = 0;
1151 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001152}
1153
Marissa Wallf58c14b2018-07-24 10:50:43 -07001154void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001155 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001156 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001157}
1158
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001159// ----------------------------------------------------------------------------
1160// pageflip handling...
1161// ----------------------------------------------------------------------------
1162
Robert Carr1f0a16a2016-10-24 16:27:39 -07001163bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001164 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001165 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001166 if (parent != nullptr && parent->isHiddenByPolicy()) {
1167 return true;
1168 }
Robert Carr1c5481e2019-07-01 14:42:27 -07001169 if (usingRelativeZ(LayerVector::StateSet::Drawing)) {
1170 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
1171 if (zOrderRelativeOf != nullptr) {
1172 if (zOrderRelativeOf->isHiddenByPolicy()) {
1173 return true;
1174 }
1175 }
1176 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001177 return s.flags & layer_state_t::eLayerHidden;
1178}
1179
David Sodman41fdfc92017-11-06 16:09:56 -08001180uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001181 // TODO: should we do something special if mSecure is set?
1182 if (mProtectedByApp) {
1183 // need a hardware-protected path to external video sink
1184 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001185 }
Riley Andrews03414a12014-07-01 14:22:59 -07001186 if (mPotentialCursor) {
1187 usage |= GraphicBuffer::USAGE_CURSOR;
1188 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001189 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001190 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001191}
1192
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001193void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001194 uint32_t orientation = 0;
Vishnu Nair5eb3f062019-04-08 08:21:03 -07001195 // Disable setting transform hint if the debug flag is set.
1196 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001197 // The transform hint is used to improve performance, but we can
1198 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001199 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001200 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001201 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001202 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001203 orientation = 0;
1204 }
1205 }
David Sodmaneb085e02017-10-05 18:49:04 -07001206 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001207}
1208
Mathias Agopian13127d82013-03-05 17:47:11 -08001209// ----------------------------------------------------------------------------
1210// debugging
1211// ----------------------------------------------------------------------------
1212
Marissa Wall61c58622018-07-18 10:12:20 -07001213// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001214LayerDebugInfo Layer::getLayerDebugInfo() const {
1215 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001216 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001217 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001218 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001219 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
chaviw8a01fa42019-08-19 12:39:31 -07001220 info.mType = getType();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001221 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001222 info.mVisibleRegion = visibleRegion;
1223 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001224 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001225 info.mX = ds.active_legacy.transform.tx();
1226 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001227 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001228 info.mWidth = ds.active_legacy.w;
1229 info.mHeight = ds.active_legacy.h;
1230 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001231 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001232 info.mFlags = ds.flags;
1233 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001234 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001235 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1236 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1237 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1238 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001239 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001240 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001241 if (buffer != 0) {
1242 info.mActiveBufferWidth = buffer->getWidth();
1243 info.mActiveBufferHeight = buffer->getHeight();
1244 info.mActiveBufferStride = buffer->getStride();
1245 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001246 } else {
1247 info.mActiveBufferWidth = 0;
1248 info.mActiveBufferHeight = 0;
1249 info.mActiveBufferStride = 0;
1250 info.mActiveBufferFormat = 0;
1251 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001252 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001253 info.mNumQueuedFrames = getQueuedFrameCount();
1254 info.mRefreshPending = isBufferLatched();
1255 info.mIsOpaque = isOpaque(ds);
1256 info.mContentDirty = contentDirty;
1257 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001258}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001259
Yiwei Zhang5434a782018-12-05 18:06:32 -08001260void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001261 result.append("-------------------------------");
1262 result.append("-------------------------------");
1263 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001264 result.append(" Layer name\n");
1265 result.append(" Z | ");
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001266 result.append(" Window Type | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001267 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001268 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001269 result.append(" Disp Frame (LTRB) | ");
1270 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001271 result.append("-------------------------------");
1272 result.append("-------------------------------");
1273 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001274}
1275
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001276void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1277 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1278 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001279 return;
1280 }
1281
Yiwei Zhang5434a782018-12-05 18:06:32 -08001282 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001283 if (mName.length() > 77) {
1284 std::string shortened;
1285 shortened.append(mName.string(), 36);
1286 shortened.append("[...]");
1287 shortened.append(mName.string() + (mName.length() - 36), 36);
Yiwei Zhang5434a782018-12-05 18:06:32 -08001288 name = shortened;
Dan Stozae22aec72016-08-01 13:20:59 -07001289 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001290 name = std::string(mName.string(), mName.size());
Dan Stozae22aec72016-08-01 13:20:59 -07001291 }
1292
Yiwei Zhang5434a782018-12-05 18:06:32 -08001293 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001294
Alec Mourib416efd2018-09-06 21:01:59 +00001295 const State& layerState(getDrawingState());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001296 const auto& compositionState = outputLayer->getState();
1297
Chia-I Wu1e043612018-03-01 09:45:09 -08001298 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001299 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001300 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001301 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001302 }
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001303 StringAppendF(&result, " %10d | ", mWindowType);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001304 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
1305 StringAppendF(&result, "%10s | ",
1306 toString(getCompositionLayer() ? compositionState.bufferTransform
1307 : static_cast<Hwc2::Transform>(0))
1308 .c_str());
1309 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001310 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001311 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001312 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1313 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001314
Yichi Chen6ca35192018-05-29 12:20:43 +08001315 result.append("- - - - - - - - - - - - - - - -");
1316 result.append("- - - - - - - - - - - - - - - -");
1317 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001318}
Dan Stozae22aec72016-08-01 13:20:59 -07001319
Yiwei Zhang5434a782018-12-05 18:06:32 -08001320void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001321 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001322}
1323
Svetoslavd85084b2014-03-20 10:28:31 -07001324void Layer::clearFrameStats() {
1325 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001326}
1327
Jamie Gennis6547ff42013-07-16 20:12:42 -07001328void Layer::logFrameStats() {
1329 mFrameTracker.logAndResetStats(mName);
1330}
1331
Svetoslavd85084b2014-03-20 10:28:31 -07001332void Layer::getFrameStats(FrameStats* outStats) const {
1333 mFrameTracker.getStats(outStats);
1334}
1335
Yiwei Zhang5434a782018-12-05 18:06:32 -08001336void Layer::dumpFrameEvents(std::string& result) {
chaviw8a01fa42019-08-19 12:39:31 -07001337 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().string(), getType(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001338 Mutex::Autolock lock(mFrameEventHistoryMutex);
1339 mFrameEventHistory.checkFencesForCompletion();
1340 mFrameEventHistory.dump(result);
1341}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001342
Brian Anderson5ea5e592016-12-01 16:54:33 -08001343void Layer::onDisconnect() {
1344 Mutex::Autolock lock(mFrameEventHistoryMutex);
1345 mFrameEventHistory.onDisconnect();
Yiwei Zhangaf8ee942018-11-22 00:15:23 -08001346 mFlinger->mTimeStats->onDestroy(getSequence());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001347}
1348
Brian Anderson3890c392016-07-25 12:48:08 -07001349void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001350 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001351 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001352 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1353 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001354 }
1355
Brian Andersond6927fb2016-07-23 23:37:30 -07001356 Mutex::Autolock lock(mFrameEventHistoryMutex);
1357 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001358 // If there are any unsignaled fences in the aquire timeline at this
1359 // point, the previously queued frame hasn't been latched yet. Go ahead
1360 // and try to get the signal time here so the syscall is taken out of
1361 // the main thread's critical path.
1362 mAcquireTimeline.updateSignalTimes();
1363 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001364 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001365 mFrameEventHistory.addQueue(*newTimestamps);
1366 }
1367
Brian Anderson3890c392016-07-25 12:48:08 -07001368 if (outDelta) {
1369 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001370 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001371}
Dan Stozae77c7662016-05-13 11:37:28 -07001372
Chia-I Wu98f1c102017-05-30 14:54:08 -07001373size_t Layer::getChildrenCount() const {
1374 size_t count = 0;
1375 for (const sp<Layer>& child : mCurrentChildren) {
1376 count += 1 + child->getChildrenCount();
1377 }
1378 return count;
1379}
1380
Robert Carr1f0a16a2016-10-24 16:27:39 -07001381void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001382 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001383 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001384
Robert Carr1f0a16a2016-10-24 16:27:39 -07001385 mCurrentChildren.add(layer);
1386 layer->setParent(this);
1387}
1388
1389ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001390 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001391 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001392
Robert Carr1323c952019-01-28 18:13:27 -08001393 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001394 return mCurrentChildren.remove(layer);
1395}
1396
Robert Carr1db73f62016-12-21 12:58:51 -08001397bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1398 sp<Handle> handle = nullptr;
1399 sp<Layer> newParent = nullptr;
1400 if (newParentHandle == nullptr) {
1401 return false;
1402 }
1403 handle = static_cast<Handle*>(newParentHandle.get());
1404 newParent = handle->owner.promote();
1405 if (newParent == nullptr) {
1406 ALOGE("Unable to promote Layer handle");
1407 return false;
1408 }
1409
chaviw5aedec92018-10-22 10:40:38 -07001410 if (attachChildren()) {
1411 setTransactionFlags(eTransactionNeeded);
1412 }
Robert Carr1db73f62016-12-21 12:58:51 -08001413 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001414 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001415 }
1416 mCurrentChildren.clear();
1417
1418 return true;
1419}
1420
Robert Carr15eae092018-03-23 13:43:53 -07001421void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001422 for (const sp<Layer>& child : mDrawingChildren) {
1423 child->mDrawingParent = newParent;
Vishnu Nairc652ff82019-03-15 12:48:54 -07001424 child->computeBounds(newParent->mBounds,
1425 newParent->getTransformWithScale(
1426 newParent->getBufferScaleTransform()));
Robert Carr578038f2018-03-09 12:25:24 -08001427 }
1428}
1429
chaviwf1961f72017-09-18 16:41:07 -07001430bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001431 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001432
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001433 // While layers are detached, we allow most operations
1434 // and simply halt performing the actual transaction. However
1435 // for reparent != null we would enter the mRemovedFromCurrentState
1436 // state, regardless of whether doTransaction was called, and
1437 // so we need to prevent the update here.
1438 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001439 return false;
1440 }
1441
Robert Carr54cf5b12019-01-25 14:02:28 -08001442 sp<Layer> newParent;
1443 if (newParentHandle != nullptr) {
1444 auto handle = static_cast<Handle*>(newParentHandle.get());
1445 newParent = handle->owner.promote();
1446 if (newParent == nullptr) {
1447 ALOGE("Unable to promote Layer handle");
1448 return false;
1449 }
1450 if (newParent == this) {
1451 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1452 return false;
1453 }
1454 }
1455
chaviwf1961f72017-09-18 16:41:07 -07001456 sp<Layer> parent = getParent();
1457 if (parent != nullptr) {
1458 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001459 }
1460
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001461 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001462 newParent->addChild(this);
1463 if (!newParent->isRemovedFromCurrentState()) {
1464 addToCurrentState();
1465 } else {
1466 onRemovedFromCurrentState();
1467 }
1468
1469 if (mLayerDetached) {
1470 mLayerDetached = false;
1471 callSetTransactionFlags = true;
1472 }
1473 } else {
1474 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001475 }
1476
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001477 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001478 setTransactionFlags(eTransactionNeeded);
1479 }
chaviw06178942017-07-27 10:25:59 -07001480 return true;
1481}
1482
Robert Carr9524cb32017-02-13 11:32:32 -08001483bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001484 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001485 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001486 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001487 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001488 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001489 child->detachChildren();
chaviw43cb3cb2019-05-31 15:23:41 -07001490 child->removeRemoteSyncPoints();
Robert Carr9524cb32017-02-13 11:32:32 -08001491 }
Robert Carr7f619b22017-11-06 12:56:35 -08001492 }
Robert Carr9524cb32017-02-13 11:32:32 -08001493
1494 return true;
1495}
1496
chaviw5aedec92018-10-22 10:40:38 -07001497bool Layer::attachChildren() {
1498 bool changed = false;
1499 for (const sp<Layer>& child : mCurrentChildren) {
1500 sp<Client> parentClient = mClientRef.promote();
1501 sp<Client> client(child->mClientRef.promote());
1502 if (client != nullptr && parentClient != client) {
1503 if (child->mLayerDetached) {
1504 child->mLayerDetached = false;
1505 changed = true;
1506 }
1507 changed |= child->attachChildren();
1508 }
1509 }
1510
1511 return changed;
1512}
1513
Peiyong Lind3788632018-09-18 16:01:31 -07001514bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001515 static const mat4 identityMatrix = mat4();
1516
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001517 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001518 return false;
1519 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001520 ++mCurrentState.sequence;
1521 mCurrentState.colorTransform = matrix;
1522 mCurrentState.hasColorTransform = matrix != identityMatrix;
1523 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001524 setTransactionFlags(eTransactionNeeded);
1525 return true;
1526}
1527
chaviwf66724d2018-11-28 16:35:21 -08001528mat4 Layer::getColorTransform() const {
1529 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1530 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1531 colorTransform = parent->getColorTransform() * colorTransform;
1532 }
1533 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001534}
1535
1536bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001537 bool hasColorTransform = getDrawingState().hasColorTransform;
1538 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1539 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1540 }
1541 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001542}
1543
Chia-I Wu11481472018-05-04 10:43:19 -07001544bool Layer::isLegacyDataSpace() const {
1545 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001546 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001547 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001548}
1549
Robert Carr1f0a16a2016-10-24 16:27:39 -07001550void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001551 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001552}
1553
Robert Carr1f0a16a2016-10-24 16:27:39 -07001554int32_t Layer::getZ() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001555 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001556}
1557
Robert Carr1c5481e2019-07-01 14:42:27 -07001558bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) const {
Robert Carr29abff82017-12-04 13:51:20 -08001559 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001560 const State& state = useDrawing ? mDrawingState : mCurrentState;
Robert Carr29abff82017-12-04 13:51:20 -08001561 return state.zOrderRelativeOf != nullptr;
1562}
1563
David Sodman41fdfc92017-11-06 16:09:56 -08001564__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001565 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001566 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1567 "makeTraversalList received invalid stateSet");
1568 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1569 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001570 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001571
Robert Carr29abff82017-12-04 13:51:20 -08001572 if (state.zOrderRelatives.size() == 0) {
1573 *outSkipRelativeZUsers = true;
1574 return children;
1575 }
1576
chaviwfd462612018-05-31 16:11:27 -07001577 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001578 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001579 sp<Layer> strongRelative = weakRelative.promote();
1580 if (strongRelative != nullptr) {
1581 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001582 }
1583 }
1584
Dan Stoza412903f2017-04-27 13:42:17 -07001585 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001586 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
Robert Carr503c7042017-09-27 15:06:08 -07001587 if (childState.zOrderRelativeOf != nullptr) {
1588 continue;
1589 }
Robert Carrdb66e622017-04-10 16:55:57 -07001590 traverse.add(child);
1591 }
1592
1593 return traverse;
1594}
1595
Robert Carr1f0a16a2016-10-24 16:27:39 -07001596/**
Robert Carrdb66e622017-04-10 16:55:57 -07001597 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001598 */
Dan Stoza412903f2017-04-27 13:42:17 -07001599void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001600 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1601 // produce a new list for traversing, including our relatives, and not including our children
1602 // who are relatives of another surface. In the case that there are no relative Z,
1603 // makeTraversalList returns our children directly to avoid significant overhead.
1604 // However in this case we need to take the responsibility for filtering children which
1605 // are relatives of another surface here.
1606 bool skipRelativeZUsers = false;
1607 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001608
Robert Carr1f0a16a2016-10-24 16:27:39 -07001609 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001610 for (; i < list.size(); i++) {
1611 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001612 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1613 continue;
1614 }
1615
Robert Carrdb66e622017-04-10 16:55:57 -07001616 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001617 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001618 }
Dan Stoza412903f2017-04-27 13:42:17 -07001619 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001620 }
Robert Carr29abff82017-12-04 13:51:20 -08001621
Dan Stoza412903f2017-04-27 13:42:17 -07001622 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001623 for (; i < list.size(); i++) {
1624 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001625
Robert Carr29abff82017-12-04 13:51:20 -08001626 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1627 continue;
1628 }
Dan Stoza412903f2017-04-27 13:42:17 -07001629 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001630 }
1631}
1632
1633/**
Robert Carrdb66e622017-04-10 16:55:57 -07001634 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001635 */
Dan Stoza412903f2017-04-27 13:42:17 -07001636void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1637 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001638 // See traverseInZOrder for documentation.
1639 bool skipRelativeZUsers = false;
1640 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001641
Robert Carr1f0a16a2016-10-24 16:27:39 -07001642 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001643 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001644 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001645
1646 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1647 continue;
1648 }
1649
Robert Carrdb66e622017-04-10 16:55:57 -07001650 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001651 break;
1652 }
Dan Stoza412903f2017-04-27 13:42:17 -07001653 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001654 }
Dan Stoza412903f2017-04-27 13:42:17 -07001655 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001656 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001657 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001658
1659 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1660 continue;
1661 }
1662
Dan Stoza412903f2017-04-27 13:42:17 -07001663 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001664 }
1665}
1666
chaviw4b129c22018-04-09 16:19:43 -07001667LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1668 const std::vector<Layer*>& layersInTree) {
1669 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1670 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001671 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1672 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001673 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001674
chaviwfd462612018-05-31 16:11:27 -07001675 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001676 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1677 sp<Layer> strongRelative = weakRelative.promote();
1678 // Only add relative layers that are also descendents of the top most parent of the tree.
1679 // If a relative layer is not a descendent, then it should be ignored.
1680 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1681 traverse.add(strongRelative);
1682 }
1683 }
1684
1685 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001686 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001687 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1688 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1689 // the child here since it won't be added later as a relative.
1690 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1691 childState.zOrderRelativeOf.promote().get())) {
1692 continue;
1693 }
1694 traverse.add(child);
1695 }
1696
1697 return traverse;
1698}
1699
1700void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1701 LayerVector::StateSet stateSet,
1702 const LayerVector::Visitor& visitor) {
1703 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001704
1705 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001706 for (; i < list.size(); i++) {
1707 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001708 if (relative->getZ() >= 0) {
1709 break;
1710 }
chaviw4b129c22018-04-09 16:19:43 -07001711 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001712 }
chaviw4b129c22018-04-09 16:19:43 -07001713
chaviwa76b2712017-09-20 12:02:26 -07001714 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001715 for (; i < list.size(); i++) {
1716 const auto& relative = list[i];
1717 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001718 }
1719}
1720
chaviw4b129c22018-04-09 16:19:43 -07001721std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1722 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1723 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1724
1725 std::vector<Layer*> layersInTree = {this};
1726 for (size_t i = 0; i < children.size(); i++) {
1727 const auto& child = children[i];
1728 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1729 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1730 }
1731
1732 return layersInTree;
1733}
1734
1735void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1736 const LayerVector::Visitor& visitor) {
1737 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1738 std::sort(layersInTree.begin(), layersInTree.end());
1739 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1740}
1741
Peiyong Linefefaac2018-08-17 12:27:51 -07001742ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001743 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001744}
1745
chaviw13fdc492017-06-27 12:40:18 -07001746half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001747 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001748
chaviw13fdc492017-06-27 12:40:18 -07001749 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1750 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001751}
Robert Carr6452f122017-03-21 10:41:29 -07001752
chaviw13fdc492017-06-27 12:40:18 -07001753half4 Layer::getColor() const {
1754 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001755 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001756}
Robert Carr6452f122017-03-21 10:41:29 -07001757
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001758Layer::RoundedCornerState Layer::getRoundedCornerState() const {
1759 const auto& p = mDrawingParent.promote();
1760 if (p != nullptr) {
Peiyong Lin27016a92019-03-29 17:36:08 +00001761 RoundedCornerState parentState = p->getRoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001762 if (parentState.radius > 0) {
1763 ui::Transform t = getActiveTransform(getDrawingState());
1764 t = t.inverse();
1765 parentState.cropRect = t.transform(parentState.cropRect);
1766 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1767 // but a transform matrix can define horizontal and vertical scales.
1768 // Let's take the average between both of them and pass into the shader, practically we
1769 // never do this type of transformation on windows anyway.
1770 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
1771 return parentState;
1772 }
1773 }
1774 const float radius = getDrawingState().cornerRadius;
Peiyong Linb9ff23e2019-04-08 11:04:59 -07001775 return radius > 0 && getCrop(getDrawingState()).isValid()
1776 ? RoundedCornerState(getCrop(getDrawingState()).toFloatRect(), radius)
1777 : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001778}
1779
Robert Carr1f0a16a2016-10-24 16:27:39 -07001780void Layer::commitChildList() {
1781 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1782 const auto& child = mCurrentChildren[i];
1783 child->commitChildList();
1784 }
1785 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001786 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001787}
1788
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001789static wp<Layer> extractLayerFromBinder(const wp<IBinder>& weakBinderHandle) {
1790 if (weakBinderHandle == nullptr) {
1791 return nullptr;
1792 }
1793 sp<IBinder> binderHandle = weakBinderHandle.promote();
1794 if (binderHandle == nullptr) {
1795 return nullptr;
1796 }
1797 sp<Layer::Handle> handle = static_cast<Layer::Handle*>(binderHandle.get());
1798 if (handle == nullptr) {
1799 return nullptr;
1800 }
1801 return handle->owner;
1802}
1803
Robert Carr720e5062018-07-30 17:45:14 -07001804void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001805 mCurrentState.inputInfo = info;
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001806 mCurrentState.touchableRegionCrop = extractLayerFromBinder(info.touchableRegionCropHandle);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001807 mCurrentState.modified = true;
1808 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07001809 setTransactionFlags(eTransactionNeeded);
1810}
1811
Vishnu Nair8406fd72019-07-30 11:29:31 -07001812void Layer::writeToProtoDrawingState(LayerProto* layerInfo, uint32_t traceFlags) const {
1813 ui::Transform transform = getTransform();
1814
1815 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
1816 for (const auto& pendingState : mPendingStatesSnapshot) {
1817 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
1818 if (barrierLayer != nullptr) {
1819 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
1820 barrierLayerProto->set_id(barrierLayer->sequence);
1821 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
1822 }
1823 }
1824
1825 auto buffer = mActiveBuffer;
1826 if (buffer != nullptr) {
1827 LayerProtoHelper::writeToProto(buffer,
1828 [&]() { return layerInfo->mutable_active_buffer(); });
1829 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
1830 layerInfo->mutable_buffer_transform());
1831 }
1832 layerInfo->set_invalidate(contentDirty);
1833 layerInfo->set_is_protected(isProtected());
1834 layerInfo->set_dataspace(
1835 dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
1836 layerInfo->set_queued_frames(getQueuedFrameCount());
1837 layerInfo->set_refresh_pending(isBufferLatched());
1838 layerInfo->set_curr_frame(mCurrentFrameNumber);
1839 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
1840
1841 layerInfo->set_corner_radius(getRoundedCornerState().radius);
1842 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1843 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
1844 [&]() { return layerInfo->mutable_position(); });
1845 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
1846 LayerProtoHelper::writeToProto(visibleRegion,
1847 [&]() { return layerInfo->mutable_visible_region(); });
1848 LayerProtoHelper::writeToProto(surfaceDamageRegion,
1849 [&]() { return layerInfo->mutable_damage_region(); });
1850 }
1851
1852 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
1853 LayerProtoHelper::writeToProto(mSourceBounds,
1854 [&]() { return layerInfo->mutable_source_bounds(); });
1855 LayerProtoHelper::writeToProto(mScreenBounds,
1856 [&]() { return layerInfo->mutable_screen_bounds(); });
1857 }
1858}
1859
1860void Layer::writeToProtoCommonState(LayerProto* layerInfo, LayerVector::StateSet stateSet,
1861 uint32_t traceFlags) const {
chaviw1d044282017-09-27 12:19:28 -07001862 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1863 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001864 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07001865
Peiyong Linefefaac2018-08-17 12:27:51 -07001866 ui::Transform requestedTransform = state.active_legacy.transform;
chaviw1d044282017-09-27 12:19:28 -07001867
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001868 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
1869 layerInfo->set_id(sequence);
1870 layerInfo->set_name(getName().c_str());
chaviw8a01fa42019-08-19 12:39:31 -07001871 layerInfo->set_type(getType());
chaviw1d044282017-09-27 12:19:28 -07001872
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001873 for (const auto& child : children) {
1874 layerInfo->add_children(child->sequence);
chaviw1d044282017-09-27 12:19:28 -07001875 }
chaviw1d044282017-09-27 12:19:28 -07001876
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001877 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1878 sp<Layer> strongRelative = weakRelative.promote();
1879 if (strongRelative != nullptr) {
1880 layerInfo->add_relatives(strongRelative->sequence);
1881 }
chaviwadc40c22018-07-10 16:57:27 -07001882 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001883
1884 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
1885 [&]() { return layerInfo->mutable_transparent_region(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001886
1887 layerInfo->set_layer_stack(getLayerStack());
1888 layerInfo->set_z(state.z);
1889
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001890 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(),
1891 [&]() {
1892 return layerInfo->mutable_requested_position();
1893 });
1894
1895 LayerProtoHelper::writeSizeToProto(state.active_legacy.w, state.active_legacy.h,
1896 [&]() { return layerInfo->mutable_size(); });
1897
1898 LayerProtoHelper::writeToProto(state.crop_legacy,
1899 [&]() { return layerInfo->mutable_crop(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001900
1901 layerInfo->set_is_opaque(isOpaque(state));
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001902
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001903
1904 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1905 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
1906 LayerProtoHelper::writeToProto(state.color,
1907 [&]() { return layerInfo->mutable_requested_color(); });
1908 layerInfo->set_flags(state.flags);
1909
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001910 LayerProtoHelper::writeToProto(requestedTransform,
1911 layerInfo->mutable_requested_transform());
1912
1913 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
1914 if (parent != nullptr) {
1915 layerInfo->set_parent(parent->sequence);
1916 } else {
1917 layerInfo->set_parent(-1);
1918 }
1919
1920 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1921 if (zOrderRelativeOf != nullptr) {
1922 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1923 } else {
1924 layerInfo->set_z_order_relative_of(-1);
1925 }
chaviwadc40c22018-07-10 16:57:27 -07001926 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001927
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001928 if (traceFlags & SurfaceTracing::TRACE_INPUT) {
1929 LayerProtoHelper::writeToProto(state.inputInfo, state.touchableRegionCrop,
1930 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001931 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001932
1933 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
1934 auto protoMap = layerInfo->mutable_metadata();
1935 for (const auto& entry : state.metadata.mMap) {
1936 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
1937 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001938 }
chaviw1d044282017-09-27 12:19:28 -07001939}
1940
Robert Carr2e102c92018-10-23 12:11:15 -07001941bool Layer::isRemovedFromCurrentState() const {
1942 return mRemovedFromCurrentState;
1943}
1944
Arthur Hungd20b2702019-01-14 18:16:16 +08001945InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001946 InputWindowInfo info = mDrawingState.inputInfo;
Robert Carr720e5062018-07-30 17:45:14 -07001947
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001948 if (info.displayId == ADISPLAY_ID_NONE) {
1949 info.displayId = mDrawingState.layerStack;
1950 }
1951
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001952 ui::Transform t = getTransform();
1953 const float xScale = t.sx();
1954 const float yScale = t.sy();
Ady Abraham282f1d72019-07-24 18:05:56 -07001955 int32_t xSurfaceInset = info.surfaceInset;
1956 int32_t ySurfaceInset = info.surfaceInset;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001957 if (xScale != 1.0f || yScale != 1.0f) {
Ady Abraham282f1d72019-07-24 18:05:56 -07001958 info.windowXScale *= (xScale != 0.0f) ? 1.0f / xScale : 0.0f;
1959 info.windowYScale *= (yScale != 0.0f) ? 1.0f / yScale : 0.0f;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001960 info.touchableRegion.scaleSelf(xScale, yScale);
Ady Abraham282f1d72019-07-24 18:05:56 -07001961 xSurfaceInset = std::round(xSurfaceInset * xScale);
1962 ySurfaceInset = std::round(ySurfaceInset * yScale);
Riddle Hsu39d4aa52018-11-30 20:46:53 +08001963 }
Robert Carre07e1032018-11-26 12:55:53 -08001964
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001965 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001966 // If this is a portal window, set the touchableRegion to the layerBounds.
1967 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
1968 ? getBufferSize(getDrawingState())
1969 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08001970 if (!layerBounds.isValid()) {
1971 layerBounds = getCroppedBufferSize(getDrawingState());
1972 }
Vishnu Nair8033a492018-12-05 07:27:23 -08001973 layerBounds = t.transform(layerBounds);
Ady Abraham282f1d72019-07-24 18:05:56 -07001974
1975 // clamp inset to layer bounds
1976 xSurfaceInset = (xSurfaceInset >= 0) ? std::min(xSurfaceInset, layerBounds.getWidth() / 2) : 0;
1977 ySurfaceInset = (ySurfaceInset >= 0) ? std::min(ySurfaceInset, layerBounds.getHeight() / 2) : 0;
1978
Arthur Hung118b1142019-05-08 21:25:59 +08001979 layerBounds.inset(xSurfaceInset, ySurfaceInset, xSurfaceInset, ySurfaceInset);
Vishnu Nair8033a492018-12-05 07:27:23 -08001980
Arthur Hungd20b2702019-01-14 18:16:16 +08001981 // Input coordinate should match the layer bounds.
1982 info.frameLeft = layerBounds.left;
1983 info.frameTop = layerBounds.top;
1984 info.frameRight = layerBounds.right;
1985 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08001986
1987 // Position the touchable region relative to frame screen location and restrict it to frame
1988 // bounds.
1989 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08001990 info.visible = canReceiveInput();
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001991
1992 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
1993 if (info.replaceTouchableRegionWithCrop) {
1994 if (cropLayer == nullptr) {
1995 info.touchableRegion = Region(Rect{mScreenBounds});
1996 } else {
1997 info.touchableRegion = Region(Rect{cropLayer->mScreenBounds});
1998 }
1999 } else if (cropLayer != nullptr) {
2000 info.touchableRegion = info.touchableRegion.intersect(Rect{cropLayer->mScreenBounds});
2001 }
2002
Robert Carr720e5062018-07-30 17:45:14 -07002003 return info;
2004}
2005
2006bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002007 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002008}
2009
Lloyd Piquefeb73d72018-12-04 17:23:44 -08002010std::shared_ptr<compositionengine::Layer> Layer::getCompositionLayer() const {
2011 return nullptr;
2012}
2013
chaviw3e727cd2019-01-31 13:41:05 -08002014bool Layer::canReceiveInput() const {
2015 return isVisible();
2016}
2017
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002018compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
2019 const sp<const DisplayDevice>& display) const {
2020 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionLayer().get());
2021}
2022
Mathias Agopian13127d82013-03-05 17:47:11 -08002023// ---------------------------------------------------------------------------
2024
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002025}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002026
2027#if defined(__gl_h_)
2028#error "don't include gl/gl.h in this file"
2029#endif
2030
2031#if defined(__gl2_h_)
2032#error "don't include gl2/gl2.h in this file"
2033#endif