blob: 3ca6ef500de9f9825a72d2a2b51a63572b4bfae4 [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
Mathias Agopian13127d82013-03-05 17:47:11 -080022#include <math.h>
David Sodman41fdfc92017-11-06 16:09:56 -080023#include <stdint.h>
24#include <stdlib.h>
25#include <sys/types.h>
chaviw4b129c22018-04-09 16:19:43 -070026#include <algorithm>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080027#include <mutex>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080028
Yiwei Zhang5434a782018-12-05 18:06:32 -080029#include <android-base/stringprintf.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080030#include <compositionengine/Display.h>
Lloyd Pique0b785d82018-12-04 17:25:27 -080031#include <compositionengine/Layer.h>
Lloyd Piquea83776c2019-01-29 18:42:32 -080032#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080033#include <compositionengine/OutputLayer.h>
Lloyd Pique0b785d82018-12-04 17:25:27 -080034#include <compositionengine/impl/LayerCompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080035#include <compositionengine/impl/OutputLayerCompositionState.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070036#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070037#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070038#include <cutils/properties.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080039#include <gui/BufferItem.h>
40#include <gui/LayerDebugInfo.h>
41#include <gui/Surface.h>
42#include <renderengine/RenderEngine.h>
43#include <ui/DebugUtils.h>
44#include <ui/GraphicBuffer.h>
45#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046#include <utils/Errors.h>
47#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080048#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080050#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051
Yiwei Zhang60d1a192018-03-07 14:52:28 -080052#include "BufferLayer.h"
Valerie Haudd0b7572019-01-29 14:59:27 -080053#include "ColorLayer.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020054#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070055#include "DisplayDevice.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080056#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057#include "Layer.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080058#include "LayerProtoHelper.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070059#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070060#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061#include "SurfaceFlinger.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080062#include "TimeStats/TimeStats.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070063
David Sodman41fdfc92017-11-06 16:09:56 -080064#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066namespace android {
67
Yiwei Zhang5434a782018-12-05 18:06:32 -080068using base::StringAppendF;
69
Lloyd Piquef1c675b2018-09-12 20:45:39 -070070std::atomic<int32_t> Layer::sSequence{1};
Mathias Agopian13127d82013-03-05 17:47:11 -080071
Lloyd Pique42ab75e2018-09-12 20:46:03 -070072Layer::Layer(const LayerCreationArgs& args)
Ady Abraham8f1ee7f2019-04-05 10:32:50 -070073 : mFlinger(args.flinger),
74 mName(args.name),
75 mClientRef(args.client),
76 mWindowType(args.metadata.getInt32(METADATA_WINDOW_TYPE, 0)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -070077 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070078
79 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -070080 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
81 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
82 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070083
Dan Stozaf7ba41a2017-05-10 15:11:11 -070084 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070085
Lloyd Pique0449b0f2018-12-20 16:23:45 -080086 mCurrentState.active_legacy.w = args.w;
87 mCurrentState.active_legacy.h = args.h;
88 mCurrentState.flags = layerFlags;
89 mCurrentState.active_legacy.transform.set(0, 0);
90 mCurrentState.crop_legacy.makeInvalid();
91 mCurrentState.requestedCrop_legacy = mCurrentState.crop_legacy;
92 mCurrentState.z = 0;
93 mCurrentState.color.a = 1.0f;
94 mCurrentState.layerStack = 0;
95 mCurrentState.sequence = 0;
96 mCurrentState.requested_legacy = mCurrentState.active_legacy;
Lloyd Pique0449b0f2018-12-20 16:23:45 -080097 mCurrentState.active.w = UINT32_MAX;
98 mCurrentState.active.h = UINT32_MAX;
99 mCurrentState.active.transform.set(0, 0);
100 mCurrentState.transform = 0;
101 mCurrentState.transformToDisplayInverse = false;
102 mCurrentState.crop.makeInvalid();
103 mCurrentState.acquireFence = new Fence(-1);
104 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
105 mCurrentState.hdrMetadata.validTypes = 0;
106 mCurrentState.surfaceDamageRegion.clear();
107 mCurrentState.cornerRadius = 0.0f;
108 mCurrentState.api = -1;
109 mCurrentState.hasColorTransform = false;
Peiyong Linc502cb72019-03-01 15:00:23 -0800110 mCurrentState.colorSpaceAgnostic = false;
Evan Roskya1f1e152019-01-24 16:17:46 -0800111 mCurrentState.metadata = args.metadata;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700112
113 // drawing state & current state are identical
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800114 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700115
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800116 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700117 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800118 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200119 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700120
Ady Abraham8f1ee7f2019-04-05 10:32:50 -0700121 mSchedulerLayerHandle = mFlinger->mScheduler->registerLayer(mName.c_str(), mWindowType);
Ady Abraham09bd3922019-04-08 10:44:56 -0700122
Robert Carr2e102c92018-10-23 12:11:15 -0700123 mFlinger->onLayerCreated();
Dan Stoza436ccf32018-06-21 12:10:12 -0700124}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700125
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700126Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800127 sp<Client> c(mClientRef.promote());
128 if (c != 0) {
129 c->detachLayer(this);
130 }
131
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000132 mFrameTracker.logAndResetStats(mName);
chaviw74d90ad2019-04-26 14:45:26 -0700133 mFlinger->onLayerDestroyed(this);
Mathias Agopian96f08192010-06-02 23:28:45 -0700134}
135
Mathias Agopian13127d82013-03-05 17:47:11 -0800136// ---------------------------------------------------------------------------
137// callbacks
138// ---------------------------------------------------------------------------
139
David Sodmaneb085e02017-10-05 18:49:04 -0700140/*
141 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
142 * Layer. So, the implementation is done in BufferLayer. When called on a
143 * ColorLayer object, it's essentially a NOP.
144 */
David Sodmaneb085e02017-10-05 18:49:04 -0700145void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800146
chaviw43cb3cb2019-05-31 15:23:41 -0700147void Layer::removeRemoteSyncPoints() {
148 for (auto& point : mRemoteSyncPoints) {
149 point->setTransactionApplied();
150 }
151 mRemoteSyncPoints.clear();
152
153 {
154 Mutex::Autolock pendingStateLock(mPendingStateMutex);
155 for (State pendingState : mPendingStates) {
156 pendingState.barrierLayer_legacy = nullptr;
157 }
158 }
159}
160
Chia-I Wuc6657022017-08-15 11:18:17 -0700161void Layer::onRemovedFromCurrentState() {
Robert Carr2e102c92018-10-23 12:11:15 -0700162 mRemovedFromCurrentState = true;
163
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800164 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
165 if (mCurrentState.zOrderRelativeOf != nullptr) {
166 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
167 if (strongRelative != nullptr) {
168 strongRelative->removeZOrderRelative(this);
169 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700170 }
chaviw606e5cf2019-03-01 10:12:10 -0800171 setZOrderRelativeOf(nullptr);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700172 }
Rob Carr4bba3702018-10-08 21:53:30 +0000173
Robert Carr2e102c92018-10-23 12:11:15 -0700174 // Since we are no longer reachable from CurrentState SurfaceFlinger
175 // will no longer invoke doTransaction for us, and so we will
176 // never finish applying transactions. We signal the sync point
177 // now so that another layer will not become indefinitely
178 // blocked.
chaviw43cb3cb2019-05-31 15:23:41 -0700179 removeRemoteSyncPoints();
Robert Carr2e102c92018-10-23 12:11:15 -0700180
181 {
182 Mutex::Autolock syncLock(mLocalSyncPointMutex);
183 for (auto& point : mLocalSyncPoints) {
184 point->setFrameAvailable();
185 }
186 mLocalSyncPoints.clear();
187 }
188
Chia-I Wuc6657022017-08-15 11:18:17 -0700189 for (const auto& child : mCurrentChildren) {
190 child->onRemovedFromCurrentState();
191 }
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800192
193 mFlinger->markLayerPendingRemovalLocked(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700194}
Chia-I Wu38512252017-05-17 14:36:16 -0700195
chaviw61626f22018-11-15 16:26:27 -0800196void Layer::addToCurrentState() {
197 mRemovedFromCurrentState = false;
198
199 for (const auto& child : mCurrentChildren) {
200 child->addToCurrentState();
201 }
202}
203
Mathias Agopian13127d82013-03-05 17:47:11 -0800204// ---------------------------------------------------------------------------
205// set-up
206// ---------------------------------------------------------------------------
207
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700208const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800209 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800210}
211
chaviw13fdc492017-06-27 12:40:18 -0700212bool Layer::getPremultipledAlpha() const {
213 return mPremultipliedAlpha;
214}
215
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700216sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800217 Mutex::Autolock _l(mLock);
Robert Carrc0df3122019-04-11 13:18:21 -0700218 if (mGetHandleCalled) {
219 ALOGE("Get handle called twice" );
220 return nullptr;
221 }
222 mGetHandleCalled = true;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700223 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800224}
225
226// ---------------------------------------------------------------------------
227// h/w composer set-up
228// ---------------------------------------------------------------------------
229
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800230bool Layer::hasHwcLayer(const sp<const DisplayDevice>& displayDevice) {
231 auto outputLayer = findOutputLayerForDisplay(displayDevice);
232 LOG_FATAL_IF(!outputLayer);
233 return outputLayer->getState().hwc && (*outputLayer->getState().hwc).hwcLayer != nullptr;
234}
235
236HWC2::Layer* Layer::getHwcLayer(const sp<const DisplayDevice>& displayDevice) {
237 auto outputLayer = findOutputLayerForDisplay(displayDevice);
238 if (!outputLayer || !outputLayer->getState().hwc) {
239 return nullptr;
240 }
241 return (*outputLayer->getState().hwc).hwcLayer.get();
Steven Thomasb02664d2017-07-26 18:48:28 -0700242}
Steven Thomasb02664d2017-07-26 18:48:28 -0700243
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);
418 if (parentType >= 0 || parentAppId >= 0) {
419 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 Piquea83776c2019-01-29 18:42:32 -0800442void Layer::latchCompositionState(compositionengine::LayerFECompositionState& compositionState,
443 bool includeGeometry) const {
444 if (includeGeometry) {
445 latchGeometry(compositionState);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700446 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800447}
Mathias Agopian29a367b2011-07-12 14:51:45 -0700448
Lloyd Piquea83776c2019-01-29 18:42:32 -0800449const char* Layer::getDebugName() const {
450 return mName.string();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800451}
452
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800453void Layer::forceClientComposition(const sp<DisplayDevice>& display) {
454 const auto outputLayer = findOutputLayerForDisplay(display);
455 LOG_FATAL_IF(!outputLayer);
456 outputLayer->editState().forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800457}
Dan Stozaee44edd2015-03-23 15:50:23 -0700458
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800459bool Layer::getForceClientComposition(const sp<DisplayDevice>& display) {
460 const auto outputLayer = findOutputLayerForDisplay(display);
461 LOG_FATAL_IF(!outputLayer);
462 return outputLayer->getState().forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800463}
464
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700465void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800466 const auto outputLayer = findOutputLayerForDisplay(display);
467 LOG_FATAL_IF(!outputLayer);
468
469 if (!outputLayer->getState().hwc ||
470 (*outputLayer->getState().hwc).hwcCompositionType !=
471 Hwc2::IComposerClient::Composition::CURSOR) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800472 return;
473 }
474
475 // This gives us only the "orientation" component of the transform
Vishnu Nair33a6eee2019-02-06 13:48:06 -0800476 const State& s(getDrawingState());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800477
478 // Apply the layer's transform, followed by the display's global transform
479 // Here we're guaranteed that the layer's transform preserves rects
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800480 Rect win = getCroppedBufferSize(s);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800481 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700482 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800483 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700484 frame.intersect(display->getViewport(), &frame);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700485 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800486 auto position = displayTransform.transform(frame);
487
Dominik Laskowski7e045462018-05-30 13:02:02 -0700488 auto error =
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800489 (*outputLayer->getState().hwc).hwcLayer->setCursorPosition(position.left, position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800490 ALOGE_IF(error != HWC2::Error::None,
491 "[%s] Failed to set cursor position "
492 "to (%d, %d): %s (%d)",
493 mName.string(), position.left, position.top, to_string(error).c_str(),
494 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800495}
Riley Andrews03414a12014-07-01 14:22:59 -0700496
Mathias Agopian13127d82013-03-05 17:47:11 -0800497// ---------------------------------------------------------------------------
498// drawing...
499// ---------------------------------------------------------------------------
500
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000501bool Layer::prepareClientLayer(const RenderArea& renderArea, const Region& clip,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800502 Region& clearRegion, const bool supportProtectedContent,
503 renderengine::LayerSettings& layer) {
504 return prepareClientLayer(renderArea, clip, false, clearRegion, supportProtectedContent, layer);
Mathias Agopian13127d82013-03-05 17:47:11 -0800505}
506
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000507bool Layer::prepareClientLayer(const RenderArea& renderArea, bool useIdentityTransform,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800508 Region& clearRegion, const bool supportProtectedContent,
509 renderengine::LayerSettings& layer) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000510 return prepareClientLayer(renderArea, Region(renderArea.getBounds()), useIdentityTransform,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800511 clearRegion, supportProtectedContent, layer);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000512}
513
514bool Layer::prepareClientLayer(const RenderArea& /*renderArea*/, const Region& /*clip*/,
515 bool useIdentityTransform, Region& /*clearRegion*/,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800516 const bool /*supportProtectedContent*/,
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000517 renderengine::LayerSettings& layer) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800518 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000519 half alpha = getAlpha();
520 layer.geometry.boundaries = bounds;
521 if (useIdentityTransform) {
522 layer.geometry.positionTransform = mat4();
523 } else {
524 const ui::Transform transform = getTransform();
525 mat4 m;
526 m[0][0] = transform[0][0];
527 m[0][1] = transform[0][1];
528 m[0][3] = transform[0][2];
529 m[1][0] = transform[1][0];
530 m[1][1] = transform[1][1];
531 m[1][3] = transform[1][2];
532 m[3][0] = transform[2][0];
533 m[3][1] = transform[2][1];
534 m[3][3] = transform[2][2];
535 layer.geometry.positionTransform = m;
536 }
537
538 if (hasColorTransform()) {
539 layer.colorTransform = getColorTransform();
540 }
541
542 const auto roundedCornerState = getRoundedCornerState();
543 layer.geometry.roundedCornersRadius = roundedCornerState.radius;
544 layer.geometry.roundedCornersCrop = roundedCornerState.cropRect;
545
546 layer.alpha = alpha;
547 layer.sourceDataspace = mCurrentDataSpace;
548 return true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800549}
550
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800551void Layer::setCompositionType(const sp<const DisplayDevice>& display,
552 Hwc2::IComposerClient::Composition type) {
553 const auto outputLayer = findOutputLayerForDisplay(display);
554 LOG_FATAL_IF(!outputLayer);
555 LOG_FATAL_IF(!outputLayer->getState().hwc);
556 auto& compositionState = outputLayer->editState();
557
558 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", ((*compositionState.hwc).hwcLayer)->getId(),
559 toString(type).c_str(), 1);
560 if ((*compositionState.hwc).hwcCompositionType != type) {
David Sodman15094112018-10-11 09:39:37 -0700561 ALOGV(" actually setting");
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800562 (*compositionState.hwc).hwcCompositionType = type;
563
564 auto error = (*compositionState.hwc)
565 .hwcLayer->setCompositionType(static_cast<HWC2::Composition>(type));
566 ALOGE_IF(error != HWC2::Error::None,
567 "[%s] Failed to set "
568 "composition type %s: %s (%d)",
569 mName.string(), toString(type).c_str(), to_string(error).c_str(),
570 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800571 }
572}
573
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800574Hwc2::IComposerClient::Composition Layer::getCompositionType(
575 const sp<const DisplayDevice>& display) const {
576 const auto outputLayer = findOutputLayerForDisplay(display);
577 LOG_FATAL_IF(!outputLayer);
578 return outputLayer->getState().hwc ? (*outputLayer->getState().hwc).hwcCompositionType
579 : Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800580}
581
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800582bool Layer::getClearClientTarget(const sp<const DisplayDevice>& display) const {
583 const auto outputLayer = findOutputLayerForDisplay(display);
584 LOG_FATAL_IF(!outputLayer);
585 return outputLayer->getState().clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800586}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800587
Dan Stozacac35382016-01-27 12:21:06 -0800588bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
589 if (point->getFrameNumber() <= mCurrentFrameNumber) {
590 // Don't bother with a SyncPoint, since we've already latched the
591 // relevant frame
592 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700593 }
Robert Carr2e102c92018-10-23 12:11:15 -0700594 if (isRemovedFromCurrentState()) {
595 return false;
596 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700597
Dan Stozacac35382016-01-27 12:21:06 -0800598 Mutex::Autolock lock(mLocalSyncPointMutex);
599 mLocalSyncPoints.push_back(point);
600 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700601}
602
Mathias Agopian13127d82013-03-05 17:47:11 -0800603// ----------------------------------------------------------------------------
604// local state
605// ----------------------------------------------------------------------------
606
Peiyong Lin833074a2018-08-28 11:53:54 -0700607void Layer::computeGeometry(const RenderArea& renderArea,
608 renderengine::Mesh& mesh,
chaviwa76b2712017-09-20 12:02:26 -0700609 bool useIdentityTransform) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700610 const ui::Transform renderAreaTransform(renderArea.getTransform());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800611 FloatRect win = getBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700612
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000613 vec2 lt = vec2(win.left, win.top);
614 vec2 lb = vec2(win.left, win.bottom);
615 vec2 rb = vec2(win.right, win.bottom);
616 vec2 rt = vec2(win.right, win.top);
617
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800618 ui::Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000619 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700620 lt = layerTransform.transform(lt);
621 lb = layerTransform.transform(lb);
622 rb = layerTransform.transform(rb);
623 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000624 }
625
Peiyong Lin833074a2018-08-28 11:53:54 -0700626 renderengine::Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700627 position[0] = renderAreaTransform.transform(lt);
628 position[1] = renderAreaTransform.transform(lb);
629 position[2] = renderAreaTransform.transform(rb);
630 position[3] = renderAreaTransform.transform(rt);
Mathias Agopian13127d82013-03-05 17:47:11 -0800631}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800632
David Sodman41fdfc92017-11-06 16:09:56 -0800633bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800634 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700635 return (s.flags & layer_state_t::eLayerSecure);
636}
637
Mathias Agopian13127d82013-03-05 17:47:11 -0800638void Layer::setVisibleRegion(const Region& visibleRegion) {
639 // always called from main thread
640 this->visibleRegion = visibleRegion;
641}
642
643void Layer::setCoveredRegion(const Region& coveredRegion) {
644 // always called from main thread
645 this->coveredRegion = coveredRegion;
646}
647
David Sodman41fdfc92017-11-06 16:09:56 -0800648void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800649 // always called from main thread
650 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
651}
652
Robert Carre5f4f692018-01-12 13:12:28 -0800653void Layer::clearVisibilityRegions() {
654 visibleRegion.clear();
655 visibleNonTransparentRegion.clear();
656 coveredRegion.clear();
657}
658
Mathias Agopian13127d82013-03-05 17:47:11 -0800659// ----------------------------------------------------------------------------
660// transaction
661// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800662
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800663void Layer::pushPendingState() {
664 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700665 return;
666 }
667
Dan Stoza7dde5992015-05-22 09:51:44 -0700668 // If this transaction is waiting on the receipt of a frame, generate a sync
669 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700670 // We don't allow installing sync points after we are removed from the current state
671 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800672 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
673 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800674 if (barrierLayer == nullptr) {
675 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700676 // If we can't promote the layer we are intended to wait on,
677 // then it is expired or otherwise invalid. Allow this transaction
678 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800679 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800680 } else {
chaviw43cb3cb2019-05-31 15:23:41 -0700681 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy, this);
Robert Carr0d480722017-01-10 16:42:54 -0800682 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800683 mRemoteSyncPoints.push_back(std::move(syncPoint));
684 } else {
685 // We already missed the frame we're supposed to synchronize
686 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800687 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800688 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700689 }
690
Dan Stoza7dde5992015-05-22 09:51:44 -0700691 // Wake us up to check if the frame has been received
692 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700693 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700694 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800695 mPendingStates.push_back(mCurrentState);
696 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700697}
698
Pablo Ceballos05289c22016-04-14 15:49:55 -0700699void Layer::popPendingState(State* stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800700 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700701
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800702 mPendingStates.removeAt(0);
703 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700704}
705
Pablo Ceballos05289c22016-04-14 15:49:55 -0700706bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700707 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800708 while (!mPendingStates.empty()) {
709 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700710 if (mRemoteSyncPoints.empty()) {
711 // If we don't have a sync point for this, apply it anyway. It
712 // will be visually wrong, but it should keep us from getting
713 // into too much trouble.
714 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700715 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700716 stateUpdateAvailable = true;
717 continue;
718 }
719
Marissa Wallf58c14b2018-07-24 10:50:43 -0700720 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800721 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800722 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800723
724 // Signal our end of the sync point and then dispose of it
725 mRemoteSyncPoints.front()->setTransactionApplied();
726 mRemoteSyncPoints.pop_front();
727 continue;
728 }
729
Dan Stoza7dde5992015-05-22 09:51:44 -0700730 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
731 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700732 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700733 stateUpdateAvailable = true;
734
735 // Signal our end of the sync point and then dispose of it
736 mRemoteSyncPoints.front()->setTransactionApplied();
737 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800738 } else {
739 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700740 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700741 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700742 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700743 stateUpdateAvailable = true;
744 }
745 }
746
747 // If we still have pending updates, wake SurfaceFlinger back up and point
748 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800749 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700750 setTransactionFlags(eTransactionNeeded);
751 mFlinger->setTransactionFlags(eTraversalNeeded);
752 }
753
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800754 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700755 return stateUpdateAvailable;
756}
757
Marissa Wall61c58622018-07-18 10:12:20 -0700758uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000759 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800760
Marissa Wall61c58622018-07-18 10:12:20 -0700761 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
762 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700763
David Sodmaneb085e02017-10-05 18:49:04 -0700764 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700765 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000766 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800767 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
768 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
769 " requested={ wh={%4u,%4u} }}\n"
770 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
771 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700772 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700773 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
774 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
775 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
776 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
777 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700778 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
779 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
780 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800781 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700782
Robert Carre392b552017-09-19 12:16:05 -0700783 // Don't let Layer::doTransaction update the drawing state
784 // if we have a pending resize, unless we are in fixed-size mode.
785 // the drawing state will be updated only once we receive a buffer
786 // with the correct size.
787 //
788 // In particular, we want to make sure the clip (which is part
789 // of the geometry state) is latched together with the size but is
790 // latched immediately when no resizing is involved.
791 //
792 // If a sideband stream is attached, however, we want to skip this
793 // optimization so that transactions aren't missed when a buffer
794 // never arrives
795 //
796 // In the case that we don't have a buffer we ignore other factors
797 // and avoid entering the resizePending state. At a high level the
798 // resizePending state is to avoid applying the state of the new buffer
799 // to the old buffer. However in the state where we don't have an old buffer
800 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700801 const bool resizePending =
802 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
803 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
Lloyd Pique0b785d82018-12-04 17:25:27 -0800804 (mActiveBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700805 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800806 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700807 flags |= eDontUpdateGeometryState;
808 }
809 }
810
Robert Carr7bf247e2017-05-18 14:02:49 -0700811 // Here we apply various requested geometry states, depending on our
812 // latching configuration. See Layer.h for a detailed discussion of
813 // how geometry latching is controlled.
814 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000815 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700816
817 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
818 // mode, which causes attributes which normally latch regardless of scaling mode,
819 // to be delayed. We copy the requested state to the active state making sure
820 // to respect these rules (again see Layer.h for a detailed discussion).
821 //
822 // There is an awkward asymmetry in the handling of the crop states in the position
823 // states, as can be seen below. Largely this arises from position and transform
824 // being stored in the same data structure while having different latching rules.
825 // b/38182305
826 //
Marissa Wall61c58622018-07-18 10:12:20 -0700827 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700828 // applyPendingStates in the presence of deferred transactions.
829 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -0700830 float tx = stateToCommit->active_legacy.transform.tx();
831 float ty = stateToCommit->active_legacy.transform.ty();
832 stateToCommit->active_legacy = stateToCommit->requested_legacy;
833 stateToCommit->active_legacy.transform.set(tx, ty);
834 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700835 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700836 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -0700837 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700838 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800839 }
840
Marissa Wall61c58622018-07-18 10:12:20 -0700841 return flags;
842}
843
844uint32_t Layer::doTransaction(uint32_t flags) {
845 ATRACE_CALL();
846
chaviw5aedec92018-10-22 10:40:38 -0700847 if (mLayerDetached) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800848 return flags;
849 }
850
851 if (mChildrenChanged) {
852 flags |= eVisibleRegion;
853 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -0700854 }
855
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800856 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +0000857 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -0700858 if (!applyPendingStates(&c)) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800859 return flags;
Marissa Wall61c58622018-07-18 10:12:20 -0700860 }
861
862 flags = doTransactionResize(flags, &c);
863
Alec Mourib416efd2018-09-06 21:01:59 +0000864 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700865
866 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800867 // invalidate and recompute the visible regions if needed
868 flags |= Layer::eVisibleRegion;
869 }
870
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700871 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800872 // invalidate and recompute the visible regions if needed
873 flags |= eVisibleRegion;
874 this->contentDirty = true;
875
876 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -0700877 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -0700878 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -0800879 }
880
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800881 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -0700882 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800883 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -0700884 }
885
Mathias Agopian13127d82013-03-05 17:47:11 -0800886 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -0700887 commitTransaction(c);
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800888 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -0800889 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800890}
891
Pablo Ceballos05289c22016-04-14 15:49:55 -0700892void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800893 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700894}
895
Mathias Agopian13127d82013-03-05 17:47:11 -0800896uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800897 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800898}
899
900uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800901 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -0800902}
903
Robert Carr82364e32016-05-15 11:27:47 -0700904bool Layer::setPosition(float x, float y, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800905 if (mCurrentState.requested_legacy.transform.tx() == x &&
906 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -0800907 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800908 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -0700909
910 // We update the requested and active position simultaneously because
911 // we want to apply the position portion of the transform matrix immediately,
912 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800913 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -0700914 if (immediate && !mFreezeGeometryUpdates) {
915 // Here we directly update the active state
916 // unlike other setters, because we store it within
917 // the transform, but use different latching rules.
918 // b/38182305
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800919 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -0700920 }
Robert Carr7bf247e2017-05-18 14:02:49 -0700921 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -0700922
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800923 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800924 setTransactionFlags(eTransactionNeeded);
925 return true;
926}
Robert Carr82364e32016-05-15 11:27:47 -0700927
Robert Carr1f0a16a2016-10-24 16:27:39 -0700928bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
929 ssize_t idx = mCurrentChildren.indexOf(childLayer);
930 if (idx < 0) {
931 return false;
932 }
933 if (childLayer->setLayer(z)) {
934 mCurrentChildren.removeAt(idx);
935 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800936 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700937 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800938 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700939}
940
Robert Carr503c7042017-09-27 15:06:08 -0700941bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
942 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
943 ssize_t idx = mCurrentChildren.indexOf(childLayer);
944 if (idx < 0) {
945 return false;
946 }
947 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
948 mCurrentChildren.removeAt(idx);
949 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800950 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700951 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800952 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700953}
954
Robert Carrae060832016-11-28 10:51:00 -0800955bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800956 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
957 mCurrentState.sequence++;
958 mCurrentState.z = z;
959 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700960
961 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800962 if (mCurrentState.zOrderRelativeOf != nullptr) {
963 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700964 if (strongRelative != nullptr) {
965 strongRelative->removeZOrderRelative(this);
966 }
chaviw606e5cf2019-03-01 10:12:10 -0800967 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -0700968 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800969 setTransactionFlags(eTransactionNeeded);
970 return true;
971}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700972
Robert Carrdb66e622017-04-10 16:55:57 -0700973void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800974 mCurrentState.zOrderRelatives.remove(relative);
975 mCurrentState.sequence++;
976 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700977 setTransactionFlags(eTransactionNeeded);
978}
979
980void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800981 mCurrentState.zOrderRelatives.add(relative);
982 mCurrentState.modified = true;
983 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -0700984 setTransactionFlags(eTransactionNeeded);
985}
986
chaviw606e5cf2019-03-01 10:12:10 -0800987void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
988 mCurrentState.zOrderRelativeOf = relativeOf;
989 mCurrentState.sequence++;
990 mCurrentState.modified = true;
991 setTransactionFlags(eTransactionNeeded);
992}
993
Robert Carr503d2bd2017-12-04 15:49:47 -0800994bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -0700995 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
996 if (handle == nullptr) {
997 return false;
998 }
999 sp<Layer> relative = handle->owner.promote();
1000 if (relative == nullptr) {
1001 return false;
1002 }
1003
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001004 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1005 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001006 return false;
1007 }
1008
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001009 mCurrentState.sequence++;
1010 mCurrentState.modified = true;
1011 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001012
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001013 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -07001014 if (oldZOrderRelativeOf != nullptr) {
1015 oldZOrderRelativeOf->removeZOrderRelative(this);
1016 }
chaviw606e5cf2019-03-01 10:12:10 -08001017 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -07001018 relative->addZOrderRelative(this);
1019
1020 setTransactionFlags(eTransactionNeeded);
1021
1022 return true;
1023}
1024
Mathias Agopian13127d82013-03-05 17:47:11 -08001025bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001026 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001027 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001028 mCurrentState.requested_legacy.w = w;
1029 mCurrentState.requested_legacy.h = h;
1030 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001031 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001032
1033 // record the new size, from this point on, when the client request
1034 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001035 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001036 return true;
1037}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001038bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001039 if (mCurrentState.color.a == alpha) return false;
1040 mCurrentState.sequence++;
1041 mCurrentState.color.a = alpha;
1042 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001043 setTransactionFlags(eTransactionNeeded);
1044 return true;
1045}
chaviw13fdc492017-06-27 12:40:18 -07001046
Valerie Haudd0b7572019-01-29 14:59:27 -08001047bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1048 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001049 return false;
Valerie Hauaa194562019-02-05 16:21:38 -08001050 }
1051 mCurrentState.sequence++;
1052 mCurrentState.modified = true;
1053 setTransactionFlags(eTransactionNeeded);
1054
1055 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001056 // create background color layer if one does not yet exist
1057 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
1058 const String8& name = mName + "BackgroundColorLayer";
Evan Roskya1f1e152019-01-24 16:17:46 -08001059 mCurrentState.bgColorLayer = new ColorLayer(
1060 LayerCreationArgs(mFlinger.get(), nullptr, name, 0, 0, flags, LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -07001061
Valerie Haudd0b7572019-01-29 14:59:27 -08001062 // add to child list
1063 addChild(mCurrentState.bgColorLayer);
1064 mFlinger->mLayersAdded = true;
1065 // set up SF to handle added color layer
1066 if (isRemovedFromCurrentState()) {
1067 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1068 }
1069 mFlinger->setTransactionFlags(eTransactionNeeded);
1070 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1071 mCurrentState.bgColorLayer->reparent(nullptr);
1072 mCurrentState.bgColorLayer = nullptr;
1073 return true;
1074 }
1075
1076 mCurrentState.bgColorLayer->setColor(color);
1077 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1078 mCurrentState.bgColorLayer->setAlpha(alpha);
1079 mCurrentState.bgColorLayer->setDataspace(dataspace);
1080
chaviw13fdc492017-06-27 12:40:18 -07001081 return true;
1082}
1083
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001084bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001085 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001086
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001087 mCurrentState.sequence++;
1088 mCurrentState.cornerRadius = cornerRadius;
1089 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001090 setTransactionFlags(eTransactionNeeded);
1091 return true;
1092}
1093
Robert Carrd4ae7f32018-06-07 16:10:57 -07001094bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1095 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001096 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001097 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1098
1099 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1100 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1101 return false;
1102 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001103 mCurrentState.sequence++;
1104 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1105 matrix.dsdy);
1106 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001107 setTransactionFlags(eTransactionNeeded);
1108 return true;
1109}
Marissa Wall61c58622018-07-18 10:12:20 -07001110
Mathias Agopian13127d82013-03-05 17:47:11 -08001111bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001112 mCurrentState.requestedTransparentRegion_legacy = transparent;
1113 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001114 setTransactionFlags(eTransactionNeeded);
1115 return true;
1116}
1117bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001118 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1119 if (mCurrentState.flags == newFlags) return false;
1120 mCurrentState.sequence++;
1121 mCurrentState.flags = newFlags;
1122 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001123 setTransactionFlags(eTransactionNeeded);
1124 return true;
1125}
Robert Carr99e27f02016-06-16 15:18:02 -07001126
Marissa Wallf58c14b2018-07-24 10:50:43 -07001127bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001128 if (mCurrentState.requestedCrop_legacy == crop) return false;
1129 mCurrentState.sequence++;
1130 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001131 if (immediate && !mFreezeGeometryUpdates) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001132 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001133 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001134 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1135
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001136 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001137 setTransactionFlags(eTransactionNeeded);
1138 return true;
1139}
Robert Carr8d5227b2017-03-16 15:41:03 -07001140
Robert Carrc3574f72016-03-24 12:19:32 -07001141bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001142 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001143 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001144 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001145 return true;
1146}
1147
Evan Roskyef876c92019-01-25 17:46:06 -08001148bool Layer::setMetadata(const LayerMetadata& data) {
1149 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001150 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001151 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001152 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001153 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001154}
1155
Mathias Agopian13127d82013-03-05 17:47:11 -08001156bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001157 if (mCurrentState.layerStack == layerStack) return false;
1158 mCurrentState.sequence++;
1159 mCurrentState.layerStack = layerStack;
1160 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001161 setTransactionFlags(eTransactionNeeded);
1162 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001163}
1164
Peiyong Linc502cb72019-03-01 15:00:23 -08001165bool Layer::setColorSpaceAgnostic(const bool agnostic) {
1166 if (mCurrentState.colorSpaceAgnostic == agnostic) {
1167 return false;
1168 }
1169 mCurrentState.sequence++;
1170 mCurrentState.colorSpaceAgnostic = agnostic;
1171 mCurrentState.modified = true;
1172 setTransactionFlags(eTransactionNeeded);
1173 return true;
1174}
1175
Robert Carr1f0a16a2016-10-24 16:27:39 -07001176uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001177 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001178 if (p == nullptr) {
1179 return getDrawingState().layerStack;
1180 }
1181 return p->getLayerStack();
1182}
1183
Marissa Wallf58c14b2018-07-24 10:50:43 -07001184void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001185 mCurrentState.barrierLayer_legacy = barrierLayer;
1186 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001187 // We don't set eTransactionNeeded, because just receiving a deferral
1188 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001189 mCurrentState.modified = true;
1190 pushPendingState();
1191 mCurrentState.barrierLayer_legacy = nullptr;
1192 mCurrentState.frameNumber_legacy = 0;
1193 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001194}
1195
Marissa Wallf58c14b2018-07-24 10:50:43 -07001196void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001197 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001198 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001199}
1200
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001201// ----------------------------------------------------------------------------
1202// pageflip handling...
1203// ----------------------------------------------------------------------------
1204
Robert Carr1f0a16a2016-10-24 16:27:39 -07001205bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001206 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001207 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001208 if (parent != nullptr && parent->isHiddenByPolicy()) {
1209 return true;
1210 }
1211 return s.flags & layer_state_t::eLayerHidden;
1212}
1213
David Sodman41fdfc92017-11-06 16:09:56 -08001214uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001215 // TODO: should we do something special if mSecure is set?
1216 if (mProtectedByApp) {
1217 // need a hardware-protected path to external video sink
1218 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001219 }
Riley Andrews03414a12014-07-01 14:22:59 -07001220 if (mPotentialCursor) {
1221 usage |= GraphicBuffer::USAGE_CURSOR;
1222 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001223 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001224 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001225}
1226
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001227void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001228 uint32_t orientation = 0;
Vishnu Nair5eb3f062019-04-08 08:21:03 -07001229 // Disable setting transform hint if the debug flag is set.
1230 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001231 // The transform hint is used to improve performance, but we can
1232 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001233 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001234 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001235 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001236 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001237 orientation = 0;
1238 }
1239 }
David Sodmaneb085e02017-10-05 18:49:04 -07001240 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001241}
1242
Mathias Agopian13127d82013-03-05 17:47:11 -08001243// ----------------------------------------------------------------------------
1244// debugging
1245// ----------------------------------------------------------------------------
1246
Marissa Wall61c58622018-07-18 10:12:20 -07001247// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001248LayerDebugInfo Layer::getLayerDebugInfo() const {
1249 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001250 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001251 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001252 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001253 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
Yiwei Zhang5434a782018-12-05 18:06:32 -08001254 info.mType = std::string(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001255 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001256 info.mVisibleRegion = visibleRegion;
1257 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001258 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001259 info.mX = ds.active_legacy.transform.tx();
1260 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001261 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001262 info.mWidth = ds.active_legacy.w;
1263 info.mHeight = ds.active_legacy.h;
1264 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001265 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001266 info.mFlags = ds.flags;
1267 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001268 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001269 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1270 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1271 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1272 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001273 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001274 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001275 if (buffer != 0) {
1276 info.mActiveBufferWidth = buffer->getWidth();
1277 info.mActiveBufferHeight = buffer->getHeight();
1278 info.mActiveBufferStride = buffer->getStride();
1279 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001280 } else {
1281 info.mActiveBufferWidth = 0;
1282 info.mActiveBufferHeight = 0;
1283 info.mActiveBufferStride = 0;
1284 info.mActiveBufferFormat = 0;
1285 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001286 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001287 info.mNumQueuedFrames = getQueuedFrameCount();
1288 info.mRefreshPending = isBufferLatched();
1289 info.mIsOpaque = isOpaque(ds);
1290 info.mContentDirty = contentDirty;
1291 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001292}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001293
Yiwei Zhang5434a782018-12-05 18:06:32 -08001294void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001295 result.append("-------------------------------");
1296 result.append("-------------------------------");
1297 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001298 result.append(" Layer name\n");
1299 result.append(" Z | ");
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001300 result.append(" Window Type | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001301 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001302 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001303 result.append(" Disp Frame (LTRB) | ");
1304 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001305 result.append("-------------------------------");
1306 result.append("-------------------------------");
1307 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001308}
1309
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001310void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1311 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1312 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001313 return;
1314 }
1315
Yiwei Zhang5434a782018-12-05 18:06:32 -08001316 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001317 if (mName.length() > 77) {
1318 std::string shortened;
1319 shortened.append(mName.string(), 36);
1320 shortened.append("[...]");
1321 shortened.append(mName.string() + (mName.length() - 36), 36);
Yiwei Zhang5434a782018-12-05 18:06:32 -08001322 name = shortened;
Dan Stozae22aec72016-08-01 13:20:59 -07001323 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001324 name = std::string(mName.string(), mName.size());
Dan Stozae22aec72016-08-01 13:20:59 -07001325 }
1326
Yiwei Zhang5434a782018-12-05 18:06:32 -08001327 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001328
Alec Mourib416efd2018-09-06 21:01:59 +00001329 const State& layerState(getDrawingState());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001330 const auto& compositionState = outputLayer->getState();
1331
Chia-I Wu1e043612018-03-01 09:45:09 -08001332 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001333 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001334 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001335 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001336 }
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001337 StringAppendF(&result, " %10d | ", mWindowType);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001338 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
1339 StringAppendF(&result, "%10s | ",
1340 toString(getCompositionLayer() ? compositionState.bufferTransform
1341 : static_cast<Hwc2::Transform>(0))
1342 .c_str());
1343 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001344 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001345 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001346 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1347 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001348
Yichi Chen6ca35192018-05-29 12:20:43 +08001349 result.append("- - - - - - - - - - - - - - - -");
1350 result.append("- - - - - - - - - - - - - - - -");
1351 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001352}
Dan Stozae22aec72016-08-01 13:20:59 -07001353
Yiwei Zhang5434a782018-12-05 18:06:32 -08001354void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001355 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001356}
1357
Svetoslavd85084b2014-03-20 10:28:31 -07001358void Layer::clearFrameStats() {
1359 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001360}
1361
Jamie Gennis6547ff42013-07-16 20:12:42 -07001362void Layer::logFrameStats() {
1363 mFrameTracker.logAndResetStats(mName);
1364}
1365
Svetoslavd85084b2014-03-20 10:28:31 -07001366void Layer::getFrameStats(FrameStats* outStats) const {
1367 mFrameTracker.getStats(outStats);
1368}
1369
Yiwei Zhang5434a782018-12-05 18:06:32 -08001370void Layer::dumpFrameEvents(std::string& result) {
1371 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001372 Mutex::Autolock lock(mFrameEventHistoryMutex);
1373 mFrameEventHistory.checkFencesForCompletion();
1374 mFrameEventHistory.dump(result);
1375}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001376
Brian Anderson5ea5e592016-12-01 16:54:33 -08001377void Layer::onDisconnect() {
1378 Mutex::Autolock lock(mFrameEventHistoryMutex);
1379 mFrameEventHistory.onDisconnect();
Yiwei Zhangaf8ee942018-11-22 00:15:23 -08001380 mFlinger->mTimeStats->onDestroy(getSequence());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001381}
1382
Brian Anderson3890c392016-07-25 12:48:08 -07001383void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001384 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001385 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001386 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1387 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001388 }
1389
Brian Andersond6927fb2016-07-23 23:37:30 -07001390 Mutex::Autolock lock(mFrameEventHistoryMutex);
1391 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001392 // If there are any unsignaled fences in the aquire timeline at this
1393 // point, the previously queued frame hasn't been latched yet. Go ahead
1394 // and try to get the signal time here so the syscall is taken out of
1395 // the main thread's critical path.
1396 mAcquireTimeline.updateSignalTimes();
1397 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001398 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001399 mFrameEventHistory.addQueue(*newTimestamps);
1400 }
1401
Brian Anderson3890c392016-07-25 12:48:08 -07001402 if (outDelta) {
1403 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001404 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001405}
Dan Stozae77c7662016-05-13 11:37:28 -07001406
Chia-I Wu98f1c102017-05-30 14:54:08 -07001407size_t Layer::getChildrenCount() const {
1408 size_t count = 0;
1409 for (const sp<Layer>& child : mCurrentChildren) {
1410 count += 1 + child->getChildrenCount();
1411 }
1412 return count;
1413}
1414
Robert Carr1f0a16a2016-10-24 16:27:39 -07001415void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001416 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001417 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001418
Robert Carr1f0a16a2016-10-24 16:27:39 -07001419 mCurrentChildren.add(layer);
1420 layer->setParent(this);
1421}
1422
1423ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001424 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001425 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001426
Robert Carr1323c952019-01-28 18:13:27 -08001427 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001428 return mCurrentChildren.remove(layer);
1429}
1430
Robert Carr1db73f62016-12-21 12:58:51 -08001431bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1432 sp<Handle> handle = nullptr;
1433 sp<Layer> newParent = nullptr;
1434 if (newParentHandle == nullptr) {
1435 return false;
1436 }
1437 handle = static_cast<Handle*>(newParentHandle.get());
1438 newParent = handle->owner.promote();
1439 if (newParent == nullptr) {
1440 ALOGE("Unable to promote Layer handle");
1441 return false;
1442 }
1443
chaviw5aedec92018-10-22 10:40:38 -07001444 if (attachChildren()) {
1445 setTransactionFlags(eTransactionNeeded);
1446 }
Robert Carr1db73f62016-12-21 12:58:51 -08001447 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001448 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001449 }
1450 mCurrentChildren.clear();
1451
1452 return true;
1453}
1454
Robert Carr15eae092018-03-23 13:43:53 -07001455void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001456 for (const sp<Layer>& child : mDrawingChildren) {
1457 child->mDrawingParent = newParent;
Vishnu Nairc652ff82019-03-15 12:48:54 -07001458 child->computeBounds(newParent->mBounds,
1459 newParent->getTransformWithScale(
1460 newParent->getBufferScaleTransform()));
Robert Carr578038f2018-03-09 12:25:24 -08001461 }
1462}
1463
chaviwf1961f72017-09-18 16:41:07 -07001464bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001465 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001466
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001467 // While layers are detached, we allow most operations
1468 // and simply halt performing the actual transaction. However
1469 // for reparent != null we would enter the mRemovedFromCurrentState
1470 // state, regardless of whether doTransaction was called, and
1471 // so we need to prevent the update here.
1472 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001473 return false;
1474 }
1475
Robert Carr54cf5b12019-01-25 14:02:28 -08001476 sp<Layer> newParent;
1477 if (newParentHandle != nullptr) {
1478 auto handle = static_cast<Handle*>(newParentHandle.get());
1479 newParent = handle->owner.promote();
1480 if (newParent == nullptr) {
1481 ALOGE("Unable to promote Layer handle");
1482 return false;
1483 }
1484 if (newParent == this) {
1485 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1486 return false;
1487 }
1488 }
1489
chaviwf1961f72017-09-18 16:41:07 -07001490 sp<Layer> parent = getParent();
1491 if (parent != nullptr) {
1492 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001493 }
1494
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001495 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001496 newParent->addChild(this);
1497 if (!newParent->isRemovedFromCurrentState()) {
1498 addToCurrentState();
1499 } else {
1500 onRemovedFromCurrentState();
1501 }
1502
1503 if (mLayerDetached) {
1504 mLayerDetached = false;
1505 callSetTransactionFlags = true;
1506 }
1507 } else {
1508 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001509 }
1510
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001511 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001512 setTransactionFlags(eTransactionNeeded);
1513 }
chaviw06178942017-07-27 10:25:59 -07001514 return true;
1515}
1516
Robert Carr9524cb32017-02-13 11:32:32 -08001517bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001518 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001519 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001520 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001521 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001522 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001523 child->detachChildren();
chaviw43cb3cb2019-05-31 15:23:41 -07001524 child->removeRemoteSyncPoints();
Robert Carr9524cb32017-02-13 11:32:32 -08001525 }
Robert Carr7f619b22017-11-06 12:56:35 -08001526 }
Robert Carr9524cb32017-02-13 11:32:32 -08001527
1528 return true;
1529}
1530
chaviw5aedec92018-10-22 10:40:38 -07001531bool Layer::attachChildren() {
1532 bool changed = false;
1533 for (const sp<Layer>& child : mCurrentChildren) {
1534 sp<Client> parentClient = mClientRef.promote();
1535 sp<Client> client(child->mClientRef.promote());
1536 if (client != nullptr && parentClient != client) {
1537 if (child->mLayerDetached) {
1538 child->mLayerDetached = false;
1539 changed = true;
1540 }
1541 changed |= child->attachChildren();
1542 }
1543 }
1544
1545 return changed;
1546}
1547
Peiyong Lind3788632018-09-18 16:01:31 -07001548bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001549 static const mat4 identityMatrix = mat4();
1550
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001551 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001552 return false;
1553 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001554 ++mCurrentState.sequence;
1555 mCurrentState.colorTransform = matrix;
1556 mCurrentState.hasColorTransform = matrix != identityMatrix;
1557 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001558 setTransactionFlags(eTransactionNeeded);
1559 return true;
1560}
1561
chaviwf66724d2018-11-28 16:35:21 -08001562mat4 Layer::getColorTransform() const {
1563 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1564 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1565 colorTransform = parent->getColorTransform() * colorTransform;
1566 }
1567 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001568}
1569
1570bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001571 bool hasColorTransform = getDrawingState().hasColorTransform;
1572 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1573 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1574 }
1575 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001576}
1577
Chia-I Wu11481472018-05-04 10:43:19 -07001578bool Layer::isLegacyDataSpace() const {
1579 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001580 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001581 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001582}
1583
Robert Carr1f0a16a2016-10-24 16:27:39 -07001584void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001585 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001586}
1587
Robert Carr1f0a16a2016-10-24 16:27:39 -07001588int32_t Layer::getZ() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001589 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001590}
1591
Robert Carr29abff82017-12-04 13:51:20 -08001592bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1593 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001594 const State& state = useDrawing ? mDrawingState : mCurrentState;
Robert Carr29abff82017-12-04 13:51:20 -08001595 return state.zOrderRelativeOf != nullptr;
1596}
1597
David Sodman41fdfc92017-11-06 16:09:56 -08001598__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001599 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001600 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1601 "makeTraversalList received invalid stateSet");
1602 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1603 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001604 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001605
Robert Carr29abff82017-12-04 13:51:20 -08001606 if (state.zOrderRelatives.size() == 0) {
1607 *outSkipRelativeZUsers = true;
1608 return children;
1609 }
1610
chaviwfd462612018-05-31 16:11:27 -07001611 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001612 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001613 sp<Layer> strongRelative = weakRelative.promote();
1614 if (strongRelative != nullptr) {
1615 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001616 }
1617 }
1618
Dan Stoza412903f2017-04-27 13:42:17 -07001619 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001620 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
Robert Carr503c7042017-09-27 15:06:08 -07001621 if (childState.zOrderRelativeOf != nullptr) {
1622 continue;
1623 }
Robert Carrdb66e622017-04-10 16:55:57 -07001624 traverse.add(child);
1625 }
1626
1627 return traverse;
1628}
1629
Robert Carr1f0a16a2016-10-24 16:27:39 -07001630/**
Robert Carrdb66e622017-04-10 16:55:57 -07001631 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001632 */
Dan Stoza412903f2017-04-27 13:42:17 -07001633void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001634 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1635 // produce a new list for traversing, including our relatives, and not including our children
1636 // who are relatives of another surface. In the case that there are no relative Z,
1637 // makeTraversalList returns our children directly to avoid significant overhead.
1638 // However in this case we need to take the responsibility for filtering children which
1639 // are relatives of another surface here.
1640 bool skipRelativeZUsers = false;
1641 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001642
Robert Carr1f0a16a2016-10-24 16:27:39 -07001643 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001644 for (; i < list.size(); i++) {
1645 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001646 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;
Robert Carrdb66e622017-04-10 16:55:57 -07001652 }
Dan Stoza412903f2017-04-27 13:42:17 -07001653 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001654 }
Robert Carr29abff82017-12-04 13:51:20 -08001655
Dan Stoza412903f2017-04-27 13:42:17 -07001656 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001657 for (; i < list.size(); i++) {
1658 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001659
Robert Carr29abff82017-12-04 13:51:20 -08001660 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1661 continue;
1662 }
Dan Stoza412903f2017-04-27 13:42:17 -07001663 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001664 }
1665}
1666
1667/**
Robert Carrdb66e622017-04-10 16:55:57 -07001668 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001669 */
Dan Stoza412903f2017-04-27 13:42:17 -07001670void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1671 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001672 // See traverseInZOrder for documentation.
1673 bool skipRelativeZUsers = false;
1674 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001675
Robert Carr1f0a16a2016-10-24 16:27:39 -07001676 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001677 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001678 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001679
1680 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1681 continue;
1682 }
1683
Robert Carrdb66e622017-04-10 16:55:57 -07001684 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001685 break;
1686 }
Dan Stoza412903f2017-04-27 13:42:17 -07001687 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001688 }
Dan Stoza412903f2017-04-27 13:42:17 -07001689 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001690 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001691 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001692
1693 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1694 continue;
1695 }
1696
Dan Stoza412903f2017-04-27 13:42:17 -07001697 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001698 }
1699}
1700
chaviw4b129c22018-04-09 16:19:43 -07001701LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1702 const std::vector<Layer*>& layersInTree) {
1703 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1704 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001705 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1706 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001707 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001708
chaviwfd462612018-05-31 16:11:27 -07001709 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001710 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1711 sp<Layer> strongRelative = weakRelative.promote();
1712 // Only add relative layers that are also descendents of the top most parent of the tree.
1713 // If a relative layer is not a descendent, then it should be ignored.
1714 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1715 traverse.add(strongRelative);
1716 }
1717 }
1718
1719 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001720 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001721 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1722 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1723 // the child here since it won't be added later as a relative.
1724 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1725 childState.zOrderRelativeOf.promote().get())) {
1726 continue;
1727 }
1728 traverse.add(child);
1729 }
1730
1731 return traverse;
1732}
1733
1734void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1735 LayerVector::StateSet stateSet,
1736 const LayerVector::Visitor& visitor) {
1737 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001738
1739 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001740 for (; i < list.size(); i++) {
1741 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001742 if (relative->getZ() >= 0) {
1743 break;
1744 }
chaviw4b129c22018-04-09 16:19:43 -07001745 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001746 }
chaviw4b129c22018-04-09 16:19:43 -07001747
chaviwa76b2712017-09-20 12:02:26 -07001748 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001749 for (; i < list.size(); i++) {
1750 const auto& relative = list[i];
1751 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001752 }
1753}
1754
chaviw4b129c22018-04-09 16:19:43 -07001755std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1756 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1757 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1758
1759 std::vector<Layer*> layersInTree = {this};
1760 for (size_t i = 0; i < children.size(); i++) {
1761 const auto& child = children[i];
1762 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1763 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1764 }
1765
1766 return layersInTree;
1767}
1768
1769void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1770 const LayerVector::Visitor& visitor) {
1771 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1772 std::sort(layersInTree.begin(), layersInTree.end());
1773 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1774}
1775
Peiyong Linefefaac2018-08-17 12:27:51 -07001776ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001777 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001778}
1779
chaviw13fdc492017-06-27 12:40:18 -07001780half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001781 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001782
chaviw13fdc492017-06-27 12:40:18 -07001783 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1784 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001785}
Robert Carr6452f122017-03-21 10:41:29 -07001786
chaviw13fdc492017-06-27 12:40:18 -07001787half4 Layer::getColor() const {
1788 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001789 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001790}
Robert Carr6452f122017-03-21 10:41:29 -07001791
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001792Layer::RoundedCornerState Layer::getRoundedCornerState() const {
1793 const auto& p = mDrawingParent.promote();
1794 if (p != nullptr) {
Peiyong Lin27016a92019-03-29 17:36:08 +00001795 RoundedCornerState parentState = p->getRoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001796 if (parentState.radius > 0) {
1797 ui::Transform t = getActiveTransform(getDrawingState());
1798 t = t.inverse();
1799 parentState.cropRect = t.transform(parentState.cropRect);
1800 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1801 // but a transform matrix can define horizontal and vertical scales.
1802 // Let's take the average between both of them and pass into the shader, practically we
1803 // never do this type of transformation on windows anyway.
1804 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
1805 return parentState;
1806 }
1807 }
1808 const float radius = getDrawingState().cornerRadius;
Peiyong Linb9ff23e2019-04-08 11:04:59 -07001809 return radius > 0 && getCrop(getDrawingState()).isValid()
1810 ? RoundedCornerState(getCrop(getDrawingState()).toFloatRect(), radius)
1811 : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001812}
1813
Robert Carr1f0a16a2016-10-24 16:27:39 -07001814void Layer::commitChildList() {
1815 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1816 const auto& child = mCurrentChildren[i];
1817 child->commitChildList();
1818 }
1819 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001820 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001821}
1822
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001823static wp<Layer> extractLayerFromBinder(const wp<IBinder>& weakBinderHandle) {
1824 if (weakBinderHandle == nullptr) {
1825 return nullptr;
1826 }
1827 sp<IBinder> binderHandle = weakBinderHandle.promote();
1828 if (binderHandle == nullptr) {
1829 return nullptr;
1830 }
1831 sp<Layer::Handle> handle = static_cast<Layer::Handle*>(binderHandle.get());
1832 if (handle == nullptr) {
1833 return nullptr;
1834 }
1835 return handle->owner;
1836}
1837
Robert Carr720e5062018-07-30 17:45:14 -07001838void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001839 mCurrentState.inputInfo = info;
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001840 mCurrentState.touchableRegionCrop = extractLayerFromBinder(info.touchableRegionCropHandle);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001841 mCurrentState.modified = true;
1842 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07001843 setTransactionFlags(eTransactionNeeded);
1844}
1845
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001846void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet,
1847 uint32_t traceFlags) {
chaviw1d044282017-09-27 12:19:28 -07001848 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1849 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001850 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07001851
Peiyong Linefefaac2018-08-17 12:27:51 -07001852 ui::Transform requestedTransform = state.active_legacy.transform;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001853 ui::Transform transform = getTransform();
chaviw1d044282017-09-27 12:19:28 -07001854
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001855 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
1856 layerInfo->set_id(sequence);
1857 layerInfo->set_name(getName().c_str());
1858 layerInfo->set_type(String8(getTypeId()));
chaviw1d044282017-09-27 12:19:28 -07001859
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001860 for (const auto& child : children) {
1861 layerInfo->add_children(child->sequence);
chaviw1d044282017-09-27 12:19:28 -07001862 }
chaviw1d044282017-09-27 12:19:28 -07001863
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001864 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1865 sp<Layer> strongRelative = weakRelative.promote();
1866 if (strongRelative != nullptr) {
1867 layerInfo->add_relatives(strongRelative->sequence);
1868 }
chaviwadc40c22018-07-10 16:57:27 -07001869 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001870
1871 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
1872 [&]() { return layerInfo->mutable_transparent_region(); });
1873 LayerProtoHelper::writeToProto(visibleRegion,
1874 [&]() { return layerInfo->mutable_visible_region(); });
1875 LayerProtoHelper::writeToProto(surfaceDamageRegion,
1876 [&]() { return layerInfo->mutable_damage_region(); });
1877
1878 layerInfo->set_layer_stack(getLayerStack());
1879 layerInfo->set_z(state.z);
1880
1881 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
1882 [&]() { return layerInfo->mutable_position(); });
1883
1884 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(),
1885 [&]() {
1886 return layerInfo->mutable_requested_position();
1887 });
1888
1889 LayerProtoHelper::writeSizeToProto(state.active_legacy.w, state.active_legacy.h,
1890 [&]() { return layerInfo->mutable_size(); });
1891
1892 LayerProtoHelper::writeToProto(state.crop_legacy,
1893 [&]() { return layerInfo->mutable_crop(); });
1894 layerInfo->set_corner_radius(getRoundedCornerState().radius);
1895
1896 layerInfo->set_is_opaque(isOpaque(state));
1897 layerInfo->set_invalidate(contentDirty);
Peiyong Lin51598552019-04-17 14:09:22 -07001898 layerInfo->set_is_protected(isProtected());
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001899
1900 // XXX (b/79210409) mCurrentDataSpace is not protected
1901 layerInfo->set_dataspace(
1902 dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
1903
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
1910 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1911 LayerProtoHelper::writeToProto(requestedTransform,
1912 layerInfo->mutable_requested_transform());
1913
1914 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
1915 if (parent != nullptr) {
1916 layerInfo->set_parent(parent->sequence);
1917 } else {
1918 layerInfo->set_parent(-1);
1919 }
1920
1921 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1922 if (zOrderRelativeOf != nullptr) {
1923 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1924 } else {
1925 layerInfo->set_z_order_relative_of(-1);
1926 }
1927
1928 auto buffer = mActiveBuffer;
1929 if (buffer != nullptr) {
1930 LayerProtoHelper::writeToProto(buffer,
1931 [&]() { return layerInfo->mutable_active_buffer(); });
1932 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
1933 layerInfo->mutable_buffer_transform());
1934 }
1935
1936 layerInfo->set_queued_frames(getQueuedFrameCount());
1937 layerInfo->set_refresh_pending(isBufferLatched());
1938 layerInfo->set_curr_frame(mCurrentFrameNumber);
1939 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
1940
1941 for (const auto& pendingState : mPendingStates) {
1942 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
1943 if (barrierLayer != nullptr) {
1944 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
1945 barrierLayerProto->set_id(barrierLayer->sequence);
1946 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
1947 }
1948 }
1949 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
chaviwadc40c22018-07-10 16:57:27 -07001950 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001951
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001952 if (traceFlags & SurfaceTracing::TRACE_INPUT) {
1953 LayerProtoHelper::writeToProto(state.inputInfo, state.touchableRegionCrop,
1954 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001955 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001956
1957 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
1958 auto protoMap = layerInfo->mutable_metadata();
1959 for (const auto& entry : state.metadata.mMap) {
1960 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
1961 }
1962 LayerProtoHelper::writeToProto(mEffectiveTransform,
1963 layerInfo->mutable_effective_transform());
1964 LayerProtoHelper::writeToProto(mSourceBounds,
1965 [&]() { return layerInfo->mutable_source_bounds(); });
1966 LayerProtoHelper::writeToProto(mScreenBounds,
1967 [&]() { return layerInfo->mutable_screen_bounds(); });
1968 }
chaviw1d044282017-09-27 12:19:28 -07001969}
1970
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001971void Layer::writeToProto(LayerProto* layerInfo, const sp<DisplayDevice>& displayDevice,
1972 uint32_t traceFlags) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001973 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1974 if (!outputLayer) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07001975 return;
1976 }
1977
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001978 writeToProto(layerInfo, LayerVector::StateSet::Drawing, traceFlags);
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001979
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001980 const auto& compositionState = outputLayer->getState();
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001981
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001982 const Rect& frame = compositionState.displayFrame;
Nataniel Borges797b0e42019-02-15 14:11:58 -08001983 LayerProtoHelper::writeToProto(frame, [&]() { return layerInfo->mutable_hwc_frame(); });
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001984
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001985 const FloatRect& crop = compositionState.sourceCrop;
Nataniel Borges797b0e42019-02-15 14:11:58 -08001986 LayerProtoHelper::writeToProto(crop, [&]() { return layerInfo->mutable_hwc_crop(); });
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001987
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001988 const int32_t transform =
1989 getCompositionLayer() ? static_cast<int32_t>(compositionState.bufferTransform) : 0;
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001990 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08001991
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001992 const int32_t compositionType =
1993 static_cast<int32_t>(compositionState.hwc ? (*compositionState.hwc).hwcCompositionType
1994 : Hwc2::IComposerClient::Composition::CLIENT);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08001995 layerInfo->set_hwc_composition_type(compositionType);
1996
1997 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
1998 static_cast<BufferLayer*>(this)->isProtected()) {
1999 layerInfo->set_is_protected(true);
2000 } else {
2001 layerInfo->set_is_protected(false);
2002 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002003}
2004
Robert Carr2e102c92018-10-23 12:11:15 -07002005bool Layer::isRemovedFromCurrentState() const {
2006 return mRemovedFromCurrentState;
2007}
2008
Arthur Hungd20b2702019-01-14 18:16:16 +08002009InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002010 InputWindowInfo info = mDrawingState.inputInfo;
Robert Carr720e5062018-07-30 17:45:14 -07002011
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002012 if (info.displayId == ADISPLAY_ID_NONE) {
2013 info.displayId = mDrawingState.layerStack;
2014 }
2015
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002016 ui::Transform t = getTransform();
2017 const float xScale = t.sx();
2018 const float yScale = t.sy();
Arthur Hung118b1142019-05-08 21:25:59 +08002019 float xSurfaceInset = info.surfaceInset;
2020 float ySurfaceInset = info.surfaceInset;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002021 if (xScale != 1.0f || yScale != 1.0f) {
2022 info.windowXScale *= 1.0f / xScale;
2023 info.windowYScale *= 1.0f / yScale;
2024 info.touchableRegion.scaleSelf(xScale, yScale);
Arthur Hung118b1142019-05-08 21:25:59 +08002025 xSurfaceInset *= xScale;
2026 ySurfaceInset *= yScale;
Riddle Hsu39d4aa52018-11-30 20:46:53 +08002027 }
Robert Carre07e1032018-11-26 12:55:53 -08002028
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002029 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002030 // If this is a portal window, set the touchableRegion to the layerBounds.
2031 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
2032 ? getBufferSize(getDrawingState())
2033 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002034 if (!layerBounds.isValid()) {
2035 layerBounds = getCroppedBufferSize(getDrawingState());
2036 }
Vishnu Nair8033a492018-12-05 07:27:23 -08002037 layerBounds = t.transform(layerBounds);
Arthur Hung118b1142019-05-08 21:25:59 +08002038 layerBounds.inset(xSurfaceInset, ySurfaceInset, xSurfaceInset, ySurfaceInset);
Vishnu Nair8033a492018-12-05 07:27:23 -08002039
Arthur Hungd20b2702019-01-14 18:16:16 +08002040 // Input coordinate should match the layer bounds.
2041 info.frameLeft = layerBounds.left;
2042 info.frameTop = layerBounds.top;
2043 info.frameRight = layerBounds.right;
2044 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08002045
2046 // Position the touchable region relative to frame screen location and restrict it to frame
2047 // bounds.
2048 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08002049 info.visible = canReceiveInput();
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002050
2051 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2052 if (info.replaceTouchableRegionWithCrop) {
2053 if (cropLayer == nullptr) {
2054 info.touchableRegion = Region(Rect{mScreenBounds});
2055 } else {
2056 info.touchableRegion = Region(Rect{cropLayer->mScreenBounds});
2057 }
2058 } else if (cropLayer != nullptr) {
2059 info.touchableRegion = info.touchableRegion.intersect(Rect{cropLayer->mScreenBounds});
2060 }
2061
Robert Carr720e5062018-07-30 17:45:14 -07002062 return info;
2063}
2064
2065bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002066 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002067}
2068
Lloyd Piquefeb73d72018-12-04 17:23:44 -08002069std::shared_ptr<compositionengine::Layer> Layer::getCompositionLayer() const {
2070 return nullptr;
2071}
2072
chaviw3e727cd2019-01-31 13:41:05 -08002073bool Layer::canReceiveInput() const {
2074 return isVisible();
2075}
2076
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002077compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
2078 const sp<const DisplayDevice>& display) const {
2079 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionLayer().get());
2080}
2081
Mathias Agopian13127d82013-03-05 17:47:11 -08002082// ---------------------------------------------------------------------------
2083
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002084}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002085
2086#if defined(__gl_h_)
2087#error "don't include gl/gl.h in this file"
2088#endif
2089
2090#if defined(__gl2_h_)
2091#error "don't include gl2/gl2.h in this file"
2092#endif