blob: c916f74291b557bad107e6e270d3051f16b1876b [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 Agopiana67932f2011-04-20 14:20:59 -070081 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070082
83 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -070084 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
85 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
86 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070087
Dan Stozaf7ba41a2017-05-10 15:11:11 -070088 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070089
Lloyd Pique0449b0f2018-12-20 16:23:45 -080090 mCurrentState.active_legacy.w = args.w;
91 mCurrentState.active_legacy.h = args.h;
92 mCurrentState.flags = layerFlags;
93 mCurrentState.active_legacy.transform.set(0, 0);
94 mCurrentState.crop_legacy.makeInvalid();
95 mCurrentState.requestedCrop_legacy = mCurrentState.crop_legacy;
96 mCurrentState.z = 0;
97 mCurrentState.color.a = 1.0f;
98 mCurrentState.layerStack = 0;
99 mCurrentState.sequence = 0;
100 mCurrentState.requested_legacy = mCurrentState.active_legacy;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800101 mCurrentState.active.w = UINT32_MAX;
102 mCurrentState.active.h = UINT32_MAX;
103 mCurrentState.active.transform.set(0, 0);
104 mCurrentState.transform = 0;
105 mCurrentState.transformToDisplayInverse = false;
106 mCurrentState.crop.makeInvalid();
107 mCurrentState.acquireFence = new Fence(-1);
108 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
109 mCurrentState.hdrMetadata.validTypes = 0;
Marissa Wall7e0a01f2019-08-14 14:29:25 -0700110 mCurrentState.surfaceDamageRegion = Region::INVALID_REGION;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800111 mCurrentState.cornerRadius = 0.0f;
112 mCurrentState.api = -1;
113 mCurrentState.hasColorTransform = false;
Peiyong Linc502cb72019-03-01 15:00:23 -0800114 mCurrentState.colorSpaceAgnostic = false;
Evan Roskya1f1e152019-01-24 16:17:46 -0800115 mCurrentState.metadata = args.metadata;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700116
117 // drawing state & current state are identical
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800118 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700119
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800120 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700121 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800122 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200123 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700124
Ady Abraham8f1ee7f2019-04-05 10:32:50 -0700125 mSchedulerLayerHandle = mFlinger->mScheduler->registerLayer(mName.c_str(), mWindowType);
Vishnu Nair0f085c62019-08-30 08:49:12 -0700126 mCallingPid = args.callingPid;
127 mCallingUid = args.callingUid;
Robert Carr2e102c92018-10-23 12:11:15 -0700128 mFlinger->onLayerCreated();
Dan Stoza436ccf32018-06-21 12:10:12 -0700129}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700130
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700131Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800132 sp<Client> c(mClientRef.promote());
133 if (c != 0) {
134 c->detachLayer(this);
135 }
136
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000137 mFrameTracker.logAndResetStats(mName);
chaviw74d90ad2019-04-26 14:45:26 -0700138 mFlinger->onLayerDestroyed(this);
Mathias Agopian96f08192010-06-02 23:28:45 -0700139}
140
Vishnu Nair0f085c62019-08-30 08:49:12 -0700141LayerCreationArgs::LayerCreationArgs(SurfaceFlinger* flinger, const sp<Client>& client,
142 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
143 LayerMetadata metadata)
144 : flinger(flinger),
145 client(client),
146 name(name),
147 w(w),
148 h(h),
149 flags(flags),
150 metadata(std::move(metadata)) {
151 IPCThreadState* ipc = IPCThreadState::self();
152 callingPid = ipc->getCallingPid();
153 callingUid = ipc->getCallingUid();
154}
155
Mathias Agopian13127d82013-03-05 17:47:11 -0800156// ---------------------------------------------------------------------------
157// callbacks
158// ---------------------------------------------------------------------------
159
David Sodmaneb085e02017-10-05 18:49:04 -0700160/*
161 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
162 * Layer. So, the implementation is done in BufferLayer. When called on a
163 * ColorLayer object, it's essentially a NOP.
164 */
David Sodmaneb085e02017-10-05 18:49:04 -0700165void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800166
chaviw43cb3cb2019-05-31 15:23:41 -0700167void Layer::removeRemoteSyncPoints() {
168 for (auto& point : mRemoteSyncPoints) {
169 point->setTransactionApplied();
170 }
171 mRemoteSyncPoints.clear();
172
173 {
chaviw43cb3cb2019-05-31 15:23:41 -0700174 for (State pendingState : mPendingStates) {
175 pendingState.barrierLayer_legacy = nullptr;
176 }
177 }
178}
179
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700180void Layer::removeRelativeZ(const std::vector<Layer*>& layersInTree) {
181 if (mCurrentState.zOrderRelativeOf == nullptr) {
182 return;
183 }
Robert Carr2e102c92018-10-23 12:11:15 -0700184
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700185 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
186 if (strongRelative == nullptr) {
187 setZOrderRelativeOf(nullptr);
188 return;
189 }
190
191 if (!std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
192 strongRelative->removeZOrderRelative(this);
193 mFlinger->setTransactionFlags(eTraversalNeeded);
chaviw606e5cf2019-03-01 10:12:10 -0800194 setZOrderRelativeOf(nullptr);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700195 }
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700196}
197
198void Layer::removeFromCurrentState() {
199 mRemovedFromCurrentState = true;
Rob Carr4bba3702018-10-08 21:53:30 +0000200
Robert Carr2e102c92018-10-23 12:11:15 -0700201 // Since we are no longer reachable from CurrentState SurfaceFlinger
202 // will no longer invoke doTransaction for us, and so we will
203 // never finish applying transactions. We signal the sync point
204 // now so that another layer will not become indefinitely
205 // blocked.
chaviw43cb3cb2019-05-31 15:23:41 -0700206 removeRemoteSyncPoints();
Robert Carr2e102c92018-10-23 12:11:15 -0700207
208 {
209 Mutex::Autolock syncLock(mLocalSyncPointMutex);
210 for (auto& point : mLocalSyncPoints) {
211 point->setFrameAvailable();
212 }
213 mLocalSyncPoints.clear();
214 }
215
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800216 mFlinger->markLayerPendingRemovalLocked(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700217}
Chia-I Wu38512252017-05-17 14:36:16 -0700218
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700219void Layer::onRemovedFromCurrentState() {
220 auto layersInTree = getLayersInTree(LayerVector::StateSet::Current);
221 std::sort(layersInTree.begin(), layersInTree.end());
222 for (const auto& layer : layersInTree) {
223 layer->removeFromCurrentState();
224 layer->removeRelativeZ(layersInTree);
225 }
226}
227
chaviw61626f22018-11-15 16:26:27 -0800228void Layer::addToCurrentState() {
229 mRemovedFromCurrentState = false;
230
231 for (const auto& child : mCurrentChildren) {
232 child->addToCurrentState();
233 }
234}
235
Mathias Agopian13127d82013-03-05 17:47:11 -0800236// ---------------------------------------------------------------------------
237// set-up
238// ---------------------------------------------------------------------------
239
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700240const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800241 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800242}
243
chaviw13fdc492017-06-27 12:40:18 -0700244bool Layer::getPremultipledAlpha() const {
245 return mPremultipliedAlpha;
246}
247
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700248sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800249 Mutex::Autolock _l(mLock);
Robert Carrc0df3122019-04-11 13:18:21 -0700250 if (mGetHandleCalled) {
251 ALOGE("Get handle called twice" );
252 return nullptr;
253 }
254 mGetHandleCalled = true;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700255 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800256}
257
258// ---------------------------------------------------------------------------
259// h/w composer set-up
260// ---------------------------------------------------------------------------
261
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800262Rect Layer::getContentCrop() const {
263 // this is the crop rectangle that applies to the buffer
264 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700265 Rect crop;
266 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800267 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700268 crop = mCurrentCrop;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800269 } else if (mActiveBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800270 // otherwise we use the whole buffer
Lloyd Pique0b785d82018-12-04 17:25:27 -0800271 crop = mActiveBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700272 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800273 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700274 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700275 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700276 return crop;
277}
278
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700279static Rect reduce(const Rect& win, const Region& exclude) {
280 if (CC_LIKELY(exclude.isEmpty())) {
281 return win;
282 }
283 if (exclude.isRect()) {
284 return win.reduce(exclude.getBounds());
285 }
286 return Region(win).subtract(exclude).getBounds();
287}
288
Dan Stoza80d61162017-12-20 15:57:52 -0800289static FloatRect reduce(const FloatRect& win, const Region& exclude) {
290 if (CC_LIKELY(exclude.isEmpty())) {
291 return win;
292 }
293 // Convert through Rect (by rounding) for lack of FloatRegion
294 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
295}
296
Vishnu Nair4351ad52019-02-11 14:13:02 -0800297Rect Layer::getScreenBounds(bool reduceTransparentRegion) const {
Vishnu Nairf0c28512019-02-08 12:40:28 -0800298 if (!reduceTransparentRegion) {
299 return Rect{mScreenBounds};
300 }
301
302 FloatRect bounds = getBounds();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800303 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800304 // Transform to screen space.
305 bounds = t.transform(bounds);
306 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700307}
308
Vishnu Nair4351ad52019-02-11 14:13:02 -0800309FloatRect Layer::getBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000310 const State& s(getDrawingState());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800311 return getBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700312}
313
Vishnu Nairf0c28512019-02-08 12:40:28 -0800314FloatRect Layer::getBounds(const Region& activeTransparentRegion) const {
315 // Subtract the transparent region and snap to the bounds.
316 return reduce(mBounds, activeTransparentRegion);
317}
318
Vishnu Nairc652ff82019-03-15 12:48:54 -0700319ui::Transform Layer::getBufferScaleTransform() const {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800320 // If the layer is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
321 // it isFixedSize) then there may be additional scaling not accounted
Vishnu Nairc652ff82019-03-15 12:48:54 -0700322 // for in the layer transform.
Lloyd Pique0b785d82018-12-04 17:25:27 -0800323 if (!isFixedSize() || !mActiveBuffer) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700324 return {};
Vishnu Nair4351ad52019-02-11 14:13:02 -0800325 }
326
Marissa Wall290ad082019-03-06 13:23:47 -0800327 // If the layer is a buffer state layer, the active width and height
328 // could be infinite. In that case, return the effective transform.
329 const uint32_t activeWidth = getActiveWidth(getDrawingState());
330 const uint32_t activeHeight = getActiveHeight(getDrawingState());
331 if (activeWidth >= UINT32_MAX && activeHeight >= UINT32_MAX) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700332 return {};
Marissa Wall290ad082019-03-06 13:23:47 -0800333 }
334
Vishnu Nairc652ff82019-03-15 12:48:54 -0700335 int bufferWidth = mActiveBuffer->getWidth();
336 int bufferHeight = mActiveBuffer->getHeight();
337
338 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
339 std::swap(bufferWidth, bufferHeight);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800340 }
Vishnu Nairc652ff82019-03-15 12:48:54 -0700341
Marissa Wall290ad082019-03-06 13:23:47 -0800342 float sx = activeWidth / static_cast<float>(bufferWidth);
343 float sy = activeHeight / static_cast<float>(bufferHeight);
344
Vishnu Nair4351ad52019-02-11 14:13:02 -0800345 ui::Transform extraParentScaling;
346 extraParentScaling.set(sx, 0, 0, sy);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700347 return extraParentScaling;
348}
349
350ui::Transform Layer::getTransformWithScale(const ui::Transform& bufferScaleTransform) const {
351 // We need to mirror this scaling to child surfaces or we will break the contract where WM can
352 // treat child surfaces as pixels in the parent surface.
353 if (!isFixedSize() || !mActiveBuffer) {
354 return mEffectiveTransform;
355 }
356 return mEffectiveTransform * bufferScaleTransform;
357}
358
359FloatRect Layer::getBoundsPreScaling(const ui::Transform& bufferScaleTransform) const {
360 // We need the pre scaled layer bounds when computing child bounds to make sure the child is
361 // cropped to its parent layer after any buffer transform scaling is applied.
362 if (!isFixedSize() || !mActiveBuffer) {
363 return mBounds;
364 }
365 return bufferScaleTransform.inverse().transform(mBounds);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800366}
367
368void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform) {
369 const State& s(getDrawingState());
370
371 // Calculate effective layer transform
372 mEffectiveTransform = parentTransform * getActiveTransform(s);
373
374 // Transform parent bounds to layer space
375 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
376
Vishnu Nairc652ff82019-03-15 12:48:54 -0700377 // Calculate source bounds
Vishnu Nair4351ad52019-02-11 14:13:02 -0800378 mSourceBounds = computeSourceBounds(parentBounds);
379
380 // Calculate bounds by croping diplay frame with layer crop and parent bounds
381 FloatRect bounds = mSourceBounds;
382 const Rect layerCrop = getCrop(s);
383 if (!layerCrop.isEmpty()) {
384 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
385 }
386 bounds = bounds.intersect(parentBounds);
387
388 mBounds = bounds;
389 mScreenBounds = mEffectiveTransform.transform(mBounds);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700390
391 // Add any buffer scaling to the layer's children.
392 ui::Transform bufferScaleTransform = getBufferScaleTransform();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800393 for (const sp<Layer>& child : mDrawingChildren) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700394 child->computeBounds(getBoundsPreScaling(bufferScaleTransform),
395 getTransformWithScale(bufferScaleTransform));
Vishnu Nair4351ad52019-02-11 14:13:02 -0800396 }
397}
398
Vishnu Nair60356342018-11-13 13:00:45 -0800399Rect Layer::getCroppedBufferSize(const State& s) const {
400 Rect size = getBufferSize(s);
401 Rect crop = getCrop(s);
402 if (!crop.isEmpty() && size.isValid()) {
403 size.intersect(crop, &size);
404 } else if (!crop.isEmpty()) {
405 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700406 }
Vishnu Nair60356342018-11-13 13:00:45 -0800407 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800408}
409
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700410void Layer::setupRoundedCornersCropCoordinates(Rect win,
411 const FloatRect& roundedCornersCrop) const {
412 // Translate win by the rounded corners rect coordinates, to have all values in
413 // layer coordinate space.
414 win.left -= roundedCornersCrop.left;
415 win.right -= roundedCornersCrop.left;
416 win.top -= roundedCornersCrop.top;
417 win.bottom -= roundedCornersCrop.top;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700418}
419
Lloyd Piquea83776c2019-01-29 18:42:32 -0800420void Layer::latchGeometry(compositionengine::LayerFECompositionState& compositionState) const {
421 const auto& drawingState{getDrawingState()};
422 auto alpha = static_cast<float>(getAlpha());
David Revemanecf0fa52017-03-03 11:32:44 -0500423 auto blendMode = HWC2::BlendMode::None;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800424 if (!isOpaque(drawingState) || alpha != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800425 blendMode =
426 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800427 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800428
Lloyd Piquea83776c2019-01-29 18:42:32 -0800429 int type = drawingState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
430 int appId = drawingState.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700431 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400432 if (parent.get()) {
433 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800434 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
435 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
Jiwen 'Steve' Cai736c74e2019-05-28 18:33:22 -0700436 if (parentType > 0 && parentAppId > 0) {
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800437 type = parentType;
438 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700439 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400440 }
441
Lloyd Piquea83776c2019-01-29 18:42:32 -0800442 compositionState.geomLayerTransform = getTransform();
443 compositionState.geomInverseLayerTransform = compositionState.geomLayerTransform.inverse();
444 compositionState.geomBufferSize = getBufferSize(drawingState);
445 compositionState.geomContentCrop = getContentCrop();
446 compositionState.geomCrop = getCrop(drawingState);
447 compositionState.geomBufferTransform = mCurrentTransform;
448 compositionState.geomBufferUsesDisplayInverseTransform = getTransformToDisplayInverse();
449 compositionState.geomActiveTransparentRegion = getActiveTransparentRegion(drawingState);
450 compositionState.geomLayerBounds = mBounds;
451 compositionState.geomUsesSourceCrop = usesSourceCrop();
452 compositionState.isSecure = isSecure();
David Sodman15094112018-10-11 09:39:37 -0700453
Lloyd Piquea83776c2019-01-29 18:42:32 -0800454 compositionState.blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
455 compositionState.alpha = alpha;
456 compositionState.type = type;
457 compositionState.appId = appId;
458}
David Sodmanba340492018-08-05 21:51:33 -0700459
Lloyd Piquef5275482019-01-29 18:42:42 -0800460void Layer::latchPerFrameState(compositionengine::LayerFECompositionState& compositionState) const {
Lloyd Pique688abd42019-02-15 15:42:24 -0800461 const auto& drawingState{getDrawingState()};
Lloyd Piquef5275482019-01-29 18:42:42 -0800462 compositionState.forceClientComposition = false;
463
464 // TODO(lpique): b/121291683 Remove this one we are sure we don't need the
465 // value recomputed / set every frame.
466 compositionState.geomVisibleRegion = visibleRegion;
467
468 compositionState.isColorspaceAgnostic = isColorSpaceAgnostic();
469 compositionState.dataspace = mCurrentDataSpace;
470 compositionState.colorTransform = getColorTransform();
471 compositionState.colorTransformIsIdentity = !hasColorTransform();
472 compositionState.surfaceDamage = surfaceDamageRegion;
Lloyd Pique688abd42019-02-15 15:42:24 -0800473 compositionState.hasProtectedContent = isProtected();
474
475 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
476 compositionState.isOpaque =
477 isOpaque(drawingState) && !usesRoundedCorners && getAlpha() == 1.0_hf;
Lloyd Piquef5275482019-01-29 18:42:42 -0800478
479 // Force client composition for special cases known only to the front-end.
Lloyd Pique688abd42019-02-15 15:42:24 -0800480 if (isHdrY410() || usesRoundedCorners) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800481 compositionState.forceClientComposition = true;
482 }
483}
484
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800485void Layer::latchCursorCompositionState(
486 compositionengine::LayerFECompositionState& compositionState) const {
487 // This gives us only the "orientation" component of the transform
488 const State& drawingState{getDrawingState()};
489
490 // Apply the layer's transform, followed by the display's global transform
491 // Here we're guaranteed that the layer's transform preserves rects
492 Rect win = getCroppedBufferSize(drawingState);
493 // Subtract the transparent region and snap to the bounds
494 Rect bounds = reduce(win, getActiveTransparentRegion(drawingState));
495 Rect frame(getTransform().transform(bounds));
496
497 compositionState.cursorFrame = frame;
498}
499
Lloyd Piquef16688f2019-02-19 17:47:57 -0800500bool Layer::onPreComposition(nsecs_t) {
501 return false;
502}
503
Lloyd Piquea83776c2019-01-29 18:42:32 -0800504void Layer::latchCompositionState(compositionengine::LayerFECompositionState& compositionState,
505 bool includeGeometry) const {
506 if (includeGeometry) {
507 latchGeometry(compositionState);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700508 }
Lloyd Piquef5275482019-01-29 18:42:42 -0800509
510 latchPerFrameState(compositionState);
Lloyd Piquea83776c2019-01-29 18:42:32 -0800511}
Mathias Agopian29a367b2011-07-12 14:51:45 -0700512
Lloyd Piquea83776c2019-01-29 18:42:32 -0800513const char* Layer::getDebugName() const {
514 return mName.string();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800515}
516
Mathias Agopian13127d82013-03-05 17:47:11 -0800517// ---------------------------------------------------------------------------
518// drawing...
519// ---------------------------------------------------------------------------
520
Lloyd Piquef16688f2019-02-19 17:47:57 -0800521std::optional<renderengine::LayerSettings> Layer::prepareClientComposition(
522 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
523 if (!getCompositionLayer()) {
524 return {};
525 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800526
Vishnu Nair4351ad52019-02-11 14:13:02 -0800527 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000528 half alpha = getAlpha();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800529 renderengine::LayerSettings layerSettings;
530 layerSettings.geometry.boundaries = bounds;
531 if (targetSettings.useIdentityTransform) {
532 layerSettings.geometry.positionTransform = mat4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000533 } else {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800534 layerSettings.geometry.positionTransform = getTransform().asMatrix4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000535 }
536
537 if (hasColorTransform()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800538 layerSettings.colorTransform = getColorTransform();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000539 }
540
541 const auto roundedCornerState = getRoundedCornerState();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800542 layerSettings.geometry.roundedCornersRadius = roundedCornerState.radius;
543 layerSettings.geometry.roundedCornersCrop = roundedCornerState.cropRect;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000544
Lloyd Piquef16688f2019-02-19 17:47:57 -0800545 layerSettings.alpha = alpha;
546 layerSettings.sourceDataspace = mCurrentDataSpace;
547 return layerSettings;
Mathias Agopian13127d82013-03-05 17:47:11 -0800548}
549
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800550Hwc2::IComposerClient::Composition Layer::getCompositionType(
551 const sp<const DisplayDevice>& display) const {
552 const auto outputLayer = findOutputLayerForDisplay(display);
553 LOG_FATAL_IF(!outputLayer);
554 return outputLayer->getState().hwc ? (*outputLayer->getState().hwc).hwcCompositionType
555 : Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800556}
557
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800558bool Layer::getClearClientTarget(const sp<const DisplayDevice>& display) const {
559 const auto outputLayer = findOutputLayerForDisplay(display);
560 LOG_FATAL_IF(!outputLayer);
561 return outputLayer->getState().clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800562}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800563
Dan Stozacac35382016-01-27 12:21:06 -0800564bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
565 if (point->getFrameNumber() <= mCurrentFrameNumber) {
566 // Don't bother with a SyncPoint, since we've already latched the
567 // relevant frame
568 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700569 }
Robert Carr2e102c92018-10-23 12:11:15 -0700570 if (isRemovedFromCurrentState()) {
571 return false;
572 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700573
Dan Stozacac35382016-01-27 12:21:06 -0800574 Mutex::Autolock lock(mLocalSyncPointMutex);
575 mLocalSyncPoints.push_back(point);
576 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700577}
578
Mathias Agopian13127d82013-03-05 17:47:11 -0800579// ----------------------------------------------------------------------------
580// local state
581// ----------------------------------------------------------------------------
582
David Sodman41fdfc92017-11-06 16:09:56 -0800583bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800584 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700585 return (s.flags & layer_state_t::eLayerSecure);
586}
587
Mathias Agopian13127d82013-03-05 17:47:11 -0800588void Layer::setVisibleRegion(const Region& visibleRegion) {
589 // always called from main thread
590 this->visibleRegion = visibleRegion;
591}
592
593void Layer::setCoveredRegion(const Region& coveredRegion) {
594 // always called from main thread
595 this->coveredRegion = coveredRegion;
596}
597
David Sodman41fdfc92017-11-06 16:09:56 -0800598void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800599 // always called from main thread
600 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
601}
602
Robert Carre5f4f692018-01-12 13:12:28 -0800603void Layer::clearVisibilityRegions() {
604 visibleRegion.clear();
605 visibleNonTransparentRegion.clear();
606 coveredRegion.clear();
607}
608
Mathias Agopian13127d82013-03-05 17:47:11 -0800609// ----------------------------------------------------------------------------
610// transaction
611// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800612
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800613void Layer::pushPendingState() {
614 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700615 return;
616 }
Alec Mourie60041e2019-06-14 18:59:51 -0700617 ATRACE_CALL();
Dan Stoza7dde5992015-05-22 09:51:44 -0700618
Dan Stoza7dde5992015-05-22 09:51:44 -0700619 // If this transaction is waiting on the receipt of a frame, generate a sync
620 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700621 // We don't allow installing sync points after we are removed from the current state
622 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800623 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
624 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800625 if (barrierLayer == nullptr) {
626 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700627 // If we can't promote the layer we are intended to wait on,
628 // then it is expired or otherwise invalid. Allow this transaction
629 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800630 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800631 } else {
chaviw43cb3cb2019-05-31 15:23:41 -0700632 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy, this);
Robert Carr0d480722017-01-10 16:42:54 -0800633 if (barrierLayer->addSyncPoint(syncPoint)) {
Alec Mourie60041e2019-06-14 18:59:51 -0700634 std::stringstream ss;
635 ss << "Adding sync point " << mCurrentState.frameNumber_legacy;
636 ATRACE_NAME(ss.str().c_str());
Dan Stozacac35382016-01-27 12:21:06 -0800637 mRemoteSyncPoints.push_back(std::move(syncPoint));
638 } else {
639 // We already missed the frame we're supposed to synchronize
640 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800641 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800642 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700643 }
644
Dan Stoza7dde5992015-05-22 09:51:44 -0700645 // Wake us up to check if the frame has been received
646 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700647 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700648 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800649 mPendingStates.push_back(mCurrentState);
650 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700651}
652
Pablo Ceballos05289c22016-04-14 15:49:55 -0700653void Layer::popPendingState(State* stateToCommit) {
Alec Mourie60041e2019-06-14 18:59:51 -0700654 ATRACE_CALL();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800655 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700656
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800657 mPendingStates.removeAt(0);
658 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700659}
660
Pablo Ceballos05289c22016-04-14 15:49:55 -0700661bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700662 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800663 while (!mPendingStates.empty()) {
664 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700665 if (mRemoteSyncPoints.empty()) {
666 // If we don't have a sync point for this, apply it anyway. It
667 // will be visually wrong, but it should keep us from getting
668 // into too much trouble.
669 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700670 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700671 stateUpdateAvailable = true;
672 continue;
673 }
674
Marissa Wallf58c14b2018-07-24 10:50:43 -0700675 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800676 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800677 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800678
679 // Signal our end of the sync point and then dispose of it
680 mRemoteSyncPoints.front()->setTransactionApplied();
681 mRemoteSyncPoints.pop_front();
682 continue;
683 }
684
Dan Stoza7dde5992015-05-22 09:51:44 -0700685 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
Alec Mourie60041e2019-06-14 18:59:51 -0700686 ATRACE_NAME("frameIsAvailable");
Dan Stoza7dde5992015-05-22 09:51:44 -0700687 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700688 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700689 stateUpdateAvailable = true;
690
691 // Signal our end of the sync point and then dispose of it
692 mRemoteSyncPoints.front()->setTransactionApplied();
693 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800694 } else {
Alec Mourie60041e2019-06-14 18:59:51 -0700695 ATRACE_NAME("!frameIsAvailable");
Dan Stoza792e5292016-02-11 11:43:58 -0800696 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700697 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700698 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700699 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700700 stateUpdateAvailable = true;
701 }
702 }
703
704 // If we still have pending updates, wake SurfaceFlinger back up and point
705 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800706 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700707 setTransactionFlags(eTransactionNeeded);
708 mFlinger->setTransactionFlags(eTraversalNeeded);
709 }
710
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800711 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700712 return stateUpdateAvailable;
713}
714
Marissa Wall61c58622018-07-18 10:12:20 -0700715uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000716 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800717
Marissa Wall61c58622018-07-18 10:12:20 -0700718 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
719 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700720
David Sodmaneb085e02017-10-05 18:49:04 -0700721 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700722 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000723 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800724 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
725 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
726 " requested={ wh={%4u,%4u} }}\n"
727 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
728 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700729 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700730 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
731 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
732 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
733 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
734 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700735 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
736 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
737 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800738 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700739
Robert Carre392b552017-09-19 12:16:05 -0700740 // Don't let Layer::doTransaction update the drawing state
741 // if we have a pending resize, unless we are in fixed-size mode.
742 // the drawing state will be updated only once we receive a buffer
743 // with the correct size.
744 //
745 // In particular, we want to make sure the clip (which is part
746 // of the geometry state) is latched together with the size but is
747 // latched immediately when no resizing is involved.
748 //
749 // If a sideband stream is attached, however, we want to skip this
750 // optimization so that transactions aren't missed when a buffer
751 // never arrives
752 //
753 // In the case that we don't have a buffer we ignore other factors
754 // and avoid entering the resizePending state. At a high level the
755 // resizePending state is to avoid applying the state of the new buffer
756 // to the old buffer. However in the state where we don't have an old buffer
757 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700758 const bool resizePending =
759 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
760 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
Lloyd Pique0b785d82018-12-04 17:25:27 -0800761 (mActiveBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700762 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800763 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700764 flags |= eDontUpdateGeometryState;
765 }
766 }
767
Robert Carr7bf247e2017-05-18 14:02:49 -0700768 // Here we apply various requested geometry states, depending on our
769 // latching configuration. See Layer.h for a detailed discussion of
770 // how geometry latching is controlled.
771 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000772 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700773
774 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
775 // mode, which causes attributes which normally latch regardless of scaling mode,
776 // to be delayed. We copy the requested state to the active state making sure
777 // to respect these rules (again see Layer.h for a detailed discussion).
778 //
779 // There is an awkward asymmetry in the handling of the crop states in the position
780 // states, as can be seen below. Largely this arises from position and transform
781 // being stored in the same data structure while having different latching rules.
782 // b/38182305
783 //
Marissa Wall61c58622018-07-18 10:12:20 -0700784 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700785 // applyPendingStates in the presence of deferred transactions.
786 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -0700787 float tx = stateToCommit->active_legacy.transform.tx();
788 float ty = stateToCommit->active_legacy.transform.ty();
789 stateToCommit->active_legacy = stateToCommit->requested_legacy;
790 stateToCommit->active_legacy.transform.set(tx, ty);
791 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700792 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700793 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -0700794 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700795 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800796 }
797
Marissa Wall61c58622018-07-18 10:12:20 -0700798 return flags;
799}
800
801uint32_t Layer::doTransaction(uint32_t flags) {
802 ATRACE_CALL();
803
chaviw5aedec92018-10-22 10:40:38 -0700804 if (mLayerDetached) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800805 return flags;
806 }
807
808 if (mChildrenChanged) {
809 flags |= eVisibleRegion;
810 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -0700811 }
812
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800813 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +0000814 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -0700815 if (!applyPendingStates(&c)) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800816 return flags;
Marissa Wall61c58622018-07-18 10:12:20 -0700817 }
818
819 flags = doTransactionResize(flags, &c);
820
Alec Mourib416efd2018-09-06 21:01:59 +0000821 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700822
823 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800824 // invalidate and recompute the visible regions if needed
825 flags |= Layer::eVisibleRegion;
826 }
827
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700828 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800829 // invalidate and recompute the visible regions if needed
830 flags |= eVisibleRegion;
831 this->contentDirty = true;
832
833 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -0700834 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -0700835 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -0800836 }
837
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800838 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -0700839 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800840 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -0700841 }
842
Mathias Agopian13127d82013-03-05 17:47:11 -0800843 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -0700844 commitTransaction(c);
Vishnu Nair8406fd72019-07-30 11:29:31 -0700845 mPendingStatesSnapshot = mPendingStates;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800846 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -0800847 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800848}
849
Pablo Ceballos05289c22016-04-14 15:49:55 -0700850void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800851 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700852}
853
Mathias Agopian13127d82013-03-05 17:47:11 -0800854uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800855 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800856}
857
858uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800859 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -0800860}
861
Robert Carr82364e32016-05-15 11:27:47 -0700862bool Layer::setPosition(float x, float y, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800863 if (mCurrentState.requested_legacy.transform.tx() == x &&
864 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -0800865 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800866 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -0700867
868 // We update the requested and active position simultaneously because
869 // we want to apply the position portion of the transform matrix immediately,
870 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800871 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -0700872 if (immediate && !mFreezeGeometryUpdates) {
873 // Here we directly update the active state
874 // unlike other setters, because we store it within
875 // the transform, but use different latching rules.
876 // b/38182305
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800877 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -0700878 }
Robert Carr7bf247e2017-05-18 14:02:49 -0700879 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -0700880
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800881 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800882 setTransactionFlags(eTransactionNeeded);
883 return true;
884}
Robert Carr82364e32016-05-15 11:27:47 -0700885
Robert Carr1f0a16a2016-10-24 16:27:39 -0700886bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
887 ssize_t idx = mCurrentChildren.indexOf(childLayer);
888 if (idx < 0) {
889 return false;
890 }
891 if (childLayer->setLayer(z)) {
892 mCurrentChildren.removeAt(idx);
893 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800894 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700895 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800896 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700897}
898
Robert Carr503c7042017-09-27 15:06:08 -0700899bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
900 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
901 ssize_t idx = mCurrentChildren.indexOf(childLayer);
902 if (idx < 0) {
903 return false;
904 }
905 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
906 mCurrentChildren.removeAt(idx);
907 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800908 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700909 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800910 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700911}
912
Robert Carrae060832016-11-28 10:51:00 -0800913bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800914 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
915 mCurrentState.sequence++;
916 mCurrentState.z = z;
917 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700918
919 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800920 if (mCurrentState.zOrderRelativeOf != nullptr) {
921 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700922 if (strongRelative != nullptr) {
923 strongRelative->removeZOrderRelative(this);
924 }
chaviw606e5cf2019-03-01 10:12:10 -0800925 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -0700926 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800927 setTransactionFlags(eTransactionNeeded);
928 return true;
929}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700930
Robert Carrdb66e622017-04-10 16:55:57 -0700931void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800932 mCurrentState.zOrderRelatives.remove(relative);
933 mCurrentState.sequence++;
934 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700935 setTransactionFlags(eTransactionNeeded);
936}
937
938void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800939 mCurrentState.zOrderRelatives.add(relative);
940 mCurrentState.modified = true;
941 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -0700942 setTransactionFlags(eTransactionNeeded);
943}
944
chaviw606e5cf2019-03-01 10:12:10 -0800945void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
946 mCurrentState.zOrderRelativeOf = relativeOf;
947 mCurrentState.sequence++;
948 mCurrentState.modified = true;
949 setTransactionFlags(eTransactionNeeded);
950}
951
Robert Carr503d2bd2017-12-04 15:49:47 -0800952bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -0700953 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
954 if (handle == nullptr) {
955 return false;
956 }
957 sp<Layer> relative = handle->owner.promote();
958 if (relative == nullptr) {
959 return false;
960 }
961
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800962 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
963 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -0800964 return false;
965 }
966
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800967 mCurrentState.sequence++;
968 mCurrentState.modified = true;
969 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -0700970
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800971 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -0700972 if (oldZOrderRelativeOf != nullptr) {
973 oldZOrderRelativeOf->removeZOrderRelative(this);
974 }
chaviw606e5cf2019-03-01 10:12:10 -0800975 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -0700976 relative->addZOrderRelative(this);
977
978 setTransactionFlags(eTransactionNeeded);
979
980 return true;
981}
982
Mathias Agopian13127d82013-03-05 17:47:11 -0800983bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800984 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -0700985 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800986 mCurrentState.requested_legacy.w = w;
987 mCurrentState.requested_legacy.h = h;
988 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800989 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -0700990
991 // record the new size, from this point on, when the client request
992 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800993 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -0800994 return true;
995}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800996bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800997 if (mCurrentState.color.a == alpha) return false;
998 mCurrentState.sequence++;
999 mCurrentState.color.a = alpha;
1000 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001001 setTransactionFlags(eTransactionNeeded);
1002 return true;
1003}
chaviw13fdc492017-06-27 12:40:18 -07001004
Valerie Haudd0b7572019-01-29 14:59:27 -08001005bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1006 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001007 return false;
Valerie Hauaa194562019-02-05 16:21:38 -08001008 }
1009 mCurrentState.sequence++;
1010 mCurrentState.modified = true;
1011 setTransactionFlags(eTransactionNeeded);
1012
1013 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001014 // create background color layer if one does not yet exist
1015 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
1016 const String8& name = mName + "BackgroundColorLayer";
Evan Roskya1f1e152019-01-24 16:17:46 -08001017 mCurrentState.bgColorLayer = new ColorLayer(
1018 LayerCreationArgs(mFlinger.get(), nullptr, name, 0, 0, flags, LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -07001019
Valerie Haudd0b7572019-01-29 14:59:27 -08001020 // add to child list
1021 addChild(mCurrentState.bgColorLayer);
1022 mFlinger->mLayersAdded = true;
1023 // set up SF to handle added color layer
1024 if (isRemovedFromCurrentState()) {
1025 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1026 }
1027 mFlinger->setTransactionFlags(eTransactionNeeded);
1028 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1029 mCurrentState.bgColorLayer->reparent(nullptr);
1030 mCurrentState.bgColorLayer = nullptr;
1031 return true;
1032 }
1033
1034 mCurrentState.bgColorLayer->setColor(color);
1035 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1036 mCurrentState.bgColorLayer->setAlpha(alpha);
1037 mCurrentState.bgColorLayer->setDataspace(dataspace);
1038
chaviw13fdc492017-06-27 12:40:18 -07001039 return true;
1040}
1041
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001042bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001043 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001044
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001045 mCurrentState.sequence++;
1046 mCurrentState.cornerRadius = cornerRadius;
1047 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001048 setTransactionFlags(eTransactionNeeded);
1049 return true;
1050}
1051
Robert Carrd4ae7f32018-06-07 16:10:57 -07001052bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1053 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001054 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001055 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1056
1057 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1058 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1059 return false;
1060 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001061 mCurrentState.sequence++;
1062 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1063 matrix.dsdy);
1064 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001065 setTransactionFlags(eTransactionNeeded);
1066 return true;
1067}
Marissa Wall61c58622018-07-18 10:12:20 -07001068
Mathias Agopian13127d82013-03-05 17:47:11 -08001069bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001070 mCurrentState.requestedTransparentRegion_legacy = transparent;
1071 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001072 setTransactionFlags(eTransactionNeeded);
1073 return true;
1074}
1075bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001076 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1077 if (mCurrentState.flags == newFlags) return false;
1078 mCurrentState.sequence++;
1079 mCurrentState.flags = newFlags;
1080 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001081 setTransactionFlags(eTransactionNeeded);
1082 return true;
1083}
Robert Carr99e27f02016-06-16 15:18:02 -07001084
Marissa Wallf58c14b2018-07-24 10:50:43 -07001085bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001086 if (mCurrentState.requestedCrop_legacy == crop) return false;
1087 mCurrentState.sequence++;
1088 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001089 if (immediate && !mFreezeGeometryUpdates) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001090 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001091 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001092 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1093
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001094 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001095 setTransactionFlags(eTransactionNeeded);
1096 return true;
1097}
Robert Carr8d5227b2017-03-16 15:41:03 -07001098
Robert Carrc3574f72016-03-24 12:19:32 -07001099bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001100 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001101 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001102 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001103 return true;
1104}
1105
Evan Roskyef876c92019-01-25 17:46:06 -08001106bool Layer::setMetadata(const LayerMetadata& data) {
1107 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001108 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001109 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001110 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001111 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001112}
1113
Mathias Agopian13127d82013-03-05 17:47:11 -08001114bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001115 if (mCurrentState.layerStack == layerStack) return false;
1116 mCurrentState.sequence++;
1117 mCurrentState.layerStack = layerStack;
1118 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001119 setTransactionFlags(eTransactionNeeded);
1120 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001121}
1122
Peiyong Linc502cb72019-03-01 15:00:23 -08001123bool Layer::setColorSpaceAgnostic(const bool agnostic) {
1124 if (mCurrentState.colorSpaceAgnostic == agnostic) {
1125 return false;
1126 }
1127 mCurrentState.sequence++;
1128 mCurrentState.colorSpaceAgnostic = agnostic;
1129 mCurrentState.modified = true;
1130 setTransactionFlags(eTransactionNeeded);
1131 return true;
1132}
1133
Robert Carr1f0a16a2016-10-24 16:27:39 -07001134uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001135 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001136 if (p == nullptr) {
1137 return getDrawingState().layerStack;
1138 }
1139 return p->getLayerStack();
1140}
1141
Marissa Wallf58c14b2018-07-24 10:50:43 -07001142void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Alec Mourie60041e2019-06-14 18:59:51 -07001143 ATRACE_CALL();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001144 mCurrentState.barrierLayer_legacy = barrierLayer;
1145 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001146 // We don't set eTransactionNeeded, because just receiving a deferral
1147 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001148 mCurrentState.modified = true;
1149 pushPendingState();
1150 mCurrentState.barrierLayer_legacy = nullptr;
1151 mCurrentState.frameNumber_legacy = 0;
1152 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001153}
1154
Marissa Wallf58c14b2018-07-24 10:50:43 -07001155void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001156 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001157 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001158}
1159
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001160// ----------------------------------------------------------------------------
1161// pageflip handling...
1162// ----------------------------------------------------------------------------
1163
Robert Carr1f0a16a2016-10-24 16:27:39 -07001164bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001165 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001166 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001167 if (parent != nullptr && parent->isHiddenByPolicy()) {
1168 return true;
1169 }
Robert Carr1c5481e2019-07-01 14:42:27 -07001170 if (usingRelativeZ(LayerVector::StateSet::Drawing)) {
1171 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
1172 if (zOrderRelativeOf != nullptr) {
1173 if (zOrderRelativeOf->isHiddenByPolicy()) {
1174 return true;
1175 }
1176 }
1177 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001178 return s.flags & layer_state_t::eLayerHidden;
1179}
1180
David Sodman41fdfc92017-11-06 16:09:56 -08001181uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001182 // TODO: should we do something special if mSecure is set?
1183 if (mProtectedByApp) {
1184 // need a hardware-protected path to external video sink
1185 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001186 }
Riley Andrews03414a12014-07-01 14:22:59 -07001187 if (mPotentialCursor) {
1188 usage |= GraphicBuffer::USAGE_CURSOR;
1189 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001190 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001191 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001192}
1193
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001194void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001195 uint32_t orientation = 0;
Vishnu Nair5eb3f062019-04-08 08:21:03 -07001196 // Disable setting transform hint if the debug flag is set.
1197 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001198 // The transform hint is used to improve performance, but we can
1199 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001200 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001201 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001202 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001203 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001204 orientation = 0;
1205 }
1206 }
David Sodmaneb085e02017-10-05 18:49:04 -07001207 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001208}
1209
Mathias Agopian13127d82013-03-05 17:47:11 -08001210// ----------------------------------------------------------------------------
1211// debugging
1212// ----------------------------------------------------------------------------
1213
Marissa Wall61c58622018-07-18 10:12:20 -07001214// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001215LayerDebugInfo Layer::getLayerDebugInfo() const {
1216 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001217 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001218 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001219 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001220 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
chaviw8a01fa42019-08-19 12:39:31 -07001221 info.mType = getType();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001222 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001223 info.mVisibleRegion = visibleRegion;
1224 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001225 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001226 info.mX = ds.active_legacy.transform.tx();
1227 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001228 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001229 info.mWidth = ds.active_legacy.w;
1230 info.mHeight = ds.active_legacy.h;
1231 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001232 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001233 info.mFlags = ds.flags;
1234 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001235 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001236 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1237 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1238 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1239 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001240 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001241 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001242 if (buffer != 0) {
1243 info.mActiveBufferWidth = buffer->getWidth();
1244 info.mActiveBufferHeight = buffer->getHeight();
1245 info.mActiveBufferStride = buffer->getStride();
1246 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001247 } else {
1248 info.mActiveBufferWidth = 0;
1249 info.mActiveBufferHeight = 0;
1250 info.mActiveBufferStride = 0;
1251 info.mActiveBufferFormat = 0;
1252 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001253 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001254 info.mNumQueuedFrames = getQueuedFrameCount();
1255 info.mRefreshPending = isBufferLatched();
1256 info.mIsOpaque = isOpaque(ds);
1257 info.mContentDirty = contentDirty;
1258 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001259}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001260
Yiwei Zhang5434a782018-12-05 18:06:32 -08001261void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001262 result.append("-------------------------------");
1263 result.append("-------------------------------");
1264 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001265 result.append(" Layer name\n");
1266 result.append(" Z | ");
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001267 result.append(" Window Type | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001268 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001269 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001270 result.append(" Disp Frame (LTRB) | ");
1271 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001272 result.append("-------------------------------");
1273 result.append("-------------------------------");
1274 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001275}
1276
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001277void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1278 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1279 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001280 return;
1281 }
1282
Yiwei Zhang5434a782018-12-05 18:06:32 -08001283 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001284 if (mName.length() > 77) {
1285 std::string shortened;
1286 shortened.append(mName.string(), 36);
1287 shortened.append("[...]");
1288 shortened.append(mName.string() + (mName.length() - 36), 36);
Yiwei Zhang5434a782018-12-05 18:06:32 -08001289 name = shortened;
Dan Stozae22aec72016-08-01 13:20:59 -07001290 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001291 name = std::string(mName.string(), mName.size());
Dan Stozae22aec72016-08-01 13:20:59 -07001292 }
1293
Yiwei Zhang5434a782018-12-05 18:06:32 -08001294 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001295
Alec Mourib416efd2018-09-06 21:01:59 +00001296 const State& layerState(getDrawingState());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001297 const auto& compositionState = outputLayer->getState();
1298
Chia-I Wu1e043612018-03-01 09:45:09 -08001299 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001300 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001301 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001302 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001303 }
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001304 StringAppendF(&result, " %10d | ", mWindowType);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001305 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
1306 StringAppendF(&result, "%10s | ",
1307 toString(getCompositionLayer() ? compositionState.bufferTransform
1308 : static_cast<Hwc2::Transform>(0))
1309 .c_str());
1310 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001311 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001312 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001313 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1314 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001315
Yichi Chen6ca35192018-05-29 12:20:43 +08001316 result.append("- - - - - - - - - - - - - - - -");
1317 result.append("- - - - - - - - - - - - - - - -");
1318 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001319}
Dan Stozae22aec72016-08-01 13:20:59 -07001320
Yiwei Zhang5434a782018-12-05 18:06:32 -08001321void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001322 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001323}
1324
Svetoslavd85084b2014-03-20 10:28:31 -07001325void Layer::clearFrameStats() {
1326 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001327}
1328
Jamie Gennis6547ff42013-07-16 20:12:42 -07001329void Layer::logFrameStats() {
1330 mFrameTracker.logAndResetStats(mName);
1331}
1332
Svetoslavd85084b2014-03-20 10:28:31 -07001333void Layer::getFrameStats(FrameStats* outStats) const {
1334 mFrameTracker.getStats(outStats);
1335}
1336
Yiwei Zhang5434a782018-12-05 18:06:32 -08001337void Layer::dumpFrameEvents(std::string& result) {
chaviw8a01fa42019-08-19 12:39:31 -07001338 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().string(), getType(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001339 Mutex::Autolock lock(mFrameEventHistoryMutex);
1340 mFrameEventHistory.checkFencesForCompletion();
1341 mFrameEventHistory.dump(result);
1342}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001343
Vishnu Nair0f085c62019-08-30 08:49:12 -07001344void Layer::dumpCallingUidPid(std::string& result) const {
1345 StringAppendF(&result, "Layer %s (%s) pid:%d uid:%d\n", getName().string(), getType(),
1346 mCallingPid, mCallingUid);
1347}
1348
Brian Anderson5ea5e592016-12-01 16:54:33 -08001349void Layer::onDisconnect() {
1350 Mutex::Autolock lock(mFrameEventHistoryMutex);
1351 mFrameEventHistory.onDisconnect();
Mikael Pessa90092f42019-08-26 17:22:04 -07001352 const int32_t layerID = getSequence();
1353 mFlinger->mTimeStats->onDestroy(layerID);
1354 mFlinger->mFrameTracer->onDestroy(layerID);
Brian Anderson5ea5e592016-12-01 16:54:33 -08001355}
1356
Brian Anderson3890c392016-07-25 12:48:08 -07001357void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001358 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001359 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001360 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1361 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001362 }
1363
Brian Andersond6927fb2016-07-23 23:37:30 -07001364 Mutex::Autolock lock(mFrameEventHistoryMutex);
1365 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001366 // If there are any unsignaled fences in the aquire timeline at this
1367 // point, the previously queued frame hasn't been latched yet. Go ahead
1368 // and try to get the signal time here so the syscall is taken out of
1369 // the main thread's critical path.
1370 mAcquireTimeline.updateSignalTimes();
1371 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001372 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001373 mFrameEventHistory.addQueue(*newTimestamps);
1374 }
1375
Brian Anderson3890c392016-07-25 12:48:08 -07001376 if (outDelta) {
1377 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001378 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001379}
Dan Stozae77c7662016-05-13 11:37:28 -07001380
Chia-I Wu98f1c102017-05-30 14:54:08 -07001381size_t Layer::getChildrenCount() const {
1382 size_t count = 0;
1383 for (const sp<Layer>& child : mCurrentChildren) {
1384 count += 1 + child->getChildrenCount();
1385 }
1386 return count;
1387}
1388
Robert Carr1f0a16a2016-10-24 16:27:39 -07001389void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001390 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001391 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001392
Robert Carr1f0a16a2016-10-24 16:27:39 -07001393 mCurrentChildren.add(layer);
1394 layer->setParent(this);
1395}
1396
1397ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001398 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001399 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001400
Robert Carr1323c952019-01-28 18:13:27 -08001401 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001402 return mCurrentChildren.remove(layer);
1403}
1404
Robert Carr1db73f62016-12-21 12:58:51 -08001405bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1406 sp<Handle> handle = nullptr;
1407 sp<Layer> newParent = nullptr;
1408 if (newParentHandle == nullptr) {
1409 return false;
1410 }
1411 handle = static_cast<Handle*>(newParentHandle.get());
1412 newParent = handle->owner.promote();
1413 if (newParent == nullptr) {
1414 ALOGE("Unable to promote Layer handle");
1415 return false;
1416 }
1417
chaviw5aedec92018-10-22 10:40:38 -07001418 if (attachChildren()) {
1419 setTransactionFlags(eTransactionNeeded);
1420 }
Robert Carr1db73f62016-12-21 12:58:51 -08001421 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001422 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001423 }
1424 mCurrentChildren.clear();
1425
1426 return true;
1427}
1428
Robert Carr15eae092018-03-23 13:43:53 -07001429void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001430 for (const sp<Layer>& child : mDrawingChildren) {
1431 child->mDrawingParent = newParent;
Vishnu Nairc652ff82019-03-15 12:48:54 -07001432 child->computeBounds(newParent->mBounds,
1433 newParent->getTransformWithScale(
1434 newParent->getBufferScaleTransform()));
Robert Carr578038f2018-03-09 12:25:24 -08001435 }
1436}
1437
chaviwf1961f72017-09-18 16:41:07 -07001438bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001439 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001440
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001441 // While layers are detached, we allow most operations
1442 // and simply halt performing the actual transaction. However
1443 // for reparent != null we would enter the mRemovedFromCurrentState
1444 // state, regardless of whether doTransaction was called, and
1445 // so we need to prevent the update here.
1446 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001447 return false;
1448 }
1449
Robert Carr54cf5b12019-01-25 14:02:28 -08001450 sp<Layer> newParent;
1451 if (newParentHandle != nullptr) {
1452 auto handle = static_cast<Handle*>(newParentHandle.get());
1453 newParent = handle->owner.promote();
1454 if (newParent == nullptr) {
1455 ALOGE("Unable to promote Layer handle");
1456 return false;
1457 }
1458 if (newParent == this) {
1459 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1460 return false;
1461 }
1462 }
1463
chaviwf1961f72017-09-18 16:41:07 -07001464 sp<Layer> parent = getParent();
1465 if (parent != nullptr) {
1466 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001467 }
1468
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001469 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001470 newParent->addChild(this);
1471 if (!newParent->isRemovedFromCurrentState()) {
1472 addToCurrentState();
1473 } else {
1474 onRemovedFromCurrentState();
1475 }
1476
1477 if (mLayerDetached) {
1478 mLayerDetached = false;
1479 callSetTransactionFlags = true;
1480 }
1481 } else {
1482 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001483 }
1484
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001485 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001486 setTransactionFlags(eTransactionNeeded);
1487 }
chaviw06178942017-07-27 10:25:59 -07001488 return true;
1489}
1490
Robert Carr9524cb32017-02-13 11:32:32 -08001491bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001492 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001493 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001494 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001495 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001496 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001497 child->detachChildren();
chaviw43cb3cb2019-05-31 15:23:41 -07001498 child->removeRemoteSyncPoints();
Robert Carr9524cb32017-02-13 11:32:32 -08001499 }
Robert Carr7f619b22017-11-06 12:56:35 -08001500 }
Robert Carr9524cb32017-02-13 11:32:32 -08001501
1502 return true;
1503}
1504
chaviw5aedec92018-10-22 10:40:38 -07001505bool Layer::attachChildren() {
1506 bool changed = false;
1507 for (const sp<Layer>& child : mCurrentChildren) {
1508 sp<Client> parentClient = mClientRef.promote();
1509 sp<Client> client(child->mClientRef.promote());
1510 if (client != nullptr && parentClient != client) {
1511 if (child->mLayerDetached) {
1512 child->mLayerDetached = false;
1513 changed = true;
1514 }
1515 changed |= child->attachChildren();
1516 }
1517 }
1518
1519 return changed;
1520}
1521
Peiyong Lind3788632018-09-18 16:01:31 -07001522bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001523 static const mat4 identityMatrix = mat4();
1524
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001525 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001526 return false;
1527 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001528 ++mCurrentState.sequence;
1529 mCurrentState.colorTransform = matrix;
1530 mCurrentState.hasColorTransform = matrix != identityMatrix;
1531 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001532 setTransactionFlags(eTransactionNeeded);
1533 return true;
1534}
1535
chaviwf66724d2018-11-28 16:35:21 -08001536mat4 Layer::getColorTransform() const {
1537 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1538 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1539 colorTransform = parent->getColorTransform() * colorTransform;
1540 }
1541 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001542}
1543
1544bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001545 bool hasColorTransform = getDrawingState().hasColorTransform;
1546 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1547 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1548 }
1549 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001550}
1551
Chia-I Wu11481472018-05-04 10:43:19 -07001552bool Layer::isLegacyDataSpace() const {
1553 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001554 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001555 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001556}
1557
Robert Carr1f0a16a2016-10-24 16:27:39 -07001558void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001559 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001560}
1561
Robert Carr1f0a16a2016-10-24 16:27:39 -07001562int32_t Layer::getZ() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001563 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001564}
1565
Robert Carr1c5481e2019-07-01 14:42:27 -07001566bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) const {
Robert Carr29abff82017-12-04 13:51:20 -08001567 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001568 const State& state = useDrawing ? mDrawingState : mCurrentState;
Robert Carr29abff82017-12-04 13:51:20 -08001569 return state.zOrderRelativeOf != nullptr;
1570}
1571
David Sodman41fdfc92017-11-06 16:09:56 -08001572__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001573 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001574 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1575 "makeTraversalList received invalid stateSet");
1576 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1577 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001578 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001579
Robert Carr29abff82017-12-04 13:51:20 -08001580 if (state.zOrderRelatives.size() == 0) {
1581 *outSkipRelativeZUsers = true;
1582 return children;
1583 }
1584
chaviwfd462612018-05-31 16:11:27 -07001585 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001586 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001587 sp<Layer> strongRelative = weakRelative.promote();
1588 if (strongRelative != nullptr) {
1589 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001590 }
1591 }
1592
Dan Stoza412903f2017-04-27 13:42:17 -07001593 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001594 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
Robert Carr503c7042017-09-27 15:06:08 -07001595 if (childState.zOrderRelativeOf != nullptr) {
1596 continue;
1597 }
Robert Carrdb66e622017-04-10 16:55:57 -07001598 traverse.add(child);
1599 }
1600
1601 return traverse;
1602}
1603
Robert Carr1f0a16a2016-10-24 16:27:39 -07001604/**
Robert Carrdb66e622017-04-10 16:55:57 -07001605 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001606 */
Dan Stoza412903f2017-04-27 13:42:17 -07001607void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001608 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1609 // produce a new list for traversing, including our relatives, and not including our children
1610 // who are relatives of another surface. In the case that there are no relative Z,
1611 // makeTraversalList returns our children directly to avoid significant overhead.
1612 // However in this case we need to take the responsibility for filtering children which
1613 // are relatives of another surface here.
1614 bool skipRelativeZUsers = false;
1615 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001616
Robert Carr1f0a16a2016-10-24 16:27:39 -07001617 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001618 for (; i < list.size(); i++) {
1619 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001620 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1621 continue;
1622 }
1623
Robert Carrdb66e622017-04-10 16:55:57 -07001624 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001625 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001626 }
Dan Stoza412903f2017-04-27 13:42:17 -07001627 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001628 }
Robert Carr29abff82017-12-04 13:51:20 -08001629
Dan Stoza412903f2017-04-27 13:42:17 -07001630 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001631 for (; i < list.size(); i++) {
1632 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001633
Robert Carr29abff82017-12-04 13:51:20 -08001634 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1635 continue;
1636 }
Dan Stoza412903f2017-04-27 13:42:17 -07001637 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001638 }
1639}
1640
1641/**
Robert Carrdb66e622017-04-10 16:55:57 -07001642 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001643 */
Dan Stoza412903f2017-04-27 13:42:17 -07001644void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1645 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001646 // See traverseInZOrder for documentation.
1647 bool skipRelativeZUsers = false;
1648 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001649
Robert Carr1f0a16a2016-10-24 16:27:39 -07001650 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001651 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001652 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001653
1654 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1655 continue;
1656 }
1657
Robert Carrdb66e622017-04-10 16:55:57 -07001658 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001659 break;
1660 }
Dan Stoza412903f2017-04-27 13:42:17 -07001661 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001662 }
Dan Stoza412903f2017-04-27 13:42:17 -07001663 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001664 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001665 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001666
1667 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1668 continue;
1669 }
1670
Dan Stoza412903f2017-04-27 13:42:17 -07001671 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001672 }
1673}
1674
chaviw4b129c22018-04-09 16:19:43 -07001675LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1676 const std::vector<Layer*>& layersInTree) {
1677 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1678 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001679 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1680 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001681 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001682
chaviwfd462612018-05-31 16:11:27 -07001683 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001684 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1685 sp<Layer> strongRelative = weakRelative.promote();
1686 // Only add relative layers that are also descendents of the top most parent of the tree.
1687 // If a relative layer is not a descendent, then it should be ignored.
1688 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1689 traverse.add(strongRelative);
1690 }
1691 }
1692
1693 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001694 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001695 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1696 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1697 // the child here since it won't be added later as a relative.
1698 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1699 childState.zOrderRelativeOf.promote().get())) {
1700 continue;
1701 }
1702 traverse.add(child);
1703 }
1704
1705 return traverse;
1706}
1707
1708void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1709 LayerVector::StateSet stateSet,
1710 const LayerVector::Visitor& visitor) {
1711 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001712
1713 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001714 for (; i < list.size(); i++) {
1715 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001716 if (relative->getZ() >= 0) {
1717 break;
1718 }
chaviw4b129c22018-04-09 16:19:43 -07001719 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001720 }
chaviw4b129c22018-04-09 16:19:43 -07001721
chaviwa76b2712017-09-20 12:02:26 -07001722 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001723 for (; i < list.size(); i++) {
1724 const auto& relative = list[i];
1725 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001726 }
1727}
1728
chaviw4b129c22018-04-09 16:19:43 -07001729std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1730 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1731 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1732
1733 std::vector<Layer*> layersInTree = {this};
1734 for (size_t i = 0; i < children.size(); i++) {
1735 const auto& child = children[i];
1736 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1737 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1738 }
1739
1740 return layersInTree;
1741}
1742
1743void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1744 const LayerVector::Visitor& visitor) {
1745 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1746 std::sort(layersInTree.begin(), layersInTree.end());
1747 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1748}
1749
Peiyong Linefefaac2018-08-17 12:27:51 -07001750ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001751 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001752}
1753
chaviw13fdc492017-06-27 12:40:18 -07001754half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001755 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001756
chaviw13fdc492017-06-27 12:40:18 -07001757 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1758 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001759}
Robert Carr6452f122017-03-21 10:41:29 -07001760
chaviw13fdc492017-06-27 12:40:18 -07001761half4 Layer::getColor() const {
1762 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001763 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001764}
Robert Carr6452f122017-03-21 10:41:29 -07001765
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001766Layer::RoundedCornerState Layer::getRoundedCornerState() const {
1767 const auto& p = mDrawingParent.promote();
1768 if (p != nullptr) {
Peiyong Lin27016a92019-03-29 17:36:08 +00001769 RoundedCornerState parentState = p->getRoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001770 if (parentState.radius > 0) {
1771 ui::Transform t = getActiveTransform(getDrawingState());
1772 t = t.inverse();
1773 parentState.cropRect = t.transform(parentState.cropRect);
1774 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1775 // but a transform matrix can define horizontal and vertical scales.
1776 // Let's take the average between both of them and pass into the shader, practically we
1777 // never do this type of transformation on windows anyway.
1778 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
1779 return parentState;
1780 }
1781 }
1782 const float radius = getDrawingState().cornerRadius;
Peiyong Linb9ff23e2019-04-08 11:04:59 -07001783 return radius > 0 && getCrop(getDrawingState()).isValid()
1784 ? RoundedCornerState(getCrop(getDrawingState()).toFloatRect(), radius)
1785 : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001786}
1787
Robert Carr1f0a16a2016-10-24 16:27:39 -07001788void Layer::commitChildList() {
1789 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1790 const auto& child = mCurrentChildren[i];
1791 child->commitChildList();
1792 }
1793 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001794 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001795}
1796
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001797static wp<Layer> extractLayerFromBinder(const wp<IBinder>& weakBinderHandle) {
1798 if (weakBinderHandle == nullptr) {
1799 return nullptr;
1800 }
1801 sp<IBinder> binderHandle = weakBinderHandle.promote();
1802 if (binderHandle == nullptr) {
1803 return nullptr;
1804 }
1805 sp<Layer::Handle> handle = static_cast<Layer::Handle*>(binderHandle.get());
1806 if (handle == nullptr) {
1807 return nullptr;
1808 }
1809 return handle->owner;
1810}
1811
Robert Carr720e5062018-07-30 17:45:14 -07001812void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001813 mCurrentState.inputInfo = info;
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001814 mCurrentState.touchableRegionCrop = extractLayerFromBinder(info.touchableRegionCropHandle);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001815 mCurrentState.modified = true;
1816 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07001817 setTransactionFlags(eTransactionNeeded);
1818}
1819
Vishnu Nair8406fd72019-07-30 11:29:31 -07001820void Layer::writeToProtoDrawingState(LayerProto* layerInfo, uint32_t traceFlags) const {
1821 ui::Transform transform = getTransform();
1822
1823 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
1824 for (const auto& pendingState : mPendingStatesSnapshot) {
1825 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
1826 if (barrierLayer != nullptr) {
1827 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
1828 barrierLayerProto->set_id(barrierLayer->sequence);
1829 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
1830 }
1831 }
1832
1833 auto buffer = mActiveBuffer;
1834 if (buffer != nullptr) {
1835 LayerProtoHelper::writeToProto(buffer,
1836 [&]() { return layerInfo->mutable_active_buffer(); });
1837 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
1838 layerInfo->mutable_buffer_transform());
1839 }
1840 layerInfo->set_invalidate(contentDirty);
1841 layerInfo->set_is_protected(isProtected());
1842 layerInfo->set_dataspace(
1843 dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
1844 layerInfo->set_queued_frames(getQueuedFrameCount());
1845 layerInfo->set_refresh_pending(isBufferLatched());
1846 layerInfo->set_curr_frame(mCurrentFrameNumber);
1847 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
1848
1849 layerInfo->set_corner_radius(getRoundedCornerState().radius);
1850 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1851 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
1852 [&]() { return layerInfo->mutable_position(); });
1853 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
1854 LayerProtoHelper::writeToProto(visibleRegion,
1855 [&]() { return layerInfo->mutable_visible_region(); });
1856 LayerProtoHelper::writeToProto(surfaceDamageRegion,
1857 [&]() { return layerInfo->mutable_damage_region(); });
1858 }
1859
1860 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
1861 LayerProtoHelper::writeToProto(mSourceBounds,
1862 [&]() { return layerInfo->mutable_source_bounds(); });
1863 LayerProtoHelper::writeToProto(mScreenBounds,
1864 [&]() { return layerInfo->mutable_screen_bounds(); });
1865 }
1866}
1867
1868void Layer::writeToProtoCommonState(LayerProto* layerInfo, LayerVector::StateSet stateSet,
1869 uint32_t traceFlags) const {
chaviw1d044282017-09-27 12:19:28 -07001870 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1871 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001872 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07001873
Peiyong Linefefaac2018-08-17 12:27:51 -07001874 ui::Transform requestedTransform = state.active_legacy.transform;
chaviw1d044282017-09-27 12:19:28 -07001875
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001876 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
1877 layerInfo->set_id(sequence);
1878 layerInfo->set_name(getName().c_str());
chaviw8a01fa42019-08-19 12:39:31 -07001879 layerInfo->set_type(getType());
chaviw1d044282017-09-27 12:19:28 -07001880
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001881 for (const auto& child : children) {
1882 layerInfo->add_children(child->sequence);
chaviw1d044282017-09-27 12:19:28 -07001883 }
chaviw1d044282017-09-27 12:19:28 -07001884
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001885 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1886 sp<Layer> strongRelative = weakRelative.promote();
1887 if (strongRelative != nullptr) {
1888 layerInfo->add_relatives(strongRelative->sequence);
1889 }
chaviwadc40c22018-07-10 16:57:27 -07001890 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001891
1892 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
1893 [&]() { return layerInfo->mutable_transparent_region(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001894
1895 layerInfo->set_layer_stack(getLayerStack());
1896 layerInfo->set_z(state.z);
1897
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001898 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(),
1899 [&]() {
1900 return layerInfo->mutable_requested_position();
1901 });
1902
1903 LayerProtoHelper::writeSizeToProto(state.active_legacy.w, state.active_legacy.h,
1904 [&]() { return layerInfo->mutable_size(); });
1905
1906 LayerProtoHelper::writeToProto(state.crop_legacy,
1907 [&]() { return layerInfo->mutable_crop(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001908
1909 layerInfo->set_is_opaque(isOpaque(state));
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001910
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001911
1912 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1913 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
1914 LayerProtoHelper::writeToProto(state.color,
1915 [&]() { return layerInfo->mutable_requested_color(); });
1916 layerInfo->set_flags(state.flags);
1917
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001918 LayerProtoHelper::writeToProto(requestedTransform,
1919 layerInfo->mutable_requested_transform());
1920
1921 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
1922 if (parent != nullptr) {
1923 layerInfo->set_parent(parent->sequence);
1924 } else {
1925 layerInfo->set_parent(-1);
1926 }
1927
1928 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1929 if (zOrderRelativeOf != nullptr) {
1930 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1931 } else {
1932 layerInfo->set_z_order_relative_of(-1);
1933 }
chaviwadc40c22018-07-10 16:57:27 -07001934 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001935
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001936 if (traceFlags & SurfaceTracing::TRACE_INPUT) {
1937 LayerProtoHelper::writeToProto(state.inputInfo, state.touchableRegionCrop,
1938 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001939 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001940
1941 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
1942 auto protoMap = layerInfo->mutable_metadata();
1943 for (const auto& entry : state.metadata.mMap) {
1944 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
1945 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001946 }
chaviw1d044282017-09-27 12:19:28 -07001947}
1948
Robert Carr2e102c92018-10-23 12:11:15 -07001949bool Layer::isRemovedFromCurrentState() const {
1950 return mRemovedFromCurrentState;
1951}
1952
Arthur Hungd20b2702019-01-14 18:16:16 +08001953InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001954 InputWindowInfo info = mDrawingState.inputInfo;
Robert Carr720e5062018-07-30 17:45:14 -07001955
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001956 if (info.displayId == ADISPLAY_ID_NONE) {
chaviwb15ea8a2019-09-03 12:40:22 -07001957 info.displayId = getLayerStack();
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001958 }
1959
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001960 ui::Transform t = getTransform();
1961 const float xScale = t.sx();
1962 const float yScale = t.sy();
Ady Abraham282f1d72019-07-24 18:05:56 -07001963 int32_t xSurfaceInset = info.surfaceInset;
1964 int32_t ySurfaceInset = info.surfaceInset;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001965 if (xScale != 1.0f || yScale != 1.0f) {
Ady Abraham282f1d72019-07-24 18:05:56 -07001966 info.windowXScale *= (xScale != 0.0f) ? 1.0f / xScale : 0.0f;
1967 info.windowYScale *= (yScale != 0.0f) ? 1.0f / yScale : 0.0f;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001968 info.touchableRegion.scaleSelf(xScale, yScale);
Ady Abraham282f1d72019-07-24 18:05:56 -07001969 xSurfaceInset = std::round(xSurfaceInset * xScale);
1970 ySurfaceInset = std::round(ySurfaceInset * yScale);
Riddle Hsu39d4aa52018-11-30 20:46:53 +08001971 }
Robert Carre07e1032018-11-26 12:55:53 -08001972
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001973 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001974 // If this is a portal window, set the touchableRegion to the layerBounds.
1975 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
1976 ? getBufferSize(getDrawingState())
1977 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08001978 if (!layerBounds.isValid()) {
1979 layerBounds = getCroppedBufferSize(getDrawingState());
1980 }
Vishnu Nair8033a492018-12-05 07:27:23 -08001981 layerBounds = t.transform(layerBounds);
Ady Abraham282f1d72019-07-24 18:05:56 -07001982
1983 // clamp inset to layer bounds
1984 xSurfaceInset = (xSurfaceInset >= 0) ? std::min(xSurfaceInset, layerBounds.getWidth() / 2) : 0;
1985 ySurfaceInset = (ySurfaceInset >= 0) ? std::min(ySurfaceInset, layerBounds.getHeight() / 2) : 0;
1986
Arthur Hung118b1142019-05-08 21:25:59 +08001987 layerBounds.inset(xSurfaceInset, ySurfaceInset, xSurfaceInset, ySurfaceInset);
Vishnu Nair8033a492018-12-05 07:27:23 -08001988
Arthur Hungd20b2702019-01-14 18:16:16 +08001989 // Input coordinate should match the layer bounds.
1990 info.frameLeft = layerBounds.left;
1991 info.frameTop = layerBounds.top;
1992 info.frameRight = layerBounds.right;
1993 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08001994
1995 // Position the touchable region relative to frame screen location and restrict it to frame
1996 // bounds.
1997 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08001998 info.visible = canReceiveInput();
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001999
2000 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2001 if (info.replaceTouchableRegionWithCrop) {
2002 if (cropLayer == nullptr) {
2003 info.touchableRegion = Region(Rect{mScreenBounds});
2004 } else {
2005 info.touchableRegion = Region(Rect{cropLayer->mScreenBounds});
2006 }
2007 } else if (cropLayer != nullptr) {
2008 info.touchableRegion = info.touchableRegion.intersect(Rect{cropLayer->mScreenBounds});
2009 }
2010
Robert Carr720e5062018-07-30 17:45:14 -07002011 return info;
2012}
2013
2014bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002015 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002016}
2017
Lloyd Piquefeb73d72018-12-04 17:23:44 -08002018std::shared_ptr<compositionengine::Layer> Layer::getCompositionLayer() const {
2019 return nullptr;
2020}
2021
chaviw3e727cd2019-01-31 13:41:05 -08002022bool Layer::canReceiveInput() const {
2023 return isVisible();
2024}
2025
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002026compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
2027 const sp<const DisplayDevice>& display) const {
2028 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionLayer().get());
2029}
2030
Mathias Agopian13127d82013-03-05 17:47:11 -08002031// ---------------------------------------------------------------------------
2032
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002033}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002034
2035#if defined(__gl_h_)
2036#error "don't include gl/gl.h in this file"
2037#endif
2038
2039#if defined(__gl2_h_)
2040#error "don't include gl2/gl2.h in this file"
2041#endif