blob: b5f080037bcae4c166aac12b260a6fc232958655 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dan Stoza9e56aa02015-11-02 13:00:03 -080017//#define LOG_NDEBUG 0
18#undef LOG_TAG
19#define LOG_TAG "Layer"
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080020#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21
Alec Mourie60041e2019-06-14 18:59:51 -070022#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023
Yiwei Zhang5434a782018-12-05 18:06:32 -080024#include <android-base/stringprintf.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080025#include <compositionengine/Display.h>
Lloyd Pique0b785d82018-12-04 17:25:27 -080026#include <compositionengine/Layer.h>
Lloyd Piquea83776c2019-01-29 18:42:32 -080027#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080028#include <compositionengine/OutputLayer.h>
Lloyd Pique0b785d82018-12-04 17:25:27 -080029#include <compositionengine/impl/LayerCompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080030#include <compositionengine/impl/OutputLayerCompositionState.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070031#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070032#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070033#include <cutils/properties.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080034#include <gui/BufferItem.h>
35#include <gui/LayerDebugInfo.h>
36#include <gui/Surface.h>
Alec Mourie60041e2019-06-14 18:59:51 -070037#include <math.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080038#include <renderengine/RenderEngine.h>
Alec Mourie60041e2019-06-14 18:59:51 -070039#include <stdint.h>
40#include <stdlib.h>
41#include <sys/types.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080042#include <ui/DebugUtils.h>
43#include <ui/GraphicBuffer.h>
44#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045#include <utils/Errors.h>
46#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080047#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080049#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050
Alec Mourie60041e2019-06-14 18:59:51 -070051#include <algorithm>
52#include <mutex>
53#include <sstream>
54
Yiwei Zhang60d1a192018-03-07 14:52:28 -080055#include "BufferLayer.h"
Valerie Haudd0b7572019-01-29 14:59:27 -080056#include "ColorLayer.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020057#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070058#include "DisplayDevice.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080059#include "DisplayHardware/HWComposer.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080060#include "LayerProtoHelper.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070061#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070062#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063#include "SurfaceFlinger.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080064#include "TimeStats/TimeStats.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070065
David Sodman41fdfc92017-11-06 16:09:56 -080066#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068namespace android {
69
Yiwei Zhang5434a782018-12-05 18:06:32 -080070using base::StringAppendF;
71
Lloyd Piquef1c675b2018-09-12 20:45:39 -070072std::atomic<int32_t> Layer::sSequence{1};
Mathias Agopian13127d82013-03-05 17:47:11 -080073
Lloyd Pique42ab75e2018-09-12 20:46:03 -070074Layer::Layer(const LayerCreationArgs& args)
Ady Abraham8f1ee7f2019-04-05 10:32:50 -070075 : mFlinger(args.flinger),
76 mName(args.name),
77 mClientRef(args.client),
78 mWindowType(args.metadata.getInt32(METADATA_WINDOW_TYPE, 0)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -070079 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070080
81 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -070082 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
83 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
84 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070085
Dan Stozaf7ba41a2017-05-10 15:11:11 -070086 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070087
Lloyd Pique0449b0f2018-12-20 16:23:45 -080088 mCurrentState.active_legacy.w = args.w;
89 mCurrentState.active_legacy.h = args.h;
90 mCurrentState.flags = layerFlags;
91 mCurrentState.active_legacy.transform.set(0, 0);
92 mCurrentState.crop_legacy.makeInvalid();
93 mCurrentState.requestedCrop_legacy = mCurrentState.crop_legacy;
94 mCurrentState.z = 0;
95 mCurrentState.color.a = 1.0f;
96 mCurrentState.layerStack = 0;
97 mCurrentState.sequence = 0;
98 mCurrentState.requested_legacy = mCurrentState.active_legacy;
Lloyd Pique0449b0f2018-12-20 16:23:45 -080099 mCurrentState.active.w = UINT32_MAX;
100 mCurrentState.active.h = UINT32_MAX;
101 mCurrentState.active.transform.set(0, 0);
102 mCurrentState.transform = 0;
103 mCurrentState.transformToDisplayInverse = false;
104 mCurrentState.crop.makeInvalid();
105 mCurrentState.acquireFence = new Fence(-1);
106 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
107 mCurrentState.hdrMetadata.validTypes = 0;
108 mCurrentState.surfaceDamageRegion.clear();
109 mCurrentState.cornerRadius = 0.0f;
110 mCurrentState.api = -1;
111 mCurrentState.hasColorTransform = false;
Peiyong Linc502cb72019-03-01 15:00:23 -0800112 mCurrentState.colorSpaceAgnostic = false;
Evan Roskya1f1e152019-01-24 16:17:46 -0800113 mCurrentState.metadata = args.metadata;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700114
115 // drawing state & current state are identical
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800116 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700117
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800118 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700119 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800120 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200121 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700122
Ady Abraham8f1ee7f2019-04-05 10:32:50 -0700123 mSchedulerLayerHandle = mFlinger->mScheduler->registerLayer(mName.c_str(), mWindowType);
Ady Abraham09bd3922019-04-08 10:44:56 -0700124
Robert Carr2e102c92018-10-23 12:11:15 -0700125 mFlinger->onLayerCreated();
Dan Stoza436ccf32018-06-21 12:10:12 -0700126}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700127
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700128Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800129 sp<Client> c(mClientRef.promote());
130 if (c != 0) {
131 c->detachLayer(this);
132 }
133
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000134 mFrameTracker.logAndResetStats(mName);
chaviw74d90ad2019-04-26 14:45:26 -0700135 mFlinger->onLayerDestroyed(this);
Mathias Agopian96f08192010-06-02 23:28:45 -0700136}
137
Mathias Agopian13127d82013-03-05 17:47:11 -0800138// ---------------------------------------------------------------------------
139// callbacks
140// ---------------------------------------------------------------------------
141
David Sodmaneb085e02017-10-05 18:49:04 -0700142/*
143 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
144 * Layer. So, the implementation is done in BufferLayer. When called on a
145 * ColorLayer object, it's essentially a NOP.
146 */
David Sodmaneb085e02017-10-05 18:49:04 -0700147void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800148
chaviw43cb3cb2019-05-31 15:23:41 -0700149void Layer::removeRemoteSyncPoints() {
150 for (auto& point : mRemoteSyncPoints) {
151 point->setTransactionApplied();
152 }
153 mRemoteSyncPoints.clear();
154
155 {
156 Mutex::Autolock pendingStateLock(mPendingStateMutex);
157 for (State pendingState : mPendingStates) {
158 pendingState.barrierLayer_legacy = nullptr;
159 }
160 }
161}
162
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700163void Layer::removeRelativeZ(const std::vector<Layer*>& layersInTree) {
164 if (mCurrentState.zOrderRelativeOf == nullptr) {
165 return;
166 }
Robert Carr2e102c92018-10-23 12:11:15 -0700167
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700168 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
169 if (strongRelative == nullptr) {
170 setZOrderRelativeOf(nullptr);
171 return;
172 }
173
174 if (!std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
175 strongRelative->removeZOrderRelative(this);
176 mFlinger->setTransactionFlags(eTraversalNeeded);
chaviw606e5cf2019-03-01 10:12:10 -0800177 setZOrderRelativeOf(nullptr);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700178 }
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700179}
180
181void Layer::removeFromCurrentState() {
182 mRemovedFromCurrentState = true;
Rob Carr4bba3702018-10-08 21:53:30 +0000183
Robert Carr2e102c92018-10-23 12:11:15 -0700184 // Since we are no longer reachable from CurrentState SurfaceFlinger
185 // will no longer invoke doTransaction for us, and so we will
186 // never finish applying transactions. We signal the sync point
187 // now so that another layer will not become indefinitely
188 // blocked.
chaviw43cb3cb2019-05-31 15:23:41 -0700189 removeRemoteSyncPoints();
Robert Carr2e102c92018-10-23 12:11:15 -0700190
191 {
192 Mutex::Autolock syncLock(mLocalSyncPointMutex);
193 for (auto& point : mLocalSyncPoints) {
194 point->setFrameAvailable();
195 }
196 mLocalSyncPoints.clear();
197 }
198
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800199 mFlinger->markLayerPendingRemovalLocked(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700200}
Chia-I Wu38512252017-05-17 14:36:16 -0700201
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700202void Layer::onRemovedFromCurrentState() {
203 auto layersInTree = getLayersInTree(LayerVector::StateSet::Current);
204 std::sort(layersInTree.begin(), layersInTree.end());
205 for (const auto& layer : layersInTree) {
206 layer->removeFromCurrentState();
207 layer->removeRelativeZ(layersInTree);
208 }
209}
210
chaviw61626f22018-11-15 16:26:27 -0800211void Layer::addToCurrentState() {
212 mRemovedFromCurrentState = false;
213
214 for (const auto& child : mCurrentChildren) {
215 child->addToCurrentState();
216 }
217}
218
Mathias Agopian13127d82013-03-05 17:47:11 -0800219// ---------------------------------------------------------------------------
220// set-up
221// ---------------------------------------------------------------------------
222
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700223const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800224 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800225}
226
chaviw13fdc492017-06-27 12:40:18 -0700227bool Layer::getPremultipledAlpha() const {
228 return mPremultipliedAlpha;
229}
230
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700231sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800232 Mutex::Autolock _l(mLock);
Robert Carrc0df3122019-04-11 13:18:21 -0700233 if (mGetHandleCalled) {
234 ALOGE("Get handle called twice" );
235 return nullptr;
236 }
237 mGetHandleCalled = true;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700238 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800239}
240
241// ---------------------------------------------------------------------------
242// h/w composer set-up
243// ---------------------------------------------------------------------------
244
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800245Rect Layer::getContentCrop() const {
246 // this is the crop rectangle that applies to the buffer
247 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700248 Rect crop;
249 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800250 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700251 crop = mCurrentCrop;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800252 } else if (mActiveBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800253 // otherwise we use the whole buffer
Lloyd Pique0b785d82018-12-04 17:25:27 -0800254 crop = mActiveBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700255 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800256 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700257 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700258 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700259 return crop;
260}
261
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700262static Rect reduce(const Rect& win, const Region& exclude) {
263 if (CC_LIKELY(exclude.isEmpty())) {
264 return win;
265 }
266 if (exclude.isRect()) {
267 return win.reduce(exclude.getBounds());
268 }
269 return Region(win).subtract(exclude).getBounds();
270}
271
Dan Stoza80d61162017-12-20 15:57:52 -0800272static FloatRect reduce(const FloatRect& win, const Region& exclude) {
273 if (CC_LIKELY(exclude.isEmpty())) {
274 return win;
275 }
276 // Convert through Rect (by rounding) for lack of FloatRegion
277 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
278}
279
Vishnu Nair4351ad52019-02-11 14:13:02 -0800280Rect Layer::getScreenBounds(bool reduceTransparentRegion) const {
Vishnu Nairf0c28512019-02-08 12:40:28 -0800281 if (!reduceTransparentRegion) {
282 return Rect{mScreenBounds};
283 }
284
285 FloatRect bounds = getBounds();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800286 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800287 // Transform to screen space.
288 bounds = t.transform(bounds);
289 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700290}
291
Vishnu Nair4351ad52019-02-11 14:13:02 -0800292FloatRect Layer::getBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000293 const State& s(getDrawingState());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800294 return getBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700295}
296
Vishnu Nairf0c28512019-02-08 12:40:28 -0800297FloatRect Layer::getBounds(const Region& activeTransparentRegion) const {
298 // Subtract the transparent region and snap to the bounds.
299 return reduce(mBounds, activeTransparentRegion);
300}
301
Vishnu Nairc652ff82019-03-15 12:48:54 -0700302ui::Transform Layer::getBufferScaleTransform() const {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800303 // If the layer is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
304 // it isFixedSize) then there may be additional scaling not accounted
Vishnu Nairc652ff82019-03-15 12:48:54 -0700305 // for in the layer transform.
Lloyd Pique0b785d82018-12-04 17:25:27 -0800306 if (!isFixedSize() || !mActiveBuffer) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700307 return {};
Vishnu Nair4351ad52019-02-11 14:13:02 -0800308 }
309
Marissa Wall290ad082019-03-06 13:23:47 -0800310 // If the layer is a buffer state layer, the active width and height
311 // could be infinite. In that case, return the effective transform.
312 const uint32_t activeWidth = getActiveWidth(getDrawingState());
313 const uint32_t activeHeight = getActiveHeight(getDrawingState());
314 if (activeWidth >= UINT32_MAX && activeHeight >= UINT32_MAX) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700315 return {};
Marissa Wall290ad082019-03-06 13:23:47 -0800316 }
317
Vishnu Nairc652ff82019-03-15 12:48:54 -0700318 int bufferWidth = mActiveBuffer->getWidth();
319 int bufferHeight = mActiveBuffer->getHeight();
320
321 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
322 std::swap(bufferWidth, bufferHeight);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800323 }
Vishnu Nairc652ff82019-03-15 12:48:54 -0700324
Marissa Wall290ad082019-03-06 13:23:47 -0800325 float sx = activeWidth / static_cast<float>(bufferWidth);
326 float sy = activeHeight / static_cast<float>(bufferHeight);
327
Vishnu Nair4351ad52019-02-11 14:13:02 -0800328 ui::Transform extraParentScaling;
329 extraParentScaling.set(sx, 0, 0, sy);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700330 return extraParentScaling;
331}
332
333ui::Transform Layer::getTransformWithScale(const ui::Transform& bufferScaleTransform) const {
334 // We need to mirror this scaling to child surfaces or we will break the contract where WM can
335 // treat child surfaces as pixels in the parent surface.
336 if (!isFixedSize() || !mActiveBuffer) {
337 return mEffectiveTransform;
338 }
339 return mEffectiveTransform * bufferScaleTransform;
340}
341
342FloatRect Layer::getBoundsPreScaling(const ui::Transform& bufferScaleTransform) const {
343 // We need the pre scaled layer bounds when computing child bounds to make sure the child is
344 // cropped to its parent layer after any buffer transform scaling is applied.
345 if (!isFixedSize() || !mActiveBuffer) {
346 return mBounds;
347 }
348 return bufferScaleTransform.inverse().transform(mBounds);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800349}
350
351void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform) {
352 const State& s(getDrawingState());
353
354 // Calculate effective layer transform
355 mEffectiveTransform = parentTransform * getActiveTransform(s);
356
357 // Transform parent bounds to layer space
358 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
359
Vishnu Nairc652ff82019-03-15 12:48:54 -0700360 // Calculate source bounds
Vishnu Nair4351ad52019-02-11 14:13:02 -0800361 mSourceBounds = computeSourceBounds(parentBounds);
362
363 // Calculate bounds by croping diplay frame with layer crop and parent bounds
364 FloatRect bounds = mSourceBounds;
365 const Rect layerCrop = getCrop(s);
366 if (!layerCrop.isEmpty()) {
367 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
368 }
369 bounds = bounds.intersect(parentBounds);
370
371 mBounds = bounds;
372 mScreenBounds = mEffectiveTransform.transform(mBounds);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700373
374 // Add any buffer scaling to the layer's children.
375 ui::Transform bufferScaleTransform = getBufferScaleTransform();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800376 for (const sp<Layer>& child : mDrawingChildren) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700377 child->computeBounds(getBoundsPreScaling(bufferScaleTransform),
378 getTransformWithScale(bufferScaleTransform));
Vishnu Nair4351ad52019-02-11 14:13:02 -0800379 }
380}
381
Vishnu Nair60356342018-11-13 13:00:45 -0800382Rect Layer::getCroppedBufferSize(const State& s) const {
383 Rect size = getBufferSize(s);
384 Rect crop = getCrop(s);
385 if (!crop.isEmpty() && size.isValid()) {
386 size.intersect(crop, &size);
387 } else if (!crop.isEmpty()) {
388 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700389 }
Vishnu Nair60356342018-11-13 13:00:45 -0800390 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800391}
392
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700393void Layer::setupRoundedCornersCropCoordinates(Rect win,
394 const FloatRect& roundedCornersCrop) const {
395 // Translate win by the rounded corners rect coordinates, to have all values in
396 // layer coordinate space.
397 win.left -= roundedCornersCrop.left;
398 win.right -= roundedCornersCrop.left;
399 win.top -= roundedCornersCrop.top;
400 win.bottom -= roundedCornersCrop.top;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700401}
402
Lloyd Piquea83776c2019-01-29 18:42:32 -0800403void Layer::latchGeometry(compositionengine::LayerFECompositionState& compositionState) const {
404 const auto& drawingState{getDrawingState()};
405 auto alpha = static_cast<float>(getAlpha());
David Revemanecf0fa52017-03-03 11:32:44 -0500406 auto blendMode = HWC2::BlendMode::None;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800407 if (!isOpaque(drawingState) || alpha != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800408 blendMode =
409 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800410 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800411
Lloyd Piquea83776c2019-01-29 18:42:32 -0800412 int type = drawingState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
413 int appId = drawingState.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700414 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400415 if (parent.get()) {
416 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800417 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
418 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
Jiwen 'Steve' Cai736c74e2019-05-28 18:33:22 -0700419 if (parentType > 0 && parentAppId > 0) {
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800420 type = parentType;
421 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700422 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400423 }
424
Lloyd Piquea83776c2019-01-29 18:42:32 -0800425 compositionState.geomLayerTransform = getTransform();
426 compositionState.geomInverseLayerTransform = compositionState.geomLayerTransform.inverse();
427 compositionState.geomBufferSize = getBufferSize(drawingState);
428 compositionState.geomContentCrop = getContentCrop();
429 compositionState.geomCrop = getCrop(drawingState);
430 compositionState.geomBufferTransform = mCurrentTransform;
431 compositionState.geomBufferUsesDisplayInverseTransform = getTransformToDisplayInverse();
432 compositionState.geomActiveTransparentRegion = getActiveTransparentRegion(drawingState);
433 compositionState.geomLayerBounds = mBounds;
434 compositionState.geomUsesSourceCrop = usesSourceCrop();
435 compositionState.isSecure = isSecure();
David Sodman15094112018-10-11 09:39:37 -0700436
Lloyd Piquea83776c2019-01-29 18:42:32 -0800437 compositionState.blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
438 compositionState.alpha = alpha;
439 compositionState.type = type;
440 compositionState.appId = appId;
441}
David Sodmanba340492018-08-05 21:51:33 -0700442
Lloyd Piquef5275482019-01-29 18:42:42 -0800443void Layer::latchPerFrameState(compositionengine::LayerFECompositionState& compositionState) const {
444 compositionState.forceClientComposition = false;
445
446 // TODO(lpique): b/121291683 Remove this one we are sure we don't need the
447 // value recomputed / set every frame.
448 compositionState.geomVisibleRegion = visibleRegion;
449
450 compositionState.isColorspaceAgnostic = isColorSpaceAgnostic();
451 compositionState.dataspace = mCurrentDataSpace;
452 compositionState.colorTransform = getColorTransform();
453 compositionState.colorTransformIsIdentity = !hasColorTransform();
454 compositionState.surfaceDamage = surfaceDamageRegion;
455
456 // Force client composition for special cases known only to the front-end.
457 if (isHdrY410() || getRoundedCornerState().radius > 0.0f) {
458 compositionState.forceClientComposition = true;
459 }
460}
461
Lloyd Piquea83776c2019-01-29 18:42:32 -0800462void Layer::latchCompositionState(compositionengine::LayerFECompositionState& compositionState,
463 bool includeGeometry) const {
464 if (includeGeometry) {
465 latchGeometry(compositionState);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700466 }
Lloyd Piquef5275482019-01-29 18:42:42 -0800467
468 latchPerFrameState(compositionState);
Lloyd Piquea83776c2019-01-29 18:42:32 -0800469}
Mathias Agopian29a367b2011-07-12 14:51:45 -0700470
Lloyd Piquea83776c2019-01-29 18:42:32 -0800471const char* Layer::getDebugName() const {
472 return mName.string();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800473}
474
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700475void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800476 const auto outputLayer = findOutputLayerForDisplay(display);
477 LOG_FATAL_IF(!outputLayer);
478
479 if (!outputLayer->getState().hwc ||
480 (*outputLayer->getState().hwc).hwcCompositionType !=
481 Hwc2::IComposerClient::Composition::CURSOR) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800482 return;
483 }
484
485 // This gives us only the "orientation" component of the transform
Vishnu Nair33a6eee2019-02-06 13:48:06 -0800486 const State& s(getDrawingState());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800487
488 // Apply the layer's transform, followed by the display's global transform
489 // Here we're guaranteed that the layer's transform preserves rects
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800490 Rect win = getCroppedBufferSize(s);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800491 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700492 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800493 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700494 frame.intersect(display->getViewport(), &frame);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700495 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800496 auto position = displayTransform.transform(frame);
497
Dominik Laskowski7e045462018-05-30 13:02:02 -0700498 auto error =
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800499 (*outputLayer->getState().hwc).hwcLayer->setCursorPosition(position.left, position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800500 ALOGE_IF(error != HWC2::Error::None,
501 "[%s] Failed to set cursor position "
502 "to (%d, %d): %s (%d)",
503 mName.string(), position.left, position.top, to_string(error).c_str(),
504 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800505}
Riley Andrews03414a12014-07-01 14:22:59 -0700506
Mathias Agopian13127d82013-03-05 17:47:11 -0800507// ---------------------------------------------------------------------------
508// drawing...
509// ---------------------------------------------------------------------------
510
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000511bool Layer::prepareClientLayer(const RenderArea& renderArea, const Region& clip,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800512 Region& clearRegion, const bool supportProtectedContent,
513 renderengine::LayerSettings& layer) {
514 return prepareClientLayer(renderArea, clip, false, clearRegion, supportProtectedContent, layer);
Mathias Agopian13127d82013-03-05 17:47:11 -0800515}
516
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000517bool Layer::prepareClientLayer(const RenderArea& renderArea, bool useIdentityTransform,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800518 Region& clearRegion, const bool supportProtectedContent,
519 renderengine::LayerSettings& layer) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000520 return prepareClientLayer(renderArea, Region(renderArea.getBounds()), useIdentityTransform,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800521 clearRegion, supportProtectedContent, layer);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000522}
523
524bool Layer::prepareClientLayer(const RenderArea& /*renderArea*/, const Region& /*clip*/,
525 bool useIdentityTransform, Region& /*clearRegion*/,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800526 const bool /*supportProtectedContent*/,
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000527 renderengine::LayerSettings& layer) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800528 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000529 half alpha = getAlpha();
530 layer.geometry.boundaries = bounds;
531 if (useIdentityTransform) {
532 layer.geometry.positionTransform = mat4();
533 } else {
534 const ui::Transform transform = getTransform();
535 mat4 m;
536 m[0][0] = transform[0][0];
537 m[0][1] = transform[0][1];
538 m[0][3] = transform[0][2];
539 m[1][0] = transform[1][0];
540 m[1][1] = transform[1][1];
541 m[1][3] = transform[1][2];
542 m[3][0] = transform[2][0];
543 m[3][1] = transform[2][1];
544 m[3][3] = transform[2][2];
545 layer.geometry.positionTransform = m;
546 }
547
548 if (hasColorTransform()) {
549 layer.colorTransform = getColorTransform();
550 }
551
552 const auto roundedCornerState = getRoundedCornerState();
553 layer.geometry.roundedCornersRadius = roundedCornerState.radius;
554 layer.geometry.roundedCornersCrop = roundedCornerState.cropRect;
555
556 layer.alpha = alpha;
557 layer.sourceDataspace = mCurrentDataSpace;
558 return true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800559}
560
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800561Hwc2::IComposerClient::Composition Layer::getCompositionType(
562 const sp<const DisplayDevice>& display) const {
563 const auto outputLayer = findOutputLayerForDisplay(display);
564 LOG_FATAL_IF(!outputLayer);
565 return outputLayer->getState().hwc ? (*outputLayer->getState().hwc).hwcCompositionType
566 : Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800567}
568
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800569bool Layer::getClearClientTarget(const sp<const DisplayDevice>& display) const {
570 const auto outputLayer = findOutputLayerForDisplay(display);
571 LOG_FATAL_IF(!outputLayer);
572 return outputLayer->getState().clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800573}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800574
Dan Stozacac35382016-01-27 12:21:06 -0800575bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
576 if (point->getFrameNumber() <= mCurrentFrameNumber) {
577 // Don't bother with a SyncPoint, since we've already latched the
578 // relevant frame
579 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700580 }
Robert Carr2e102c92018-10-23 12:11:15 -0700581 if (isRemovedFromCurrentState()) {
582 return false;
583 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700584
Dan Stozacac35382016-01-27 12:21:06 -0800585 Mutex::Autolock lock(mLocalSyncPointMutex);
586 mLocalSyncPoints.push_back(point);
587 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700588}
589
Mathias Agopian13127d82013-03-05 17:47:11 -0800590// ----------------------------------------------------------------------------
591// local state
592// ----------------------------------------------------------------------------
593
David Sodman41fdfc92017-11-06 16:09:56 -0800594bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800595 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700596 return (s.flags & layer_state_t::eLayerSecure);
597}
598
Mathias Agopian13127d82013-03-05 17:47:11 -0800599void Layer::setVisibleRegion(const Region& visibleRegion) {
600 // always called from main thread
601 this->visibleRegion = visibleRegion;
602}
603
604void Layer::setCoveredRegion(const Region& coveredRegion) {
605 // always called from main thread
606 this->coveredRegion = coveredRegion;
607}
608
David Sodman41fdfc92017-11-06 16:09:56 -0800609void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800610 // always called from main thread
611 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
612}
613
Robert Carre5f4f692018-01-12 13:12:28 -0800614void Layer::clearVisibilityRegions() {
615 visibleRegion.clear();
616 visibleNonTransparentRegion.clear();
617 coveredRegion.clear();
618}
619
Mathias Agopian13127d82013-03-05 17:47:11 -0800620// ----------------------------------------------------------------------------
621// transaction
622// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800623
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800624void Layer::pushPendingState() {
625 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700626 return;
627 }
Alec Mourie60041e2019-06-14 18:59:51 -0700628 ATRACE_CALL();
Dan Stoza7dde5992015-05-22 09:51:44 -0700629
Dan Stoza7dde5992015-05-22 09:51:44 -0700630 // If this transaction is waiting on the receipt of a frame, generate a sync
631 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700632 // We don't allow installing sync points after we are removed from the current state
633 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800634 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
635 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800636 if (barrierLayer == nullptr) {
637 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700638 // If we can't promote the layer we are intended to wait on,
639 // then it is expired or otherwise invalid. Allow this transaction
640 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800641 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800642 } else {
chaviw43cb3cb2019-05-31 15:23:41 -0700643 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy, this);
Robert Carr0d480722017-01-10 16:42:54 -0800644 if (barrierLayer->addSyncPoint(syncPoint)) {
Alec Mourie60041e2019-06-14 18:59:51 -0700645 std::stringstream ss;
646 ss << "Adding sync point " << mCurrentState.frameNumber_legacy;
647 ATRACE_NAME(ss.str().c_str());
Dan Stozacac35382016-01-27 12:21:06 -0800648 mRemoteSyncPoints.push_back(std::move(syncPoint));
649 } else {
650 // We already missed the frame we're supposed to synchronize
651 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800652 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800653 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700654 }
655
Dan Stoza7dde5992015-05-22 09:51:44 -0700656 // Wake us up to check if the frame has been received
657 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700658 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700659 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800660 mPendingStates.push_back(mCurrentState);
661 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700662}
663
Pablo Ceballos05289c22016-04-14 15:49:55 -0700664void Layer::popPendingState(State* stateToCommit) {
Alec Mourie60041e2019-06-14 18:59:51 -0700665 ATRACE_CALL();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800666 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700667
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800668 mPendingStates.removeAt(0);
669 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700670}
671
Pablo Ceballos05289c22016-04-14 15:49:55 -0700672bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700673 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800674 while (!mPendingStates.empty()) {
675 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700676 if (mRemoteSyncPoints.empty()) {
677 // If we don't have a sync point for this, apply it anyway. It
678 // will be visually wrong, but it should keep us from getting
679 // into too much trouble.
680 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700681 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700682 stateUpdateAvailable = true;
683 continue;
684 }
685
Marissa Wallf58c14b2018-07-24 10:50:43 -0700686 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800687 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800688 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800689
690 // Signal our end of the sync point and then dispose of it
691 mRemoteSyncPoints.front()->setTransactionApplied();
692 mRemoteSyncPoints.pop_front();
693 continue;
694 }
695
Dan Stoza7dde5992015-05-22 09:51:44 -0700696 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
Alec Mourie60041e2019-06-14 18:59:51 -0700697 ATRACE_NAME("frameIsAvailable");
Dan Stoza7dde5992015-05-22 09:51:44 -0700698 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700699 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700700 stateUpdateAvailable = true;
701
702 // Signal our end of the sync point and then dispose of it
703 mRemoteSyncPoints.front()->setTransactionApplied();
704 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800705 } else {
Alec Mourie60041e2019-06-14 18:59:51 -0700706 ATRACE_NAME("!frameIsAvailable");
Dan Stoza792e5292016-02-11 11:43:58 -0800707 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700708 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700709 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700710 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700711 stateUpdateAvailable = true;
712 }
713 }
714
715 // If we still have pending updates, wake SurfaceFlinger back up and point
716 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800717 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700718 setTransactionFlags(eTransactionNeeded);
719 mFlinger->setTransactionFlags(eTraversalNeeded);
720 }
721
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800722 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700723 return stateUpdateAvailable;
724}
725
Marissa Wall61c58622018-07-18 10:12:20 -0700726uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000727 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800728
Marissa Wall61c58622018-07-18 10:12:20 -0700729 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
730 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700731
David Sodmaneb085e02017-10-05 18:49:04 -0700732 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700733 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000734 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800735 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
736 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
737 " requested={ wh={%4u,%4u} }}\n"
738 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
739 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700740 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700741 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
742 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
743 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
744 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
745 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700746 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
747 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
748 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800749 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700750
Robert Carre392b552017-09-19 12:16:05 -0700751 // Don't let Layer::doTransaction update the drawing state
752 // if we have a pending resize, unless we are in fixed-size mode.
753 // the drawing state will be updated only once we receive a buffer
754 // with the correct size.
755 //
756 // In particular, we want to make sure the clip (which is part
757 // of the geometry state) is latched together with the size but is
758 // latched immediately when no resizing is involved.
759 //
760 // If a sideband stream is attached, however, we want to skip this
761 // optimization so that transactions aren't missed when a buffer
762 // never arrives
763 //
764 // In the case that we don't have a buffer we ignore other factors
765 // and avoid entering the resizePending state. At a high level the
766 // resizePending state is to avoid applying the state of the new buffer
767 // to the old buffer. However in the state where we don't have an old buffer
768 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700769 const bool resizePending =
770 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
771 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
Lloyd Pique0b785d82018-12-04 17:25:27 -0800772 (mActiveBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700773 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800774 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700775 flags |= eDontUpdateGeometryState;
776 }
777 }
778
Robert Carr7bf247e2017-05-18 14:02:49 -0700779 // Here we apply various requested geometry states, depending on our
780 // latching configuration. See Layer.h for a detailed discussion of
781 // how geometry latching is controlled.
782 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000783 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700784
785 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
786 // mode, which causes attributes which normally latch regardless of scaling mode,
787 // to be delayed. We copy the requested state to the active state making sure
788 // to respect these rules (again see Layer.h for a detailed discussion).
789 //
790 // There is an awkward asymmetry in the handling of the crop states in the position
791 // states, as can be seen below. Largely this arises from position and transform
792 // being stored in the same data structure while having different latching rules.
793 // b/38182305
794 //
Marissa Wall61c58622018-07-18 10:12:20 -0700795 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700796 // applyPendingStates in the presence of deferred transactions.
797 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -0700798 float tx = stateToCommit->active_legacy.transform.tx();
799 float ty = stateToCommit->active_legacy.transform.ty();
800 stateToCommit->active_legacy = stateToCommit->requested_legacy;
801 stateToCommit->active_legacy.transform.set(tx, ty);
802 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700803 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700804 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -0700805 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700806 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800807 }
808
Marissa Wall61c58622018-07-18 10:12:20 -0700809 return flags;
810}
811
812uint32_t Layer::doTransaction(uint32_t flags) {
813 ATRACE_CALL();
814
chaviw5aedec92018-10-22 10:40:38 -0700815 if (mLayerDetached) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800816 return flags;
817 }
818
819 if (mChildrenChanged) {
820 flags |= eVisibleRegion;
821 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -0700822 }
823
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800824 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +0000825 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -0700826 if (!applyPendingStates(&c)) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800827 return flags;
Marissa Wall61c58622018-07-18 10:12:20 -0700828 }
829
830 flags = doTransactionResize(flags, &c);
831
Alec Mourib416efd2018-09-06 21:01:59 +0000832 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700833
834 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800835 // invalidate and recompute the visible regions if needed
836 flags |= Layer::eVisibleRegion;
837 }
838
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700839 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800840 // invalidate and recompute the visible regions if needed
841 flags |= eVisibleRegion;
842 this->contentDirty = true;
843
844 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -0700845 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -0700846 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -0800847 }
848
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800849 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -0700850 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800851 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -0700852 }
853
Mathias Agopian13127d82013-03-05 17:47:11 -0800854 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -0700855 commitTransaction(c);
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800856 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -0800857 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800858}
859
Pablo Ceballos05289c22016-04-14 15:49:55 -0700860void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800861 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700862}
863
Mathias Agopian13127d82013-03-05 17:47:11 -0800864uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800865 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800866}
867
868uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800869 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -0800870}
871
Robert Carr82364e32016-05-15 11:27:47 -0700872bool Layer::setPosition(float x, float y, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800873 if (mCurrentState.requested_legacy.transform.tx() == x &&
874 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -0800875 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800876 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -0700877
878 // We update the requested and active position simultaneously because
879 // we want to apply the position portion of the transform matrix immediately,
880 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800881 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -0700882 if (immediate && !mFreezeGeometryUpdates) {
883 // Here we directly update the active state
884 // unlike other setters, because we store it within
885 // the transform, but use different latching rules.
886 // b/38182305
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800887 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -0700888 }
Robert Carr7bf247e2017-05-18 14:02:49 -0700889 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -0700890
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800891 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800892 setTransactionFlags(eTransactionNeeded);
893 return true;
894}
Robert Carr82364e32016-05-15 11:27:47 -0700895
Robert Carr1f0a16a2016-10-24 16:27:39 -0700896bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
897 ssize_t idx = mCurrentChildren.indexOf(childLayer);
898 if (idx < 0) {
899 return false;
900 }
901 if (childLayer->setLayer(z)) {
902 mCurrentChildren.removeAt(idx);
903 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800904 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700905 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800906 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700907}
908
Robert Carr503c7042017-09-27 15:06:08 -0700909bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
910 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
911 ssize_t idx = mCurrentChildren.indexOf(childLayer);
912 if (idx < 0) {
913 return false;
914 }
915 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
916 mCurrentChildren.removeAt(idx);
917 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800918 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700919 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800920 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700921}
922
Robert Carrae060832016-11-28 10:51:00 -0800923bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800924 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
925 mCurrentState.sequence++;
926 mCurrentState.z = z;
927 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700928
929 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800930 if (mCurrentState.zOrderRelativeOf != nullptr) {
931 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700932 if (strongRelative != nullptr) {
933 strongRelative->removeZOrderRelative(this);
934 }
chaviw606e5cf2019-03-01 10:12:10 -0800935 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -0700936 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800937 setTransactionFlags(eTransactionNeeded);
938 return true;
939}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700940
Robert Carrdb66e622017-04-10 16:55:57 -0700941void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800942 mCurrentState.zOrderRelatives.remove(relative);
943 mCurrentState.sequence++;
944 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700945 setTransactionFlags(eTransactionNeeded);
946}
947
948void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800949 mCurrentState.zOrderRelatives.add(relative);
950 mCurrentState.modified = true;
951 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -0700952 setTransactionFlags(eTransactionNeeded);
953}
954
chaviw606e5cf2019-03-01 10:12:10 -0800955void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
956 mCurrentState.zOrderRelativeOf = relativeOf;
957 mCurrentState.sequence++;
958 mCurrentState.modified = true;
959 setTransactionFlags(eTransactionNeeded);
960}
961
Robert Carr503d2bd2017-12-04 15:49:47 -0800962bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -0700963 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
964 if (handle == nullptr) {
965 return false;
966 }
967 sp<Layer> relative = handle->owner.promote();
968 if (relative == nullptr) {
969 return false;
970 }
971
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800972 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
973 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -0800974 return false;
975 }
976
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800977 mCurrentState.sequence++;
978 mCurrentState.modified = true;
979 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -0700980
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800981 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -0700982 if (oldZOrderRelativeOf != nullptr) {
983 oldZOrderRelativeOf->removeZOrderRelative(this);
984 }
chaviw606e5cf2019-03-01 10:12:10 -0800985 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -0700986 relative->addZOrderRelative(this);
987
988 setTransactionFlags(eTransactionNeeded);
989
990 return true;
991}
992
Mathias Agopian13127d82013-03-05 17:47:11 -0800993bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800994 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -0700995 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800996 mCurrentState.requested_legacy.w = w;
997 mCurrentState.requested_legacy.h = h;
998 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800999 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001000
1001 // record the new size, from this point on, when the client request
1002 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001003 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001004 return true;
1005}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001006bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001007 if (mCurrentState.color.a == alpha) return false;
1008 mCurrentState.sequence++;
1009 mCurrentState.color.a = alpha;
1010 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001011 setTransactionFlags(eTransactionNeeded);
1012 return true;
1013}
chaviw13fdc492017-06-27 12:40:18 -07001014
Valerie Haudd0b7572019-01-29 14:59:27 -08001015bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1016 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001017 return false;
Valerie Hauaa194562019-02-05 16:21:38 -08001018 }
1019 mCurrentState.sequence++;
1020 mCurrentState.modified = true;
1021 setTransactionFlags(eTransactionNeeded);
1022
1023 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001024 // create background color layer if one does not yet exist
1025 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
1026 const String8& name = mName + "BackgroundColorLayer";
Evan Roskya1f1e152019-01-24 16:17:46 -08001027 mCurrentState.bgColorLayer = new ColorLayer(
1028 LayerCreationArgs(mFlinger.get(), nullptr, name, 0, 0, flags, LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -07001029
Valerie Haudd0b7572019-01-29 14:59:27 -08001030 // add to child list
1031 addChild(mCurrentState.bgColorLayer);
1032 mFlinger->mLayersAdded = true;
1033 // set up SF to handle added color layer
1034 if (isRemovedFromCurrentState()) {
1035 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1036 }
1037 mFlinger->setTransactionFlags(eTransactionNeeded);
1038 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1039 mCurrentState.bgColorLayer->reparent(nullptr);
1040 mCurrentState.bgColorLayer = nullptr;
1041 return true;
1042 }
1043
1044 mCurrentState.bgColorLayer->setColor(color);
1045 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1046 mCurrentState.bgColorLayer->setAlpha(alpha);
1047 mCurrentState.bgColorLayer->setDataspace(dataspace);
1048
chaviw13fdc492017-06-27 12:40:18 -07001049 return true;
1050}
1051
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001052bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001053 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001054
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001055 mCurrentState.sequence++;
1056 mCurrentState.cornerRadius = cornerRadius;
1057 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001058 setTransactionFlags(eTransactionNeeded);
1059 return true;
1060}
1061
Robert Carrd4ae7f32018-06-07 16:10:57 -07001062bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1063 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001064 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001065 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1066
1067 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1068 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1069 return false;
1070 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001071 mCurrentState.sequence++;
1072 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1073 matrix.dsdy);
1074 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001075 setTransactionFlags(eTransactionNeeded);
1076 return true;
1077}
Marissa Wall61c58622018-07-18 10:12:20 -07001078
Mathias Agopian13127d82013-03-05 17:47:11 -08001079bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001080 mCurrentState.requestedTransparentRegion_legacy = transparent;
1081 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001082 setTransactionFlags(eTransactionNeeded);
1083 return true;
1084}
1085bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001086 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1087 if (mCurrentState.flags == newFlags) return false;
1088 mCurrentState.sequence++;
1089 mCurrentState.flags = newFlags;
1090 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001091 setTransactionFlags(eTransactionNeeded);
1092 return true;
1093}
Robert Carr99e27f02016-06-16 15:18:02 -07001094
Marissa Wallf58c14b2018-07-24 10:50:43 -07001095bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001096 if (mCurrentState.requestedCrop_legacy == crop) return false;
1097 mCurrentState.sequence++;
1098 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001099 if (immediate && !mFreezeGeometryUpdates) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001100 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001101 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001102 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1103
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001104 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001105 setTransactionFlags(eTransactionNeeded);
1106 return true;
1107}
Robert Carr8d5227b2017-03-16 15:41:03 -07001108
Robert Carrc3574f72016-03-24 12:19:32 -07001109bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001110 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001111 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001112 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001113 return true;
1114}
1115
Evan Roskyef876c92019-01-25 17:46:06 -08001116bool Layer::setMetadata(const LayerMetadata& data) {
1117 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001118 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001119 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001120 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001121 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001122}
1123
Mathias Agopian13127d82013-03-05 17:47:11 -08001124bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001125 if (mCurrentState.layerStack == layerStack) return false;
1126 mCurrentState.sequence++;
1127 mCurrentState.layerStack = layerStack;
1128 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001129 setTransactionFlags(eTransactionNeeded);
1130 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001131}
1132
Peiyong Linc502cb72019-03-01 15:00:23 -08001133bool Layer::setColorSpaceAgnostic(const bool agnostic) {
1134 if (mCurrentState.colorSpaceAgnostic == agnostic) {
1135 return false;
1136 }
1137 mCurrentState.sequence++;
1138 mCurrentState.colorSpaceAgnostic = agnostic;
1139 mCurrentState.modified = true;
1140 setTransactionFlags(eTransactionNeeded);
1141 return true;
1142}
1143
Robert Carr1f0a16a2016-10-24 16:27:39 -07001144uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001145 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001146 if (p == nullptr) {
1147 return getDrawingState().layerStack;
1148 }
1149 return p->getLayerStack();
1150}
1151
Marissa Wallf58c14b2018-07-24 10:50:43 -07001152void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Alec Mourie60041e2019-06-14 18:59:51 -07001153 ATRACE_CALL();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001154 mCurrentState.barrierLayer_legacy = barrierLayer;
1155 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001156 // We don't set eTransactionNeeded, because just receiving a deferral
1157 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001158 mCurrentState.modified = true;
1159 pushPendingState();
1160 mCurrentState.barrierLayer_legacy = nullptr;
1161 mCurrentState.frameNumber_legacy = 0;
1162 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001163}
1164
Marissa Wallf58c14b2018-07-24 10:50:43 -07001165void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001166 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001167 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001168}
1169
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001170// ----------------------------------------------------------------------------
1171// pageflip handling...
1172// ----------------------------------------------------------------------------
1173
Robert Carr1f0a16a2016-10-24 16:27:39 -07001174bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001175 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001176 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001177 if (parent != nullptr && parent->isHiddenByPolicy()) {
1178 return true;
1179 }
Robert Carr1c5481e2019-07-01 14:42:27 -07001180 if (usingRelativeZ(LayerVector::StateSet::Drawing)) {
1181 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
1182 if (zOrderRelativeOf != nullptr) {
1183 if (zOrderRelativeOf->isHiddenByPolicy()) {
1184 return true;
1185 }
1186 }
1187 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001188 return s.flags & layer_state_t::eLayerHidden;
1189}
1190
David Sodman41fdfc92017-11-06 16:09:56 -08001191uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001192 // TODO: should we do something special if mSecure is set?
1193 if (mProtectedByApp) {
1194 // need a hardware-protected path to external video sink
1195 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001196 }
Riley Andrews03414a12014-07-01 14:22:59 -07001197 if (mPotentialCursor) {
1198 usage |= GraphicBuffer::USAGE_CURSOR;
1199 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001200 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001201 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001202}
1203
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001204void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001205 uint32_t orientation = 0;
Vishnu Nair5eb3f062019-04-08 08:21:03 -07001206 // Disable setting transform hint if the debug flag is set.
1207 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001208 // The transform hint is used to improve performance, but we can
1209 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001210 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001211 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001212 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001213 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001214 orientation = 0;
1215 }
1216 }
David Sodmaneb085e02017-10-05 18:49:04 -07001217 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001218}
1219
Mathias Agopian13127d82013-03-05 17:47:11 -08001220// ----------------------------------------------------------------------------
1221// debugging
1222// ----------------------------------------------------------------------------
1223
Marissa Wall61c58622018-07-18 10:12:20 -07001224// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001225LayerDebugInfo Layer::getLayerDebugInfo() const {
1226 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001227 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001228 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001229 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001230 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
Yiwei Zhang5434a782018-12-05 18:06:32 -08001231 info.mType = std::string(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001232 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001233 info.mVisibleRegion = visibleRegion;
1234 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001235 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001236 info.mX = ds.active_legacy.transform.tx();
1237 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001238 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001239 info.mWidth = ds.active_legacy.w;
1240 info.mHeight = ds.active_legacy.h;
1241 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001242 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001243 info.mFlags = ds.flags;
1244 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001245 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001246 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1247 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1248 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1249 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001250 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001251 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001252 if (buffer != 0) {
1253 info.mActiveBufferWidth = buffer->getWidth();
1254 info.mActiveBufferHeight = buffer->getHeight();
1255 info.mActiveBufferStride = buffer->getStride();
1256 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001257 } else {
1258 info.mActiveBufferWidth = 0;
1259 info.mActiveBufferHeight = 0;
1260 info.mActiveBufferStride = 0;
1261 info.mActiveBufferFormat = 0;
1262 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001263 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001264 info.mNumQueuedFrames = getQueuedFrameCount();
1265 info.mRefreshPending = isBufferLatched();
1266 info.mIsOpaque = isOpaque(ds);
1267 info.mContentDirty = contentDirty;
1268 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001269}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001270
Yiwei Zhang5434a782018-12-05 18:06:32 -08001271void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001272 result.append("-------------------------------");
1273 result.append("-------------------------------");
1274 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001275 result.append(" Layer name\n");
1276 result.append(" Z | ");
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001277 result.append(" Window Type | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001278 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001279 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001280 result.append(" Disp Frame (LTRB) | ");
1281 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001282 result.append("-------------------------------");
1283 result.append("-------------------------------");
1284 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001285}
1286
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001287void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1288 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1289 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001290 return;
1291 }
1292
Yiwei Zhang5434a782018-12-05 18:06:32 -08001293 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001294 if (mName.length() > 77) {
1295 std::string shortened;
1296 shortened.append(mName.string(), 36);
1297 shortened.append("[...]");
1298 shortened.append(mName.string() + (mName.length() - 36), 36);
Yiwei Zhang5434a782018-12-05 18:06:32 -08001299 name = shortened;
Dan Stozae22aec72016-08-01 13:20:59 -07001300 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001301 name = std::string(mName.string(), mName.size());
Dan Stozae22aec72016-08-01 13:20:59 -07001302 }
1303
Yiwei Zhang5434a782018-12-05 18:06:32 -08001304 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001305
Alec Mourib416efd2018-09-06 21:01:59 +00001306 const State& layerState(getDrawingState());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001307 const auto& compositionState = outputLayer->getState();
1308
Chia-I Wu1e043612018-03-01 09:45:09 -08001309 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001310 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001311 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001312 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001313 }
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001314 StringAppendF(&result, " %10d | ", mWindowType);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001315 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
1316 StringAppendF(&result, "%10s | ",
1317 toString(getCompositionLayer() ? compositionState.bufferTransform
1318 : static_cast<Hwc2::Transform>(0))
1319 .c_str());
1320 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001321 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001322 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001323 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1324 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001325
Yichi Chen6ca35192018-05-29 12:20:43 +08001326 result.append("- - - - - - - - - - - - - - - -");
1327 result.append("- - - - - - - - - - - - - - - -");
1328 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001329}
Dan Stozae22aec72016-08-01 13:20:59 -07001330
Yiwei Zhang5434a782018-12-05 18:06:32 -08001331void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001332 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001333}
1334
Svetoslavd85084b2014-03-20 10:28:31 -07001335void Layer::clearFrameStats() {
1336 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001337}
1338
Jamie Gennis6547ff42013-07-16 20:12:42 -07001339void Layer::logFrameStats() {
1340 mFrameTracker.logAndResetStats(mName);
1341}
1342
Svetoslavd85084b2014-03-20 10:28:31 -07001343void Layer::getFrameStats(FrameStats* outStats) const {
1344 mFrameTracker.getStats(outStats);
1345}
1346
Yiwei Zhang5434a782018-12-05 18:06:32 -08001347void Layer::dumpFrameEvents(std::string& result) {
1348 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001349 Mutex::Autolock lock(mFrameEventHistoryMutex);
1350 mFrameEventHistory.checkFencesForCompletion();
1351 mFrameEventHistory.dump(result);
1352}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001353
Brian Anderson5ea5e592016-12-01 16:54:33 -08001354void Layer::onDisconnect() {
1355 Mutex::Autolock lock(mFrameEventHistoryMutex);
1356 mFrameEventHistory.onDisconnect();
Yiwei Zhangaf8ee942018-11-22 00:15:23 -08001357 mFlinger->mTimeStats->onDestroy(getSequence());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001358}
1359
Brian Anderson3890c392016-07-25 12:48:08 -07001360void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001361 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001362 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001363 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1364 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001365 }
1366
Brian Andersond6927fb2016-07-23 23:37:30 -07001367 Mutex::Autolock lock(mFrameEventHistoryMutex);
1368 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001369 // If there are any unsignaled fences in the aquire timeline at this
1370 // point, the previously queued frame hasn't been latched yet. Go ahead
1371 // and try to get the signal time here so the syscall is taken out of
1372 // the main thread's critical path.
1373 mAcquireTimeline.updateSignalTimes();
1374 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001375 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001376 mFrameEventHistory.addQueue(*newTimestamps);
1377 }
1378
Brian Anderson3890c392016-07-25 12:48:08 -07001379 if (outDelta) {
1380 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001381 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001382}
Dan Stozae77c7662016-05-13 11:37:28 -07001383
Chia-I Wu98f1c102017-05-30 14:54:08 -07001384size_t Layer::getChildrenCount() const {
1385 size_t count = 0;
1386 for (const sp<Layer>& child : mCurrentChildren) {
1387 count += 1 + child->getChildrenCount();
1388 }
1389 return count;
1390}
1391
Robert Carr1f0a16a2016-10-24 16:27:39 -07001392void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001393 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001394 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001395
Robert Carr1f0a16a2016-10-24 16:27:39 -07001396 mCurrentChildren.add(layer);
1397 layer->setParent(this);
1398}
1399
1400ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001401 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001402 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001403
Robert Carr1323c952019-01-28 18:13:27 -08001404 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001405 return mCurrentChildren.remove(layer);
1406}
1407
Robert Carr1db73f62016-12-21 12:58:51 -08001408bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1409 sp<Handle> handle = nullptr;
1410 sp<Layer> newParent = nullptr;
1411 if (newParentHandle == nullptr) {
1412 return false;
1413 }
1414 handle = static_cast<Handle*>(newParentHandle.get());
1415 newParent = handle->owner.promote();
1416 if (newParent == nullptr) {
1417 ALOGE("Unable to promote Layer handle");
1418 return false;
1419 }
1420
chaviw5aedec92018-10-22 10:40:38 -07001421 if (attachChildren()) {
1422 setTransactionFlags(eTransactionNeeded);
1423 }
Robert Carr1db73f62016-12-21 12:58:51 -08001424 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001425 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001426 }
1427 mCurrentChildren.clear();
1428
1429 return true;
1430}
1431
Robert Carr15eae092018-03-23 13:43:53 -07001432void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001433 for (const sp<Layer>& child : mDrawingChildren) {
1434 child->mDrawingParent = newParent;
Vishnu Nairc652ff82019-03-15 12:48:54 -07001435 child->computeBounds(newParent->mBounds,
1436 newParent->getTransformWithScale(
1437 newParent->getBufferScaleTransform()));
Robert Carr578038f2018-03-09 12:25:24 -08001438 }
1439}
1440
chaviwf1961f72017-09-18 16:41:07 -07001441bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001442 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001443
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001444 // While layers are detached, we allow most operations
1445 // and simply halt performing the actual transaction. However
1446 // for reparent != null we would enter the mRemovedFromCurrentState
1447 // state, regardless of whether doTransaction was called, and
1448 // so we need to prevent the update here.
1449 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001450 return false;
1451 }
1452
Robert Carr54cf5b12019-01-25 14:02:28 -08001453 sp<Layer> newParent;
1454 if (newParentHandle != nullptr) {
1455 auto handle = static_cast<Handle*>(newParentHandle.get());
1456 newParent = handle->owner.promote();
1457 if (newParent == nullptr) {
1458 ALOGE("Unable to promote Layer handle");
1459 return false;
1460 }
1461 if (newParent == this) {
1462 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1463 return false;
1464 }
1465 }
1466
chaviwf1961f72017-09-18 16:41:07 -07001467 sp<Layer> parent = getParent();
1468 if (parent != nullptr) {
1469 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001470 }
1471
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001472 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001473 newParent->addChild(this);
1474 if (!newParent->isRemovedFromCurrentState()) {
1475 addToCurrentState();
1476 } else {
1477 onRemovedFromCurrentState();
1478 }
1479
1480 if (mLayerDetached) {
1481 mLayerDetached = false;
1482 callSetTransactionFlags = true;
1483 }
1484 } else {
1485 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001486 }
1487
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001488 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001489 setTransactionFlags(eTransactionNeeded);
1490 }
chaviw06178942017-07-27 10:25:59 -07001491 return true;
1492}
1493
Robert Carr9524cb32017-02-13 11:32:32 -08001494bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001495 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001496 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001497 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001498 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001499 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001500 child->detachChildren();
chaviw43cb3cb2019-05-31 15:23:41 -07001501 child->removeRemoteSyncPoints();
Robert Carr9524cb32017-02-13 11:32:32 -08001502 }
Robert Carr7f619b22017-11-06 12:56:35 -08001503 }
Robert Carr9524cb32017-02-13 11:32:32 -08001504
1505 return true;
1506}
1507
chaviw5aedec92018-10-22 10:40:38 -07001508bool Layer::attachChildren() {
1509 bool changed = false;
1510 for (const sp<Layer>& child : mCurrentChildren) {
1511 sp<Client> parentClient = mClientRef.promote();
1512 sp<Client> client(child->mClientRef.promote());
1513 if (client != nullptr && parentClient != client) {
1514 if (child->mLayerDetached) {
1515 child->mLayerDetached = false;
1516 changed = true;
1517 }
1518 changed |= child->attachChildren();
1519 }
1520 }
1521
1522 return changed;
1523}
1524
Peiyong Lind3788632018-09-18 16:01:31 -07001525bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001526 static const mat4 identityMatrix = mat4();
1527
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001528 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001529 return false;
1530 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001531 ++mCurrentState.sequence;
1532 mCurrentState.colorTransform = matrix;
1533 mCurrentState.hasColorTransform = matrix != identityMatrix;
1534 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001535 setTransactionFlags(eTransactionNeeded);
1536 return true;
1537}
1538
chaviwf66724d2018-11-28 16:35:21 -08001539mat4 Layer::getColorTransform() const {
1540 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1541 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1542 colorTransform = parent->getColorTransform() * colorTransform;
1543 }
1544 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001545}
1546
1547bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001548 bool hasColorTransform = getDrawingState().hasColorTransform;
1549 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1550 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1551 }
1552 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001553}
1554
Chia-I Wu11481472018-05-04 10:43:19 -07001555bool Layer::isLegacyDataSpace() const {
1556 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001557 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001558 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001559}
1560
Robert Carr1f0a16a2016-10-24 16:27:39 -07001561void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001562 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001563}
1564
Robert Carr1f0a16a2016-10-24 16:27:39 -07001565int32_t Layer::getZ() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001566 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001567}
1568
Robert Carr1c5481e2019-07-01 14:42:27 -07001569bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) const {
Robert Carr29abff82017-12-04 13:51:20 -08001570 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001571 const State& state = useDrawing ? mDrawingState : mCurrentState;
Robert Carr29abff82017-12-04 13:51:20 -08001572 return state.zOrderRelativeOf != nullptr;
1573}
1574
David Sodman41fdfc92017-11-06 16:09:56 -08001575__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001576 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001577 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1578 "makeTraversalList received invalid stateSet");
1579 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1580 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001581 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001582
Robert Carr29abff82017-12-04 13:51:20 -08001583 if (state.zOrderRelatives.size() == 0) {
1584 *outSkipRelativeZUsers = true;
1585 return children;
1586 }
1587
chaviwfd462612018-05-31 16:11:27 -07001588 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001589 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001590 sp<Layer> strongRelative = weakRelative.promote();
1591 if (strongRelative != nullptr) {
1592 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001593 }
1594 }
1595
Dan Stoza412903f2017-04-27 13:42:17 -07001596 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001597 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
Robert Carr503c7042017-09-27 15:06:08 -07001598 if (childState.zOrderRelativeOf != nullptr) {
1599 continue;
1600 }
Robert Carrdb66e622017-04-10 16:55:57 -07001601 traverse.add(child);
1602 }
1603
1604 return traverse;
1605}
1606
Robert Carr1f0a16a2016-10-24 16:27:39 -07001607/**
Robert Carrdb66e622017-04-10 16:55:57 -07001608 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001609 */
Dan Stoza412903f2017-04-27 13:42:17 -07001610void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001611 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1612 // produce a new list for traversing, including our relatives, and not including our children
1613 // who are relatives of another surface. In the case that there are no relative Z,
1614 // makeTraversalList returns our children directly to avoid significant overhead.
1615 // However in this case we need to take the responsibility for filtering children which
1616 // are relatives of another surface here.
1617 bool skipRelativeZUsers = false;
1618 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001619
Robert Carr1f0a16a2016-10-24 16:27:39 -07001620 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001621 for (; i < list.size(); i++) {
1622 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001623 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1624 continue;
1625 }
1626
Robert Carrdb66e622017-04-10 16:55:57 -07001627 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001628 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001629 }
Dan Stoza412903f2017-04-27 13:42:17 -07001630 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001631 }
Robert Carr29abff82017-12-04 13:51:20 -08001632
Dan Stoza412903f2017-04-27 13:42:17 -07001633 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001634 for (; i < list.size(); i++) {
1635 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001636
Robert Carr29abff82017-12-04 13:51:20 -08001637 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1638 continue;
1639 }
Dan Stoza412903f2017-04-27 13:42:17 -07001640 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001641 }
1642}
1643
1644/**
Robert Carrdb66e622017-04-10 16:55:57 -07001645 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001646 */
Dan Stoza412903f2017-04-27 13:42:17 -07001647void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1648 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001649 // See traverseInZOrder for documentation.
1650 bool skipRelativeZUsers = false;
1651 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001652
Robert Carr1f0a16a2016-10-24 16:27:39 -07001653 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001654 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001655 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001656
1657 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1658 continue;
1659 }
1660
Robert Carrdb66e622017-04-10 16:55:57 -07001661 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001662 break;
1663 }
Dan Stoza412903f2017-04-27 13:42:17 -07001664 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001665 }
Dan Stoza412903f2017-04-27 13:42:17 -07001666 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001667 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001668 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001669
1670 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1671 continue;
1672 }
1673
Dan Stoza412903f2017-04-27 13:42:17 -07001674 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001675 }
1676}
1677
chaviw4b129c22018-04-09 16:19:43 -07001678LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1679 const std::vector<Layer*>& layersInTree) {
1680 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1681 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001682 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1683 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001684 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001685
chaviwfd462612018-05-31 16:11:27 -07001686 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001687 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1688 sp<Layer> strongRelative = weakRelative.promote();
1689 // Only add relative layers that are also descendents of the top most parent of the tree.
1690 // If a relative layer is not a descendent, then it should be ignored.
1691 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1692 traverse.add(strongRelative);
1693 }
1694 }
1695
1696 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001697 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001698 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1699 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1700 // the child here since it won't be added later as a relative.
1701 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1702 childState.zOrderRelativeOf.promote().get())) {
1703 continue;
1704 }
1705 traverse.add(child);
1706 }
1707
1708 return traverse;
1709}
1710
1711void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1712 LayerVector::StateSet stateSet,
1713 const LayerVector::Visitor& visitor) {
1714 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001715
1716 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001717 for (; i < list.size(); i++) {
1718 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001719 if (relative->getZ() >= 0) {
1720 break;
1721 }
chaviw4b129c22018-04-09 16:19:43 -07001722 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001723 }
chaviw4b129c22018-04-09 16:19:43 -07001724
chaviwa76b2712017-09-20 12:02:26 -07001725 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001726 for (; i < list.size(); i++) {
1727 const auto& relative = list[i];
1728 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001729 }
1730}
1731
chaviw4b129c22018-04-09 16:19:43 -07001732std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1733 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1734 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1735
1736 std::vector<Layer*> layersInTree = {this};
1737 for (size_t i = 0; i < children.size(); i++) {
1738 const auto& child = children[i];
1739 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1740 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1741 }
1742
1743 return layersInTree;
1744}
1745
1746void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1747 const LayerVector::Visitor& visitor) {
1748 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1749 std::sort(layersInTree.begin(), layersInTree.end());
1750 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1751}
1752
Peiyong Linefefaac2018-08-17 12:27:51 -07001753ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001754 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001755}
1756
chaviw13fdc492017-06-27 12:40:18 -07001757half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001758 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001759
chaviw13fdc492017-06-27 12:40:18 -07001760 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1761 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001762}
Robert Carr6452f122017-03-21 10:41:29 -07001763
chaviw13fdc492017-06-27 12:40:18 -07001764half4 Layer::getColor() const {
1765 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001766 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001767}
Robert Carr6452f122017-03-21 10:41:29 -07001768
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001769Layer::RoundedCornerState Layer::getRoundedCornerState() const {
1770 const auto& p = mDrawingParent.promote();
1771 if (p != nullptr) {
Peiyong Lin27016a92019-03-29 17:36:08 +00001772 RoundedCornerState parentState = p->getRoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001773 if (parentState.radius > 0) {
1774 ui::Transform t = getActiveTransform(getDrawingState());
1775 t = t.inverse();
1776 parentState.cropRect = t.transform(parentState.cropRect);
1777 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1778 // but a transform matrix can define horizontal and vertical scales.
1779 // Let's take the average between both of them and pass into the shader, practically we
1780 // never do this type of transformation on windows anyway.
1781 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
1782 return parentState;
1783 }
1784 }
1785 const float radius = getDrawingState().cornerRadius;
Peiyong Linb9ff23e2019-04-08 11:04:59 -07001786 return radius > 0 && getCrop(getDrawingState()).isValid()
1787 ? RoundedCornerState(getCrop(getDrawingState()).toFloatRect(), radius)
1788 : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001789}
1790
Robert Carr1f0a16a2016-10-24 16:27:39 -07001791void Layer::commitChildList() {
1792 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1793 const auto& child = mCurrentChildren[i];
1794 child->commitChildList();
1795 }
1796 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001797 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001798}
1799
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001800static wp<Layer> extractLayerFromBinder(const wp<IBinder>& weakBinderHandle) {
1801 if (weakBinderHandle == nullptr) {
1802 return nullptr;
1803 }
1804 sp<IBinder> binderHandle = weakBinderHandle.promote();
1805 if (binderHandle == nullptr) {
1806 return nullptr;
1807 }
1808 sp<Layer::Handle> handle = static_cast<Layer::Handle*>(binderHandle.get());
1809 if (handle == nullptr) {
1810 return nullptr;
1811 }
1812 return handle->owner;
1813}
1814
Robert Carr720e5062018-07-30 17:45:14 -07001815void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001816 mCurrentState.inputInfo = info;
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001817 mCurrentState.touchableRegionCrop = extractLayerFromBinder(info.touchableRegionCropHandle);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001818 mCurrentState.modified = true;
1819 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07001820 setTransactionFlags(eTransactionNeeded);
1821}
1822
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001823void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet,
1824 uint32_t traceFlags) {
chaviw1d044282017-09-27 12:19:28 -07001825 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1826 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001827 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07001828
Peiyong Linefefaac2018-08-17 12:27:51 -07001829 ui::Transform requestedTransform = state.active_legacy.transform;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001830 ui::Transform transform = getTransform();
chaviw1d044282017-09-27 12:19:28 -07001831
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001832 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
1833 layerInfo->set_id(sequence);
1834 layerInfo->set_name(getName().c_str());
1835 layerInfo->set_type(String8(getTypeId()));
chaviw1d044282017-09-27 12:19:28 -07001836
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001837 for (const auto& child : children) {
1838 layerInfo->add_children(child->sequence);
chaviw1d044282017-09-27 12:19:28 -07001839 }
chaviw1d044282017-09-27 12:19:28 -07001840
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001841 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1842 sp<Layer> strongRelative = weakRelative.promote();
1843 if (strongRelative != nullptr) {
1844 layerInfo->add_relatives(strongRelative->sequence);
1845 }
chaviwadc40c22018-07-10 16:57:27 -07001846 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001847
1848 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
1849 [&]() { return layerInfo->mutable_transparent_region(); });
1850 LayerProtoHelper::writeToProto(visibleRegion,
1851 [&]() { return layerInfo->mutable_visible_region(); });
1852 LayerProtoHelper::writeToProto(surfaceDamageRegion,
1853 [&]() { return layerInfo->mutable_damage_region(); });
1854
1855 layerInfo->set_layer_stack(getLayerStack());
1856 layerInfo->set_z(state.z);
1857
1858 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
1859 [&]() { return layerInfo->mutable_position(); });
1860
1861 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(),
1862 [&]() {
1863 return layerInfo->mutable_requested_position();
1864 });
1865
1866 LayerProtoHelper::writeSizeToProto(state.active_legacy.w, state.active_legacy.h,
1867 [&]() { return layerInfo->mutable_size(); });
1868
1869 LayerProtoHelper::writeToProto(state.crop_legacy,
1870 [&]() { return layerInfo->mutable_crop(); });
1871 layerInfo->set_corner_radius(getRoundedCornerState().radius);
1872
1873 layerInfo->set_is_opaque(isOpaque(state));
1874 layerInfo->set_invalidate(contentDirty);
Peiyong Lin51598552019-04-17 14:09:22 -07001875 layerInfo->set_is_protected(isProtected());
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001876
1877 // XXX (b/79210409) mCurrentDataSpace is not protected
1878 layerInfo->set_dataspace(
1879 dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
1880
1881 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1882 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
1883 LayerProtoHelper::writeToProto(state.color,
1884 [&]() { return layerInfo->mutable_requested_color(); });
1885 layerInfo->set_flags(state.flags);
1886
1887 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1888 LayerProtoHelper::writeToProto(requestedTransform,
1889 layerInfo->mutable_requested_transform());
1890
1891 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
1892 if (parent != nullptr) {
1893 layerInfo->set_parent(parent->sequence);
1894 } else {
1895 layerInfo->set_parent(-1);
1896 }
1897
1898 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1899 if (zOrderRelativeOf != nullptr) {
1900 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1901 } else {
1902 layerInfo->set_z_order_relative_of(-1);
1903 }
1904
1905 auto buffer = mActiveBuffer;
1906 if (buffer != nullptr) {
1907 LayerProtoHelper::writeToProto(buffer,
1908 [&]() { return layerInfo->mutable_active_buffer(); });
1909 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
1910 layerInfo->mutable_buffer_transform());
1911 }
1912
1913 layerInfo->set_queued_frames(getQueuedFrameCount());
1914 layerInfo->set_refresh_pending(isBufferLatched());
1915 layerInfo->set_curr_frame(mCurrentFrameNumber);
1916 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
1917
1918 for (const auto& pendingState : mPendingStates) {
1919 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
1920 if (barrierLayer != nullptr) {
1921 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
1922 barrierLayerProto->set_id(barrierLayer->sequence);
1923 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
1924 }
1925 }
1926 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
chaviwadc40c22018-07-10 16:57:27 -07001927 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001928
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001929 if (traceFlags & SurfaceTracing::TRACE_INPUT) {
1930 LayerProtoHelper::writeToProto(state.inputInfo, state.touchableRegionCrop,
1931 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001932 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001933
1934 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
1935 auto protoMap = layerInfo->mutable_metadata();
1936 for (const auto& entry : state.metadata.mMap) {
1937 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
1938 }
1939 LayerProtoHelper::writeToProto(mEffectiveTransform,
1940 layerInfo->mutable_effective_transform());
1941 LayerProtoHelper::writeToProto(mSourceBounds,
1942 [&]() { return layerInfo->mutable_source_bounds(); });
1943 LayerProtoHelper::writeToProto(mScreenBounds,
1944 [&]() { return layerInfo->mutable_screen_bounds(); });
1945 }
chaviw1d044282017-09-27 12:19:28 -07001946}
1947
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001948void Layer::writeToProto(LayerProto* layerInfo, const sp<DisplayDevice>& displayDevice,
1949 uint32_t traceFlags) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001950 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1951 if (!outputLayer) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07001952 return;
1953 }
1954
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001955 writeToProto(layerInfo, LayerVector::StateSet::Drawing, traceFlags);
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001956
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001957 const auto& compositionState = outputLayer->getState();
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001958
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001959 const Rect& frame = compositionState.displayFrame;
Nataniel Borges797b0e42019-02-15 14:11:58 -08001960 LayerProtoHelper::writeToProto(frame, [&]() { return layerInfo->mutable_hwc_frame(); });
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001961
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001962 const FloatRect& crop = compositionState.sourceCrop;
Nataniel Borges797b0e42019-02-15 14:11:58 -08001963 LayerProtoHelper::writeToProto(crop, [&]() { return layerInfo->mutable_hwc_crop(); });
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001964
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001965 const int32_t transform =
1966 getCompositionLayer() ? static_cast<int32_t>(compositionState.bufferTransform) : 0;
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001967 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08001968
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001969 const int32_t compositionType =
1970 static_cast<int32_t>(compositionState.hwc ? (*compositionState.hwc).hwcCompositionType
1971 : Hwc2::IComposerClient::Composition::CLIENT);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08001972 layerInfo->set_hwc_composition_type(compositionType);
1973
1974 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
1975 static_cast<BufferLayer*>(this)->isProtected()) {
1976 layerInfo->set_is_protected(true);
1977 } else {
1978 layerInfo->set_is_protected(false);
1979 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001980}
1981
Robert Carr2e102c92018-10-23 12:11:15 -07001982bool Layer::isRemovedFromCurrentState() const {
1983 return mRemovedFromCurrentState;
1984}
1985
Ady Abraham38172ad2019-07-16 16:10:28 -07001986// Debug helper for b/137560795
1987#define INT32_MIGHT_OVERFLOW(n) (((n) >= INT32_MAX / 2) || ((n) <= INT32_MIN / 2))
1988
1989#define RECT_BOUNDS_INVALID(rect) \
1990 (INT32_MIGHT_OVERFLOW((rect).left) || INT32_MIGHT_OVERFLOW((rect).right) || \
1991 INT32_MIGHT_OVERFLOW((rect).bottom) || INT32_MIGHT_OVERFLOW((rect).top))
1992
Arthur Hungd20b2702019-01-14 18:16:16 +08001993InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001994 InputWindowInfo info = mDrawingState.inputInfo;
Robert Carr720e5062018-07-30 17:45:14 -07001995
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001996 if (info.displayId == ADISPLAY_ID_NONE) {
1997 info.displayId = mDrawingState.layerStack;
1998 }
1999
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002000 ui::Transform t = getTransform();
2001 const float xScale = t.sx();
2002 const float yScale = t.sy();
Arthur Hung118b1142019-05-08 21:25:59 +08002003 float xSurfaceInset = info.surfaceInset;
2004 float ySurfaceInset = info.surfaceInset;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002005 if (xScale != 1.0f || yScale != 1.0f) {
2006 info.windowXScale *= 1.0f / xScale;
2007 info.windowYScale *= 1.0f / yScale;
2008 info.touchableRegion.scaleSelf(xScale, yScale);
Arthur Hung118b1142019-05-08 21:25:59 +08002009 xSurfaceInset *= xScale;
2010 ySurfaceInset *= yScale;
Riddle Hsu39d4aa52018-11-30 20:46:53 +08002011 }
Robert Carre07e1032018-11-26 12:55:53 -08002012
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002013 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002014 // If this is a portal window, set the touchableRegion to the layerBounds.
2015 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
2016 ? getBufferSize(getDrawingState())
2017 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002018 if (!layerBounds.isValid()) {
2019 layerBounds = getCroppedBufferSize(getDrawingState());
2020 }
Vishnu Nair8033a492018-12-05 07:27:23 -08002021 layerBounds = t.transform(layerBounds);
Ady Abraham38172ad2019-07-16 16:10:28 -07002022
2023 // debug check for b/137560795
2024 {
2025 if (RECT_BOUNDS_INVALID(layerBounds)) {
2026 ALOGE("layer %s bounds are invalid (%" PRIi32 ", %" PRIi32 ", %" PRIi32 ", %" PRIi32
2027 ")",
2028 mName.c_str(), layerBounds.left, layerBounds.top, layerBounds.right,
2029 layerBounds.bottom);
2030 std::string out;
2031 getTransform().dump(out, "Transform");
2032 ALOGE("%s", out.c_str());
2033 layerBounds.left = layerBounds.top = layerBounds.right = layerBounds.bottom = 0;
2034 }
2035
2036 if (INT32_MIGHT_OVERFLOW(xSurfaceInset) || INT32_MIGHT_OVERFLOW(ySurfaceInset)) {
2037 ALOGE("layer %s surface inset are invalid (%" PRIi32 ", %" PRIi32 ")", mName.c_str(),
2038 int32_t(xSurfaceInset), int32_t(ySurfaceInset));
2039 xSurfaceInset = ySurfaceInset = 0;
2040 }
2041 }
Arthur Hung118b1142019-05-08 21:25:59 +08002042 layerBounds.inset(xSurfaceInset, ySurfaceInset, xSurfaceInset, ySurfaceInset);
Vishnu Nair8033a492018-12-05 07:27:23 -08002043
Arthur Hungd20b2702019-01-14 18:16:16 +08002044 // Input coordinate should match the layer bounds.
2045 info.frameLeft = layerBounds.left;
2046 info.frameTop = layerBounds.top;
2047 info.frameRight = layerBounds.right;
2048 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08002049
2050 // Position the touchable region relative to frame screen location and restrict it to frame
2051 // bounds.
2052 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08002053 info.visible = canReceiveInput();
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002054
2055 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2056 if (info.replaceTouchableRegionWithCrop) {
2057 if (cropLayer == nullptr) {
2058 info.touchableRegion = Region(Rect{mScreenBounds});
2059 } else {
2060 info.touchableRegion = Region(Rect{cropLayer->mScreenBounds});
2061 }
2062 } else if (cropLayer != nullptr) {
2063 info.touchableRegion = info.touchableRegion.intersect(Rect{cropLayer->mScreenBounds});
2064 }
2065
Robert Carr720e5062018-07-30 17:45:14 -07002066 return info;
2067}
2068
2069bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002070 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002071}
2072
Lloyd Piquefeb73d72018-12-04 17:23:44 -08002073std::shared_ptr<compositionengine::Layer> Layer::getCompositionLayer() const {
2074 return nullptr;
2075}
2076
chaviw3e727cd2019-01-31 13:41:05 -08002077bool Layer::canReceiveInput() const {
2078 return isVisible();
2079}
2080
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002081compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
2082 const sp<const DisplayDevice>& display) const {
2083 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionLayer().get());
2084}
2085
Mathias Agopian13127d82013-03-05 17:47:11 -08002086// ---------------------------------------------------------------------------
2087
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002088}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002089
2090#if defined(__gl_h_)
2091#error "don't include gl/gl.h in this file"
2092#endif
2093
2094#if defined(__gl2_h_)
2095#error "don't include gl2/gl2.h in this file"
2096#endif