blob: 60f6fe1e39c6df0b6bcc6789d62c9b9a3c43053b [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);
Robert Carr2e102c92018-10-23 12:11:15 -0700133 mFlinger->onLayerDestroyed();
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;
389
Lloyd Pique0b785d82018-12-04 17:25:27 -0800390 renderengine::Mesh::VertexArray<vec2> cropCoords(
391 getCompositionLayer()->editState().reMesh.getCropCoordArray<vec2>());
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700392 cropCoords[0] = vec2(win.left, win.top);
393 cropCoords[1] = vec2(win.left, win.top + win.getHeight());
394 cropCoords[2] = vec2(win.right, win.top + win.getHeight());
395 cropCoords[3] = vec2(win.right, win.top);
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700396}
397
Lloyd Piquea83776c2019-01-29 18:42:32 -0800398void Layer::latchGeometry(compositionengine::LayerFECompositionState& compositionState) const {
399 const auto& drawingState{getDrawingState()};
400 auto alpha = static_cast<float>(getAlpha());
David Revemanecf0fa52017-03-03 11:32:44 -0500401 auto blendMode = HWC2::BlendMode::None;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800402 if (!isOpaque(drawingState) || alpha != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800403 blendMode =
404 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800405 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800406
Lloyd Piquea83776c2019-01-29 18:42:32 -0800407 int type = drawingState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
408 int appId = drawingState.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700409 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400410 if (parent.get()) {
411 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800412 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
413 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
414 if (parentType >= 0 || parentAppId >= 0) {
415 type = parentType;
416 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700417 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400418 }
419
Lloyd Piquea83776c2019-01-29 18:42:32 -0800420 compositionState.geomLayerTransform = getTransform();
421 compositionState.geomInverseLayerTransform = compositionState.geomLayerTransform.inverse();
422 compositionState.geomBufferSize = getBufferSize(drawingState);
423 compositionState.geomContentCrop = getContentCrop();
424 compositionState.geomCrop = getCrop(drawingState);
425 compositionState.geomBufferTransform = mCurrentTransform;
426 compositionState.geomBufferUsesDisplayInverseTransform = getTransformToDisplayInverse();
427 compositionState.geomActiveTransparentRegion = getActiveTransparentRegion(drawingState);
428 compositionState.geomLayerBounds = mBounds;
429 compositionState.geomUsesSourceCrop = usesSourceCrop();
430 compositionState.isSecure = isSecure();
David Sodman15094112018-10-11 09:39:37 -0700431
Lloyd Piquea83776c2019-01-29 18:42:32 -0800432 compositionState.blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
433 compositionState.alpha = alpha;
434 compositionState.type = type;
435 compositionState.appId = appId;
436}
David Sodmanba340492018-08-05 21:51:33 -0700437
Lloyd Piquea83776c2019-01-29 18:42:32 -0800438void Layer::latchCompositionState(compositionengine::LayerFECompositionState& compositionState,
439 bool includeGeometry) const {
440 if (includeGeometry) {
441 latchGeometry(compositionState);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700442 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800443}
Mathias Agopian29a367b2011-07-12 14:51:45 -0700444
Lloyd Piquea83776c2019-01-29 18:42:32 -0800445const char* Layer::getDebugName() const {
446 return mName.string();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800447}
448
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800449void Layer::forceClientComposition(const sp<DisplayDevice>& display) {
450 const auto outputLayer = findOutputLayerForDisplay(display);
451 LOG_FATAL_IF(!outputLayer);
452 outputLayer->editState().forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800453}
Dan Stozaee44edd2015-03-23 15:50:23 -0700454
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800455bool Layer::getForceClientComposition(const sp<DisplayDevice>& display) {
456 const auto outputLayer = findOutputLayerForDisplay(display);
457 LOG_FATAL_IF(!outputLayer);
458 return outputLayer->getState().forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800459}
460
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700461void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800462 const auto outputLayer = findOutputLayerForDisplay(display);
463 LOG_FATAL_IF(!outputLayer);
464
465 if (!outputLayer->getState().hwc ||
466 (*outputLayer->getState().hwc).hwcCompositionType !=
467 Hwc2::IComposerClient::Composition::CURSOR) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800468 return;
469 }
470
471 // This gives us only the "orientation" component of the transform
Vishnu Nair33a6eee2019-02-06 13:48:06 -0800472 const State& s(getDrawingState());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800473
474 // Apply the layer's transform, followed by the display's global transform
475 // Here we're guaranteed that the layer's transform preserves rects
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800476 Rect win = getCroppedBufferSize(s);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800477 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700478 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800479 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700480 frame.intersect(display->getViewport(), &frame);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700481 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800482 auto position = displayTransform.transform(frame);
483
Dominik Laskowski7e045462018-05-30 13:02:02 -0700484 auto error =
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800485 (*outputLayer->getState().hwc).hwcLayer->setCursorPosition(position.left, position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800486 ALOGE_IF(error != HWC2::Error::None,
487 "[%s] Failed to set cursor position "
488 "to (%d, %d): %s (%d)",
489 mName.string(), position.left, position.top, to_string(error).c_str(),
490 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800491}
Riley Andrews03414a12014-07-01 14:22:59 -0700492
Mathias Agopian13127d82013-03-05 17:47:11 -0800493// ---------------------------------------------------------------------------
494// drawing...
495// ---------------------------------------------------------------------------
496
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000497bool Layer::prepareClientLayer(const RenderArea& renderArea, const Region& clip,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800498 Region& clearRegion, const bool supportProtectedContent,
499 renderengine::LayerSettings& layer) {
500 return prepareClientLayer(renderArea, clip, false, clearRegion, supportProtectedContent, layer);
Mathias Agopian13127d82013-03-05 17:47:11 -0800501}
502
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000503bool Layer::prepareClientLayer(const RenderArea& renderArea, bool useIdentityTransform,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800504 Region& clearRegion, const bool supportProtectedContent,
505 renderengine::LayerSettings& layer) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000506 return prepareClientLayer(renderArea, Region(renderArea.getBounds()), useIdentityTransform,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800507 clearRegion, supportProtectedContent, layer);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000508}
509
510bool Layer::prepareClientLayer(const RenderArea& /*renderArea*/, const Region& /*clip*/,
511 bool useIdentityTransform, Region& /*clearRegion*/,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800512 const bool /*supportProtectedContent*/,
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000513 renderengine::LayerSettings& layer) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800514 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000515 half alpha = getAlpha();
516 layer.geometry.boundaries = bounds;
517 if (useIdentityTransform) {
518 layer.geometry.positionTransform = mat4();
519 } else {
520 const ui::Transform transform = getTransform();
521 mat4 m;
522 m[0][0] = transform[0][0];
523 m[0][1] = transform[0][1];
524 m[0][3] = transform[0][2];
525 m[1][0] = transform[1][0];
526 m[1][1] = transform[1][1];
527 m[1][3] = transform[1][2];
528 m[3][0] = transform[2][0];
529 m[3][1] = transform[2][1];
530 m[3][3] = transform[2][2];
531 layer.geometry.positionTransform = m;
532 }
533
534 if (hasColorTransform()) {
535 layer.colorTransform = getColorTransform();
536 }
537
538 const auto roundedCornerState = getRoundedCornerState();
539 layer.geometry.roundedCornersRadius = roundedCornerState.radius;
540 layer.geometry.roundedCornersCrop = roundedCornerState.cropRect;
541
542 layer.alpha = alpha;
543 layer.sourceDataspace = mCurrentDataSpace;
544 return true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800545}
546
David Sodman41fdfc92017-11-06 16:09:56 -0800547void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
548 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800549 auto& engine(mFlinger->getRenderEngine());
Lloyd Pique0b785d82018-12-04 17:25:27 -0800550 computeGeometry(renderArea, getCompositionLayer()->editState().reMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700551 engine.setupFillWithColor(red, green, blue, alpha);
Lloyd Pique0b785d82018-12-04 17:25:27 -0800552 engine.drawMesh(getCompositionLayer()->getState().reMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800553}
554
David Sodmanc1498e62018-09-12 14:36:26 -0700555void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
556 clearWithOpenGL(renderArea, 0, 0, 0, 0);
557}
558
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800559void Layer::setCompositionType(const sp<const DisplayDevice>& display,
560 Hwc2::IComposerClient::Composition type) {
561 const auto outputLayer = findOutputLayerForDisplay(display);
562 LOG_FATAL_IF(!outputLayer);
563 LOG_FATAL_IF(!outputLayer->getState().hwc);
564 auto& compositionState = outputLayer->editState();
565
566 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", ((*compositionState.hwc).hwcLayer)->getId(),
567 toString(type).c_str(), 1);
568 if ((*compositionState.hwc).hwcCompositionType != type) {
David Sodman15094112018-10-11 09:39:37 -0700569 ALOGV(" actually setting");
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800570 (*compositionState.hwc).hwcCompositionType = type;
571
572 auto error = (*compositionState.hwc)
573 .hwcLayer->setCompositionType(static_cast<HWC2::Composition>(type));
574 ALOGE_IF(error != HWC2::Error::None,
575 "[%s] Failed to set "
576 "composition type %s: %s (%d)",
577 mName.string(), toString(type).c_str(), to_string(error).c_str(),
578 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800579 }
580}
581
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800582Hwc2::IComposerClient::Composition Layer::getCompositionType(
583 const sp<const DisplayDevice>& display) const {
584 const auto outputLayer = findOutputLayerForDisplay(display);
585 LOG_FATAL_IF(!outputLayer);
586 return outputLayer->getState().hwc ? (*outputLayer->getState().hwc).hwcCompositionType
587 : Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800588}
589
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800590bool Layer::getClearClientTarget(const sp<const DisplayDevice>& display) const {
591 const auto outputLayer = findOutputLayerForDisplay(display);
592 LOG_FATAL_IF(!outputLayer);
593 return outputLayer->getState().clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800594}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800595
Dan Stozacac35382016-01-27 12:21:06 -0800596bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
597 if (point->getFrameNumber() <= mCurrentFrameNumber) {
598 // Don't bother with a SyncPoint, since we've already latched the
599 // relevant frame
600 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700601 }
Robert Carr2e102c92018-10-23 12:11:15 -0700602 if (isRemovedFromCurrentState()) {
603 return false;
604 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700605
Dan Stozacac35382016-01-27 12:21:06 -0800606 Mutex::Autolock lock(mLocalSyncPointMutex);
607 mLocalSyncPoints.push_back(point);
608 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700609}
610
Mathias Agopian13127d82013-03-05 17:47:11 -0800611// ----------------------------------------------------------------------------
612// local state
613// ----------------------------------------------------------------------------
614
Peiyong Lin833074a2018-08-28 11:53:54 -0700615void Layer::computeGeometry(const RenderArea& renderArea,
616 renderengine::Mesh& mesh,
chaviwa76b2712017-09-20 12:02:26 -0700617 bool useIdentityTransform) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700618 const ui::Transform renderAreaTransform(renderArea.getTransform());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800619 FloatRect win = getBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700620
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000621 vec2 lt = vec2(win.left, win.top);
622 vec2 lb = vec2(win.left, win.bottom);
623 vec2 rb = vec2(win.right, win.bottom);
624 vec2 rt = vec2(win.right, win.top);
625
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800626 ui::Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000627 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700628 lt = layerTransform.transform(lt);
629 lb = layerTransform.transform(lb);
630 rb = layerTransform.transform(rb);
631 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000632 }
633
Peiyong Lin833074a2018-08-28 11:53:54 -0700634 renderengine::Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700635 position[0] = renderAreaTransform.transform(lt);
636 position[1] = renderAreaTransform.transform(lb);
637 position[2] = renderAreaTransform.transform(rb);
638 position[3] = renderAreaTransform.transform(rt);
Mathias Agopian13127d82013-03-05 17:47:11 -0800639}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800640
David Sodman41fdfc92017-11-06 16:09:56 -0800641bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800642 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700643 return (s.flags & layer_state_t::eLayerSecure);
644}
645
Mathias Agopian13127d82013-03-05 17:47:11 -0800646void Layer::setVisibleRegion(const Region& visibleRegion) {
647 // always called from main thread
648 this->visibleRegion = visibleRegion;
649}
650
651void Layer::setCoveredRegion(const Region& coveredRegion) {
652 // always called from main thread
653 this->coveredRegion = coveredRegion;
654}
655
David Sodman41fdfc92017-11-06 16:09:56 -0800656void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800657 // always called from main thread
658 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
659}
660
Robert Carre5f4f692018-01-12 13:12:28 -0800661void Layer::clearVisibilityRegions() {
662 visibleRegion.clear();
663 visibleNonTransparentRegion.clear();
664 coveredRegion.clear();
665}
666
Mathias Agopian13127d82013-03-05 17:47:11 -0800667// ----------------------------------------------------------------------------
668// transaction
669// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800670
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800671void Layer::pushPendingState() {
672 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700673 return;
674 }
675
Dan Stoza7dde5992015-05-22 09:51:44 -0700676 // If this transaction is waiting on the receipt of a frame, generate a sync
677 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700678 // We don't allow installing sync points after we are removed from the current state
679 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800680 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
681 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800682 if (barrierLayer == nullptr) {
683 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700684 // If we can't promote the layer we are intended to wait on,
685 // then it is expired or otherwise invalid. Allow this transaction
686 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800687 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800688 } else {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800689 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy);
Robert Carr0d480722017-01-10 16:42:54 -0800690 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800691 mRemoteSyncPoints.push_back(std::move(syncPoint));
692 } else {
693 // We already missed the frame we're supposed to synchronize
694 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800695 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800696 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700697 }
698
Dan Stoza7dde5992015-05-22 09:51:44 -0700699 // Wake us up to check if the frame has been received
700 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700701 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700702 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800703 mPendingStates.push_back(mCurrentState);
704 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700705}
706
Pablo Ceballos05289c22016-04-14 15:49:55 -0700707void Layer::popPendingState(State* stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800708 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700709
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800710 mPendingStates.removeAt(0);
711 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700712}
713
Pablo Ceballos05289c22016-04-14 15:49:55 -0700714bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700715 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800716 while (!mPendingStates.empty()) {
717 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700718 if (mRemoteSyncPoints.empty()) {
719 // If we don't have a sync point for this, apply it anyway. It
720 // will be visually wrong, but it should keep us from getting
721 // into too much trouble.
722 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700723 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700724 stateUpdateAvailable = true;
725 continue;
726 }
727
Marissa Wallf58c14b2018-07-24 10:50:43 -0700728 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800729 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800730 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800731
732 // Signal our end of the sync point and then dispose of it
733 mRemoteSyncPoints.front()->setTransactionApplied();
734 mRemoteSyncPoints.pop_front();
735 continue;
736 }
737
Dan Stoza7dde5992015-05-22 09:51:44 -0700738 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
739 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700740 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700741 stateUpdateAvailable = true;
742
743 // Signal our end of the sync point and then dispose of it
744 mRemoteSyncPoints.front()->setTransactionApplied();
745 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800746 } else {
747 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700748 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700749 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700750 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700751 stateUpdateAvailable = true;
752 }
753 }
754
755 // If we still have pending updates, wake SurfaceFlinger back up and point
756 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800757 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700758 setTransactionFlags(eTransactionNeeded);
759 mFlinger->setTransactionFlags(eTraversalNeeded);
760 }
761
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800762 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700763 return stateUpdateAvailable;
764}
765
Marissa Wall61c58622018-07-18 10:12:20 -0700766uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000767 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800768
Marissa Wall61c58622018-07-18 10:12:20 -0700769 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
770 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700771
David Sodmaneb085e02017-10-05 18:49:04 -0700772 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700773 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000774 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800775 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
776 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
777 " requested={ wh={%4u,%4u} }}\n"
778 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
779 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700780 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700781 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
782 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
783 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
784 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
785 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700786 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
787 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
788 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800789 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700790
Robert Carre392b552017-09-19 12:16:05 -0700791 // Don't let Layer::doTransaction update the drawing state
792 // if we have a pending resize, unless we are in fixed-size mode.
793 // the drawing state will be updated only once we receive a buffer
794 // with the correct size.
795 //
796 // In particular, we want to make sure the clip (which is part
797 // of the geometry state) is latched together with the size but is
798 // latched immediately when no resizing is involved.
799 //
800 // If a sideband stream is attached, however, we want to skip this
801 // optimization so that transactions aren't missed when a buffer
802 // never arrives
803 //
804 // In the case that we don't have a buffer we ignore other factors
805 // and avoid entering the resizePending state. At a high level the
806 // resizePending state is to avoid applying the state of the new buffer
807 // to the old buffer. However in the state where we don't have an old buffer
808 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700809 const bool resizePending =
810 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
811 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
Lloyd Pique0b785d82018-12-04 17:25:27 -0800812 (mActiveBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700813 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800814 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700815 flags |= eDontUpdateGeometryState;
816 }
817 }
818
Robert Carr7bf247e2017-05-18 14:02:49 -0700819 // Here we apply various requested geometry states, depending on our
820 // latching configuration. See Layer.h for a detailed discussion of
821 // how geometry latching is controlled.
822 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000823 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700824
825 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
826 // mode, which causes attributes which normally latch regardless of scaling mode,
827 // to be delayed. We copy the requested state to the active state making sure
828 // to respect these rules (again see Layer.h for a detailed discussion).
829 //
830 // There is an awkward asymmetry in the handling of the crop states in the position
831 // states, as can be seen below. Largely this arises from position and transform
832 // being stored in the same data structure while having different latching rules.
833 // b/38182305
834 //
Marissa Wall61c58622018-07-18 10:12:20 -0700835 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700836 // applyPendingStates in the presence of deferred transactions.
837 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -0700838 float tx = stateToCommit->active_legacy.transform.tx();
839 float ty = stateToCommit->active_legacy.transform.ty();
840 stateToCommit->active_legacy = stateToCommit->requested_legacy;
841 stateToCommit->active_legacy.transform.set(tx, ty);
842 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700843 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700844 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -0700845 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700846 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800847 }
848
Marissa Wall61c58622018-07-18 10:12:20 -0700849 return flags;
850}
851
852uint32_t Layer::doTransaction(uint32_t flags) {
853 ATRACE_CALL();
854
chaviw5aedec92018-10-22 10:40:38 -0700855 if (mLayerDetached) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800856 return flags;
857 }
858
859 if (mChildrenChanged) {
860 flags |= eVisibleRegion;
861 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -0700862 }
863
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800864 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +0000865 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -0700866 if (!applyPendingStates(&c)) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800867 return flags;
Marissa Wall61c58622018-07-18 10:12:20 -0700868 }
869
870 flags = doTransactionResize(flags, &c);
871
Alec Mourib416efd2018-09-06 21:01:59 +0000872 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700873
874 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800875 // invalidate and recompute the visible regions if needed
876 flags |= Layer::eVisibleRegion;
877 }
878
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700879 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800880 // invalidate and recompute the visible regions if needed
881 flags |= eVisibleRegion;
882 this->contentDirty = true;
883
884 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -0700885 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -0700886 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -0800887 }
888
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800889 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -0700890 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800891 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -0700892 }
893
Mathias Agopian13127d82013-03-05 17:47:11 -0800894 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -0700895 commitTransaction(c);
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800896 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -0800897 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800898}
899
Pablo Ceballos05289c22016-04-14 15:49:55 -0700900void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800901 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700902}
903
Mathias Agopian13127d82013-03-05 17:47:11 -0800904uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800905 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800906}
907
908uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800909 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -0800910}
911
Robert Carr82364e32016-05-15 11:27:47 -0700912bool Layer::setPosition(float x, float y, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800913 if (mCurrentState.requested_legacy.transform.tx() == x &&
914 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -0800915 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800916 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -0700917
918 // We update the requested and active position simultaneously because
919 // we want to apply the position portion of the transform matrix immediately,
920 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800921 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -0700922 if (immediate && !mFreezeGeometryUpdates) {
923 // Here we directly update the active state
924 // unlike other setters, because we store it within
925 // the transform, but use different latching rules.
926 // b/38182305
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800927 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -0700928 }
Robert Carr7bf247e2017-05-18 14:02:49 -0700929 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -0700930
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800931 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800932 setTransactionFlags(eTransactionNeeded);
933 return true;
934}
Robert Carr82364e32016-05-15 11:27:47 -0700935
Robert Carr1f0a16a2016-10-24 16:27:39 -0700936bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
937 ssize_t idx = mCurrentChildren.indexOf(childLayer);
938 if (idx < 0) {
939 return false;
940 }
941 if (childLayer->setLayer(z)) {
942 mCurrentChildren.removeAt(idx);
943 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800944 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700945 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800946 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700947}
948
Robert Carr503c7042017-09-27 15:06:08 -0700949bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
950 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
951 ssize_t idx = mCurrentChildren.indexOf(childLayer);
952 if (idx < 0) {
953 return false;
954 }
955 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
956 mCurrentChildren.removeAt(idx);
957 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800958 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700959 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800960 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700961}
962
Robert Carrae060832016-11-28 10:51:00 -0800963bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800964 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
965 mCurrentState.sequence++;
966 mCurrentState.z = z;
967 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700968
969 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800970 if (mCurrentState.zOrderRelativeOf != nullptr) {
971 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700972 if (strongRelative != nullptr) {
973 strongRelative->removeZOrderRelative(this);
974 }
chaviw606e5cf2019-03-01 10:12:10 -0800975 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -0700976 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800977 setTransactionFlags(eTransactionNeeded);
978 return true;
979}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700980
Robert Carrdb66e622017-04-10 16:55:57 -0700981void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800982 mCurrentState.zOrderRelatives.remove(relative);
983 mCurrentState.sequence++;
984 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700985 setTransactionFlags(eTransactionNeeded);
986}
987
988void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800989 mCurrentState.zOrderRelatives.add(relative);
990 mCurrentState.modified = true;
991 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -0700992 setTransactionFlags(eTransactionNeeded);
993}
994
chaviw606e5cf2019-03-01 10:12:10 -0800995void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
996 mCurrentState.zOrderRelativeOf = relativeOf;
997 mCurrentState.sequence++;
998 mCurrentState.modified = true;
999 setTransactionFlags(eTransactionNeeded);
1000}
1001
Robert Carr503d2bd2017-12-04 15:49:47 -08001002bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001003 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1004 if (handle == nullptr) {
1005 return false;
1006 }
1007 sp<Layer> relative = handle->owner.promote();
1008 if (relative == nullptr) {
1009 return false;
1010 }
1011
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001012 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1013 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001014 return false;
1015 }
1016
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001017 mCurrentState.sequence++;
1018 mCurrentState.modified = true;
1019 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001020
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001021 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -07001022 if (oldZOrderRelativeOf != nullptr) {
1023 oldZOrderRelativeOf->removeZOrderRelative(this);
1024 }
chaviw606e5cf2019-03-01 10:12:10 -08001025 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -07001026 relative->addZOrderRelative(this);
1027
1028 setTransactionFlags(eTransactionNeeded);
1029
1030 return true;
1031}
1032
Mathias Agopian13127d82013-03-05 17:47:11 -08001033bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001034 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001035 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001036 mCurrentState.requested_legacy.w = w;
1037 mCurrentState.requested_legacy.h = h;
1038 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001039 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001040
1041 // record the new size, from this point on, when the client request
1042 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001043 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001044 return true;
1045}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001046bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001047 if (mCurrentState.color.a == alpha) return false;
1048 mCurrentState.sequence++;
1049 mCurrentState.color.a = alpha;
1050 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001051 setTransactionFlags(eTransactionNeeded);
1052 return true;
1053}
chaviw13fdc492017-06-27 12:40:18 -07001054
Valerie Haudd0b7572019-01-29 14:59:27 -08001055bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1056 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001057 return false;
Valerie Hauaa194562019-02-05 16:21:38 -08001058 }
1059 mCurrentState.sequence++;
1060 mCurrentState.modified = true;
1061 setTransactionFlags(eTransactionNeeded);
1062
1063 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001064 // create background color layer if one does not yet exist
1065 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
1066 const String8& name = mName + "BackgroundColorLayer";
Evan Roskya1f1e152019-01-24 16:17:46 -08001067 mCurrentState.bgColorLayer = new ColorLayer(
1068 LayerCreationArgs(mFlinger.get(), nullptr, name, 0, 0, flags, LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -07001069
Valerie Haudd0b7572019-01-29 14:59:27 -08001070 // add to child list
1071 addChild(mCurrentState.bgColorLayer);
1072 mFlinger->mLayersAdded = true;
1073 // set up SF to handle added color layer
1074 if (isRemovedFromCurrentState()) {
1075 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1076 }
1077 mFlinger->setTransactionFlags(eTransactionNeeded);
1078 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1079 mCurrentState.bgColorLayer->reparent(nullptr);
1080 mCurrentState.bgColorLayer = nullptr;
1081 return true;
1082 }
1083
1084 mCurrentState.bgColorLayer->setColor(color);
1085 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1086 mCurrentState.bgColorLayer->setAlpha(alpha);
1087 mCurrentState.bgColorLayer->setDataspace(dataspace);
1088
chaviw13fdc492017-06-27 12:40:18 -07001089 return true;
1090}
1091
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001092bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001093 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001094
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001095 mCurrentState.sequence++;
1096 mCurrentState.cornerRadius = cornerRadius;
1097 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001098 setTransactionFlags(eTransactionNeeded);
1099 return true;
1100}
1101
Robert Carrd4ae7f32018-06-07 16:10:57 -07001102bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1103 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001104 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001105 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1106
1107 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1108 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1109 return false;
1110 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001111 mCurrentState.sequence++;
1112 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1113 matrix.dsdy);
1114 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001115 setTransactionFlags(eTransactionNeeded);
1116 return true;
1117}
Marissa Wall61c58622018-07-18 10:12:20 -07001118
Mathias Agopian13127d82013-03-05 17:47:11 -08001119bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001120 mCurrentState.requestedTransparentRegion_legacy = transparent;
1121 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001122 setTransactionFlags(eTransactionNeeded);
1123 return true;
1124}
1125bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001126 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1127 if (mCurrentState.flags == newFlags) return false;
1128 mCurrentState.sequence++;
1129 mCurrentState.flags = newFlags;
1130 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001131 setTransactionFlags(eTransactionNeeded);
1132 return true;
1133}
Robert Carr99e27f02016-06-16 15:18:02 -07001134
Marissa Wallf58c14b2018-07-24 10:50:43 -07001135bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001136 if (mCurrentState.requestedCrop_legacy == crop) return false;
1137 mCurrentState.sequence++;
1138 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001139 if (immediate && !mFreezeGeometryUpdates) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001140 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001141 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001142 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1143
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001144 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001145 setTransactionFlags(eTransactionNeeded);
1146 return true;
1147}
Robert Carr8d5227b2017-03-16 15:41:03 -07001148
Robert Carrc3574f72016-03-24 12:19:32 -07001149bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001150 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001151 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001152 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001153 return true;
1154}
1155
Evan Roskyef876c92019-01-25 17:46:06 -08001156bool Layer::setMetadata(const LayerMetadata& data) {
1157 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001158 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001159 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001160 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001161 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001162}
1163
Mathias Agopian13127d82013-03-05 17:47:11 -08001164bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001165 if (mCurrentState.layerStack == layerStack) return false;
1166 mCurrentState.sequence++;
1167 mCurrentState.layerStack = layerStack;
1168 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001169 setTransactionFlags(eTransactionNeeded);
1170 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001171}
1172
Peiyong Linc502cb72019-03-01 15:00:23 -08001173bool Layer::setColorSpaceAgnostic(const bool agnostic) {
1174 if (mCurrentState.colorSpaceAgnostic == agnostic) {
1175 return false;
1176 }
1177 mCurrentState.sequence++;
1178 mCurrentState.colorSpaceAgnostic = agnostic;
1179 mCurrentState.modified = true;
1180 setTransactionFlags(eTransactionNeeded);
1181 return true;
1182}
1183
Robert Carr1f0a16a2016-10-24 16:27:39 -07001184uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001185 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001186 if (p == nullptr) {
1187 return getDrawingState().layerStack;
1188 }
1189 return p->getLayerStack();
1190}
1191
Marissa Wallf58c14b2018-07-24 10:50:43 -07001192void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001193 mCurrentState.barrierLayer_legacy = barrierLayer;
1194 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001195 // We don't set eTransactionNeeded, because just receiving a deferral
1196 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001197 mCurrentState.modified = true;
1198 pushPendingState();
1199 mCurrentState.barrierLayer_legacy = nullptr;
1200 mCurrentState.frameNumber_legacy = 0;
1201 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001202}
1203
Marissa Wallf58c14b2018-07-24 10:50:43 -07001204void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001205 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001206 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001207}
1208
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001209// ----------------------------------------------------------------------------
1210// pageflip handling...
1211// ----------------------------------------------------------------------------
1212
Robert Carr1f0a16a2016-10-24 16:27:39 -07001213bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001214 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001215 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001216 if (parent != nullptr && parent->isHiddenByPolicy()) {
1217 return true;
1218 }
1219 return s.flags & layer_state_t::eLayerHidden;
1220}
1221
David Sodman41fdfc92017-11-06 16:09:56 -08001222uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001223 // TODO: should we do something special if mSecure is set?
1224 if (mProtectedByApp) {
1225 // need a hardware-protected path to external video sink
1226 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001227 }
Riley Andrews03414a12014-07-01 14:22:59 -07001228 if (mPotentialCursor) {
1229 usage |= GraphicBuffer::USAGE_CURSOR;
1230 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001231 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001232 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001233}
1234
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001235void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001236 uint32_t orientation = 0;
Vishnu Nair5eb3f062019-04-08 08:21:03 -07001237 // Disable setting transform hint if the debug flag is set.
1238 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001239 // The transform hint is used to improve performance, but we can
1240 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001241 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001242 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001243 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001244 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001245 orientation = 0;
1246 }
1247 }
David Sodmaneb085e02017-10-05 18:49:04 -07001248 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001249}
1250
Mathias Agopian13127d82013-03-05 17:47:11 -08001251// ----------------------------------------------------------------------------
1252// debugging
1253// ----------------------------------------------------------------------------
1254
Marissa Wall61c58622018-07-18 10:12:20 -07001255// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001256LayerDebugInfo Layer::getLayerDebugInfo() const {
1257 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001258 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001259 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001260 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001261 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
Yiwei Zhang5434a782018-12-05 18:06:32 -08001262 info.mType = std::string(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001263 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001264 info.mVisibleRegion = visibleRegion;
1265 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001266 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001267 info.mX = ds.active_legacy.transform.tx();
1268 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001269 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001270 info.mWidth = ds.active_legacy.w;
1271 info.mHeight = ds.active_legacy.h;
1272 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001273 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001274 info.mFlags = ds.flags;
1275 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001276 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001277 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1278 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1279 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1280 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001281 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001282 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001283 if (buffer != 0) {
1284 info.mActiveBufferWidth = buffer->getWidth();
1285 info.mActiveBufferHeight = buffer->getHeight();
1286 info.mActiveBufferStride = buffer->getStride();
1287 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001288 } else {
1289 info.mActiveBufferWidth = 0;
1290 info.mActiveBufferHeight = 0;
1291 info.mActiveBufferStride = 0;
1292 info.mActiveBufferFormat = 0;
1293 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001294 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001295 info.mNumQueuedFrames = getQueuedFrameCount();
1296 info.mRefreshPending = isBufferLatched();
1297 info.mIsOpaque = isOpaque(ds);
1298 info.mContentDirty = contentDirty;
1299 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001300}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001301
Yiwei Zhang5434a782018-12-05 18:06:32 -08001302void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001303 result.append("-------------------------------");
1304 result.append("-------------------------------");
1305 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001306 result.append(" Layer name\n");
1307 result.append(" Z | ");
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001308 result.append(" Window Type | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001309 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001310 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001311 result.append(" Disp Frame (LTRB) | ");
1312 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001313 result.append("-------------------------------");
1314 result.append("-------------------------------");
1315 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001316}
1317
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001318void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1319 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1320 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001321 return;
1322 }
1323
Yiwei Zhang5434a782018-12-05 18:06:32 -08001324 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001325 if (mName.length() > 77) {
1326 std::string shortened;
1327 shortened.append(mName.string(), 36);
1328 shortened.append("[...]");
1329 shortened.append(mName.string() + (mName.length() - 36), 36);
Yiwei Zhang5434a782018-12-05 18:06:32 -08001330 name = shortened;
Dan Stozae22aec72016-08-01 13:20:59 -07001331 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001332 name = std::string(mName.string(), mName.size());
Dan Stozae22aec72016-08-01 13:20:59 -07001333 }
1334
Yiwei Zhang5434a782018-12-05 18:06:32 -08001335 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001336
Alec Mourib416efd2018-09-06 21:01:59 +00001337 const State& layerState(getDrawingState());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001338 const auto& compositionState = outputLayer->getState();
1339
Chia-I Wu1e043612018-03-01 09:45:09 -08001340 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001341 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001342 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001343 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001344 }
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001345 StringAppendF(&result, " %10d | ", mWindowType);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001346 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
1347 StringAppendF(&result, "%10s | ",
1348 toString(getCompositionLayer() ? compositionState.bufferTransform
1349 : static_cast<Hwc2::Transform>(0))
1350 .c_str());
1351 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001352 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001353 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001354 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1355 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001356
Yichi Chen6ca35192018-05-29 12:20:43 +08001357 result.append("- - - - - - - - - - - - - - - -");
1358 result.append("- - - - - - - - - - - - - - - -");
1359 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001360}
Dan Stozae22aec72016-08-01 13:20:59 -07001361
Yiwei Zhang5434a782018-12-05 18:06:32 -08001362void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001363 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001364}
1365
Svetoslavd85084b2014-03-20 10:28:31 -07001366void Layer::clearFrameStats() {
1367 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001368}
1369
Jamie Gennis6547ff42013-07-16 20:12:42 -07001370void Layer::logFrameStats() {
1371 mFrameTracker.logAndResetStats(mName);
1372}
1373
Svetoslavd85084b2014-03-20 10:28:31 -07001374void Layer::getFrameStats(FrameStats* outStats) const {
1375 mFrameTracker.getStats(outStats);
1376}
1377
Yiwei Zhang5434a782018-12-05 18:06:32 -08001378void Layer::dumpFrameEvents(std::string& result) {
1379 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001380 Mutex::Autolock lock(mFrameEventHistoryMutex);
1381 mFrameEventHistory.checkFencesForCompletion();
1382 mFrameEventHistory.dump(result);
1383}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001384
Brian Anderson5ea5e592016-12-01 16:54:33 -08001385void Layer::onDisconnect() {
1386 Mutex::Autolock lock(mFrameEventHistoryMutex);
1387 mFrameEventHistory.onDisconnect();
Yiwei Zhangaf8ee942018-11-22 00:15:23 -08001388 mFlinger->mTimeStats->onDestroy(getSequence());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001389}
1390
Brian Anderson3890c392016-07-25 12:48:08 -07001391void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001392 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001393 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001394 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1395 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001396 }
1397
Brian Andersond6927fb2016-07-23 23:37:30 -07001398 Mutex::Autolock lock(mFrameEventHistoryMutex);
1399 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001400 // If there are any unsignaled fences in the aquire timeline at this
1401 // point, the previously queued frame hasn't been latched yet. Go ahead
1402 // and try to get the signal time here so the syscall is taken out of
1403 // the main thread's critical path.
1404 mAcquireTimeline.updateSignalTimes();
1405 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001406 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001407 mFrameEventHistory.addQueue(*newTimestamps);
1408 }
1409
Brian Anderson3890c392016-07-25 12:48:08 -07001410 if (outDelta) {
1411 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001412 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001413}
Dan Stozae77c7662016-05-13 11:37:28 -07001414
Chia-I Wu98f1c102017-05-30 14:54:08 -07001415size_t Layer::getChildrenCount() const {
1416 size_t count = 0;
1417 for (const sp<Layer>& child : mCurrentChildren) {
1418 count += 1 + child->getChildrenCount();
1419 }
1420 return count;
1421}
1422
Robert Carr1f0a16a2016-10-24 16:27:39 -07001423void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001424 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001425 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001426
Robert Carr1f0a16a2016-10-24 16:27:39 -07001427 mCurrentChildren.add(layer);
1428 layer->setParent(this);
1429}
1430
1431ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001432 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001433 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001434
Robert Carr1323c952019-01-28 18:13:27 -08001435 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001436 return mCurrentChildren.remove(layer);
1437}
1438
Robert Carr1db73f62016-12-21 12:58:51 -08001439bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1440 sp<Handle> handle = nullptr;
1441 sp<Layer> newParent = nullptr;
1442 if (newParentHandle == nullptr) {
1443 return false;
1444 }
1445 handle = static_cast<Handle*>(newParentHandle.get());
1446 newParent = handle->owner.promote();
1447 if (newParent == nullptr) {
1448 ALOGE("Unable to promote Layer handle");
1449 return false;
1450 }
1451
chaviw5aedec92018-10-22 10:40:38 -07001452 if (attachChildren()) {
1453 setTransactionFlags(eTransactionNeeded);
1454 }
Robert Carr1db73f62016-12-21 12:58:51 -08001455 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001456 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001457 }
1458 mCurrentChildren.clear();
1459
1460 return true;
1461}
1462
Robert Carr15eae092018-03-23 13:43:53 -07001463void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001464 for (const sp<Layer>& child : mDrawingChildren) {
1465 child->mDrawingParent = newParent;
Vishnu Nairc652ff82019-03-15 12:48:54 -07001466 child->computeBounds(newParent->mBounds,
1467 newParent->getTransformWithScale(
1468 newParent->getBufferScaleTransform()));
Robert Carr578038f2018-03-09 12:25:24 -08001469 }
1470}
1471
chaviwf1961f72017-09-18 16:41:07 -07001472bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001473 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001474
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001475 // While layers are detached, we allow most operations
1476 // and simply halt performing the actual transaction. However
1477 // for reparent != null we would enter the mRemovedFromCurrentState
1478 // state, regardless of whether doTransaction was called, and
1479 // so we need to prevent the update here.
1480 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001481 return false;
1482 }
1483
Robert Carr54cf5b12019-01-25 14:02:28 -08001484 sp<Layer> newParent;
1485 if (newParentHandle != nullptr) {
1486 auto handle = static_cast<Handle*>(newParentHandle.get());
1487 newParent = handle->owner.promote();
1488 if (newParent == nullptr) {
1489 ALOGE("Unable to promote Layer handle");
1490 return false;
1491 }
1492 if (newParent == this) {
1493 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1494 return false;
1495 }
1496 }
1497
chaviwf1961f72017-09-18 16:41:07 -07001498 sp<Layer> parent = getParent();
1499 if (parent != nullptr) {
1500 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001501 }
1502
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001503 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001504 newParent->addChild(this);
1505 if (!newParent->isRemovedFromCurrentState()) {
1506 addToCurrentState();
1507 } else {
1508 onRemovedFromCurrentState();
1509 }
1510
1511 if (mLayerDetached) {
1512 mLayerDetached = false;
1513 callSetTransactionFlags = true;
1514 }
1515 } else {
1516 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001517 }
1518
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001519 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001520 setTransactionFlags(eTransactionNeeded);
1521 }
chaviw06178942017-07-27 10:25:59 -07001522 return true;
1523}
1524
Robert Carr9524cb32017-02-13 11:32:32 -08001525bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001526 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001527 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001528 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001529 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001530 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001531 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001532 }
Robert Carr7f619b22017-11-06 12:56:35 -08001533 }
Robert Carr9524cb32017-02-13 11:32:32 -08001534
1535 return true;
1536}
1537
chaviw5aedec92018-10-22 10:40:38 -07001538bool Layer::attachChildren() {
1539 bool changed = false;
1540 for (const sp<Layer>& child : mCurrentChildren) {
1541 sp<Client> parentClient = mClientRef.promote();
1542 sp<Client> client(child->mClientRef.promote());
1543 if (client != nullptr && parentClient != client) {
1544 if (child->mLayerDetached) {
1545 child->mLayerDetached = false;
1546 changed = true;
1547 }
1548 changed |= child->attachChildren();
1549 }
1550 }
1551
1552 return changed;
1553}
1554
Peiyong Lind3788632018-09-18 16:01:31 -07001555bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001556 static const mat4 identityMatrix = mat4();
1557
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001558 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001559 return false;
1560 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001561 ++mCurrentState.sequence;
1562 mCurrentState.colorTransform = matrix;
1563 mCurrentState.hasColorTransform = matrix != identityMatrix;
1564 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001565 setTransactionFlags(eTransactionNeeded);
1566 return true;
1567}
1568
chaviwf66724d2018-11-28 16:35:21 -08001569mat4 Layer::getColorTransform() const {
1570 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1571 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1572 colorTransform = parent->getColorTransform() * colorTransform;
1573 }
1574 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001575}
1576
1577bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001578 bool hasColorTransform = getDrawingState().hasColorTransform;
1579 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1580 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1581 }
1582 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001583}
1584
Chia-I Wu11481472018-05-04 10:43:19 -07001585bool Layer::isLegacyDataSpace() const {
1586 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001587 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001588 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001589}
1590
Robert Carr1f0a16a2016-10-24 16:27:39 -07001591void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001592 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001593}
1594
Robert Carr1f0a16a2016-10-24 16:27:39 -07001595int32_t Layer::getZ() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001596 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001597}
1598
Robert Carr29abff82017-12-04 13:51:20 -08001599bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1600 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001601 const State& state = useDrawing ? mDrawingState : mCurrentState;
Robert Carr29abff82017-12-04 13:51:20 -08001602 return state.zOrderRelativeOf != nullptr;
1603}
1604
David Sodman41fdfc92017-11-06 16:09:56 -08001605__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001606 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001607 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1608 "makeTraversalList received invalid stateSet");
1609 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1610 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001611 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001612
Robert Carr29abff82017-12-04 13:51:20 -08001613 if (state.zOrderRelatives.size() == 0) {
1614 *outSkipRelativeZUsers = true;
1615 return children;
1616 }
1617
chaviwfd462612018-05-31 16:11:27 -07001618 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001619 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001620 sp<Layer> strongRelative = weakRelative.promote();
1621 if (strongRelative != nullptr) {
1622 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001623 }
1624 }
1625
Dan Stoza412903f2017-04-27 13:42:17 -07001626 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001627 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
Robert Carr503c7042017-09-27 15:06:08 -07001628 if (childState.zOrderRelativeOf != nullptr) {
1629 continue;
1630 }
Robert Carrdb66e622017-04-10 16:55:57 -07001631 traverse.add(child);
1632 }
1633
1634 return traverse;
1635}
1636
Robert Carr1f0a16a2016-10-24 16:27:39 -07001637/**
Robert Carrdb66e622017-04-10 16:55:57 -07001638 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001639 */
Dan Stoza412903f2017-04-27 13:42:17 -07001640void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001641 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1642 // produce a new list for traversing, including our relatives, and not including our children
1643 // who are relatives of another surface. In the case that there are no relative Z,
1644 // makeTraversalList returns our children directly to avoid significant overhead.
1645 // However in this case we need to take the responsibility for filtering children which
1646 // are relatives of another surface here.
1647 bool skipRelativeZUsers = false;
1648 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001649
Robert Carr1f0a16a2016-10-24 16:27:39 -07001650 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001651 for (; i < list.size(); i++) {
1652 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001653 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1654 continue;
1655 }
1656
Robert Carrdb66e622017-04-10 16:55:57 -07001657 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001658 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001659 }
Dan Stoza412903f2017-04-27 13:42:17 -07001660 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001661 }
Robert Carr29abff82017-12-04 13:51:20 -08001662
Dan Stoza412903f2017-04-27 13:42:17 -07001663 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001664 for (; i < list.size(); i++) {
1665 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001666
Robert Carr29abff82017-12-04 13:51:20 -08001667 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1668 continue;
1669 }
Dan Stoza412903f2017-04-27 13:42:17 -07001670 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001671 }
1672}
1673
1674/**
Robert Carrdb66e622017-04-10 16:55:57 -07001675 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001676 */
Dan Stoza412903f2017-04-27 13:42:17 -07001677void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1678 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001679 // See traverseInZOrder for documentation.
1680 bool skipRelativeZUsers = false;
1681 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001682
Robert Carr1f0a16a2016-10-24 16:27:39 -07001683 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001684 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001685 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001686
1687 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1688 continue;
1689 }
1690
Robert Carrdb66e622017-04-10 16:55:57 -07001691 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001692 break;
1693 }
Dan Stoza412903f2017-04-27 13:42:17 -07001694 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001695 }
Dan Stoza412903f2017-04-27 13:42:17 -07001696 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001697 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001698 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001699
1700 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1701 continue;
1702 }
1703
Dan Stoza412903f2017-04-27 13:42:17 -07001704 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001705 }
1706}
1707
chaviw4b129c22018-04-09 16:19:43 -07001708LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1709 const std::vector<Layer*>& layersInTree) {
1710 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1711 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001712 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1713 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001714 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001715
chaviwfd462612018-05-31 16:11:27 -07001716 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001717 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1718 sp<Layer> strongRelative = weakRelative.promote();
1719 // Only add relative layers that are also descendents of the top most parent of the tree.
1720 // If a relative layer is not a descendent, then it should be ignored.
1721 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1722 traverse.add(strongRelative);
1723 }
1724 }
1725
1726 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001727 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001728 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1729 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1730 // the child here since it won't be added later as a relative.
1731 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1732 childState.zOrderRelativeOf.promote().get())) {
1733 continue;
1734 }
1735 traverse.add(child);
1736 }
1737
1738 return traverse;
1739}
1740
1741void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1742 LayerVector::StateSet stateSet,
1743 const LayerVector::Visitor& visitor) {
1744 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001745
1746 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001747 for (; i < list.size(); i++) {
1748 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001749 if (relative->getZ() >= 0) {
1750 break;
1751 }
chaviw4b129c22018-04-09 16:19:43 -07001752 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001753 }
chaviw4b129c22018-04-09 16:19:43 -07001754
chaviwa76b2712017-09-20 12:02:26 -07001755 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001756 for (; i < list.size(); i++) {
1757 const auto& relative = list[i];
1758 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001759 }
1760}
1761
chaviw4b129c22018-04-09 16:19:43 -07001762std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1763 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1764 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1765
1766 std::vector<Layer*> layersInTree = {this};
1767 for (size_t i = 0; i < children.size(); i++) {
1768 const auto& child = children[i];
1769 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1770 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1771 }
1772
1773 return layersInTree;
1774}
1775
1776void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1777 const LayerVector::Visitor& visitor) {
1778 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1779 std::sort(layersInTree.begin(), layersInTree.end());
1780 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1781}
1782
Peiyong Linefefaac2018-08-17 12:27:51 -07001783ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001784 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001785}
1786
chaviw13fdc492017-06-27 12:40:18 -07001787half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001788 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001789
chaviw13fdc492017-06-27 12:40:18 -07001790 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1791 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001792}
Robert Carr6452f122017-03-21 10:41:29 -07001793
chaviw13fdc492017-06-27 12:40:18 -07001794half4 Layer::getColor() const {
1795 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001796 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001797}
Robert Carr6452f122017-03-21 10:41:29 -07001798
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001799Layer::RoundedCornerState Layer::getRoundedCornerState() const {
1800 const auto& p = mDrawingParent.promote();
1801 if (p != nullptr) {
Peiyong Lin27016a92019-03-29 17:36:08 +00001802 RoundedCornerState parentState = p->getRoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001803 if (parentState.radius > 0) {
1804 ui::Transform t = getActiveTransform(getDrawingState());
1805 t = t.inverse();
1806 parentState.cropRect = t.transform(parentState.cropRect);
1807 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1808 // but a transform matrix can define horizontal and vertical scales.
1809 // Let's take the average between both of them and pass into the shader, practically we
1810 // never do this type of transformation on windows anyway.
1811 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
1812 return parentState;
1813 }
1814 }
1815 const float radius = getDrawingState().cornerRadius;
Peiyong Linb9ff23e2019-04-08 11:04:59 -07001816 return radius > 0 && getCrop(getDrawingState()).isValid()
1817 ? RoundedCornerState(getCrop(getDrawingState()).toFloatRect(), radius)
1818 : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001819}
1820
Robert Carr1f0a16a2016-10-24 16:27:39 -07001821void Layer::commitChildList() {
1822 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1823 const auto& child = mCurrentChildren[i];
1824 child->commitChildList();
1825 }
1826 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001827 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001828}
1829
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001830static wp<Layer> extractLayerFromBinder(const wp<IBinder>& weakBinderHandle) {
1831 if (weakBinderHandle == nullptr) {
1832 return nullptr;
1833 }
1834 sp<IBinder> binderHandle = weakBinderHandle.promote();
1835 if (binderHandle == nullptr) {
1836 return nullptr;
1837 }
1838 sp<Layer::Handle> handle = static_cast<Layer::Handle*>(binderHandle.get());
1839 if (handle == nullptr) {
1840 return nullptr;
1841 }
1842 return handle->owner;
1843}
1844
Robert Carr720e5062018-07-30 17:45:14 -07001845void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001846 mCurrentState.inputInfo = info;
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001847 mCurrentState.touchableRegionCrop = extractLayerFromBinder(info.touchableRegionCropHandle);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001848 mCurrentState.modified = true;
1849 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07001850 setTransactionFlags(eTransactionNeeded);
1851}
1852
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001853void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet,
1854 uint32_t traceFlags) {
chaviw1d044282017-09-27 12:19:28 -07001855 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1856 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001857 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07001858
Peiyong Linefefaac2018-08-17 12:27:51 -07001859 ui::Transform requestedTransform = state.active_legacy.transform;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001860 ui::Transform transform = getTransform();
chaviw1d044282017-09-27 12:19:28 -07001861
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001862 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
1863 layerInfo->set_id(sequence);
1864 layerInfo->set_name(getName().c_str());
1865 layerInfo->set_type(String8(getTypeId()));
chaviw1d044282017-09-27 12:19:28 -07001866
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001867 for (const auto& child : children) {
1868 layerInfo->add_children(child->sequence);
chaviw1d044282017-09-27 12:19:28 -07001869 }
chaviw1d044282017-09-27 12:19:28 -07001870
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001871 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1872 sp<Layer> strongRelative = weakRelative.promote();
1873 if (strongRelative != nullptr) {
1874 layerInfo->add_relatives(strongRelative->sequence);
1875 }
chaviwadc40c22018-07-10 16:57:27 -07001876 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001877
1878 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
1879 [&]() { return layerInfo->mutable_transparent_region(); });
1880 LayerProtoHelper::writeToProto(visibleRegion,
1881 [&]() { return layerInfo->mutable_visible_region(); });
1882 LayerProtoHelper::writeToProto(surfaceDamageRegion,
1883 [&]() { return layerInfo->mutable_damage_region(); });
1884
1885 layerInfo->set_layer_stack(getLayerStack());
1886 layerInfo->set_z(state.z);
1887
1888 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
1889 [&]() { return layerInfo->mutable_position(); });
1890
1891 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(),
1892 [&]() {
1893 return layerInfo->mutable_requested_position();
1894 });
1895
1896 LayerProtoHelper::writeSizeToProto(state.active_legacy.w, state.active_legacy.h,
1897 [&]() { return layerInfo->mutable_size(); });
1898
1899 LayerProtoHelper::writeToProto(state.crop_legacy,
1900 [&]() { return layerInfo->mutable_crop(); });
1901 layerInfo->set_corner_radius(getRoundedCornerState().radius);
1902
1903 layerInfo->set_is_opaque(isOpaque(state));
1904 layerInfo->set_invalidate(contentDirty);
1905
1906 // XXX (b/79210409) mCurrentDataSpace is not protected
1907 layerInfo->set_dataspace(
1908 dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
1909
1910 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1911 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
1912 LayerProtoHelper::writeToProto(state.color,
1913 [&]() { return layerInfo->mutable_requested_color(); });
1914 layerInfo->set_flags(state.flags);
1915
1916 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1917 LayerProtoHelper::writeToProto(requestedTransform,
1918 layerInfo->mutable_requested_transform());
1919
1920 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
1921 if (parent != nullptr) {
1922 layerInfo->set_parent(parent->sequence);
1923 } else {
1924 layerInfo->set_parent(-1);
1925 }
1926
1927 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1928 if (zOrderRelativeOf != nullptr) {
1929 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1930 } else {
1931 layerInfo->set_z_order_relative_of(-1);
1932 }
1933
1934 auto buffer = mActiveBuffer;
1935 if (buffer != nullptr) {
1936 LayerProtoHelper::writeToProto(buffer,
1937 [&]() { return layerInfo->mutable_active_buffer(); });
1938 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
1939 layerInfo->mutable_buffer_transform());
1940 }
1941
1942 layerInfo->set_queued_frames(getQueuedFrameCount());
1943 layerInfo->set_refresh_pending(isBufferLatched());
1944 layerInfo->set_curr_frame(mCurrentFrameNumber);
1945 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
1946
1947 for (const auto& pendingState : mPendingStates) {
1948 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
1949 if (barrierLayer != nullptr) {
1950 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
1951 barrierLayerProto->set_id(barrierLayer->sequence);
1952 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
1953 }
1954 }
1955 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
chaviwadc40c22018-07-10 16:57:27 -07001956 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001957
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001958 if (traceFlags & SurfaceTracing::TRACE_INPUT) {
1959 LayerProtoHelper::writeToProto(state.inputInfo, state.touchableRegionCrop,
1960 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001961 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001962
1963 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
1964 auto protoMap = layerInfo->mutable_metadata();
1965 for (const auto& entry : state.metadata.mMap) {
1966 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
1967 }
1968 LayerProtoHelper::writeToProto(mEffectiveTransform,
1969 layerInfo->mutable_effective_transform());
1970 LayerProtoHelper::writeToProto(mSourceBounds,
1971 [&]() { return layerInfo->mutable_source_bounds(); });
1972 LayerProtoHelper::writeToProto(mScreenBounds,
1973 [&]() { return layerInfo->mutable_screen_bounds(); });
1974 }
chaviw1d044282017-09-27 12:19:28 -07001975}
1976
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001977void Layer::writeToProto(LayerProto* layerInfo, const sp<DisplayDevice>& displayDevice,
1978 uint32_t traceFlags) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001979 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1980 if (!outputLayer) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07001981 return;
1982 }
1983
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001984 writeToProto(layerInfo, LayerVector::StateSet::Drawing, traceFlags);
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001985
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001986 const auto& compositionState = outputLayer->getState();
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001987
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001988 const Rect& frame = compositionState.displayFrame;
Nataniel Borges797b0e42019-02-15 14:11:58 -08001989 LayerProtoHelper::writeToProto(frame, [&]() { return layerInfo->mutable_hwc_frame(); });
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001990
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001991 const FloatRect& crop = compositionState.sourceCrop;
Nataniel Borges797b0e42019-02-15 14:11:58 -08001992 LayerProtoHelper::writeToProto(crop, [&]() { return layerInfo->mutable_hwc_crop(); });
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001993
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001994 const int32_t transform =
1995 getCompositionLayer() ? static_cast<int32_t>(compositionState.bufferTransform) : 0;
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001996 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08001997
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001998 const int32_t compositionType =
1999 static_cast<int32_t>(compositionState.hwc ? (*compositionState.hwc).hwcCompositionType
2000 : Hwc2::IComposerClient::Composition::CLIENT);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002001 layerInfo->set_hwc_composition_type(compositionType);
2002
2003 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
2004 static_cast<BufferLayer*>(this)->isProtected()) {
2005 layerInfo->set_is_protected(true);
2006 } else {
2007 layerInfo->set_is_protected(false);
2008 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002009}
2010
Robert Carr2e102c92018-10-23 12:11:15 -07002011bool Layer::isRemovedFromCurrentState() const {
2012 return mRemovedFromCurrentState;
2013}
2014
Arthur Hungd20b2702019-01-14 18:16:16 +08002015InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002016 InputWindowInfo info = mDrawingState.inputInfo;
Robert Carr720e5062018-07-30 17:45:14 -07002017
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002018 if (info.displayId == ADISPLAY_ID_NONE) {
2019 info.displayId = mDrawingState.layerStack;
2020 }
2021
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002022 ui::Transform t = getTransform();
2023 const float xScale = t.sx();
2024 const float yScale = t.sy();
2025 if (xScale != 1.0f || yScale != 1.0f) {
2026 info.windowXScale *= 1.0f / xScale;
2027 info.windowYScale *= 1.0f / yScale;
2028 info.touchableRegion.scaleSelf(xScale, yScale);
Riddle Hsu39d4aa52018-11-30 20:46:53 +08002029 }
Robert Carre07e1032018-11-26 12:55:53 -08002030
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002031 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002032 // If this is a portal window, set the touchableRegion to the layerBounds.
2033 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
2034 ? getBufferSize(getDrawingState())
2035 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002036 if (!layerBounds.isValid()) {
2037 layerBounds = getCroppedBufferSize(getDrawingState());
2038 }
Vishnu Nair8033a492018-12-05 07:27:23 -08002039 layerBounds = t.transform(layerBounds);
2040 layerBounds.inset(info.surfaceInset, info.surfaceInset, info.surfaceInset, info.surfaceInset);
2041
Arthur Hungd20b2702019-01-14 18:16:16 +08002042 // Input coordinate should match the layer bounds.
2043 info.frameLeft = layerBounds.left;
2044 info.frameTop = layerBounds.top;
2045 info.frameRight = layerBounds.right;
2046 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08002047
2048 // Position the touchable region relative to frame screen location and restrict it to frame
2049 // bounds.
2050 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08002051 info.visible = canReceiveInput();
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002052
2053 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2054 if (info.replaceTouchableRegionWithCrop) {
2055 if (cropLayer == nullptr) {
2056 info.touchableRegion = Region(Rect{mScreenBounds});
2057 } else {
2058 info.touchableRegion = Region(Rect{cropLayer->mScreenBounds});
2059 }
2060 } else if (cropLayer != nullptr) {
2061 info.touchableRegion = info.touchableRegion.intersect(Rect{cropLayer->mScreenBounds});
2062 }
2063
Robert Carr720e5062018-07-30 17:45:14 -07002064 return info;
2065}
2066
2067bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002068 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002069}
2070
Lloyd Piquefeb73d72018-12-04 17:23:44 -08002071std::shared_ptr<compositionengine::Layer> Layer::getCompositionLayer() const {
2072 return nullptr;
2073}
2074
chaviw3e727cd2019-01-31 13:41:05 -08002075bool Layer::canReceiveInput() const {
2076 return isVisible();
2077}
2078
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002079compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
2080 const sp<const DisplayDevice>& display) const {
2081 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionLayer().get());
2082}
2083
Mathias Agopian13127d82013-03-05 17:47:11 -08002084// ---------------------------------------------------------------------------
2085
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002086}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002087
2088#if defined(__gl_h_)
2089#error "don't include gl/gl.h in this file"
2090#endif
2091
2092#if defined(__gl2_h_)
2093#error "don't include gl2/gl2.h in this file"
2094#endif