blob: d0b9187d56e19b9b21348c497c3a7e24e25a3dd5 [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>
Lloyd Pique0b785d82018-12-04 17:25:27 -080030#include <compositionengine/impl/LayerCompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080031#include <compositionengine/impl/OutputLayerCompositionState.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070032#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070033#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070034#include <cutils/properties.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080035#include <gui/BufferItem.h>
36#include <gui/LayerDebugInfo.h>
37#include <gui/Surface.h>
Alec Mourie60041e2019-06-14 18:59:51 -070038#include <math.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080039#include <renderengine/RenderEngine.h>
Alec Mourie60041e2019-06-14 18:59:51 -070040#include <stdint.h>
41#include <stdlib.h>
42#include <sys/types.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080043#include <ui/DebugUtils.h>
44#include <ui/GraphicBuffer.h>
45#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046#include <utils/Errors.h>
47#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080048#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080050#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051
Alec Mourie60041e2019-06-14 18:59:51 -070052#include <algorithm>
53#include <mutex>
54#include <sstream>
55
Yiwei Zhang60d1a192018-03-07 14:52:28 -080056#include "BufferLayer.h"
Valerie Haudd0b7572019-01-29 14:59:27 -080057#include "ColorLayer.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020058#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070059#include "DisplayDevice.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080060#include "DisplayHardware/HWComposer.h"
Mikael Pessa90092f42019-08-26 17:22:04 -070061#include "FrameTracer/FrameTracer.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080062#include "LayerProtoHelper.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070063#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070064#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065#include "SurfaceFlinger.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080066#include "TimeStats/TimeStats.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070067
David Sodman41fdfc92017-11-06 16:09:56 -080068#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070namespace android {
71
Yiwei Zhang5434a782018-12-05 18:06:32 -080072using base::StringAppendF;
73
Lloyd Piquef1c675b2018-09-12 20:45:39 -070074std::atomic<int32_t> Layer::sSequence{1};
Mathias Agopian13127d82013-03-05 17:47:11 -080075
Lloyd Pique42ab75e2018-09-12 20:46:03 -070076Layer::Layer(const LayerCreationArgs& args)
Ady Abraham8f1ee7f2019-04-05 10:32:50 -070077 : mFlinger(args.flinger),
78 mName(args.name),
79 mClientRef(args.client),
80 mWindowType(args.metadata.getInt32(METADATA_WINDOW_TYPE, 0)) {
Mathias Agopian4d9b8222013-03-12 17:11:48 -070081
82 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -070083 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
84 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
85 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070086
Dan Stozaf7ba41a2017-05-10 15:11:11 -070087 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070088
Lloyd Pique0449b0f2018-12-20 16:23:45 -080089 mCurrentState.active_legacy.w = args.w;
90 mCurrentState.active_legacy.h = args.h;
91 mCurrentState.flags = layerFlags;
92 mCurrentState.active_legacy.transform.set(0, 0);
93 mCurrentState.crop_legacy.makeInvalid();
94 mCurrentState.requestedCrop_legacy = mCurrentState.crop_legacy;
95 mCurrentState.z = 0;
96 mCurrentState.color.a = 1.0f;
97 mCurrentState.layerStack = 0;
98 mCurrentState.sequence = 0;
99 mCurrentState.requested_legacy = mCurrentState.active_legacy;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800100 mCurrentState.active.w = UINT32_MAX;
101 mCurrentState.active.h = UINT32_MAX;
102 mCurrentState.active.transform.set(0, 0);
103 mCurrentState.transform = 0;
104 mCurrentState.transformToDisplayInverse = false;
105 mCurrentState.crop.makeInvalid();
106 mCurrentState.acquireFence = new Fence(-1);
107 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
108 mCurrentState.hdrMetadata.validTypes = 0;
Marissa Wall7e0a01f2019-08-14 14:29:25 -0700109 mCurrentState.surfaceDamageRegion = Region::INVALID_REGION;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800110 mCurrentState.cornerRadius = 0.0f;
111 mCurrentState.api = -1;
112 mCurrentState.hasColorTransform = false;
Peiyong Linc502cb72019-03-01 15:00:23 -0800113 mCurrentState.colorSpaceAgnostic = false;
Evan Roskya1f1e152019-01-24 16:17:46 -0800114 mCurrentState.metadata = args.metadata;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700115
116 // drawing state & current state are identical
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800117 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700118
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800119 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700120 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800121 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200122 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700123
Ady Abraham8f1ee7f2019-04-05 10:32:50 -0700124 mSchedulerLayerHandle = mFlinger->mScheduler->registerLayer(mName.c_str(), mWindowType);
Vishnu Nair0f085c62019-08-30 08:49:12 -0700125 mCallingPid = args.callingPid;
126 mCallingUid = args.callingUid;
Robert Carr2e102c92018-10-23 12:11:15 -0700127 mFlinger->onLayerCreated();
Dan Stoza436ccf32018-06-21 12:10:12 -0700128}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700129
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700130Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800131 sp<Client> c(mClientRef.promote());
132 if (c != 0) {
133 c->detachLayer(this);
134 }
135
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000136 mFrameTracker.logAndResetStats(mName);
chaviw74d90ad2019-04-26 14:45:26 -0700137 mFlinger->onLayerDestroyed(this);
Mathias Agopian96f08192010-06-02 23:28:45 -0700138}
139
Vishnu Nair0f085c62019-08-30 08:49:12 -0700140LayerCreationArgs::LayerCreationArgs(SurfaceFlinger* flinger, const sp<Client>& client,
141 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
142 LayerMetadata metadata)
143 : flinger(flinger),
144 client(client),
145 name(name),
146 w(w),
147 h(h),
148 flags(flags),
149 metadata(std::move(metadata)) {
150 IPCThreadState* ipc = IPCThreadState::self();
151 callingPid = ipc->getCallingPid();
152 callingUid = ipc->getCallingUid();
153}
154
Mathias Agopian13127d82013-03-05 17:47:11 -0800155// ---------------------------------------------------------------------------
156// callbacks
157// ---------------------------------------------------------------------------
158
David Sodmaneb085e02017-10-05 18:49:04 -0700159/*
160 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
161 * Layer. So, the implementation is done in BufferLayer. When called on a
162 * ColorLayer object, it's essentially a NOP.
163 */
David Sodmaneb085e02017-10-05 18:49:04 -0700164void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800165
chaviw43cb3cb2019-05-31 15:23:41 -0700166void Layer::removeRemoteSyncPoints() {
167 for (auto& point : mRemoteSyncPoints) {
168 point->setTransactionApplied();
169 }
170 mRemoteSyncPoints.clear();
171
172 {
chaviw43cb3cb2019-05-31 15:23:41 -0700173 for (State pendingState : mPendingStates) {
174 pendingState.barrierLayer_legacy = nullptr;
175 }
176 }
177}
178
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700179void Layer::removeRelativeZ(const std::vector<Layer*>& layersInTree) {
180 if (mCurrentState.zOrderRelativeOf == nullptr) {
181 return;
182 }
Robert Carr2e102c92018-10-23 12:11:15 -0700183
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700184 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
185 if (strongRelative == nullptr) {
186 setZOrderRelativeOf(nullptr);
187 return;
188 }
189
190 if (!std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
191 strongRelative->removeZOrderRelative(this);
192 mFlinger->setTransactionFlags(eTraversalNeeded);
chaviw606e5cf2019-03-01 10:12:10 -0800193 setZOrderRelativeOf(nullptr);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700194 }
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700195}
196
197void Layer::removeFromCurrentState() {
198 mRemovedFromCurrentState = true;
Rob Carr4bba3702018-10-08 21:53:30 +0000199
Robert Carr2e102c92018-10-23 12:11:15 -0700200 // Since we are no longer reachable from CurrentState SurfaceFlinger
201 // will no longer invoke doTransaction for us, and so we will
202 // never finish applying transactions. We signal the sync point
203 // now so that another layer will not become indefinitely
204 // blocked.
chaviw43cb3cb2019-05-31 15:23:41 -0700205 removeRemoteSyncPoints();
Robert Carr2e102c92018-10-23 12:11:15 -0700206
207 {
208 Mutex::Autolock syncLock(mLocalSyncPointMutex);
209 for (auto& point : mLocalSyncPoints) {
210 point->setFrameAvailable();
211 }
212 mLocalSyncPoints.clear();
213 }
214
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800215 mFlinger->markLayerPendingRemovalLocked(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700216}
Chia-I Wu38512252017-05-17 14:36:16 -0700217
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700218void Layer::onRemovedFromCurrentState() {
219 auto layersInTree = getLayersInTree(LayerVector::StateSet::Current);
220 std::sort(layersInTree.begin(), layersInTree.end());
221 for (const auto& layer : layersInTree) {
222 layer->removeFromCurrentState();
223 layer->removeRelativeZ(layersInTree);
224 }
225}
226
chaviw61626f22018-11-15 16:26:27 -0800227void Layer::addToCurrentState() {
228 mRemovedFromCurrentState = false;
229
230 for (const auto& child : mCurrentChildren) {
231 child->addToCurrentState();
232 }
233}
234
Mathias Agopian13127d82013-03-05 17:47:11 -0800235// ---------------------------------------------------------------------------
236// set-up
237// ---------------------------------------------------------------------------
238
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700239const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800240 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800241}
242
chaviw13fdc492017-06-27 12:40:18 -0700243bool Layer::getPremultipledAlpha() const {
244 return mPremultipliedAlpha;
245}
246
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700247sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800248 Mutex::Autolock _l(mLock);
Robert Carrc0df3122019-04-11 13:18:21 -0700249 if (mGetHandleCalled) {
250 ALOGE("Get handle called twice" );
251 return nullptr;
252 }
253 mGetHandleCalled = true;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700254 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800255}
256
257// ---------------------------------------------------------------------------
258// h/w composer set-up
259// ---------------------------------------------------------------------------
260
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700261static Rect reduce(const Rect& win, const Region& exclude) {
262 if (CC_LIKELY(exclude.isEmpty())) {
263 return win;
264 }
265 if (exclude.isRect()) {
266 return win.reduce(exclude.getBounds());
267 }
268 return Region(win).subtract(exclude).getBounds();
269}
270
Dan Stoza80d61162017-12-20 15:57:52 -0800271static FloatRect reduce(const FloatRect& win, const Region& exclude) {
272 if (CC_LIKELY(exclude.isEmpty())) {
273 return win;
274 }
275 // Convert through Rect (by rounding) for lack of FloatRegion
276 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
277}
278
Vishnu Nair4351ad52019-02-11 14:13:02 -0800279Rect Layer::getScreenBounds(bool reduceTransparentRegion) const {
Vishnu Nairf0c28512019-02-08 12:40:28 -0800280 if (!reduceTransparentRegion) {
281 return Rect{mScreenBounds};
282 }
283
284 FloatRect bounds = getBounds();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800285 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800286 // Transform to screen space.
287 bounds = t.transform(bounds);
288 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700289}
290
Vishnu Nair4351ad52019-02-11 14:13:02 -0800291FloatRect Layer::getBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000292 const State& s(getDrawingState());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800293 return getBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700294}
295
Vishnu Nairf0c28512019-02-08 12:40:28 -0800296FloatRect Layer::getBounds(const Region& activeTransparentRegion) const {
297 // Subtract the transparent region and snap to the bounds.
298 return reduce(mBounds, activeTransparentRegion);
299}
300
Vishnu Nairc652ff82019-03-15 12:48:54 -0700301ui::Transform Layer::getBufferScaleTransform() const {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800302 // If the layer is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
303 // it isFixedSize) then there may be additional scaling not accounted
Vishnu Nairc652ff82019-03-15 12:48:54 -0700304 // for in the layer transform.
chaviwd62d3062019-09-04 14:48:02 -0700305 if (!isFixedSize() || getBuffer() == nullptr) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700306 return {};
Vishnu Nair4351ad52019-02-11 14:13:02 -0800307 }
308
Marissa Wall290ad082019-03-06 13:23:47 -0800309 // If the layer is a buffer state layer, the active width and height
310 // could be infinite. In that case, return the effective transform.
311 const uint32_t activeWidth = getActiveWidth(getDrawingState());
312 const uint32_t activeHeight = getActiveHeight(getDrawingState());
313 if (activeWidth >= UINT32_MAX && activeHeight >= UINT32_MAX) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700314 return {};
Marissa Wall290ad082019-03-06 13:23:47 -0800315 }
316
chaviwd62d3062019-09-04 14:48:02 -0700317 int bufferWidth = getBuffer()->getWidth();
318 int bufferHeight = getBuffer()->getHeight();
Vishnu Nairc652ff82019-03-15 12:48:54 -0700319
chaviw4244e032019-09-04 11:27:49 -0700320 if (getBufferTransform() & NATIVE_WINDOW_TRANSFORM_ROT_90) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700321 std::swap(bufferWidth, bufferHeight);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800322 }
Vishnu Nairc652ff82019-03-15 12:48:54 -0700323
Marissa Wall290ad082019-03-06 13:23:47 -0800324 float sx = activeWidth / static_cast<float>(bufferWidth);
325 float sy = activeHeight / static_cast<float>(bufferHeight);
326
Vishnu Nair4351ad52019-02-11 14:13:02 -0800327 ui::Transform extraParentScaling;
328 extraParentScaling.set(sx, 0, 0, sy);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700329 return extraParentScaling;
330}
331
332ui::Transform Layer::getTransformWithScale(const ui::Transform& bufferScaleTransform) const {
333 // We need to mirror this scaling to child surfaces or we will break the contract where WM can
334 // treat child surfaces as pixels in the parent surface.
chaviwd62d3062019-09-04 14:48:02 -0700335 if (!isFixedSize() || getBuffer() == nullptr) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700336 return mEffectiveTransform;
337 }
338 return mEffectiveTransform * bufferScaleTransform;
339}
340
341FloatRect Layer::getBoundsPreScaling(const ui::Transform& bufferScaleTransform) const {
342 // We need the pre scaled layer bounds when computing child bounds to make sure the child is
343 // cropped to its parent layer after any buffer transform scaling is applied.
chaviwd62d3062019-09-04 14:48:02 -0700344 if (!isFixedSize() || getBuffer() == nullptr) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700345 return mBounds;
346 }
347 return bufferScaleTransform.inverse().transform(mBounds);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800348}
349
350void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform) {
351 const State& s(getDrawingState());
352
353 // Calculate effective layer transform
354 mEffectiveTransform = parentTransform * getActiveTransform(s);
355
356 // Transform parent bounds to layer space
357 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
358
Vishnu Nairc652ff82019-03-15 12:48:54 -0700359 // Calculate source bounds
Vishnu Nair4351ad52019-02-11 14:13:02 -0800360 mSourceBounds = computeSourceBounds(parentBounds);
361
362 // Calculate bounds by croping diplay frame with layer crop and parent bounds
363 FloatRect bounds = mSourceBounds;
364 const Rect layerCrop = getCrop(s);
365 if (!layerCrop.isEmpty()) {
366 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
367 }
368 bounds = bounds.intersect(parentBounds);
369
370 mBounds = bounds;
371 mScreenBounds = mEffectiveTransform.transform(mBounds);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700372
373 // Add any buffer scaling to the layer's children.
374 ui::Transform bufferScaleTransform = getBufferScaleTransform();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800375 for (const sp<Layer>& child : mDrawingChildren) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700376 child->computeBounds(getBoundsPreScaling(bufferScaleTransform),
377 getTransformWithScale(bufferScaleTransform));
Vishnu Nair4351ad52019-02-11 14:13:02 -0800378 }
379}
380
Vishnu Nair60356342018-11-13 13:00:45 -0800381Rect Layer::getCroppedBufferSize(const State& s) const {
382 Rect size = getBufferSize(s);
383 Rect crop = getCrop(s);
384 if (!crop.isEmpty() && size.isValid()) {
385 size.intersect(crop, &size);
386 } else if (!crop.isEmpty()) {
387 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700388 }
Vishnu Nair60356342018-11-13 13:00:45 -0800389 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800390}
391
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700392void Layer::setupRoundedCornersCropCoordinates(Rect win,
393 const FloatRect& roundedCornersCrop) const {
394 // Translate win by the rounded corners rect coordinates, to have all values in
395 // layer coordinate space.
396 win.left -= roundedCornersCrop.left;
397 win.right -= roundedCornersCrop.left;
398 win.top -= roundedCornersCrop.top;
399 win.bottom -= roundedCornersCrop.top;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700400}
401
Lloyd Piquea83776c2019-01-29 18:42:32 -0800402void Layer::latchGeometry(compositionengine::LayerFECompositionState& compositionState) const {
403 const auto& drawingState{getDrawingState()};
404 auto alpha = static_cast<float>(getAlpha());
David Revemanecf0fa52017-03-03 11:32:44 -0500405 auto blendMode = HWC2::BlendMode::None;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800406 if (!isOpaque(drawingState) || alpha != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800407 blendMode =
408 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800409 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800410
Lloyd Piquea83776c2019-01-29 18:42:32 -0800411 int type = drawingState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
412 int appId = drawingState.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700413 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400414 if (parent.get()) {
415 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800416 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
417 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
Jiwen 'Steve' Cai736c74e2019-05-28 18:33:22 -0700418 if (parentType > 0 && parentAppId > 0) {
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800419 type = parentType;
420 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700421 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400422 }
423
Lloyd Piquea83776c2019-01-29 18:42:32 -0800424 compositionState.geomLayerTransform = getTransform();
425 compositionState.geomInverseLayerTransform = compositionState.geomLayerTransform.inverse();
426 compositionState.geomBufferSize = getBufferSize(drawingState);
chaviw4244e032019-09-04 11:27:49 -0700427 compositionState.geomContentCrop = getBufferCrop();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800428 compositionState.geomCrop = getCrop(drawingState);
chaviw4244e032019-09-04 11:27:49 -0700429 compositionState.geomBufferTransform = getBufferTransform();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800430 compositionState.geomBufferUsesDisplayInverseTransform = getTransformToDisplayInverse();
431 compositionState.geomActiveTransparentRegion = getActiveTransparentRegion(drawingState);
432 compositionState.geomLayerBounds = mBounds;
433 compositionState.geomUsesSourceCrop = usesSourceCrop();
434 compositionState.isSecure = isSecure();
David Sodman15094112018-10-11 09:39:37 -0700435
Lloyd Piquea83776c2019-01-29 18:42:32 -0800436 compositionState.blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
437 compositionState.alpha = alpha;
438 compositionState.type = type;
439 compositionState.appId = appId;
440}
David Sodmanba340492018-08-05 21:51:33 -0700441
Lloyd Piquef5275482019-01-29 18:42:42 -0800442void Layer::latchPerFrameState(compositionengine::LayerFECompositionState& compositionState) const {
Lloyd Pique688abd42019-02-15 15:42:24 -0800443 const auto& drawingState{getDrawingState()};
Lloyd Piquef5275482019-01-29 18:42:42 -0800444 compositionState.forceClientComposition = false;
445
446 // TODO(lpique): b/121291683 Remove this one we are sure we don't need the
447 // value recomputed / set every frame.
448 compositionState.geomVisibleRegion = visibleRegion;
449
450 compositionState.isColorspaceAgnostic = isColorSpaceAgnostic();
chaviw4244e032019-09-04 11:27:49 -0700451 compositionState.dataspace = getDataSpace();
Lloyd Piquef5275482019-01-29 18:42:42 -0800452 compositionState.colorTransform = getColorTransform();
453 compositionState.colorTransformIsIdentity = !hasColorTransform();
454 compositionState.surfaceDamage = surfaceDamageRegion;
Lloyd Pique688abd42019-02-15 15:42:24 -0800455 compositionState.hasProtectedContent = isProtected();
456
457 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
458 compositionState.isOpaque =
459 isOpaque(drawingState) && !usesRoundedCorners && getAlpha() == 1.0_hf;
Lloyd Piquef5275482019-01-29 18:42:42 -0800460
461 // Force client composition for special cases known only to the front-end.
Lloyd Pique688abd42019-02-15 15:42:24 -0800462 if (isHdrY410() || usesRoundedCorners) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800463 compositionState.forceClientComposition = true;
464 }
465}
466
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800467void Layer::latchCursorCompositionState(
468 compositionengine::LayerFECompositionState& compositionState) const {
469 // This gives us only the "orientation" component of the transform
470 const State& drawingState{getDrawingState()};
471
472 // Apply the layer's transform, followed by the display's global transform
473 // Here we're guaranteed that the layer's transform preserves rects
474 Rect win = getCroppedBufferSize(drawingState);
475 // Subtract the transparent region and snap to the bounds
476 Rect bounds = reduce(win, getActiveTransparentRegion(drawingState));
477 Rect frame(getTransform().transform(bounds));
478
479 compositionState.cursorFrame = frame;
480}
481
Lloyd Piquef16688f2019-02-19 17:47:57 -0800482bool Layer::onPreComposition(nsecs_t) {
483 return false;
484}
485
Lloyd Piquea83776c2019-01-29 18:42:32 -0800486void Layer::latchCompositionState(compositionengine::LayerFECompositionState& compositionState,
487 bool includeGeometry) const {
488 if (includeGeometry) {
489 latchGeometry(compositionState);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700490 }
Lloyd Piquef5275482019-01-29 18:42:42 -0800491
492 latchPerFrameState(compositionState);
Lloyd Piquea83776c2019-01-29 18:42:32 -0800493}
Mathias Agopian29a367b2011-07-12 14:51:45 -0700494
Lloyd Piquea83776c2019-01-29 18:42:32 -0800495const char* Layer::getDebugName() const {
496 return mName.string();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800497}
498
Mathias Agopian13127d82013-03-05 17:47:11 -0800499// ---------------------------------------------------------------------------
500// drawing...
501// ---------------------------------------------------------------------------
502
Lloyd Piquef16688f2019-02-19 17:47:57 -0800503std::optional<renderengine::LayerSettings> Layer::prepareClientComposition(
504 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
505 if (!getCompositionLayer()) {
506 return {};
507 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800508
Vishnu Nair4351ad52019-02-11 14:13:02 -0800509 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000510 half alpha = getAlpha();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800511 renderengine::LayerSettings layerSettings;
512 layerSettings.geometry.boundaries = bounds;
513 if (targetSettings.useIdentityTransform) {
514 layerSettings.geometry.positionTransform = mat4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000515 } else {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800516 layerSettings.geometry.positionTransform = getTransform().asMatrix4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000517 }
518
519 if (hasColorTransform()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800520 layerSettings.colorTransform = getColorTransform();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000521 }
522
523 const auto roundedCornerState = getRoundedCornerState();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800524 layerSettings.geometry.roundedCornersRadius = roundedCornerState.radius;
525 layerSettings.geometry.roundedCornersCrop = roundedCornerState.cropRect;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000526
Lloyd Piquef16688f2019-02-19 17:47:57 -0800527 layerSettings.alpha = alpha;
chaviw4244e032019-09-04 11:27:49 -0700528 layerSettings.sourceDataspace = getDataSpace();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800529 return layerSettings;
Mathias Agopian13127d82013-03-05 17:47:11 -0800530}
531
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800532Hwc2::IComposerClient::Composition Layer::getCompositionType(
533 const sp<const DisplayDevice>& display) const {
534 const auto outputLayer = findOutputLayerForDisplay(display);
535 LOG_FATAL_IF(!outputLayer);
536 return outputLayer->getState().hwc ? (*outputLayer->getState().hwc).hwcCompositionType
537 : Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800538}
539
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800540bool Layer::getClearClientTarget(const sp<const DisplayDevice>& display) const {
541 const auto outputLayer = findOutputLayerForDisplay(display);
542 LOG_FATAL_IF(!outputLayer);
543 return outputLayer->getState().clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800544}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800545
Dan Stozacac35382016-01-27 12:21:06 -0800546bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
547 if (point->getFrameNumber() <= mCurrentFrameNumber) {
548 // Don't bother with a SyncPoint, since we've already latched the
549 // relevant frame
550 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700551 }
Robert Carr2e102c92018-10-23 12:11:15 -0700552 if (isRemovedFromCurrentState()) {
553 return false;
554 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700555
Dan Stozacac35382016-01-27 12:21:06 -0800556 Mutex::Autolock lock(mLocalSyncPointMutex);
557 mLocalSyncPoints.push_back(point);
558 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700559}
560
Mathias Agopian13127d82013-03-05 17:47:11 -0800561// ----------------------------------------------------------------------------
562// local state
563// ----------------------------------------------------------------------------
564
David Sodman41fdfc92017-11-06 16:09:56 -0800565bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800566 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700567 return (s.flags & layer_state_t::eLayerSecure);
568}
569
Mathias Agopian13127d82013-03-05 17:47:11 -0800570void Layer::setVisibleRegion(const Region& visibleRegion) {
571 // always called from main thread
572 this->visibleRegion = visibleRegion;
573}
574
575void Layer::setCoveredRegion(const Region& coveredRegion) {
576 // always called from main thread
577 this->coveredRegion = coveredRegion;
578}
579
David Sodman41fdfc92017-11-06 16:09:56 -0800580void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800581 // always called from main thread
582 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
583}
584
Robert Carre5f4f692018-01-12 13:12:28 -0800585void Layer::clearVisibilityRegions() {
586 visibleRegion.clear();
587 visibleNonTransparentRegion.clear();
588 coveredRegion.clear();
589}
590
Mathias Agopian13127d82013-03-05 17:47:11 -0800591// ----------------------------------------------------------------------------
592// transaction
593// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800594
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800595void Layer::pushPendingState() {
596 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700597 return;
598 }
Alec Mourie60041e2019-06-14 18:59:51 -0700599 ATRACE_CALL();
Dan Stoza7dde5992015-05-22 09:51:44 -0700600
Dan Stoza7dde5992015-05-22 09:51:44 -0700601 // If this transaction is waiting on the receipt of a frame, generate a sync
602 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700603 // We don't allow installing sync points after we are removed from the current state
604 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800605 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
606 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800607 if (barrierLayer == nullptr) {
608 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700609 // If we can't promote the layer we are intended to wait on,
610 // then it is expired or otherwise invalid. Allow this transaction
611 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800612 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800613 } else {
chaviw43cb3cb2019-05-31 15:23:41 -0700614 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy, this);
Robert Carr0d480722017-01-10 16:42:54 -0800615 if (barrierLayer->addSyncPoint(syncPoint)) {
Alec Mourie60041e2019-06-14 18:59:51 -0700616 std::stringstream ss;
617 ss << "Adding sync point " << mCurrentState.frameNumber_legacy;
618 ATRACE_NAME(ss.str().c_str());
Dan Stozacac35382016-01-27 12:21:06 -0800619 mRemoteSyncPoints.push_back(std::move(syncPoint));
620 } else {
621 // We already missed the frame we're supposed to synchronize
622 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800623 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800624 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700625 }
626
Dan Stoza7dde5992015-05-22 09:51:44 -0700627 // Wake us up to check if the frame has been received
628 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700629 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700630 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800631 mPendingStates.push_back(mCurrentState);
632 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700633}
634
Pablo Ceballos05289c22016-04-14 15:49:55 -0700635void Layer::popPendingState(State* stateToCommit) {
Alec Mourie60041e2019-06-14 18:59:51 -0700636 ATRACE_CALL();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800637 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700638
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800639 mPendingStates.removeAt(0);
640 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700641}
642
Pablo Ceballos05289c22016-04-14 15:49:55 -0700643bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700644 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800645 while (!mPendingStates.empty()) {
646 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700647 if (mRemoteSyncPoints.empty()) {
648 // If we don't have a sync point for this, apply it anyway. It
649 // will be visually wrong, but it should keep us from getting
650 // into too much trouble.
651 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700652 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700653 stateUpdateAvailable = true;
654 continue;
655 }
656
Marissa Wallf58c14b2018-07-24 10:50:43 -0700657 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800658 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800659 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800660
661 // Signal our end of the sync point and then dispose of it
662 mRemoteSyncPoints.front()->setTransactionApplied();
663 mRemoteSyncPoints.pop_front();
664 continue;
665 }
666
Dan Stoza7dde5992015-05-22 09:51:44 -0700667 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
Alec Mourie60041e2019-06-14 18:59:51 -0700668 ATRACE_NAME("frameIsAvailable");
Dan Stoza7dde5992015-05-22 09:51:44 -0700669 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700670 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700671 stateUpdateAvailable = true;
672
673 // Signal our end of the sync point and then dispose of it
674 mRemoteSyncPoints.front()->setTransactionApplied();
675 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800676 } else {
Alec Mourie60041e2019-06-14 18:59:51 -0700677 ATRACE_NAME("!frameIsAvailable");
Dan Stoza792e5292016-02-11 11:43:58 -0800678 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700679 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700680 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700681 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700682 stateUpdateAvailable = true;
683 }
684 }
685
686 // If we still have pending updates, wake SurfaceFlinger back up and point
687 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800688 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700689 setTransactionFlags(eTransactionNeeded);
690 mFlinger->setTransactionFlags(eTraversalNeeded);
691 }
692
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800693 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700694 return stateUpdateAvailable;
695}
696
Marissa Wall61c58622018-07-18 10:12:20 -0700697uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000698 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800699
Marissa Wall61c58622018-07-18 10:12:20 -0700700 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
701 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700702
David Sodmaneb085e02017-10-05 18:49:04 -0700703 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700704 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000705 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800706 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
707 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
708 " requested={ wh={%4u,%4u} }}\n"
709 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
710 " requested={ wh={%4u,%4u} }}\n",
chaviw4244e032019-09-04 11:27:49 -0700711 this, getName().string(), getBufferTransform(), getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700712 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
713 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
714 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
715 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
716 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700717 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
718 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
719 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800720 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700721
Robert Carre392b552017-09-19 12:16:05 -0700722 // Don't let Layer::doTransaction update the drawing state
723 // if we have a pending resize, unless we are in fixed-size mode.
724 // the drawing state will be updated only once we receive a buffer
725 // with the correct size.
726 //
727 // In particular, we want to make sure the clip (which is part
728 // of the geometry state) is latched together with the size but is
729 // latched immediately when no resizing is involved.
730 //
731 // If a sideband stream is attached, however, we want to skip this
732 // optimization so that transactions aren't missed when a buffer
733 // never arrives
734 //
735 // In the case that we don't have a buffer we ignore other factors
736 // and avoid entering the resizePending state. At a high level the
737 // resizePending state is to avoid applying the state of the new buffer
738 // to the old buffer. However in the state where we don't have an old buffer
739 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700740 const bool resizePending =
741 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
742 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
chaviwd62d3062019-09-04 14:48:02 -0700743 (getBuffer() != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700744 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800745 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700746 flags |= eDontUpdateGeometryState;
747 }
748 }
749
Robert Carr7bf247e2017-05-18 14:02:49 -0700750 // Here we apply various requested geometry states, depending on our
751 // latching configuration. See Layer.h for a detailed discussion of
752 // how geometry latching is controlled.
753 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000754 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700755
756 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
757 // mode, which causes attributes which normally latch regardless of scaling mode,
758 // to be delayed. We copy the requested state to the active state making sure
759 // to respect these rules (again see Layer.h for a detailed discussion).
760 //
761 // There is an awkward asymmetry in the handling of the crop states in the position
762 // states, as can be seen below. Largely this arises from position and transform
763 // being stored in the same data structure while having different latching rules.
764 // b/38182305
765 //
Marissa Wall61c58622018-07-18 10:12:20 -0700766 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700767 // applyPendingStates in the presence of deferred transactions.
768 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -0700769 float tx = stateToCommit->active_legacy.transform.tx();
770 float ty = stateToCommit->active_legacy.transform.ty();
771 stateToCommit->active_legacy = stateToCommit->requested_legacy;
772 stateToCommit->active_legacy.transform.set(tx, ty);
773 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700774 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700775 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -0700776 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700777 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800778 }
779
Marissa Wall61c58622018-07-18 10:12:20 -0700780 return flags;
781}
782
783uint32_t Layer::doTransaction(uint32_t flags) {
784 ATRACE_CALL();
785
chaviw5aedec92018-10-22 10:40:38 -0700786 if (mLayerDetached) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800787 return flags;
788 }
789
790 if (mChildrenChanged) {
791 flags |= eVisibleRegion;
792 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -0700793 }
794
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800795 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +0000796 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -0700797 if (!applyPendingStates(&c)) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800798 return flags;
Marissa Wall61c58622018-07-18 10:12:20 -0700799 }
800
801 flags = doTransactionResize(flags, &c);
802
Alec Mourib416efd2018-09-06 21:01:59 +0000803 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700804
805 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800806 // invalidate and recompute the visible regions if needed
807 flags |= Layer::eVisibleRegion;
808 }
809
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700810 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800811 // invalidate and recompute the visible regions if needed
812 flags |= eVisibleRegion;
813 this->contentDirty = true;
814
815 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -0700816 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -0700817 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -0800818 }
819
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800820 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -0700821 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800822 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -0700823 }
824
Mathias Agopian13127d82013-03-05 17:47:11 -0800825 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -0700826 commitTransaction(c);
Vishnu Nair8406fd72019-07-30 11:29:31 -0700827 mPendingStatesSnapshot = mPendingStates;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800828 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -0800829 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800830}
831
Pablo Ceballos05289c22016-04-14 15:49:55 -0700832void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800833 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700834}
835
Mathias Agopian13127d82013-03-05 17:47:11 -0800836uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800837 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800838}
839
840uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800841 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -0800842}
843
Robert Carr82364e32016-05-15 11:27:47 -0700844bool Layer::setPosition(float x, float y, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800845 if (mCurrentState.requested_legacy.transform.tx() == x &&
846 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -0800847 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800848 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -0700849
850 // We update the requested and active position simultaneously because
851 // we want to apply the position portion of the transform matrix immediately,
852 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800853 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -0700854 if (immediate && !mFreezeGeometryUpdates) {
855 // Here we directly update the active state
856 // unlike other setters, because we store it within
857 // the transform, but use different latching rules.
858 // b/38182305
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800859 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -0700860 }
Robert Carr7bf247e2017-05-18 14:02:49 -0700861 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -0700862
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800863 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800864 setTransactionFlags(eTransactionNeeded);
865 return true;
866}
Robert Carr82364e32016-05-15 11:27:47 -0700867
Robert Carr1f0a16a2016-10-24 16:27:39 -0700868bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
869 ssize_t idx = mCurrentChildren.indexOf(childLayer);
870 if (idx < 0) {
871 return false;
872 }
873 if (childLayer->setLayer(z)) {
874 mCurrentChildren.removeAt(idx);
875 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800876 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700877 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800878 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700879}
880
Robert Carr503c7042017-09-27 15:06:08 -0700881bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
882 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
883 ssize_t idx = mCurrentChildren.indexOf(childLayer);
884 if (idx < 0) {
885 return false;
886 }
887 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
888 mCurrentChildren.removeAt(idx);
889 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800890 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700891 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800892 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700893}
894
Robert Carrae060832016-11-28 10:51:00 -0800895bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800896 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
897 mCurrentState.sequence++;
898 mCurrentState.z = z;
899 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700900
901 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800902 if (mCurrentState.zOrderRelativeOf != nullptr) {
903 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700904 if (strongRelative != nullptr) {
905 strongRelative->removeZOrderRelative(this);
906 }
chaviw606e5cf2019-03-01 10:12:10 -0800907 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -0700908 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800909 setTransactionFlags(eTransactionNeeded);
910 return true;
911}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700912
Robert Carrdb66e622017-04-10 16:55:57 -0700913void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800914 mCurrentState.zOrderRelatives.remove(relative);
915 mCurrentState.sequence++;
916 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700917 setTransactionFlags(eTransactionNeeded);
918}
919
920void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800921 mCurrentState.zOrderRelatives.add(relative);
922 mCurrentState.modified = true;
923 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -0700924 setTransactionFlags(eTransactionNeeded);
925}
926
chaviw606e5cf2019-03-01 10:12:10 -0800927void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
928 mCurrentState.zOrderRelativeOf = relativeOf;
929 mCurrentState.sequence++;
930 mCurrentState.modified = true;
931 setTransactionFlags(eTransactionNeeded);
932}
933
Robert Carr503d2bd2017-12-04 15:49:47 -0800934bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -0700935 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
936 if (handle == nullptr) {
937 return false;
938 }
939 sp<Layer> relative = handle->owner.promote();
940 if (relative == nullptr) {
941 return false;
942 }
943
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800944 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
945 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -0800946 return false;
947 }
948
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800949 mCurrentState.sequence++;
950 mCurrentState.modified = true;
951 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -0700952
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800953 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -0700954 if (oldZOrderRelativeOf != nullptr) {
955 oldZOrderRelativeOf->removeZOrderRelative(this);
956 }
chaviw606e5cf2019-03-01 10:12:10 -0800957 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -0700958 relative->addZOrderRelative(this);
959
960 setTransactionFlags(eTransactionNeeded);
961
962 return true;
963}
964
Mathias Agopian13127d82013-03-05 17:47:11 -0800965bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800966 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -0700967 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800968 mCurrentState.requested_legacy.w = w;
969 mCurrentState.requested_legacy.h = h;
970 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800971 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -0700972
973 // record the new size, from this point on, when the client request
974 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800975 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -0800976 return true;
977}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800978bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800979 if (mCurrentState.color.a == alpha) return false;
980 mCurrentState.sequence++;
981 mCurrentState.color.a = alpha;
982 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800983 setTransactionFlags(eTransactionNeeded);
984 return true;
985}
chaviw13fdc492017-06-27 12:40:18 -0700986
Valerie Haudd0b7572019-01-29 14:59:27 -0800987bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
988 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -0700989 return false;
Valerie Hauaa194562019-02-05 16:21:38 -0800990 }
991 mCurrentState.sequence++;
992 mCurrentState.modified = true;
993 setTransactionFlags(eTransactionNeeded);
994
995 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -0800996 // create background color layer if one does not yet exist
997 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
998 const String8& name = mName + "BackgroundColorLayer";
Evan Roskya1f1e152019-01-24 16:17:46 -0800999 mCurrentState.bgColorLayer = new ColorLayer(
1000 LayerCreationArgs(mFlinger.get(), nullptr, name, 0, 0, flags, LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -07001001
Valerie Haudd0b7572019-01-29 14:59:27 -08001002 // add to child list
1003 addChild(mCurrentState.bgColorLayer);
1004 mFlinger->mLayersAdded = true;
1005 // set up SF to handle added color layer
1006 if (isRemovedFromCurrentState()) {
1007 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1008 }
1009 mFlinger->setTransactionFlags(eTransactionNeeded);
1010 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1011 mCurrentState.bgColorLayer->reparent(nullptr);
1012 mCurrentState.bgColorLayer = nullptr;
1013 return true;
1014 }
1015
1016 mCurrentState.bgColorLayer->setColor(color);
1017 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1018 mCurrentState.bgColorLayer->setAlpha(alpha);
1019 mCurrentState.bgColorLayer->setDataspace(dataspace);
1020
chaviw13fdc492017-06-27 12:40:18 -07001021 return true;
1022}
1023
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001024bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001025 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001026
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001027 mCurrentState.sequence++;
1028 mCurrentState.cornerRadius = cornerRadius;
1029 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001030 setTransactionFlags(eTransactionNeeded);
1031 return true;
1032}
1033
Robert Carrd4ae7f32018-06-07 16:10:57 -07001034bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1035 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001036 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001037 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1038
1039 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1040 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1041 return false;
1042 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001043 mCurrentState.sequence++;
1044 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1045 matrix.dsdy);
1046 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001047 setTransactionFlags(eTransactionNeeded);
1048 return true;
1049}
Marissa Wall61c58622018-07-18 10:12:20 -07001050
Mathias Agopian13127d82013-03-05 17:47:11 -08001051bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001052 mCurrentState.requestedTransparentRegion_legacy = transparent;
1053 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001054 setTransactionFlags(eTransactionNeeded);
1055 return true;
1056}
1057bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001058 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1059 if (mCurrentState.flags == newFlags) return false;
1060 mCurrentState.sequence++;
1061 mCurrentState.flags = newFlags;
1062 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001063 setTransactionFlags(eTransactionNeeded);
1064 return true;
1065}
Robert Carr99e27f02016-06-16 15:18:02 -07001066
Marissa Wallf58c14b2018-07-24 10:50:43 -07001067bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001068 if (mCurrentState.requestedCrop_legacy == crop) return false;
1069 mCurrentState.sequence++;
1070 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001071 if (immediate && !mFreezeGeometryUpdates) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001072 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001073 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001074 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1075
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001076 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001077 setTransactionFlags(eTransactionNeeded);
1078 return true;
1079}
Robert Carr8d5227b2017-03-16 15:41:03 -07001080
Robert Carrc3574f72016-03-24 12:19:32 -07001081bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001082 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001083 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001084 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001085 return true;
1086}
1087
Evan Roskyef876c92019-01-25 17:46:06 -08001088bool Layer::setMetadata(const LayerMetadata& data) {
1089 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001090 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001091 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001092 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001093 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001094}
1095
Mathias Agopian13127d82013-03-05 17:47:11 -08001096bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001097 if (mCurrentState.layerStack == layerStack) return false;
1098 mCurrentState.sequence++;
1099 mCurrentState.layerStack = layerStack;
1100 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001101 setTransactionFlags(eTransactionNeeded);
1102 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001103}
1104
Peiyong Linc502cb72019-03-01 15:00:23 -08001105bool Layer::setColorSpaceAgnostic(const bool agnostic) {
1106 if (mCurrentState.colorSpaceAgnostic == agnostic) {
1107 return false;
1108 }
1109 mCurrentState.sequence++;
1110 mCurrentState.colorSpaceAgnostic = agnostic;
1111 mCurrentState.modified = true;
1112 setTransactionFlags(eTransactionNeeded);
1113 return true;
1114}
1115
Robert Carr1f0a16a2016-10-24 16:27:39 -07001116uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001117 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001118 if (p == nullptr) {
1119 return getDrawingState().layerStack;
1120 }
1121 return p->getLayerStack();
1122}
1123
Marissa Wallf58c14b2018-07-24 10:50:43 -07001124void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Alec Mourie60041e2019-06-14 18:59:51 -07001125 ATRACE_CALL();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001126 mCurrentState.barrierLayer_legacy = barrierLayer;
1127 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001128 // We don't set eTransactionNeeded, because just receiving a deferral
1129 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001130 mCurrentState.modified = true;
1131 pushPendingState();
1132 mCurrentState.barrierLayer_legacy = nullptr;
1133 mCurrentState.frameNumber_legacy = 0;
1134 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001135}
1136
Marissa Wallf58c14b2018-07-24 10:50:43 -07001137void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001138 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001139 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001140}
1141
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001142// ----------------------------------------------------------------------------
1143// pageflip handling...
1144// ----------------------------------------------------------------------------
1145
Robert Carr1f0a16a2016-10-24 16:27:39 -07001146bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001147 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001148 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001149 if (parent != nullptr && parent->isHiddenByPolicy()) {
1150 return true;
1151 }
Robert Carr1c5481e2019-07-01 14:42:27 -07001152 if (usingRelativeZ(LayerVector::StateSet::Drawing)) {
1153 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
1154 if (zOrderRelativeOf != nullptr) {
1155 if (zOrderRelativeOf->isHiddenByPolicy()) {
1156 return true;
1157 }
1158 }
1159 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001160 return s.flags & layer_state_t::eLayerHidden;
1161}
1162
David Sodman41fdfc92017-11-06 16:09:56 -08001163uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001164 // TODO: should we do something special if mSecure is set?
1165 if (mProtectedByApp) {
1166 // need a hardware-protected path to external video sink
1167 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001168 }
Riley Andrews03414a12014-07-01 14:22:59 -07001169 if (mPotentialCursor) {
1170 usage |= GraphicBuffer::USAGE_CURSOR;
1171 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001172 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001173 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001174}
1175
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001176void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001177 uint32_t orientation = 0;
Vishnu Nair5eb3f062019-04-08 08:21:03 -07001178 // Disable setting transform hint if the debug flag is set.
1179 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001180 // The transform hint is used to improve performance, but we can
1181 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001182 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001183 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001184 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001185 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001186 orientation = 0;
1187 }
1188 }
David Sodmaneb085e02017-10-05 18:49:04 -07001189 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001190}
1191
Mathias Agopian13127d82013-03-05 17:47:11 -08001192// ----------------------------------------------------------------------------
1193// debugging
1194// ----------------------------------------------------------------------------
1195
Marissa Wall61c58622018-07-18 10:12:20 -07001196// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001197LayerDebugInfo Layer::getLayerDebugInfo() const {
1198 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001199 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001200 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001201 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001202 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
chaviw8a01fa42019-08-19 12:39:31 -07001203 info.mType = getType();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001204 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001205 info.mVisibleRegion = visibleRegion;
1206 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001207 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001208 info.mX = ds.active_legacy.transform.tx();
1209 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001210 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001211 info.mWidth = ds.active_legacy.w;
1212 info.mHeight = ds.active_legacy.h;
1213 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001214 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001215 info.mFlags = ds.flags;
1216 info.mPixelFormat = getPixelFormat();
chaviw4244e032019-09-04 11:27:49 -07001217 info.mDataSpace = static_cast<android_dataspace>(getDataSpace());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001218 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1219 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1220 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1221 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001222 {
chaviwd62d3062019-09-04 14:48:02 -07001223 sp<const GraphicBuffer> buffer = getBuffer();
David Sodman5b4cffc2017-11-23 13:20:29 -08001224 if (buffer != 0) {
1225 info.mActiveBufferWidth = buffer->getWidth();
1226 info.mActiveBufferHeight = buffer->getHeight();
1227 info.mActiveBufferStride = buffer->getStride();
1228 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001229 } else {
1230 info.mActiveBufferWidth = 0;
1231 info.mActiveBufferHeight = 0;
1232 info.mActiveBufferStride = 0;
1233 info.mActiveBufferFormat = 0;
1234 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001235 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001236 info.mNumQueuedFrames = getQueuedFrameCount();
1237 info.mRefreshPending = isBufferLatched();
1238 info.mIsOpaque = isOpaque(ds);
1239 info.mContentDirty = contentDirty;
1240 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001241}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001242
Yiwei Zhang5434a782018-12-05 18:06:32 -08001243void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001244 result.append("-------------------------------");
1245 result.append("-------------------------------");
1246 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001247 result.append(" Layer name\n");
1248 result.append(" Z | ");
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001249 result.append(" Window Type | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001250 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001251 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001252 result.append(" Disp Frame (LTRB) | ");
1253 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001254 result.append("-------------------------------");
1255 result.append("-------------------------------");
1256 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001257}
1258
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001259void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1260 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1261 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001262 return;
1263 }
1264
Yiwei Zhang5434a782018-12-05 18:06:32 -08001265 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001266 if (mName.length() > 77) {
1267 std::string shortened;
1268 shortened.append(mName.string(), 36);
1269 shortened.append("[...]");
1270 shortened.append(mName.string() + (mName.length() - 36), 36);
Yiwei Zhang5434a782018-12-05 18:06:32 -08001271 name = shortened;
Dan Stozae22aec72016-08-01 13:20:59 -07001272 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001273 name = std::string(mName.string(), mName.size());
Dan Stozae22aec72016-08-01 13:20:59 -07001274 }
1275
Yiwei Zhang5434a782018-12-05 18:06:32 -08001276 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001277
Alec Mourib416efd2018-09-06 21:01:59 +00001278 const State& layerState(getDrawingState());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001279 const auto& compositionState = outputLayer->getState();
1280
Chia-I Wu1e043612018-03-01 09:45:09 -08001281 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001282 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001283 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001284 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001285 }
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001286 StringAppendF(&result, " %10d | ", mWindowType);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001287 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
1288 StringAppendF(&result, "%10s | ",
1289 toString(getCompositionLayer() ? compositionState.bufferTransform
1290 : static_cast<Hwc2::Transform>(0))
1291 .c_str());
1292 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001293 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001294 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001295 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1296 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001297
Yichi Chen6ca35192018-05-29 12:20:43 +08001298 result.append("- - - - - - - - - - - - - - - -");
1299 result.append("- - - - - - - - - - - - - - - -");
1300 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001301}
Dan Stozae22aec72016-08-01 13:20:59 -07001302
Yiwei Zhang5434a782018-12-05 18:06:32 -08001303void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001304 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001305}
1306
Svetoslavd85084b2014-03-20 10:28:31 -07001307void Layer::clearFrameStats() {
1308 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001309}
1310
Jamie Gennis6547ff42013-07-16 20:12:42 -07001311void Layer::logFrameStats() {
1312 mFrameTracker.logAndResetStats(mName);
1313}
1314
Svetoslavd85084b2014-03-20 10:28:31 -07001315void Layer::getFrameStats(FrameStats* outStats) const {
1316 mFrameTracker.getStats(outStats);
1317}
1318
Yiwei Zhang5434a782018-12-05 18:06:32 -08001319void Layer::dumpFrameEvents(std::string& result) {
chaviw8a01fa42019-08-19 12:39:31 -07001320 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().string(), getType(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001321 Mutex::Autolock lock(mFrameEventHistoryMutex);
1322 mFrameEventHistory.checkFencesForCompletion();
1323 mFrameEventHistory.dump(result);
1324}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001325
Vishnu Nair0f085c62019-08-30 08:49:12 -07001326void Layer::dumpCallingUidPid(std::string& result) const {
1327 StringAppendF(&result, "Layer %s (%s) pid:%d uid:%d\n", getName().string(), getType(),
1328 mCallingPid, mCallingUid);
1329}
1330
Brian Anderson5ea5e592016-12-01 16:54:33 -08001331void Layer::onDisconnect() {
1332 Mutex::Autolock lock(mFrameEventHistoryMutex);
1333 mFrameEventHistory.onDisconnect();
Mikael Pessa90092f42019-08-26 17:22:04 -07001334 const int32_t layerID = getSequence();
1335 mFlinger->mTimeStats->onDestroy(layerID);
1336 mFlinger->mFrameTracer->onDestroy(layerID);
Brian Anderson5ea5e592016-12-01 16:54:33 -08001337}
1338
Brian Anderson3890c392016-07-25 12:48:08 -07001339void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001340 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001341 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001342 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1343 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001344 }
1345
Brian Andersond6927fb2016-07-23 23:37:30 -07001346 Mutex::Autolock lock(mFrameEventHistoryMutex);
1347 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001348 // If there are any unsignaled fences in the aquire timeline at this
1349 // point, the previously queued frame hasn't been latched yet. Go ahead
1350 // and try to get the signal time here so the syscall is taken out of
1351 // the main thread's critical path.
1352 mAcquireTimeline.updateSignalTimes();
1353 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001354 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001355 mFrameEventHistory.addQueue(*newTimestamps);
1356 }
1357
Brian Anderson3890c392016-07-25 12:48:08 -07001358 if (outDelta) {
1359 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001360 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001361}
Dan Stozae77c7662016-05-13 11:37:28 -07001362
Chia-I Wu98f1c102017-05-30 14:54:08 -07001363size_t Layer::getChildrenCount() const {
1364 size_t count = 0;
1365 for (const sp<Layer>& child : mCurrentChildren) {
1366 count += 1 + child->getChildrenCount();
1367 }
1368 return count;
1369}
1370
Robert Carr1f0a16a2016-10-24 16:27:39 -07001371void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001372 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001373 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001374
Robert Carr1f0a16a2016-10-24 16:27:39 -07001375 mCurrentChildren.add(layer);
1376 layer->setParent(this);
1377}
1378
1379ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001380 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001381 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001382
Robert Carr1323c952019-01-28 18:13:27 -08001383 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001384 return mCurrentChildren.remove(layer);
1385}
1386
Robert Carr1db73f62016-12-21 12:58:51 -08001387bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1388 sp<Handle> handle = nullptr;
1389 sp<Layer> newParent = nullptr;
1390 if (newParentHandle == nullptr) {
1391 return false;
1392 }
1393 handle = static_cast<Handle*>(newParentHandle.get());
1394 newParent = handle->owner.promote();
1395 if (newParent == nullptr) {
1396 ALOGE("Unable to promote Layer handle");
1397 return false;
1398 }
1399
chaviw5aedec92018-10-22 10:40:38 -07001400 if (attachChildren()) {
1401 setTransactionFlags(eTransactionNeeded);
1402 }
Robert Carr1db73f62016-12-21 12:58:51 -08001403 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001404 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001405 }
1406 mCurrentChildren.clear();
1407
1408 return true;
1409}
1410
Robert Carr15eae092018-03-23 13:43:53 -07001411void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001412 for (const sp<Layer>& child : mDrawingChildren) {
1413 child->mDrawingParent = newParent;
Vishnu Nairc652ff82019-03-15 12:48:54 -07001414 child->computeBounds(newParent->mBounds,
1415 newParent->getTransformWithScale(
1416 newParent->getBufferScaleTransform()));
Robert Carr578038f2018-03-09 12:25:24 -08001417 }
1418}
1419
chaviwf1961f72017-09-18 16:41:07 -07001420bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001421 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001422
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001423 // While layers are detached, we allow most operations
1424 // and simply halt performing the actual transaction. However
1425 // for reparent != null we would enter the mRemovedFromCurrentState
1426 // state, regardless of whether doTransaction was called, and
1427 // so we need to prevent the update here.
1428 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001429 return false;
1430 }
1431
Robert Carr54cf5b12019-01-25 14:02:28 -08001432 sp<Layer> newParent;
1433 if (newParentHandle != nullptr) {
1434 auto handle = static_cast<Handle*>(newParentHandle.get());
1435 newParent = handle->owner.promote();
1436 if (newParent == nullptr) {
1437 ALOGE("Unable to promote Layer handle");
1438 return false;
1439 }
1440 if (newParent == this) {
1441 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1442 return false;
1443 }
1444 }
1445
chaviwf1961f72017-09-18 16:41:07 -07001446 sp<Layer> parent = getParent();
1447 if (parent != nullptr) {
1448 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001449 }
1450
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001451 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001452 newParent->addChild(this);
1453 if (!newParent->isRemovedFromCurrentState()) {
1454 addToCurrentState();
1455 } else {
1456 onRemovedFromCurrentState();
1457 }
1458
1459 if (mLayerDetached) {
1460 mLayerDetached = false;
1461 callSetTransactionFlags = true;
1462 }
1463 } else {
1464 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001465 }
1466
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001467 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001468 setTransactionFlags(eTransactionNeeded);
1469 }
chaviw06178942017-07-27 10:25:59 -07001470 return true;
1471}
1472
Robert Carr9524cb32017-02-13 11:32:32 -08001473bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001474 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001475 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001476 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001477 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001478 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001479 child->detachChildren();
chaviw43cb3cb2019-05-31 15:23:41 -07001480 child->removeRemoteSyncPoints();
Robert Carr9524cb32017-02-13 11:32:32 -08001481 }
Robert Carr7f619b22017-11-06 12:56:35 -08001482 }
Robert Carr9524cb32017-02-13 11:32:32 -08001483
1484 return true;
1485}
1486
chaviw5aedec92018-10-22 10:40:38 -07001487bool Layer::attachChildren() {
1488 bool changed = false;
1489 for (const sp<Layer>& child : mCurrentChildren) {
1490 sp<Client> parentClient = mClientRef.promote();
1491 sp<Client> client(child->mClientRef.promote());
1492 if (client != nullptr && parentClient != client) {
1493 if (child->mLayerDetached) {
1494 child->mLayerDetached = false;
1495 changed = true;
1496 }
1497 changed |= child->attachChildren();
1498 }
1499 }
1500
1501 return changed;
1502}
1503
Peiyong Lind3788632018-09-18 16:01:31 -07001504bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001505 static const mat4 identityMatrix = mat4();
1506
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001507 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001508 return false;
1509 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001510 ++mCurrentState.sequence;
1511 mCurrentState.colorTransform = matrix;
1512 mCurrentState.hasColorTransform = matrix != identityMatrix;
1513 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001514 setTransactionFlags(eTransactionNeeded);
1515 return true;
1516}
1517
chaviwf66724d2018-11-28 16:35:21 -08001518mat4 Layer::getColorTransform() const {
1519 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1520 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1521 colorTransform = parent->getColorTransform() * colorTransform;
1522 }
1523 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001524}
1525
1526bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001527 bool hasColorTransform = getDrawingState().hasColorTransform;
1528 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1529 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1530 }
1531 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001532}
1533
Chia-I Wu11481472018-05-04 10:43:19 -07001534bool Layer::isLegacyDataSpace() const {
1535 // return true when no higher bits are set
chaviw4244e032019-09-04 11:27:49 -07001536 return !(getDataSpace() &
1537 (ui::Dataspace::STANDARD_MASK | ui::Dataspace::TRANSFER_MASK |
1538 ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001539}
1540
Robert Carr1f0a16a2016-10-24 16:27:39 -07001541void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001542 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001543}
1544
Robert Carr1f0a16a2016-10-24 16:27:39 -07001545int32_t Layer::getZ() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001546 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001547}
1548
Robert Carr1c5481e2019-07-01 14:42:27 -07001549bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) const {
Robert Carr29abff82017-12-04 13:51:20 -08001550 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001551 const State& state = useDrawing ? mDrawingState : mCurrentState;
Robert Carr29abff82017-12-04 13:51:20 -08001552 return state.zOrderRelativeOf != nullptr;
1553}
1554
David Sodman41fdfc92017-11-06 16:09:56 -08001555__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001556 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001557 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1558 "makeTraversalList received invalid stateSet");
1559 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1560 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001561 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001562
Robert Carr29abff82017-12-04 13:51:20 -08001563 if (state.zOrderRelatives.size() == 0) {
1564 *outSkipRelativeZUsers = true;
1565 return children;
1566 }
1567
chaviwfd462612018-05-31 16:11:27 -07001568 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001569 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001570 sp<Layer> strongRelative = weakRelative.promote();
1571 if (strongRelative != nullptr) {
1572 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001573 }
1574 }
1575
Dan Stoza412903f2017-04-27 13:42:17 -07001576 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001577 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
Robert Carr503c7042017-09-27 15:06:08 -07001578 if (childState.zOrderRelativeOf != nullptr) {
1579 continue;
1580 }
Robert Carrdb66e622017-04-10 16:55:57 -07001581 traverse.add(child);
1582 }
1583
1584 return traverse;
1585}
1586
Robert Carr1f0a16a2016-10-24 16:27:39 -07001587/**
Robert Carrdb66e622017-04-10 16:55:57 -07001588 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001589 */
Dan Stoza412903f2017-04-27 13:42:17 -07001590void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001591 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1592 // produce a new list for traversing, including our relatives, and not including our children
1593 // who are relatives of another surface. In the case that there are no relative Z,
1594 // makeTraversalList returns our children directly to avoid significant overhead.
1595 // However in this case we need to take the responsibility for filtering children which
1596 // are relatives of another surface here.
1597 bool skipRelativeZUsers = false;
1598 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001599
Robert Carr1f0a16a2016-10-24 16:27:39 -07001600 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001601 for (; i < list.size(); i++) {
1602 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001603 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1604 continue;
1605 }
1606
Robert Carrdb66e622017-04-10 16:55:57 -07001607 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001608 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001609 }
Dan Stoza412903f2017-04-27 13:42:17 -07001610 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001611 }
Robert Carr29abff82017-12-04 13:51:20 -08001612
Dan Stoza412903f2017-04-27 13:42:17 -07001613 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001614 for (; i < list.size(); i++) {
1615 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001616
Robert Carr29abff82017-12-04 13:51:20 -08001617 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1618 continue;
1619 }
Dan Stoza412903f2017-04-27 13:42:17 -07001620 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001621 }
1622}
1623
1624/**
Robert Carrdb66e622017-04-10 16:55:57 -07001625 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001626 */
Dan Stoza412903f2017-04-27 13:42:17 -07001627void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1628 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001629 // See traverseInZOrder for documentation.
1630 bool skipRelativeZUsers = false;
1631 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001632
Robert Carr1f0a16a2016-10-24 16:27:39 -07001633 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001634 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001635 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001636
1637 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1638 continue;
1639 }
1640
Robert Carrdb66e622017-04-10 16:55:57 -07001641 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001642 break;
1643 }
Dan Stoza412903f2017-04-27 13:42:17 -07001644 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001645 }
Dan Stoza412903f2017-04-27 13:42:17 -07001646 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001647 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001648 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001649
1650 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1651 continue;
1652 }
1653
Dan Stoza412903f2017-04-27 13:42:17 -07001654 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001655 }
1656}
1657
chaviw4b129c22018-04-09 16:19:43 -07001658LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1659 const std::vector<Layer*>& layersInTree) {
1660 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1661 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001662 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1663 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001664 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001665
chaviwfd462612018-05-31 16:11:27 -07001666 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001667 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1668 sp<Layer> strongRelative = weakRelative.promote();
1669 // Only add relative layers that are also descendents of the top most parent of the tree.
1670 // If a relative layer is not a descendent, then it should be ignored.
1671 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1672 traverse.add(strongRelative);
1673 }
1674 }
1675
1676 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001677 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001678 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1679 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1680 // the child here since it won't be added later as a relative.
1681 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1682 childState.zOrderRelativeOf.promote().get())) {
1683 continue;
1684 }
1685 traverse.add(child);
1686 }
1687
1688 return traverse;
1689}
1690
1691void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1692 LayerVector::StateSet stateSet,
1693 const LayerVector::Visitor& visitor) {
1694 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001695
1696 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001697 for (; i < list.size(); i++) {
1698 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001699 if (relative->getZ() >= 0) {
1700 break;
1701 }
chaviw4b129c22018-04-09 16:19:43 -07001702 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001703 }
chaviw4b129c22018-04-09 16:19:43 -07001704
chaviwa76b2712017-09-20 12:02:26 -07001705 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001706 for (; i < list.size(); i++) {
1707 const auto& relative = list[i];
1708 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001709 }
1710}
1711
chaviw4b129c22018-04-09 16:19:43 -07001712std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1713 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1714 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1715
1716 std::vector<Layer*> layersInTree = {this};
1717 for (size_t i = 0; i < children.size(); i++) {
1718 const auto& child = children[i];
1719 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1720 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1721 }
1722
1723 return layersInTree;
1724}
1725
1726void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1727 const LayerVector::Visitor& visitor) {
1728 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1729 std::sort(layersInTree.begin(), layersInTree.end());
1730 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1731}
1732
Peiyong Linefefaac2018-08-17 12:27:51 -07001733ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001734 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001735}
1736
chaviw13fdc492017-06-27 12:40:18 -07001737half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001738 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001739
chaviw13fdc492017-06-27 12:40:18 -07001740 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1741 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001742}
Robert Carr6452f122017-03-21 10:41:29 -07001743
chaviw13fdc492017-06-27 12:40:18 -07001744half4 Layer::getColor() const {
1745 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001746 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001747}
Robert Carr6452f122017-03-21 10:41:29 -07001748
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001749Layer::RoundedCornerState Layer::getRoundedCornerState() const {
1750 const auto& p = mDrawingParent.promote();
1751 if (p != nullptr) {
Peiyong Lin27016a92019-03-29 17:36:08 +00001752 RoundedCornerState parentState = p->getRoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001753 if (parentState.radius > 0) {
1754 ui::Transform t = getActiveTransform(getDrawingState());
1755 t = t.inverse();
1756 parentState.cropRect = t.transform(parentState.cropRect);
1757 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1758 // but a transform matrix can define horizontal and vertical scales.
1759 // Let's take the average between both of them and pass into the shader, practically we
1760 // never do this type of transformation on windows anyway.
1761 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
1762 return parentState;
1763 }
1764 }
1765 const float radius = getDrawingState().cornerRadius;
Peiyong Linb9ff23e2019-04-08 11:04:59 -07001766 return radius > 0 && getCrop(getDrawingState()).isValid()
1767 ? RoundedCornerState(getCrop(getDrawingState()).toFloatRect(), radius)
1768 : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001769}
1770
Robert Carr1f0a16a2016-10-24 16:27:39 -07001771void Layer::commitChildList() {
1772 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1773 const auto& child = mCurrentChildren[i];
1774 child->commitChildList();
1775 }
1776 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001777 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001778}
1779
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001780static wp<Layer> extractLayerFromBinder(const wp<IBinder>& weakBinderHandle) {
1781 if (weakBinderHandle == nullptr) {
1782 return nullptr;
1783 }
1784 sp<IBinder> binderHandle = weakBinderHandle.promote();
1785 if (binderHandle == nullptr) {
1786 return nullptr;
1787 }
1788 sp<Layer::Handle> handle = static_cast<Layer::Handle*>(binderHandle.get());
1789 if (handle == nullptr) {
1790 return nullptr;
1791 }
1792 return handle->owner;
1793}
1794
Robert Carr720e5062018-07-30 17:45:14 -07001795void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001796 mCurrentState.inputInfo = info;
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001797 mCurrentState.touchableRegionCrop = extractLayerFromBinder(info.touchableRegionCropHandle);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001798 mCurrentState.modified = true;
1799 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07001800 setTransactionFlags(eTransactionNeeded);
1801}
1802
Vishnu Nair8406fd72019-07-30 11:29:31 -07001803void Layer::writeToProtoDrawingState(LayerProto* layerInfo, uint32_t traceFlags) const {
1804 ui::Transform transform = getTransform();
1805
1806 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
1807 for (const auto& pendingState : mPendingStatesSnapshot) {
1808 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
1809 if (barrierLayer != nullptr) {
1810 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
1811 barrierLayerProto->set_id(barrierLayer->sequence);
1812 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
1813 }
1814 }
1815
chaviwd62d3062019-09-04 14:48:02 -07001816 auto buffer = getBuffer();
Vishnu Nair8406fd72019-07-30 11:29:31 -07001817 if (buffer != nullptr) {
1818 LayerProtoHelper::writeToProto(buffer,
1819 [&]() { return layerInfo->mutable_active_buffer(); });
chaviw4244e032019-09-04 11:27:49 -07001820 LayerProtoHelper::writeToProto(ui::Transform(getBufferTransform()),
Vishnu Nair8406fd72019-07-30 11:29:31 -07001821 layerInfo->mutable_buffer_transform());
1822 }
1823 layerInfo->set_invalidate(contentDirty);
1824 layerInfo->set_is_protected(isProtected());
chaviw4244e032019-09-04 11:27:49 -07001825 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(getDataSpace())));
Vishnu Nair8406fd72019-07-30 11:29:31 -07001826 layerInfo->set_queued_frames(getQueuedFrameCount());
1827 layerInfo->set_refresh_pending(isBufferLatched());
1828 layerInfo->set_curr_frame(mCurrentFrameNumber);
1829 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
1830
1831 layerInfo->set_corner_radius(getRoundedCornerState().radius);
1832 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1833 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
1834 [&]() { return layerInfo->mutable_position(); });
1835 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
1836 LayerProtoHelper::writeToProto(visibleRegion,
1837 [&]() { return layerInfo->mutable_visible_region(); });
1838 LayerProtoHelper::writeToProto(surfaceDamageRegion,
1839 [&]() { return layerInfo->mutable_damage_region(); });
1840 }
1841
1842 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
1843 LayerProtoHelper::writeToProto(mSourceBounds,
1844 [&]() { return layerInfo->mutable_source_bounds(); });
1845 LayerProtoHelper::writeToProto(mScreenBounds,
1846 [&]() { return layerInfo->mutable_screen_bounds(); });
1847 }
1848}
1849
1850void Layer::writeToProtoCommonState(LayerProto* layerInfo, LayerVector::StateSet stateSet,
1851 uint32_t traceFlags) const {
chaviw1d044282017-09-27 12:19:28 -07001852 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1853 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001854 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07001855
Peiyong Linefefaac2018-08-17 12:27:51 -07001856 ui::Transform requestedTransform = state.active_legacy.transform;
chaviw1d044282017-09-27 12:19:28 -07001857
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001858 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
1859 layerInfo->set_id(sequence);
1860 layerInfo->set_name(getName().c_str());
chaviw8a01fa42019-08-19 12:39:31 -07001861 layerInfo->set_type(getType());
chaviw1d044282017-09-27 12:19:28 -07001862
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001863 for (const auto& child : children) {
1864 layerInfo->add_children(child->sequence);
chaviw1d044282017-09-27 12:19:28 -07001865 }
chaviw1d044282017-09-27 12:19:28 -07001866
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001867 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1868 sp<Layer> strongRelative = weakRelative.promote();
1869 if (strongRelative != nullptr) {
1870 layerInfo->add_relatives(strongRelative->sequence);
1871 }
chaviwadc40c22018-07-10 16:57:27 -07001872 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001873
1874 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
1875 [&]() { return layerInfo->mutable_transparent_region(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001876
1877 layerInfo->set_layer_stack(getLayerStack());
1878 layerInfo->set_z(state.z);
1879
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001880 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(),
1881 [&]() {
1882 return layerInfo->mutable_requested_position();
1883 });
1884
1885 LayerProtoHelper::writeSizeToProto(state.active_legacy.w, state.active_legacy.h,
1886 [&]() { return layerInfo->mutable_size(); });
1887
1888 LayerProtoHelper::writeToProto(state.crop_legacy,
1889 [&]() { return layerInfo->mutable_crop(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001890
1891 layerInfo->set_is_opaque(isOpaque(state));
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001892
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001893
1894 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1895 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
1896 LayerProtoHelper::writeToProto(state.color,
1897 [&]() { return layerInfo->mutable_requested_color(); });
1898 layerInfo->set_flags(state.flags);
1899
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001900 LayerProtoHelper::writeToProto(requestedTransform,
1901 layerInfo->mutable_requested_transform());
1902
1903 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
1904 if (parent != nullptr) {
1905 layerInfo->set_parent(parent->sequence);
1906 } else {
1907 layerInfo->set_parent(-1);
1908 }
1909
1910 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1911 if (zOrderRelativeOf != nullptr) {
1912 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1913 } else {
1914 layerInfo->set_z_order_relative_of(-1);
1915 }
chaviwadc40c22018-07-10 16:57:27 -07001916 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001917
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001918 if (traceFlags & SurfaceTracing::TRACE_INPUT) {
1919 LayerProtoHelper::writeToProto(state.inputInfo, state.touchableRegionCrop,
1920 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001921 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001922
1923 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
1924 auto protoMap = layerInfo->mutable_metadata();
1925 for (const auto& entry : state.metadata.mMap) {
1926 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
1927 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001928 }
chaviw1d044282017-09-27 12:19:28 -07001929}
1930
Robert Carr2e102c92018-10-23 12:11:15 -07001931bool Layer::isRemovedFromCurrentState() const {
1932 return mRemovedFromCurrentState;
1933}
1934
Arthur Hungd20b2702019-01-14 18:16:16 +08001935InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001936 InputWindowInfo info = mDrawingState.inputInfo;
Robert Carr720e5062018-07-30 17:45:14 -07001937
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001938 if (info.displayId == ADISPLAY_ID_NONE) {
chaviwb15ea8a2019-09-03 12:40:22 -07001939 info.displayId = getLayerStack();
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001940 }
1941
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001942 ui::Transform t = getTransform();
1943 const float xScale = t.sx();
1944 const float yScale = t.sy();
Ady Abraham282f1d72019-07-24 18:05:56 -07001945 int32_t xSurfaceInset = info.surfaceInset;
1946 int32_t ySurfaceInset = info.surfaceInset;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001947 if (xScale != 1.0f || yScale != 1.0f) {
Ady Abraham282f1d72019-07-24 18:05:56 -07001948 info.windowXScale *= (xScale != 0.0f) ? 1.0f / xScale : 0.0f;
1949 info.windowYScale *= (yScale != 0.0f) ? 1.0f / yScale : 0.0f;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001950 info.touchableRegion.scaleSelf(xScale, yScale);
Ady Abraham282f1d72019-07-24 18:05:56 -07001951 xSurfaceInset = std::round(xSurfaceInset * xScale);
1952 ySurfaceInset = std::round(ySurfaceInset * yScale);
Riddle Hsu39d4aa52018-11-30 20:46:53 +08001953 }
Robert Carre07e1032018-11-26 12:55:53 -08001954
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001955 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001956 // If this is a portal window, set the touchableRegion to the layerBounds.
1957 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
1958 ? getBufferSize(getDrawingState())
1959 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08001960 if (!layerBounds.isValid()) {
1961 layerBounds = getCroppedBufferSize(getDrawingState());
1962 }
Vishnu Nair8033a492018-12-05 07:27:23 -08001963 layerBounds = t.transform(layerBounds);
Ady Abraham282f1d72019-07-24 18:05:56 -07001964
1965 // clamp inset to layer bounds
1966 xSurfaceInset = (xSurfaceInset >= 0) ? std::min(xSurfaceInset, layerBounds.getWidth() / 2) : 0;
1967 ySurfaceInset = (ySurfaceInset >= 0) ? std::min(ySurfaceInset, layerBounds.getHeight() / 2) : 0;
1968
Arthur Hung118b1142019-05-08 21:25:59 +08001969 layerBounds.inset(xSurfaceInset, ySurfaceInset, xSurfaceInset, ySurfaceInset);
Vishnu Nair8033a492018-12-05 07:27:23 -08001970
Arthur Hungd20b2702019-01-14 18:16:16 +08001971 // Input coordinate should match the layer bounds.
1972 info.frameLeft = layerBounds.left;
1973 info.frameTop = layerBounds.top;
1974 info.frameRight = layerBounds.right;
1975 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08001976
1977 // Position the touchable region relative to frame screen location and restrict it to frame
1978 // bounds.
1979 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08001980 info.visible = canReceiveInput();
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001981
1982 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
1983 if (info.replaceTouchableRegionWithCrop) {
1984 if (cropLayer == nullptr) {
1985 info.touchableRegion = Region(Rect{mScreenBounds});
1986 } else {
1987 info.touchableRegion = Region(Rect{cropLayer->mScreenBounds});
1988 }
1989 } else if (cropLayer != nullptr) {
1990 info.touchableRegion = info.touchableRegion.intersect(Rect{cropLayer->mScreenBounds});
1991 }
1992
Robert Carr720e5062018-07-30 17:45:14 -07001993 return info;
1994}
1995
1996bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001997 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07001998}
1999
Lloyd Piquefeb73d72018-12-04 17:23:44 -08002000std::shared_ptr<compositionengine::Layer> Layer::getCompositionLayer() const {
2001 return nullptr;
2002}
2003
chaviw3e727cd2019-01-31 13:41:05 -08002004bool Layer::canReceiveInput() const {
2005 return isVisible();
2006}
2007
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002008compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
2009 const sp<const DisplayDevice>& display) const {
2010 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionLayer().get());
2011}
2012
Mathias Agopian13127d82013-03-05 17:47:11 -08002013// ---------------------------------------------------------------------------
2014
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002015}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002016
2017#if defined(__gl_h_)
2018#error "don't include gl/gl.h in this file"
2019#endif
2020
2021#if defined(__gl2_h_)
2022#error "don't include gl2/gl2.h in this file"
2023#endif