blob: 974488da62c8904fc751c294e47d913f9af86d8f [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 Piquec6687342019-03-07 21:34:57 -0800420void Layer::latchBasicGeometry(compositionengine::LayerFECompositionState& compositionState) const {
421 const auto& drawingState{getDrawingState()};
422 const uint32_t layerStack = getLayerStack();
423 const auto alpha = static_cast<float>(getAlpha());
424 const bool opaque = isOpaque(drawingState);
425 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
426
427 auto blendMode = Hwc2::IComposerClient::BlendMode::NONE;
428 if (!opaque || alpha != 1.0f) {
429 blendMode = mPremultipliedAlpha ? Hwc2::IComposerClient::BlendMode::PREMULTIPLIED
430 : Hwc2::IComposerClient::BlendMode::COVERAGE;
431 }
432
433 // TODO(b/121291683): Instead of filling in a passed-in compositionState
434 // structure, switch to Layer owning the structure and have
435 // CompositionEngine be able to get a reference to it.
436
437 compositionState.layerStackId =
438 (layerStack != ~0u) ? std::make_optional(layerStack) : std::nullopt;
439 compositionState.internalOnly = getPrimaryDisplayOnly();
440 compositionState.isVisible = isVisible();
441 compositionState.isOpaque = opaque && !usesRoundedCorners && alpha == 1.f;
442
443 compositionState.contentDirty = contentDirty;
444 contentDirty = false;
445
446 compositionState.geomLayerBounds = mBounds;
447 compositionState.geomLayerTransform = getTransform();
448 compositionState.geomInverseLayerTransform = compositionState.geomLayerTransform.inverse();
449 compositionState.transparentRegionHint = getActiveTransparentRegion(drawingState);
450
451 compositionState.blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
452 compositionState.alpha = alpha;
453}
454
Lloyd Piquea83776c2019-01-29 18:42:32 -0800455void Layer::latchGeometry(compositionengine::LayerFECompositionState& compositionState) const {
456 const auto& drawingState{getDrawingState()};
Mathias Agopian13127d82013-03-05 17:47:11 -0800457
Lloyd Piquea83776c2019-01-29 18:42:32 -0800458 int type = drawingState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
459 int appId = drawingState.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700460 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400461 if (parent.get()) {
462 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800463 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
464 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
Jiwen 'Steve' Cai736c74e2019-05-28 18:33:22 -0700465 if (parentType > 0 && parentAppId > 0) {
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800466 type = parentType;
467 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700468 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400469 }
470
Lloyd Piquea83776c2019-01-29 18:42:32 -0800471 compositionState.geomBufferSize = getBufferSize(drawingState);
472 compositionState.geomContentCrop = getContentCrop();
473 compositionState.geomCrop = getCrop(drawingState);
474 compositionState.geomBufferTransform = mCurrentTransform;
475 compositionState.geomBufferUsesDisplayInverseTransform = getTransformToDisplayInverse();
Lloyd Piquea83776c2019-01-29 18:42:32 -0800476 compositionState.geomUsesSourceCrop = usesSourceCrop();
477 compositionState.isSecure = isSecure();
David Sodman15094112018-10-11 09:39:37 -0700478
Lloyd Piquea83776c2019-01-29 18:42:32 -0800479 compositionState.type = type;
480 compositionState.appId = appId;
481}
David Sodmanba340492018-08-05 21:51:33 -0700482
Lloyd Piquef5275482019-01-29 18:42:42 -0800483void Layer::latchPerFrameState(compositionengine::LayerFECompositionState& compositionState) const {
Lloyd Pique688abd42019-02-15 15:42:24 -0800484 const auto& drawingState{getDrawingState()};
Lloyd Piquef5275482019-01-29 18:42:42 -0800485 compositionState.forceClientComposition = false;
486
Lloyd Piquef5275482019-01-29 18:42:42 -0800487 compositionState.isColorspaceAgnostic = isColorSpaceAgnostic();
488 compositionState.dataspace = mCurrentDataSpace;
489 compositionState.colorTransform = getColorTransform();
490 compositionState.colorTransformIsIdentity = !hasColorTransform();
491 compositionState.surfaceDamage = surfaceDamageRegion;
Lloyd Pique688abd42019-02-15 15:42:24 -0800492 compositionState.hasProtectedContent = isProtected();
493
494 const bool usesRoundedCorners = getRoundedCornerState().radius != 0.f;
495 compositionState.isOpaque =
496 isOpaque(drawingState) && !usesRoundedCorners && getAlpha() == 1.0_hf;
Lloyd Piquef5275482019-01-29 18:42:42 -0800497
498 // Force client composition for special cases known only to the front-end.
Lloyd Pique688abd42019-02-15 15:42:24 -0800499 if (isHdrY410() || usesRoundedCorners) {
Lloyd Piquef5275482019-01-29 18:42:42 -0800500 compositionState.forceClientComposition = true;
501 }
502}
503
Lloyd Piquec7b0c752019-03-07 20:59:59 -0800504void Layer::latchCursorCompositionState(
505 compositionengine::LayerFECompositionState& compositionState) const {
506 // This gives us only the "orientation" component of the transform
507 const State& drawingState{getDrawingState()};
508
509 // Apply the layer's transform, followed by the display's global transform
510 // Here we're guaranteed that the layer's transform preserves rects
511 Rect win = getCroppedBufferSize(drawingState);
512 // Subtract the transparent region and snap to the bounds
513 Rect bounds = reduce(win, getActiveTransparentRegion(drawingState));
514 Rect frame(getTransform().transform(bounds));
515
516 compositionState.cursorFrame = frame;
517}
518
Lloyd Piquef16688f2019-02-19 17:47:57 -0800519bool Layer::onPreComposition(nsecs_t) {
520 return false;
521}
522
Lloyd Piquea83776c2019-01-29 18:42:32 -0800523void Layer::latchCompositionState(compositionengine::LayerFECompositionState& compositionState,
Lloyd Piquec6687342019-03-07 21:34:57 -0800524 compositionengine::LayerFE::StateSubset subset) const {
525 using StateSubset = compositionengine::LayerFE::StateSubset;
Lloyd Piquef5275482019-01-29 18:42:42 -0800526
Lloyd Piquec6687342019-03-07 21:34:57 -0800527 switch (subset) {
528 case StateSubset::BasicGeometry:
529 latchBasicGeometry(compositionState);
530 break;
531
532 case StateSubset::GeometryAndContent:
533 latchBasicGeometry(compositionState);
534 latchGeometry(compositionState);
535 latchPerFrameState(compositionState);
536 break;
537
538 case StateSubset::Content:
539 latchPerFrameState(compositionState);
540 break;
541 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800542}
Mathias Agopian29a367b2011-07-12 14:51:45 -0700543
Lloyd Piquea83776c2019-01-29 18:42:32 -0800544const char* Layer::getDebugName() const {
545 return mName.string();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800546}
547
Mathias Agopian13127d82013-03-05 17:47:11 -0800548// ---------------------------------------------------------------------------
549// drawing...
550// ---------------------------------------------------------------------------
551
Lloyd Piquef16688f2019-02-19 17:47:57 -0800552std::optional<renderengine::LayerSettings> Layer::prepareClientComposition(
553 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
554 if (!getCompositionLayer()) {
555 return {};
556 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800557
Vishnu Nair4351ad52019-02-11 14:13:02 -0800558 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000559 half alpha = getAlpha();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800560 renderengine::LayerSettings layerSettings;
561 layerSettings.geometry.boundaries = bounds;
562 if (targetSettings.useIdentityTransform) {
563 layerSettings.geometry.positionTransform = mat4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000564 } else {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800565 layerSettings.geometry.positionTransform = getTransform().asMatrix4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000566 }
567
568 if (hasColorTransform()) {
Lloyd Piquef16688f2019-02-19 17:47:57 -0800569 layerSettings.colorTransform = getColorTransform();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000570 }
571
572 const auto roundedCornerState = getRoundedCornerState();
Lloyd Piquef16688f2019-02-19 17:47:57 -0800573 layerSettings.geometry.roundedCornersRadius = roundedCornerState.radius;
574 layerSettings.geometry.roundedCornersCrop = roundedCornerState.cropRect;
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000575
Lloyd Piquef16688f2019-02-19 17:47:57 -0800576 layerSettings.alpha = alpha;
577 layerSettings.sourceDataspace = mCurrentDataSpace;
578 return layerSettings;
Mathias Agopian13127d82013-03-05 17:47:11 -0800579}
580
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800581Hwc2::IComposerClient::Composition Layer::getCompositionType(
582 const sp<const DisplayDevice>& display) const {
583 const auto outputLayer = findOutputLayerForDisplay(display);
584 LOG_FATAL_IF(!outputLayer);
585 return outputLayer->getState().hwc ? (*outputLayer->getState().hwc).hwcCompositionType
586 : Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800587}
588
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800589bool Layer::getClearClientTarget(const sp<const DisplayDevice>& display) const {
590 const auto outputLayer = findOutputLayerForDisplay(display);
591 LOG_FATAL_IF(!outputLayer);
592 return outputLayer->getState().clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800593}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800594
Dan Stozacac35382016-01-27 12:21:06 -0800595bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
596 if (point->getFrameNumber() <= mCurrentFrameNumber) {
597 // Don't bother with a SyncPoint, since we've already latched the
598 // relevant frame
599 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700600 }
Robert Carr2e102c92018-10-23 12:11:15 -0700601 if (isRemovedFromCurrentState()) {
602 return false;
603 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700604
Dan Stozacac35382016-01-27 12:21:06 -0800605 Mutex::Autolock lock(mLocalSyncPointMutex);
606 mLocalSyncPoints.push_back(point);
607 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700608}
609
Mathias Agopian13127d82013-03-05 17:47:11 -0800610// ----------------------------------------------------------------------------
611// local state
612// ----------------------------------------------------------------------------
613
David Sodman41fdfc92017-11-06 16:09:56 -0800614bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800615 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700616 return (s.flags & layer_state_t::eLayerSecure);
617}
618
Mathias Agopian13127d82013-03-05 17:47:11 -0800619// ----------------------------------------------------------------------------
620// transaction
621// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800622
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800623void Layer::pushPendingState() {
624 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700625 return;
626 }
Alec Mourie60041e2019-06-14 18:59:51 -0700627 ATRACE_CALL();
Dan Stoza7dde5992015-05-22 09:51:44 -0700628
Dan Stoza7dde5992015-05-22 09:51:44 -0700629 // If this transaction is waiting on the receipt of a frame, generate a sync
630 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700631 // We don't allow installing sync points after we are removed from the current state
632 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800633 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
634 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800635 if (barrierLayer == nullptr) {
636 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700637 // If we can't promote the layer we are intended to wait on,
638 // then it is expired or otherwise invalid. Allow this transaction
639 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800640 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800641 } else {
chaviw43cb3cb2019-05-31 15:23:41 -0700642 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy, this);
Robert Carr0d480722017-01-10 16:42:54 -0800643 if (barrierLayer->addSyncPoint(syncPoint)) {
Alec Mourie60041e2019-06-14 18:59:51 -0700644 std::stringstream ss;
645 ss << "Adding sync point " << mCurrentState.frameNumber_legacy;
646 ATRACE_NAME(ss.str().c_str());
Dan Stozacac35382016-01-27 12:21:06 -0800647 mRemoteSyncPoints.push_back(std::move(syncPoint));
648 } else {
649 // We already missed the frame we're supposed to synchronize
650 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800651 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800652 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700653 }
654
Dan Stoza7dde5992015-05-22 09:51:44 -0700655 // Wake us up to check if the frame has been received
656 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700657 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700658 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800659 mPendingStates.push_back(mCurrentState);
660 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700661}
662
Pablo Ceballos05289c22016-04-14 15:49:55 -0700663void Layer::popPendingState(State* stateToCommit) {
Alec Mourie60041e2019-06-14 18:59:51 -0700664 ATRACE_CALL();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800665 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700666
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800667 mPendingStates.removeAt(0);
668 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700669}
670
Pablo Ceballos05289c22016-04-14 15:49:55 -0700671bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700672 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800673 while (!mPendingStates.empty()) {
674 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700675 if (mRemoteSyncPoints.empty()) {
676 // If we don't have a sync point for this, apply it anyway. It
677 // will be visually wrong, but it should keep us from getting
678 // into too much trouble.
679 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700680 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700681 stateUpdateAvailable = true;
682 continue;
683 }
684
Marissa Wallf58c14b2018-07-24 10:50:43 -0700685 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800686 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800687 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800688
689 // Signal our end of the sync point and then dispose of it
690 mRemoteSyncPoints.front()->setTransactionApplied();
691 mRemoteSyncPoints.pop_front();
692 continue;
693 }
694
Dan Stoza7dde5992015-05-22 09:51:44 -0700695 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
Alec Mourie60041e2019-06-14 18:59:51 -0700696 ATRACE_NAME("frameIsAvailable");
Dan Stoza7dde5992015-05-22 09:51:44 -0700697 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700698 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700699 stateUpdateAvailable = true;
700
701 // Signal our end of the sync point and then dispose of it
702 mRemoteSyncPoints.front()->setTransactionApplied();
703 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800704 } else {
Alec Mourie60041e2019-06-14 18:59:51 -0700705 ATRACE_NAME("!frameIsAvailable");
Dan Stoza792e5292016-02-11 11:43:58 -0800706 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700707 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700708 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700709 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700710 stateUpdateAvailable = true;
711 }
712 }
713
714 // If we still have pending updates, wake SurfaceFlinger back up and point
715 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800716 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700717 setTransactionFlags(eTransactionNeeded);
718 mFlinger->setTransactionFlags(eTraversalNeeded);
719 }
720
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800721 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700722 return stateUpdateAvailable;
723}
724
Marissa Wall61c58622018-07-18 10:12:20 -0700725uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000726 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800727
Marissa Wall61c58622018-07-18 10:12:20 -0700728 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
729 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700730
David Sodmaneb085e02017-10-05 18:49:04 -0700731 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700732 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000733 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800734 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
735 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
736 " requested={ wh={%4u,%4u} }}\n"
737 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
738 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700739 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700740 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
741 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
742 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
743 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
744 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700745 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
746 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
747 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800748 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700749
Robert Carre392b552017-09-19 12:16:05 -0700750 // Don't let Layer::doTransaction update the drawing state
751 // if we have a pending resize, unless we are in fixed-size mode.
752 // the drawing state will be updated only once we receive a buffer
753 // with the correct size.
754 //
755 // In particular, we want to make sure the clip (which is part
756 // of the geometry state) is latched together with the size but is
757 // latched immediately when no resizing is involved.
758 //
759 // If a sideband stream is attached, however, we want to skip this
760 // optimization so that transactions aren't missed when a buffer
761 // never arrives
762 //
763 // In the case that we don't have a buffer we ignore other factors
764 // and avoid entering the resizePending state. At a high level the
765 // resizePending state is to avoid applying the state of the new buffer
766 // to the old buffer. However in the state where we don't have an old buffer
767 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700768 const bool resizePending =
769 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
770 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
Lloyd Pique0b785d82018-12-04 17:25:27 -0800771 (mActiveBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700772 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800773 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700774 flags |= eDontUpdateGeometryState;
775 }
776 }
777
Robert Carr7bf247e2017-05-18 14:02:49 -0700778 // Here we apply various requested geometry states, depending on our
779 // latching configuration. See Layer.h for a detailed discussion of
780 // how geometry latching is controlled.
781 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000782 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700783
784 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
785 // mode, which causes attributes which normally latch regardless of scaling mode,
786 // to be delayed. We copy the requested state to the active state making sure
787 // to respect these rules (again see Layer.h for a detailed discussion).
788 //
789 // There is an awkward asymmetry in the handling of the crop states in the position
790 // states, as can be seen below. Largely this arises from position and transform
791 // being stored in the same data structure while having different latching rules.
792 // b/38182305
793 //
Marissa Wall61c58622018-07-18 10:12:20 -0700794 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700795 // applyPendingStates in the presence of deferred transactions.
796 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -0700797 float tx = stateToCommit->active_legacy.transform.tx();
798 float ty = stateToCommit->active_legacy.transform.ty();
799 stateToCommit->active_legacy = stateToCommit->requested_legacy;
800 stateToCommit->active_legacy.transform.set(tx, ty);
801 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700802 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700803 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -0700804 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700805 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800806 }
807
Marissa Wall61c58622018-07-18 10:12:20 -0700808 return flags;
809}
810
811uint32_t Layer::doTransaction(uint32_t flags) {
812 ATRACE_CALL();
813
chaviw5aedec92018-10-22 10:40:38 -0700814 if (mLayerDetached) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800815 return flags;
816 }
817
818 if (mChildrenChanged) {
819 flags |= eVisibleRegion;
820 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -0700821 }
822
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800823 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +0000824 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -0700825 if (!applyPendingStates(&c)) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800826 return flags;
Marissa Wall61c58622018-07-18 10:12:20 -0700827 }
828
829 flags = doTransactionResize(flags, &c);
830
Alec Mourib416efd2018-09-06 21:01:59 +0000831 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700832
833 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800834 // invalidate and recompute the visible regions if needed
835 flags |= Layer::eVisibleRegion;
836 }
837
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700838 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800839 // invalidate and recompute the visible regions if needed
840 flags |= eVisibleRegion;
841 this->contentDirty = true;
842
843 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -0700844 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -0700845 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -0800846 }
847
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800848 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -0700849 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800850 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -0700851 }
852
Mathias Agopian13127d82013-03-05 17:47:11 -0800853 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -0700854 commitTransaction(c);
Vishnu Nair8406fd72019-07-30 11:29:31 -0700855 mPendingStatesSnapshot = mPendingStates;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800856 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -0800857 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800858}
859
Pablo Ceballos05289c22016-04-14 15:49:55 -0700860void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800861 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700862}
863
Mathias Agopian13127d82013-03-05 17:47:11 -0800864uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800865 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800866}
867
868uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800869 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -0800870}
871
Robert Carr82364e32016-05-15 11:27:47 -0700872bool Layer::setPosition(float x, float y, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800873 if (mCurrentState.requested_legacy.transform.tx() == x &&
874 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -0800875 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800876 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -0700877
878 // We update the requested and active position simultaneously because
879 // we want to apply the position portion of the transform matrix immediately,
880 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800881 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -0700882 if (immediate && !mFreezeGeometryUpdates) {
883 // Here we directly update the active state
884 // unlike other setters, because we store it within
885 // the transform, but use different latching rules.
886 // b/38182305
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800887 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -0700888 }
Robert Carr7bf247e2017-05-18 14:02:49 -0700889 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -0700890
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800891 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800892 setTransactionFlags(eTransactionNeeded);
893 return true;
894}
Robert Carr82364e32016-05-15 11:27:47 -0700895
Robert Carr1f0a16a2016-10-24 16:27:39 -0700896bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
897 ssize_t idx = mCurrentChildren.indexOf(childLayer);
898 if (idx < 0) {
899 return false;
900 }
901 if (childLayer->setLayer(z)) {
902 mCurrentChildren.removeAt(idx);
903 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800904 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700905 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800906 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700907}
908
Robert Carr503c7042017-09-27 15:06:08 -0700909bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
910 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
911 ssize_t idx = mCurrentChildren.indexOf(childLayer);
912 if (idx < 0) {
913 return false;
914 }
915 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
916 mCurrentChildren.removeAt(idx);
917 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800918 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700919 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800920 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700921}
922
Robert Carrae060832016-11-28 10:51:00 -0800923bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800924 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
925 mCurrentState.sequence++;
926 mCurrentState.z = z;
927 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700928
929 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800930 if (mCurrentState.zOrderRelativeOf != nullptr) {
931 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700932 if (strongRelative != nullptr) {
933 strongRelative->removeZOrderRelative(this);
934 }
chaviw606e5cf2019-03-01 10:12:10 -0800935 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -0700936 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800937 setTransactionFlags(eTransactionNeeded);
938 return true;
939}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700940
Robert Carrdb66e622017-04-10 16:55:57 -0700941void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800942 mCurrentState.zOrderRelatives.remove(relative);
943 mCurrentState.sequence++;
944 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700945 setTransactionFlags(eTransactionNeeded);
946}
947
948void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800949 mCurrentState.zOrderRelatives.add(relative);
950 mCurrentState.modified = true;
951 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -0700952 setTransactionFlags(eTransactionNeeded);
953}
954
chaviw606e5cf2019-03-01 10:12:10 -0800955void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
956 mCurrentState.zOrderRelativeOf = relativeOf;
957 mCurrentState.sequence++;
958 mCurrentState.modified = true;
959 setTransactionFlags(eTransactionNeeded);
960}
961
Robert Carr503d2bd2017-12-04 15:49:47 -0800962bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -0700963 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
964 if (handle == nullptr) {
965 return false;
966 }
967 sp<Layer> relative = handle->owner.promote();
968 if (relative == nullptr) {
969 return false;
970 }
971
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800972 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
973 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -0800974 return false;
975 }
976
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800977 mCurrentState.sequence++;
978 mCurrentState.modified = true;
979 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -0700980
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800981 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -0700982 if (oldZOrderRelativeOf != nullptr) {
983 oldZOrderRelativeOf->removeZOrderRelative(this);
984 }
chaviw606e5cf2019-03-01 10:12:10 -0800985 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -0700986 relative->addZOrderRelative(this);
987
988 setTransactionFlags(eTransactionNeeded);
989
990 return true;
991}
992
Mathias Agopian13127d82013-03-05 17:47:11 -0800993bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800994 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -0700995 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800996 mCurrentState.requested_legacy.w = w;
997 mCurrentState.requested_legacy.h = h;
998 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800999 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001000
1001 // record the new size, from this point on, when the client request
1002 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001003 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001004 return true;
1005}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001006bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001007 if (mCurrentState.color.a == alpha) return false;
1008 mCurrentState.sequence++;
1009 mCurrentState.color.a = alpha;
1010 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001011 setTransactionFlags(eTransactionNeeded);
1012 return true;
1013}
chaviw13fdc492017-06-27 12:40:18 -07001014
Valerie Haudd0b7572019-01-29 14:59:27 -08001015bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1016 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001017 return false;
Valerie Hauaa194562019-02-05 16:21:38 -08001018 }
1019 mCurrentState.sequence++;
1020 mCurrentState.modified = true;
1021 setTransactionFlags(eTransactionNeeded);
1022
1023 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001024 // create background color layer if one does not yet exist
1025 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
1026 const String8& name = mName + "BackgroundColorLayer";
Evan Roskya1f1e152019-01-24 16:17:46 -08001027 mCurrentState.bgColorLayer = new ColorLayer(
1028 LayerCreationArgs(mFlinger.get(), nullptr, name, 0, 0, flags, LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -07001029
Valerie Haudd0b7572019-01-29 14:59:27 -08001030 // add to child list
1031 addChild(mCurrentState.bgColorLayer);
1032 mFlinger->mLayersAdded = true;
1033 // set up SF to handle added color layer
1034 if (isRemovedFromCurrentState()) {
1035 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1036 }
1037 mFlinger->setTransactionFlags(eTransactionNeeded);
1038 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1039 mCurrentState.bgColorLayer->reparent(nullptr);
1040 mCurrentState.bgColorLayer = nullptr;
1041 return true;
1042 }
1043
1044 mCurrentState.bgColorLayer->setColor(color);
1045 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1046 mCurrentState.bgColorLayer->setAlpha(alpha);
1047 mCurrentState.bgColorLayer->setDataspace(dataspace);
1048
chaviw13fdc492017-06-27 12:40:18 -07001049 return true;
1050}
1051
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001052bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001053 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001054
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001055 mCurrentState.sequence++;
1056 mCurrentState.cornerRadius = cornerRadius;
1057 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001058 setTransactionFlags(eTransactionNeeded);
1059 return true;
1060}
1061
Robert Carrd4ae7f32018-06-07 16:10:57 -07001062bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1063 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001064 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001065 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1066
1067 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1068 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1069 return false;
1070 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001071 mCurrentState.sequence++;
1072 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1073 matrix.dsdy);
1074 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001075 setTransactionFlags(eTransactionNeeded);
1076 return true;
1077}
Marissa Wall61c58622018-07-18 10:12:20 -07001078
Mathias Agopian13127d82013-03-05 17:47:11 -08001079bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001080 mCurrentState.requestedTransparentRegion_legacy = transparent;
1081 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001082 setTransactionFlags(eTransactionNeeded);
1083 return true;
1084}
1085bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001086 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1087 if (mCurrentState.flags == newFlags) return false;
1088 mCurrentState.sequence++;
1089 mCurrentState.flags = newFlags;
1090 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001091 setTransactionFlags(eTransactionNeeded);
1092 return true;
1093}
Robert Carr99e27f02016-06-16 15:18:02 -07001094
Marissa Wallf58c14b2018-07-24 10:50:43 -07001095bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001096 if (mCurrentState.requestedCrop_legacy == crop) return false;
1097 mCurrentState.sequence++;
1098 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001099 if (immediate && !mFreezeGeometryUpdates) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001100 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001101 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001102 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1103
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001104 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001105 setTransactionFlags(eTransactionNeeded);
1106 return true;
1107}
Robert Carr8d5227b2017-03-16 15:41:03 -07001108
Robert Carrc3574f72016-03-24 12:19:32 -07001109bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001110 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001111 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001112 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001113 return true;
1114}
1115
Evan Roskyef876c92019-01-25 17:46:06 -08001116bool Layer::setMetadata(const LayerMetadata& data) {
1117 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001118 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001119 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001120 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001121 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001122}
1123
Mathias Agopian13127d82013-03-05 17:47:11 -08001124bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001125 if (mCurrentState.layerStack == layerStack) return false;
1126 mCurrentState.sequence++;
1127 mCurrentState.layerStack = layerStack;
1128 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001129 setTransactionFlags(eTransactionNeeded);
1130 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001131}
1132
Peiyong Linc502cb72019-03-01 15:00:23 -08001133bool Layer::setColorSpaceAgnostic(const bool agnostic) {
1134 if (mCurrentState.colorSpaceAgnostic == agnostic) {
1135 return false;
1136 }
1137 mCurrentState.sequence++;
1138 mCurrentState.colorSpaceAgnostic = agnostic;
1139 mCurrentState.modified = true;
1140 setTransactionFlags(eTransactionNeeded);
1141 return true;
1142}
1143
Robert Carr1f0a16a2016-10-24 16:27:39 -07001144uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001145 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001146 if (p == nullptr) {
1147 return getDrawingState().layerStack;
1148 }
1149 return p->getLayerStack();
1150}
1151
Marissa Wallf58c14b2018-07-24 10:50:43 -07001152void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Alec Mourie60041e2019-06-14 18:59:51 -07001153 ATRACE_CALL();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001154 mCurrentState.barrierLayer_legacy = barrierLayer;
1155 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001156 // We don't set eTransactionNeeded, because just receiving a deferral
1157 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001158 mCurrentState.modified = true;
1159 pushPendingState();
1160 mCurrentState.barrierLayer_legacy = nullptr;
1161 mCurrentState.frameNumber_legacy = 0;
1162 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001163}
1164
Marissa Wallf58c14b2018-07-24 10:50:43 -07001165void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001166 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001167 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001168}
1169
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001170// ----------------------------------------------------------------------------
1171// pageflip handling...
1172// ----------------------------------------------------------------------------
1173
Robert Carr1f0a16a2016-10-24 16:27:39 -07001174bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001175 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001176 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001177 if (parent != nullptr && parent->isHiddenByPolicy()) {
1178 return true;
1179 }
Robert Carr1c5481e2019-07-01 14:42:27 -07001180 if (usingRelativeZ(LayerVector::StateSet::Drawing)) {
1181 auto zOrderRelativeOf = mDrawingState.zOrderRelativeOf.promote();
1182 if (zOrderRelativeOf != nullptr) {
1183 if (zOrderRelativeOf->isHiddenByPolicy()) {
1184 return true;
1185 }
1186 }
1187 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001188 return s.flags & layer_state_t::eLayerHidden;
1189}
1190
David Sodman41fdfc92017-11-06 16:09:56 -08001191uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001192 // TODO: should we do something special if mSecure is set?
1193 if (mProtectedByApp) {
1194 // need a hardware-protected path to external video sink
1195 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001196 }
Riley Andrews03414a12014-07-01 14:22:59 -07001197 if (mPotentialCursor) {
1198 usage |= GraphicBuffer::USAGE_CURSOR;
1199 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001200 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001201 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001202}
1203
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001204void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001205 uint32_t orientation = 0;
Vishnu Nair5eb3f062019-04-08 08:21:03 -07001206 // Disable setting transform hint if the debug flag is set.
1207 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001208 // The transform hint is used to improve performance, but we can
1209 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001210 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001211 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001212 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001213 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001214 orientation = 0;
1215 }
1216 }
David Sodmaneb085e02017-10-05 18:49:04 -07001217 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001218}
1219
Mathias Agopian13127d82013-03-05 17:47:11 -08001220// ----------------------------------------------------------------------------
1221// debugging
1222// ----------------------------------------------------------------------------
1223
Marissa Wall61c58622018-07-18 10:12:20 -07001224// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001225LayerDebugInfo Layer::getLayerDebugInfo() const {
1226 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001227 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001228 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001229 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001230 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
chaviw8a01fa42019-08-19 12:39:31 -07001231 info.mType = getType();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001232 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Lloyd Piquea2468662019-03-07 21:31:06 -08001233
1234 info.mVisibleRegion = debugGetVisibleRegionOnDefaultDisplay();
Kalle Raitaa099a242017-01-11 11:17:29 -08001235 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001236 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001237 info.mX = ds.active_legacy.transform.tx();
1238 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001239 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001240 info.mWidth = ds.active_legacy.w;
1241 info.mHeight = ds.active_legacy.h;
1242 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001243 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001244 info.mFlags = ds.flags;
1245 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001246 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001247 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1248 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1249 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1250 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001251 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001252 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001253 if (buffer != 0) {
1254 info.mActiveBufferWidth = buffer->getWidth();
1255 info.mActiveBufferHeight = buffer->getHeight();
1256 info.mActiveBufferStride = buffer->getStride();
1257 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001258 } else {
1259 info.mActiveBufferWidth = 0;
1260 info.mActiveBufferHeight = 0;
1261 info.mActiveBufferStride = 0;
1262 info.mActiveBufferFormat = 0;
1263 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001264 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001265 info.mNumQueuedFrames = getQueuedFrameCount();
1266 info.mRefreshPending = isBufferLatched();
1267 info.mIsOpaque = isOpaque(ds);
1268 info.mContentDirty = contentDirty;
1269 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001270}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001271
Yiwei Zhang5434a782018-12-05 18:06:32 -08001272void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001273 result.append("-------------------------------");
1274 result.append("-------------------------------");
1275 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001276 result.append(" Layer name\n");
1277 result.append(" Z | ");
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001278 result.append(" Window Type | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001279 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001280 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001281 result.append(" Disp Frame (LTRB) | ");
1282 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001283 result.append("-------------------------------");
1284 result.append("-------------------------------");
1285 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001286}
1287
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001288void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1289 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1290 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001291 return;
1292 }
1293
Yiwei Zhang5434a782018-12-05 18:06:32 -08001294 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001295 if (mName.length() > 77) {
1296 std::string shortened;
1297 shortened.append(mName.string(), 36);
1298 shortened.append("[...]");
1299 shortened.append(mName.string() + (mName.length() - 36), 36);
Yiwei Zhang5434a782018-12-05 18:06:32 -08001300 name = shortened;
Dan Stozae22aec72016-08-01 13:20:59 -07001301 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001302 name = std::string(mName.string(), mName.size());
Dan Stozae22aec72016-08-01 13:20:59 -07001303 }
1304
Yiwei Zhang5434a782018-12-05 18:06:32 -08001305 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001306
Alec Mourib416efd2018-09-06 21:01:59 +00001307 const State& layerState(getDrawingState());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001308 const auto& compositionState = outputLayer->getState();
1309
Chia-I Wu1e043612018-03-01 09:45:09 -08001310 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001311 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001312 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001313 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001314 }
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001315 StringAppendF(&result, " %10d | ", mWindowType);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001316 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
1317 StringAppendF(&result, "%10s | ",
1318 toString(getCompositionLayer() ? compositionState.bufferTransform
1319 : static_cast<Hwc2::Transform>(0))
1320 .c_str());
1321 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001322 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001323 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001324 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1325 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001326
Yichi Chen6ca35192018-05-29 12:20:43 +08001327 result.append("- - - - - - - - - - - - - - - -");
1328 result.append("- - - - - - - - - - - - - - - -");
1329 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001330}
Dan Stozae22aec72016-08-01 13:20:59 -07001331
Yiwei Zhang5434a782018-12-05 18:06:32 -08001332void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001333 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001334}
1335
Svetoslavd85084b2014-03-20 10:28:31 -07001336void Layer::clearFrameStats() {
1337 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001338}
1339
Jamie Gennis6547ff42013-07-16 20:12:42 -07001340void Layer::logFrameStats() {
1341 mFrameTracker.logAndResetStats(mName);
1342}
1343
Svetoslavd85084b2014-03-20 10:28:31 -07001344void Layer::getFrameStats(FrameStats* outStats) const {
1345 mFrameTracker.getStats(outStats);
1346}
1347
Yiwei Zhang5434a782018-12-05 18:06:32 -08001348void Layer::dumpFrameEvents(std::string& result) {
chaviw8a01fa42019-08-19 12:39:31 -07001349 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().string(), getType(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001350 Mutex::Autolock lock(mFrameEventHistoryMutex);
1351 mFrameEventHistory.checkFencesForCompletion();
1352 mFrameEventHistory.dump(result);
1353}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001354
Vishnu Nair0f085c62019-08-30 08:49:12 -07001355void Layer::dumpCallingUidPid(std::string& result) const {
1356 StringAppendF(&result, "Layer %s (%s) pid:%d uid:%d\n", getName().string(), getType(),
1357 mCallingPid, mCallingUid);
1358}
1359
Brian Anderson5ea5e592016-12-01 16:54:33 -08001360void Layer::onDisconnect() {
1361 Mutex::Autolock lock(mFrameEventHistoryMutex);
1362 mFrameEventHistory.onDisconnect();
Mikael Pessa90092f42019-08-26 17:22:04 -07001363 const int32_t layerID = getSequence();
1364 mFlinger->mTimeStats->onDestroy(layerID);
1365 mFlinger->mFrameTracer->onDestroy(layerID);
Brian Anderson5ea5e592016-12-01 16:54:33 -08001366}
1367
Brian Anderson3890c392016-07-25 12:48:08 -07001368void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001369 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001370 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001371 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1372 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001373 }
1374
Brian Andersond6927fb2016-07-23 23:37:30 -07001375 Mutex::Autolock lock(mFrameEventHistoryMutex);
1376 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001377 // If there are any unsignaled fences in the aquire timeline at this
1378 // point, the previously queued frame hasn't been latched yet. Go ahead
1379 // and try to get the signal time here so the syscall is taken out of
1380 // the main thread's critical path.
1381 mAcquireTimeline.updateSignalTimes();
1382 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001383 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001384 mFrameEventHistory.addQueue(*newTimestamps);
1385 }
1386
Brian Anderson3890c392016-07-25 12:48:08 -07001387 if (outDelta) {
1388 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001389 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001390}
Dan Stozae77c7662016-05-13 11:37:28 -07001391
Chia-I Wu98f1c102017-05-30 14:54:08 -07001392size_t Layer::getChildrenCount() const {
1393 size_t count = 0;
1394 for (const sp<Layer>& child : mCurrentChildren) {
1395 count += 1 + child->getChildrenCount();
1396 }
1397 return count;
1398}
1399
Robert Carr1f0a16a2016-10-24 16:27:39 -07001400void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001401 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001402 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001403
Robert Carr1f0a16a2016-10-24 16:27:39 -07001404 mCurrentChildren.add(layer);
1405 layer->setParent(this);
1406}
1407
1408ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001409 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001410 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001411
Robert Carr1323c952019-01-28 18:13:27 -08001412 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001413 return mCurrentChildren.remove(layer);
1414}
1415
Robert Carr1db73f62016-12-21 12:58:51 -08001416bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1417 sp<Handle> handle = nullptr;
1418 sp<Layer> newParent = nullptr;
1419 if (newParentHandle == nullptr) {
1420 return false;
1421 }
1422 handle = static_cast<Handle*>(newParentHandle.get());
1423 newParent = handle->owner.promote();
1424 if (newParent == nullptr) {
1425 ALOGE("Unable to promote Layer handle");
1426 return false;
1427 }
1428
chaviw5aedec92018-10-22 10:40:38 -07001429 if (attachChildren()) {
1430 setTransactionFlags(eTransactionNeeded);
1431 }
Robert Carr1db73f62016-12-21 12:58:51 -08001432 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001433 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001434 }
1435 mCurrentChildren.clear();
1436
1437 return true;
1438}
1439
Robert Carr15eae092018-03-23 13:43:53 -07001440void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001441 for (const sp<Layer>& child : mDrawingChildren) {
1442 child->mDrawingParent = newParent;
Vishnu Nairc652ff82019-03-15 12:48:54 -07001443 child->computeBounds(newParent->mBounds,
1444 newParent->getTransformWithScale(
1445 newParent->getBufferScaleTransform()));
Robert Carr578038f2018-03-09 12:25:24 -08001446 }
1447}
1448
chaviwf1961f72017-09-18 16:41:07 -07001449bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001450 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001451
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001452 // While layers are detached, we allow most operations
1453 // and simply halt performing the actual transaction. However
1454 // for reparent != null we would enter the mRemovedFromCurrentState
1455 // state, regardless of whether doTransaction was called, and
1456 // so we need to prevent the update here.
1457 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001458 return false;
1459 }
1460
Robert Carr54cf5b12019-01-25 14:02:28 -08001461 sp<Layer> newParent;
1462 if (newParentHandle != nullptr) {
1463 auto handle = static_cast<Handle*>(newParentHandle.get());
1464 newParent = handle->owner.promote();
1465 if (newParent == nullptr) {
1466 ALOGE("Unable to promote Layer handle");
1467 return false;
1468 }
1469 if (newParent == this) {
1470 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1471 return false;
1472 }
1473 }
1474
chaviwf1961f72017-09-18 16:41:07 -07001475 sp<Layer> parent = getParent();
1476 if (parent != nullptr) {
1477 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001478 }
1479
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001480 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001481 newParent->addChild(this);
1482 if (!newParent->isRemovedFromCurrentState()) {
1483 addToCurrentState();
1484 } else {
1485 onRemovedFromCurrentState();
1486 }
1487
1488 if (mLayerDetached) {
1489 mLayerDetached = false;
1490 callSetTransactionFlags = true;
1491 }
1492 } else {
1493 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001494 }
1495
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001496 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001497 setTransactionFlags(eTransactionNeeded);
1498 }
chaviw06178942017-07-27 10:25:59 -07001499 return true;
1500}
1501
Robert Carr9524cb32017-02-13 11:32:32 -08001502bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001503 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001504 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001505 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001506 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001507 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001508 child->detachChildren();
chaviw43cb3cb2019-05-31 15:23:41 -07001509 child->removeRemoteSyncPoints();
Robert Carr9524cb32017-02-13 11:32:32 -08001510 }
Robert Carr7f619b22017-11-06 12:56:35 -08001511 }
Robert Carr9524cb32017-02-13 11:32:32 -08001512
1513 return true;
1514}
1515
chaviw5aedec92018-10-22 10:40:38 -07001516bool Layer::attachChildren() {
1517 bool changed = false;
1518 for (const sp<Layer>& child : mCurrentChildren) {
1519 sp<Client> parentClient = mClientRef.promote();
1520 sp<Client> client(child->mClientRef.promote());
1521 if (client != nullptr && parentClient != client) {
1522 if (child->mLayerDetached) {
1523 child->mLayerDetached = false;
1524 changed = true;
1525 }
1526 changed |= child->attachChildren();
1527 }
1528 }
1529
1530 return changed;
1531}
1532
Peiyong Lind3788632018-09-18 16:01:31 -07001533bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001534 static const mat4 identityMatrix = mat4();
1535
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001536 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001537 return false;
1538 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001539 ++mCurrentState.sequence;
1540 mCurrentState.colorTransform = matrix;
1541 mCurrentState.hasColorTransform = matrix != identityMatrix;
1542 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001543 setTransactionFlags(eTransactionNeeded);
1544 return true;
1545}
1546
chaviwf66724d2018-11-28 16:35:21 -08001547mat4 Layer::getColorTransform() const {
1548 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1549 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1550 colorTransform = parent->getColorTransform() * colorTransform;
1551 }
1552 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001553}
1554
1555bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001556 bool hasColorTransform = getDrawingState().hasColorTransform;
1557 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1558 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1559 }
1560 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001561}
1562
Chia-I Wu11481472018-05-04 10:43:19 -07001563bool Layer::isLegacyDataSpace() const {
1564 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001565 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001566 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001567}
1568
Robert Carr1f0a16a2016-10-24 16:27:39 -07001569void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001570 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001571}
1572
Robert Carr1f0a16a2016-10-24 16:27:39 -07001573int32_t Layer::getZ() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001574 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001575}
1576
Robert Carr1c5481e2019-07-01 14:42:27 -07001577bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) const {
Robert Carr29abff82017-12-04 13:51:20 -08001578 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001579 const State& state = useDrawing ? mDrawingState : mCurrentState;
Robert Carr29abff82017-12-04 13:51:20 -08001580 return state.zOrderRelativeOf != nullptr;
1581}
1582
David Sodman41fdfc92017-11-06 16:09:56 -08001583__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001584 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001585 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1586 "makeTraversalList received invalid stateSet");
1587 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1588 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001589 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001590
Robert Carr29abff82017-12-04 13:51:20 -08001591 if (state.zOrderRelatives.size() == 0) {
1592 *outSkipRelativeZUsers = true;
1593 return children;
1594 }
1595
chaviwfd462612018-05-31 16:11:27 -07001596 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001597 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001598 sp<Layer> strongRelative = weakRelative.promote();
1599 if (strongRelative != nullptr) {
1600 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001601 }
1602 }
1603
Dan Stoza412903f2017-04-27 13:42:17 -07001604 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001605 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
Robert Carr503c7042017-09-27 15:06:08 -07001606 if (childState.zOrderRelativeOf != nullptr) {
1607 continue;
1608 }
Robert Carrdb66e622017-04-10 16:55:57 -07001609 traverse.add(child);
1610 }
1611
1612 return traverse;
1613}
1614
Robert Carr1f0a16a2016-10-24 16:27:39 -07001615/**
Robert Carrdb66e622017-04-10 16:55:57 -07001616 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001617 */
Dan Stoza412903f2017-04-27 13:42:17 -07001618void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001619 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1620 // produce a new list for traversing, including our relatives, and not including our children
1621 // who are relatives of another surface. In the case that there are no relative Z,
1622 // makeTraversalList returns our children directly to avoid significant overhead.
1623 // However in this case we need to take the responsibility for filtering children which
1624 // are relatives of another surface here.
1625 bool skipRelativeZUsers = false;
1626 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001627
Robert Carr1f0a16a2016-10-24 16:27:39 -07001628 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001629 for (; i < list.size(); i++) {
1630 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001631 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1632 continue;
1633 }
1634
Robert Carrdb66e622017-04-10 16:55:57 -07001635 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001636 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001637 }
Dan Stoza412903f2017-04-27 13:42:17 -07001638 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001639 }
Robert Carr29abff82017-12-04 13:51:20 -08001640
Dan Stoza412903f2017-04-27 13:42:17 -07001641 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001642 for (; i < list.size(); i++) {
1643 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001644
Robert Carr29abff82017-12-04 13:51:20 -08001645 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1646 continue;
1647 }
Dan Stoza412903f2017-04-27 13:42:17 -07001648 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001649 }
1650}
1651
1652/**
Robert Carrdb66e622017-04-10 16:55:57 -07001653 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001654 */
Dan Stoza412903f2017-04-27 13:42:17 -07001655void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1656 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001657 // See traverseInZOrder for documentation.
1658 bool skipRelativeZUsers = false;
1659 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001660
Robert Carr1f0a16a2016-10-24 16:27:39 -07001661 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001662 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001663 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001664
1665 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1666 continue;
1667 }
1668
Robert Carrdb66e622017-04-10 16:55:57 -07001669 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001670 break;
1671 }
Dan Stoza412903f2017-04-27 13:42:17 -07001672 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001673 }
Dan Stoza412903f2017-04-27 13:42:17 -07001674 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001675 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001676 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001677
1678 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1679 continue;
1680 }
1681
Dan Stoza412903f2017-04-27 13:42:17 -07001682 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001683 }
1684}
1685
chaviw4b129c22018-04-09 16:19:43 -07001686LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1687 const std::vector<Layer*>& layersInTree) {
1688 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1689 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001690 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1691 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001692 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001693
chaviwfd462612018-05-31 16:11:27 -07001694 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001695 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1696 sp<Layer> strongRelative = weakRelative.promote();
1697 // Only add relative layers that are also descendents of the top most parent of the tree.
1698 // If a relative layer is not a descendent, then it should be ignored.
1699 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1700 traverse.add(strongRelative);
1701 }
1702 }
1703
1704 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001705 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001706 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1707 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1708 // the child here since it won't be added later as a relative.
1709 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1710 childState.zOrderRelativeOf.promote().get())) {
1711 continue;
1712 }
1713 traverse.add(child);
1714 }
1715
1716 return traverse;
1717}
1718
1719void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1720 LayerVector::StateSet stateSet,
1721 const LayerVector::Visitor& visitor) {
1722 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001723
1724 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001725 for (; i < list.size(); i++) {
1726 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001727 if (relative->getZ() >= 0) {
1728 break;
1729 }
chaviw4b129c22018-04-09 16:19:43 -07001730 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001731 }
chaviw4b129c22018-04-09 16:19:43 -07001732
chaviwa76b2712017-09-20 12:02:26 -07001733 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001734 for (; i < list.size(); i++) {
1735 const auto& relative = list[i];
1736 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001737 }
1738}
1739
chaviw4b129c22018-04-09 16:19:43 -07001740std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1741 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1742 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1743
1744 std::vector<Layer*> layersInTree = {this};
1745 for (size_t i = 0; i < children.size(); i++) {
1746 const auto& child = children[i];
1747 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1748 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1749 }
1750
1751 return layersInTree;
1752}
1753
1754void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1755 const LayerVector::Visitor& visitor) {
1756 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1757 std::sort(layersInTree.begin(), layersInTree.end());
1758 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1759}
1760
Peiyong Linefefaac2018-08-17 12:27:51 -07001761ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001762 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001763}
1764
chaviw13fdc492017-06-27 12:40:18 -07001765half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001766 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001767
chaviw13fdc492017-06-27 12:40:18 -07001768 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1769 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001770}
Robert Carr6452f122017-03-21 10:41:29 -07001771
chaviw13fdc492017-06-27 12:40:18 -07001772half4 Layer::getColor() const {
1773 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001774 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001775}
Robert Carr6452f122017-03-21 10:41:29 -07001776
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001777Layer::RoundedCornerState Layer::getRoundedCornerState() const {
1778 const auto& p = mDrawingParent.promote();
1779 if (p != nullptr) {
Peiyong Lin27016a92019-03-29 17:36:08 +00001780 RoundedCornerState parentState = p->getRoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001781 if (parentState.radius > 0) {
1782 ui::Transform t = getActiveTransform(getDrawingState());
1783 t = t.inverse();
1784 parentState.cropRect = t.transform(parentState.cropRect);
1785 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1786 // but a transform matrix can define horizontal and vertical scales.
1787 // Let's take the average between both of them and pass into the shader, practically we
1788 // never do this type of transformation on windows anyway.
1789 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
1790 return parentState;
1791 }
1792 }
1793 const float radius = getDrawingState().cornerRadius;
Peiyong Linb9ff23e2019-04-08 11:04:59 -07001794 return radius > 0 && getCrop(getDrawingState()).isValid()
1795 ? RoundedCornerState(getCrop(getDrawingState()).toFloatRect(), radius)
1796 : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001797}
1798
Robert Carr1f0a16a2016-10-24 16:27:39 -07001799void Layer::commitChildList() {
1800 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1801 const auto& child = mCurrentChildren[i];
1802 child->commitChildList();
1803 }
1804 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001805 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001806}
1807
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001808static wp<Layer> extractLayerFromBinder(const wp<IBinder>& weakBinderHandle) {
1809 if (weakBinderHandle == nullptr) {
1810 return nullptr;
1811 }
1812 sp<IBinder> binderHandle = weakBinderHandle.promote();
1813 if (binderHandle == nullptr) {
1814 return nullptr;
1815 }
1816 sp<Layer::Handle> handle = static_cast<Layer::Handle*>(binderHandle.get());
1817 if (handle == nullptr) {
1818 return nullptr;
1819 }
1820 return handle->owner;
1821}
1822
Robert Carr720e5062018-07-30 17:45:14 -07001823void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001824 mCurrentState.inputInfo = info;
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001825 mCurrentState.touchableRegionCrop = extractLayerFromBinder(info.touchableRegionCropHandle);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001826 mCurrentState.modified = true;
1827 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07001828 setTransactionFlags(eTransactionNeeded);
1829}
1830
Vishnu Nair8406fd72019-07-30 11:29:31 -07001831void Layer::writeToProtoDrawingState(LayerProto* layerInfo, uint32_t traceFlags) const {
1832 ui::Transform transform = getTransform();
1833
1834 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
1835 for (const auto& pendingState : mPendingStatesSnapshot) {
1836 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
1837 if (barrierLayer != nullptr) {
1838 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
1839 barrierLayerProto->set_id(barrierLayer->sequence);
1840 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
1841 }
1842 }
1843
1844 auto buffer = mActiveBuffer;
1845 if (buffer != nullptr) {
1846 LayerProtoHelper::writeToProto(buffer,
1847 [&]() { return layerInfo->mutable_active_buffer(); });
1848 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
1849 layerInfo->mutable_buffer_transform());
1850 }
1851 layerInfo->set_invalidate(contentDirty);
1852 layerInfo->set_is_protected(isProtected());
1853 layerInfo->set_dataspace(
1854 dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
1855 layerInfo->set_queued_frames(getQueuedFrameCount());
1856 layerInfo->set_refresh_pending(isBufferLatched());
1857 layerInfo->set_curr_frame(mCurrentFrameNumber);
1858 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
1859
1860 layerInfo->set_corner_radius(getRoundedCornerState().radius);
1861 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1862 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
1863 [&]() { return layerInfo->mutable_position(); });
1864 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
Lloyd Piquea2468662019-03-07 21:31:06 -08001865 LayerProtoHelper::writeToProto(debugGetVisibleRegionOnDefaultDisplay(),
Vishnu Nair8406fd72019-07-30 11:29:31 -07001866 [&]() { return layerInfo->mutable_visible_region(); });
1867 LayerProtoHelper::writeToProto(surfaceDamageRegion,
1868 [&]() { return layerInfo->mutable_damage_region(); });
1869 }
1870
1871 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
1872 LayerProtoHelper::writeToProto(mSourceBounds,
1873 [&]() { return layerInfo->mutable_source_bounds(); });
1874 LayerProtoHelper::writeToProto(mScreenBounds,
1875 [&]() { return layerInfo->mutable_screen_bounds(); });
1876 }
1877}
1878
1879void Layer::writeToProtoCommonState(LayerProto* layerInfo, LayerVector::StateSet stateSet,
1880 uint32_t traceFlags) const {
chaviw1d044282017-09-27 12:19:28 -07001881 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1882 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001883 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07001884
Peiyong Linefefaac2018-08-17 12:27:51 -07001885 ui::Transform requestedTransform = state.active_legacy.transform;
chaviw1d044282017-09-27 12:19:28 -07001886
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001887 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
1888 layerInfo->set_id(sequence);
1889 layerInfo->set_name(getName().c_str());
chaviw8a01fa42019-08-19 12:39:31 -07001890 layerInfo->set_type(getType());
chaviw1d044282017-09-27 12:19:28 -07001891
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001892 for (const auto& child : children) {
1893 layerInfo->add_children(child->sequence);
chaviw1d044282017-09-27 12:19:28 -07001894 }
chaviw1d044282017-09-27 12:19:28 -07001895
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001896 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1897 sp<Layer> strongRelative = weakRelative.promote();
1898 if (strongRelative != nullptr) {
1899 layerInfo->add_relatives(strongRelative->sequence);
1900 }
chaviwadc40c22018-07-10 16:57:27 -07001901 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001902
1903 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
1904 [&]() { return layerInfo->mutable_transparent_region(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001905
1906 layerInfo->set_layer_stack(getLayerStack());
1907 layerInfo->set_z(state.z);
1908
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001909 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(),
1910 [&]() {
1911 return layerInfo->mutable_requested_position();
1912 });
1913
1914 LayerProtoHelper::writeSizeToProto(state.active_legacy.w, state.active_legacy.h,
1915 [&]() { return layerInfo->mutable_size(); });
1916
1917 LayerProtoHelper::writeToProto(state.crop_legacy,
1918 [&]() { return layerInfo->mutable_crop(); });
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001919
1920 layerInfo->set_is_opaque(isOpaque(state));
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001921
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001922
1923 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1924 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
1925 LayerProtoHelper::writeToProto(state.color,
1926 [&]() { return layerInfo->mutable_requested_color(); });
1927 layerInfo->set_flags(state.flags);
1928
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001929 LayerProtoHelper::writeToProto(requestedTransform,
1930 layerInfo->mutable_requested_transform());
1931
1932 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
1933 if (parent != nullptr) {
1934 layerInfo->set_parent(parent->sequence);
1935 } else {
1936 layerInfo->set_parent(-1);
1937 }
1938
1939 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1940 if (zOrderRelativeOf != nullptr) {
1941 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1942 } else {
1943 layerInfo->set_z_order_relative_of(-1);
1944 }
chaviwadc40c22018-07-10 16:57:27 -07001945 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001946
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001947 if (traceFlags & SurfaceTracing::TRACE_INPUT) {
1948 LayerProtoHelper::writeToProto(state.inputInfo, state.touchableRegionCrop,
1949 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001950 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001951
1952 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
1953 auto protoMap = layerInfo->mutable_metadata();
1954 for (const auto& entry : state.metadata.mMap) {
1955 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
1956 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001957 }
chaviw1d044282017-09-27 12:19:28 -07001958}
1959
Robert Carr2e102c92018-10-23 12:11:15 -07001960bool Layer::isRemovedFromCurrentState() const {
1961 return mRemovedFromCurrentState;
1962}
1963
Arthur Hungd20b2702019-01-14 18:16:16 +08001964InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001965 InputWindowInfo info = mDrawingState.inputInfo;
Robert Carr720e5062018-07-30 17:45:14 -07001966
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001967 if (info.displayId == ADISPLAY_ID_NONE) {
chaviwb15ea8a2019-09-03 12:40:22 -07001968 info.displayId = getLayerStack();
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001969 }
1970
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001971 ui::Transform t = getTransform();
1972 const float xScale = t.sx();
1973 const float yScale = t.sy();
Ady Abraham282f1d72019-07-24 18:05:56 -07001974 int32_t xSurfaceInset = info.surfaceInset;
1975 int32_t ySurfaceInset = info.surfaceInset;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001976 if (xScale != 1.0f || yScale != 1.0f) {
Ady Abraham282f1d72019-07-24 18:05:56 -07001977 info.windowXScale *= (xScale != 0.0f) ? 1.0f / xScale : 0.0f;
1978 info.windowYScale *= (yScale != 0.0f) ? 1.0f / yScale : 0.0f;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001979 info.touchableRegion.scaleSelf(xScale, yScale);
Ady Abraham282f1d72019-07-24 18:05:56 -07001980 xSurfaceInset = std::round(xSurfaceInset * xScale);
1981 ySurfaceInset = std::round(ySurfaceInset * yScale);
Riddle Hsu39d4aa52018-11-30 20:46:53 +08001982 }
Robert Carre07e1032018-11-26 12:55:53 -08001983
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001984 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001985 // If this is a portal window, set the touchableRegion to the layerBounds.
1986 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
1987 ? getBufferSize(getDrawingState())
1988 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08001989 if (!layerBounds.isValid()) {
1990 layerBounds = getCroppedBufferSize(getDrawingState());
1991 }
Vishnu Nair8033a492018-12-05 07:27:23 -08001992 layerBounds = t.transform(layerBounds);
Ady Abraham282f1d72019-07-24 18:05:56 -07001993
1994 // clamp inset to layer bounds
1995 xSurfaceInset = (xSurfaceInset >= 0) ? std::min(xSurfaceInset, layerBounds.getWidth() / 2) : 0;
1996 ySurfaceInset = (ySurfaceInset >= 0) ? std::min(ySurfaceInset, layerBounds.getHeight() / 2) : 0;
1997
Arthur Hung118b1142019-05-08 21:25:59 +08001998 layerBounds.inset(xSurfaceInset, ySurfaceInset, xSurfaceInset, ySurfaceInset);
Vishnu Nair8033a492018-12-05 07:27:23 -08001999
Arthur Hungd20b2702019-01-14 18:16:16 +08002000 // Input coordinate should match the layer bounds.
2001 info.frameLeft = layerBounds.left;
2002 info.frameTop = layerBounds.top;
2003 info.frameRight = layerBounds.right;
2004 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08002005
2006 // Position the touchable region relative to frame screen location and restrict it to frame
2007 // bounds.
2008 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08002009 info.visible = canReceiveInput();
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002010
2011 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2012 if (info.replaceTouchableRegionWithCrop) {
2013 if (cropLayer == nullptr) {
2014 info.touchableRegion = Region(Rect{mScreenBounds});
2015 } else {
2016 info.touchableRegion = Region(Rect{cropLayer->mScreenBounds});
2017 }
2018 } else if (cropLayer != nullptr) {
2019 info.touchableRegion = info.touchableRegion.intersect(Rect{cropLayer->mScreenBounds});
2020 }
2021
Robert Carr720e5062018-07-30 17:45:14 -07002022 return info;
2023}
2024
2025bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002026 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002027}
2028
Lloyd Piquefeb73d72018-12-04 17:23:44 -08002029std::shared_ptr<compositionengine::Layer> Layer::getCompositionLayer() const {
2030 return nullptr;
2031}
2032
chaviw3e727cd2019-01-31 13:41:05 -08002033bool Layer::canReceiveInput() const {
2034 return isVisible();
2035}
2036
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002037compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
2038 const sp<const DisplayDevice>& display) const {
2039 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionLayer().get());
2040}
2041
Lloyd Piquea2468662019-03-07 21:31:06 -08002042Region Layer::debugGetVisibleRegionOnDefaultDisplay() const {
2043 sp<DisplayDevice> displayDevice = mFlinger->getDefaultDisplayDeviceLocked();
2044 if (displayDevice == nullptr) {
2045 return {};
2046 }
2047
2048 auto outputLayer = findOutputLayerForDisplay(displayDevice);
2049 if (outputLayer == nullptr) {
2050 return {};
2051 }
2052
2053 return outputLayer->getState().visibleRegion;
2054}
2055
Mathias Agopian13127d82013-03-05 17:47:11 -08002056// ---------------------------------------------------------------------------
2057
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002058}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002059
2060#if defined(__gl_h_)
2061#error "don't include gl/gl.h in this file"
2062#endif
2063
2064#if defined(__gl2_h_)
2065#error "don't include gl2/gl2.h in this file"
2066#endif