blob: 106d505faae671ab1fadcd14dd4a3448ad49e1c4 [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"
Mikael Pessa90092f42019-08-26 17:22:04 -070060#include "FrameTracer/FrameTracer.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080061#include "LayerProtoHelper.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070062#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070063#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064#include "SurfaceFlinger.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080065#include "TimeStats/TimeStats.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070066
David Sodman41fdfc92017-11-06 16:09:56 -080067#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069namespace android {
70
Yiwei Zhang5434a782018-12-05 18:06:32 -080071using base::StringAppendF;
72
Lloyd Piquef1c675b2018-09-12 20:45:39 -070073std::atomic<int32_t> Layer::sSequence{1};
Mathias Agopian13127d82013-03-05 17:47:11 -080074
Lloyd Pique42ab75e2018-09-12 20:46:03 -070075Layer::Layer(const LayerCreationArgs& args)
Ady Abraham8f1ee7f2019-04-05 10:32:50 -070076 : mFlinger(args.flinger),
77 mName(args.name),
78 mClientRef(args.client),
79 mWindowType(args.metadata.getInt32(METADATA_WINDOW_TYPE, 0)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -070080 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070081
82 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -070083 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
84 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
85 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070086
Dan Stozaf7ba41a2017-05-10 15:11:11 -070087 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070088
Lloyd Pique0449b0f2018-12-20 16:23:45 -080089 mCurrentState.active_legacy.w = args.w;
90 mCurrentState.active_legacy.h = args.h;
91 mCurrentState.flags = layerFlags;
92 mCurrentState.active_legacy.transform.set(0, 0);
93 mCurrentState.crop_legacy.makeInvalid();
94 mCurrentState.requestedCrop_legacy = mCurrentState.crop_legacy;
95 mCurrentState.z = 0;
96 mCurrentState.color.a = 1.0f;
97 mCurrentState.layerStack = 0;
98 mCurrentState.sequence = 0;
99 mCurrentState.requested_legacy = mCurrentState.active_legacy;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800100 mCurrentState.active.w = UINT32_MAX;
101 mCurrentState.active.h = UINT32_MAX;
102 mCurrentState.active.transform.set(0, 0);
103 mCurrentState.transform = 0;
104 mCurrentState.transformToDisplayInverse = false;
105 mCurrentState.crop.makeInvalid();
106 mCurrentState.acquireFence = new Fence(-1);
107 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
108 mCurrentState.hdrMetadata.validTypes = 0;
Marissa Wall7e0a01f2019-08-14 14:29:25 -0700109 mCurrentState.surfaceDamageRegion = Region::INVALID_REGION;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800110 mCurrentState.cornerRadius = 0.0f;
111 mCurrentState.api = -1;
112 mCurrentState.hasColorTransform = false;
Peiyong Linc502cb72019-03-01 15:00:23 -0800113 mCurrentState.colorSpaceAgnostic = false;
Evan Roskya1f1e152019-01-24 16:17:46 -0800114 mCurrentState.metadata = args.metadata;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700115
116 // drawing state & current state are identical
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800117 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700118
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800119 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700120 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800121 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200122 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700123
Ady Abraham8f1ee7f2019-04-05 10:32:50 -0700124 mSchedulerLayerHandle = mFlinger->mScheduler->registerLayer(mName.c_str(), mWindowType);
Ady Abraham09bd3922019-04-08 10:44:56 -0700125
Robert Carr2e102c92018-10-23 12:11:15 -0700126 mFlinger->onLayerCreated();
Dan Stoza436ccf32018-06-21 12:10:12 -0700127}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700128
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700129Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800130 sp<Client> c(mClientRef.promote());
131 if (c != 0) {
132 c->detachLayer(this);
133 }
134
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000135 mFrameTracker.logAndResetStats(mName);
chaviw74d90ad2019-04-26 14:45:26 -0700136 mFlinger->onLayerDestroyed(this);
Mathias Agopian96f08192010-06-02 23:28:45 -0700137}
138
Mathias Agopian13127d82013-03-05 17:47:11 -0800139// ---------------------------------------------------------------------------
140// callbacks
141// ---------------------------------------------------------------------------
142
David Sodmaneb085e02017-10-05 18:49:04 -0700143/*
144 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
145 * Layer. So, the implementation is done in BufferLayer. When called on a
146 * ColorLayer object, it's essentially a NOP.
147 */
David Sodmaneb085e02017-10-05 18:49:04 -0700148void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800149
chaviw43cb3cb2019-05-31 15:23:41 -0700150void Layer::removeRemoteSyncPoints() {
151 for (auto& point : mRemoteSyncPoints) {
152 point->setTransactionApplied();
153 }
154 mRemoteSyncPoints.clear();
155
156 {
chaviw43cb3cb2019-05-31 15:23:41 -0700157 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 {
Lloyd Pique688abd42019-02-15 15:42:24 -0800444 const auto& drawingState{getDrawingState()};
Lloyd Piquef5275482019-01-29 18:42:42 -0800445 compositionState.forceClientComposition = false;
446
447 // TODO(lpique): b/121291683 Remove this one we are sure we don't need the
448 // value recomputed / set every frame.
449 compositionState.geomVisibleRegion = visibleRegion;
450
451 compositionState.isColorspaceAgnostic = isColorSpaceAgnostic();
452 compositionState.dataspace = mCurrentDataSpace;
453 compositionState.colorTransform = getColorTransform();
454 compositionState.colorTransformIsIdentity = !hasColorTransform();
455 compositionState.surfaceDamage = surfaceDamageRegion;
Lloyd Pique688abd42019-02-15 15:42:24 -0800456 compositionState.hasProtectedContent = isProtected();
457
458 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
459 compositionState.isOpaque =
460 isOpaque(drawingState) && !usesRoundedCorners && getAlpha() == 1.0_hf;
Lloyd Piquef5275482019-01-29 18:42:42 -0800461
462 // Force client composition for special cases known only to the front-end.
Lloyd Pique688abd42019-02-15 15:42:24 -0800463 if (isHdrY410() || usesRoundedCorners) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800464 compositionState.forceClientComposition = true;
465 }
466}
467
Lloyd Piquef16688f2019-02-19 17:47:57 -0800468bool Layer::onPreComposition(nsecs_t) {
469 return false;
470}
471
Lloyd Piquea83776c2019-01-29 18:42:32 -0800472void Layer::latchCompositionState(compositionengine::LayerFECompositionState& compositionState,
473 bool includeGeometry) const {
474 if (includeGeometry) {
475 latchGeometry(compositionState);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700476 }
Lloyd Piquef5275482019-01-29 18:42:42 -0800477
478 latchPerFrameState(compositionState);
Lloyd Piquea83776c2019-01-29 18:42:32 -0800479}
Mathias Agopian29a367b2011-07-12 14:51:45 -0700480
Lloyd Piquea83776c2019-01-29 18:42:32 -0800481const char* Layer::getDebugName() const {
482 return mName.string();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800483}
484
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700485void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800486 const auto outputLayer = findOutputLayerForDisplay(display);
487 LOG_FATAL_IF(!outputLayer);
488
489 if (!outputLayer->getState().hwc ||
490 (*outputLayer->getState().hwc).hwcCompositionType !=
491 Hwc2::IComposerClient::Composition::CURSOR) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800492 return;
493 }
494
495 // This gives us only the "orientation" component of the transform
Vishnu Nair33a6eee2019-02-06 13:48:06 -0800496 const State& s(getDrawingState());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800497
498 // Apply the layer's transform, followed by the display's global transform
499 // Here we're guaranteed that the layer's transform preserves rects
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800500 Rect win = getCroppedBufferSize(s);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800501 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700502 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800503 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700504 frame.intersect(display->getViewport(), &frame);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700505 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800506 auto position = displayTransform.transform(frame);
507
Dominik Laskowski7e045462018-05-30 13:02:02 -0700508 auto error =
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800509 (*outputLayer->getState().hwc).hwcLayer->setCursorPosition(position.left, position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800510 ALOGE_IF(error != HWC2::Error::None,
511 "[%s] Failed to set cursor position "
512 "to (%d, %d): %s (%d)",
513 mName.string(), position.left, position.top, to_string(error).c_str(),
514 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800515}
Riley Andrews03414a12014-07-01 14:22:59 -0700516
Mathias Agopian13127d82013-03-05 17:47:11 -0800517// ---------------------------------------------------------------------------
518// drawing...
519// ---------------------------------------------------------------------------
520
Lloyd Piquef16688f2019-02-19 17:47:57 -0800521std::optional<renderengine::LayerSettings> Layer::prepareClientComposition(
522 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
523 if (!getCompositionLayer()) {
524 return {};
525 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800526
Vishnu Nair4351ad52019-02-11 14:13:02 -0800527 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000528 half alpha = getAlpha();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800529 renderengine::LayerSettings layerSettings;
530 layerSettings.geometry.boundaries = bounds;
531 if (targetSettings.useIdentityTransform) {
532 layerSettings.geometry.positionTransform = mat4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000533 } else {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800534 layerSettings.geometry.positionTransform = getTransform().asMatrix4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000535 }
536
537 if (hasColorTransform()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800538 layerSettings.colorTransform = getColorTransform();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000539 }
540
541 const auto roundedCornerState = getRoundedCornerState();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800542 layerSettings.geometry.roundedCornersRadius = roundedCornerState.radius;
543 layerSettings.geometry.roundedCornersCrop = roundedCornerState.cropRect;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000544
Lloyd Piquef16688f2019-02-19 17:47:57 -0800545 layerSettings.alpha = alpha;
546 layerSettings.sourceDataspace = mCurrentDataSpace;
547 return layerSettings;
Mathias Agopian13127d82013-03-05 17:47:11 -0800548}
549
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800550Hwc2::IComposerClient::Composition Layer::getCompositionType(
551 const sp<const DisplayDevice>& display) const {
552 const auto outputLayer = findOutputLayerForDisplay(display);
553 LOG_FATAL_IF(!outputLayer);
554 return outputLayer->getState().hwc ? (*outputLayer->getState().hwc).hwcCompositionType
555 : Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800556}
557
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800558bool Layer::getClearClientTarget(const sp<const DisplayDevice>& display) const {
559 const auto outputLayer = findOutputLayerForDisplay(display);
560 LOG_FATAL_IF(!outputLayer);
561 return outputLayer->getState().clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800562}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800563
Dan Stozacac35382016-01-27 12:21:06 -0800564bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
565 if (point->getFrameNumber() <= mCurrentFrameNumber) {
566 // Don't bother with a SyncPoint, since we've already latched the
567 // relevant frame
568 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700569 }
Robert Carr2e102c92018-10-23 12:11:15 -0700570 if (isRemovedFromCurrentState()) {
571 return false;
572 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700573
Dan Stozacac35382016-01-27 12:21:06 -0800574 Mutex::Autolock lock(mLocalSyncPointMutex);
575 mLocalSyncPoints.push_back(point);
576 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700577}
578
Mathias Agopian13127d82013-03-05 17:47:11 -0800579// ----------------------------------------------------------------------------
580// local state
581// ----------------------------------------------------------------------------
582
David Sodman41fdfc92017-11-06 16:09:56 -0800583bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800584 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700585 return (s.flags & layer_state_t::eLayerSecure);
586}
587
Mathias Agopian13127d82013-03-05 17:47:11 -0800588void Layer::setVisibleRegion(const Region& visibleRegion) {
589 // always called from main thread
590 this->visibleRegion = visibleRegion;
591}
592
593void Layer::setCoveredRegion(const Region& coveredRegion) {
594 // always called from main thread
595 this->coveredRegion = coveredRegion;
596}
597
David Sodman41fdfc92017-11-06 16:09:56 -0800598void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800599 // always called from main thread
600 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
601}
602
Robert Carre5f4f692018-01-12 13:12:28 -0800603void Layer::clearVisibilityRegions() {
604 visibleRegion.clear();
605 visibleNonTransparentRegion.clear();
606 coveredRegion.clear();
607}
608
Mathias Agopian13127d82013-03-05 17:47:11 -0800609// ----------------------------------------------------------------------------
610// transaction
611// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800612
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800613void Layer::pushPendingState() {
614 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700615 return;
616 }
Alec Mourie60041e2019-06-14 18:59:51 -0700617 ATRACE_CALL();
Dan Stoza7dde5992015-05-22 09:51:44 -0700618
Dan Stoza7dde5992015-05-22 09:51:44 -0700619 // If this transaction is waiting on the receipt of a frame, generate a sync
620 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700621 // We don't allow installing sync points after we are removed from the current state
622 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800623 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
624 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800625 if (barrierLayer == nullptr) {
626 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700627 // If we can't promote the layer we are intended to wait on,
628 // then it is expired or otherwise invalid. Allow this transaction
629 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800630 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800631 } else {
chaviw43cb3cb2019-05-31 15:23:41 -0700632 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy, this);
Robert Carr0d480722017-01-10 16:42:54 -0800633 if (barrierLayer->addSyncPoint(syncPoint)) {
Alec Mourie60041e2019-06-14 18:59:51 -0700634 std::stringstream ss;
635 ss << "Adding sync point " << mCurrentState.frameNumber_legacy;
636 ATRACE_NAME(ss.str().c_str());
Dan Stozacac35382016-01-27 12:21:06 -0800637 mRemoteSyncPoints.push_back(std::move(syncPoint));
638 } else {
639 // We already missed the frame we're supposed to synchronize
640 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800641 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800642 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700643 }
644
Dan Stoza7dde5992015-05-22 09:51:44 -0700645 // Wake us up to check if the frame has been received
646 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700647 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700648 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800649 mPendingStates.push_back(mCurrentState);
650 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700651}
652
Pablo Ceballos05289c22016-04-14 15:49:55 -0700653void Layer::popPendingState(State* stateToCommit) {
Alec Mourie60041e2019-06-14 18:59:51 -0700654 ATRACE_CALL();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800655 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700656
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800657 mPendingStates.removeAt(0);
658 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700659}
660
Pablo Ceballos05289c22016-04-14 15:49:55 -0700661bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700662 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800663 while (!mPendingStates.empty()) {
664 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700665 if (mRemoteSyncPoints.empty()) {
666 // If we don't have a sync point for this, apply it anyway. It
667 // will be visually wrong, but it should keep us from getting
668 // into too much trouble.
669 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700670 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700671 stateUpdateAvailable = true;
672 continue;
673 }
674
Marissa Wallf58c14b2018-07-24 10:50:43 -0700675 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800676 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800677 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800678
679 // Signal our end of the sync point and then dispose of it
680 mRemoteSyncPoints.front()->setTransactionApplied();
681 mRemoteSyncPoints.pop_front();
682 continue;
683 }
684
Dan Stoza7dde5992015-05-22 09:51:44 -0700685 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
Alec Mourie60041e2019-06-14 18:59:51 -0700686 ATRACE_NAME("frameIsAvailable");
Dan Stoza7dde5992015-05-22 09:51:44 -0700687 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700688 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700689 stateUpdateAvailable = true;
690
691 // Signal our end of the sync point and then dispose of it
692 mRemoteSyncPoints.front()->setTransactionApplied();
693 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800694 } else {
Alec Mourie60041e2019-06-14 18:59:51 -0700695 ATRACE_NAME("!frameIsAvailable");
Dan Stoza792e5292016-02-11 11:43:58 -0800696 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700697 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700698 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700699 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700700 stateUpdateAvailable = true;
701 }
702 }
703
704 // If we still have pending updates, wake SurfaceFlinger back up and point
705 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800706 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700707 setTransactionFlags(eTransactionNeeded);
708 mFlinger->setTransactionFlags(eTraversalNeeded);
709 }
710
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800711 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700712 return stateUpdateAvailable;
713}
714
Marissa Wall61c58622018-07-18 10:12:20 -0700715uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000716 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800717
Marissa Wall61c58622018-07-18 10:12:20 -0700718 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
719 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700720
David Sodmaneb085e02017-10-05 18:49:04 -0700721 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700722 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000723 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800724 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
725 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
726 " requested={ wh={%4u,%4u} }}\n"
727 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
728 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700729 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700730 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
731 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
732 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
733 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
734 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700735 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
736 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
737 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800738 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700739
Robert Carre392b552017-09-19 12:16:05 -0700740 // Don't let Layer::doTransaction update the drawing state
741 // if we have a pending resize, unless we are in fixed-size mode.
742 // the drawing state will be updated only once we receive a buffer
743 // with the correct size.
744 //
745 // In particular, we want to make sure the clip (which is part
746 // of the geometry state) is latched together with the size but is
747 // latched immediately when no resizing is involved.
748 //
749 // If a sideband stream is attached, however, we want to skip this
750 // optimization so that transactions aren't missed when a buffer
751 // never arrives
752 //
753 // In the case that we don't have a buffer we ignore other factors
754 // and avoid entering the resizePending state. At a high level the
755 // resizePending state is to avoid applying the state of the new buffer
756 // to the old buffer. However in the state where we don't have an old buffer
757 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700758 const bool resizePending =
759 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
760 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
Lloyd Pique0b785d82018-12-04 17:25:27 -0800761 (mActiveBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700762 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800763 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700764 flags |= eDontUpdateGeometryState;
765 }
766 }
767
Robert Carr7bf247e2017-05-18 14:02:49 -0700768 // Here we apply various requested geometry states, depending on our
769 // latching configuration. See Layer.h for a detailed discussion of
770 // how geometry latching is controlled.
771 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000772 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700773
774 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
775 // mode, which causes attributes which normally latch regardless of scaling mode,
776 // to be delayed. We copy the requested state to the active state making sure
777 // to respect these rules (again see Layer.h for a detailed discussion).
778 //
779 // There is an awkward asymmetry in the handling of the crop states in the position
780 // states, as can be seen below. Largely this arises from position and transform
781 // being stored in the same data structure while having different latching rules.
782 // b/38182305
783 //
Marissa Wall61c58622018-07-18 10:12:20 -0700784 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700785 // applyPendingStates in the presence of deferred transactions.
786 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -0700787 float tx = stateToCommit->active_legacy.transform.tx();
788 float ty = stateToCommit->active_legacy.transform.ty();
789 stateToCommit->active_legacy = stateToCommit->requested_legacy;
790 stateToCommit->active_legacy.transform.set(tx, ty);
791 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700792 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700793 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -0700794 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700795 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800796 }
797
Marissa Wall61c58622018-07-18 10:12:20 -0700798 return flags;
799}
800
801uint32_t Layer::doTransaction(uint32_t flags) {
802 ATRACE_CALL();
803
chaviw5aedec92018-10-22 10:40:38 -0700804 if (mLayerDetached) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800805 return flags;
806 }
807
808 if (mChildrenChanged) {
809 flags |= eVisibleRegion;
810 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -0700811 }
812
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800813 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +0000814 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -0700815 if (!applyPendingStates(&c)) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800816 return flags;
Marissa Wall61c58622018-07-18 10:12:20 -0700817 }
818
819 flags = doTransactionResize(flags, &c);
820
Alec Mourib416efd2018-09-06 21:01:59 +0000821 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700822
823 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800824 // invalidate and recompute the visible regions if needed
825 flags |= Layer::eVisibleRegion;
826 }
827
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700828 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800829 // invalidate and recompute the visible regions if needed
830 flags |= eVisibleRegion;
831 this->contentDirty = true;
832
833 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -0700834 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -0700835 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -0800836 }
837
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800838 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -0700839 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800840 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -0700841 }
842
Mathias Agopian13127d82013-03-05 17:47:11 -0800843 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -0700844 commitTransaction(c);
Vishnu Nair8406fd72019-07-30 11:29:31 -0700845 mPendingStatesSnapshot = mPendingStates;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800846 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -0800847 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800848}
849
Pablo Ceballos05289c22016-04-14 15:49:55 -0700850void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800851 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700852}
853
Mathias Agopian13127d82013-03-05 17:47:11 -0800854uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800855 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800856}
857
858uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800859 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -0800860}
861
Robert Carr82364e32016-05-15 11:27:47 -0700862bool Layer::setPosition(float x, float y, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800863 if (mCurrentState.requested_legacy.transform.tx() == x &&
864 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -0800865 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800866 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -0700867
868 // We update the requested and active position simultaneously because
869 // we want to apply the position portion of the transform matrix immediately,
870 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800871 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -0700872 if (immediate && !mFreezeGeometryUpdates) {
873 // Here we directly update the active state
874 // unlike other setters, because we store it within
875 // the transform, but use different latching rules.
876 // b/38182305
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800877 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -0700878 }
Robert Carr7bf247e2017-05-18 14:02:49 -0700879 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -0700880
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800881 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800882 setTransactionFlags(eTransactionNeeded);
883 return true;
884}
Robert Carr82364e32016-05-15 11:27:47 -0700885
Robert Carr1f0a16a2016-10-24 16:27:39 -0700886bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
887 ssize_t idx = mCurrentChildren.indexOf(childLayer);
888 if (idx < 0) {
889 return false;
890 }
891 if (childLayer->setLayer(z)) {
892 mCurrentChildren.removeAt(idx);
893 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800894 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700895 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800896 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700897}
898
Robert Carr503c7042017-09-27 15:06:08 -0700899bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
900 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
901 ssize_t idx = mCurrentChildren.indexOf(childLayer);
902 if (idx < 0) {
903 return false;
904 }
905 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
906 mCurrentChildren.removeAt(idx);
907 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800908 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700909 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800910 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700911}
912
Robert Carrae060832016-11-28 10:51:00 -0800913bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800914 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
915 mCurrentState.sequence++;
916 mCurrentState.z = z;
917 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700918
919 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800920 if (mCurrentState.zOrderRelativeOf != nullptr) {
921 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700922 if (strongRelative != nullptr) {
923 strongRelative->removeZOrderRelative(this);
924 }
chaviw606e5cf2019-03-01 10:12:10 -0800925 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -0700926 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800927 setTransactionFlags(eTransactionNeeded);
928 return true;
929}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700930
Robert Carrdb66e622017-04-10 16:55:57 -0700931void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800932 mCurrentState.zOrderRelatives.remove(relative);
933 mCurrentState.sequence++;
934 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700935 setTransactionFlags(eTransactionNeeded);
936}
937
938void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800939 mCurrentState.zOrderRelatives.add(relative);
940 mCurrentState.modified = true;
941 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -0700942 setTransactionFlags(eTransactionNeeded);
943}
944
chaviw606e5cf2019-03-01 10:12:10 -0800945void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
946 mCurrentState.zOrderRelativeOf = relativeOf;
947 mCurrentState.sequence++;
948 mCurrentState.modified = true;
949 setTransactionFlags(eTransactionNeeded);
950}
951
Robert Carr503d2bd2017-12-04 15:49:47 -0800952bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -0700953 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
954 if (handle == nullptr) {
955 return false;
956 }
957 sp<Layer> relative = handle->owner.promote();
958 if (relative == nullptr) {
959 return false;
960 }
961
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800962 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
963 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -0800964 return false;
965 }
966
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800967 mCurrentState.sequence++;
968 mCurrentState.modified = true;
969 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -0700970
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800971 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -0700972 if (oldZOrderRelativeOf != nullptr) {
973 oldZOrderRelativeOf->removeZOrderRelative(this);
974 }
chaviw606e5cf2019-03-01 10:12:10 -0800975 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -0700976 relative->addZOrderRelative(this);
977
978 setTransactionFlags(eTransactionNeeded);
979
980 return true;
981}
982
Mathias Agopian13127d82013-03-05 17:47:11 -0800983bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800984 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -0700985 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800986 mCurrentState.requested_legacy.w = w;
987 mCurrentState.requested_legacy.h = h;
988 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800989 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -0700990
991 // record the new size, from this point on, when the client request
992 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800993 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -0800994 return true;
995}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800996bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800997 if (mCurrentState.color.a == alpha) return false;
998 mCurrentState.sequence++;
999 mCurrentState.color.a = alpha;
1000 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001001 setTransactionFlags(eTransactionNeeded);
1002 return true;
1003}
chaviw13fdc492017-06-27 12:40:18 -07001004
Valerie Haudd0b7572019-01-29 14:59:27 -08001005bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1006 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001007 return false;
Valerie Hauaa194562019-02-05 16:21:38 -08001008 }
1009 mCurrentState.sequence++;
1010 mCurrentState.modified = true;
1011 setTransactionFlags(eTransactionNeeded);
1012
1013 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001014 // create background color layer if one does not yet exist
1015 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
1016 const String8& name = mName + "BackgroundColorLayer";
Evan Roskya1f1e152019-01-24 16:17:46 -08001017 mCurrentState.bgColorLayer = new ColorLayer(
1018 LayerCreationArgs(mFlinger.get(), nullptr, name, 0, 0, flags, LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -07001019
Valerie Haudd0b7572019-01-29 14:59:27 -08001020 // add to child list
1021 addChild(mCurrentState.bgColorLayer);
1022 mFlinger->mLayersAdded = true;
1023 // set up SF to handle added color layer
1024 if (isRemovedFromCurrentState()) {
1025 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1026 }
1027 mFlinger->setTransactionFlags(eTransactionNeeded);
1028 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1029 mCurrentState.bgColorLayer->reparent(nullptr);
1030 mCurrentState.bgColorLayer = nullptr;
1031 return true;
1032 }
1033
1034 mCurrentState.bgColorLayer->setColor(color);
1035 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1036 mCurrentState.bgColorLayer->setAlpha(alpha);
1037 mCurrentState.bgColorLayer->setDataspace(dataspace);
1038
chaviw13fdc492017-06-27 12:40:18 -07001039 return true;
1040}
1041
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001042bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001043 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001044
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001045 mCurrentState.sequence++;
1046 mCurrentState.cornerRadius = cornerRadius;
1047 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001048 setTransactionFlags(eTransactionNeeded);
1049 return true;
1050}
1051
Robert Carrd4ae7f32018-06-07 16:10:57 -07001052bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1053 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001054 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001055 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1056
1057 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1058 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1059 return false;
1060 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001061 mCurrentState.sequence++;
1062 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1063 matrix.dsdy);
1064 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001065 setTransactionFlags(eTransactionNeeded);
1066 return true;
1067}
Marissa Wall61c58622018-07-18 10:12:20 -07001068
Mathias Agopian13127d82013-03-05 17:47:11 -08001069bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001070 mCurrentState.requestedTransparentRegion_legacy = transparent;
1071 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001072 setTransactionFlags(eTransactionNeeded);
1073 return true;
1074}
1075bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001076 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1077 if (mCurrentState.flags == newFlags) return false;
1078 mCurrentState.sequence++;
1079 mCurrentState.flags = newFlags;
1080 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001081 setTransactionFlags(eTransactionNeeded);
1082 return true;
1083}
Robert Carr99e27f02016-06-16 15:18:02 -07001084
Marissa Wallf58c14b2018-07-24 10:50:43 -07001085bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001086 if (mCurrentState.requestedCrop_legacy == crop) return false;
1087 mCurrentState.sequence++;
1088 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001089 if (immediate && !mFreezeGeometryUpdates) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001090 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001091 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001092 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1093
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001094 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001095 setTransactionFlags(eTransactionNeeded);
1096 return true;
1097}
Robert Carr8d5227b2017-03-16 15:41:03 -07001098
Robert Carrc3574f72016-03-24 12:19:32 -07001099bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001100 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001101 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001102 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001103 return true;
1104}
1105
Evan Roskyef876c92019-01-25 17:46:06 -08001106bool Layer::setMetadata(const LayerMetadata& data) {
1107 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001108 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001109 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001110 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001111 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001112}
1113
Mathias Agopian13127d82013-03-05 17:47:11 -08001114bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001115 if (mCurrentState.layerStack == layerStack) return false;
1116 mCurrentState.sequence++;
1117 mCurrentState.layerStack = layerStack;
1118 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001119 setTransactionFlags(eTransactionNeeded);
1120 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001121}
1122
Peiyong Linc502cb72019-03-01 15:00:23 -08001123bool Layer::setColorSpaceAgnostic(const bool agnostic) {
1124 if (mCurrentState.colorSpaceAgnostic == agnostic) {
1125 return false;
1126 }
1127 mCurrentState.sequence++;
1128 mCurrentState.colorSpaceAgnostic = agnostic;
1129 mCurrentState.modified = true;
1130 setTransactionFlags(eTransactionNeeded);
1131 return true;
1132}
1133
Robert Carr1f0a16a2016-10-24 16:27:39 -07001134uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001135 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001136 if (p == nullptr) {
1137 return getDrawingState().layerStack;
1138 }
1139 return p->getLayerStack();
1140}
1141
Marissa Wallf58c14b2018-07-24 10:50:43 -07001142void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Alec Mourie60041e2019-06-14 18:59:51 -07001143 ATRACE_CALL();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001144 mCurrentState.barrierLayer_legacy = barrierLayer;
1145 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001146 // We don't set eTransactionNeeded, because just receiving a deferral
1147 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001148 mCurrentState.modified = true;
1149 pushPendingState();
1150 mCurrentState.barrierLayer_legacy = nullptr;
1151 mCurrentState.frameNumber_legacy = 0;
1152 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001153}
1154
Marissa Wallf58c14b2018-07-24 10:50:43 -07001155void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001156 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001157 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001158}
1159
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001160// ----------------------------------------------------------------------------
1161// pageflip handling...
1162// ----------------------------------------------------------------------------
1163
Robert Carr1f0a16a2016-10-24 16:27:39 -07001164bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001165 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001166 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001167 if (parent != nullptr && parent->isHiddenByPolicy()) {
1168 return true;
1169 }
Robert Carr1c5481e2019-07-01 14:42:27 -07001170 if (usingRelativeZ(LayerVector::StateSet::Drawing)) {
1171 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
1172 if (zOrderRelativeOf != nullptr) {
1173 if (zOrderRelativeOf->isHiddenByPolicy()) {
1174 return true;
1175 }
1176 }
1177 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001178 return s.flags & layer_state_t::eLayerHidden;
1179}
1180
David Sodman41fdfc92017-11-06 16:09:56 -08001181uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001182 // TODO: should we do something special if mSecure is set?
1183 if (mProtectedByApp) {
1184 // need a hardware-protected path to external video sink
1185 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001186 }
Riley Andrews03414a12014-07-01 14:22:59 -07001187 if (mPotentialCursor) {
1188 usage |= GraphicBuffer::USAGE_CURSOR;
1189 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001190 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001191 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001192}
1193
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001194void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001195 uint32_t orientation = 0;
Vishnu Nair5eb3f062019-04-08 08:21:03 -07001196 // Disable setting transform hint if the debug flag is set.
1197 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001198 // The transform hint is used to improve performance, but we can
1199 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001200 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001201 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001202 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001203 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001204 orientation = 0;
1205 }
1206 }
David Sodmaneb085e02017-10-05 18:49:04 -07001207 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001208}
1209
Mathias Agopian13127d82013-03-05 17:47:11 -08001210// ----------------------------------------------------------------------------
1211// debugging
1212// ----------------------------------------------------------------------------
1213
Marissa Wall61c58622018-07-18 10:12:20 -07001214// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001215LayerDebugInfo Layer::getLayerDebugInfo() const {
1216 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001217 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001218 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001219 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001220 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
chaviw8a01fa42019-08-19 12:39:31 -07001221 info.mType = getType();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001222 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001223 info.mVisibleRegion = visibleRegion;
1224 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001225 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001226 info.mX = ds.active_legacy.transform.tx();
1227 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001228 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001229 info.mWidth = ds.active_legacy.w;
1230 info.mHeight = ds.active_legacy.h;
1231 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001232 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001233 info.mFlags = ds.flags;
1234 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001235 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001236 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1237 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1238 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1239 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001240 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001241 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001242 if (buffer != 0) {
1243 info.mActiveBufferWidth = buffer->getWidth();
1244 info.mActiveBufferHeight = buffer->getHeight();
1245 info.mActiveBufferStride = buffer->getStride();
1246 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001247 } else {
1248 info.mActiveBufferWidth = 0;
1249 info.mActiveBufferHeight = 0;
1250 info.mActiveBufferStride = 0;
1251 info.mActiveBufferFormat = 0;
1252 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001253 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001254 info.mNumQueuedFrames = getQueuedFrameCount();
1255 info.mRefreshPending = isBufferLatched();
1256 info.mIsOpaque = isOpaque(ds);
1257 info.mContentDirty = contentDirty;
1258 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001259}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001260
Yiwei Zhang5434a782018-12-05 18:06:32 -08001261void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001262 result.append("-------------------------------");
1263 result.append("-------------------------------");
1264 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001265 result.append(" Layer name\n");
1266 result.append(" Z | ");
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001267 result.append(" Window Type | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001268 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001269 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001270 result.append(" Disp Frame (LTRB) | ");
1271 result.append(" Source Crop (LTRB)\n");
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}
1276
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001277void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1278 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1279 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001280 return;
1281 }
1282
Yiwei Zhang5434a782018-12-05 18:06:32 -08001283 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001284 if (mName.length() > 77) {
1285 std::string shortened;
1286 shortened.append(mName.string(), 36);
1287 shortened.append("[...]");
1288 shortened.append(mName.string() + (mName.length() - 36), 36);
Yiwei Zhang5434a782018-12-05 18:06:32 -08001289 name = shortened;
Dan Stozae22aec72016-08-01 13:20:59 -07001290 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001291 name = std::string(mName.string(), mName.size());
Dan Stozae22aec72016-08-01 13:20:59 -07001292 }
1293
Yiwei Zhang5434a782018-12-05 18:06:32 -08001294 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001295
Alec Mourib416efd2018-09-06 21:01:59 +00001296 const State& layerState(getDrawingState());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001297 const auto& compositionState = outputLayer->getState();
1298
Chia-I Wu1e043612018-03-01 09:45:09 -08001299 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001300 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001301 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001302 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001303 }
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001304 StringAppendF(&result, " %10d | ", mWindowType);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001305 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
1306 StringAppendF(&result, "%10s | ",
1307 toString(getCompositionLayer() ? compositionState.bufferTransform
1308 : static_cast<Hwc2::Transform>(0))
1309 .c_str());
1310 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001311 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001312 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001313 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1314 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001315
Yichi Chen6ca35192018-05-29 12:20:43 +08001316 result.append("- - - - - - - - - - - - - - - -");
1317 result.append("- - - - - - - - - - - - - - - -");
1318 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001319}
Dan Stozae22aec72016-08-01 13:20:59 -07001320
Yiwei Zhang5434a782018-12-05 18:06:32 -08001321void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001322 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001323}
1324
Svetoslavd85084b2014-03-20 10:28:31 -07001325void Layer::clearFrameStats() {
1326 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001327}
1328
Jamie Gennis6547ff42013-07-16 20:12:42 -07001329void Layer::logFrameStats() {
1330 mFrameTracker.logAndResetStats(mName);
1331}
1332
Svetoslavd85084b2014-03-20 10:28:31 -07001333void Layer::getFrameStats(FrameStats* outStats) const {
1334 mFrameTracker.getStats(outStats);
1335}
1336
Yiwei Zhang5434a782018-12-05 18:06:32 -08001337void Layer::dumpFrameEvents(std::string& result) {
chaviw8a01fa42019-08-19 12:39:31 -07001338 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().string(), getType(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001339 Mutex::Autolock lock(mFrameEventHistoryMutex);
1340 mFrameEventHistory.checkFencesForCompletion();
1341 mFrameEventHistory.dump(result);
1342}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001343
Brian Anderson5ea5e592016-12-01 16:54:33 -08001344void Layer::onDisconnect() {
1345 Mutex::Autolock lock(mFrameEventHistoryMutex);
1346 mFrameEventHistory.onDisconnect();
Mikael Pessa90092f42019-08-26 17:22:04 -07001347 const int32_t layerID = getSequence();
1348 mFlinger->mTimeStats->onDestroy(layerID);
1349 mFlinger->mFrameTracer->onDestroy(layerID);
Brian Anderson5ea5e592016-12-01 16:54:33 -08001350}
1351
Brian Anderson3890c392016-07-25 12:48:08 -07001352void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001353 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001354 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001355 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1356 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001357 }
1358
Brian Andersond6927fb2016-07-23 23:37:30 -07001359 Mutex::Autolock lock(mFrameEventHistoryMutex);
1360 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001361 // If there are any unsignaled fences in the aquire timeline at this
1362 // point, the previously queued frame hasn't been latched yet. Go ahead
1363 // and try to get the signal time here so the syscall is taken out of
1364 // the main thread's critical path.
1365 mAcquireTimeline.updateSignalTimes();
1366 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001367 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001368 mFrameEventHistory.addQueue(*newTimestamps);
1369 }
1370
Brian Anderson3890c392016-07-25 12:48:08 -07001371 if (outDelta) {
1372 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001373 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001374}
Dan Stozae77c7662016-05-13 11:37:28 -07001375
Chia-I Wu98f1c102017-05-30 14:54:08 -07001376size_t Layer::getChildrenCount() const {
1377 size_t count = 0;
1378 for (const sp<Layer>& child : mCurrentChildren) {
1379 count += 1 + child->getChildrenCount();
1380 }
1381 return count;
1382}
1383
Robert Carr1f0a16a2016-10-24 16:27:39 -07001384void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001385 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001386 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001387
Robert Carr1f0a16a2016-10-24 16:27:39 -07001388 mCurrentChildren.add(layer);
1389 layer->setParent(this);
1390}
1391
1392ssize_t Layer::removeChild(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 Carr6fb1a7e2018-12-11 12:07:25 -08001395
Robert Carr1323c952019-01-28 18:13:27 -08001396 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001397 return mCurrentChildren.remove(layer);
1398}
1399
Robert Carr1db73f62016-12-21 12:58:51 -08001400bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1401 sp<Handle> handle = nullptr;
1402 sp<Layer> newParent = nullptr;
1403 if (newParentHandle == nullptr) {
1404 return false;
1405 }
1406 handle = static_cast<Handle*>(newParentHandle.get());
1407 newParent = handle->owner.promote();
1408 if (newParent == nullptr) {
1409 ALOGE("Unable to promote Layer handle");
1410 return false;
1411 }
1412
chaviw5aedec92018-10-22 10:40:38 -07001413 if (attachChildren()) {
1414 setTransactionFlags(eTransactionNeeded);
1415 }
Robert Carr1db73f62016-12-21 12:58:51 -08001416 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001417 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001418 }
1419 mCurrentChildren.clear();
1420
1421 return true;
1422}
1423
Robert Carr15eae092018-03-23 13:43:53 -07001424void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001425 for (const sp<Layer>& child : mDrawingChildren) {
1426 child->mDrawingParent = newParent;
Vishnu Nairc652ff82019-03-15 12:48:54 -07001427 child->computeBounds(newParent->mBounds,
1428 newParent->getTransformWithScale(
1429 newParent->getBufferScaleTransform()));
Robert Carr578038f2018-03-09 12:25:24 -08001430 }
1431}
1432
chaviwf1961f72017-09-18 16:41:07 -07001433bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001434 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001435
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001436 // While layers are detached, we allow most operations
1437 // and simply halt performing the actual transaction. However
1438 // for reparent != null we would enter the mRemovedFromCurrentState
1439 // state, regardless of whether doTransaction was called, and
1440 // so we need to prevent the update here.
1441 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001442 return false;
1443 }
1444
Robert Carr54cf5b12019-01-25 14:02:28 -08001445 sp<Layer> newParent;
1446 if (newParentHandle != nullptr) {
1447 auto handle = static_cast<Handle*>(newParentHandle.get());
1448 newParent = handle->owner.promote();
1449 if (newParent == nullptr) {
1450 ALOGE("Unable to promote Layer handle");
1451 return false;
1452 }
1453 if (newParent == this) {
1454 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1455 return false;
1456 }
1457 }
1458
chaviwf1961f72017-09-18 16:41:07 -07001459 sp<Layer> parent = getParent();
1460 if (parent != nullptr) {
1461 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001462 }
1463
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001464 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001465 newParent->addChild(this);
1466 if (!newParent->isRemovedFromCurrentState()) {
1467 addToCurrentState();
1468 } else {
1469 onRemovedFromCurrentState();
1470 }
1471
1472 if (mLayerDetached) {
1473 mLayerDetached = false;
1474 callSetTransactionFlags = true;
1475 }
1476 } else {
1477 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001478 }
1479
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001480 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001481 setTransactionFlags(eTransactionNeeded);
1482 }
chaviw06178942017-07-27 10:25:59 -07001483 return true;
1484}
1485
Robert Carr9524cb32017-02-13 11:32:32 -08001486bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001487 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001488 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001489 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001490 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001491 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001492 child->detachChildren();
chaviw43cb3cb2019-05-31 15:23:41 -07001493 child->removeRemoteSyncPoints();
Robert Carr9524cb32017-02-13 11:32:32 -08001494 }
Robert Carr7f619b22017-11-06 12:56:35 -08001495 }
Robert Carr9524cb32017-02-13 11:32:32 -08001496
1497 return true;
1498}
1499
chaviw5aedec92018-10-22 10:40:38 -07001500bool Layer::attachChildren() {
1501 bool changed = false;
1502 for (const sp<Layer>& child : mCurrentChildren) {
1503 sp<Client> parentClient = mClientRef.promote();
1504 sp<Client> client(child->mClientRef.promote());
1505 if (client != nullptr && parentClient != client) {
1506 if (child->mLayerDetached) {
1507 child->mLayerDetached = false;
1508 changed = true;
1509 }
1510 changed |= child->attachChildren();
1511 }
1512 }
1513
1514 return changed;
1515}
1516
Peiyong Lind3788632018-09-18 16:01:31 -07001517bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001518 static const mat4 identityMatrix = mat4();
1519
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001520 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001521 return false;
1522 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001523 ++mCurrentState.sequence;
1524 mCurrentState.colorTransform = matrix;
1525 mCurrentState.hasColorTransform = matrix != identityMatrix;
1526 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001527 setTransactionFlags(eTransactionNeeded);
1528 return true;
1529}
1530
chaviwf66724d2018-11-28 16:35:21 -08001531mat4 Layer::getColorTransform() const {
1532 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1533 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1534 colorTransform = parent->getColorTransform() * colorTransform;
1535 }
1536 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001537}
1538
1539bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001540 bool hasColorTransform = getDrawingState().hasColorTransform;
1541 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1542 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1543 }
1544 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001545}
1546
Chia-I Wu11481472018-05-04 10:43:19 -07001547bool Layer::isLegacyDataSpace() const {
1548 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001549 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001550 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001551}
1552
Robert Carr1f0a16a2016-10-24 16:27:39 -07001553void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001554 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001555}
1556
Robert Carr1f0a16a2016-10-24 16:27:39 -07001557int32_t Layer::getZ() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001558 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001559}
1560
Robert Carr1c5481e2019-07-01 14:42:27 -07001561bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) const {
Robert Carr29abff82017-12-04 13:51:20 -08001562 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001563 const State& state = useDrawing ? mDrawingState : mCurrentState;
Robert Carr29abff82017-12-04 13:51:20 -08001564 return state.zOrderRelativeOf != nullptr;
1565}
1566
David Sodman41fdfc92017-11-06 16:09:56 -08001567__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001568 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001569 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1570 "makeTraversalList received invalid stateSet");
1571 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1572 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001573 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001574
Robert Carr29abff82017-12-04 13:51:20 -08001575 if (state.zOrderRelatives.size() == 0) {
1576 *outSkipRelativeZUsers = true;
1577 return children;
1578 }
1579
chaviwfd462612018-05-31 16:11:27 -07001580 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001581 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001582 sp<Layer> strongRelative = weakRelative.promote();
1583 if (strongRelative != nullptr) {
1584 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001585 }
1586 }
1587
Dan Stoza412903f2017-04-27 13:42:17 -07001588 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001589 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
Robert Carr503c7042017-09-27 15:06:08 -07001590 if (childState.zOrderRelativeOf != nullptr) {
1591 continue;
1592 }
Robert Carrdb66e622017-04-10 16:55:57 -07001593 traverse.add(child);
1594 }
1595
1596 return traverse;
1597}
1598
Robert Carr1f0a16a2016-10-24 16:27:39 -07001599/**
Robert Carrdb66e622017-04-10 16:55:57 -07001600 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001601 */
Dan Stoza412903f2017-04-27 13:42:17 -07001602void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001603 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1604 // produce a new list for traversing, including our relatives, and not including our children
1605 // who are relatives of another surface. In the case that there are no relative Z,
1606 // makeTraversalList returns our children directly to avoid significant overhead.
1607 // However in this case we need to take the responsibility for filtering children which
1608 // are relatives of another surface here.
1609 bool skipRelativeZUsers = false;
1610 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001611
Robert Carr1f0a16a2016-10-24 16:27:39 -07001612 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001613 for (; i < list.size(); i++) {
1614 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001615 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1616 continue;
1617 }
1618
Robert Carrdb66e622017-04-10 16:55:57 -07001619 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001620 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001621 }
Dan Stoza412903f2017-04-27 13:42:17 -07001622 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001623 }
Robert Carr29abff82017-12-04 13:51:20 -08001624
Dan Stoza412903f2017-04-27 13:42:17 -07001625 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001626 for (; i < list.size(); i++) {
1627 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001628
Robert Carr29abff82017-12-04 13:51:20 -08001629 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1630 continue;
1631 }
Dan Stoza412903f2017-04-27 13:42:17 -07001632 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001633 }
1634}
1635
1636/**
Robert Carrdb66e622017-04-10 16:55:57 -07001637 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001638 */
Dan Stoza412903f2017-04-27 13:42:17 -07001639void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1640 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001641 // See traverseInZOrder for documentation.
1642 bool skipRelativeZUsers = false;
1643 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001644
Robert Carr1f0a16a2016-10-24 16:27:39 -07001645 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001646 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001647 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001648
1649 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1650 continue;
1651 }
1652
Robert Carrdb66e622017-04-10 16:55:57 -07001653 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001654 break;
1655 }
Dan Stoza412903f2017-04-27 13:42:17 -07001656 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001657 }
Dan Stoza412903f2017-04-27 13:42:17 -07001658 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001659 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001660 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001661
1662 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1663 continue;
1664 }
1665
Dan Stoza412903f2017-04-27 13:42:17 -07001666 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001667 }
1668}
1669
chaviw4b129c22018-04-09 16:19:43 -07001670LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1671 const std::vector<Layer*>& layersInTree) {
1672 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1673 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001674 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1675 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001676 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001677
chaviwfd462612018-05-31 16:11:27 -07001678 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001679 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1680 sp<Layer> strongRelative = weakRelative.promote();
1681 // Only add relative layers that are also descendents of the top most parent of the tree.
1682 // If a relative layer is not a descendent, then it should be ignored.
1683 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1684 traverse.add(strongRelative);
1685 }
1686 }
1687
1688 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001689 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001690 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1691 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1692 // the child here since it won't be added later as a relative.
1693 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1694 childState.zOrderRelativeOf.promote().get())) {
1695 continue;
1696 }
1697 traverse.add(child);
1698 }
1699
1700 return traverse;
1701}
1702
1703void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1704 LayerVector::StateSet stateSet,
1705 const LayerVector::Visitor& visitor) {
1706 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001707
1708 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001709 for (; i < list.size(); i++) {
1710 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001711 if (relative->getZ() >= 0) {
1712 break;
1713 }
chaviw4b129c22018-04-09 16:19:43 -07001714 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001715 }
chaviw4b129c22018-04-09 16:19:43 -07001716
chaviwa76b2712017-09-20 12:02:26 -07001717 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001718 for (; i < list.size(); i++) {
1719 const auto& relative = list[i];
1720 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001721 }
1722}
1723
chaviw4b129c22018-04-09 16:19:43 -07001724std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1725 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1726 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1727
1728 std::vector<Layer*> layersInTree = {this};
1729 for (size_t i = 0; i < children.size(); i++) {
1730 const auto& child = children[i];
1731 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1732 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1733 }
1734
1735 return layersInTree;
1736}
1737
1738void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1739 const LayerVector::Visitor& visitor) {
1740 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1741 std::sort(layersInTree.begin(), layersInTree.end());
1742 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1743}
1744
Peiyong Linefefaac2018-08-17 12:27:51 -07001745ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001746 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001747}
1748
chaviw13fdc492017-06-27 12:40:18 -07001749half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001750 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001751
chaviw13fdc492017-06-27 12:40:18 -07001752 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1753 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001754}
Robert Carr6452f122017-03-21 10:41:29 -07001755
chaviw13fdc492017-06-27 12:40:18 -07001756half4 Layer::getColor() const {
1757 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001758 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001759}
Robert Carr6452f122017-03-21 10:41:29 -07001760
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001761Layer::RoundedCornerState Layer::getRoundedCornerState() const {
1762 const auto& p = mDrawingParent.promote();
1763 if (p != nullptr) {
Peiyong Lin27016a92019-03-29 17:36:08 +00001764 RoundedCornerState parentState = p->getRoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001765 if (parentState.radius > 0) {
1766 ui::Transform t = getActiveTransform(getDrawingState());
1767 t = t.inverse();
1768 parentState.cropRect = t.transform(parentState.cropRect);
1769 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1770 // but a transform matrix can define horizontal and vertical scales.
1771 // Let's take the average between both of them and pass into the shader, practically we
1772 // never do this type of transformation on windows anyway.
1773 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
1774 return parentState;
1775 }
1776 }
1777 const float radius = getDrawingState().cornerRadius;
Peiyong Linb9ff23e2019-04-08 11:04:59 -07001778 return radius > 0 && getCrop(getDrawingState()).isValid()
1779 ? RoundedCornerState(getCrop(getDrawingState()).toFloatRect(), radius)
1780 : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001781}
1782
Robert Carr1f0a16a2016-10-24 16:27:39 -07001783void Layer::commitChildList() {
1784 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1785 const auto& child = mCurrentChildren[i];
1786 child->commitChildList();
1787 }
1788 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001789 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001790}
1791
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001792static wp<Layer> extractLayerFromBinder(const wp<IBinder>& weakBinderHandle) {
1793 if (weakBinderHandle == nullptr) {
1794 return nullptr;
1795 }
1796 sp<IBinder> binderHandle = weakBinderHandle.promote();
1797 if (binderHandle == nullptr) {
1798 return nullptr;
1799 }
1800 sp<Layer::Handle> handle = static_cast<Layer::Handle*>(binderHandle.get());
1801 if (handle == nullptr) {
1802 return nullptr;
1803 }
1804 return handle->owner;
1805}
1806
Robert Carr720e5062018-07-30 17:45:14 -07001807void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001808 mCurrentState.inputInfo = info;
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001809 mCurrentState.touchableRegionCrop = extractLayerFromBinder(info.touchableRegionCropHandle);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001810 mCurrentState.modified = true;
1811 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07001812 setTransactionFlags(eTransactionNeeded);
1813}
1814
Vishnu Nair8406fd72019-07-30 11:29:31 -07001815void Layer::writeToProtoDrawingState(LayerProto* layerInfo, uint32_t traceFlags) const {
1816 ui::Transform transform = getTransform();
1817
1818 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
1819 for (const auto& pendingState : mPendingStatesSnapshot) {
1820 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
1821 if (barrierLayer != nullptr) {
1822 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
1823 barrierLayerProto->set_id(barrierLayer->sequence);
1824 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
1825 }
1826 }
1827
1828 auto buffer = mActiveBuffer;
1829 if (buffer != nullptr) {
1830 LayerProtoHelper::writeToProto(buffer,
1831 [&]() { return layerInfo->mutable_active_buffer(); });
1832 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
1833 layerInfo->mutable_buffer_transform());
1834 }
1835 layerInfo->set_invalidate(contentDirty);
1836 layerInfo->set_is_protected(isProtected());
1837 layerInfo->set_dataspace(
1838 dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
1839 layerInfo->set_queued_frames(getQueuedFrameCount());
1840 layerInfo->set_refresh_pending(isBufferLatched());
1841 layerInfo->set_curr_frame(mCurrentFrameNumber);
1842 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
1843
1844 layerInfo->set_corner_radius(getRoundedCornerState().radius);
1845 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1846 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
1847 [&]() { return layerInfo->mutable_position(); });
1848 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
1849 LayerProtoHelper::writeToProto(visibleRegion,
1850 [&]() { return layerInfo->mutable_visible_region(); });
1851 LayerProtoHelper::writeToProto(surfaceDamageRegion,
1852 [&]() { return layerInfo->mutable_damage_region(); });
1853 }
1854
1855 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
1856 LayerProtoHelper::writeToProto(mSourceBounds,
1857 [&]() { return layerInfo->mutable_source_bounds(); });
1858 LayerProtoHelper::writeToProto(mScreenBounds,
1859 [&]() { return layerInfo->mutable_screen_bounds(); });
1860 }
1861}
1862
1863void Layer::writeToProtoCommonState(LayerProto* layerInfo, LayerVector::StateSet stateSet,
1864 uint32_t traceFlags) const {
chaviw1d044282017-09-27 12:19:28 -07001865 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1866 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001867 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07001868
Peiyong Linefefaac2018-08-17 12:27:51 -07001869 ui::Transform requestedTransform = state.active_legacy.transform;
chaviw1d044282017-09-27 12:19:28 -07001870
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001871 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
1872 layerInfo->set_id(sequence);
1873 layerInfo->set_name(getName().c_str());
chaviw8a01fa42019-08-19 12:39:31 -07001874 layerInfo->set_type(getType());
chaviw1d044282017-09-27 12:19:28 -07001875
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001876 for (const auto& child : children) {
1877 layerInfo->add_children(child->sequence);
chaviw1d044282017-09-27 12:19:28 -07001878 }
chaviw1d044282017-09-27 12:19:28 -07001879
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001880 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1881 sp<Layer> strongRelative = weakRelative.promote();
1882 if (strongRelative != nullptr) {
1883 layerInfo->add_relatives(strongRelative->sequence);
1884 }
chaviwadc40c22018-07-10 16:57:27 -07001885 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001886
1887 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
1888 [&]() { return layerInfo->mutable_transparent_region(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001889
1890 layerInfo->set_layer_stack(getLayerStack());
1891 layerInfo->set_z(state.z);
1892
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001893 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(),
1894 [&]() {
1895 return layerInfo->mutable_requested_position();
1896 });
1897
1898 LayerProtoHelper::writeSizeToProto(state.active_legacy.w, state.active_legacy.h,
1899 [&]() { return layerInfo->mutable_size(); });
1900
1901 LayerProtoHelper::writeToProto(state.crop_legacy,
1902 [&]() { return layerInfo->mutable_crop(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001903
1904 layerInfo->set_is_opaque(isOpaque(state));
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001905
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001906
1907 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1908 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
1909 LayerProtoHelper::writeToProto(state.color,
1910 [&]() { return layerInfo->mutable_requested_color(); });
1911 layerInfo->set_flags(state.flags);
1912
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001913 LayerProtoHelper::writeToProto(requestedTransform,
1914 layerInfo->mutable_requested_transform());
1915
1916 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
1917 if (parent != nullptr) {
1918 layerInfo->set_parent(parent->sequence);
1919 } else {
1920 layerInfo->set_parent(-1);
1921 }
1922
1923 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1924 if (zOrderRelativeOf != nullptr) {
1925 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1926 } else {
1927 layerInfo->set_z_order_relative_of(-1);
1928 }
chaviwadc40c22018-07-10 16:57:27 -07001929 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001930
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001931 if (traceFlags & SurfaceTracing::TRACE_INPUT) {
1932 LayerProtoHelper::writeToProto(state.inputInfo, state.touchableRegionCrop,
1933 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001934 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001935
1936 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
1937 auto protoMap = layerInfo->mutable_metadata();
1938 for (const auto& entry : state.metadata.mMap) {
1939 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
1940 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001941 }
chaviw1d044282017-09-27 12:19:28 -07001942}
1943
Robert Carr2e102c92018-10-23 12:11:15 -07001944bool Layer::isRemovedFromCurrentState() const {
1945 return mRemovedFromCurrentState;
1946}
1947
Arthur Hungd20b2702019-01-14 18:16:16 +08001948InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001949 InputWindowInfo info = mDrawingState.inputInfo;
Robert Carr720e5062018-07-30 17:45:14 -07001950
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001951 if (info.displayId == ADISPLAY_ID_NONE) {
1952 info.displayId = mDrawingState.layerStack;
1953 }
1954
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001955 ui::Transform t = getTransform();
1956 const float xScale = t.sx();
1957 const float yScale = t.sy();
Ady Abraham282f1d72019-07-24 18:05:56 -07001958 int32_t xSurfaceInset = info.surfaceInset;
1959 int32_t ySurfaceInset = info.surfaceInset;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001960 if (xScale != 1.0f || yScale != 1.0f) {
Ady Abraham282f1d72019-07-24 18:05:56 -07001961 info.windowXScale *= (xScale != 0.0f) ? 1.0f / xScale : 0.0f;
1962 info.windowYScale *= (yScale != 0.0f) ? 1.0f / yScale : 0.0f;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001963 info.touchableRegion.scaleSelf(xScale, yScale);
Ady Abraham282f1d72019-07-24 18:05:56 -07001964 xSurfaceInset = std::round(xSurfaceInset * xScale);
1965 ySurfaceInset = std::round(ySurfaceInset * yScale);
Riddle Hsu39d4aa52018-11-30 20:46:53 +08001966 }
Robert Carre07e1032018-11-26 12:55:53 -08001967
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001968 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001969 // If this is a portal window, set the touchableRegion to the layerBounds.
1970 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
1971 ? getBufferSize(getDrawingState())
1972 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08001973 if (!layerBounds.isValid()) {
1974 layerBounds = getCroppedBufferSize(getDrawingState());
1975 }
Vishnu Nair8033a492018-12-05 07:27:23 -08001976 layerBounds = t.transform(layerBounds);
Ady Abraham282f1d72019-07-24 18:05:56 -07001977
1978 // clamp inset to layer bounds
1979 xSurfaceInset = (xSurfaceInset >= 0) ? std::min(xSurfaceInset, layerBounds.getWidth() / 2) : 0;
1980 ySurfaceInset = (ySurfaceInset >= 0) ? std::min(ySurfaceInset, layerBounds.getHeight() / 2) : 0;
1981
Arthur Hung118b1142019-05-08 21:25:59 +08001982 layerBounds.inset(xSurfaceInset, ySurfaceInset, xSurfaceInset, ySurfaceInset);
Vishnu Nair8033a492018-12-05 07:27:23 -08001983
Arthur Hungd20b2702019-01-14 18:16:16 +08001984 // Input coordinate should match the layer bounds.
1985 info.frameLeft = layerBounds.left;
1986 info.frameTop = layerBounds.top;
1987 info.frameRight = layerBounds.right;
1988 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08001989
1990 // Position the touchable region relative to frame screen location and restrict it to frame
1991 // bounds.
1992 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08001993 info.visible = canReceiveInput();
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001994
1995 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
1996 if (info.replaceTouchableRegionWithCrop) {
1997 if (cropLayer == nullptr) {
1998 info.touchableRegion = Region(Rect{mScreenBounds});
1999 } else {
2000 info.touchableRegion = Region(Rect{cropLayer->mScreenBounds});
2001 }
2002 } else if (cropLayer != nullptr) {
2003 info.touchableRegion = info.touchableRegion.intersect(Rect{cropLayer->mScreenBounds});
2004 }
2005
Robert Carr720e5062018-07-30 17:45:14 -07002006 return info;
2007}
2008
2009bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002010 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002011}
2012
Lloyd Piquefeb73d72018-12-04 17:23:44 -08002013std::shared_ptr<compositionengine::Layer> Layer::getCompositionLayer() const {
2014 return nullptr;
2015}
2016
chaviw3e727cd2019-01-31 13:41:05 -08002017bool Layer::canReceiveInput() const {
2018 return isVisible();
2019}
2020
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002021compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
2022 const sp<const DisplayDevice>& display) const {
2023 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionLayer().get());
2024}
2025
Mathias Agopian13127d82013-03-05 17:47:11 -08002026// ---------------------------------------------------------------------------
2027
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002028}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002029
2030#if defined(__gl_h_)
2031#error "don't include gl/gl.h in this file"
2032#endif
2033
2034#if defined(__gl2_h_)
2035#error "don't include gl2/gl2.h in this file"
2036#endif