blob: cbe8b299db34ffa70ee71e75b7f685d1434bd9ae [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
Chia-I Wuc6657022017-08-15 11:18:17 -0700147void Layer::onRemovedFromCurrentState() {
Robert Carr2e102c92018-10-23 12:11:15 -0700148 mRemovedFromCurrentState = true;
149
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800150 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
151 if (mCurrentState.zOrderRelativeOf != nullptr) {
152 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
153 if (strongRelative != nullptr) {
154 strongRelative->removeZOrderRelative(this);
155 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700156 }
chaviw606e5cf2019-03-01 10:12:10 -0800157 setZOrderRelativeOf(nullptr);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700158 }
Rob Carr4bba3702018-10-08 21:53:30 +0000159
Robert Carr2e102c92018-10-23 12:11:15 -0700160 // Since we are no longer reachable from CurrentState SurfaceFlinger
161 // will no longer invoke doTransaction for us, and so we will
162 // never finish applying transactions. We signal the sync point
163 // now so that another layer will not become indefinitely
164 // blocked.
165 for (auto& point: mRemoteSyncPoints) {
166 point->setTransactionApplied();
167 }
168 mRemoteSyncPoints.clear();
169
170 {
171 Mutex::Autolock syncLock(mLocalSyncPointMutex);
172 for (auto& point : mLocalSyncPoints) {
173 point->setFrameAvailable();
174 }
175 mLocalSyncPoints.clear();
176 }
177
Chia-I Wuc6657022017-08-15 11:18:17 -0700178 for (const auto& child : mCurrentChildren) {
179 child->onRemovedFromCurrentState();
180 }
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800181
182 mFlinger->markLayerPendingRemovalLocked(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700183}
Chia-I Wu38512252017-05-17 14:36:16 -0700184
chaviw61626f22018-11-15 16:26:27 -0800185void Layer::addToCurrentState() {
186 mRemovedFromCurrentState = false;
187
188 for (const auto& child : mCurrentChildren) {
189 child->addToCurrentState();
190 }
191}
192
Mathias Agopian13127d82013-03-05 17:47:11 -0800193// ---------------------------------------------------------------------------
194// set-up
195// ---------------------------------------------------------------------------
196
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700197const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800198 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800199}
200
chaviw13fdc492017-06-27 12:40:18 -0700201bool Layer::getPremultipledAlpha() const {
202 return mPremultipliedAlpha;
203}
204
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700205sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800206 Mutex::Autolock _l(mLock);
Robert Carrc0df3122019-04-11 13:18:21 -0700207 if (mGetHandleCalled) {
208 ALOGE("Get handle called twice" );
209 return nullptr;
210 }
211 mGetHandleCalled = true;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700212 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800213}
214
215// ---------------------------------------------------------------------------
216// h/w composer set-up
217// ---------------------------------------------------------------------------
218
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800219bool Layer::hasHwcLayer(const sp<const DisplayDevice>& displayDevice) {
220 auto outputLayer = findOutputLayerForDisplay(displayDevice);
221 LOG_FATAL_IF(!outputLayer);
222 return outputLayer->getState().hwc && (*outputLayer->getState().hwc).hwcLayer != nullptr;
223}
224
225HWC2::Layer* Layer::getHwcLayer(const sp<const DisplayDevice>& displayDevice) {
226 auto outputLayer = findOutputLayerForDisplay(displayDevice);
227 if (!outputLayer || !outputLayer->getState().hwc) {
228 return nullptr;
229 }
230 return (*outputLayer->getState().hwc).hwcLayer.get();
Steven Thomasb02664d2017-07-26 18:48:28 -0700231}
Steven Thomasb02664d2017-07-26 18:48:28 -0700232
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800233Rect Layer::getContentCrop() const {
234 // this is the crop rectangle that applies to the buffer
235 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700236 Rect crop;
237 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800238 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700239 crop = mCurrentCrop;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800240 } else if (mActiveBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800241 // otherwise we use the whole buffer
Lloyd Pique0b785d82018-12-04 17:25:27 -0800242 crop = mActiveBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700243 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800244 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700245 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700246 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700247 return crop;
248}
249
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700250static Rect reduce(const Rect& win, const Region& exclude) {
251 if (CC_LIKELY(exclude.isEmpty())) {
252 return win;
253 }
254 if (exclude.isRect()) {
255 return win.reduce(exclude.getBounds());
256 }
257 return Region(win).subtract(exclude).getBounds();
258}
259
Dan Stoza80d61162017-12-20 15:57:52 -0800260static FloatRect reduce(const FloatRect& win, const Region& exclude) {
261 if (CC_LIKELY(exclude.isEmpty())) {
262 return win;
263 }
264 // Convert through Rect (by rounding) for lack of FloatRegion
265 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
266}
267
Vishnu Nair4351ad52019-02-11 14:13:02 -0800268Rect Layer::getScreenBounds(bool reduceTransparentRegion) const {
Vishnu Nairf0c28512019-02-08 12:40:28 -0800269 if (!reduceTransparentRegion) {
270 return Rect{mScreenBounds};
271 }
272
273 FloatRect bounds = getBounds();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800274 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800275 // Transform to screen space.
276 bounds = t.transform(bounds);
277 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700278}
279
Vishnu Nair4351ad52019-02-11 14:13:02 -0800280FloatRect Layer::getBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000281 const State& s(getDrawingState());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800282 return getBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700283}
284
Vishnu Nairf0c28512019-02-08 12:40:28 -0800285FloatRect Layer::getBounds(const Region& activeTransparentRegion) const {
286 // Subtract the transparent region and snap to the bounds.
287 return reduce(mBounds, activeTransparentRegion);
288}
289
Vishnu Nairc652ff82019-03-15 12:48:54 -0700290ui::Transform Layer::getBufferScaleTransform() const {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800291 // If the layer is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
292 // it isFixedSize) then there may be additional scaling not accounted
Vishnu Nairc652ff82019-03-15 12:48:54 -0700293 // for in the layer transform.
Lloyd Pique0b785d82018-12-04 17:25:27 -0800294 if (!isFixedSize() || !mActiveBuffer) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700295 return {};
Vishnu Nair4351ad52019-02-11 14:13:02 -0800296 }
297
Marissa Wall290ad082019-03-06 13:23:47 -0800298 // If the layer is a buffer state layer, the active width and height
299 // could be infinite. In that case, return the effective transform.
300 const uint32_t activeWidth = getActiveWidth(getDrawingState());
301 const uint32_t activeHeight = getActiveHeight(getDrawingState());
302 if (activeWidth >= UINT32_MAX && activeHeight >= UINT32_MAX) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700303 return {};
Marissa Wall290ad082019-03-06 13:23:47 -0800304 }
305
Vishnu Nairc652ff82019-03-15 12:48:54 -0700306 int bufferWidth = mActiveBuffer->getWidth();
307 int bufferHeight = mActiveBuffer->getHeight();
308
309 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
310 std::swap(bufferWidth, bufferHeight);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800311 }
Vishnu Nairc652ff82019-03-15 12:48:54 -0700312
Marissa Wall290ad082019-03-06 13:23:47 -0800313 float sx = activeWidth / static_cast<float>(bufferWidth);
314 float sy = activeHeight / static_cast<float>(bufferHeight);
315
Vishnu Nair4351ad52019-02-11 14:13:02 -0800316 ui::Transform extraParentScaling;
317 extraParentScaling.set(sx, 0, 0, sy);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700318 return extraParentScaling;
319}
320
321ui::Transform Layer::getTransformWithScale(const ui::Transform& bufferScaleTransform) const {
322 // We need to mirror this scaling to child surfaces or we will break the contract where WM can
323 // treat child surfaces as pixels in the parent surface.
324 if (!isFixedSize() || !mActiveBuffer) {
325 return mEffectiveTransform;
326 }
327 return mEffectiveTransform * bufferScaleTransform;
328}
329
330FloatRect Layer::getBoundsPreScaling(const ui::Transform& bufferScaleTransform) const {
331 // We need the pre scaled layer bounds when computing child bounds to make sure the child is
332 // cropped to its parent layer after any buffer transform scaling is applied.
333 if (!isFixedSize() || !mActiveBuffer) {
334 return mBounds;
335 }
336 return bufferScaleTransform.inverse().transform(mBounds);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800337}
338
339void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform) {
340 const State& s(getDrawingState());
341
342 // Calculate effective layer transform
343 mEffectiveTransform = parentTransform * getActiveTransform(s);
344
345 // Transform parent bounds to layer space
346 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
347
Vishnu Nairc652ff82019-03-15 12:48:54 -0700348 // Calculate source bounds
Vishnu Nair4351ad52019-02-11 14:13:02 -0800349 mSourceBounds = computeSourceBounds(parentBounds);
350
351 // Calculate bounds by croping diplay frame with layer crop and parent bounds
352 FloatRect bounds = mSourceBounds;
353 const Rect layerCrop = getCrop(s);
354 if (!layerCrop.isEmpty()) {
355 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
356 }
357 bounds = bounds.intersect(parentBounds);
358
359 mBounds = bounds;
360 mScreenBounds = mEffectiveTransform.transform(mBounds);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700361
362 // Add any buffer scaling to the layer's children.
363 ui::Transform bufferScaleTransform = getBufferScaleTransform();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800364 for (const sp<Layer>& child : mDrawingChildren) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700365 child->computeBounds(getBoundsPreScaling(bufferScaleTransform),
366 getTransformWithScale(bufferScaleTransform));
Vishnu Nair4351ad52019-02-11 14:13:02 -0800367 }
368}
369
Vishnu Nair60356342018-11-13 13:00:45 -0800370Rect Layer::getCroppedBufferSize(const State& s) const {
371 Rect size = getBufferSize(s);
372 Rect crop = getCrop(s);
373 if (!crop.isEmpty() && size.isValid()) {
374 size.intersect(crop, &size);
375 } else if (!crop.isEmpty()) {
376 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700377 }
Vishnu Nair60356342018-11-13 13:00:45 -0800378 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800379}
380
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700381void Layer::setupRoundedCornersCropCoordinates(Rect win,
382 const FloatRect& roundedCornersCrop) const {
383 // Translate win by the rounded corners rect coordinates, to have all values in
384 // layer coordinate space.
385 win.left -= roundedCornersCrop.left;
386 win.right -= roundedCornersCrop.left;
387 win.top -= roundedCornersCrop.top;
388 win.bottom -= roundedCornersCrop.top;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700389}
390
Lloyd Piquea83776c2019-01-29 18:42:32 -0800391void Layer::latchGeometry(compositionengine::LayerFECompositionState& compositionState) const {
392 const auto& drawingState{getDrawingState()};
393 auto alpha = static_cast<float>(getAlpha());
David Revemanecf0fa52017-03-03 11:32:44 -0500394 auto blendMode = HWC2::BlendMode::None;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800395 if (!isOpaque(drawingState) || alpha != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800396 blendMode =
397 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800398 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800399
Lloyd Piquea83776c2019-01-29 18:42:32 -0800400 int type = drawingState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
401 int appId = drawingState.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700402 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400403 if (parent.get()) {
404 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800405 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
406 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
407 if (parentType >= 0 || parentAppId >= 0) {
408 type = parentType;
409 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700410 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400411 }
412
Lloyd Piquea83776c2019-01-29 18:42:32 -0800413 compositionState.geomLayerTransform = getTransform();
414 compositionState.geomInverseLayerTransform = compositionState.geomLayerTransform.inverse();
415 compositionState.geomBufferSize = getBufferSize(drawingState);
416 compositionState.geomContentCrop = getContentCrop();
417 compositionState.geomCrop = getCrop(drawingState);
418 compositionState.geomBufferTransform = mCurrentTransform;
419 compositionState.geomBufferUsesDisplayInverseTransform = getTransformToDisplayInverse();
420 compositionState.geomActiveTransparentRegion = getActiveTransparentRegion(drawingState);
421 compositionState.geomLayerBounds = mBounds;
422 compositionState.geomUsesSourceCrop = usesSourceCrop();
423 compositionState.isSecure = isSecure();
David Sodman15094112018-10-11 09:39:37 -0700424
Lloyd Piquea83776c2019-01-29 18:42:32 -0800425 compositionState.blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
426 compositionState.alpha = alpha;
427 compositionState.type = type;
428 compositionState.appId = appId;
429}
David Sodmanba340492018-08-05 21:51:33 -0700430
Lloyd Piquea83776c2019-01-29 18:42:32 -0800431void Layer::latchCompositionState(compositionengine::LayerFECompositionState& compositionState,
432 bool includeGeometry) const {
433 if (includeGeometry) {
434 latchGeometry(compositionState);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700435 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800436}
Mathias Agopian29a367b2011-07-12 14:51:45 -0700437
Lloyd Piquea83776c2019-01-29 18:42:32 -0800438const char* Layer::getDebugName() const {
439 return mName.string();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800440}
441
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800442void Layer::forceClientComposition(const sp<DisplayDevice>& display) {
443 const auto outputLayer = findOutputLayerForDisplay(display);
444 LOG_FATAL_IF(!outputLayer);
445 outputLayer->editState().forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800446}
Dan Stozaee44edd2015-03-23 15:50:23 -0700447
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800448bool Layer::getForceClientComposition(const sp<DisplayDevice>& display) {
449 const auto outputLayer = findOutputLayerForDisplay(display);
450 LOG_FATAL_IF(!outputLayer);
451 return outputLayer->getState().forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800452}
453
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700454void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800455 const auto outputLayer = findOutputLayerForDisplay(display);
456 LOG_FATAL_IF(!outputLayer);
457
458 if (!outputLayer->getState().hwc ||
459 (*outputLayer->getState().hwc).hwcCompositionType !=
460 Hwc2::IComposerClient::Composition::CURSOR) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800461 return;
462 }
463
464 // This gives us only the "orientation" component of the transform
Vishnu Nair33a6eee2019-02-06 13:48:06 -0800465 const State& s(getDrawingState());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800466
467 // Apply the layer's transform, followed by the display's global transform
468 // Here we're guaranteed that the layer's transform preserves rects
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800469 Rect win = getCroppedBufferSize(s);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800470 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700471 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800472 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700473 frame.intersect(display->getViewport(), &frame);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700474 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800475 auto position = displayTransform.transform(frame);
476
Dominik Laskowski7e045462018-05-30 13:02:02 -0700477 auto error =
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800478 (*outputLayer->getState().hwc).hwcLayer->setCursorPosition(position.left, position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800479 ALOGE_IF(error != HWC2::Error::None,
480 "[%s] Failed to set cursor position "
481 "to (%d, %d): %s (%d)",
482 mName.string(), position.left, position.top, to_string(error).c_str(),
483 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800484}
Riley Andrews03414a12014-07-01 14:22:59 -0700485
Mathias Agopian13127d82013-03-05 17:47:11 -0800486// ---------------------------------------------------------------------------
487// drawing...
488// ---------------------------------------------------------------------------
489
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000490bool Layer::prepareClientLayer(const RenderArea& renderArea, const Region& clip,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800491 Region& clearRegion, const bool supportProtectedContent,
492 renderengine::LayerSettings& layer) {
493 return prepareClientLayer(renderArea, clip, false, clearRegion, supportProtectedContent, layer);
Mathias Agopian13127d82013-03-05 17:47:11 -0800494}
495
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000496bool Layer::prepareClientLayer(const RenderArea& renderArea, bool useIdentityTransform,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800497 Region& clearRegion, const bool supportProtectedContent,
498 renderengine::LayerSettings& layer) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000499 return prepareClientLayer(renderArea, Region(renderArea.getBounds()), useIdentityTransform,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800500 clearRegion, supportProtectedContent, layer);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000501}
502
503bool Layer::prepareClientLayer(const RenderArea& /*renderArea*/, const Region& /*clip*/,
504 bool useIdentityTransform, Region& /*clearRegion*/,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800505 const bool /*supportProtectedContent*/,
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000506 renderengine::LayerSettings& layer) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800507 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000508 half alpha = getAlpha();
509 layer.geometry.boundaries = bounds;
510 if (useIdentityTransform) {
511 layer.geometry.positionTransform = mat4();
512 } else {
513 const ui::Transform transform = getTransform();
514 mat4 m;
515 m[0][0] = transform[0][0];
516 m[0][1] = transform[0][1];
517 m[0][3] = transform[0][2];
518 m[1][0] = transform[1][0];
519 m[1][1] = transform[1][1];
520 m[1][3] = transform[1][2];
521 m[3][0] = transform[2][0];
522 m[3][1] = transform[2][1];
523 m[3][3] = transform[2][2];
524 layer.geometry.positionTransform = m;
525 }
526
527 if (hasColorTransform()) {
528 layer.colorTransform = getColorTransform();
529 }
530
531 const auto roundedCornerState = getRoundedCornerState();
532 layer.geometry.roundedCornersRadius = roundedCornerState.radius;
533 layer.geometry.roundedCornersCrop = roundedCornerState.cropRect;
534
535 layer.alpha = alpha;
536 layer.sourceDataspace = mCurrentDataSpace;
537 return true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800538}
539
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800540void Layer::setCompositionType(const sp<const DisplayDevice>& display,
541 Hwc2::IComposerClient::Composition type) {
542 const auto outputLayer = findOutputLayerForDisplay(display);
543 LOG_FATAL_IF(!outputLayer);
544 LOG_FATAL_IF(!outputLayer->getState().hwc);
545 auto& compositionState = outputLayer->editState();
546
547 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", ((*compositionState.hwc).hwcLayer)->getId(),
548 toString(type).c_str(), 1);
549 if ((*compositionState.hwc).hwcCompositionType != type) {
David Sodman15094112018-10-11 09:39:37 -0700550 ALOGV(" actually setting");
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800551 (*compositionState.hwc).hwcCompositionType = type;
552
553 auto error = (*compositionState.hwc)
554 .hwcLayer->setCompositionType(static_cast<HWC2::Composition>(type));
555 ALOGE_IF(error != HWC2::Error::None,
556 "[%s] Failed to set "
557 "composition type %s: %s (%d)",
558 mName.string(), toString(type).c_str(), to_string(error).c_str(),
559 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800560 }
561}
562
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800563Hwc2::IComposerClient::Composition Layer::getCompositionType(
564 const sp<const DisplayDevice>& display) const {
565 const auto outputLayer = findOutputLayerForDisplay(display);
566 LOG_FATAL_IF(!outputLayer);
567 return outputLayer->getState().hwc ? (*outputLayer->getState().hwc).hwcCompositionType
568 : Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800569}
570
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800571bool Layer::getClearClientTarget(const sp<const DisplayDevice>& display) const {
572 const auto outputLayer = findOutputLayerForDisplay(display);
573 LOG_FATAL_IF(!outputLayer);
574 return outputLayer->getState().clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800575}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800576
Dan Stozacac35382016-01-27 12:21:06 -0800577bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
578 if (point->getFrameNumber() <= mCurrentFrameNumber) {
579 // Don't bother with a SyncPoint, since we've already latched the
580 // relevant frame
581 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700582 }
Robert Carr2e102c92018-10-23 12:11:15 -0700583 if (isRemovedFromCurrentState()) {
584 return false;
585 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700586
Dan Stozacac35382016-01-27 12:21:06 -0800587 Mutex::Autolock lock(mLocalSyncPointMutex);
588 mLocalSyncPoints.push_back(point);
589 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700590}
591
Mathias Agopian13127d82013-03-05 17:47:11 -0800592// ----------------------------------------------------------------------------
593// local state
594// ----------------------------------------------------------------------------
595
Peiyong Lin833074a2018-08-28 11:53:54 -0700596void Layer::computeGeometry(const RenderArea& renderArea,
597 renderengine::Mesh& mesh,
chaviwa76b2712017-09-20 12:02:26 -0700598 bool useIdentityTransform) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700599 const ui::Transform renderAreaTransform(renderArea.getTransform());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800600 FloatRect win = getBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700601
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000602 vec2 lt = vec2(win.left, win.top);
603 vec2 lb = vec2(win.left, win.bottom);
604 vec2 rb = vec2(win.right, win.bottom);
605 vec2 rt = vec2(win.right, win.top);
606
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800607 ui::Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000608 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700609 lt = layerTransform.transform(lt);
610 lb = layerTransform.transform(lb);
611 rb = layerTransform.transform(rb);
612 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000613 }
614
Peiyong Lin833074a2018-08-28 11:53:54 -0700615 renderengine::Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700616 position[0] = renderAreaTransform.transform(lt);
617 position[1] = renderAreaTransform.transform(lb);
618 position[2] = renderAreaTransform.transform(rb);
619 position[3] = renderAreaTransform.transform(rt);
Mathias Agopian13127d82013-03-05 17:47:11 -0800620}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800621
David Sodman41fdfc92017-11-06 16:09:56 -0800622bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800623 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700624 return (s.flags & layer_state_t::eLayerSecure);
625}
626
Mathias Agopian13127d82013-03-05 17:47:11 -0800627void Layer::setVisibleRegion(const Region& visibleRegion) {
628 // always called from main thread
629 this->visibleRegion = visibleRegion;
630}
631
632void Layer::setCoveredRegion(const Region& coveredRegion) {
633 // always called from main thread
634 this->coveredRegion = coveredRegion;
635}
636
David Sodman41fdfc92017-11-06 16:09:56 -0800637void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800638 // always called from main thread
639 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
640}
641
Robert Carre5f4f692018-01-12 13:12:28 -0800642void Layer::clearVisibilityRegions() {
643 visibleRegion.clear();
644 visibleNonTransparentRegion.clear();
645 coveredRegion.clear();
646}
647
Mathias Agopian13127d82013-03-05 17:47:11 -0800648// ----------------------------------------------------------------------------
649// transaction
650// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800651
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800652void Layer::pushPendingState() {
653 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700654 return;
655 }
656
Dan Stoza7dde5992015-05-22 09:51:44 -0700657 // If this transaction is waiting on the receipt of a frame, generate a sync
658 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700659 // We don't allow installing sync points after we are removed from the current state
660 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800661 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
662 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800663 if (barrierLayer == nullptr) {
664 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700665 // If we can't promote the layer we are intended to wait on,
666 // then it is expired or otherwise invalid. Allow this transaction
667 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800668 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800669 } else {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800670 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy);
Robert Carr0d480722017-01-10 16:42:54 -0800671 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800672 mRemoteSyncPoints.push_back(std::move(syncPoint));
673 } else {
674 // We already missed the frame we're supposed to synchronize
675 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800676 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800677 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700678 }
679
Dan Stoza7dde5992015-05-22 09:51:44 -0700680 // Wake us up to check if the frame has been received
681 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700682 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700683 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800684 mPendingStates.push_back(mCurrentState);
685 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700686}
687
Pablo Ceballos05289c22016-04-14 15:49:55 -0700688void Layer::popPendingState(State* stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800689 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700690
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800691 mPendingStates.removeAt(0);
692 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700693}
694
Pablo Ceballos05289c22016-04-14 15:49:55 -0700695bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700696 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800697 while (!mPendingStates.empty()) {
698 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700699 if (mRemoteSyncPoints.empty()) {
700 // If we don't have a sync point for this, apply it anyway. It
701 // will be visually wrong, but it should keep us from getting
702 // into too much trouble.
703 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700704 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700705 stateUpdateAvailable = true;
706 continue;
707 }
708
Marissa Wallf58c14b2018-07-24 10:50:43 -0700709 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800710 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800711 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800712
713 // Signal our end of the sync point and then dispose of it
714 mRemoteSyncPoints.front()->setTransactionApplied();
715 mRemoteSyncPoints.pop_front();
716 continue;
717 }
718
Dan Stoza7dde5992015-05-22 09:51:44 -0700719 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
720 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700721 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700722 stateUpdateAvailable = true;
723
724 // Signal our end of the sync point and then dispose of it
725 mRemoteSyncPoints.front()->setTransactionApplied();
726 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800727 } else {
728 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700729 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700730 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700731 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700732 stateUpdateAvailable = true;
733 }
734 }
735
736 // If we still have pending updates, wake SurfaceFlinger back up and point
737 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800738 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700739 setTransactionFlags(eTransactionNeeded);
740 mFlinger->setTransactionFlags(eTraversalNeeded);
741 }
742
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800743 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700744 return stateUpdateAvailable;
745}
746
Marissa Wall61c58622018-07-18 10:12:20 -0700747uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000748 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800749
Marissa Wall61c58622018-07-18 10:12:20 -0700750 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
751 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700752
David Sodmaneb085e02017-10-05 18:49:04 -0700753 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700754 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000755 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800756 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
757 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
758 " requested={ wh={%4u,%4u} }}\n"
759 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
760 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700761 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700762 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
763 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
764 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
765 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
766 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700767 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
768 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
769 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800770 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700771
Robert Carre392b552017-09-19 12:16:05 -0700772 // Don't let Layer::doTransaction update the drawing state
773 // if we have a pending resize, unless we are in fixed-size mode.
774 // the drawing state will be updated only once we receive a buffer
775 // with the correct size.
776 //
777 // In particular, we want to make sure the clip (which is part
778 // of the geometry state) is latched together with the size but is
779 // latched immediately when no resizing is involved.
780 //
781 // If a sideband stream is attached, however, we want to skip this
782 // optimization so that transactions aren't missed when a buffer
783 // never arrives
784 //
785 // In the case that we don't have a buffer we ignore other factors
786 // and avoid entering the resizePending state. At a high level the
787 // resizePending state is to avoid applying the state of the new buffer
788 // to the old buffer. However in the state where we don't have an old buffer
789 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700790 const bool resizePending =
791 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
792 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
Lloyd Pique0b785d82018-12-04 17:25:27 -0800793 (mActiveBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700794 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800795 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700796 flags |= eDontUpdateGeometryState;
797 }
798 }
799
Robert Carr7bf247e2017-05-18 14:02:49 -0700800 // Here we apply various requested geometry states, depending on our
801 // latching configuration. See Layer.h for a detailed discussion of
802 // how geometry latching is controlled.
803 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000804 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700805
806 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
807 // mode, which causes attributes which normally latch regardless of scaling mode,
808 // to be delayed. We copy the requested state to the active state making sure
809 // to respect these rules (again see Layer.h for a detailed discussion).
810 //
811 // There is an awkward asymmetry in the handling of the crop states in the position
812 // states, as can be seen below. Largely this arises from position and transform
813 // being stored in the same data structure while having different latching rules.
814 // b/38182305
815 //
Marissa Wall61c58622018-07-18 10:12:20 -0700816 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700817 // applyPendingStates in the presence of deferred transactions.
818 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -0700819 float tx = stateToCommit->active_legacy.transform.tx();
820 float ty = stateToCommit->active_legacy.transform.ty();
821 stateToCommit->active_legacy = stateToCommit->requested_legacy;
822 stateToCommit->active_legacy.transform.set(tx, ty);
823 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700824 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700825 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -0700826 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700827 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800828 }
829
Marissa Wall61c58622018-07-18 10:12:20 -0700830 return flags;
831}
832
833uint32_t Layer::doTransaction(uint32_t flags) {
834 ATRACE_CALL();
835
chaviw5aedec92018-10-22 10:40:38 -0700836 if (mLayerDetached) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800837 return flags;
838 }
839
840 if (mChildrenChanged) {
841 flags |= eVisibleRegion;
842 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -0700843 }
844
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800845 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +0000846 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -0700847 if (!applyPendingStates(&c)) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800848 return flags;
Marissa Wall61c58622018-07-18 10:12:20 -0700849 }
850
851 flags = doTransactionResize(flags, &c);
852
Alec Mourib416efd2018-09-06 21:01:59 +0000853 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700854
855 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800856 // invalidate and recompute the visible regions if needed
857 flags |= Layer::eVisibleRegion;
858 }
859
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700860 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800861 // invalidate and recompute the visible regions if needed
862 flags |= eVisibleRegion;
863 this->contentDirty = true;
864
865 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -0700866 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -0700867 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -0800868 }
869
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800870 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -0700871 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800872 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -0700873 }
874
Mathias Agopian13127d82013-03-05 17:47:11 -0800875 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -0700876 commitTransaction(c);
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800877 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -0800878 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800879}
880
Pablo Ceballos05289c22016-04-14 15:49:55 -0700881void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800882 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700883}
884
Mathias Agopian13127d82013-03-05 17:47:11 -0800885uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800886 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800887}
888
889uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800890 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -0800891}
892
Robert Carr82364e32016-05-15 11:27:47 -0700893bool Layer::setPosition(float x, float y, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800894 if (mCurrentState.requested_legacy.transform.tx() == x &&
895 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -0800896 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800897 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -0700898
899 // We update the requested and active position simultaneously because
900 // we want to apply the position portion of the transform matrix immediately,
901 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800902 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -0700903 if (immediate && !mFreezeGeometryUpdates) {
904 // Here we directly update the active state
905 // unlike other setters, because we store it within
906 // the transform, but use different latching rules.
907 // b/38182305
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800908 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -0700909 }
Robert Carr7bf247e2017-05-18 14:02:49 -0700910 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -0700911
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800912 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800913 setTransactionFlags(eTransactionNeeded);
914 return true;
915}
Robert Carr82364e32016-05-15 11:27:47 -0700916
Robert Carr1f0a16a2016-10-24 16:27:39 -0700917bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
918 ssize_t idx = mCurrentChildren.indexOf(childLayer);
919 if (idx < 0) {
920 return false;
921 }
922 if (childLayer->setLayer(z)) {
923 mCurrentChildren.removeAt(idx);
924 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800925 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700926 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800927 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700928}
929
Robert Carr503c7042017-09-27 15:06:08 -0700930bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
931 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
932 ssize_t idx = mCurrentChildren.indexOf(childLayer);
933 if (idx < 0) {
934 return false;
935 }
936 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
937 mCurrentChildren.removeAt(idx);
938 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800939 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700940 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800941 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700942}
943
Robert Carrae060832016-11-28 10:51:00 -0800944bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800945 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
946 mCurrentState.sequence++;
947 mCurrentState.z = z;
948 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700949
950 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800951 if (mCurrentState.zOrderRelativeOf != nullptr) {
952 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700953 if (strongRelative != nullptr) {
954 strongRelative->removeZOrderRelative(this);
955 }
chaviw606e5cf2019-03-01 10:12:10 -0800956 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -0700957 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800958 setTransactionFlags(eTransactionNeeded);
959 return true;
960}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700961
Robert Carrdb66e622017-04-10 16:55:57 -0700962void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800963 mCurrentState.zOrderRelatives.remove(relative);
964 mCurrentState.sequence++;
965 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700966 setTransactionFlags(eTransactionNeeded);
967}
968
969void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800970 mCurrentState.zOrderRelatives.add(relative);
971 mCurrentState.modified = true;
972 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -0700973 setTransactionFlags(eTransactionNeeded);
974}
975
chaviw606e5cf2019-03-01 10:12:10 -0800976void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
977 mCurrentState.zOrderRelativeOf = relativeOf;
978 mCurrentState.sequence++;
979 mCurrentState.modified = true;
980 setTransactionFlags(eTransactionNeeded);
981}
982
Robert Carr503d2bd2017-12-04 15:49:47 -0800983bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -0700984 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
985 if (handle == nullptr) {
986 return false;
987 }
988 sp<Layer> relative = handle->owner.promote();
989 if (relative == nullptr) {
990 return false;
991 }
992
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800993 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
994 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -0800995 return false;
996 }
997
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800998 mCurrentState.sequence++;
999 mCurrentState.modified = true;
1000 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001001
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001002 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -07001003 if (oldZOrderRelativeOf != nullptr) {
1004 oldZOrderRelativeOf->removeZOrderRelative(this);
1005 }
chaviw606e5cf2019-03-01 10:12:10 -08001006 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -07001007 relative->addZOrderRelative(this);
1008
1009 setTransactionFlags(eTransactionNeeded);
1010
1011 return true;
1012}
1013
Mathias Agopian13127d82013-03-05 17:47:11 -08001014bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001015 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001016 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001017 mCurrentState.requested_legacy.w = w;
1018 mCurrentState.requested_legacy.h = h;
1019 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001020 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001021
1022 // record the new size, from this point on, when the client request
1023 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001024 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001025 return true;
1026}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001027bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001028 if (mCurrentState.color.a == alpha) return false;
1029 mCurrentState.sequence++;
1030 mCurrentState.color.a = alpha;
1031 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001032 setTransactionFlags(eTransactionNeeded);
1033 return true;
1034}
chaviw13fdc492017-06-27 12:40:18 -07001035
Valerie Haudd0b7572019-01-29 14:59:27 -08001036bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1037 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001038 return false;
Valerie Hauaa194562019-02-05 16:21:38 -08001039 }
1040 mCurrentState.sequence++;
1041 mCurrentState.modified = true;
1042 setTransactionFlags(eTransactionNeeded);
1043
1044 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001045 // create background color layer if one does not yet exist
1046 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
1047 const String8& name = mName + "BackgroundColorLayer";
Evan Roskya1f1e152019-01-24 16:17:46 -08001048 mCurrentState.bgColorLayer = new ColorLayer(
1049 LayerCreationArgs(mFlinger.get(), nullptr, name, 0, 0, flags, LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -07001050
Valerie Haudd0b7572019-01-29 14:59:27 -08001051 // add to child list
1052 addChild(mCurrentState.bgColorLayer);
1053 mFlinger->mLayersAdded = true;
1054 // set up SF to handle added color layer
1055 if (isRemovedFromCurrentState()) {
1056 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1057 }
1058 mFlinger->setTransactionFlags(eTransactionNeeded);
1059 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1060 mCurrentState.bgColorLayer->reparent(nullptr);
1061 mCurrentState.bgColorLayer = nullptr;
1062 return true;
1063 }
1064
1065 mCurrentState.bgColorLayer->setColor(color);
1066 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1067 mCurrentState.bgColorLayer->setAlpha(alpha);
1068 mCurrentState.bgColorLayer->setDataspace(dataspace);
1069
chaviw13fdc492017-06-27 12:40:18 -07001070 return true;
1071}
1072
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001073bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001074 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001075
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001076 mCurrentState.sequence++;
1077 mCurrentState.cornerRadius = cornerRadius;
1078 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001079 setTransactionFlags(eTransactionNeeded);
1080 return true;
1081}
1082
Robert Carrd4ae7f32018-06-07 16:10:57 -07001083bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1084 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001085 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001086 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1087
1088 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1089 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1090 return false;
1091 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001092 mCurrentState.sequence++;
1093 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1094 matrix.dsdy);
1095 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001096 setTransactionFlags(eTransactionNeeded);
1097 return true;
1098}
Marissa Wall61c58622018-07-18 10:12:20 -07001099
Mathias Agopian13127d82013-03-05 17:47:11 -08001100bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001101 mCurrentState.requestedTransparentRegion_legacy = transparent;
1102 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001103 setTransactionFlags(eTransactionNeeded);
1104 return true;
1105}
1106bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001107 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1108 if (mCurrentState.flags == newFlags) return false;
1109 mCurrentState.sequence++;
1110 mCurrentState.flags = newFlags;
1111 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001112 setTransactionFlags(eTransactionNeeded);
1113 return true;
1114}
Robert Carr99e27f02016-06-16 15:18:02 -07001115
Marissa Wallf58c14b2018-07-24 10:50:43 -07001116bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001117 if (mCurrentState.requestedCrop_legacy == crop) return false;
1118 mCurrentState.sequence++;
1119 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001120 if (immediate && !mFreezeGeometryUpdates) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001121 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001122 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001123 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1124
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001125 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001126 setTransactionFlags(eTransactionNeeded);
1127 return true;
1128}
Robert Carr8d5227b2017-03-16 15:41:03 -07001129
Robert Carrc3574f72016-03-24 12:19:32 -07001130bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001131 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001132 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001133 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001134 return true;
1135}
1136
Evan Roskyef876c92019-01-25 17:46:06 -08001137bool Layer::setMetadata(const LayerMetadata& data) {
1138 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001139 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001140 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001141 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001142 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001143}
1144
Mathias Agopian13127d82013-03-05 17:47:11 -08001145bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001146 if (mCurrentState.layerStack == layerStack) return false;
1147 mCurrentState.sequence++;
1148 mCurrentState.layerStack = layerStack;
1149 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001150 setTransactionFlags(eTransactionNeeded);
1151 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001152}
1153
Peiyong Linc502cb72019-03-01 15:00:23 -08001154bool Layer::setColorSpaceAgnostic(const bool agnostic) {
1155 if (mCurrentState.colorSpaceAgnostic == agnostic) {
1156 return false;
1157 }
1158 mCurrentState.sequence++;
1159 mCurrentState.colorSpaceAgnostic = agnostic;
1160 mCurrentState.modified = true;
1161 setTransactionFlags(eTransactionNeeded);
1162 return true;
1163}
1164
Robert Carr1f0a16a2016-10-24 16:27:39 -07001165uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001166 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001167 if (p == nullptr) {
1168 return getDrawingState().layerStack;
1169 }
1170 return p->getLayerStack();
1171}
1172
Marissa Wallf58c14b2018-07-24 10:50:43 -07001173void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001174 mCurrentState.barrierLayer_legacy = barrierLayer;
1175 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001176 // We don't set eTransactionNeeded, because just receiving a deferral
1177 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001178 mCurrentState.modified = true;
1179 pushPendingState();
1180 mCurrentState.barrierLayer_legacy = nullptr;
1181 mCurrentState.frameNumber_legacy = 0;
1182 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001183}
1184
Marissa Wallf58c14b2018-07-24 10:50:43 -07001185void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001186 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001187 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001188}
1189
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001190// ----------------------------------------------------------------------------
1191// pageflip handling...
1192// ----------------------------------------------------------------------------
1193
Robert Carr1f0a16a2016-10-24 16:27:39 -07001194bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001195 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001196 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001197 if (parent != nullptr && parent->isHiddenByPolicy()) {
1198 return true;
1199 }
1200 return s.flags & layer_state_t::eLayerHidden;
1201}
1202
David Sodman41fdfc92017-11-06 16:09:56 -08001203uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001204 // TODO: should we do something special if mSecure is set?
1205 if (mProtectedByApp) {
1206 // need a hardware-protected path to external video sink
1207 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001208 }
Riley Andrews03414a12014-07-01 14:22:59 -07001209 if (mPotentialCursor) {
1210 usage |= GraphicBuffer::USAGE_CURSOR;
1211 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001212 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001213 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001214}
1215
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001216void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001217 uint32_t orientation = 0;
Vishnu Nair5eb3f062019-04-08 08:21:03 -07001218 // Disable setting transform hint if the debug flag is set.
1219 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001220 // The transform hint is used to improve performance, but we can
1221 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001222 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001223 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001224 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001225 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001226 orientation = 0;
1227 }
1228 }
David Sodmaneb085e02017-10-05 18:49:04 -07001229 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001230}
1231
Mathias Agopian13127d82013-03-05 17:47:11 -08001232// ----------------------------------------------------------------------------
1233// debugging
1234// ----------------------------------------------------------------------------
1235
Marissa Wall61c58622018-07-18 10:12:20 -07001236// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001237LayerDebugInfo Layer::getLayerDebugInfo() const {
1238 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001239 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001240 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001241 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001242 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
Yiwei Zhang5434a782018-12-05 18:06:32 -08001243 info.mType = std::string(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001244 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001245 info.mVisibleRegion = visibleRegion;
1246 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001247 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001248 info.mX = ds.active_legacy.transform.tx();
1249 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001250 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001251 info.mWidth = ds.active_legacy.w;
1252 info.mHeight = ds.active_legacy.h;
1253 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001254 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001255 info.mFlags = ds.flags;
1256 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001257 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001258 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1259 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1260 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1261 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001262 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001263 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001264 if (buffer != 0) {
1265 info.mActiveBufferWidth = buffer->getWidth();
1266 info.mActiveBufferHeight = buffer->getHeight();
1267 info.mActiveBufferStride = buffer->getStride();
1268 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001269 } else {
1270 info.mActiveBufferWidth = 0;
1271 info.mActiveBufferHeight = 0;
1272 info.mActiveBufferStride = 0;
1273 info.mActiveBufferFormat = 0;
1274 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001275 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001276 info.mNumQueuedFrames = getQueuedFrameCount();
1277 info.mRefreshPending = isBufferLatched();
1278 info.mIsOpaque = isOpaque(ds);
1279 info.mContentDirty = contentDirty;
1280 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001281}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001282
Yiwei Zhang5434a782018-12-05 18:06:32 -08001283void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001284 result.append("-------------------------------");
1285 result.append("-------------------------------");
1286 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001287 result.append(" Layer name\n");
1288 result.append(" Z | ");
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001289 result.append(" Window Type | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001290 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001291 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001292 result.append(" Disp Frame (LTRB) | ");
1293 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001294 result.append("-------------------------------");
1295 result.append("-------------------------------");
1296 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001297}
1298
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001299void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1300 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1301 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001302 return;
1303 }
1304
Yiwei Zhang5434a782018-12-05 18:06:32 -08001305 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001306 if (mName.length() > 77) {
1307 std::string shortened;
1308 shortened.append(mName.string(), 36);
1309 shortened.append("[...]");
1310 shortened.append(mName.string() + (mName.length() - 36), 36);
Yiwei Zhang5434a782018-12-05 18:06:32 -08001311 name = shortened;
Dan Stozae22aec72016-08-01 13:20:59 -07001312 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001313 name = std::string(mName.string(), mName.size());
Dan Stozae22aec72016-08-01 13:20:59 -07001314 }
1315
Yiwei Zhang5434a782018-12-05 18:06:32 -08001316 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001317
Alec Mourib416efd2018-09-06 21:01:59 +00001318 const State& layerState(getDrawingState());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001319 const auto& compositionState = outputLayer->getState();
1320
Chia-I Wu1e043612018-03-01 09:45:09 -08001321 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001322 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001323 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001324 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001325 }
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001326 StringAppendF(&result, " %10d | ", mWindowType);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001327 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
1328 StringAppendF(&result, "%10s | ",
1329 toString(getCompositionLayer() ? compositionState.bufferTransform
1330 : static_cast<Hwc2::Transform>(0))
1331 .c_str());
1332 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001333 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001334 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001335 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1336 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001337
Yichi Chen6ca35192018-05-29 12:20:43 +08001338 result.append("- - - - - - - - - - - - - - - -");
1339 result.append("- - - - - - - - - - - - - - - -");
1340 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001341}
Dan Stozae22aec72016-08-01 13:20:59 -07001342
Yiwei Zhang5434a782018-12-05 18:06:32 -08001343void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001344 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001345}
1346
Svetoslavd85084b2014-03-20 10:28:31 -07001347void Layer::clearFrameStats() {
1348 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001349}
1350
Jamie Gennis6547ff42013-07-16 20:12:42 -07001351void Layer::logFrameStats() {
1352 mFrameTracker.logAndResetStats(mName);
1353}
1354
Svetoslavd85084b2014-03-20 10:28:31 -07001355void Layer::getFrameStats(FrameStats* outStats) const {
1356 mFrameTracker.getStats(outStats);
1357}
1358
Yiwei Zhang5434a782018-12-05 18:06:32 -08001359void Layer::dumpFrameEvents(std::string& result) {
1360 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001361 Mutex::Autolock lock(mFrameEventHistoryMutex);
1362 mFrameEventHistory.checkFencesForCompletion();
1363 mFrameEventHistory.dump(result);
1364}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001365
Brian Anderson5ea5e592016-12-01 16:54:33 -08001366void Layer::onDisconnect() {
1367 Mutex::Autolock lock(mFrameEventHistoryMutex);
1368 mFrameEventHistory.onDisconnect();
Yiwei Zhangaf8ee942018-11-22 00:15:23 -08001369 mFlinger->mTimeStats->onDestroy(getSequence());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001370}
1371
Brian Anderson3890c392016-07-25 12:48:08 -07001372void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001373 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001374 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001375 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1376 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001377 }
1378
Brian Andersond6927fb2016-07-23 23:37:30 -07001379 Mutex::Autolock lock(mFrameEventHistoryMutex);
1380 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001381 // If there are any unsignaled fences in the aquire timeline at this
1382 // point, the previously queued frame hasn't been latched yet. Go ahead
1383 // and try to get the signal time here so the syscall is taken out of
1384 // the main thread's critical path.
1385 mAcquireTimeline.updateSignalTimes();
1386 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001387 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001388 mFrameEventHistory.addQueue(*newTimestamps);
1389 }
1390
Brian Anderson3890c392016-07-25 12:48:08 -07001391 if (outDelta) {
1392 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001393 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001394}
Dan Stozae77c7662016-05-13 11:37:28 -07001395
Chia-I Wu98f1c102017-05-30 14:54:08 -07001396size_t Layer::getChildrenCount() const {
1397 size_t count = 0;
1398 for (const sp<Layer>& child : mCurrentChildren) {
1399 count += 1 + child->getChildrenCount();
1400 }
1401 return count;
1402}
1403
Robert Carr1f0a16a2016-10-24 16:27:39 -07001404void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001405 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001406 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001407
Robert Carr1f0a16a2016-10-24 16:27:39 -07001408 mCurrentChildren.add(layer);
1409 layer->setParent(this);
1410}
1411
1412ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001413 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001414 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001415
Robert Carr1323c952019-01-28 18:13:27 -08001416 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001417 return mCurrentChildren.remove(layer);
1418}
1419
Robert Carr1db73f62016-12-21 12:58:51 -08001420bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1421 sp<Handle> handle = nullptr;
1422 sp<Layer> newParent = nullptr;
1423 if (newParentHandle == nullptr) {
1424 return false;
1425 }
1426 handle = static_cast<Handle*>(newParentHandle.get());
1427 newParent = handle->owner.promote();
1428 if (newParent == nullptr) {
1429 ALOGE("Unable to promote Layer handle");
1430 return false;
1431 }
1432
chaviw5aedec92018-10-22 10:40:38 -07001433 if (attachChildren()) {
1434 setTransactionFlags(eTransactionNeeded);
1435 }
Robert Carr1db73f62016-12-21 12:58:51 -08001436 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001437 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001438 }
1439 mCurrentChildren.clear();
1440
1441 return true;
1442}
1443
Robert Carr15eae092018-03-23 13:43:53 -07001444void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001445 for (const sp<Layer>& child : mDrawingChildren) {
1446 child->mDrawingParent = newParent;
Vishnu Nairc652ff82019-03-15 12:48:54 -07001447 child->computeBounds(newParent->mBounds,
1448 newParent->getTransformWithScale(
1449 newParent->getBufferScaleTransform()));
Robert Carr578038f2018-03-09 12:25:24 -08001450 }
1451}
1452
chaviwf1961f72017-09-18 16:41:07 -07001453bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001454 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001455
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001456 // While layers are detached, we allow most operations
1457 // and simply halt performing the actual transaction. However
1458 // for reparent != null we would enter the mRemovedFromCurrentState
1459 // state, regardless of whether doTransaction was called, and
1460 // so we need to prevent the update here.
1461 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001462 return false;
1463 }
1464
Robert Carr54cf5b12019-01-25 14:02:28 -08001465 sp<Layer> newParent;
1466 if (newParentHandle != nullptr) {
1467 auto handle = static_cast<Handle*>(newParentHandle.get());
1468 newParent = handle->owner.promote();
1469 if (newParent == nullptr) {
1470 ALOGE("Unable to promote Layer handle");
1471 return false;
1472 }
1473 if (newParent == this) {
1474 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1475 return false;
1476 }
1477 }
1478
chaviwf1961f72017-09-18 16:41:07 -07001479 sp<Layer> parent = getParent();
1480 if (parent != nullptr) {
1481 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001482 }
1483
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001484 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001485 newParent->addChild(this);
1486 if (!newParent->isRemovedFromCurrentState()) {
1487 addToCurrentState();
1488 } else {
1489 onRemovedFromCurrentState();
1490 }
1491
1492 if (mLayerDetached) {
1493 mLayerDetached = false;
1494 callSetTransactionFlags = true;
1495 }
1496 } else {
1497 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001498 }
1499
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001500 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001501 setTransactionFlags(eTransactionNeeded);
1502 }
chaviw06178942017-07-27 10:25:59 -07001503 return true;
1504}
1505
Robert Carr9524cb32017-02-13 11:32:32 -08001506bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001507 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001508 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001509 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001510 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001511 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001512 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001513 }
Robert Carr7f619b22017-11-06 12:56:35 -08001514 }
Robert Carr9524cb32017-02-13 11:32:32 -08001515
1516 return true;
1517}
1518
chaviw5aedec92018-10-22 10:40:38 -07001519bool Layer::attachChildren() {
1520 bool changed = false;
1521 for (const sp<Layer>& child : mCurrentChildren) {
1522 sp<Client> parentClient = mClientRef.promote();
1523 sp<Client> client(child->mClientRef.promote());
1524 if (client != nullptr && parentClient != client) {
1525 if (child->mLayerDetached) {
1526 child->mLayerDetached = false;
1527 changed = true;
1528 }
1529 changed |= child->attachChildren();
1530 }
1531 }
1532
1533 return changed;
1534}
1535
Peiyong Lind3788632018-09-18 16:01:31 -07001536bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001537 static const mat4 identityMatrix = mat4();
1538
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001539 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001540 return false;
1541 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001542 ++mCurrentState.sequence;
1543 mCurrentState.colorTransform = matrix;
1544 mCurrentState.hasColorTransform = matrix != identityMatrix;
1545 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001546 setTransactionFlags(eTransactionNeeded);
1547 return true;
1548}
1549
chaviwf66724d2018-11-28 16:35:21 -08001550mat4 Layer::getColorTransform() const {
1551 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1552 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1553 colorTransform = parent->getColorTransform() * colorTransform;
1554 }
1555 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001556}
1557
1558bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001559 bool hasColorTransform = getDrawingState().hasColorTransform;
1560 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1561 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1562 }
1563 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001564}
1565
Chia-I Wu11481472018-05-04 10:43:19 -07001566bool Layer::isLegacyDataSpace() const {
1567 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001568 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001569 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001570}
1571
Robert Carr1f0a16a2016-10-24 16:27:39 -07001572void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001573 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001574}
1575
Robert Carr1f0a16a2016-10-24 16:27:39 -07001576int32_t Layer::getZ() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001577 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001578}
1579
Robert Carr29abff82017-12-04 13:51:20 -08001580bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1581 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001582 const State& state = useDrawing ? mDrawingState : mCurrentState;
Robert Carr29abff82017-12-04 13:51:20 -08001583 return state.zOrderRelativeOf != nullptr;
1584}
1585
David Sodman41fdfc92017-11-06 16:09:56 -08001586__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001587 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001588 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1589 "makeTraversalList received invalid stateSet");
1590 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1591 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001592 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001593
Robert Carr29abff82017-12-04 13:51:20 -08001594 if (state.zOrderRelatives.size() == 0) {
1595 *outSkipRelativeZUsers = true;
1596 return children;
1597 }
1598
chaviwfd462612018-05-31 16:11:27 -07001599 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001600 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001601 sp<Layer> strongRelative = weakRelative.promote();
1602 if (strongRelative != nullptr) {
1603 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001604 }
1605 }
1606
Dan Stoza412903f2017-04-27 13:42:17 -07001607 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001608 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
Robert Carr503c7042017-09-27 15:06:08 -07001609 if (childState.zOrderRelativeOf != nullptr) {
1610 continue;
1611 }
Robert Carrdb66e622017-04-10 16:55:57 -07001612 traverse.add(child);
1613 }
1614
1615 return traverse;
1616}
1617
Robert Carr1f0a16a2016-10-24 16:27:39 -07001618/**
Robert Carrdb66e622017-04-10 16:55:57 -07001619 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001620 */
Dan Stoza412903f2017-04-27 13:42:17 -07001621void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001622 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1623 // produce a new list for traversing, including our relatives, and not including our children
1624 // who are relatives of another surface. In the case that there are no relative Z,
1625 // makeTraversalList returns our children directly to avoid significant overhead.
1626 // However in this case we need to take the responsibility for filtering children which
1627 // are relatives of another surface here.
1628 bool skipRelativeZUsers = false;
1629 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001630
Robert Carr1f0a16a2016-10-24 16:27:39 -07001631 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001632 for (; i < list.size(); i++) {
1633 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001634 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1635 continue;
1636 }
1637
Robert Carrdb66e622017-04-10 16:55:57 -07001638 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001639 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001640 }
Dan Stoza412903f2017-04-27 13:42:17 -07001641 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001642 }
Robert Carr29abff82017-12-04 13:51:20 -08001643
Dan Stoza412903f2017-04-27 13:42:17 -07001644 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001645 for (; i < list.size(); i++) {
1646 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001647
Robert Carr29abff82017-12-04 13:51:20 -08001648 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1649 continue;
1650 }
Dan Stoza412903f2017-04-27 13:42:17 -07001651 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001652 }
1653}
1654
1655/**
Robert Carrdb66e622017-04-10 16:55:57 -07001656 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001657 */
Dan Stoza412903f2017-04-27 13:42:17 -07001658void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1659 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001660 // See traverseInZOrder for documentation.
1661 bool skipRelativeZUsers = false;
1662 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001663
Robert Carr1f0a16a2016-10-24 16:27:39 -07001664 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001665 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001666 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001667
1668 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1669 continue;
1670 }
1671
Robert Carrdb66e622017-04-10 16:55:57 -07001672 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001673 break;
1674 }
Dan Stoza412903f2017-04-27 13:42:17 -07001675 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001676 }
Dan Stoza412903f2017-04-27 13:42:17 -07001677 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001678 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001679 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001680
1681 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1682 continue;
1683 }
1684
Dan Stoza412903f2017-04-27 13:42:17 -07001685 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001686 }
1687}
1688
chaviw4b129c22018-04-09 16:19:43 -07001689LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1690 const std::vector<Layer*>& layersInTree) {
1691 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1692 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001693 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1694 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001695 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001696
chaviwfd462612018-05-31 16:11:27 -07001697 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001698 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1699 sp<Layer> strongRelative = weakRelative.promote();
1700 // Only add relative layers that are also descendents of the top most parent of the tree.
1701 // If a relative layer is not a descendent, then it should be ignored.
1702 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1703 traverse.add(strongRelative);
1704 }
1705 }
1706
1707 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001708 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001709 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1710 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1711 // the child here since it won't be added later as a relative.
1712 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1713 childState.zOrderRelativeOf.promote().get())) {
1714 continue;
1715 }
1716 traverse.add(child);
1717 }
1718
1719 return traverse;
1720}
1721
1722void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1723 LayerVector::StateSet stateSet,
1724 const LayerVector::Visitor& visitor) {
1725 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001726
1727 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001728 for (; i < list.size(); i++) {
1729 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001730 if (relative->getZ() >= 0) {
1731 break;
1732 }
chaviw4b129c22018-04-09 16:19:43 -07001733 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001734 }
chaviw4b129c22018-04-09 16:19:43 -07001735
chaviwa76b2712017-09-20 12:02:26 -07001736 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001737 for (; i < list.size(); i++) {
1738 const auto& relative = list[i];
1739 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001740 }
1741}
1742
chaviw4b129c22018-04-09 16:19:43 -07001743std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1744 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1745 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1746
1747 std::vector<Layer*> layersInTree = {this};
1748 for (size_t i = 0; i < children.size(); i++) {
1749 const auto& child = children[i];
1750 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1751 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1752 }
1753
1754 return layersInTree;
1755}
1756
1757void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1758 const LayerVector::Visitor& visitor) {
1759 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1760 std::sort(layersInTree.begin(), layersInTree.end());
1761 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1762}
1763
Peiyong Linefefaac2018-08-17 12:27:51 -07001764ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001765 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001766}
1767
chaviw13fdc492017-06-27 12:40:18 -07001768half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001769 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001770
chaviw13fdc492017-06-27 12:40:18 -07001771 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1772 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001773}
Robert Carr6452f122017-03-21 10:41:29 -07001774
chaviw13fdc492017-06-27 12:40:18 -07001775half4 Layer::getColor() const {
1776 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001777 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001778}
Robert Carr6452f122017-03-21 10:41:29 -07001779
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001780Layer::RoundedCornerState Layer::getRoundedCornerState() const {
1781 const auto& p = mDrawingParent.promote();
1782 if (p != nullptr) {
Peiyong Lin27016a92019-03-29 17:36:08 +00001783 RoundedCornerState parentState = p->getRoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001784 if (parentState.radius > 0) {
1785 ui::Transform t = getActiveTransform(getDrawingState());
1786 t = t.inverse();
1787 parentState.cropRect = t.transform(parentState.cropRect);
1788 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1789 // but a transform matrix can define horizontal and vertical scales.
1790 // Let's take the average between both of them and pass into the shader, practically we
1791 // never do this type of transformation on windows anyway.
1792 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
1793 return parentState;
1794 }
1795 }
1796 const float radius = getDrawingState().cornerRadius;
Peiyong Linb9ff23e2019-04-08 11:04:59 -07001797 return radius > 0 && getCrop(getDrawingState()).isValid()
1798 ? RoundedCornerState(getCrop(getDrawingState()).toFloatRect(), radius)
1799 : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001800}
1801
Robert Carr1f0a16a2016-10-24 16:27:39 -07001802void Layer::commitChildList() {
1803 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1804 const auto& child = mCurrentChildren[i];
1805 child->commitChildList();
1806 }
1807 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001808 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001809}
1810
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001811static wp<Layer> extractLayerFromBinder(const wp<IBinder>& weakBinderHandle) {
1812 if (weakBinderHandle == nullptr) {
1813 return nullptr;
1814 }
1815 sp<IBinder> binderHandle = weakBinderHandle.promote();
1816 if (binderHandle == nullptr) {
1817 return nullptr;
1818 }
1819 sp<Layer::Handle> handle = static_cast<Layer::Handle*>(binderHandle.get());
1820 if (handle == nullptr) {
1821 return nullptr;
1822 }
1823 return handle->owner;
1824}
1825
Robert Carr720e5062018-07-30 17:45:14 -07001826void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001827 mCurrentState.inputInfo = info;
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001828 mCurrentState.touchableRegionCrop = extractLayerFromBinder(info.touchableRegionCropHandle);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001829 mCurrentState.modified = true;
1830 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07001831 setTransactionFlags(eTransactionNeeded);
1832}
1833
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001834void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet,
1835 uint32_t traceFlags) {
chaviw1d044282017-09-27 12:19:28 -07001836 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1837 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001838 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07001839
Peiyong Linefefaac2018-08-17 12:27:51 -07001840 ui::Transform requestedTransform = state.active_legacy.transform;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001841 ui::Transform transform = getTransform();
chaviw1d044282017-09-27 12:19:28 -07001842
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001843 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
1844 layerInfo->set_id(sequence);
1845 layerInfo->set_name(getName().c_str());
1846 layerInfo->set_type(String8(getTypeId()));
chaviw1d044282017-09-27 12:19:28 -07001847
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001848 for (const auto& child : children) {
1849 layerInfo->add_children(child->sequence);
chaviw1d044282017-09-27 12:19:28 -07001850 }
chaviw1d044282017-09-27 12:19:28 -07001851
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001852 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1853 sp<Layer> strongRelative = weakRelative.promote();
1854 if (strongRelative != nullptr) {
1855 layerInfo->add_relatives(strongRelative->sequence);
1856 }
chaviwadc40c22018-07-10 16:57:27 -07001857 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001858
1859 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
1860 [&]() { return layerInfo->mutable_transparent_region(); });
1861 LayerProtoHelper::writeToProto(visibleRegion,
1862 [&]() { return layerInfo->mutable_visible_region(); });
1863 LayerProtoHelper::writeToProto(surfaceDamageRegion,
1864 [&]() { return layerInfo->mutable_damage_region(); });
1865
1866 layerInfo->set_layer_stack(getLayerStack());
1867 layerInfo->set_z(state.z);
1868
1869 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
1870 [&]() { return layerInfo->mutable_position(); });
1871
1872 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(),
1873 [&]() {
1874 return layerInfo->mutable_requested_position();
1875 });
1876
1877 LayerProtoHelper::writeSizeToProto(state.active_legacy.w, state.active_legacy.h,
1878 [&]() { return layerInfo->mutable_size(); });
1879
1880 LayerProtoHelper::writeToProto(state.crop_legacy,
1881 [&]() { return layerInfo->mutable_crop(); });
1882 layerInfo->set_corner_radius(getRoundedCornerState().radius);
1883
1884 layerInfo->set_is_opaque(isOpaque(state));
1885 layerInfo->set_invalidate(contentDirty);
Peiyong Lin51598552019-04-17 14:09:22 -07001886 layerInfo->set_is_protected(isProtected());
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001887
1888 // XXX (b/79210409) mCurrentDataSpace is not protected
1889 layerInfo->set_dataspace(
1890 dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
1891
1892 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1893 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
1894 LayerProtoHelper::writeToProto(state.color,
1895 [&]() { return layerInfo->mutable_requested_color(); });
1896 layerInfo->set_flags(state.flags);
1897
1898 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1899 LayerProtoHelper::writeToProto(requestedTransform,
1900 layerInfo->mutable_requested_transform());
1901
1902 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
1903 if (parent != nullptr) {
1904 layerInfo->set_parent(parent->sequence);
1905 } else {
1906 layerInfo->set_parent(-1);
1907 }
1908
1909 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1910 if (zOrderRelativeOf != nullptr) {
1911 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1912 } else {
1913 layerInfo->set_z_order_relative_of(-1);
1914 }
1915
1916 auto buffer = mActiveBuffer;
1917 if (buffer != nullptr) {
1918 LayerProtoHelper::writeToProto(buffer,
1919 [&]() { return layerInfo->mutable_active_buffer(); });
1920 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
1921 layerInfo->mutable_buffer_transform());
1922 }
1923
1924 layerInfo->set_queued_frames(getQueuedFrameCount());
1925 layerInfo->set_refresh_pending(isBufferLatched());
1926 layerInfo->set_curr_frame(mCurrentFrameNumber);
1927 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
1928
1929 for (const auto& pendingState : mPendingStates) {
1930 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
1931 if (barrierLayer != nullptr) {
1932 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
1933 barrierLayerProto->set_id(barrierLayer->sequence);
1934 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
1935 }
1936 }
1937 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
chaviwadc40c22018-07-10 16:57:27 -07001938 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001939
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001940 if (traceFlags & SurfaceTracing::TRACE_INPUT) {
1941 LayerProtoHelper::writeToProto(state.inputInfo, state.touchableRegionCrop,
1942 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001943 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001944
1945 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
1946 auto protoMap = layerInfo->mutable_metadata();
1947 for (const auto& entry : state.metadata.mMap) {
1948 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
1949 }
1950 LayerProtoHelper::writeToProto(mEffectiveTransform,
1951 layerInfo->mutable_effective_transform());
1952 LayerProtoHelper::writeToProto(mSourceBounds,
1953 [&]() { return layerInfo->mutable_source_bounds(); });
1954 LayerProtoHelper::writeToProto(mScreenBounds,
1955 [&]() { return layerInfo->mutable_screen_bounds(); });
1956 }
chaviw1d044282017-09-27 12:19:28 -07001957}
1958
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001959void Layer::writeToProto(LayerProto* layerInfo, const sp<DisplayDevice>& displayDevice,
1960 uint32_t traceFlags) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001961 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1962 if (!outputLayer) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07001963 return;
1964 }
1965
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001966 writeToProto(layerInfo, LayerVector::StateSet::Drawing, traceFlags);
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001967
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001968 const auto& compositionState = outputLayer->getState();
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001969
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001970 const Rect& frame = compositionState.displayFrame;
Nataniel Borges797b0e42019-02-15 14:11:58 -08001971 LayerProtoHelper::writeToProto(frame, [&]() { return layerInfo->mutable_hwc_frame(); });
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001972
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001973 const FloatRect& crop = compositionState.sourceCrop;
Nataniel Borges797b0e42019-02-15 14:11:58 -08001974 LayerProtoHelper::writeToProto(crop, [&]() { return layerInfo->mutable_hwc_crop(); });
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001975
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001976 const int32_t transform =
1977 getCompositionLayer() ? static_cast<int32_t>(compositionState.bufferTransform) : 0;
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001978 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08001979
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001980 const int32_t compositionType =
1981 static_cast<int32_t>(compositionState.hwc ? (*compositionState.hwc).hwcCompositionType
1982 : Hwc2::IComposerClient::Composition::CLIENT);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08001983 layerInfo->set_hwc_composition_type(compositionType);
1984
1985 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
1986 static_cast<BufferLayer*>(this)->isProtected()) {
1987 layerInfo->set_is_protected(true);
1988 } else {
1989 layerInfo->set_is_protected(false);
1990 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001991}
1992
Robert Carr2e102c92018-10-23 12:11:15 -07001993bool Layer::isRemovedFromCurrentState() const {
1994 return mRemovedFromCurrentState;
1995}
1996
Arthur Hungd20b2702019-01-14 18:16:16 +08001997InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001998 InputWindowInfo info = mDrawingState.inputInfo;
Robert Carr720e5062018-07-30 17:45:14 -07001999
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002000 if (info.displayId == ADISPLAY_ID_NONE) {
2001 info.displayId = mDrawingState.layerStack;
2002 }
2003
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002004 ui::Transform t = getTransform();
2005 const float xScale = t.sx();
2006 const float yScale = t.sy();
2007 if (xScale != 1.0f || yScale != 1.0f) {
2008 info.windowXScale *= 1.0f / xScale;
2009 info.windowYScale *= 1.0f / yScale;
2010 info.touchableRegion.scaleSelf(xScale, yScale);
Riddle Hsu39d4aa52018-11-30 20:46:53 +08002011 }
Robert Carre07e1032018-11-26 12:55:53 -08002012
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002013 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002014 // If this is a portal window, set the touchableRegion to the layerBounds.
2015 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
2016 ? getBufferSize(getDrawingState())
2017 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002018 if (!layerBounds.isValid()) {
2019 layerBounds = getCroppedBufferSize(getDrawingState());
2020 }
Vishnu Nair8033a492018-12-05 07:27:23 -08002021 layerBounds = t.transform(layerBounds);
2022 layerBounds.inset(info.surfaceInset, info.surfaceInset, info.surfaceInset, info.surfaceInset);
2023
Arthur Hungd20b2702019-01-14 18:16:16 +08002024 // Input coordinate should match the layer bounds.
2025 info.frameLeft = layerBounds.left;
2026 info.frameTop = layerBounds.top;
2027 info.frameRight = layerBounds.right;
2028 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08002029
2030 // Position the touchable region relative to frame screen location and restrict it to frame
2031 // bounds.
2032 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08002033 info.visible = canReceiveInput();
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002034
2035 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2036 if (info.replaceTouchableRegionWithCrop) {
2037 if (cropLayer == nullptr) {
2038 info.touchableRegion = Region(Rect{mScreenBounds});
2039 } else {
2040 info.touchableRegion = Region(Rect{cropLayer->mScreenBounds});
2041 }
2042 } else if (cropLayer != nullptr) {
2043 info.touchableRegion = info.touchableRegion.intersect(Rect{cropLayer->mScreenBounds});
2044 }
2045
Robert Carr720e5062018-07-30 17:45:14 -07002046 return info;
2047}
2048
2049bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002050 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002051}
2052
Lloyd Piquefeb73d72018-12-04 17:23:44 -08002053std::shared_ptr<compositionengine::Layer> Layer::getCompositionLayer() const {
2054 return nullptr;
2055}
2056
chaviw3e727cd2019-01-31 13:41:05 -08002057bool Layer::canReceiveInput() const {
2058 return isVisible();
2059}
2060
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002061compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
2062 const sp<const DisplayDevice>& display) const {
2063 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionLayer().get());
2064}
2065
Mathias Agopian13127d82013-03-05 17:47:11 -08002066// ---------------------------------------------------------------------------
2067
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002068}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002069
2070#if defined(__gl_h_)
2071#error "don't include gl/gl.h in this file"
2072#endif
2073
2074#if defined(__gl2_h_)
2075#error "don't include gl2/gl2.h in this file"
2076#endif