blob: 37caab2b3feaa9d35528a7e961a1700575680f49 [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
Lloyd Pique0449b0f2018-12-20 16:23:45 -080086 mCurrentState.active_legacy.w = args.w;
87 mCurrentState.active_legacy.h = args.h;
88 mCurrentState.flags = layerFlags;
89 mCurrentState.active_legacy.transform.set(0, 0);
90 mCurrentState.crop_legacy.makeInvalid();
91 mCurrentState.requestedCrop_legacy = mCurrentState.crop_legacy;
92 mCurrentState.z = 0;
93 mCurrentState.color.a = 1.0f;
94 mCurrentState.layerStack = 0;
95 mCurrentState.sequence = 0;
96 mCurrentState.requested_legacy = mCurrentState.active_legacy;
Lloyd Pique0449b0f2018-12-20 16:23:45 -080097 mCurrentState.active.w = UINT32_MAX;
98 mCurrentState.active.h = UINT32_MAX;
99 mCurrentState.active.transform.set(0, 0);
100 mCurrentState.transform = 0;
101 mCurrentState.transformToDisplayInverse = false;
102 mCurrentState.crop.makeInvalid();
103 mCurrentState.acquireFence = new Fence(-1);
104 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
105 mCurrentState.hdrMetadata.validTypes = 0;
Marissa Wall7e0a01f2019-08-14 14:29:25 -0700106 mCurrentState.surfaceDamageRegion = Region::INVALID_REGION;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800107 mCurrentState.cornerRadius = 0.0f;
108 mCurrentState.api = -1;
109 mCurrentState.hasColorTransform = false;
Peiyong Linc502cb72019-03-01 15:00:23 -0800110 mCurrentState.colorSpaceAgnostic = false;
Evan Roskya1f1e152019-01-24 16:17:46 -0800111 mCurrentState.metadata = args.metadata;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700112
113 // drawing state & current state are identical
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800114 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700115
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800116 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700117 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800118 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200119 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700120
Ady Abraham8f1ee7f2019-04-05 10:32:50 -0700121 mSchedulerLayerHandle = mFlinger->mScheduler->registerLayer(mName.c_str(), mWindowType);
Vishnu Nair0f085c62019-08-30 08:49:12 -0700122 mCallingPid = args.callingPid;
123 mCallingUid = args.callingUid;
Robert Carr2e102c92018-10-23 12:11:15 -0700124 mFlinger->onLayerCreated();
Dan Stoza436ccf32018-06-21 12:10:12 -0700125}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700126
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700127Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800128 sp<Client> c(mClientRef.promote());
129 if (c != 0) {
130 c->detachLayer(this);
131 }
132
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000133 mFrameTracker.logAndResetStats(mName);
chaviw74d90ad2019-04-26 14:45:26 -0700134 mFlinger->onLayerDestroyed(this);
Mathias Agopian96f08192010-06-02 23:28:45 -0700135}
136
Vishnu Nair0f085c62019-08-30 08:49:12 -0700137LayerCreationArgs::LayerCreationArgs(SurfaceFlinger* flinger, const sp<Client>& client,
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700138 std::string name, uint32_t w, uint32_t h, uint32_t flags,
Vishnu Nair0f085c62019-08-30 08:49:12 -0700139 LayerMetadata metadata)
140 : flinger(flinger),
141 client(client),
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700142 name(std::move(name)),
Vishnu Nair0f085c62019-08-30 08:49:12 -0700143 w(w),
144 h(h),
145 flags(flags),
146 metadata(std::move(metadata)) {
147 IPCThreadState* ipc = IPCThreadState::self();
148 callingPid = ipc->getCallingPid();
149 callingUid = ipc->getCallingUid();
150}
151
Mathias Agopian13127d82013-03-05 17:47:11 -0800152// ---------------------------------------------------------------------------
153// callbacks
154// ---------------------------------------------------------------------------
155
David Sodmaneb085e02017-10-05 18:49:04 -0700156/*
157 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
158 * Layer. So, the implementation is done in BufferLayer. When called on a
159 * ColorLayer object, it's essentially a NOP.
160 */
David Sodmaneb085e02017-10-05 18:49:04 -0700161void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800162
chaviw43cb3cb2019-05-31 15:23:41 -0700163void Layer::removeRemoteSyncPoints() {
164 for (auto& point : mRemoteSyncPoints) {
165 point->setTransactionApplied();
166 }
167 mRemoteSyncPoints.clear();
168
169 {
chaviw43cb3cb2019-05-31 15:23:41 -0700170 for (State pendingState : mPendingStates) {
171 pendingState.barrierLayer_legacy = nullptr;
172 }
173 }
174}
175
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700176void Layer::removeRelativeZ(const std::vector<Layer*>& layersInTree) {
177 if (mCurrentState.zOrderRelativeOf == nullptr) {
178 return;
179 }
Robert Carr2e102c92018-10-23 12:11:15 -0700180
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700181 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
182 if (strongRelative == nullptr) {
183 setZOrderRelativeOf(nullptr);
184 return;
185 }
186
187 if (!std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
188 strongRelative->removeZOrderRelative(this);
189 mFlinger->setTransactionFlags(eTraversalNeeded);
chaviw606e5cf2019-03-01 10:12:10 -0800190 setZOrderRelativeOf(nullptr);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700191 }
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700192}
193
194void Layer::removeFromCurrentState() {
195 mRemovedFromCurrentState = true;
Rob Carr4bba3702018-10-08 21:53:30 +0000196
Robert Carr2e102c92018-10-23 12:11:15 -0700197 // Since we are no longer reachable from CurrentState SurfaceFlinger
198 // will no longer invoke doTransaction for us, and so we will
199 // never finish applying transactions. We signal the sync point
200 // now so that another layer will not become indefinitely
201 // blocked.
chaviw43cb3cb2019-05-31 15:23:41 -0700202 removeRemoteSyncPoints();
Robert Carr2e102c92018-10-23 12:11:15 -0700203
204 {
205 Mutex::Autolock syncLock(mLocalSyncPointMutex);
206 for (auto& point : mLocalSyncPoints) {
207 point->setFrameAvailable();
208 }
209 mLocalSyncPoints.clear();
210 }
211
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800212 mFlinger->markLayerPendingRemovalLocked(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700213}
Chia-I Wu38512252017-05-17 14:36:16 -0700214
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700215void Layer::onRemovedFromCurrentState() {
216 auto layersInTree = getLayersInTree(LayerVector::StateSet::Current);
217 std::sort(layersInTree.begin(), layersInTree.end());
218 for (const auto& layer : layersInTree) {
219 layer->removeFromCurrentState();
220 layer->removeRelativeZ(layersInTree);
221 }
222}
223
chaviw61626f22018-11-15 16:26:27 -0800224void Layer::addToCurrentState() {
225 mRemovedFromCurrentState = false;
226
227 for (const auto& child : mCurrentChildren) {
228 child->addToCurrentState();
229 }
230}
231
Mathias Agopian13127d82013-03-05 17:47:11 -0800232// ---------------------------------------------------------------------------
233// set-up
234// ---------------------------------------------------------------------------
235
chaviw13fdc492017-06-27 12:40:18 -0700236bool Layer::getPremultipledAlpha() const {
237 return mPremultipliedAlpha;
238}
239
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700240sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800241 Mutex::Autolock _l(mLock);
Robert Carrc0df3122019-04-11 13:18:21 -0700242 if (mGetHandleCalled) {
243 ALOGE("Get handle called twice" );
244 return nullptr;
245 }
246 mGetHandleCalled = true;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700247 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800248}
249
250// ---------------------------------------------------------------------------
251// h/w composer set-up
252// ---------------------------------------------------------------------------
253
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700254static Rect reduce(const Rect& win, const Region& exclude) {
255 if (CC_LIKELY(exclude.isEmpty())) {
256 return win;
257 }
258 if (exclude.isRect()) {
259 return win.reduce(exclude.getBounds());
260 }
261 return Region(win).subtract(exclude).getBounds();
262}
263
Dan Stoza80d61162017-12-20 15:57:52 -0800264static FloatRect reduce(const FloatRect& win, const Region& exclude) {
265 if (CC_LIKELY(exclude.isEmpty())) {
266 return win;
267 }
268 // Convert through Rect (by rounding) for lack of FloatRegion
269 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
270}
271
Vishnu Nair4351ad52019-02-11 14:13:02 -0800272Rect Layer::getScreenBounds(bool reduceTransparentRegion) const {
Vishnu Nairf0c28512019-02-08 12:40:28 -0800273 if (!reduceTransparentRegion) {
274 return Rect{mScreenBounds};
275 }
276
277 FloatRect bounds = getBounds();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800278 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800279 // Transform to screen space.
280 bounds = t.transform(bounds);
281 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700282}
283
Vishnu Nair4351ad52019-02-11 14:13:02 -0800284FloatRect Layer::getBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000285 const State& s(getDrawingState());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800286 return getBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700287}
288
Vishnu Nairf0c28512019-02-08 12:40:28 -0800289FloatRect Layer::getBounds(const Region& activeTransparentRegion) const {
290 // Subtract the transparent region and snap to the bounds.
291 return reduce(mBounds, activeTransparentRegion);
292}
293
Vishnu Nairc652ff82019-03-15 12:48:54 -0700294ui::Transform Layer::getBufferScaleTransform() const {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800295 // If the layer is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
296 // it isFixedSize) then there may be additional scaling not accounted
Vishnu Nairc652ff82019-03-15 12:48:54 -0700297 // for in the layer transform.
chaviwd62d3062019-09-04 14:48:02 -0700298 if (!isFixedSize() || getBuffer() == nullptr) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700299 return {};
Vishnu Nair4351ad52019-02-11 14:13:02 -0800300 }
301
Marissa Wall290ad082019-03-06 13:23:47 -0800302 // If the layer is a buffer state layer, the active width and height
303 // could be infinite. In that case, return the effective transform.
304 const uint32_t activeWidth = getActiveWidth(getDrawingState());
305 const uint32_t activeHeight = getActiveHeight(getDrawingState());
306 if (activeWidth >= UINT32_MAX && activeHeight >= UINT32_MAX) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700307 return {};
Marissa Wall290ad082019-03-06 13:23:47 -0800308 }
309
chaviwd62d3062019-09-04 14:48:02 -0700310 int bufferWidth = getBuffer()->getWidth();
311 int bufferHeight = getBuffer()->getHeight();
Vishnu Nairc652ff82019-03-15 12:48:54 -0700312
chaviw4244e032019-09-04 11:27:49 -0700313 if (getBufferTransform() & NATIVE_WINDOW_TRANSFORM_ROT_90) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700314 std::swap(bufferWidth, bufferHeight);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800315 }
Vishnu Nairc652ff82019-03-15 12:48:54 -0700316
Marissa Wall290ad082019-03-06 13:23:47 -0800317 float sx = activeWidth / static_cast<float>(bufferWidth);
318 float sy = activeHeight / static_cast<float>(bufferHeight);
319
Vishnu Nair4351ad52019-02-11 14:13:02 -0800320 ui::Transform extraParentScaling;
321 extraParentScaling.set(sx, 0, 0, sy);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700322 return extraParentScaling;
323}
324
325ui::Transform Layer::getTransformWithScale(const ui::Transform& bufferScaleTransform) const {
326 // We need to mirror this scaling to child surfaces or we will break the contract where WM can
327 // treat child surfaces as pixels in the parent surface.
chaviwd62d3062019-09-04 14:48:02 -0700328 if (!isFixedSize() || getBuffer() == nullptr) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700329 return mEffectiveTransform;
330 }
331 return mEffectiveTransform * bufferScaleTransform;
332}
333
334FloatRect Layer::getBoundsPreScaling(const ui::Transform& bufferScaleTransform) const {
335 // We need the pre scaled layer bounds when computing child bounds to make sure the child is
336 // cropped to its parent layer after any buffer transform scaling is applied.
chaviwd62d3062019-09-04 14:48:02 -0700337 if (!isFixedSize() || getBuffer() == nullptr) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700338 return mBounds;
339 }
340 return bufferScaleTransform.inverse().transform(mBounds);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800341}
342
343void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform) {
344 const State& s(getDrawingState());
345
346 // Calculate effective layer transform
347 mEffectiveTransform = parentTransform * getActiveTransform(s);
348
349 // Transform parent bounds to layer space
350 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
351
Vishnu Nairc652ff82019-03-15 12:48:54 -0700352 // Calculate source bounds
Vishnu Nair4351ad52019-02-11 14:13:02 -0800353 mSourceBounds = computeSourceBounds(parentBounds);
354
355 // Calculate bounds by croping diplay frame with layer crop and parent bounds
356 FloatRect bounds = mSourceBounds;
357 const Rect layerCrop = getCrop(s);
358 if (!layerCrop.isEmpty()) {
359 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
360 }
361 bounds = bounds.intersect(parentBounds);
362
363 mBounds = bounds;
364 mScreenBounds = mEffectiveTransform.transform(mBounds);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700365
366 // Add any buffer scaling to the layer's children.
367 ui::Transform bufferScaleTransform = getBufferScaleTransform();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800368 for (const sp<Layer>& child : mDrawingChildren) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700369 child->computeBounds(getBoundsPreScaling(bufferScaleTransform),
370 getTransformWithScale(bufferScaleTransform));
Vishnu Nair4351ad52019-02-11 14:13:02 -0800371 }
372}
373
Vishnu Nair60356342018-11-13 13:00:45 -0800374Rect Layer::getCroppedBufferSize(const State& s) const {
375 Rect size = getBufferSize(s);
376 Rect crop = getCrop(s);
377 if (!crop.isEmpty() && size.isValid()) {
378 size.intersect(crop, &size);
379 } else if (!crop.isEmpty()) {
380 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700381 }
Vishnu Nair60356342018-11-13 13:00:45 -0800382 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800383}
384
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700385void Layer::setupRoundedCornersCropCoordinates(Rect win,
386 const FloatRect& roundedCornersCrop) const {
387 // Translate win by the rounded corners rect coordinates, to have all values in
388 // layer coordinate space.
389 win.left -= roundedCornersCrop.left;
390 win.right -= roundedCornersCrop.left;
391 win.top -= roundedCornersCrop.top;
392 win.bottom -= roundedCornersCrop.top;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700393}
394
Lloyd Piquec6687342019-03-07 21:34:57 -0800395void Layer::latchBasicGeometry(compositionengine::LayerFECompositionState& compositionState) const {
396 const auto& drawingState{getDrawingState()};
397 const uint32_t layerStack = getLayerStack();
398 const auto alpha = static_cast<float>(getAlpha());
399 const bool opaque = isOpaque(drawingState);
400 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
401
402 auto blendMode = Hwc2::IComposerClient::BlendMode::NONE;
403 if (!opaque || alpha != 1.0f) {
404 blendMode = mPremultipliedAlpha ? Hwc2::IComposerClient::BlendMode::PREMULTIPLIED
405 : Hwc2::IComposerClient::BlendMode::COVERAGE;
406 }
407
408 // TODO(b/121291683): Instead of filling in a passed-in compositionState
409 // structure, switch to Layer owning the structure and have
410 // CompositionEngine be able to get a reference to it.
411
412 compositionState.layerStackId =
413 (layerStack != ~0u) ? std::make_optional(layerStack) : std::nullopt;
414 compositionState.internalOnly = getPrimaryDisplayOnly();
415 compositionState.isVisible = isVisible();
416 compositionState.isOpaque = opaque && !usesRoundedCorners && alpha == 1.f;
417
418 compositionState.contentDirty = contentDirty;
419 contentDirty = false;
420
421 compositionState.geomLayerBounds = mBounds;
422 compositionState.geomLayerTransform = getTransform();
423 compositionState.geomInverseLayerTransform = compositionState.geomLayerTransform.inverse();
424 compositionState.transparentRegionHint = getActiveTransparentRegion(drawingState);
425
426 compositionState.blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
427 compositionState.alpha = alpha;
428}
429
Lloyd Piquea83776c2019-01-29 18:42:32 -0800430void Layer::latchGeometry(compositionengine::LayerFECompositionState& compositionState) const {
431 const auto& drawingState{getDrawingState()};
Mathias Agopian13127d82013-03-05 17:47:11 -0800432
Lloyd Piquea83776c2019-01-29 18:42:32 -0800433 int type = drawingState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
434 int appId = drawingState.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700435 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400436 if (parent.get()) {
437 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800438 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
439 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
Jiwen 'Steve' Cai736c74e2019-05-28 18:33:22 -0700440 if (parentType > 0 && parentAppId > 0) {
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800441 type = parentType;
442 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700443 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400444 }
445
Lloyd Piquea83776c2019-01-29 18:42:32 -0800446 compositionState.geomBufferSize = getBufferSize(drawingState);
chaviw4244e032019-09-04 11:27:49 -0700447 compositionState.geomContentCrop = getBufferCrop();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800448 compositionState.geomCrop = getCrop(drawingState);
chaviw4244e032019-09-04 11:27:49 -0700449 compositionState.geomBufferTransform = getBufferTransform();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800450 compositionState.geomBufferUsesDisplayInverseTransform = getTransformToDisplayInverse();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800451 compositionState.geomUsesSourceCrop = usesSourceCrop();
452 compositionState.isSecure = isSecure();
David Sodman15094112018-10-11 09:39:37 -0700453
Lloyd Piquea83776c2019-01-29 18:42:32 -0800454 compositionState.type = type;
455 compositionState.appId = appId;
456}
David Sodmanba340492018-08-05 21:51:33 -0700457
Lloyd Piquef5275482019-01-29 18:42:42 -0800458void Layer::latchPerFrameState(compositionengine::LayerFECompositionState& compositionState) const {
Lloyd Pique688abd42019-02-15 15:42:24 -0800459 const auto& drawingState{getDrawingState()};
Lloyd Piquef5275482019-01-29 18:42:42 -0800460 compositionState.forceClientComposition = false;
461
Lloyd Piquef5275482019-01-29 18:42:42 -0800462 compositionState.isColorspaceAgnostic = isColorSpaceAgnostic();
chaviw4244e032019-09-04 11:27:49 -0700463 compositionState.dataspace = getDataSpace();
Lloyd Piquef5275482019-01-29 18:42:42 -0800464 compositionState.colorTransform = getColorTransform();
465 compositionState.colorTransformIsIdentity = !hasColorTransform();
466 compositionState.surfaceDamage = surfaceDamageRegion;
Lloyd Pique688abd42019-02-15 15:42:24 -0800467 compositionState.hasProtectedContent = isProtected();
468
469 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
470 compositionState.isOpaque =
471 isOpaque(drawingState) && !usesRoundedCorners && getAlpha() == 1.0_hf;
Lloyd Piquef5275482019-01-29 18:42:42 -0800472
473 // Force client composition for special cases known only to the front-end.
Lloyd Pique688abd42019-02-15 15:42:24 -0800474 if (isHdrY410() || usesRoundedCorners) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800475 compositionState.forceClientComposition = true;
476 }
477}
478
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800479void Layer::latchCursorCompositionState(
480 compositionengine::LayerFECompositionState& compositionState) const {
481 // This gives us only the "orientation" component of the transform
482 const State& drawingState{getDrawingState()};
483
484 // Apply the layer's transform, followed by the display's global transform
485 // Here we're guaranteed that the layer's transform preserves rects
486 Rect win = getCroppedBufferSize(drawingState);
487 // Subtract the transparent region and snap to the bounds
488 Rect bounds = reduce(win, getActiveTransparentRegion(drawingState));
489 Rect frame(getTransform().transform(bounds));
490
491 compositionState.cursorFrame = frame;
492}
493
Lloyd Piquef16688f2019-02-19 17:47:57 -0800494bool Layer::onPreComposition(nsecs_t) {
495 return false;
496}
497
Lloyd Piquea83776c2019-01-29 18:42:32 -0800498void Layer::latchCompositionState(compositionengine::LayerFECompositionState& compositionState,
Lloyd Piquec6687342019-03-07 21:34:57 -0800499 compositionengine::LayerFE::StateSubset subset) const {
500 using StateSubset = compositionengine::LayerFE::StateSubset;
Lloyd Piquef5275482019-01-29 18:42:42 -0800501
Lloyd Piquec6687342019-03-07 21:34:57 -0800502 switch (subset) {
503 case StateSubset::BasicGeometry:
504 latchBasicGeometry(compositionState);
505 break;
506
507 case StateSubset::GeometryAndContent:
508 latchBasicGeometry(compositionState);
509 latchGeometry(compositionState);
510 latchPerFrameState(compositionState);
511 break;
512
513 case StateSubset::Content:
514 latchPerFrameState(compositionState);
515 break;
516 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800517}
Mathias Agopian29a367b2011-07-12 14:51:45 -0700518
Lloyd Piquea83776c2019-01-29 18:42:32 -0800519const char* Layer::getDebugName() const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700520 return mName.c_str();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800521}
522
Mathias Agopian13127d82013-03-05 17:47:11 -0800523// ---------------------------------------------------------------------------
524// drawing...
525// ---------------------------------------------------------------------------
526
Lloyd Piquef16688f2019-02-19 17:47:57 -0800527std::optional<renderengine::LayerSettings> Layer::prepareClientComposition(
528 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
529 if (!getCompositionLayer()) {
530 return {};
531 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800532
Vishnu Nair4351ad52019-02-11 14:13:02 -0800533 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000534 half alpha = getAlpha();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800535 renderengine::LayerSettings layerSettings;
536 layerSettings.geometry.boundaries = bounds;
537 if (targetSettings.useIdentityTransform) {
538 layerSettings.geometry.positionTransform = mat4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000539 } else {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800540 layerSettings.geometry.positionTransform = getTransform().asMatrix4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000541 }
542
543 if (hasColorTransform()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800544 layerSettings.colorTransform = getColorTransform();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000545 }
546
547 const auto roundedCornerState = getRoundedCornerState();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800548 layerSettings.geometry.roundedCornersRadius = roundedCornerState.radius;
549 layerSettings.geometry.roundedCornersCrop = roundedCornerState.cropRect;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000550
Lloyd Piquef16688f2019-02-19 17:47:57 -0800551 layerSettings.alpha = alpha;
chaviw4244e032019-09-04 11:27:49 -0700552 layerSettings.sourceDataspace = getDataSpace();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800553 return layerSettings;
Mathias Agopian13127d82013-03-05 17:47:11 -0800554}
555
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800556Hwc2::IComposerClient::Composition Layer::getCompositionType(
557 const sp<const DisplayDevice>& display) const {
558 const auto outputLayer = findOutputLayerForDisplay(display);
559 LOG_FATAL_IF(!outputLayer);
560 return outputLayer->getState().hwc ? (*outputLayer->getState().hwc).hwcCompositionType
561 : Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800562}
563
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800564bool Layer::getClearClientTarget(const sp<const DisplayDevice>& display) const {
565 const auto outputLayer = findOutputLayerForDisplay(display);
566 LOG_FATAL_IF(!outputLayer);
567 return outputLayer->getState().clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800568}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800569
Dan Stozacac35382016-01-27 12:21:06 -0800570bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
571 if (point->getFrameNumber() <= mCurrentFrameNumber) {
572 // Don't bother with a SyncPoint, since we've already latched the
573 // relevant frame
574 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700575 }
Robert Carr2e102c92018-10-23 12:11:15 -0700576 if (isRemovedFromCurrentState()) {
577 return false;
578 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700579
Dan Stozacac35382016-01-27 12:21:06 -0800580 Mutex::Autolock lock(mLocalSyncPointMutex);
581 mLocalSyncPoints.push_back(point);
582 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700583}
584
Mathias Agopian13127d82013-03-05 17:47:11 -0800585// ----------------------------------------------------------------------------
586// local state
587// ----------------------------------------------------------------------------
588
David Sodman41fdfc92017-11-06 16:09:56 -0800589bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800590 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700591 return (s.flags & layer_state_t::eLayerSecure);
592}
593
Mathias Agopian13127d82013-03-05 17:47:11 -0800594// ----------------------------------------------------------------------------
595// transaction
596// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800597
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800598void Layer::pushPendingState() {
599 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700600 return;
601 }
Alec Mourie60041e2019-06-14 18:59:51 -0700602 ATRACE_CALL();
Dan Stoza7dde5992015-05-22 09:51:44 -0700603
Dan Stoza7dde5992015-05-22 09:51:44 -0700604 // If this transaction is waiting on the receipt of a frame, generate a sync
605 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700606 // We don't allow installing sync points after we are removed from the current state
607 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800608 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
609 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800610 if (barrierLayer == nullptr) {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700611 ALOGE("[%s] Unable to promote barrier Layer.", getDebugName());
Dan Stoza7dde5992015-05-22 09:51:44 -0700612 // If we can't promote the layer we are intended to wait on,
613 // then it is expired or otherwise invalid. Allow this transaction
614 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800615 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800616 } else {
chaviw43cb3cb2019-05-31 15:23:41 -0700617 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy, this);
Robert Carr0d480722017-01-10 16:42:54 -0800618 if (barrierLayer->addSyncPoint(syncPoint)) {
Alec Mourie60041e2019-06-14 18:59:51 -0700619 std::stringstream ss;
620 ss << "Adding sync point " << mCurrentState.frameNumber_legacy;
621 ATRACE_NAME(ss.str().c_str());
Dan Stozacac35382016-01-27 12:21:06 -0800622 mRemoteSyncPoints.push_back(std::move(syncPoint));
623 } else {
624 // We already missed the frame we're supposed to synchronize
625 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800626 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800627 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700628 }
629
Dan Stoza7dde5992015-05-22 09:51:44 -0700630 // Wake us up to check if the frame has been received
631 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700632 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700633 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800634 mPendingStates.push_back(mCurrentState);
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700635 ATRACE_INT(mTransactionName.c_str(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700636}
637
Pablo Ceballos05289c22016-04-14 15:49:55 -0700638void Layer::popPendingState(State* stateToCommit) {
Alec Mourie60041e2019-06-14 18:59:51 -0700639 ATRACE_CALL();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800640 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700641
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800642 mPendingStates.removeAt(0);
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700643 ATRACE_INT(mTransactionName.c_str(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700644}
645
Pablo Ceballos05289c22016-04-14 15:49:55 -0700646bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700647 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800648 while (!mPendingStates.empty()) {
649 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700650 if (mRemoteSyncPoints.empty()) {
651 // If we don't have a sync point for this, apply it anyway. It
652 // will be visually wrong, but it should keep us from getting
653 // into too much trouble.
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700654 ALOGE("[%s] No local sync point found", getDebugName());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700655 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700656 stateUpdateAvailable = true;
657 continue;
658 }
659
Marissa Wallf58c14b2018-07-24 10:50:43 -0700660 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800661 mPendingStates[0].frameNumber_legacy) {
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700662 ALOGE("[%s] Unexpected sync point frame number found", getDebugName());
Dan Stozacac35382016-01-27 12:21:06 -0800663
664 // Signal our end of the sync point and then dispose of it
665 mRemoteSyncPoints.front()->setTransactionApplied();
666 mRemoteSyncPoints.pop_front();
667 continue;
668 }
669
Dan Stoza7dde5992015-05-22 09:51:44 -0700670 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
Alec Mourie60041e2019-06-14 18:59:51 -0700671 ATRACE_NAME("frameIsAvailable");
Dan Stoza7dde5992015-05-22 09:51:44 -0700672 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700673 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700674 stateUpdateAvailable = true;
675
676 // Signal our end of the sync point and then dispose of it
677 mRemoteSyncPoints.front()->setTransactionApplied();
678 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800679 } else {
Alec Mourie60041e2019-06-14 18:59:51 -0700680 ATRACE_NAME("!frameIsAvailable");
Dan Stoza792e5292016-02-11 11:43:58 -0800681 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700682 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700683 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700684 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700685 stateUpdateAvailable = true;
686 }
687 }
688
689 // If we still have pending updates, wake SurfaceFlinger back up and point
690 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800691 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700692 setTransactionFlags(eTransactionNeeded);
693 mFlinger->setTransactionFlags(eTraversalNeeded);
694 }
695
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800696 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700697 return stateUpdateAvailable;
698}
699
Marissa Wall61c58622018-07-18 10:12:20 -0700700uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000701 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800702
Marissa Wall61c58622018-07-18 10:12:20 -0700703 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
704 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700705
David Sodmaneb085e02017-10-05 18:49:04 -0700706 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700707 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000708 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800709 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
710 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
711 " requested={ wh={%4u,%4u} }}\n"
712 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
713 " requested={ wh={%4u,%4u} }}\n",
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700714 this, getName().c_str(), getBufferTransform(), getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700715 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
716 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
717 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
718 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
719 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700720 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
721 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
722 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800723 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700724
Robert Carre392b552017-09-19 12:16:05 -0700725 // Don't let Layer::doTransaction update the drawing state
726 // if we have a pending resize, unless we are in fixed-size mode.
727 // the drawing state will be updated only once we receive a buffer
728 // with the correct size.
729 //
730 // In particular, we want to make sure the clip (which is part
731 // of the geometry state) is latched together with the size but is
732 // latched immediately when no resizing is involved.
733 //
734 // If a sideband stream is attached, however, we want to skip this
735 // optimization so that transactions aren't missed when a buffer
736 // never arrives
737 //
738 // In the case that we don't have a buffer we ignore other factors
739 // and avoid entering the resizePending state. At a high level the
740 // resizePending state is to avoid applying the state of the new buffer
741 // to the old buffer. However in the state where we don't have an old buffer
742 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700743 const bool resizePending =
744 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
745 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
chaviwd62d3062019-09-04 14:48:02 -0700746 (getBuffer() != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700747 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800748 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700749 flags |= eDontUpdateGeometryState;
750 }
751 }
752
Robert Carr7bf247e2017-05-18 14:02:49 -0700753 // Here we apply various requested geometry states, depending on our
754 // latching configuration. See Layer.h for a detailed discussion of
755 // how geometry latching is controlled.
756 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000757 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700758
Robert Carr7bf247e2017-05-18 14:02:49 -0700759 // There is an awkward asymmetry in the handling of the crop states in the position
760 // states, as can be seen below. Largely this arises from position and transform
761 // being stored in the same data structure while having different latching rules.
762 // b/38182305
763 //
Marissa Wall61c58622018-07-18 10:12:20 -0700764 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700765 // applyPendingStates in the presence of deferred transactions.
chaviw214c89d2019-09-04 16:03:53 -0700766 editCurrentState.active_legacy = editCurrentState.requested_legacy;
767 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Mathias Agopian13127d82013-03-05 17:47:11 -0800768 }
769
Marissa Wall61c58622018-07-18 10:12:20 -0700770 return flags;
771}
772
773uint32_t Layer::doTransaction(uint32_t flags) {
774 ATRACE_CALL();
775
chaviw5aedec92018-10-22 10:40:38 -0700776 if (mLayerDetached) {
Robert Carr321e83c2019-08-19 15:49:30 -0700777 // Ensure BLAST buffer callbacks are processed.
778 // detachChildren and mLayerDetached were implemented to avoid geometry updates
779 // to layers in the cases of animation. For BufferQueue layers buffers are still
780 // consumed as normal. This is useful as otherwise the client could get hung
781 // inevitably waiting on a buffer to return. We recreate this semantic for BufferQueue
782 // even though it is a little consistent. detachChildren is shortly slated for removal
783 // by the hierarchy mirroring work so we don't need to worry about it too much.
784 mDrawingState.callbackHandles = mCurrentState.callbackHandles;
785 mCurrentState.callbackHandles = {};
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800786 return flags;
787 }
788
789 if (mChildrenChanged) {
790 flags |= eVisibleRegion;
791 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -0700792 }
793
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800794 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +0000795 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -0700796 if (!applyPendingStates(&c)) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800797 return flags;
Marissa Wall61c58622018-07-18 10:12:20 -0700798 }
799
800 flags = doTransactionResize(flags, &c);
801
Alec Mourib416efd2018-09-06 21:01:59 +0000802 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700803
804 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800805 // invalidate and recompute the visible regions if needed
806 flags |= Layer::eVisibleRegion;
807 }
808
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700809 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800810 // invalidate and recompute the visible regions if needed
811 flags |= eVisibleRegion;
812 this->contentDirty = true;
813
814 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -0700815 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -0700816 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -0800817 }
818
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800819 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -0700820 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800821 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -0700822 }
823
Mathias Agopian13127d82013-03-05 17:47:11 -0800824 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -0700825 commitTransaction(c);
Vishnu Nair8406fd72019-07-30 11:29:31 -0700826 mPendingStatesSnapshot = mPendingStates;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800827 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -0800828 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800829}
830
Pablo Ceballos05289c22016-04-14 15:49:55 -0700831void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800832 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700833}
834
Mathias Agopian13127d82013-03-05 17:47:11 -0800835uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800836 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800837}
838
839uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800840 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -0800841}
842
chaviw214c89d2019-09-04 16:03:53 -0700843bool Layer::setPosition(float x, float y) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800844 if (mCurrentState.requested_legacy.transform.tx() == x &&
845 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -0800846 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800847 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -0700848
849 // We update the requested and active position simultaneously because
850 // we want to apply the position portion of the transform matrix immediately,
851 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800852 mCurrentState.requested_legacy.transform.set(x, y);
chaviw214c89d2019-09-04 16:03:53 -0700853 // Here we directly update the active state
854 // unlike other setters, because we store it within
855 // the transform, but use different latching rules.
856 // b/38182305
857 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr69663fb2016-03-27 19:59:19 -0700858
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800859 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800860 setTransactionFlags(eTransactionNeeded);
861 return true;
862}
Robert Carr82364e32016-05-15 11:27:47 -0700863
Robert Carr1f0a16a2016-10-24 16:27:39 -0700864bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
865 ssize_t idx = mCurrentChildren.indexOf(childLayer);
866 if (idx < 0) {
867 return false;
868 }
869 if (childLayer->setLayer(z)) {
870 mCurrentChildren.removeAt(idx);
871 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800872 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700873 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800874 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700875}
876
Robert Carr503c7042017-09-27 15:06:08 -0700877bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
878 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
879 ssize_t idx = mCurrentChildren.indexOf(childLayer);
880 if (idx < 0) {
881 return false;
882 }
883 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
884 mCurrentChildren.removeAt(idx);
885 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800886 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700887 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800888 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700889}
890
Robert Carrae060832016-11-28 10:51:00 -0800891bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800892 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
893 mCurrentState.sequence++;
894 mCurrentState.z = z;
895 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700896
897 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800898 if (mCurrentState.zOrderRelativeOf != nullptr) {
899 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700900 if (strongRelative != nullptr) {
901 strongRelative->removeZOrderRelative(this);
902 }
chaviw606e5cf2019-03-01 10:12:10 -0800903 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -0700904 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800905 setTransactionFlags(eTransactionNeeded);
906 return true;
907}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700908
Robert Carrdb66e622017-04-10 16:55:57 -0700909void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800910 mCurrentState.zOrderRelatives.remove(relative);
911 mCurrentState.sequence++;
912 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700913 setTransactionFlags(eTransactionNeeded);
914}
915
916void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800917 mCurrentState.zOrderRelatives.add(relative);
918 mCurrentState.modified = true;
919 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -0700920 setTransactionFlags(eTransactionNeeded);
921}
922
chaviw606e5cf2019-03-01 10:12:10 -0800923void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
924 mCurrentState.zOrderRelativeOf = relativeOf;
925 mCurrentState.sequence++;
926 mCurrentState.modified = true;
chaviwbdb8b802019-10-14 09:17:12 -0700927 mCurrentState.isRelativeOf = relativeOf != nullptr;
928
chaviw606e5cf2019-03-01 10:12:10 -0800929 setTransactionFlags(eTransactionNeeded);
930}
931
Robert Carr503d2bd2017-12-04 15:49:47 -0800932bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -0700933 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
934 if (handle == nullptr) {
935 return false;
936 }
937 sp<Layer> relative = handle->owner.promote();
938 if (relative == nullptr) {
939 return false;
940 }
941
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800942 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
943 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -0800944 return false;
945 }
946
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800947 mCurrentState.sequence++;
948 mCurrentState.modified = true;
949 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -0700950
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800951 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -0700952 if (oldZOrderRelativeOf != nullptr) {
953 oldZOrderRelativeOf->removeZOrderRelative(this);
954 }
chaviw606e5cf2019-03-01 10:12:10 -0800955 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -0700956 relative->addZOrderRelative(this);
957
958 setTransactionFlags(eTransactionNeeded);
959
960 return true;
961}
962
Mathias Agopian13127d82013-03-05 17:47:11 -0800963bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800964 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -0700965 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800966 mCurrentState.requested_legacy.w = w;
967 mCurrentState.requested_legacy.h = h;
968 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800969 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -0700970
971 // record the new size, from this point on, when the client request
972 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800973 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -0800974 return true;
975}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800976bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800977 if (mCurrentState.color.a == alpha) return false;
978 mCurrentState.sequence++;
979 mCurrentState.color.a = alpha;
980 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800981 setTransactionFlags(eTransactionNeeded);
982 return true;
983}
chaviw13fdc492017-06-27 12:40:18 -0700984
Valerie Haudd0b7572019-01-29 14:59:27 -0800985bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
986 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -0700987 return false;
Valerie Hauaa194562019-02-05 16:21:38 -0800988 }
989 mCurrentState.sequence++;
990 mCurrentState.modified = true;
991 setTransactionFlags(eTransactionNeeded);
992
993 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -0800994 // create background color layer if one does not yet exist
995 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700996 std::string name = mName + "BackgroundColorLayer";
Lloyd Pique958be492019-03-28 15:34:59 -0700997 mCurrentState.bgColorLayer = mFlinger->getFactory().createColorLayer(
Dominik Laskowski87a07e42019-10-10 20:38:02 -0700998 LayerCreationArgs(mFlinger.get(), nullptr, std::move(name), 0, 0, flags,
999 LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -07001000
Valerie Haudd0b7572019-01-29 14:59:27 -08001001 // add to child list
1002 addChild(mCurrentState.bgColorLayer);
1003 mFlinger->mLayersAdded = true;
1004 // set up SF to handle added color layer
1005 if (isRemovedFromCurrentState()) {
1006 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1007 }
1008 mFlinger->setTransactionFlags(eTransactionNeeded);
1009 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1010 mCurrentState.bgColorLayer->reparent(nullptr);
1011 mCurrentState.bgColorLayer = nullptr;
1012 return true;
1013 }
1014
1015 mCurrentState.bgColorLayer->setColor(color);
1016 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1017 mCurrentState.bgColorLayer->setAlpha(alpha);
1018 mCurrentState.bgColorLayer->setDataspace(dataspace);
1019
chaviw13fdc492017-06-27 12:40:18 -07001020 return true;
1021}
1022
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001023bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001024 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001025
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001026 mCurrentState.sequence++;
1027 mCurrentState.cornerRadius = cornerRadius;
1028 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001029 setTransactionFlags(eTransactionNeeded);
1030 return true;
1031}
1032
Robert Carrd4ae7f32018-06-07 16:10:57 -07001033bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1034 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001035 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001036 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1037
1038 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1039 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1040 return false;
1041 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001042 mCurrentState.sequence++;
1043 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1044 matrix.dsdy);
1045 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001046 setTransactionFlags(eTransactionNeeded);
1047 return true;
1048}
Marissa Wall61c58622018-07-18 10:12:20 -07001049
Mathias Agopian13127d82013-03-05 17:47:11 -08001050bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001051 mCurrentState.requestedTransparentRegion_legacy = transparent;
1052 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001053 setTransactionFlags(eTransactionNeeded);
1054 return true;
1055}
1056bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001057 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1058 if (mCurrentState.flags == newFlags) return false;
1059 mCurrentState.sequence++;
1060 mCurrentState.flags = newFlags;
1061 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001062 setTransactionFlags(eTransactionNeeded);
1063 return true;
1064}
Robert Carr99e27f02016-06-16 15:18:02 -07001065
chaviw214c89d2019-09-04 16:03:53 -07001066bool Layer::setCrop_legacy(const Rect& crop) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001067 if (mCurrentState.requestedCrop_legacy == crop) return false;
1068 mCurrentState.sequence++;
1069 mCurrentState.requestedCrop_legacy = crop;
chaviw214c89d2019-09-04 16:03:53 -07001070 mCurrentState.crop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001071
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001072 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001073 setTransactionFlags(eTransactionNeeded);
1074 return true;
1075}
Robert Carr8d5227b2017-03-16 15:41:03 -07001076
Robert Carrc3574f72016-03-24 12:19:32 -07001077bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001078 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001079 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001080 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001081 return true;
1082}
1083
Evan Roskyef876c92019-01-25 17:46:06 -08001084bool Layer::setMetadata(const LayerMetadata& data) {
1085 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001086 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001087 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001088 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001089 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001090}
1091
Mathias Agopian13127d82013-03-05 17:47:11 -08001092bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001093 if (mCurrentState.layerStack == layerStack) return false;
1094 mCurrentState.sequence++;
1095 mCurrentState.layerStack = layerStack;
1096 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001097 setTransactionFlags(eTransactionNeeded);
1098 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001099}
1100
Peiyong Linc502cb72019-03-01 15:00:23 -08001101bool Layer::setColorSpaceAgnostic(const bool agnostic) {
1102 if (mCurrentState.colorSpaceAgnostic == agnostic) {
1103 return false;
1104 }
1105 mCurrentState.sequence++;
1106 mCurrentState.colorSpaceAgnostic = agnostic;
1107 mCurrentState.modified = true;
1108 setTransactionFlags(eTransactionNeeded);
1109 return true;
1110}
1111
Robert Carr1f0a16a2016-10-24 16:27:39 -07001112uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001113 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001114 if (p == nullptr) {
1115 return getDrawingState().layerStack;
1116 }
1117 return p->getLayerStack();
1118}
1119
Marissa Wallf58c14b2018-07-24 10:50:43 -07001120void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Alec Mourie60041e2019-06-14 18:59:51 -07001121 ATRACE_CALL();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001122 mCurrentState.barrierLayer_legacy = barrierLayer;
1123 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001124 // We don't set eTransactionNeeded, because just receiving a deferral
1125 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001126 mCurrentState.modified = true;
1127 pushPendingState();
1128 mCurrentState.barrierLayer_legacy = nullptr;
1129 mCurrentState.frameNumber_legacy = 0;
1130 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001131}
1132
Marissa Wallf58c14b2018-07-24 10:50:43 -07001133void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001134 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001135 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001136}
1137
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001138// ----------------------------------------------------------------------------
1139// pageflip handling...
1140// ----------------------------------------------------------------------------
1141
Robert Carr1f0a16a2016-10-24 16:27:39 -07001142bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001143 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001144 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001145 if (parent != nullptr && parent->isHiddenByPolicy()) {
1146 return true;
1147 }
Robert Carr1c5481e2019-07-01 14:42:27 -07001148 if (usingRelativeZ(LayerVector::StateSet::Drawing)) {
1149 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
1150 if (zOrderRelativeOf != nullptr) {
1151 if (zOrderRelativeOf->isHiddenByPolicy()) {
1152 return true;
1153 }
1154 }
1155 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001156 return s.flags & layer_state_t::eLayerHidden;
1157}
1158
David Sodman41fdfc92017-11-06 16:09:56 -08001159uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001160 // TODO: should we do something special if mSecure is set?
1161 if (mProtectedByApp) {
1162 // need a hardware-protected path to external video sink
1163 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001164 }
Riley Andrews03414a12014-07-01 14:22:59 -07001165 if (mPotentialCursor) {
1166 usage |= GraphicBuffer::USAGE_CURSOR;
1167 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001168 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001169 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001170}
1171
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001172void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001173 uint32_t orientation = 0;
Vishnu Nair5eb3f062019-04-08 08:21:03 -07001174 // Disable setting transform hint if the debug flag is set.
1175 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001176 // The transform hint is used to improve performance, but we can
1177 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001178 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001179 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001180 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001181 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001182 orientation = 0;
1183 }
1184 }
David Sodmaneb085e02017-10-05 18:49:04 -07001185 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001186}
1187
Mathias Agopian13127d82013-03-05 17:47:11 -08001188// ----------------------------------------------------------------------------
1189// debugging
1190// ----------------------------------------------------------------------------
1191
Marissa Wall61c58622018-07-18 10:12:20 -07001192// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001193LayerDebugInfo Layer::getLayerDebugInfo() const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001194 using namespace std::string_literals;
1195
Kalle Raitaa099a242017-01-11 11:17:29 -08001196 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001197 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001198 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001199 sp<Layer> parent = getParent();
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001200 info.mParentName = parent ? parent->getName() : "none"s;
chaviw8a01fa42019-08-19 12:39:31 -07001201 info.mType = getType();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001202 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Lloyd Piquea2468662019-03-07 21:31:06 -08001203
1204 info.mVisibleRegion = debugGetVisibleRegionOnDefaultDisplay();
Kalle Raitaa099a242017-01-11 11:17:29 -08001205 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001206 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001207 info.mX = ds.active_legacy.transform.tx();
1208 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001209 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001210 info.mWidth = ds.active_legacy.w;
1211 info.mHeight = ds.active_legacy.h;
1212 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001213 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001214 info.mFlags = ds.flags;
1215 info.mPixelFormat = getPixelFormat();
chaviw4244e032019-09-04 11:27:49 -07001216 info.mDataSpace = static_cast<android_dataspace>(getDataSpace());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001217 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1218 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1219 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1220 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001221 {
chaviwd62d3062019-09-04 14:48:02 -07001222 sp<const GraphicBuffer> buffer = getBuffer();
David Sodman5b4cffc2017-11-23 13:20:29 -08001223 if (buffer != 0) {
1224 info.mActiveBufferWidth = buffer->getWidth();
1225 info.mActiveBufferHeight = buffer->getHeight();
1226 info.mActiveBufferStride = buffer->getStride();
1227 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001228 } else {
1229 info.mActiveBufferWidth = 0;
1230 info.mActiveBufferHeight = 0;
1231 info.mActiveBufferStride = 0;
1232 info.mActiveBufferFormat = 0;
1233 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001234 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001235 info.mNumQueuedFrames = getQueuedFrameCount();
1236 info.mRefreshPending = isBufferLatched();
1237 info.mIsOpaque = isOpaque(ds);
1238 info.mContentDirty = contentDirty;
1239 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001240}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001241
Yiwei Zhang5434a782018-12-05 18:06:32 -08001242void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001243 result.append("-------------------------------");
1244 result.append("-------------------------------");
1245 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001246 result.append(" Layer name\n");
1247 result.append(" Z | ");
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001248 result.append(" Window Type | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001249 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001250 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001251 result.append(" Disp Frame (LTRB) | ");
1252 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001253 result.append("-------------------------------");
1254 result.append("-------------------------------");
1255 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001256}
1257
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001258void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1259 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1260 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001261 return;
1262 }
1263
Yiwei Zhang5434a782018-12-05 18:06:32 -08001264 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001265 if (mName.length() > 77) {
1266 std::string shortened;
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001267 shortened.append(mName, 0, 36);
Dan Stozae22aec72016-08-01 13:20:59 -07001268 shortened.append("[...]");
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001269 shortened.append(mName, mName.length() - 36);
1270 name = std::move(shortened);
Dan Stozae22aec72016-08-01 13:20:59 -07001271 } else {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001272 name = mName;
Dan Stozae22aec72016-08-01 13:20:59 -07001273 }
1274
Yiwei Zhang5434a782018-12-05 18:06:32 -08001275 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001276
Alec Mourib416efd2018-09-06 21:01:59 +00001277 const State& layerState(getDrawingState());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001278 const auto& compositionState = outputLayer->getState();
1279
Chia-I Wu1e043612018-03-01 09:45:09 -08001280 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001281 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001282 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001283 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001284 }
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001285 StringAppendF(&result, " %10d | ", mWindowType);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001286 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
1287 StringAppendF(&result, "%10s | ",
1288 toString(getCompositionLayer() ? compositionState.bufferTransform
1289 : static_cast<Hwc2::Transform>(0))
1290 .c_str());
1291 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001292 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001293 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001294 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1295 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001296
Yichi Chen6ca35192018-05-29 12:20:43 +08001297 result.append("- - - - - - - - - - - - - - - -");
1298 result.append("- - - - - - - - - - - - - - - -");
1299 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001300}
Dan Stozae22aec72016-08-01 13:20:59 -07001301
Yiwei Zhang5434a782018-12-05 18:06:32 -08001302void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001303 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001304}
1305
Svetoslavd85084b2014-03-20 10:28:31 -07001306void Layer::clearFrameStats() {
1307 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001308}
1309
Jamie Gennis6547ff42013-07-16 20:12:42 -07001310void Layer::logFrameStats() {
1311 mFrameTracker.logAndResetStats(mName);
1312}
1313
Svetoslavd85084b2014-03-20 10:28:31 -07001314void Layer::getFrameStats(FrameStats* outStats) const {
1315 mFrameTracker.getStats(outStats);
1316}
1317
Yiwei Zhang5434a782018-12-05 18:06:32 -08001318void Layer::dumpFrameEvents(std::string& result) {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001319 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().c_str(), getType(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001320 Mutex::Autolock lock(mFrameEventHistoryMutex);
1321 mFrameEventHistory.checkFencesForCompletion();
1322 mFrameEventHistory.dump(result);
1323}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001324
Vishnu Nair0f085c62019-08-30 08:49:12 -07001325void Layer::dumpCallingUidPid(std::string& result) const {
Dominik Laskowski87a07e42019-10-10 20:38:02 -07001326 StringAppendF(&result, "Layer %s (%s) pid:%d uid:%d\n", getName().c_str(), getType(),
Vishnu Nair0f085c62019-08-30 08:49:12 -07001327 mCallingPid, mCallingUid);
1328}
1329
Brian Anderson5ea5e592016-12-01 16:54:33 -08001330void Layer::onDisconnect() {
1331 Mutex::Autolock lock(mFrameEventHistoryMutex);
1332 mFrameEventHistory.onDisconnect();
Mikael Pessa90092f42019-08-26 17:22:04 -07001333 const int32_t layerID = getSequence();
1334 mFlinger->mTimeStats->onDestroy(layerID);
1335 mFlinger->mFrameTracer->onDestroy(layerID);
Brian Anderson5ea5e592016-12-01 16:54:33 -08001336}
1337
Brian Anderson3890c392016-07-25 12:48:08 -07001338void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001339 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001340 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001341 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1342 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001343 }
1344
Brian Andersond6927fb2016-07-23 23:37:30 -07001345 Mutex::Autolock lock(mFrameEventHistoryMutex);
1346 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001347 // If there are any unsignaled fences in the aquire timeline at this
1348 // point, the previously queued frame hasn't been latched yet. Go ahead
1349 // and try to get the signal time here so the syscall is taken out of
1350 // the main thread's critical path.
1351 mAcquireTimeline.updateSignalTimes();
1352 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001353 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001354 mFrameEventHistory.addQueue(*newTimestamps);
1355 }
1356
Brian Anderson3890c392016-07-25 12:48:08 -07001357 if (outDelta) {
1358 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001359 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001360}
Dan Stozae77c7662016-05-13 11:37:28 -07001361
Chia-I Wu98f1c102017-05-30 14:54:08 -07001362size_t Layer::getChildrenCount() const {
1363 size_t count = 0;
1364 for (const sp<Layer>& child : mCurrentChildren) {
1365 count += 1 + child->getChildrenCount();
1366 }
1367 return count;
1368}
1369
Robert Carr1f0a16a2016-10-24 16:27:39 -07001370void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001371 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001372 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001373
Robert Carr1f0a16a2016-10-24 16:27:39 -07001374 mCurrentChildren.add(layer);
1375 layer->setParent(this);
1376}
1377
1378ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001379 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001380 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001381
Robert Carr1323c952019-01-28 18:13:27 -08001382 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001383 return mCurrentChildren.remove(layer);
1384}
1385
Robert Carr1db73f62016-12-21 12:58:51 -08001386bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1387 sp<Handle> handle = nullptr;
1388 sp<Layer> newParent = nullptr;
1389 if (newParentHandle == nullptr) {
1390 return false;
1391 }
1392 handle = static_cast<Handle*>(newParentHandle.get());
1393 newParent = handle->owner.promote();
1394 if (newParent == nullptr) {
1395 ALOGE("Unable to promote Layer handle");
1396 return false;
1397 }
1398
chaviw5aedec92018-10-22 10:40:38 -07001399 if (attachChildren()) {
1400 setTransactionFlags(eTransactionNeeded);
1401 }
Robert Carr1db73f62016-12-21 12:58:51 -08001402 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001403 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001404 }
1405 mCurrentChildren.clear();
1406
1407 return true;
1408}
1409
Robert Carr15eae092018-03-23 13:43:53 -07001410void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001411 for (const sp<Layer>& child : mDrawingChildren) {
1412 child->mDrawingParent = newParent;
Vishnu Nairc652ff82019-03-15 12:48:54 -07001413 child->computeBounds(newParent->mBounds,
1414 newParent->getTransformWithScale(
1415 newParent->getBufferScaleTransform()));
Robert Carr578038f2018-03-09 12:25:24 -08001416 }
1417}
1418
chaviwf1961f72017-09-18 16:41:07 -07001419bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001420 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001421
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001422 // While layers are detached, we allow most operations
1423 // and simply halt performing the actual transaction. However
1424 // for reparent != null we would enter the mRemovedFromCurrentState
1425 // state, regardless of whether doTransaction was called, and
1426 // so we need to prevent the update here.
1427 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001428 return false;
1429 }
1430
Robert Carr54cf5b12019-01-25 14:02:28 -08001431 sp<Layer> newParent;
1432 if (newParentHandle != nullptr) {
1433 auto handle = static_cast<Handle*>(newParentHandle.get());
1434 newParent = handle->owner.promote();
1435 if (newParent == nullptr) {
1436 ALOGE("Unable to promote Layer handle");
1437 return false;
1438 }
1439 if (newParent == this) {
1440 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1441 return false;
1442 }
1443 }
1444
chaviwf1961f72017-09-18 16:41:07 -07001445 sp<Layer> parent = getParent();
1446 if (parent != nullptr) {
1447 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001448 }
1449
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001450 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001451 newParent->addChild(this);
1452 if (!newParent->isRemovedFromCurrentState()) {
1453 addToCurrentState();
1454 } else {
1455 onRemovedFromCurrentState();
1456 }
1457
1458 if (mLayerDetached) {
1459 mLayerDetached = false;
1460 callSetTransactionFlags = true;
1461 }
1462 } else {
1463 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001464 }
1465
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001466 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001467 setTransactionFlags(eTransactionNeeded);
1468 }
chaviw06178942017-07-27 10:25:59 -07001469 return true;
1470}
1471
Robert Carr9524cb32017-02-13 11:32:32 -08001472bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001473 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001474 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001475 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001476 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001477 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001478 child->detachChildren();
chaviw43cb3cb2019-05-31 15:23:41 -07001479 child->removeRemoteSyncPoints();
Robert Carr9524cb32017-02-13 11:32:32 -08001480 }
Robert Carr7f619b22017-11-06 12:56:35 -08001481 }
Robert Carr9524cb32017-02-13 11:32:32 -08001482
1483 return true;
1484}
1485
chaviw5aedec92018-10-22 10:40:38 -07001486bool Layer::attachChildren() {
1487 bool changed = false;
1488 for (const sp<Layer>& child : mCurrentChildren) {
1489 sp<Client> parentClient = mClientRef.promote();
1490 sp<Client> client(child->mClientRef.promote());
1491 if (client != nullptr && parentClient != client) {
1492 if (child->mLayerDetached) {
1493 child->mLayerDetached = false;
1494 changed = true;
1495 }
1496 changed |= child->attachChildren();
1497 }
1498 }
1499
1500 return changed;
1501}
1502
Peiyong Lind3788632018-09-18 16:01:31 -07001503bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001504 static const mat4 identityMatrix = mat4();
1505
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001506 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001507 return false;
1508 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001509 ++mCurrentState.sequence;
1510 mCurrentState.colorTransform = matrix;
1511 mCurrentState.hasColorTransform = matrix != identityMatrix;
1512 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001513 setTransactionFlags(eTransactionNeeded);
1514 return true;
1515}
1516
chaviwf66724d2018-11-28 16:35:21 -08001517mat4 Layer::getColorTransform() const {
1518 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1519 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1520 colorTransform = parent->getColorTransform() * colorTransform;
1521 }
1522 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001523}
1524
1525bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001526 bool hasColorTransform = getDrawingState().hasColorTransform;
1527 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1528 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1529 }
1530 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001531}
1532
Chia-I Wu11481472018-05-04 10:43:19 -07001533bool Layer::isLegacyDataSpace() const {
1534 // return true when no higher bits are set
chaviw4244e032019-09-04 11:27:49 -07001535 return !(getDataSpace() &
1536 (ui::Dataspace::STANDARD_MASK | ui::Dataspace::TRANSFER_MASK |
1537 ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001538}
1539
Robert Carr1f0a16a2016-10-24 16:27:39 -07001540void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001541 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001542}
1543
Robert Carr1f0a16a2016-10-24 16:27:39 -07001544int32_t Layer::getZ() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001545 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001546}
1547
Robert Carr1c5481e2019-07-01 14:42:27 -07001548bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) const {
Robert Carr29abff82017-12-04 13:51:20 -08001549 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001550 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviwe5ac40f2019-09-24 16:36:55 -07001551 return state.isRelativeOf;
Robert Carr29abff82017-12-04 13:51:20 -08001552}
1553
David Sodman41fdfc92017-11-06 16:09:56 -08001554__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001555 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001556 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1557 "makeTraversalList received invalid stateSet");
1558 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1559 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001560 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001561
Robert Carr29abff82017-12-04 13:51:20 -08001562 if (state.zOrderRelatives.size() == 0) {
1563 *outSkipRelativeZUsers = true;
1564 return children;
1565 }
1566
chaviwfd462612018-05-31 16:11:27 -07001567 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001568 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001569 sp<Layer> strongRelative = weakRelative.promote();
1570 if (strongRelative != nullptr) {
1571 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001572 }
1573 }
1574
Dan Stoza412903f2017-04-27 13:42:17 -07001575 for (const sp<Layer>& child : children) {
chaviwe5ac40f2019-09-24 16:36:55 -07001576 if (child->usingRelativeZ(stateSet)) {
Robert Carr503c7042017-09-27 15:06:08 -07001577 continue;
1578 }
Robert Carrdb66e622017-04-10 16:55:57 -07001579 traverse.add(child);
1580 }
1581
1582 return traverse;
1583}
1584
Robert Carr1f0a16a2016-10-24 16:27:39 -07001585/**
Robert Carrdb66e622017-04-10 16:55:57 -07001586 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001587 */
Dan Stoza412903f2017-04-27 13:42:17 -07001588void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001589 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1590 // produce a new list for traversing, including our relatives, and not including our children
1591 // who are relatives of another surface. In the case that there are no relative Z,
1592 // makeTraversalList returns our children directly to avoid significant overhead.
1593 // However in this case we need to take the responsibility for filtering children which
1594 // are relatives of another surface here.
1595 bool skipRelativeZUsers = false;
1596 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001597
Robert Carr1f0a16a2016-10-24 16:27:39 -07001598 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001599 for (; i < list.size(); i++) {
1600 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001601 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1602 continue;
1603 }
1604
Robert Carrdb66e622017-04-10 16:55:57 -07001605 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001606 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001607 }
Dan Stoza412903f2017-04-27 13:42:17 -07001608 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001609 }
Robert Carr29abff82017-12-04 13:51:20 -08001610
Dan Stoza412903f2017-04-27 13:42:17 -07001611 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001612 for (; i < list.size(); i++) {
1613 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001614
Robert Carr29abff82017-12-04 13:51:20 -08001615 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1616 continue;
1617 }
Dan Stoza412903f2017-04-27 13:42:17 -07001618 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001619 }
1620}
1621
1622/**
Robert Carrdb66e622017-04-10 16:55:57 -07001623 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001624 */
Dan Stoza412903f2017-04-27 13:42:17 -07001625void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1626 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001627 // See traverseInZOrder for documentation.
1628 bool skipRelativeZUsers = false;
1629 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001630
Robert Carr1f0a16a2016-10-24 16:27:39 -07001631 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001632 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001633 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001634
1635 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1636 continue;
1637 }
1638
Robert Carrdb66e622017-04-10 16:55:57 -07001639 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001640 break;
1641 }
Dan Stoza412903f2017-04-27 13:42:17 -07001642 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001643 }
Dan Stoza412903f2017-04-27 13:42:17 -07001644 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001645 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001646 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001647
1648 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1649 continue;
1650 }
1651
Dan Stoza412903f2017-04-27 13:42:17 -07001652 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001653 }
1654}
1655
chaviw4b129c22018-04-09 16:19:43 -07001656LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1657 const std::vector<Layer*>& layersInTree) {
1658 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1659 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001660 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1661 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001662 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001663
chaviwfd462612018-05-31 16:11:27 -07001664 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001665 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1666 sp<Layer> strongRelative = weakRelative.promote();
1667 // Only add relative layers that are also descendents of the top most parent of the tree.
1668 // If a relative layer is not a descendent, then it should be ignored.
1669 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1670 traverse.add(strongRelative);
1671 }
1672 }
1673
1674 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001675 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001676 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1677 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1678 // the child here since it won't be added later as a relative.
1679 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1680 childState.zOrderRelativeOf.promote().get())) {
1681 continue;
1682 }
1683 traverse.add(child);
1684 }
1685
1686 return traverse;
1687}
1688
1689void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1690 LayerVector::StateSet stateSet,
1691 const LayerVector::Visitor& visitor) {
1692 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001693
1694 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001695 for (; i < list.size(); i++) {
1696 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001697 if (relative->getZ() >= 0) {
1698 break;
1699 }
chaviw4b129c22018-04-09 16:19:43 -07001700 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001701 }
chaviw4b129c22018-04-09 16:19:43 -07001702
chaviwa76b2712017-09-20 12:02:26 -07001703 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001704 for (; i < list.size(); i++) {
1705 const auto& relative = list[i];
1706 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001707 }
1708}
1709
chaviw4b129c22018-04-09 16:19:43 -07001710std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1711 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1712 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1713
1714 std::vector<Layer*> layersInTree = {this};
1715 for (size_t i = 0; i < children.size(); i++) {
1716 const auto& child = children[i];
1717 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1718 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1719 }
1720
1721 return layersInTree;
1722}
1723
1724void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1725 const LayerVector::Visitor& visitor) {
1726 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1727 std::sort(layersInTree.begin(), layersInTree.end());
1728 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1729}
1730
Peiyong Linefefaac2018-08-17 12:27:51 -07001731ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001732 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001733}
1734
chaviw13fdc492017-06-27 12:40:18 -07001735half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001736 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001737
chaviw13fdc492017-06-27 12:40:18 -07001738 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1739 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001740}
Robert Carr6452f122017-03-21 10:41:29 -07001741
chaviw13fdc492017-06-27 12:40:18 -07001742half4 Layer::getColor() const {
1743 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001744 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001745}
Robert Carr6452f122017-03-21 10:41:29 -07001746
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001747Layer::RoundedCornerState Layer::getRoundedCornerState() const {
1748 const auto& p = mDrawingParent.promote();
1749 if (p != nullptr) {
Peiyong Lin27016a92019-03-29 17:36:08 +00001750 RoundedCornerState parentState = p->getRoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001751 if (parentState.radius > 0) {
1752 ui::Transform t = getActiveTransform(getDrawingState());
1753 t = t.inverse();
1754 parentState.cropRect = t.transform(parentState.cropRect);
1755 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1756 // but a transform matrix can define horizontal and vertical scales.
1757 // Let's take the average between both of them and pass into the shader, practically we
1758 // never do this type of transformation on windows anyway.
1759 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
1760 return parentState;
1761 }
1762 }
1763 const float radius = getDrawingState().cornerRadius;
Peiyong Linb9ff23e2019-04-08 11:04:59 -07001764 return radius > 0 && getCrop(getDrawingState()).isValid()
1765 ? RoundedCornerState(getCrop(getDrawingState()).toFloatRect(), radius)
1766 : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001767}
1768
Robert Carr1f0a16a2016-10-24 16:27:39 -07001769void Layer::commitChildList() {
1770 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1771 const auto& child = mCurrentChildren[i];
1772 child->commitChildList();
1773 }
1774 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001775 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001776}
1777
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001778static wp<Layer> extractLayerFromBinder(const wp<IBinder>& weakBinderHandle) {
1779 if (weakBinderHandle == nullptr) {
1780 return nullptr;
1781 }
1782 sp<IBinder> binderHandle = weakBinderHandle.promote();
1783 if (binderHandle == nullptr) {
1784 return nullptr;
1785 }
1786 sp<Layer::Handle> handle = static_cast<Layer::Handle*>(binderHandle.get());
1787 if (handle == nullptr) {
1788 return nullptr;
1789 }
1790 return handle->owner;
1791}
1792
Robert Carr720e5062018-07-30 17:45:14 -07001793void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001794 mCurrentState.inputInfo = info;
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001795 mCurrentState.touchableRegionCrop = extractLayerFromBinder(info.touchableRegionCropHandle);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001796 mCurrentState.modified = true;
1797 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07001798 setTransactionFlags(eTransactionNeeded);
1799}
1800
Vishnu Nair8406fd72019-07-30 11:29:31 -07001801void Layer::writeToProtoDrawingState(LayerProto* layerInfo, uint32_t traceFlags) const {
1802 ui::Transform transform = getTransform();
1803
1804 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
1805 for (const auto& pendingState : mPendingStatesSnapshot) {
1806 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
1807 if (barrierLayer != nullptr) {
1808 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
1809 barrierLayerProto->set_id(barrierLayer->sequence);
1810 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
1811 }
1812 }
1813
chaviwd62d3062019-09-04 14:48:02 -07001814 auto buffer = getBuffer();
Vishnu Nair8406fd72019-07-30 11:29:31 -07001815 if (buffer != nullptr) {
1816 LayerProtoHelper::writeToProto(buffer,
1817 [&]() { return layerInfo->mutable_active_buffer(); });
chaviw4244e032019-09-04 11:27:49 -07001818 LayerProtoHelper::writeToProto(ui::Transform(getBufferTransform()),
Vishnu Nair8406fd72019-07-30 11:29:31 -07001819 layerInfo->mutable_buffer_transform());
1820 }
1821 layerInfo->set_invalidate(contentDirty);
1822 layerInfo->set_is_protected(isProtected());
chaviw4244e032019-09-04 11:27:49 -07001823 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(getDataSpace())));
Vishnu Nair8406fd72019-07-30 11:29:31 -07001824 layerInfo->set_queued_frames(getQueuedFrameCount());
1825 layerInfo->set_refresh_pending(isBufferLatched());
1826 layerInfo->set_curr_frame(mCurrentFrameNumber);
1827 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
1828
1829 layerInfo->set_corner_radius(getRoundedCornerState().radius);
1830 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1831 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
1832 [&]() { return layerInfo->mutable_position(); });
1833 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
Lloyd Piquea2468662019-03-07 21:31:06 -08001834 LayerProtoHelper::writeToProto(debugGetVisibleRegionOnDefaultDisplay(),
Vishnu Nair8406fd72019-07-30 11:29:31 -07001835 [&]() { return layerInfo->mutable_visible_region(); });
1836 LayerProtoHelper::writeToProto(surfaceDamageRegion,
1837 [&]() { return layerInfo->mutable_damage_region(); });
1838 }
1839
1840 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
1841 LayerProtoHelper::writeToProto(mSourceBounds,
1842 [&]() { return layerInfo->mutable_source_bounds(); });
1843 LayerProtoHelper::writeToProto(mScreenBounds,
1844 [&]() { return layerInfo->mutable_screen_bounds(); });
1845 }
1846}
1847
1848void Layer::writeToProtoCommonState(LayerProto* layerInfo, LayerVector::StateSet stateSet,
1849 uint32_t traceFlags) const {
chaviw1d044282017-09-27 12:19:28 -07001850 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1851 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001852 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07001853
Peiyong Linefefaac2018-08-17 12:27:51 -07001854 ui::Transform requestedTransform = state.active_legacy.transform;
chaviw1d044282017-09-27 12:19:28 -07001855
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001856 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
1857 layerInfo->set_id(sequence);
1858 layerInfo->set_name(getName().c_str());
chaviw8a01fa42019-08-19 12:39:31 -07001859 layerInfo->set_type(getType());
chaviw1d044282017-09-27 12:19:28 -07001860
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001861 for (const auto& child : children) {
1862 layerInfo->add_children(child->sequence);
chaviw1d044282017-09-27 12:19:28 -07001863 }
chaviw1d044282017-09-27 12:19:28 -07001864
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001865 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1866 sp<Layer> strongRelative = weakRelative.promote();
1867 if (strongRelative != nullptr) {
1868 layerInfo->add_relatives(strongRelative->sequence);
1869 }
chaviwadc40c22018-07-10 16:57:27 -07001870 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001871
1872 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
1873 [&]() { return layerInfo->mutable_transparent_region(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001874
1875 layerInfo->set_layer_stack(getLayerStack());
1876 layerInfo->set_z(state.z);
1877
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001878 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(),
1879 [&]() {
1880 return layerInfo->mutable_requested_position();
1881 });
1882
1883 LayerProtoHelper::writeSizeToProto(state.active_legacy.w, state.active_legacy.h,
1884 [&]() { return layerInfo->mutable_size(); });
1885
1886 LayerProtoHelper::writeToProto(state.crop_legacy,
1887 [&]() { return layerInfo->mutable_crop(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001888
1889 layerInfo->set_is_opaque(isOpaque(state));
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001890
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001891
1892 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1893 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
1894 LayerProtoHelper::writeToProto(state.color,
1895 [&]() { return layerInfo->mutable_requested_color(); });
1896 layerInfo->set_flags(state.flags);
1897
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001898 LayerProtoHelper::writeToProto(requestedTransform,
1899 layerInfo->mutable_requested_transform());
1900
1901 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
1902 if (parent != nullptr) {
1903 layerInfo->set_parent(parent->sequence);
1904 } else {
1905 layerInfo->set_parent(-1);
1906 }
1907
1908 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1909 if (zOrderRelativeOf != nullptr) {
1910 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1911 } else {
1912 layerInfo->set_z_order_relative_of(-1);
1913 }
chaviwadc40c22018-07-10 16:57:27 -07001914 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001915
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001916 if (traceFlags & SurfaceTracing::TRACE_INPUT) {
1917 LayerProtoHelper::writeToProto(state.inputInfo, state.touchableRegionCrop,
1918 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001919 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001920
1921 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
1922 auto protoMap = layerInfo->mutable_metadata();
1923 for (const auto& entry : state.metadata.mMap) {
1924 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
1925 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001926 }
chaviw1d044282017-09-27 12:19:28 -07001927}
1928
Robert Carr2e102c92018-10-23 12:11:15 -07001929bool Layer::isRemovedFromCurrentState() const {
1930 return mRemovedFromCurrentState;
1931}
1932
Arthur Hungd20b2702019-01-14 18:16:16 +08001933InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001934 InputWindowInfo info = mDrawingState.inputInfo;
Robert Carr720e5062018-07-30 17:45:14 -07001935
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001936 if (info.displayId == ADISPLAY_ID_NONE) {
chaviwb15ea8a2019-09-03 12:40:22 -07001937 info.displayId = getLayerStack();
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001938 }
1939
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001940 ui::Transform t = getTransform();
1941 const float xScale = t.sx();
1942 const float yScale = t.sy();
Ady Abraham282f1d72019-07-24 18:05:56 -07001943 int32_t xSurfaceInset = info.surfaceInset;
1944 int32_t ySurfaceInset = info.surfaceInset;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001945 if (xScale != 1.0f || yScale != 1.0f) {
Ady Abraham282f1d72019-07-24 18:05:56 -07001946 info.windowXScale *= (xScale != 0.0f) ? 1.0f / xScale : 0.0f;
1947 info.windowYScale *= (yScale != 0.0f) ? 1.0f / yScale : 0.0f;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001948 info.touchableRegion.scaleSelf(xScale, yScale);
Ady Abraham282f1d72019-07-24 18:05:56 -07001949 xSurfaceInset = std::round(xSurfaceInset * xScale);
1950 ySurfaceInset = std::round(ySurfaceInset * yScale);
Riddle Hsu39d4aa52018-11-30 20:46:53 +08001951 }
Robert Carre07e1032018-11-26 12:55:53 -08001952
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001953 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001954 // If this is a portal window, set the touchableRegion to the layerBounds.
1955 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
1956 ? getBufferSize(getDrawingState())
1957 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08001958 if (!layerBounds.isValid()) {
1959 layerBounds = getCroppedBufferSize(getDrawingState());
1960 }
Vishnu Nair8033a492018-12-05 07:27:23 -08001961 layerBounds = t.transform(layerBounds);
Ady Abraham282f1d72019-07-24 18:05:56 -07001962
1963 // clamp inset to layer bounds
1964 xSurfaceInset = (xSurfaceInset >= 0) ? std::min(xSurfaceInset, layerBounds.getWidth() / 2) : 0;
1965 ySurfaceInset = (ySurfaceInset >= 0) ? std::min(ySurfaceInset, layerBounds.getHeight() / 2) : 0;
1966
Arthur Hung118b1142019-05-08 21:25:59 +08001967 layerBounds.inset(xSurfaceInset, ySurfaceInset, xSurfaceInset, ySurfaceInset);
Vishnu Nair8033a492018-12-05 07:27:23 -08001968
Arthur Hungd20b2702019-01-14 18:16:16 +08001969 // Input coordinate should match the layer bounds.
1970 info.frameLeft = layerBounds.left;
1971 info.frameTop = layerBounds.top;
1972 info.frameRight = layerBounds.right;
1973 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08001974
1975 // Position the touchable region relative to frame screen location and restrict it to frame
1976 // bounds.
1977 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08001978 info.visible = canReceiveInput();
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001979
1980 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
1981 if (info.replaceTouchableRegionWithCrop) {
1982 if (cropLayer == nullptr) {
1983 info.touchableRegion = Region(Rect{mScreenBounds});
1984 } else {
1985 info.touchableRegion = Region(Rect{cropLayer->mScreenBounds});
1986 }
1987 } else if (cropLayer != nullptr) {
1988 info.touchableRegion = info.touchableRegion.intersect(Rect{cropLayer->mScreenBounds});
1989 }
1990
Robert Carr720e5062018-07-30 17:45:14 -07001991 return info;
1992}
1993
1994bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001995 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07001996}
1997
Lloyd Piquefeb73d72018-12-04 17:23:44 -08001998std::shared_ptr<compositionengine::Layer> Layer::getCompositionLayer() const {
1999 return nullptr;
2000}
2001
chaviw3e727cd2019-01-31 13:41:05 -08002002bool Layer::canReceiveInput() const {
Vishnu Nair82353e92019-09-12 12:38:47 -07002003 return !isHiddenByPolicy();
chaviw3e727cd2019-01-31 13:41:05 -08002004}
2005
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002006compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
2007 const sp<const DisplayDevice>& display) const {
2008 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionLayer().get());
2009}
2010
Lloyd Piquea2468662019-03-07 21:31:06 -08002011Region Layer::debugGetVisibleRegionOnDefaultDisplay() const {
2012 sp<DisplayDevice> displayDevice = mFlinger->getDefaultDisplayDeviceLocked();
2013 if (displayDevice == nullptr) {
2014 return {};
2015 }
2016
2017 auto outputLayer = findOutputLayerForDisplay(displayDevice);
2018 if (outputLayer == nullptr) {
2019 return {};
2020 }
2021
2022 return outputLayer->getState().visibleRegion;
2023}
2024
chaviwb4c6e582019-08-16 14:35:07 -07002025void Layer::setInitialValuesForClone(const sp<Layer>& clonedFrom) {
2026 // copy drawing state from cloned layer
2027 mDrawingState = clonedFrom->mDrawingState;
2028 mClonedFrom = clonedFrom;
2029
2030 // TODO: (b/140756730) Ignore input for now since InputDispatcher doesn't support multiple
2031 // InputWindows per client token yet.
2032 mDrawingState.inputInfo.token = nullptr;
2033}
chaviw74b03172019-08-19 11:09:03 -07002034
2035void Layer::updateMirrorInfo() {
2036 if (mClonedChild == nullptr || !mClonedChild->isClonedFromAlive()) {
2037 // If mClonedChild is null, there is nothing to mirror. If isClonedFromAlive returns false,
2038 // it means that there is a clone, but the layer it was cloned from has been destroyed. In
2039 // that case, we want to delete the reference to the clone since we want it to get
2040 // destroyed. The root, this layer, will still be around since the client can continue
2041 // to hold a reference, but no cloned layers will be displayed.
2042 mClonedChild = nullptr;
2043 return;
2044 }
2045
2046 std::map<sp<Layer>, sp<Layer>> clonedLayersMap;
2047 // If the real layer exists and is in current state, add the clone as a child of the root.
2048 // There's no need to remove from drawingState when the layer is offscreen since currentState is
2049 // copied to drawingState for the root layer. So the clonedChild is always removed from
2050 // drawingState and then needs to be added back each traversal.
2051 if (!mClonedChild->getClonedFrom()->isRemovedFromCurrentState()) {
2052 addChildToDrawing(mClonedChild);
2053 }
2054
2055 mClonedChild->updateClonedDrawingState(clonedLayersMap);
2056 mClonedChild->updateClonedChildren(this, clonedLayersMap);
2057 mClonedChild->updateClonedRelatives(clonedLayersMap);
2058}
2059
2060void Layer::updateClonedDrawingState(std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2061 // If the layer the clone was cloned from is alive, copy the content of the drawingState
2062 // to the clone. If the real layer is no longer alive, continue traversing the children
2063 // since we may be able to pull out other children that are still alive.
2064 if (isClonedFromAlive()) {
2065 sp<Layer> clonedFrom = getClonedFrom();
2066 mDrawingState = clonedFrom->mDrawingState;
2067 // TODO: (b/140756730) Ignore input for now since InputDispatcher doesn't support multiple
2068 // InputWindows per client token yet.
2069 mDrawingState.inputInfo.token = nullptr;
2070 clonedLayersMap.emplace(clonedFrom, this);
2071 }
2072
2073 // The clone layer may have children in drawingState since they may have been created and
2074 // added from a previous request to updateMirorInfo. This is to ensure we don't recreate clones
2075 // that already exist, since we can just re-use them.
2076 // The drawingChildren will not get overwritten by the currentChildren since the clones are
2077 // not updated in the regular traversal. They are skipped since the root will lose the
2078 // reference to them when it copies its currentChildren to drawing.
2079 for (sp<Layer>& child : mDrawingChildren) {
2080 child->updateClonedDrawingState(clonedLayersMap);
2081 }
2082}
2083
2084void Layer::updateClonedChildren(const sp<Layer>& mirrorRoot,
2085 std::map<sp<Layer>, sp<Layer>>& clonedLayersMap) {
2086 mDrawingChildren.clear();
2087
2088 if (!isClonedFromAlive()) {
2089 return;
2090 }
2091
2092 sp<Layer> clonedFrom = getClonedFrom();
2093 for (sp<Layer>& child : clonedFrom->mDrawingChildren) {
2094 if (child == mirrorRoot) {
2095 // This is to avoid cyclical mirroring.
2096 continue;
2097 }
2098 sp<Layer> clonedChild = clonedLayersMap[child];
2099 if (clonedChild == nullptr) {
2100 clonedChild = child->createClone();
2101 clonedLayersMap[child] = clonedChild;
2102 }
2103 addChildToDrawing(clonedChild);
2104 clonedChild->updateClonedChildren(mirrorRoot, clonedLayersMap);
2105 }
2106}
2107
2108void Layer::updateClonedRelatives(std::map<sp<Layer>, sp<Layer>> clonedLayersMap) {
2109 mDrawingState.zOrderRelativeOf = nullptr;
2110 mDrawingState.zOrderRelatives.clear();
2111
2112 if (!isClonedFromAlive()) {
2113 return;
2114 }
2115
2116 sp<Layer> clonedFrom = getClonedFrom();
2117 for (wp<Layer>& relativeWeak : clonedFrom->mDrawingState.zOrderRelatives) {
2118 sp<Layer> relative = relativeWeak.promote();
2119 auto clonedRelative = clonedLayersMap[relative];
2120 if (clonedRelative != nullptr) {
2121 mDrawingState.zOrderRelatives.add(clonedRelative);
2122 }
2123 }
2124
2125 // Check if the relativeLayer for the real layer is part of the cloned hierarchy.
2126 // It's possible that the layer it's relative to is outside the requested cloned hierarchy.
2127 // In that case, we treat the layer as if the relativeOf has been removed. This way, it will
2128 // still traverse the children, but the layer with the missing relativeOf will not be shown
2129 // on screen.
2130 sp<Layer> relativeOf = clonedFrom->mDrawingState.zOrderRelativeOf.promote();
2131 sp<Layer> clonedRelativeOf = clonedLayersMap[relativeOf];
2132 if (clonedRelativeOf != nullptr) {
2133 mDrawingState.zOrderRelativeOf = clonedRelativeOf;
2134 }
2135
2136 for (sp<Layer>& child : mDrawingChildren) {
2137 child->updateClonedRelatives(clonedLayersMap);
2138 }
2139}
2140
2141void Layer::addChildToDrawing(const sp<Layer>& layer) {
2142 mDrawingChildren.add(layer);
2143 layer->mDrawingParent = this;
2144}
2145
Mathias Agopian13127d82013-03-05 17:47:11 -08002146// ---------------------------------------------------------------------------
2147
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002148}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002149
2150#if defined(__gl_h_)
2151#error "don't include gl/gl.h in this file"
2152#endif
2153
2154#if defined(__gl2_h_)
2155#error "don't include gl2/gl2.h in this file"
2156#endif