blob: 2efa23e2eba255616ba64e9b9830870b906c0aa5 [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>
Vishnu Nair0f085c62019-08-30 08:49:12 -070025#include <binder/IPCThreadState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080026#include <compositionengine/Display.h>
Lloyd Pique0b785d82018-12-04 17:25:27 -080027#include <compositionengine/Layer.h>
Lloyd Piquea83776c2019-01-29 18:42:32 -080028#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080029#include <compositionengine/OutputLayer.h>
30#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 Agopian4d9b8222013-03-12 17:11:48 -070080
81 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -070082 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
83 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
84 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070085
Dan Stozaf7ba41a2017-05-10 15:11:11 -070086 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070087
Lloyd Pique0449b0f2018-12-20 16:23:45 -080088 mCurrentState.active_legacy.w = args.w;
89 mCurrentState.active_legacy.h = args.h;
90 mCurrentState.flags = layerFlags;
91 mCurrentState.active_legacy.transform.set(0, 0);
92 mCurrentState.crop_legacy.makeInvalid();
93 mCurrentState.requestedCrop_legacy = mCurrentState.crop_legacy;
94 mCurrentState.z = 0;
95 mCurrentState.color.a = 1.0f;
96 mCurrentState.layerStack = 0;
97 mCurrentState.sequence = 0;
98 mCurrentState.requested_legacy = mCurrentState.active_legacy;
Lloyd Pique0449b0f2018-12-20 16:23:45 -080099 mCurrentState.active.w = UINT32_MAX;
100 mCurrentState.active.h = UINT32_MAX;
101 mCurrentState.active.transform.set(0, 0);
102 mCurrentState.transform = 0;
103 mCurrentState.transformToDisplayInverse = false;
104 mCurrentState.crop.makeInvalid();
105 mCurrentState.acquireFence = new Fence(-1);
106 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
107 mCurrentState.hdrMetadata.validTypes = 0;
Marissa Wall7e0a01f2019-08-14 14:29:25 -0700108 mCurrentState.surfaceDamageRegion = Region::INVALID_REGION;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800109 mCurrentState.cornerRadius = 0.0f;
110 mCurrentState.api = -1;
111 mCurrentState.hasColorTransform = false;
Peiyong Linc502cb72019-03-01 15:00:23 -0800112 mCurrentState.colorSpaceAgnostic = false;
Evan Roskya1f1e152019-01-24 16:17:46 -0800113 mCurrentState.metadata = args.metadata;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700114
115 // drawing state & current state are identical
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800116 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700117
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800118 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700119 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800120 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200121 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700122
Ady Abraham8f1ee7f2019-04-05 10:32:50 -0700123 mSchedulerLayerHandle = mFlinger->mScheduler->registerLayer(mName.c_str(), mWindowType);
Vishnu Nair0f085c62019-08-30 08:49:12 -0700124 mCallingPid = args.callingPid;
125 mCallingUid = args.callingUid;
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
Vishnu Nair0f085c62019-08-30 08:49:12 -0700139LayerCreationArgs::LayerCreationArgs(SurfaceFlinger* flinger, const sp<Client>& client,
140 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
141 LayerMetadata metadata)
142 : flinger(flinger),
143 client(client),
144 name(name),
145 w(w),
146 h(h),
147 flags(flags),
148 metadata(std::move(metadata)) {
149 IPCThreadState* ipc = IPCThreadState::self();
150 callingPid = ipc->getCallingPid();
151 callingUid = ipc->getCallingUid();
152}
153
Mathias Agopian13127d82013-03-05 17:47:11 -0800154// ---------------------------------------------------------------------------
155// callbacks
156// ---------------------------------------------------------------------------
157
David Sodmaneb085e02017-10-05 18:49:04 -0700158/*
159 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
160 * Layer. So, the implementation is done in BufferLayer. When called on a
161 * ColorLayer object, it's essentially a NOP.
162 */
David Sodmaneb085e02017-10-05 18:49:04 -0700163void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800164
chaviw43cb3cb2019-05-31 15:23:41 -0700165void Layer::removeRemoteSyncPoints() {
166 for (auto& point : mRemoteSyncPoints) {
167 point->setTransactionApplied();
168 }
169 mRemoteSyncPoints.clear();
170
171 {
chaviw43cb3cb2019-05-31 15:23:41 -0700172 for (State pendingState : mPendingStates) {
173 pendingState.barrierLayer_legacy = nullptr;
174 }
175 }
176}
177
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700178void Layer::removeRelativeZ(const std::vector<Layer*>& layersInTree) {
179 if (mCurrentState.zOrderRelativeOf == nullptr) {
180 return;
181 }
Robert Carr2e102c92018-10-23 12:11:15 -0700182
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700183 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
184 if (strongRelative == nullptr) {
185 setZOrderRelativeOf(nullptr);
186 return;
187 }
188
189 if (!std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
190 strongRelative->removeZOrderRelative(this);
191 mFlinger->setTransactionFlags(eTraversalNeeded);
chaviw606e5cf2019-03-01 10:12:10 -0800192 setZOrderRelativeOf(nullptr);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700193 }
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700194}
195
196void Layer::removeFromCurrentState() {
197 mRemovedFromCurrentState = true;
Rob Carr4bba3702018-10-08 21:53:30 +0000198
Robert Carr2e102c92018-10-23 12:11:15 -0700199 // Since we are no longer reachable from CurrentState SurfaceFlinger
200 // will no longer invoke doTransaction for us, and so we will
201 // never finish applying transactions. We signal the sync point
202 // now so that another layer will not become indefinitely
203 // blocked.
chaviw43cb3cb2019-05-31 15:23:41 -0700204 removeRemoteSyncPoints();
Robert Carr2e102c92018-10-23 12:11:15 -0700205
206 {
207 Mutex::Autolock syncLock(mLocalSyncPointMutex);
208 for (auto& point : mLocalSyncPoints) {
209 point->setFrameAvailable();
210 }
211 mLocalSyncPoints.clear();
212 }
213
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800214 mFlinger->markLayerPendingRemovalLocked(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700215}
Chia-I Wu38512252017-05-17 14:36:16 -0700216
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700217void Layer::onRemovedFromCurrentState() {
218 auto layersInTree = getLayersInTree(LayerVector::StateSet::Current);
219 std::sort(layersInTree.begin(), layersInTree.end());
220 for (const auto& layer : layersInTree) {
221 layer->removeFromCurrentState();
222 layer->removeRelativeZ(layersInTree);
223 }
224}
225
chaviw61626f22018-11-15 16:26:27 -0800226void Layer::addToCurrentState() {
227 mRemovedFromCurrentState = false;
228
229 for (const auto& child : mCurrentChildren) {
230 child->addToCurrentState();
231 }
232}
233
Mathias Agopian13127d82013-03-05 17:47:11 -0800234// ---------------------------------------------------------------------------
235// set-up
236// ---------------------------------------------------------------------------
237
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700238const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800239 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800240}
241
chaviw13fdc492017-06-27 12:40:18 -0700242bool Layer::getPremultipledAlpha() const {
243 return mPremultipliedAlpha;
244}
245
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700246sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800247 Mutex::Autolock _l(mLock);
Robert Carrc0df3122019-04-11 13:18:21 -0700248 if (mGetHandleCalled) {
249 ALOGE("Get handle called twice" );
250 return nullptr;
251 }
252 mGetHandleCalled = true;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700253 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800254}
255
256// ---------------------------------------------------------------------------
257// h/w composer set-up
258// ---------------------------------------------------------------------------
259
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700260static Rect reduce(const Rect& win, const Region& exclude) {
261 if (CC_LIKELY(exclude.isEmpty())) {
262 return win;
263 }
264 if (exclude.isRect()) {
265 return win.reduce(exclude.getBounds());
266 }
267 return Region(win).subtract(exclude).getBounds();
268}
269
Dan Stoza80d61162017-12-20 15:57:52 -0800270static FloatRect reduce(const FloatRect& win, const Region& exclude) {
271 if (CC_LIKELY(exclude.isEmpty())) {
272 return win;
273 }
274 // Convert through Rect (by rounding) for lack of FloatRegion
275 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
276}
277
Vishnu Nair4351ad52019-02-11 14:13:02 -0800278Rect Layer::getScreenBounds(bool reduceTransparentRegion) const {
Vishnu Nairf0c28512019-02-08 12:40:28 -0800279 if (!reduceTransparentRegion) {
280 return Rect{mScreenBounds};
281 }
282
283 FloatRect bounds = getBounds();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800284 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800285 // Transform to screen space.
286 bounds = t.transform(bounds);
287 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700288}
289
Vishnu Nair4351ad52019-02-11 14:13:02 -0800290FloatRect Layer::getBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000291 const State& s(getDrawingState());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800292 return getBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700293}
294
Vishnu Nairf0c28512019-02-08 12:40:28 -0800295FloatRect Layer::getBounds(const Region& activeTransparentRegion) const {
296 // Subtract the transparent region and snap to the bounds.
297 return reduce(mBounds, activeTransparentRegion);
298}
299
Vishnu Nairc652ff82019-03-15 12:48:54 -0700300ui::Transform Layer::getBufferScaleTransform() const {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800301 // If the layer is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
302 // it isFixedSize) then there may be additional scaling not accounted
Vishnu Nairc652ff82019-03-15 12:48:54 -0700303 // for in the layer transform.
chaviwd62d3062019-09-04 14:48:02 -0700304 if (!isFixedSize() || getBuffer() == nullptr) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700305 return {};
Vishnu Nair4351ad52019-02-11 14:13:02 -0800306 }
307
Marissa Wall290ad082019-03-06 13:23:47 -0800308 // If the layer is a buffer state layer, the active width and height
309 // could be infinite. In that case, return the effective transform.
310 const uint32_t activeWidth = getActiveWidth(getDrawingState());
311 const uint32_t activeHeight = getActiveHeight(getDrawingState());
312 if (activeWidth >= UINT32_MAX && activeHeight >= UINT32_MAX) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700313 return {};
Marissa Wall290ad082019-03-06 13:23:47 -0800314 }
315
chaviwd62d3062019-09-04 14:48:02 -0700316 int bufferWidth = getBuffer()->getWidth();
317 int bufferHeight = getBuffer()->getHeight();
Vishnu Nairc652ff82019-03-15 12:48:54 -0700318
chaviw4244e032019-09-04 11:27:49 -0700319 if (getBufferTransform() & NATIVE_WINDOW_TRANSFORM_ROT_90) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700320 std::swap(bufferWidth, bufferHeight);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800321 }
Vishnu Nairc652ff82019-03-15 12:48:54 -0700322
Marissa Wall290ad082019-03-06 13:23:47 -0800323 float sx = activeWidth / static_cast<float>(bufferWidth);
324 float sy = activeHeight / static_cast<float>(bufferHeight);
325
Vishnu Nair4351ad52019-02-11 14:13:02 -0800326 ui::Transform extraParentScaling;
327 extraParentScaling.set(sx, 0, 0, sy);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700328 return extraParentScaling;
329}
330
331ui::Transform Layer::getTransformWithScale(const ui::Transform& bufferScaleTransform) const {
332 // We need to mirror this scaling to child surfaces or we will break the contract where WM can
333 // treat child surfaces as pixels in the parent surface.
chaviwd62d3062019-09-04 14:48:02 -0700334 if (!isFixedSize() || getBuffer() == nullptr) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700335 return mEffectiveTransform;
336 }
337 return mEffectiveTransform * bufferScaleTransform;
338}
339
340FloatRect Layer::getBoundsPreScaling(const ui::Transform& bufferScaleTransform) const {
341 // We need the pre scaled layer bounds when computing child bounds to make sure the child is
342 // cropped to its parent layer after any buffer transform scaling is applied.
chaviwd62d3062019-09-04 14:48:02 -0700343 if (!isFixedSize() || getBuffer() == nullptr) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700344 return mBounds;
345 }
346 return bufferScaleTransform.inverse().transform(mBounds);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800347}
348
349void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform) {
350 const State& s(getDrawingState());
351
352 // Calculate effective layer transform
353 mEffectiveTransform = parentTransform * getActiveTransform(s);
354
355 // Transform parent bounds to layer space
356 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
357
Vishnu Nairc652ff82019-03-15 12:48:54 -0700358 // Calculate source bounds
Vishnu Nair4351ad52019-02-11 14:13:02 -0800359 mSourceBounds = computeSourceBounds(parentBounds);
360
361 // Calculate bounds by croping diplay frame with layer crop and parent bounds
362 FloatRect bounds = mSourceBounds;
363 const Rect layerCrop = getCrop(s);
364 if (!layerCrop.isEmpty()) {
365 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
366 }
367 bounds = bounds.intersect(parentBounds);
368
369 mBounds = bounds;
370 mScreenBounds = mEffectiveTransform.transform(mBounds);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700371
372 // Add any buffer scaling to the layer's children.
373 ui::Transform bufferScaleTransform = getBufferScaleTransform();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800374 for (const sp<Layer>& child : mDrawingChildren) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700375 child->computeBounds(getBoundsPreScaling(bufferScaleTransform),
376 getTransformWithScale(bufferScaleTransform));
Vishnu Nair4351ad52019-02-11 14:13:02 -0800377 }
378}
379
Vishnu Nair60356342018-11-13 13:00:45 -0800380Rect Layer::getCroppedBufferSize(const State& s) const {
381 Rect size = getBufferSize(s);
382 Rect crop = getCrop(s);
383 if (!crop.isEmpty() && size.isValid()) {
384 size.intersect(crop, &size);
385 } else if (!crop.isEmpty()) {
386 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700387 }
Vishnu Nair60356342018-11-13 13:00:45 -0800388 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800389}
390
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700391void Layer::setupRoundedCornersCropCoordinates(Rect win,
392 const FloatRect& roundedCornersCrop) const {
393 // Translate win by the rounded corners rect coordinates, to have all values in
394 // layer coordinate space.
395 win.left -= roundedCornersCrop.left;
396 win.right -= roundedCornersCrop.left;
397 win.top -= roundedCornersCrop.top;
398 win.bottom -= roundedCornersCrop.top;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700399}
400
Lloyd Piquec6687342019-03-07 21:34:57 -0800401void Layer::latchBasicGeometry(compositionengine::LayerFECompositionState& compositionState) const {
402 const auto& drawingState{getDrawingState()};
403 const uint32_t layerStack = getLayerStack();
404 const auto alpha = static_cast<float>(getAlpha());
405 const bool opaque = isOpaque(drawingState);
406 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
407
408 auto blendMode = Hwc2::IComposerClient::BlendMode::NONE;
409 if (!opaque || alpha != 1.0f) {
410 blendMode = mPremultipliedAlpha ? Hwc2::IComposerClient::BlendMode::PREMULTIPLIED
411 : Hwc2::IComposerClient::BlendMode::COVERAGE;
412 }
413
414 // TODO(b/121291683): Instead of filling in a passed-in compositionState
415 // structure, switch to Layer owning the structure and have
416 // CompositionEngine be able to get a reference to it.
417
418 compositionState.layerStackId =
419 (layerStack != ~0u) ? std::make_optional(layerStack) : std::nullopt;
420 compositionState.internalOnly = getPrimaryDisplayOnly();
421 compositionState.isVisible = isVisible();
422 compositionState.isOpaque = opaque && !usesRoundedCorners && alpha == 1.f;
423
424 compositionState.contentDirty = contentDirty;
425 contentDirty = false;
426
427 compositionState.geomLayerBounds = mBounds;
428 compositionState.geomLayerTransform = getTransform();
429 compositionState.geomInverseLayerTransform = compositionState.geomLayerTransform.inverse();
430 compositionState.transparentRegionHint = getActiveTransparentRegion(drawingState);
431
432 compositionState.blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
433 compositionState.alpha = alpha;
434}
435
Lloyd Piquea83776c2019-01-29 18:42:32 -0800436void Layer::latchGeometry(compositionengine::LayerFECompositionState& compositionState) const {
437 const auto& drawingState{getDrawingState()};
Mathias Agopian13127d82013-03-05 17:47:11 -0800438
Lloyd Piquea83776c2019-01-29 18:42:32 -0800439 int type = drawingState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
440 int appId = drawingState.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700441 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400442 if (parent.get()) {
443 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800444 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
445 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
Jiwen 'Steve' Cai736c74e2019-05-28 18:33:22 -0700446 if (parentType > 0 && parentAppId > 0) {
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800447 type = parentType;
448 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700449 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400450 }
451
Lloyd Piquea83776c2019-01-29 18:42:32 -0800452 compositionState.geomBufferSize = getBufferSize(drawingState);
chaviw4244e032019-09-04 11:27:49 -0700453 compositionState.geomContentCrop = getBufferCrop();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800454 compositionState.geomCrop = getCrop(drawingState);
chaviw4244e032019-09-04 11:27:49 -0700455 compositionState.geomBufferTransform = getBufferTransform();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800456 compositionState.geomBufferUsesDisplayInverseTransform = getTransformToDisplayInverse();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800457 compositionState.geomUsesSourceCrop = usesSourceCrop();
458 compositionState.isSecure = isSecure();
David Sodman15094112018-10-11 09:39:37 -0700459
Lloyd Piquea83776c2019-01-29 18:42:32 -0800460 compositionState.type = type;
461 compositionState.appId = appId;
462}
David Sodmanba340492018-08-05 21:51:33 -0700463
Lloyd Piquef5275482019-01-29 18:42:42 -0800464void Layer::latchPerFrameState(compositionengine::LayerFECompositionState& compositionState) const {
Lloyd Pique688abd42019-02-15 15:42:24 -0800465 const auto& drawingState{getDrawingState()};
Lloyd Piquef5275482019-01-29 18:42:42 -0800466 compositionState.forceClientComposition = false;
467
Lloyd Piquef5275482019-01-29 18:42:42 -0800468 compositionState.isColorspaceAgnostic = isColorSpaceAgnostic();
chaviw4244e032019-09-04 11:27:49 -0700469 compositionState.dataspace = getDataSpace();
Lloyd Piquef5275482019-01-29 18:42:42 -0800470 compositionState.colorTransform = getColorTransform();
471 compositionState.colorTransformIsIdentity = !hasColorTransform();
472 compositionState.surfaceDamage = surfaceDamageRegion;
Lloyd Pique688abd42019-02-15 15:42:24 -0800473 compositionState.hasProtectedContent = isProtected();
474
475 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
476 compositionState.isOpaque =
477 isOpaque(drawingState) && !usesRoundedCorners && getAlpha() == 1.0_hf;
Lloyd Piquef5275482019-01-29 18:42:42 -0800478
479 // Force client composition for special cases known only to the front-end.
Lloyd Pique688abd42019-02-15 15:42:24 -0800480 if (isHdrY410() || usesRoundedCorners) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800481 compositionState.forceClientComposition = true;
482 }
483}
484
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800485void Layer::latchCursorCompositionState(
486 compositionengine::LayerFECompositionState& compositionState) const {
487 // This gives us only the "orientation" component of the transform
488 const State& drawingState{getDrawingState()};
489
490 // Apply the layer's transform, followed by the display's global transform
491 // Here we're guaranteed that the layer's transform preserves rects
492 Rect win = getCroppedBufferSize(drawingState);
493 // Subtract the transparent region and snap to the bounds
494 Rect bounds = reduce(win, getActiveTransparentRegion(drawingState));
495 Rect frame(getTransform().transform(bounds));
496
497 compositionState.cursorFrame = frame;
498}
499
Lloyd Piquef16688f2019-02-19 17:47:57 -0800500bool Layer::onPreComposition(nsecs_t) {
501 return false;
502}
503
Lloyd Piquea83776c2019-01-29 18:42:32 -0800504void Layer::latchCompositionState(compositionengine::LayerFECompositionState& compositionState,
Lloyd Piquec6687342019-03-07 21:34:57 -0800505 compositionengine::LayerFE::StateSubset subset) const {
506 using StateSubset = compositionengine::LayerFE::StateSubset;
Lloyd Piquef5275482019-01-29 18:42:42 -0800507
Lloyd Piquec6687342019-03-07 21:34:57 -0800508 switch (subset) {
509 case StateSubset::BasicGeometry:
510 latchBasicGeometry(compositionState);
511 break;
512
513 case StateSubset::GeometryAndContent:
514 latchBasicGeometry(compositionState);
515 latchGeometry(compositionState);
516 latchPerFrameState(compositionState);
517 break;
518
519 case StateSubset::Content:
520 latchPerFrameState(compositionState);
521 break;
522 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800523}
Mathias Agopian29a367b2011-07-12 14:51:45 -0700524
Lloyd Piquea83776c2019-01-29 18:42:32 -0800525const char* Layer::getDebugName() const {
526 return mName.string();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800527}
528
Mathias Agopian13127d82013-03-05 17:47:11 -0800529// ---------------------------------------------------------------------------
530// drawing...
531// ---------------------------------------------------------------------------
532
Lloyd Piquef16688f2019-02-19 17:47:57 -0800533std::optional<renderengine::LayerSettings> Layer::prepareClientComposition(
534 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
535 if (!getCompositionLayer()) {
536 return {};
537 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800538
Vishnu Nair4351ad52019-02-11 14:13:02 -0800539 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000540 half alpha = getAlpha();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800541 renderengine::LayerSettings layerSettings;
542 layerSettings.geometry.boundaries = bounds;
543 if (targetSettings.useIdentityTransform) {
544 layerSettings.geometry.positionTransform = mat4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000545 } else {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800546 layerSettings.geometry.positionTransform = getTransform().asMatrix4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000547 }
548
549 if (hasColorTransform()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800550 layerSettings.colorTransform = getColorTransform();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000551 }
552
553 const auto roundedCornerState = getRoundedCornerState();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800554 layerSettings.geometry.roundedCornersRadius = roundedCornerState.radius;
555 layerSettings.geometry.roundedCornersCrop = roundedCornerState.cropRect;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000556
Lloyd Piquef16688f2019-02-19 17:47:57 -0800557 layerSettings.alpha = alpha;
chaviw4244e032019-09-04 11:27:49 -0700558 layerSettings.sourceDataspace = getDataSpace();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800559 return layerSettings;
Mathias Agopian13127d82013-03-05 17:47:11 -0800560}
561
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800562Hwc2::IComposerClient::Composition Layer::getCompositionType(
563 const sp<const DisplayDevice>& display) const {
564 const auto outputLayer = findOutputLayerForDisplay(display);
565 LOG_FATAL_IF(!outputLayer);
566 return outputLayer->getState().hwc ? (*outputLayer->getState().hwc).hwcCompositionType
567 : Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800568}
569
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800570bool Layer::getClearClientTarget(const sp<const DisplayDevice>& display) const {
571 const auto outputLayer = findOutputLayerForDisplay(display);
572 LOG_FATAL_IF(!outputLayer);
573 return outputLayer->getState().clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800574}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800575
Dan Stozacac35382016-01-27 12:21:06 -0800576bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
577 if (point->getFrameNumber() <= mCurrentFrameNumber) {
578 // Don't bother with a SyncPoint, since we've already latched the
579 // relevant frame
580 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700581 }
Robert Carr2e102c92018-10-23 12:11:15 -0700582 if (isRemovedFromCurrentState()) {
583 return false;
584 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700585
Dan Stozacac35382016-01-27 12:21:06 -0800586 Mutex::Autolock lock(mLocalSyncPointMutex);
587 mLocalSyncPoints.push_back(point);
588 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700589}
590
Mathias Agopian13127d82013-03-05 17:47:11 -0800591// ----------------------------------------------------------------------------
592// local state
593// ----------------------------------------------------------------------------
594
David Sodman41fdfc92017-11-06 16:09:56 -0800595bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800596 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700597 return (s.flags & layer_state_t::eLayerSecure);
598}
599
Mathias Agopian13127d82013-03-05 17:47:11 -0800600// ----------------------------------------------------------------------------
601// transaction
602// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800603
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800604void Layer::pushPendingState() {
605 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700606 return;
607 }
Alec Mourie60041e2019-06-14 18:59:51 -0700608 ATRACE_CALL();
Dan Stoza7dde5992015-05-22 09:51:44 -0700609
Dan Stoza7dde5992015-05-22 09:51:44 -0700610 // If this transaction is waiting on the receipt of a frame, generate a sync
611 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700612 // We don't allow installing sync points after we are removed from the current state
613 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800614 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
615 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800616 if (barrierLayer == nullptr) {
617 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700618 // If we can't promote the layer we are intended to wait on,
619 // then it is expired or otherwise invalid. Allow this transaction
620 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800621 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800622 } else {
chaviw43cb3cb2019-05-31 15:23:41 -0700623 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy, this);
Robert Carr0d480722017-01-10 16:42:54 -0800624 if (barrierLayer->addSyncPoint(syncPoint)) {
Alec Mourie60041e2019-06-14 18:59:51 -0700625 std::stringstream ss;
626 ss << "Adding sync point " << mCurrentState.frameNumber_legacy;
627 ATRACE_NAME(ss.str().c_str());
Dan Stozacac35382016-01-27 12:21:06 -0800628 mRemoteSyncPoints.push_back(std::move(syncPoint));
629 } else {
630 // We already missed the frame we're supposed to synchronize
631 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800632 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800633 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700634 }
635
Dan Stoza7dde5992015-05-22 09:51:44 -0700636 // Wake us up to check if the frame has been received
637 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700638 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700639 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800640 mPendingStates.push_back(mCurrentState);
641 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700642}
643
Pablo Ceballos05289c22016-04-14 15:49:55 -0700644void Layer::popPendingState(State* stateToCommit) {
Alec Mourie60041e2019-06-14 18:59:51 -0700645 ATRACE_CALL();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800646 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700647
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800648 mPendingStates.removeAt(0);
649 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700650}
651
Pablo Ceballos05289c22016-04-14 15:49:55 -0700652bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700653 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800654 while (!mPendingStates.empty()) {
655 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700656 if (mRemoteSyncPoints.empty()) {
657 // If we don't have a sync point for this, apply it anyway. It
658 // will be visually wrong, but it should keep us from getting
659 // into too much trouble.
660 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700661 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700662 stateUpdateAvailable = true;
663 continue;
664 }
665
Marissa Wallf58c14b2018-07-24 10:50:43 -0700666 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800667 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800668 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800669
670 // Signal our end of the sync point and then dispose of it
671 mRemoteSyncPoints.front()->setTransactionApplied();
672 mRemoteSyncPoints.pop_front();
673 continue;
674 }
675
Dan Stoza7dde5992015-05-22 09:51:44 -0700676 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
Alec Mourie60041e2019-06-14 18:59:51 -0700677 ATRACE_NAME("frameIsAvailable");
Dan Stoza7dde5992015-05-22 09:51:44 -0700678 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700679 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700680 stateUpdateAvailable = true;
681
682 // Signal our end of the sync point and then dispose of it
683 mRemoteSyncPoints.front()->setTransactionApplied();
684 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800685 } else {
Alec Mourie60041e2019-06-14 18:59:51 -0700686 ATRACE_NAME("!frameIsAvailable");
Dan Stoza792e5292016-02-11 11:43:58 -0800687 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700688 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700689 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700690 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700691 stateUpdateAvailable = true;
692 }
693 }
694
695 // If we still have pending updates, wake SurfaceFlinger back up and point
696 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800697 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700698 setTransactionFlags(eTransactionNeeded);
699 mFlinger->setTransactionFlags(eTraversalNeeded);
700 }
701
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800702 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700703 return stateUpdateAvailable;
704}
705
Marissa Wall61c58622018-07-18 10:12:20 -0700706uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000707 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800708
Marissa Wall61c58622018-07-18 10:12:20 -0700709 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
710 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700711
David Sodmaneb085e02017-10-05 18:49:04 -0700712 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700713 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000714 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800715 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
716 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
717 " requested={ wh={%4u,%4u} }}\n"
718 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
719 " requested={ wh={%4u,%4u} }}\n",
chaviw4244e032019-09-04 11:27:49 -0700720 this, getName().string(), getBufferTransform(), getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700721 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
722 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
723 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
724 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
725 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700726 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
727 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
728 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800729 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700730
Robert Carre392b552017-09-19 12:16:05 -0700731 // Don't let Layer::doTransaction update the drawing state
732 // if we have a pending resize, unless we are in fixed-size mode.
733 // the drawing state will be updated only once we receive a buffer
734 // with the correct size.
735 //
736 // In particular, we want to make sure the clip (which is part
737 // of the geometry state) is latched together with the size but is
738 // latched immediately when no resizing is involved.
739 //
740 // If a sideband stream is attached, however, we want to skip this
741 // optimization so that transactions aren't missed when a buffer
742 // never arrives
743 //
744 // In the case that we don't have a buffer we ignore other factors
745 // and avoid entering the resizePending state. At a high level the
746 // resizePending state is to avoid applying the state of the new buffer
747 // to the old buffer. However in the state where we don't have an old buffer
748 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700749 const bool resizePending =
750 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
751 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
chaviwd62d3062019-09-04 14:48:02 -0700752 (getBuffer() != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700753 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800754 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700755 flags |= eDontUpdateGeometryState;
756 }
757 }
758
Robert Carr7bf247e2017-05-18 14:02:49 -0700759 // Here we apply various requested geometry states, depending on our
760 // latching configuration. See Layer.h for a detailed discussion of
761 // how geometry latching is controlled.
762 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000763 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700764
Robert Carr7bf247e2017-05-18 14:02:49 -0700765 // There is an awkward asymmetry in the handling of the crop states in the position
766 // states, as can be seen below. Largely this arises from position and transform
767 // being stored in the same data structure while having different latching rules.
768 // b/38182305
769 //
Marissa Wall61c58622018-07-18 10:12:20 -0700770 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700771 // applyPendingStates in the presence of deferred transactions.
chaviw214c89d2019-09-04 16:03:53 -0700772 editCurrentState.active_legacy = editCurrentState.requested_legacy;
773 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Mathias Agopian13127d82013-03-05 17:47:11 -0800774 }
775
Marissa Wall61c58622018-07-18 10:12:20 -0700776 return flags;
777}
778
779uint32_t Layer::doTransaction(uint32_t flags) {
780 ATRACE_CALL();
781
chaviw5aedec92018-10-22 10:40:38 -0700782 if (mLayerDetached) {
Robert Carr321e83c2019-08-19 15:49:30 -0700783 // Ensure BLAST buffer callbacks are processed.
784 // detachChildren and mLayerDetached were implemented to avoid geometry updates
785 // to layers in the cases of animation. For BufferQueue layers buffers are still
786 // consumed as normal. This is useful as otherwise the client could get hung
787 // inevitably waiting on a buffer to return. We recreate this semantic for BufferQueue
788 // even though it is a little consistent. detachChildren is shortly slated for removal
789 // by the hierarchy mirroring work so we don't need to worry about it too much.
790 mDrawingState.callbackHandles = mCurrentState.callbackHandles;
791 mCurrentState.callbackHandles = {};
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800792 return flags;
793 }
794
795 if (mChildrenChanged) {
796 flags |= eVisibleRegion;
797 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -0700798 }
799
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800800 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +0000801 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -0700802 if (!applyPendingStates(&c)) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800803 return flags;
Marissa Wall61c58622018-07-18 10:12:20 -0700804 }
805
806 flags = doTransactionResize(flags, &c);
807
Alec Mourib416efd2018-09-06 21:01:59 +0000808 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700809
810 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800811 // invalidate and recompute the visible regions if needed
812 flags |= Layer::eVisibleRegion;
813 }
814
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700815 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800816 // invalidate and recompute the visible regions if needed
817 flags |= eVisibleRegion;
818 this->contentDirty = true;
819
820 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -0700821 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -0700822 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -0800823 }
824
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800825 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -0700826 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800827 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -0700828 }
829
Mathias Agopian13127d82013-03-05 17:47:11 -0800830 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -0700831 commitTransaction(c);
Vishnu Nair8406fd72019-07-30 11:29:31 -0700832 mPendingStatesSnapshot = mPendingStates;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800833 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -0800834 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800835}
836
Pablo Ceballos05289c22016-04-14 15:49:55 -0700837void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800838 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700839}
840
Mathias Agopian13127d82013-03-05 17:47:11 -0800841uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800842 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800843}
844
845uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800846 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -0800847}
848
chaviw214c89d2019-09-04 16:03:53 -0700849bool Layer::setPosition(float x, float y) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800850 if (mCurrentState.requested_legacy.transform.tx() == x &&
851 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -0800852 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800853 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -0700854
855 // We update the requested and active position simultaneously because
856 // we want to apply the position portion of the transform matrix immediately,
857 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800858 mCurrentState.requested_legacy.transform.set(x, y);
chaviw214c89d2019-09-04 16:03:53 -0700859 // Here we directly update the active state
860 // unlike other setters, because we store it within
861 // the transform, but use different latching rules.
862 // b/38182305
863 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr69663fb2016-03-27 19:59:19 -0700864
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800865 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800866 setTransactionFlags(eTransactionNeeded);
867 return true;
868}
Robert Carr82364e32016-05-15 11:27:47 -0700869
Robert Carr1f0a16a2016-10-24 16:27:39 -0700870bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
871 ssize_t idx = mCurrentChildren.indexOf(childLayer);
872 if (idx < 0) {
873 return false;
874 }
875 if (childLayer->setLayer(z)) {
876 mCurrentChildren.removeAt(idx);
877 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800878 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700879 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800880 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700881}
882
Robert Carr503c7042017-09-27 15:06:08 -0700883bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
884 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
885 ssize_t idx = mCurrentChildren.indexOf(childLayer);
886 if (idx < 0) {
887 return false;
888 }
889 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
890 mCurrentChildren.removeAt(idx);
891 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800892 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700893 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800894 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700895}
896
Robert Carrae060832016-11-28 10:51:00 -0800897bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800898 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
899 mCurrentState.sequence++;
900 mCurrentState.z = z;
901 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700902
903 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800904 if (mCurrentState.zOrderRelativeOf != nullptr) {
905 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700906 if (strongRelative != nullptr) {
907 strongRelative->removeZOrderRelative(this);
908 }
chaviw606e5cf2019-03-01 10:12:10 -0800909 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -0700910 }
chaviwe5ac40f2019-09-24 16:36:55 -0700911 mCurrentState.isRelativeOf = false;
Mathias Agopian13127d82013-03-05 17:47:11 -0800912 setTransactionFlags(eTransactionNeeded);
913 return true;
914}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700915
Robert Carrdb66e622017-04-10 16:55:57 -0700916void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800917 mCurrentState.zOrderRelatives.remove(relative);
918 mCurrentState.sequence++;
919 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700920 setTransactionFlags(eTransactionNeeded);
921}
922
923void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800924 mCurrentState.zOrderRelatives.add(relative);
925 mCurrentState.modified = true;
926 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -0700927 setTransactionFlags(eTransactionNeeded);
928}
929
chaviw606e5cf2019-03-01 10:12:10 -0800930void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
931 mCurrentState.zOrderRelativeOf = relativeOf;
932 mCurrentState.sequence++;
933 mCurrentState.modified = true;
934 setTransactionFlags(eTransactionNeeded);
935}
936
Robert Carr503d2bd2017-12-04 15:49:47 -0800937bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -0700938 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
939 if (handle == nullptr) {
940 return false;
941 }
942 sp<Layer> relative = handle->owner.promote();
943 if (relative == nullptr) {
944 return false;
945 }
946
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800947 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
948 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -0800949 return false;
950 }
951
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800952 mCurrentState.sequence++;
953 mCurrentState.modified = true;
954 mCurrentState.z = relativeZ;
chaviwe5ac40f2019-09-24 16:36:55 -0700955 mCurrentState.isRelativeOf = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700956
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800957 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -0700958 if (oldZOrderRelativeOf != nullptr) {
959 oldZOrderRelativeOf->removeZOrderRelative(this);
960 }
chaviw606e5cf2019-03-01 10:12:10 -0800961 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -0700962 relative->addZOrderRelative(this);
963
964 setTransactionFlags(eTransactionNeeded);
965
966 return true;
967}
968
Mathias Agopian13127d82013-03-05 17:47:11 -0800969bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800970 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -0700971 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800972 mCurrentState.requested_legacy.w = w;
973 mCurrentState.requested_legacy.h = h;
974 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800975 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -0700976
977 // record the new size, from this point on, when the client request
978 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800979 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -0800980 return true;
981}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800982bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800983 if (mCurrentState.color.a == alpha) return false;
984 mCurrentState.sequence++;
985 mCurrentState.color.a = alpha;
986 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800987 setTransactionFlags(eTransactionNeeded);
988 return true;
989}
chaviw13fdc492017-06-27 12:40:18 -0700990
Valerie Haudd0b7572019-01-29 14:59:27 -0800991bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
992 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -0700993 return false;
Valerie Hauaa194562019-02-05 16:21:38 -0800994 }
995 mCurrentState.sequence++;
996 mCurrentState.modified = true;
997 setTransactionFlags(eTransactionNeeded);
998
999 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001000 // create background color layer if one does not yet exist
1001 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
1002 const String8& name = mName + "BackgroundColorLayer";
Lloyd Pique958be492019-03-28 15:34:59 -07001003 mCurrentState.bgColorLayer = mFlinger->getFactory().createColorLayer(
Evan Roskya1f1e152019-01-24 16:17:46 -08001004 LayerCreationArgs(mFlinger.get(), nullptr, name, 0, 0, flags, LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -07001005
Valerie Haudd0b7572019-01-29 14:59:27 -08001006 // add to child list
1007 addChild(mCurrentState.bgColorLayer);
1008 mFlinger->mLayersAdded = true;
1009 // set up SF to handle added color layer
1010 if (isRemovedFromCurrentState()) {
1011 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1012 }
1013 mFlinger->setTransactionFlags(eTransactionNeeded);
1014 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1015 mCurrentState.bgColorLayer->reparent(nullptr);
1016 mCurrentState.bgColorLayer = nullptr;
1017 return true;
1018 }
1019
1020 mCurrentState.bgColorLayer->setColor(color);
1021 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1022 mCurrentState.bgColorLayer->setAlpha(alpha);
1023 mCurrentState.bgColorLayer->setDataspace(dataspace);
1024
chaviw13fdc492017-06-27 12:40:18 -07001025 return true;
1026}
1027
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001028bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001029 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001030
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001031 mCurrentState.sequence++;
1032 mCurrentState.cornerRadius = cornerRadius;
1033 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001034 setTransactionFlags(eTransactionNeeded);
1035 return true;
1036}
1037
Robert Carrd4ae7f32018-06-07 16:10:57 -07001038bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1039 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001040 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001041 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1042
1043 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1044 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1045 return false;
1046 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001047 mCurrentState.sequence++;
1048 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1049 matrix.dsdy);
1050 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001051 setTransactionFlags(eTransactionNeeded);
1052 return true;
1053}
Marissa Wall61c58622018-07-18 10:12:20 -07001054
Mathias Agopian13127d82013-03-05 17:47:11 -08001055bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001056 mCurrentState.requestedTransparentRegion_legacy = transparent;
1057 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001058 setTransactionFlags(eTransactionNeeded);
1059 return true;
1060}
1061bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001062 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1063 if (mCurrentState.flags == newFlags) return false;
1064 mCurrentState.sequence++;
1065 mCurrentState.flags = newFlags;
1066 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001067 setTransactionFlags(eTransactionNeeded);
1068 return true;
1069}
Robert Carr99e27f02016-06-16 15:18:02 -07001070
chaviw214c89d2019-09-04 16:03:53 -07001071bool Layer::setCrop_legacy(const Rect& crop) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001072 if (mCurrentState.requestedCrop_legacy == crop) return false;
1073 mCurrentState.sequence++;
1074 mCurrentState.requestedCrop_legacy = crop;
chaviw214c89d2019-09-04 16:03:53 -07001075 mCurrentState.crop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001076
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001077 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001078 setTransactionFlags(eTransactionNeeded);
1079 return true;
1080}
Robert Carr8d5227b2017-03-16 15:41:03 -07001081
Robert Carrc3574f72016-03-24 12:19:32 -07001082bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001083 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001084 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001085 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001086 return true;
1087}
1088
Evan Roskyef876c92019-01-25 17:46:06 -08001089bool Layer::setMetadata(const LayerMetadata& data) {
1090 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001091 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001092 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001093 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001094 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001095}
1096
Mathias Agopian13127d82013-03-05 17:47:11 -08001097bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001098 if (mCurrentState.layerStack == layerStack) return false;
1099 mCurrentState.sequence++;
1100 mCurrentState.layerStack = layerStack;
1101 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001102 setTransactionFlags(eTransactionNeeded);
1103 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001104}
1105
Peiyong Linc502cb72019-03-01 15:00:23 -08001106bool Layer::setColorSpaceAgnostic(const bool agnostic) {
1107 if (mCurrentState.colorSpaceAgnostic == agnostic) {
1108 return false;
1109 }
1110 mCurrentState.sequence++;
1111 mCurrentState.colorSpaceAgnostic = agnostic;
1112 mCurrentState.modified = true;
1113 setTransactionFlags(eTransactionNeeded);
1114 return true;
1115}
1116
Robert Carr1f0a16a2016-10-24 16:27:39 -07001117uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001118 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001119 if (p == nullptr) {
1120 return getDrawingState().layerStack;
1121 }
1122 return p->getLayerStack();
1123}
1124
Marissa Wallf58c14b2018-07-24 10:50:43 -07001125void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Alec Mourie60041e2019-06-14 18:59:51 -07001126 ATRACE_CALL();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001127 mCurrentState.barrierLayer_legacy = barrierLayer;
1128 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001129 // We don't set eTransactionNeeded, because just receiving a deferral
1130 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001131 mCurrentState.modified = true;
1132 pushPendingState();
1133 mCurrentState.barrierLayer_legacy = nullptr;
1134 mCurrentState.frameNumber_legacy = 0;
1135 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001136}
1137
Marissa Wallf58c14b2018-07-24 10:50:43 -07001138void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001139 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001140 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001141}
1142
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001143// ----------------------------------------------------------------------------
1144// pageflip handling...
1145// ----------------------------------------------------------------------------
1146
Robert Carr1f0a16a2016-10-24 16:27:39 -07001147bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001148 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001149 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001150 if (parent != nullptr && parent->isHiddenByPolicy()) {
1151 return true;
1152 }
Robert Carr1c5481e2019-07-01 14:42:27 -07001153 if (usingRelativeZ(LayerVector::StateSet::Drawing)) {
1154 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
1155 if (zOrderRelativeOf != nullptr) {
1156 if (zOrderRelativeOf->isHiddenByPolicy()) {
1157 return true;
1158 }
1159 }
1160 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001161 return s.flags & layer_state_t::eLayerHidden;
1162}
1163
David Sodman41fdfc92017-11-06 16:09:56 -08001164uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001165 // TODO: should we do something special if mSecure is set?
1166 if (mProtectedByApp) {
1167 // need a hardware-protected path to external video sink
1168 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001169 }
Riley Andrews03414a12014-07-01 14:22:59 -07001170 if (mPotentialCursor) {
1171 usage |= GraphicBuffer::USAGE_CURSOR;
1172 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001173 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001174 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001175}
1176
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001177void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001178 uint32_t orientation = 0;
Vishnu Nair5eb3f062019-04-08 08:21:03 -07001179 // Disable setting transform hint if the debug flag is set.
1180 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001181 // The transform hint is used to improve performance, but we can
1182 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001183 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001184 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001185 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001186 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001187 orientation = 0;
1188 }
1189 }
David Sodmaneb085e02017-10-05 18:49:04 -07001190 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001191}
1192
Mathias Agopian13127d82013-03-05 17:47:11 -08001193// ----------------------------------------------------------------------------
1194// debugging
1195// ----------------------------------------------------------------------------
1196
Marissa Wall61c58622018-07-18 10:12:20 -07001197// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001198LayerDebugInfo Layer::getLayerDebugInfo() const {
1199 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001200 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001201 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001202 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001203 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
chaviw8a01fa42019-08-19 12:39:31 -07001204 info.mType = getType();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001205 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Lloyd Piquea2468662019-03-07 21:31:06 -08001206
1207 info.mVisibleRegion = debugGetVisibleRegionOnDefaultDisplay();
Kalle Raitaa099a242017-01-11 11:17:29 -08001208 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001209 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001210 info.mX = ds.active_legacy.transform.tx();
1211 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001212 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001213 info.mWidth = ds.active_legacy.w;
1214 info.mHeight = ds.active_legacy.h;
1215 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001216 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001217 info.mFlags = ds.flags;
1218 info.mPixelFormat = getPixelFormat();
chaviw4244e032019-09-04 11:27:49 -07001219 info.mDataSpace = static_cast<android_dataspace>(getDataSpace());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001220 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1221 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1222 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1223 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001224 {
chaviwd62d3062019-09-04 14:48:02 -07001225 sp<const GraphicBuffer> buffer = getBuffer();
David Sodman5b4cffc2017-11-23 13:20:29 -08001226 if (buffer != 0) {
1227 info.mActiveBufferWidth = buffer->getWidth();
1228 info.mActiveBufferHeight = buffer->getHeight();
1229 info.mActiveBufferStride = buffer->getStride();
1230 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001231 } else {
1232 info.mActiveBufferWidth = 0;
1233 info.mActiveBufferHeight = 0;
1234 info.mActiveBufferStride = 0;
1235 info.mActiveBufferFormat = 0;
1236 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001237 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001238 info.mNumQueuedFrames = getQueuedFrameCount();
1239 info.mRefreshPending = isBufferLatched();
1240 info.mIsOpaque = isOpaque(ds);
1241 info.mContentDirty = contentDirty;
1242 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001243}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001244
Yiwei Zhang5434a782018-12-05 18:06:32 -08001245void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001246 result.append("-------------------------------");
1247 result.append("-------------------------------");
1248 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001249 result.append(" Layer name\n");
1250 result.append(" Z | ");
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001251 result.append(" Window Type | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001252 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001253 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001254 result.append(" Disp Frame (LTRB) | ");
1255 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001256 result.append("-------------------------------");
1257 result.append("-------------------------------");
1258 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001259}
1260
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001261void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1262 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1263 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001264 return;
1265 }
1266
Yiwei Zhang5434a782018-12-05 18:06:32 -08001267 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001268 if (mName.length() > 77) {
1269 std::string shortened;
1270 shortened.append(mName.string(), 36);
1271 shortened.append("[...]");
1272 shortened.append(mName.string() + (mName.length() - 36), 36);
Yiwei Zhang5434a782018-12-05 18:06:32 -08001273 name = shortened;
Dan Stozae22aec72016-08-01 13:20:59 -07001274 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001275 name = std::string(mName.string(), mName.size());
Dan Stozae22aec72016-08-01 13:20:59 -07001276 }
1277
Yiwei Zhang5434a782018-12-05 18:06:32 -08001278 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001279
Alec Mourib416efd2018-09-06 21:01:59 +00001280 const State& layerState(getDrawingState());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001281 const auto& compositionState = outputLayer->getState();
1282
Chia-I Wu1e043612018-03-01 09:45:09 -08001283 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001284 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001285 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001286 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001287 }
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001288 StringAppendF(&result, " %10d | ", mWindowType);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001289 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
1290 StringAppendF(&result, "%10s | ",
1291 toString(getCompositionLayer() ? compositionState.bufferTransform
1292 : static_cast<Hwc2::Transform>(0))
1293 .c_str());
1294 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001295 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001296 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001297 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1298 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001299
Yichi Chen6ca35192018-05-29 12:20:43 +08001300 result.append("- - - - - - - - - - - - - - - -");
1301 result.append("- - - - - - - - - - - - - - - -");
1302 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001303}
Dan Stozae22aec72016-08-01 13:20:59 -07001304
Yiwei Zhang5434a782018-12-05 18:06:32 -08001305void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001306 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001307}
1308
Svetoslavd85084b2014-03-20 10:28:31 -07001309void Layer::clearFrameStats() {
1310 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001311}
1312
Jamie Gennis6547ff42013-07-16 20:12:42 -07001313void Layer::logFrameStats() {
1314 mFrameTracker.logAndResetStats(mName);
1315}
1316
Svetoslavd85084b2014-03-20 10:28:31 -07001317void Layer::getFrameStats(FrameStats* outStats) const {
1318 mFrameTracker.getStats(outStats);
1319}
1320
Yiwei Zhang5434a782018-12-05 18:06:32 -08001321void Layer::dumpFrameEvents(std::string& result) {
chaviw8a01fa42019-08-19 12:39:31 -07001322 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().string(), getType(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001323 Mutex::Autolock lock(mFrameEventHistoryMutex);
1324 mFrameEventHistory.checkFencesForCompletion();
1325 mFrameEventHistory.dump(result);
1326}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001327
Vishnu Nair0f085c62019-08-30 08:49:12 -07001328void Layer::dumpCallingUidPid(std::string& result) const {
1329 StringAppendF(&result, "Layer %s (%s) pid:%d uid:%d\n", getName().string(), getType(),
1330 mCallingPid, mCallingUid);
1331}
1332
Brian Anderson5ea5e592016-12-01 16:54:33 -08001333void Layer::onDisconnect() {
1334 Mutex::Autolock lock(mFrameEventHistoryMutex);
1335 mFrameEventHistory.onDisconnect();
Mikael Pessa90092f42019-08-26 17:22:04 -07001336 const int32_t layerID = getSequence();
1337 mFlinger->mTimeStats->onDestroy(layerID);
1338 mFlinger->mFrameTracer->onDestroy(layerID);
Brian Anderson5ea5e592016-12-01 16:54:33 -08001339}
1340
Brian Anderson3890c392016-07-25 12:48:08 -07001341void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001342 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001343 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001344 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1345 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001346 }
1347
Brian Andersond6927fb2016-07-23 23:37:30 -07001348 Mutex::Autolock lock(mFrameEventHistoryMutex);
1349 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001350 // If there are any unsignaled fences in the aquire timeline at this
1351 // point, the previously queued frame hasn't been latched yet. Go ahead
1352 // and try to get the signal time here so the syscall is taken out of
1353 // the main thread's critical path.
1354 mAcquireTimeline.updateSignalTimes();
1355 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001356 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001357 mFrameEventHistory.addQueue(*newTimestamps);
1358 }
1359
Brian Anderson3890c392016-07-25 12:48:08 -07001360 if (outDelta) {
1361 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001362 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001363}
Dan Stozae77c7662016-05-13 11:37:28 -07001364
Chia-I Wu98f1c102017-05-30 14:54:08 -07001365size_t Layer::getChildrenCount() const {
1366 size_t count = 0;
1367 for (const sp<Layer>& child : mCurrentChildren) {
1368 count += 1 + child->getChildrenCount();
1369 }
1370 return count;
1371}
1372
Robert Carr1f0a16a2016-10-24 16:27:39 -07001373void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001374 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001375 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001376
Robert Carr1f0a16a2016-10-24 16:27:39 -07001377 mCurrentChildren.add(layer);
1378 layer->setParent(this);
1379}
1380
1381ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001382 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001383 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001384
Robert Carr1323c952019-01-28 18:13:27 -08001385 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001386 return mCurrentChildren.remove(layer);
1387}
1388
Robert Carr1db73f62016-12-21 12:58:51 -08001389bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1390 sp<Handle> handle = nullptr;
1391 sp<Layer> newParent = nullptr;
1392 if (newParentHandle == nullptr) {
1393 return false;
1394 }
1395 handle = static_cast<Handle*>(newParentHandle.get());
1396 newParent = handle->owner.promote();
1397 if (newParent == nullptr) {
1398 ALOGE("Unable to promote Layer handle");
1399 return false;
1400 }
1401
chaviw5aedec92018-10-22 10:40:38 -07001402 if (attachChildren()) {
1403 setTransactionFlags(eTransactionNeeded);
1404 }
Robert Carr1db73f62016-12-21 12:58:51 -08001405 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001406 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001407 }
1408 mCurrentChildren.clear();
1409
1410 return true;
1411}
1412
Robert Carr15eae092018-03-23 13:43:53 -07001413void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001414 for (const sp<Layer>& child : mDrawingChildren) {
1415 child->mDrawingParent = newParent;
Vishnu Nairc652ff82019-03-15 12:48:54 -07001416 child->computeBounds(newParent->mBounds,
1417 newParent->getTransformWithScale(
1418 newParent->getBufferScaleTransform()));
Robert Carr578038f2018-03-09 12:25:24 -08001419 }
1420}
1421
chaviwf1961f72017-09-18 16:41:07 -07001422bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001423 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001424
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001425 // While layers are detached, we allow most operations
1426 // and simply halt performing the actual transaction. However
1427 // for reparent != null we would enter the mRemovedFromCurrentState
1428 // state, regardless of whether doTransaction was called, and
1429 // so we need to prevent the update here.
1430 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001431 return false;
1432 }
1433
Robert Carr54cf5b12019-01-25 14:02:28 -08001434 sp<Layer> newParent;
1435 if (newParentHandle != nullptr) {
1436 auto handle = static_cast<Handle*>(newParentHandle.get());
1437 newParent = handle->owner.promote();
1438 if (newParent == nullptr) {
1439 ALOGE("Unable to promote Layer handle");
1440 return false;
1441 }
1442 if (newParent == this) {
1443 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1444 return false;
1445 }
1446 }
1447
chaviwf1961f72017-09-18 16:41:07 -07001448 sp<Layer> parent = getParent();
1449 if (parent != nullptr) {
1450 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001451 }
1452
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001453 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001454 newParent->addChild(this);
1455 if (!newParent->isRemovedFromCurrentState()) {
1456 addToCurrentState();
1457 } else {
1458 onRemovedFromCurrentState();
1459 }
1460
1461 if (mLayerDetached) {
1462 mLayerDetached = false;
1463 callSetTransactionFlags = true;
1464 }
1465 } else {
1466 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001467 }
1468
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001469 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001470 setTransactionFlags(eTransactionNeeded);
1471 }
chaviw06178942017-07-27 10:25:59 -07001472 return true;
1473}
1474
Robert Carr9524cb32017-02-13 11:32:32 -08001475bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001476 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001477 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001478 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001479 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001480 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001481 child->detachChildren();
chaviw43cb3cb2019-05-31 15:23:41 -07001482 child->removeRemoteSyncPoints();
Robert Carr9524cb32017-02-13 11:32:32 -08001483 }
Robert Carr7f619b22017-11-06 12:56:35 -08001484 }
Robert Carr9524cb32017-02-13 11:32:32 -08001485
1486 return true;
1487}
1488
chaviw5aedec92018-10-22 10:40:38 -07001489bool Layer::attachChildren() {
1490 bool changed = false;
1491 for (const sp<Layer>& child : mCurrentChildren) {
1492 sp<Client> parentClient = mClientRef.promote();
1493 sp<Client> client(child->mClientRef.promote());
1494 if (client != nullptr && parentClient != client) {
1495 if (child->mLayerDetached) {
1496 child->mLayerDetached = false;
1497 changed = true;
1498 }
1499 changed |= child->attachChildren();
1500 }
1501 }
1502
1503 return changed;
1504}
1505
Peiyong Lind3788632018-09-18 16:01:31 -07001506bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001507 static const mat4 identityMatrix = mat4();
1508
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001509 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001510 return false;
1511 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001512 ++mCurrentState.sequence;
1513 mCurrentState.colorTransform = matrix;
1514 mCurrentState.hasColorTransform = matrix != identityMatrix;
1515 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001516 setTransactionFlags(eTransactionNeeded);
1517 return true;
1518}
1519
chaviwf66724d2018-11-28 16:35:21 -08001520mat4 Layer::getColorTransform() const {
1521 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1522 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1523 colorTransform = parent->getColorTransform() * colorTransform;
1524 }
1525 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001526}
1527
1528bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001529 bool hasColorTransform = getDrawingState().hasColorTransform;
1530 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1531 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1532 }
1533 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001534}
1535
Chia-I Wu11481472018-05-04 10:43:19 -07001536bool Layer::isLegacyDataSpace() const {
1537 // return true when no higher bits are set
chaviw4244e032019-09-04 11:27:49 -07001538 return !(getDataSpace() &
1539 (ui::Dataspace::STANDARD_MASK | ui::Dataspace::TRANSFER_MASK |
1540 ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001541}
1542
Robert Carr1f0a16a2016-10-24 16:27:39 -07001543void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001544 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001545}
1546
Robert Carr1f0a16a2016-10-24 16:27:39 -07001547int32_t Layer::getZ() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001548 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001549}
1550
Robert Carr1c5481e2019-07-01 14:42:27 -07001551bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) const {
Robert Carr29abff82017-12-04 13:51:20 -08001552 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001553 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviwe5ac40f2019-09-24 16:36:55 -07001554 return state.isRelativeOf;
Robert Carr29abff82017-12-04 13:51:20 -08001555}
1556
David Sodman41fdfc92017-11-06 16:09:56 -08001557__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001558 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001559 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1560 "makeTraversalList received invalid stateSet");
1561 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1562 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001563 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001564
Robert Carr29abff82017-12-04 13:51:20 -08001565 if (state.zOrderRelatives.size() == 0) {
1566 *outSkipRelativeZUsers = true;
1567 return children;
1568 }
1569
chaviwfd462612018-05-31 16:11:27 -07001570 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001571 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001572 sp<Layer> strongRelative = weakRelative.promote();
1573 if (strongRelative != nullptr) {
1574 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001575 }
1576 }
1577
Dan Stoza412903f2017-04-27 13:42:17 -07001578 for (const sp<Layer>& child : children) {
chaviwe5ac40f2019-09-24 16:36:55 -07001579 if (child->usingRelativeZ(stateSet)) {
Robert Carr503c7042017-09-27 15:06:08 -07001580 continue;
1581 }
Robert Carrdb66e622017-04-10 16:55:57 -07001582 traverse.add(child);
1583 }
1584
1585 return traverse;
1586}
1587
Robert Carr1f0a16a2016-10-24 16:27:39 -07001588/**
Robert Carrdb66e622017-04-10 16:55:57 -07001589 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001590 */
Dan Stoza412903f2017-04-27 13:42:17 -07001591void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001592 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1593 // produce a new list for traversing, including our relatives, and not including our children
1594 // who are relatives of another surface. In the case that there are no relative Z,
1595 // makeTraversalList returns our children directly to avoid significant overhead.
1596 // However in this case we need to take the responsibility for filtering children which
1597 // are relatives of another surface here.
1598 bool skipRelativeZUsers = false;
1599 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001600
Robert Carr1f0a16a2016-10-24 16:27:39 -07001601 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001602 for (; i < list.size(); i++) {
1603 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001604 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1605 continue;
1606 }
1607
Robert Carrdb66e622017-04-10 16:55:57 -07001608 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001609 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001610 }
Dan Stoza412903f2017-04-27 13:42:17 -07001611 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001612 }
Robert Carr29abff82017-12-04 13:51:20 -08001613
Dan Stoza412903f2017-04-27 13:42:17 -07001614 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001615 for (; i < list.size(); i++) {
1616 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001617
Robert Carr29abff82017-12-04 13:51:20 -08001618 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1619 continue;
1620 }
Dan Stoza412903f2017-04-27 13:42:17 -07001621 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001622 }
1623}
1624
1625/**
Robert Carrdb66e622017-04-10 16:55:57 -07001626 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001627 */
Dan Stoza412903f2017-04-27 13:42:17 -07001628void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1629 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001630 // See traverseInZOrder for documentation.
1631 bool skipRelativeZUsers = false;
1632 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001633
Robert Carr1f0a16a2016-10-24 16:27:39 -07001634 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001635 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001636 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001637
1638 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1639 continue;
1640 }
1641
Robert Carrdb66e622017-04-10 16:55:57 -07001642 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001643 break;
1644 }
Dan Stoza412903f2017-04-27 13:42:17 -07001645 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001646 }
Dan Stoza412903f2017-04-27 13:42:17 -07001647 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001648 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001649 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001650
1651 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1652 continue;
1653 }
1654
Dan Stoza412903f2017-04-27 13:42:17 -07001655 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001656 }
1657}
1658
chaviw4b129c22018-04-09 16:19:43 -07001659LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1660 const std::vector<Layer*>& layersInTree) {
1661 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1662 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001663 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1664 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001665 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001666
chaviwfd462612018-05-31 16:11:27 -07001667 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001668 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1669 sp<Layer> strongRelative = weakRelative.promote();
1670 // Only add relative layers that are also descendents of the top most parent of the tree.
1671 // If a relative layer is not a descendent, then it should be ignored.
1672 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1673 traverse.add(strongRelative);
1674 }
1675 }
1676
1677 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001678 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001679 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1680 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1681 // the child here since it won't be added later as a relative.
1682 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1683 childState.zOrderRelativeOf.promote().get())) {
1684 continue;
1685 }
1686 traverse.add(child);
1687 }
1688
1689 return traverse;
1690}
1691
1692void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1693 LayerVector::StateSet stateSet,
1694 const LayerVector::Visitor& visitor) {
1695 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001696
1697 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001698 for (; i < list.size(); i++) {
1699 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001700 if (relative->getZ() >= 0) {
1701 break;
1702 }
chaviw4b129c22018-04-09 16:19:43 -07001703 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001704 }
chaviw4b129c22018-04-09 16:19:43 -07001705
chaviwa76b2712017-09-20 12:02:26 -07001706 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001707 for (; i < list.size(); i++) {
1708 const auto& relative = list[i];
1709 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001710 }
1711}
1712
chaviw4b129c22018-04-09 16:19:43 -07001713std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1714 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1715 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1716
1717 std::vector<Layer*> layersInTree = {this};
1718 for (size_t i = 0; i < children.size(); i++) {
1719 const auto& child = children[i];
1720 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1721 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1722 }
1723
1724 return layersInTree;
1725}
1726
1727void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1728 const LayerVector::Visitor& visitor) {
1729 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1730 std::sort(layersInTree.begin(), layersInTree.end());
1731 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1732}
1733
Peiyong Linefefaac2018-08-17 12:27:51 -07001734ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001735 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001736}
1737
chaviw13fdc492017-06-27 12:40:18 -07001738half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001739 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001740
chaviw13fdc492017-06-27 12:40:18 -07001741 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1742 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001743}
Robert Carr6452f122017-03-21 10:41:29 -07001744
chaviw13fdc492017-06-27 12:40:18 -07001745half4 Layer::getColor() const {
1746 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001747 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001748}
Robert Carr6452f122017-03-21 10:41:29 -07001749
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001750Layer::RoundedCornerState Layer::getRoundedCornerState() const {
1751 const auto& p = mDrawingParent.promote();
1752 if (p != nullptr) {
Peiyong Lin27016a92019-03-29 17:36:08 +00001753 RoundedCornerState parentState = p->getRoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001754 if (parentState.radius > 0) {
1755 ui::Transform t = getActiveTransform(getDrawingState());
1756 t = t.inverse();
1757 parentState.cropRect = t.transform(parentState.cropRect);
1758 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1759 // but a transform matrix can define horizontal and vertical scales.
1760 // Let's take the average between both of them and pass into the shader, practically we
1761 // never do this type of transformation on windows anyway.
1762 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
1763 return parentState;
1764 }
1765 }
1766 const float radius = getDrawingState().cornerRadius;
Peiyong Linb9ff23e2019-04-08 11:04:59 -07001767 return radius > 0 && getCrop(getDrawingState()).isValid()
1768 ? RoundedCornerState(getCrop(getDrawingState()).toFloatRect(), radius)
1769 : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001770}
1771
Robert Carr1f0a16a2016-10-24 16:27:39 -07001772void Layer::commitChildList() {
1773 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1774 const auto& child = mCurrentChildren[i];
1775 child->commitChildList();
1776 }
1777 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001778 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001779}
1780
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001781static wp<Layer> extractLayerFromBinder(const wp<IBinder>& weakBinderHandle) {
1782 if (weakBinderHandle == nullptr) {
1783 return nullptr;
1784 }
1785 sp<IBinder> binderHandle = weakBinderHandle.promote();
1786 if (binderHandle == nullptr) {
1787 return nullptr;
1788 }
1789 sp<Layer::Handle> handle = static_cast<Layer::Handle*>(binderHandle.get());
1790 if (handle == nullptr) {
1791 return nullptr;
1792 }
1793 return handle->owner;
1794}
1795
Robert Carr720e5062018-07-30 17:45:14 -07001796void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001797 mCurrentState.inputInfo = info;
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001798 mCurrentState.touchableRegionCrop = extractLayerFromBinder(info.touchableRegionCropHandle);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001799 mCurrentState.modified = true;
1800 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07001801 setTransactionFlags(eTransactionNeeded);
1802}
1803
Vishnu Nair8406fd72019-07-30 11:29:31 -07001804void Layer::writeToProtoDrawingState(LayerProto* layerInfo, uint32_t traceFlags) const {
1805 ui::Transform transform = getTransform();
1806
1807 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
1808 for (const auto& pendingState : mPendingStatesSnapshot) {
1809 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
1810 if (barrierLayer != nullptr) {
1811 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
1812 barrierLayerProto->set_id(barrierLayer->sequence);
1813 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
1814 }
1815 }
1816
chaviwd62d3062019-09-04 14:48:02 -07001817 auto buffer = getBuffer();
Vishnu Nair8406fd72019-07-30 11:29:31 -07001818 if (buffer != nullptr) {
1819 LayerProtoHelper::writeToProto(buffer,
1820 [&]() { return layerInfo->mutable_active_buffer(); });
chaviw4244e032019-09-04 11:27:49 -07001821 LayerProtoHelper::writeToProto(ui::Transform(getBufferTransform()),
Vishnu Nair8406fd72019-07-30 11:29:31 -07001822 layerInfo->mutable_buffer_transform());
1823 }
1824 layerInfo->set_invalidate(contentDirty);
1825 layerInfo->set_is_protected(isProtected());
chaviw4244e032019-09-04 11:27:49 -07001826 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(getDataSpace())));
Vishnu Nair8406fd72019-07-30 11:29:31 -07001827 layerInfo->set_queued_frames(getQueuedFrameCount());
1828 layerInfo->set_refresh_pending(isBufferLatched());
1829 layerInfo->set_curr_frame(mCurrentFrameNumber);
1830 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
1831
1832 layerInfo->set_corner_radius(getRoundedCornerState().radius);
1833 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1834 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
1835 [&]() { return layerInfo->mutable_position(); });
1836 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
Lloyd Piquea2468662019-03-07 21:31:06 -08001837 LayerProtoHelper::writeToProto(debugGetVisibleRegionOnDefaultDisplay(),
Vishnu Nair8406fd72019-07-30 11:29:31 -07001838 [&]() { return layerInfo->mutable_visible_region(); });
1839 LayerProtoHelper::writeToProto(surfaceDamageRegion,
1840 [&]() { return layerInfo->mutable_damage_region(); });
1841 }
1842
1843 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
1844 LayerProtoHelper::writeToProto(mSourceBounds,
1845 [&]() { return layerInfo->mutable_source_bounds(); });
1846 LayerProtoHelper::writeToProto(mScreenBounds,
1847 [&]() { return layerInfo->mutable_screen_bounds(); });
1848 }
1849}
1850
1851void Layer::writeToProtoCommonState(LayerProto* layerInfo, LayerVector::StateSet stateSet,
1852 uint32_t traceFlags) const {
chaviw1d044282017-09-27 12:19:28 -07001853 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1854 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001855 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07001856
Peiyong Linefefaac2018-08-17 12:27:51 -07001857 ui::Transform requestedTransform = state.active_legacy.transform;
chaviw1d044282017-09-27 12:19:28 -07001858
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001859 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
1860 layerInfo->set_id(sequence);
1861 layerInfo->set_name(getName().c_str());
chaviw8a01fa42019-08-19 12:39:31 -07001862 layerInfo->set_type(getType());
chaviw1d044282017-09-27 12:19:28 -07001863
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001864 for (const auto& child : children) {
1865 layerInfo->add_children(child->sequence);
chaviw1d044282017-09-27 12:19:28 -07001866 }
chaviw1d044282017-09-27 12:19:28 -07001867
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001868 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1869 sp<Layer> strongRelative = weakRelative.promote();
1870 if (strongRelative != nullptr) {
1871 layerInfo->add_relatives(strongRelative->sequence);
1872 }
chaviwadc40c22018-07-10 16:57:27 -07001873 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001874
1875 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
1876 [&]() { return layerInfo->mutable_transparent_region(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001877
1878 layerInfo->set_layer_stack(getLayerStack());
1879 layerInfo->set_z(state.z);
1880
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001881 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(),
1882 [&]() {
1883 return layerInfo->mutable_requested_position();
1884 });
1885
1886 LayerProtoHelper::writeSizeToProto(state.active_legacy.w, state.active_legacy.h,
1887 [&]() { return layerInfo->mutable_size(); });
1888
1889 LayerProtoHelper::writeToProto(state.crop_legacy,
1890 [&]() { return layerInfo->mutable_crop(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001891
1892 layerInfo->set_is_opaque(isOpaque(state));
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001893
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001894
1895 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1896 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
1897 LayerProtoHelper::writeToProto(state.color,
1898 [&]() { return layerInfo->mutable_requested_color(); });
1899 layerInfo->set_flags(state.flags);
1900
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001901 LayerProtoHelper::writeToProto(requestedTransform,
1902 layerInfo->mutable_requested_transform());
1903
1904 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
1905 if (parent != nullptr) {
1906 layerInfo->set_parent(parent->sequence);
1907 } else {
1908 layerInfo->set_parent(-1);
1909 }
1910
1911 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1912 if (zOrderRelativeOf != nullptr) {
1913 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1914 } else {
1915 layerInfo->set_z_order_relative_of(-1);
1916 }
chaviwadc40c22018-07-10 16:57:27 -07001917 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001918
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001919 if (traceFlags & SurfaceTracing::TRACE_INPUT) {
1920 LayerProtoHelper::writeToProto(state.inputInfo, state.touchableRegionCrop,
1921 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001922 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001923
1924 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
1925 auto protoMap = layerInfo->mutable_metadata();
1926 for (const auto& entry : state.metadata.mMap) {
1927 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
1928 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001929 }
chaviw1d044282017-09-27 12:19:28 -07001930}
1931
Robert Carr2e102c92018-10-23 12:11:15 -07001932bool Layer::isRemovedFromCurrentState() const {
1933 return mRemovedFromCurrentState;
1934}
1935
Arthur Hungd20b2702019-01-14 18:16:16 +08001936InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001937 InputWindowInfo info = mDrawingState.inputInfo;
Robert Carr720e5062018-07-30 17:45:14 -07001938
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001939 if (info.displayId == ADISPLAY_ID_NONE) {
chaviwb15ea8a2019-09-03 12:40:22 -07001940 info.displayId = getLayerStack();
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001941 }
1942
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001943 ui::Transform t = getTransform();
1944 const float xScale = t.sx();
1945 const float yScale = t.sy();
Ady Abraham282f1d72019-07-24 18:05:56 -07001946 int32_t xSurfaceInset = info.surfaceInset;
1947 int32_t ySurfaceInset = info.surfaceInset;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001948 if (xScale != 1.0f || yScale != 1.0f) {
Ady Abraham282f1d72019-07-24 18:05:56 -07001949 info.windowXScale *= (xScale != 0.0f) ? 1.0f / xScale : 0.0f;
1950 info.windowYScale *= (yScale != 0.0f) ? 1.0f / yScale : 0.0f;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001951 info.touchableRegion.scaleSelf(xScale, yScale);
Ady Abraham282f1d72019-07-24 18:05:56 -07001952 xSurfaceInset = std::round(xSurfaceInset * xScale);
1953 ySurfaceInset = std::round(ySurfaceInset * yScale);
Riddle Hsu39d4aa52018-11-30 20:46:53 +08001954 }
Robert Carre07e1032018-11-26 12:55:53 -08001955
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001956 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001957 // If this is a portal window, set the touchableRegion to the layerBounds.
1958 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
1959 ? getBufferSize(getDrawingState())
1960 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08001961 if (!layerBounds.isValid()) {
1962 layerBounds = getCroppedBufferSize(getDrawingState());
1963 }
Vishnu Nair8033a492018-12-05 07:27:23 -08001964 layerBounds = t.transform(layerBounds);
Ady Abraham282f1d72019-07-24 18:05:56 -07001965
1966 // clamp inset to layer bounds
1967 xSurfaceInset = (xSurfaceInset >= 0) ? std::min(xSurfaceInset, layerBounds.getWidth() / 2) : 0;
1968 ySurfaceInset = (ySurfaceInset >= 0) ? std::min(ySurfaceInset, layerBounds.getHeight() / 2) : 0;
1969
Arthur Hung118b1142019-05-08 21:25:59 +08001970 layerBounds.inset(xSurfaceInset, ySurfaceInset, xSurfaceInset, ySurfaceInset);
Vishnu Nair8033a492018-12-05 07:27:23 -08001971
Arthur Hungd20b2702019-01-14 18:16:16 +08001972 // Input coordinate should match the layer bounds.
1973 info.frameLeft = layerBounds.left;
1974 info.frameTop = layerBounds.top;
1975 info.frameRight = layerBounds.right;
1976 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08001977
1978 // Position the touchable region relative to frame screen location and restrict it to frame
1979 // bounds.
1980 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08001981 info.visible = canReceiveInput();
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001982
1983 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
1984 if (info.replaceTouchableRegionWithCrop) {
1985 if (cropLayer == nullptr) {
1986 info.touchableRegion = Region(Rect{mScreenBounds});
1987 } else {
1988 info.touchableRegion = Region(Rect{cropLayer->mScreenBounds});
1989 }
1990 } else if (cropLayer != nullptr) {
1991 info.touchableRegion = info.touchableRegion.intersect(Rect{cropLayer->mScreenBounds});
1992 }
1993
Robert Carr720e5062018-07-30 17:45:14 -07001994 return info;
1995}
1996
1997bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001998 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07001999}
2000
Lloyd Piquefeb73d72018-12-04 17:23:44 -08002001std::shared_ptr<compositionengine::Layer> Layer::getCompositionLayer() const {
2002 return nullptr;
2003}
2004
chaviw3e727cd2019-01-31 13:41:05 -08002005bool Layer::canReceiveInput() const {
Vishnu Nair82353e92019-09-12 12:38:47 -07002006 return !isHiddenByPolicy();
chaviw3e727cd2019-01-31 13:41:05 -08002007}
2008
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002009compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
2010 const sp<const DisplayDevice>& display) const {
2011 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionLayer().get());
2012}
2013
Lloyd Piquea2468662019-03-07 21:31:06 -08002014Region Layer::debugGetVisibleRegionOnDefaultDisplay() const {
2015 sp<DisplayDevice> displayDevice = mFlinger->getDefaultDisplayDeviceLocked();
2016 if (displayDevice == nullptr) {
2017 return {};
2018 }
2019
2020 auto outputLayer = findOutputLayerForDisplay(displayDevice);
2021 if (outputLayer == nullptr) {
2022 return {};
2023 }
2024
2025 return outputLayer->getState().visibleRegion;
2026}
2027
chaviwb4c6e582019-08-16 14:35:07 -07002028void Layer::setInitialValuesForClone(const sp<Layer>& clonedFrom) {
2029 // copy drawing state from cloned layer
2030 mDrawingState = clonedFrom->mDrawingState;
2031 mClonedFrom = clonedFrom;
2032
2033 // TODO: (b/140756730) Ignore input for now since InputDispatcher doesn't support multiple
2034 // InputWindows per client token yet.
2035 mDrawingState.inputInfo.token = nullptr;
2036}
Mathias Agopian13127d82013-03-05 17:47:11 -08002037// ---------------------------------------------------------------------------
2038
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002039}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002040
2041#if defined(__gl_h_)
2042#error "don't include gl/gl.h in this file"
2043#endif
2044
2045#if defined(__gl2_h_)
2046#error "don't include gl2/gl2.h in this file"
2047#endif