blob: 5e5302d83c0c9a336946613f9c477a6c4b304703 [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
Lloyd Piquef5275482019-01-29 18:42:42 -0800464 compositionState.isColorspaceAgnostic = isColorSpaceAgnostic();
465 compositionState.dataspace = mCurrentDataSpace;
466 compositionState.colorTransform = getColorTransform();
467 compositionState.colorTransformIsIdentity = !hasColorTransform();
468 compositionState.surfaceDamage = surfaceDamageRegion;
Lloyd Pique688abd42019-02-15 15:42:24 -0800469 compositionState.hasProtectedContent = isProtected();
470
471 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
472 compositionState.isOpaque =
473 isOpaque(drawingState) && !usesRoundedCorners && getAlpha() == 1.0_hf;
Lloyd Piquef5275482019-01-29 18:42:42 -0800474
475 // Force client composition for special cases known only to the front-end.
Lloyd Pique688abd42019-02-15 15:42:24 -0800476 if (isHdrY410() || usesRoundedCorners) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800477 compositionState.forceClientComposition = true;
478 }
479}
480
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800481void Layer::latchCursorCompositionState(
482 compositionengine::LayerFECompositionState& compositionState) const {
483 // This gives us only the "orientation" component of the transform
484 const State& drawingState{getDrawingState()};
485
486 // Apply the layer's transform, followed by the display's global transform
487 // Here we're guaranteed that the layer's transform preserves rects
488 Rect win = getCroppedBufferSize(drawingState);
489 // Subtract the transparent region and snap to the bounds
490 Rect bounds = reduce(win, getActiveTransparentRegion(drawingState));
491 Rect frame(getTransform().transform(bounds));
492
493 compositionState.cursorFrame = frame;
494}
495
Lloyd Piquef16688f2019-02-19 17:47:57 -0800496bool Layer::onPreComposition(nsecs_t) {
497 return false;
498}
499
Lloyd Piquea83776c2019-01-29 18:42:32 -0800500void Layer::latchCompositionState(compositionengine::LayerFECompositionState& compositionState,
501 bool includeGeometry) const {
502 if (includeGeometry) {
503 latchGeometry(compositionState);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700504 }
Lloyd Piquef5275482019-01-29 18:42:42 -0800505
506 latchPerFrameState(compositionState);
Lloyd Piquea83776c2019-01-29 18:42:32 -0800507}
Mathias Agopian29a367b2011-07-12 14:51:45 -0700508
Lloyd Piquea83776c2019-01-29 18:42:32 -0800509const char* Layer::getDebugName() const {
510 return mName.string();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800511}
512
Mathias Agopian13127d82013-03-05 17:47:11 -0800513// ---------------------------------------------------------------------------
514// drawing...
515// ---------------------------------------------------------------------------
516
Lloyd Piquef16688f2019-02-19 17:47:57 -0800517std::optional<renderengine::LayerSettings> Layer::prepareClientComposition(
518 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
519 if (!getCompositionLayer()) {
520 return {};
521 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800522
Vishnu Nair4351ad52019-02-11 14:13:02 -0800523 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000524 half alpha = getAlpha();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800525 renderengine::LayerSettings layerSettings;
526 layerSettings.geometry.boundaries = bounds;
527 if (targetSettings.useIdentityTransform) {
528 layerSettings.geometry.positionTransform = mat4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000529 } else {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800530 layerSettings.geometry.positionTransform = getTransform().asMatrix4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000531 }
532
533 if (hasColorTransform()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800534 layerSettings.colorTransform = getColorTransform();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000535 }
536
537 const auto roundedCornerState = getRoundedCornerState();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800538 layerSettings.geometry.roundedCornersRadius = roundedCornerState.radius;
539 layerSettings.geometry.roundedCornersCrop = roundedCornerState.cropRect;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000540
Lloyd Piquef16688f2019-02-19 17:47:57 -0800541 layerSettings.alpha = alpha;
542 layerSettings.sourceDataspace = mCurrentDataSpace;
543 return layerSettings;
Mathias Agopian13127d82013-03-05 17:47:11 -0800544}
545
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800546Hwc2::IComposerClient::Composition Layer::getCompositionType(
547 const sp<const DisplayDevice>& display) const {
548 const auto outputLayer = findOutputLayerForDisplay(display);
549 LOG_FATAL_IF(!outputLayer);
550 return outputLayer->getState().hwc ? (*outputLayer->getState().hwc).hwcCompositionType
551 : Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800552}
553
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800554bool Layer::getClearClientTarget(const sp<const DisplayDevice>& display) const {
555 const auto outputLayer = findOutputLayerForDisplay(display);
556 LOG_FATAL_IF(!outputLayer);
557 return outputLayer->getState().clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800558}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800559
Dan Stozacac35382016-01-27 12:21:06 -0800560bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
561 if (point->getFrameNumber() <= mCurrentFrameNumber) {
562 // Don't bother with a SyncPoint, since we've already latched the
563 // relevant frame
564 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700565 }
Robert Carr2e102c92018-10-23 12:11:15 -0700566 if (isRemovedFromCurrentState()) {
567 return false;
568 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700569
Dan Stozacac35382016-01-27 12:21:06 -0800570 Mutex::Autolock lock(mLocalSyncPointMutex);
571 mLocalSyncPoints.push_back(point);
572 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700573}
574
Mathias Agopian13127d82013-03-05 17:47:11 -0800575// ----------------------------------------------------------------------------
576// local state
577// ----------------------------------------------------------------------------
578
David Sodman41fdfc92017-11-06 16:09:56 -0800579bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800580 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700581 return (s.flags & layer_state_t::eLayerSecure);
582}
583
Mathias Agopian13127d82013-03-05 17:47:11 -0800584// ----------------------------------------------------------------------------
585// transaction
586// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800587
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800588void Layer::pushPendingState() {
589 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700590 return;
591 }
Alec Mourie60041e2019-06-14 18:59:51 -0700592 ATRACE_CALL();
Dan Stoza7dde5992015-05-22 09:51:44 -0700593
Dan Stoza7dde5992015-05-22 09:51:44 -0700594 // If this transaction is waiting on the receipt of a frame, generate a sync
595 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700596 // We don't allow installing sync points after we are removed from the current state
597 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800598 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
599 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800600 if (barrierLayer == nullptr) {
601 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700602 // If we can't promote the layer we are intended to wait on,
603 // then it is expired or otherwise invalid. Allow this transaction
604 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800605 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800606 } else {
chaviw43cb3cb2019-05-31 15:23:41 -0700607 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy, this);
Robert Carr0d480722017-01-10 16:42:54 -0800608 if (barrierLayer->addSyncPoint(syncPoint)) {
Alec Mourie60041e2019-06-14 18:59:51 -0700609 std::stringstream ss;
610 ss << "Adding sync point " << mCurrentState.frameNumber_legacy;
611 ATRACE_NAME(ss.str().c_str());
Dan Stozacac35382016-01-27 12:21:06 -0800612 mRemoteSyncPoints.push_back(std::move(syncPoint));
613 } else {
614 // We already missed the frame we're supposed to synchronize
615 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800616 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800617 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700618 }
619
Dan Stoza7dde5992015-05-22 09:51:44 -0700620 // Wake us up to check if the frame has been received
621 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700622 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700623 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800624 mPendingStates.push_back(mCurrentState);
625 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700626}
627
Pablo Ceballos05289c22016-04-14 15:49:55 -0700628void Layer::popPendingState(State* stateToCommit) {
Alec Mourie60041e2019-06-14 18:59:51 -0700629 ATRACE_CALL();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800630 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700631
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800632 mPendingStates.removeAt(0);
633 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700634}
635
Pablo Ceballos05289c22016-04-14 15:49:55 -0700636bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700637 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800638 while (!mPendingStates.empty()) {
639 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700640 if (mRemoteSyncPoints.empty()) {
641 // If we don't have a sync point for this, apply it anyway. It
642 // will be visually wrong, but it should keep us from getting
643 // into too much trouble.
644 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700645 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700646 stateUpdateAvailable = true;
647 continue;
648 }
649
Marissa Wallf58c14b2018-07-24 10:50:43 -0700650 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800651 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800652 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800653
654 // Signal our end of the sync point and then dispose of it
655 mRemoteSyncPoints.front()->setTransactionApplied();
656 mRemoteSyncPoints.pop_front();
657 continue;
658 }
659
Dan Stoza7dde5992015-05-22 09:51:44 -0700660 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
Alec Mourie60041e2019-06-14 18:59:51 -0700661 ATRACE_NAME("frameIsAvailable");
Dan Stoza7dde5992015-05-22 09:51:44 -0700662 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700663 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700664 stateUpdateAvailable = true;
665
666 // Signal our end of the sync point and then dispose of it
667 mRemoteSyncPoints.front()->setTransactionApplied();
668 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800669 } else {
Alec Mourie60041e2019-06-14 18:59:51 -0700670 ATRACE_NAME("!frameIsAvailable");
Dan Stoza792e5292016-02-11 11:43:58 -0800671 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700672 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700673 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700674 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700675 stateUpdateAvailable = true;
676 }
677 }
678
679 // If we still have pending updates, wake SurfaceFlinger back up and point
680 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800681 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700682 setTransactionFlags(eTransactionNeeded);
683 mFlinger->setTransactionFlags(eTraversalNeeded);
684 }
685
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800686 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700687 return stateUpdateAvailable;
688}
689
Marissa Wall61c58622018-07-18 10:12:20 -0700690uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000691 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800692
Marissa Wall61c58622018-07-18 10:12:20 -0700693 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
694 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700695
David Sodmaneb085e02017-10-05 18:49:04 -0700696 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700697 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000698 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800699 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
700 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
701 " requested={ wh={%4u,%4u} }}\n"
702 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
703 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700704 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700705 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
706 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
707 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
708 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
709 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700710 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
711 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
712 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800713 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700714
Robert Carre392b552017-09-19 12:16:05 -0700715 // Don't let Layer::doTransaction update the drawing state
716 // if we have a pending resize, unless we are in fixed-size mode.
717 // the drawing state will be updated only once we receive a buffer
718 // with the correct size.
719 //
720 // In particular, we want to make sure the clip (which is part
721 // of the geometry state) is latched together with the size but is
722 // latched immediately when no resizing is involved.
723 //
724 // If a sideband stream is attached, however, we want to skip this
725 // optimization so that transactions aren't missed when a buffer
726 // never arrives
727 //
728 // In the case that we don't have a buffer we ignore other factors
729 // and avoid entering the resizePending state. At a high level the
730 // resizePending state is to avoid applying the state of the new buffer
731 // to the old buffer. However in the state where we don't have an old buffer
732 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700733 const bool resizePending =
734 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
735 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
Lloyd Pique0b785d82018-12-04 17:25:27 -0800736 (mActiveBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700737 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800738 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700739 flags |= eDontUpdateGeometryState;
740 }
741 }
742
Robert Carr7bf247e2017-05-18 14:02:49 -0700743 // Here we apply various requested geometry states, depending on our
744 // latching configuration. See Layer.h for a detailed discussion of
745 // how geometry latching is controlled.
746 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000747 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700748
749 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
750 // mode, which causes attributes which normally latch regardless of scaling mode,
751 // to be delayed. We copy the requested state to the active state making sure
752 // to respect these rules (again see Layer.h for a detailed discussion).
753 //
754 // There is an awkward asymmetry in the handling of the crop states in the position
755 // states, as can be seen below. Largely this arises from position and transform
756 // being stored in the same data structure while having different latching rules.
757 // b/38182305
758 //
Marissa Wall61c58622018-07-18 10:12:20 -0700759 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700760 // applyPendingStates in the presence of deferred transactions.
761 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -0700762 float tx = stateToCommit->active_legacy.transform.tx();
763 float ty = stateToCommit->active_legacy.transform.ty();
764 stateToCommit->active_legacy = stateToCommit->requested_legacy;
765 stateToCommit->active_legacy.transform.set(tx, ty);
766 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700767 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700768 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -0700769 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700770 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800771 }
772
Marissa Wall61c58622018-07-18 10:12:20 -0700773 return flags;
774}
775
776uint32_t Layer::doTransaction(uint32_t flags) {
777 ATRACE_CALL();
778
chaviw5aedec92018-10-22 10:40:38 -0700779 if (mLayerDetached) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800780 return flags;
781 }
782
783 if (mChildrenChanged) {
784 flags |= eVisibleRegion;
785 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -0700786 }
787
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800788 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +0000789 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -0700790 if (!applyPendingStates(&c)) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800791 return flags;
Marissa Wall61c58622018-07-18 10:12:20 -0700792 }
793
794 flags = doTransactionResize(flags, &c);
795
Alec Mourib416efd2018-09-06 21:01:59 +0000796 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700797
798 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800799 // invalidate and recompute the visible regions if needed
800 flags |= Layer::eVisibleRegion;
801 }
802
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700803 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800804 // invalidate and recompute the visible regions if needed
805 flags |= eVisibleRegion;
806 this->contentDirty = true;
807
808 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -0700809 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -0700810 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -0800811 }
812
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800813 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -0700814 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800815 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -0700816 }
817
Mathias Agopian13127d82013-03-05 17:47:11 -0800818 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -0700819 commitTransaction(c);
Vishnu Nair8406fd72019-07-30 11:29:31 -0700820 mPendingStatesSnapshot = mPendingStates;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800821 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -0800822 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800823}
824
Pablo Ceballos05289c22016-04-14 15:49:55 -0700825void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800826 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700827}
828
Mathias Agopian13127d82013-03-05 17:47:11 -0800829uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800830 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800831}
832
833uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800834 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -0800835}
836
Robert Carr82364e32016-05-15 11:27:47 -0700837bool Layer::setPosition(float x, float y, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800838 if (mCurrentState.requested_legacy.transform.tx() == x &&
839 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -0800840 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800841 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -0700842
843 // We update the requested and active position simultaneously because
844 // we want to apply the position portion of the transform matrix immediately,
845 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800846 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -0700847 if (immediate && !mFreezeGeometryUpdates) {
848 // Here we directly update the active state
849 // unlike other setters, because we store it within
850 // the transform, but use different latching rules.
851 // b/38182305
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800852 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -0700853 }
Robert Carr7bf247e2017-05-18 14:02:49 -0700854 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -0700855
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800856 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800857 setTransactionFlags(eTransactionNeeded);
858 return true;
859}
Robert Carr82364e32016-05-15 11:27:47 -0700860
Robert Carr1f0a16a2016-10-24 16:27:39 -0700861bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
862 ssize_t idx = mCurrentChildren.indexOf(childLayer);
863 if (idx < 0) {
864 return false;
865 }
866 if (childLayer->setLayer(z)) {
867 mCurrentChildren.removeAt(idx);
868 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800869 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700870 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800871 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700872}
873
Robert Carr503c7042017-09-27 15:06:08 -0700874bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
875 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
876 ssize_t idx = mCurrentChildren.indexOf(childLayer);
877 if (idx < 0) {
878 return false;
879 }
880 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
881 mCurrentChildren.removeAt(idx);
882 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800883 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700884 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800885 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700886}
887
Robert Carrae060832016-11-28 10:51:00 -0800888bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800889 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
890 mCurrentState.sequence++;
891 mCurrentState.z = z;
892 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700893
894 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800895 if (mCurrentState.zOrderRelativeOf != nullptr) {
896 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700897 if (strongRelative != nullptr) {
898 strongRelative->removeZOrderRelative(this);
899 }
chaviw606e5cf2019-03-01 10:12:10 -0800900 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -0700901 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800902 setTransactionFlags(eTransactionNeeded);
903 return true;
904}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700905
Robert Carrdb66e622017-04-10 16:55:57 -0700906void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800907 mCurrentState.zOrderRelatives.remove(relative);
908 mCurrentState.sequence++;
909 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700910 setTransactionFlags(eTransactionNeeded);
911}
912
913void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800914 mCurrentState.zOrderRelatives.add(relative);
915 mCurrentState.modified = true;
916 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -0700917 setTransactionFlags(eTransactionNeeded);
918}
919
chaviw606e5cf2019-03-01 10:12:10 -0800920void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
921 mCurrentState.zOrderRelativeOf = relativeOf;
922 mCurrentState.sequence++;
923 mCurrentState.modified = true;
924 setTransactionFlags(eTransactionNeeded);
925}
926
Robert Carr503d2bd2017-12-04 15:49:47 -0800927bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -0700928 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
929 if (handle == nullptr) {
930 return false;
931 }
932 sp<Layer> relative = handle->owner.promote();
933 if (relative == nullptr) {
934 return false;
935 }
936
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800937 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
938 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -0800939 return false;
940 }
941
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800942 mCurrentState.sequence++;
943 mCurrentState.modified = true;
944 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -0700945
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800946 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -0700947 if (oldZOrderRelativeOf != nullptr) {
948 oldZOrderRelativeOf->removeZOrderRelative(this);
949 }
chaviw606e5cf2019-03-01 10:12:10 -0800950 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -0700951 relative->addZOrderRelative(this);
952
953 setTransactionFlags(eTransactionNeeded);
954
955 return true;
956}
957
Mathias Agopian13127d82013-03-05 17:47:11 -0800958bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800959 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -0700960 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800961 mCurrentState.requested_legacy.w = w;
962 mCurrentState.requested_legacy.h = h;
963 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800964 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -0700965
966 // record the new size, from this point on, when the client request
967 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800968 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -0800969 return true;
970}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800971bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800972 if (mCurrentState.color.a == alpha) return false;
973 mCurrentState.sequence++;
974 mCurrentState.color.a = alpha;
975 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800976 setTransactionFlags(eTransactionNeeded);
977 return true;
978}
chaviw13fdc492017-06-27 12:40:18 -0700979
Valerie Haudd0b7572019-01-29 14:59:27 -0800980bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
981 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -0700982 return false;
Valerie Hauaa194562019-02-05 16:21:38 -0800983 }
984 mCurrentState.sequence++;
985 mCurrentState.modified = true;
986 setTransactionFlags(eTransactionNeeded);
987
988 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -0800989 // create background color layer if one does not yet exist
990 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
991 const String8& name = mName + "BackgroundColorLayer";
Evan Roskya1f1e152019-01-24 16:17:46 -0800992 mCurrentState.bgColorLayer = new ColorLayer(
993 LayerCreationArgs(mFlinger.get(), nullptr, name, 0, 0, flags, LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -0700994
Valerie Haudd0b7572019-01-29 14:59:27 -0800995 // add to child list
996 addChild(mCurrentState.bgColorLayer);
997 mFlinger->mLayersAdded = true;
998 // set up SF to handle added color layer
999 if (isRemovedFromCurrentState()) {
1000 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1001 }
1002 mFlinger->setTransactionFlags(eTransactionNeeded);
1003 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1004 mCurrentState.bgColorLayer->reparent(nullptr);
1005 mCurrentState.bgColorLayer = nullptr;
1006 return true;
1007 }
1008
1009 mCurrentState.bgColorLayer->setColor(color);
1010 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1011 mCurrentState.bgColorLayer->setAlpha(alpha);
1012 mCurrentState.bgColorLayer->setDataspace(dataspace);
1013
chaviw13fdc492017-06-27 12:40:18 -07001014 return true;
1015}
1016
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001017bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001018 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001019
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001020 mCurrentState.sequence++;
1021 mCurrentState.cornerRadius = cornerRadius;
1022 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001023 setTransactionFlags(eTransactionNeeded);
1024 return true;
1025}
1026
Robert Carrd4ae7f32018-06-07 16:10:57 -07001027bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1028 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001029 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001030 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1031
1032 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1033 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1034 return false;
1035 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001036 mCurrentState.sequence++;
1037 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1038 matrix.dsdy);
1039 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001040 setTransactionFlags(eTransactionNeeded);
1041 return true;
1042}
Marissa Wall61c58622018-07-18 10:12:20 -07001043
Mathias Agopian13127d82013-03-05 17:47:11 -08001044bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001045 mCurrentState.requestedTransparentRegion_legacy = transparent;
1046 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001047 setTransactionFlags(eTransactionNeeded);
1048 return true;
1049}
1050bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001051 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1052 if (mCurrentState.flags == newFlags) return false;
1053 mCurrentState.sequence++;
1054 mCurrentState.flags = newFlags;
1055 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001056 setTransactionFlags(eTransactionNeeded);
1057 return true;
1058}
Robert Carr99e27f02016-06-16 15:18:02 -07001059
Marissa Wallf58c14b2018-07-24 10:50:43 -07001060bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001061 if (mCurrentState.requestedCrop_legacy == crop) return false;
1062 mCurrentState.sequence++;
1063 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001064 if (immediate && !mFreezeGeometryUpdates) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001065 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001066 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001067 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1068
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001069 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001070 setTransactionFlags(eTransactionNeeded);
1071 return true;
1072}
Robert Carr8d5227b2017-03-16 15:41:03 -07001073
Robert Carrc3574f72016-03-24 12:19:32 -07001074bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001075 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001076 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001077 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001078 return true;
1079}
1080
Evan Roskyef876c92019-01-25 17:46:06 -08001081bool Layer::setMetadata(const LayerMetadata& data) {
1082 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001083 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001084 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001085 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001086 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001087}
1088
Mathias Agopian13127d82013-03-05 17:47:11 -08001089bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001090 if (mCurrentState.layerStack == layerStack) return false;
1091 mCurrentState.sequence++;
1092 mCurrentState.layerStack = layerStack;
1093 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001094 setTransactionFlags(eTransactionNeeded);
1095 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001096}
1097
Peiyong Linc502cb72019-03-01 15:00:23 -08001098bool Layer::setColorSpaceAgnostic(const bool agnostic) {
1099 if (mCurrentState.colorSpaceAgnostic == agnostic) {
1100 return false;
1101 }
1102 mCurrentState.sequence++;
1103 mCurrentState.colorSpaceAgnostic = agnostic;
1104 mCurrentState.modified = true;
1105 setTransactionFlags(eTransactionNeeded);
1106 return true;
1107}
1108
Robert Carr1f0a16a2016-10-24 16:27:39 -07001109uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001110 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001111 if (p == nullptr) {
1112 return getDrawingState().layerStack;
1113 }
1114 return p->getLayerStack();
1115}
1116
Marissa Wallf58c14b2018-07-24 10:50:43 -07001117void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Alec Mourie60041e2019-06-14 18:59:51 -07001118 ATRACE_CALL();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001119 mCurrentState.barrierLayer_legacy = barrierLayer;
1120 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001121 // We don't set eTransactionNeeded, because just receiving a deferral
1122 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001123 mCurrentState.modified = true;
1124 pushPendingState();
1125 mCurrentState.barrierLayer_legacy = nullptr;
1126 mCurrentState.frameNumber_legacy = 0;
1127 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001128}
1129
Marissa Wallf58c14b2018-07-24 10:50:43 -07001130void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001131 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001132 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001133}
1134
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001135// ----------------------------------------------------------------------------
1136// pageflip handling...
1137// ----------------------------------------------------------------------------
1138
Robert Carr1f0a16a2016-10-24 16:27:39 -07001139bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001140 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001141 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001142 if (parent != nullptr && parent->isHiddenByPolicy()) {
1143 return true;
1144 }
Robert Carr1c5481e2019-07-01 14:42:27 -07001145 if (usingRelativeZ(LayerVector::StateSet::Drawing)) {
1146 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
1147 if (zOrderRelativeOf != nullptr) {
1148 if (zOrderRelativeOf->isHiddenByPolicy()) {
1149 return true;
1150 }
1151 }
1152 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001153 return s.flags & layer_state_t::eLayerHidden;
1154}
1155
David Sodman41fdfc92017-11-06 16:09:56 -08001156uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001157 // TODO: should we do something special if mSecure is set?
1158 if (mProtectedByApp) {
1159 // need a hardware-protected path to external video sink
1160 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001161 }
Riley Andrews03414a12014-07-01 14:22:59 -07001162 if (mPotentialCursor) {
1163 usage |= GraphicBuffer::USAGE_CURSOR;
1164 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001165 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001166 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001167}
1168
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001169void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001170 uint32_t orientation = 0;
Vishnu Nair5eb3f062019-04-08 08:21:03 -07001171 // Disable setting transform hint if the debug flag is set.
1172 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001173 // The transform hint is used to improve performance, but we can
1174 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001175 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001176 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001177 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001178 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001179 orientation = 0;
1180 }
1181 }
David Sodmaneb085e02017-10-05 18:49:04 -07001182 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001183}
1184
Mathias Agopian13127d82013-03-05 17:47:11 -08001185// ----------------------------------------------------------------------------
1186// debugging
1187// ----------------------------------------------------------------------------
1188
Marissa Wall61c58622018-07-18 10:12:20 -07001189// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001190LayerDebugInfo Layer::getLayerDebugInfo() const {
1191 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001192 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001193 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001194 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001195 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
chaviw8a01fa42019-08-19 12:39:31 -07001196 info.mType = getType();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001197 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Lloyd Piquea2468662019-03-07 21:31:06 -08001198
1199 info.mVisibleRegion = debugGetVisibleRegionOnDefaultDisplay();
Kalle Raitaa099a242017-01-11 11:17:29 -08001200 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001201 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001202 info.mX = ds.active_legacy.transform.tx();
1203 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001204 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001205 info.mWidth = ds.active_legacy.w;
1206 info.mHeight = ds.active_legacy.h;
1207 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001208 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001209 info.mFlags = ds.flags;
1210 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001211 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001212 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1213 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1214 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1215 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001216 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001217 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001218 if (buffer != 0) {
1219 info.mActiveBufferWidth = buffer->getWidth();
1220 info.mActiveBufferHeight = buffer->getHeight();
1221 info.mActiveBufferStride = buffer->getStride();
1222 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001223 } else {
1224 info.mActiveBufferWidth = 0;
1225 info.mActiveBufferHeight = 0;
1226 info.mActiveBufferStride = 0;
1227 info.mActiveBufferFormat = 0;
1228 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001229 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001230 info.mNumQueuedFrames = getQueuedFrameCount();
1231 info.mRefreshPending = isBufferLatched();
1232 info.mIsOpaque = isOpaque(ds);
1233 info.mContentDirty = contentDirty;
1234 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001235}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001236
Yiwei Zhang5434a782018-12-05 18:06:32 -08001237void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001238 result.append("-------------------------------");
1239 result.append("-------------------------------");
1240 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001241 result.append(" Layer name\n");
1242 result.append(" Z | ");
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001243 result.append(" Window Type | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001244 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001245 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001246 result.append(" Disp Frame (LTRB) | ");
1247 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001248 result.append("-------------------------------");
1249 result.append("-------------------------------");
1250 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001251}
1252
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001253void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1254 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1255 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001256 return;
1257 }
1258
Yiwei Zhang5434a782018-12-05 18:06:32 -08001259 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001260 if (mName.length() > 77) {
1261 std::string shortened;
1262 shortened.append(mName.string(), 36);
1263 shortened.append("[...]");
1264 shortened.append(mName.string() + (mName.length() - 36), 36);
Yiwei Zhang5434a782018-12-05 18:06:32 -08001265 name = shortened;
Dan Stozae22aec72016-08-01 13:20:59 -07001266 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001267 name = std::string(mName.string(), mName.size());
Dan Stozae22aec72016-08-01 13:20:59 -07001268 }
1269
Yiwei Zhang5434a782018-12-05 18:06:32 -08001270 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001271
Alec Mourib416efd2018-09-06 21:01:59 +00001272 const State& layerState(getDrawingState());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001273 const auto& compositionState = outputLayer->getState();
1274
Chia-I Wu1e043612018-03-01 09:45:09 -08001275 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001276 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001277 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001278 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001279 }
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001280 StringAppendF(&result, " %10d | ", mWindowType);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001281 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
1282 StringAppendF(&result, "%10s | ",
1283 toString(getCompositionLayer() ? compositionState.bufferTransform
1284 : static_cast<Hwc2::Transform>(0))
1285 .c_str());
1286 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001287 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001288 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001289 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1290 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001291
Yichi Chen6ca35192018-05-29 12:20:43 +08001292 result.append("- - - - - - - - - - - - - - - -");
1293 result.append("- - - - - - - - - - - - - - - -");
1294 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001295}
Dan Stozae22aec72016-08-01 13:20:59 -07001296
Yiwei Zhang5434a782018-12-05 18:06:32 -08001297void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001298 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001299}
1300
Svetoslavd85084b2014-03-20 10:28:31 -07001301void Layer::clearFrameStats() {
1302 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001303}
1304
Jamie Gennis6547ff42013-07-16 20:12:42 -07001305void Layer::logFrameStats() {
1306 mFrameTracker.logAndResetStats(mName);
1307}
1308
Svetoslavd85084b2014-03-20 10:28:31 -07001309void Layer::getFrameStats(FrameStats* outStats) const {
1310 mFrameTracker.getStats(outStats);
1311}
1312
Yiwei Zhang5434a782018-12-05 18:06:32 -08001313void Layer::dumpFrameEvents(std::string& result) {
chaviw8a01fa42019-08-19 12:39:31 -07001314 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().string(), getType(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001315 Mutex::Autolock lock(mFrameEventHistoryMutex);
1316 mFrameEventHistory.checkFencesForCompletion();
1317 mFrameEventHistory.dump(result);
1318}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001319
Vishnu Nair0f085c62019-08-30 08:49:12 -07001320void Layer::dumpCallingUidPid(std::string& result) const {
1321 StringAppendF(&result, "Layer %s (%s) pid:%d uid:%d\n", getName().string(), getType(),
1322 mCallingPid, mCallingUid);
1323}
1324
Brian Anderson5ea5e592016-12-01 16:54:33 -08001325void Layer::onDisconnect() {
1326 Mutex::Autolock lock(mFrameEventHistoryMutex);
1327 mFrameEventHistory.onDisconnect();
Mikael Pessa90092f42019-08-26 17:22:04 -07001328 const int32_t layerID = getSequence();
1329 mFlinger->mTimeStats->onDestroy(layerID);
1330 mFlinger->mFrameTracer->onDestroy(layerID);
Brian Anderson5ea5e592016-12-01 16:54:33 -08001331}
1332
Brian Anderson3890c392016-07-25 12:48:08 -07001333void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001334 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001335 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001336 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1337 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001338 }
1339
Brian Andersond6927fb2016-07-23 23:37:30 -07001340 Mutex::Autolock lock(mFrameEventHistoryMutex);
1341 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001342 // If there are any unsignaled fences in the aquire timeline at this
1343 // point, the previously queued frame hasn't been latched yet. Go ahead
1344 // and try to get the signal time here so the syscall is taken out of
1345 // the main thread's critical path.
1346 mAcquireTimeline.updateSignalTimes();
1347 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001348 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001349 mFrameEventHistory.addQueue(*newTimestamps);
1350 }
1351
Brian Anderson3890c392016-07-25 12:48:08 -07001352 if (outDelta) {
1353 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001354 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001355}
Dan Stozae77c7662016-05-13 11:37:28 -07001356
Chia-I Wu98f1c102017-05-30 14:54:08 -07001357size_t Layer::getChildrenCount() const {
1358 size_t count = 0;
1359 for (const sp<Layer>& child : mCurrentChildren) {
1360 count += 1 + child->getChildrenCount();
1361 }
1362 return count;
1363}
1364
Robert Carr1f0a16a2016-10-24 16:27:39 -07001365void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001366 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001367 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001368
Robert Carr1f0a16a2016-10-24 16:27:39 -07001369 mCurrentChildren.add(layer);
1370 layer->setParent(this);
1371}
1372
1373ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001374 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001375 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001376
Robert Carr1323c952019-01-28 18:13:27 -08001377 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001378 return mCurrentChildren.remove(layer);
1379}
1380
Robert Carr1db73f62016-12-21 12:58:51 -08001381bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1382 sp<Handle> handle = nullptr;
1383 sp<Layer> newParent = nullptr;
1384 if (newParentHandle == nullptr) {
1385 return false;
1386 }
1387 handle = static_cast<Handle*>(newParentHandle.get());
1388 newParent = handle->owner.promote();
1389 if (newParent == nullptr) {
1390 ALOGE("Unable to promote Layer handle");
1391 return false;
1392 }
1393
chaviw5aedec92018-10-22 10:40:38 -07001394 if (attachChildren()) {
1395 setTransactionFlags(eTransactionNeeded);
1396 }
Robert Carr1db73f62016-12-21 12:58:51 -08001397 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001398 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001399 }
1400 mCurrentChildren.clear();
1401
1402 return true;
1403}
1404
Robert Carr15eae092018-03-23 13:43:53 -07001405void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001406 for (const sp<Layer>& child : mDrawingChildren) {
1407 child->mDrawingParent = newParent;
Vishnu Nairc652ff82019-03-15 12:48:54 -07001408 child->computeBounds(newParent->mBounds,
1409 newParent->getTransformWithScale(
1410 newParent->getBufferScaleTransform()));
Robert Carr578038f2018-03-09 12:25:24 -08001411 }
1412}
1413
chaviwf1961f72017-09-18 16:41:07 -07001414bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001415 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001416
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001417 // While layers are detached, we allow most operations
1418 // and simply halt performing the actual transaction. However
1419 // for reparent != null we would enter the mRemovedFromCurrentState
1420 // state, regardless of whether doTransaction was called, and
1421 // so we need to prevent the update here.
1422 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001423 return false;
1424 }
1425
Robert Carr54cf5b12019-01-25 14:02:28 -08001426 sp<Layer> newParent;
1427 if (newParentHandle != nullptr) {
1428 auto handle = static_cast<Handle*>(newParentHandle.get());
1429 newParent = handle->owner.promote();
1430 if (newParent == nullptr) {
1431 ALOGE("Unable to promote Layer handle");
1432 return false;
1433 }
1434 if (newParent == this) {
1435 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1436 return false;
1437 }
1438 }
1439
chaviwf1961f72017-09-18 16:41:07 -07001440 sp<Layer> parent = getParent();
1441 if (parent != nullptr) {
1442 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001443 }
1444
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001445 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001446 newParent->addChild(this);
1447 if (!newParent->isRemovedFromCurrentState()) {
1448 addToCurrentState();
1449 } else {
1450 onRemovedFromCurrentState();
1451 }
1452
1453 if (mLayerDetached) {
1454 mLayerDetached = false;
1455 callSetTransactionFlags = true;
1456 }
1457 } else {
1458 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001459 }
1460
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001461 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001462 setTransactionFlags(eTransactionNeeded);
1463 }
chaviw06178942017-07-27 10:25:59 -07001464 return true;
1465}
1466
Robert Carr9524cb32017-02-13 11:32:32 -08001467bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001468 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001469 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001470 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001471 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001472 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001473 child->detachChildren();
chaviw43cb3cb2019-05-31 15:23:41 -07001474 child->removeRemoteSyncPoints();
Robert Carr9524cb32017-02-13 11:32:32 -08001475 }
Robert Carr7f619b22017-11-06 12:56:35 -08001476 }
Robert Carr9524cb32017-02-13 11:32:32 -08001477
1478 return true;
1479}
1480
chaviw5aedec92018-10-22 10:40:38 -07001481bool Layer::attachChildren() {
1482 bool changed = false;
1483 for (const sp<Layer>& child : mCurrentChildren) {
1484 sp<Client> parentClient = mClientRef.promote();
1485 sp<Client> client(child->mClientRef.promote());
1486 if (client != nullptr && parentClient != client) {
1487 if (child->mLayerDetached) {
1488 child->mLayerDetached = false;
1489 changed = true;
1490 }
1491 changed |= child->attachChildren();
1492 }
1493 }
1494
1495 return changed;
1496}
1497
Peiyong Lind3788632018-09-18 16:01:31 -07001498bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001499 static const mat4 identityMatrix = mat4();
1500
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001501 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001502 return false;
1503 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001504 ++mCurrentState.sequence;
1505 mCurrentState.colorTransform = matrix;
1506 mCurrentState.hasColorTransform = matrix != identityMatrix;
1507 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001508 setTransactionFlags(eTransactionNeeded);
1509 return true;
1510}
1511
chaviwf66724d2018-11-28 16:35:21 -08001512mat4 Layer::getColorTransform() const {
1513 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1514 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1515 colorTransform = parent->getColorTransform() * colorTransform;
1516 }
1517 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001518}
1519
1520bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001521 bool hasColorTransform = getDrawingState().hasColorTransform;
1522 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1523 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1524 }
1525 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001526}
1527
Chia-I Wu11481472018-05-04 10:43:19 -07001528bool Layer::isLegacyDataSpace() const {
1529 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001530 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001531 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001532}
1533
Robert Carr1f0a16a2016-10-24 16:27:39 -07001534void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001535 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001536}
1537
Robert Carr1f0a16a2016-10-24 16:27:39 -07001538int32_t Layer::getZ() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001539 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001540}
1541
Robert Carr1c5481e2019-07-01 14:42:27 -07001542bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) const {
Robert Carr29abff82017-12-04 13:51:20 -08001543 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001544 const State& state = useDrawing ? mDrawingState : mCurrentState;
Robert Carr29abff82017-12-04 13:51:20 -08001545 return state.zOrderRelativeOf != nullptr;
1546}
1547
David Sodman41fdfc92017-11-06 16:09:56 -08001548__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001549 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001550 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1551 "makeTraversalList received invalid stateSet");
1552 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1553 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001554 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001555
Robert Carr29abff82017-12-04 13:51:20 -08001556 if (state.zOrderRelatives.size() == 0) {
1557 *outSkipRelativeZUsers = true;
1558 return children;
1559 }
1560
chaviwfd462612018-05-31 16:11:27 -07001561 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001562 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001563 sp<Layer> strongRelative = weakRelative.promote();
1564 if (strongRelative != nullptr) {
1565 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001566 }
1567 }
1568
Dan Stoza412903f2017-04-27 13:42:17 -07001569 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001570 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
Robert Carr503c7042017-09-27 15:06:08 -07001571 if (childState.zOrderRelativeOf != nullptr) {
1572 continue;
1573 }
Robert Carrdb66e622017-04-10 16:55:57 -07001574 traverse.add(child);
1575 }
1576
1577 return traverse;
1578}
1579
Robert Carr1f0a16a2016-10-24 16:27:39 -07001580/**
Robert Carrdb66e622017-04-10 16:55:57 -07001581 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001582 */
Dan Stoza412903f2017-04-27 13:42:17 -07001583void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001584 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1585 // produce a new list for traversing, including our relatives, and not including our children
1586 // who are relatives of another surface. In the case that there are no relative Z,
1587 // makeTraversalList returns our children directly to avoid significant overhead.
1588 // However in this case we need to take the responsibility for filtering children which
1589 // are relatives of another surface here.
1590 bool skipRelativeZUsers = false;
1591 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001592
Robert Carr1f0a16a2016-10-24 16:27:39 -07001593 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001594 for (; i < list.size(); i++) {
1595 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001596 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1597 continue;
1598 }
1599
Robert Carrdb66e622017-04-10 16:55:57 -07001600 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001601 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001602 }
Dan Stoza412903f2017-04-27 13:42:17 -07001603 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001604 }
Robert Carr29abff82017-12-04 13:51:20 -08001605
Dan Stoza412903f2017-04-27 13:42:17 -07001606 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001607 for (; i < list.size(); i++) {
1608 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001609
Robert Carr29abff82017-12-04 13:51:20 -08001610 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1611 continue;
1612 }
Dan Stoza412903f2017-04-27 13:42:17 -07001613 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001614 }
1615}
1616
1617/**
Robert Carrdb66e622017-04-10 16:55:57 -07001618 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001619 */
Dan Stoza412903f2017-04-27 13:42:17 -07001620void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1621 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001622 // See traverseInZOrder for documentation.
1623 bool skipRelativeZUsers = false;
1624 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001625
Robert Carr1f0a16a2016-10-24 16:27:39 -07001626 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001627 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001628 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001629
1630 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1631 continue;
1632 }
1633
Robert Carrdb66e622017-04-10 16:55:57 -07001634 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001635 break;
1636 }
Dan Stoza412903f2017-04-27 13:42:17 -07001637 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001638 }
Dan Stoza412903f2017-04-27 13:42:17 -07001639 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001640 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001641 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001642
1643 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1644 continue;
1645 }
1646
Dan Stoza412903f2017-04-27 13:42:17 -07001647 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001648 }
1649}
1650
chaviw4b129c22018-04-09 16:19:43 -07001651LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1652 const std::vector<Layer*>& layersInTree) {
1653 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1654 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001655 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1656 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001657 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001658
chaviwfd462612018-05-31 16:11:27 -07001659 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001660 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1661 sp<Layer> strongRelative = weakRelative.promote();
1662 // Only add relative layers that are also descendents of the top most parent of the tree.
1663 // If a relative layer is not a descendent, then it should be ignored.
1664 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1665 traverse.add(strongRelative);
1666 }
1667 }
1668
1669 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001670 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001671 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1672 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1673 // the child here since it won't be added later as a relative.
1674 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1675 childState.zOrderRelativeOf.promote().get())) {
1676 continue;
1677 }
1678 traverse.add(child);
1679 }
1680
1681 return traverse;
1682}
1683
1684void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1685 LayerVector::StateSet stateSet,
1686 const LayerVector::Visitor& visitor) {
1687 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001688
1689 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001690 for (; i < list.size(); i++) {
1691 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001692 if (relative->getZ() >= 0) {
1693 break;
1694 }
chaviw4b129c22018-04-09 16:19:43 -07001695 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001696 }
chaviw4b129c22018-04-09 16:19:43 -07001697
chaviwa76b2712017-09-20 12:02:26 -07001698 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001699 for (; i < list.size(); i++) {
1700 const auto& relative = list[i];
1701 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001702 }
1703}
1704
chaviw4b129c22018-04-09 16:19:43 -07001705std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1706 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1707 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1708
1709 std::vector<Layer*> layersInTree = {this};
1710 for (size_t i = 0; i < children.size(); i++) {
1711 const auto& child = children[i];
1712 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1713 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1714 }
1715
1716 return layersInTree;
1717}
1718
1719void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1720 const LayerVector::Visitor& visitor) {
1721 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1722 std::sort(layersInTree.begin(), layersInTree.end());
1723 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1724}
1725
Peiyong Linefefaac2018-08-17 12:27:51 -07001726ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001727 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001728}
1729
chaviw13fdc492017-06-27 12:40:18 -07001730half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001731 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001732
chaviw13fdc492017-06-27 12:40:18 -07001733 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1734 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001735}
Robert Carr6452f122017-03-21 10:41:29 -07001736
chaviw13fdc492017-06-27 12:40:18 -07001737half4 Layer::getColor() const {
1738 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001739 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001740}
Robert Carr6452f122017-03-21 10:41:29 -07001741
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001742Layer::RoundedCornerState Layer::getRoundedCornerState() const {
1743 const auto& p = mDrawingParent.promote();
1744 if (p != nullptr) {
Peiyong Lin27016a92019-03-29 17:36:08 +00001745 RoundedCornerState parentState = p->getRoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001746 if (parentState.radius > 0) {
1747 ui::Transform t = getActiveTransform(getDrawingState());
1748 t = t.inverse();
1749 parentState.cropRect = t.transform(parentState.cropRect);
1750 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1751 // but a transform matrix can define horizontal and vertical scales.
1752 // Let's take the average between both of them and pass into the shader, practically we
1753 // never do this type of transformation on windows anyway.
1754 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
1755 return parentState;
1756 }
1757 }
1758 const float radius = getDrawingState().cornerRadius;
Peiyong Linb9ff23e2019-04-08 11:04:59 -07001759 return radius > 0 && getCrop(getDrawingState()).isValid()
1760 ? RoundedCornerState(getCrop(getDrawingState()).toFloatRect(), radius)
1761 : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001762}
1763
Robert Carr1f0a16a2016-10-24 16:27:39 -07001764void Layer::commitChildList() {
1765 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1766 const auto& child = mCurrentChildren[i];
1767 child->commitChildList();
1768 }
1769 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001770 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001771}
1772
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001773static wp<Layer> extractLayerFromBinder(const wp<IBinder>& weakBinderHandle) {
1774 if (weakBinderHandle == nullptr) {
1775 return nullptr;
1776 }
1777 sp<IBinder> binderHandle = weakBinderHandle.promote();
1778 if (binderHandle == nullptr) {
1779 return nullptr;
1780 }
1781 sp<Layer::Handle> handle = static_cast<Layer::Handle*>(binderHandle.get());
1782 if (handle == nullptr) {
1783 return nullptr;
1784 }
1785 return handle->owner;
1786}
1787
Robert Carr720e5062018-07-30 17:45:14 -07001788void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001789 mCurrentState.inputInfo = info;
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001790 mCurrentState.touchableRegionCrop = extractLayerFromBinder(info.touchableRegionCropHandle);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001791 mCurrentState.modified = true;
1792 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07001793 setTransactionFlags(eTransactionNeeded);
1794}
1795
Vishnu Nair8406fd72019-07-30 11:29:31 -07001796void Layer::writeToProtoDrawingState(LayerProto* layerInfo, uint32_t traceFlags) const {
1797 ui::Transform transform = getTransform();
1798
1799 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
1800 for (const auto& pendingState : mPendingStatesSnapshot) {
1801 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
1802 if (barrierLayer != nullptr) {
1803 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
1804 barrierLayerProto->set_id(barrierLayer->sequence);
1805 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
1806 }
1807 }
1808
1809 auto buffer = mActiveBuffer;
1810 if (buffer != nullptr) {
1811 LayerProtoHelper::writeToProto(buffer,
1812 [&]() { return layerInfo->mutable_active_buffer(); });
1813 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
1814 layerInfo->mutable_buffer_transform());
1815 }
1816 layerInfo->set_invalidate(contentDirty);
1817 layerInfo->set_is_protected(isProtected());
1818 layerInfo->set_dataspace(
1819 dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
1820 layerInfo->set_queued_frames(getQueuedFrameCount());
1821 layerInfo->set_refresh_pending(isBufferLatched());
1822 layerInfo->set_curr_frame(mCurrentFrameNumber);
1823 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
1824
1825 layerInfo->set_corner_radius(getRoundedCornerState().radius);
1826 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1827 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
1828 [&]() { return layerInfo->mutable_position(); });
1829 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
Lloyd Piquea2468662019-03-07 21:31:06 -08001830 LayerProtoHelper::writeToProto(debugGetVisibleRegionOnDefaultDisplay(),
Vishnu Nair8406fd72019-07-30 11:29:31 -07001831 [&]() { return layerInfo->mutable_visible_region(); });
1832 LayerProtoHelper::writeToProto(surfaceDamageRegion,
1833 [&]() { return layerInfo->mutable_damage_region(); });
1834 }
1835
1836 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
1837 LayerProtoHelper::writeToProto(mSourceBounds,
1838 [&]() { return layerInfo->mutable_source_bounds(); });
1839 LayerProtoHelper::writeToProto(mScreenBounds,
1840 [&]() { return layerInfo->mutable_screen_bounds(); });
1841 }
1842}
1843
1844void Layer::writeToProtoCommonState(LayerProto* layerInfo, LayerVector::StateSet stateSet,
1845 uint32_t traceFlags) const {
chaviw1d044282017-09-27 12:19:28 -07001846 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1847 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001848 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07001849
Peiyong Linefefaac2018-08-17 12:27:51 -07001850 ui::Transform requestedTransform = state.active_legacy.transform;
chaviw1d044282017-09-27 12:19:28 -07001851
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001852 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
1853 layerInfo->set_id(sequence);
1854 layerInfo->set_name(getName().c_str());
chaviw8a01fa42019-08-19 12:39:31 -07001855 layerInfo->set_type(getType());
chaviw1d044282017-09-27 12:19:28 -07001856
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001857 for (const auto& child : children) {
1858 layerInfo->add_children(child->sequence);
chaviw1d044282017-09-27 12:19:28 -07001859 }
chaviw1d044282017-09-27 12:19:28 -07001860
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001861 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1862 sp<Layer> strongRelative = weakRelative.promote();
1863 if (strongRelative != nullptr) {
1864 layerInfo->add_relatives(strongRelative->sequence);
1865 }
chaviwadc40c22018-07-10 16:57:27 -07001866 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001867
1868 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
1869 [&]() { return layerInfo->mutable_transparent_region(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001870
1871 layerInfo->set_layer_stack(getLayerStack());
1872 layerInfo->set_z(state.z);
1873
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001874 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(),
1875 [&]() {
1876 return layerInfo->mutable_requested_position();
1877 });
1878
1879 LayerProtoHelper::writeSizeToProto(state.active_legacy.w, state.active_legacy.h,
1880 [&]() { return layerInfo->mutable_size(); });
1881
1882 LayerProtoHelper::writeToProto(state.crop_legacy,
1883 [&]() { return layerInfo->mutable_crop(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001884
1885 layerInfo->set_is_opaque(isOpaque(state));
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001886
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001887
1888 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1889 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
1890 LayerProtoHelper::writeToProto(state.color,
1891 [&]() { return layerInfo->mutable_requested_color(); });
1892 layerInfo->set_flags(state.flags);
1893
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001894 LayerProtoHelper::writeToProto(requestedTransform,
1895 layerInfo->mutable_requested_transform());
1896
1897 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
1898 if (parent != nullptr) {
1899 layerInfo->set_parent(parent->sequence);
1900 } else {
1901 layerInfo->set_parent(-1);
1902 }
1903
1904 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1905 if (zOrderRelativeOf != nullptr) {
1906 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1907 } else {
1908 layerInfo->set_z_order_relative_of(-1);
1909 }
chaviwadc40c22018-07-10 16:57:27 -07001910 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001911
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001912 if (traceFlags & SurfaceTracing::TRACE_INPUT) {
1913 LayerProtoHelper::writeToProto(state.inputInfo, state.touchableRegionCrop,
1914 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001915 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001916
1917 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
1918 auto protoMap = layerInfo->mutable_metadata();
1919 for (const auto& entry : state.metadata.mMap) {
1920 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
1921 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001922 }
chaviw1d044282017-09-27 12:19:28 -07001923}
1924
Robert Carr2e102c92018-10-23 12:11:15 -07001925bool Layer::isRemovedFromCurrentState() const {
1926 return mRemovedFromCurrentState;
1927}
1928
Arthur Hungd20b2702019-01-14 18:16:16 +08001929InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001930 InputWindowInfo info = mDrawingState.inputInfo;
Robert Carr720e5062018-07-30 17:45:14 -07001931
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001932 if (info.displayId == ADISPLAY_ID_NONE) {
chaviwb15ea8a2019-09-03 12:40:22 -07001933 info.displayId = getLayerStack();
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001934 }
1935
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001936 ui::Transform t = getTransform();
1937 const float xScale = t.sx();
1938 const float yScale = t.sy();
Ady Abraham282f1d72019-07-24 18:05:56 -07001939 int32_t xSurfaceInset = info.surfaceInset;
1940 int32_t ySurfaceInset = info.surfaceInset;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001941 if (xScale != 1.0f || yScale != 1.0f) {
Ady Abraham282f1d72019-07-24 18:05:56 -07001942 info.windowXScale *= (xScale != 0.0f) ? 1.0f / xScale : 0.0f;
1943 info.windowYScale *= (yScale != 0.0f) ? 1.0f / yScale : 0.0f;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001944 info.touchableRegion.scaleSelf(xScale, yScale);
Ady Abraham282f1d72019-07-24 18:05:56 -07001945 xSurfaceInset = std::round(xSurfaceInset * xScale);
1946 ySurfaceInset = std::round(ySurfaceInset * yScale);
Riddle Hsu39d4aa52018-11-30 20:46:53 +08001947 }
Robert Carre07e1032018-11-26 12:55:53 -08001948
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001949 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001950 // If this is a portal window, set the touchableRegion to the layerBounds.
1951 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
1952 ? getBufferSize(getDrawingState())
1953 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08001954 if (!layerBounds.isValid()) {
1955 layerBounds = getCroppedBufferSize(getDrawingState());
1956 }
Vishnu Nair8033a492018-12-05 07:27:23 -08001957 layerBounds = t.transform(layerBounds);
Ady Abraham282f1d72019-07-24 18:05:56 -07001958
1959 // clamp inset to layer bounds
1960 xSurfaceInset = (xSurfaceInset >= 0) ? std::min(xSurfaceInset, layerBounds.getWidth() / 2) : 0;
1961 ySurfaceInset = (ySurfaceInset >= 0) ? std::min(ySurfaceInset, layerBounds.getHeight() / 2) : 0;
1962
Arthur Hung118b1142019-05-08 21:25:59 +08001963 layerBounds.inset(xSurfaceInset, ySurfaceInset, xSurfaceInset, ySurfaceInset);
Vishnu Nair8033a492018-12-05 07:27:23 -08001964
Arthur Hungd20b2702019-01-14 18:16:16 +08001965 // Input coordinate should match the layer bounds.
1966 info.frameLeft = layerBounds.left;
1967 info.frameTop = layerBounds.top;
1968 info.frameRight = layerBounds.right;
1969 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08001970
1971 // Position the touchable region relative to frame screen location and restrict it to frame
1972 // bounds.
1973 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08001974 info.visible = canReceiveInput();
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001975
1976 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
1977 if (info.replaceTouchableRegionWithCrop) {
1978 if (cropLayer == nullptr) {
1979 info.touchableRegion = Region(Rect{mScreenBounds});
1980 } else {
1981 info.touchableRegion = Region(Rect{cropLayer->mScreenBounds});
1982 }
1983 } else if (cropLayer != nullptr) {
1984 info.touchableRegion = info.touchableRegion.intersect(Rect{cropLayer->mScreenBounds});
1985 }
1986
Robert Carr720e5062018-07-30 17:45:14 -07001987 return info;
1988}
1989
1990bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001991 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07001992}
1993
Lloyd Piquefeb73d72018-12-04 17:23:44 -08001994std::shared_ptr<compositionengine::Layer> Layer::getCompositionLayer() const {
1995 return nullptr;
1996}
1997
chaviw3e727cd2019-01-31 13:41:05 -08001998bool Layer::canReceiveInput() const {
1999 return isVisible();
2000}
2001
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002002compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
2003 const sp<const DisplayDevice>& display) const {
2004 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionLayer().get());
2005}
2006
Lloyd Piquea2468662019-03-07 21:31:06 -08002007Region Layer::debugGetVisibleRegionOnDefaultDisplay() const {
2008 sp<DisplayDevice> displayDevice = mFlinger->getDefaultDisplayDeviceLocked();
2009 if (displayDevice == nullptr) {
2010 return {};
2011 }
2012
2013 auto outputLayer = findOutputLayerForDisplay(displayDevice);
2014 if (outputLayer == nullptr) {
2015 return {};
2016 }
2017
2018 return outputLayer->getState().visibleRegion;
2019}
2020
Mathias Agopian13127d82013-03-05 17:47:11 -08002021// ---------------------------------------------------------------------------
2022
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002023}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002024
2025#if defined(__gl_h_)
2026#error "don't include gl/gl.h in this file"
2027#endif
2028
2029#if defined(__gl2_h_)
2030#error "don't include gl2/gl2.h in this file"
2031#endif