blob: 898f3bc9923c38ae9f8bb5346afe304a7156e14b [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
Mathias Agopian13127d82013-03-05 17:47:11 -080022#include <math.h>
David Sodman41fdfc92017-11-06 16:09:56 -080023#include <stdint.h>
24#include <stdlib.h>
25#include <sys/types.h>
chaviw4b129c22018-04-09 16:19:43 -070026#include <algorithm>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080027#include <mutex>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080028
Yiwei Zhang5434a782018-12-05 18:06:32 -080029#include <android-base/stringprintf.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080030#include <compositionengine/Display.h>
Lloyd Pique0b785d82018-12-04 17:25:27 -080031#include <compositionengine/Layer.h>
Lloyd Piquea83776c2019-01-29 18:42:32 -080032#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080033#include <compositionengine/OutputLayer.h>
Lloyd Pique0b785d82018-12-04 17:25:27 -080034#include <compositionengine/impl/LayerCompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080035#include <compositionengine/impl/OutputLayerCompositionState.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070036#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070037#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070038#include <cutils/properties.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080039#include <gui/BufferItem.h>
40#include <gui/LayerDebugInfo.h>
41#include <gui/Surface.h>
42#include <renderengine/RenderEngine.h>
43#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
Yiwei Zhang60d1a192018-03-07 14:52:28 -080052#include "BufferLayer.h"
Valerie Haudd0b7572019-01-29 14:59:27 -080053#include "ColorLayer.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020054#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070055#include "DisplayDevice.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080056#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057#include "Layer.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080058#include "LayerProtoHelper.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070059#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070060#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061#include "SurfaceFlinger.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080062#include "TimeStats/TimeStats.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070063
David Sodman41fdfc92017-11-06 16:09:56 -080064#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066namespace android {
67
Yiwei Zhang5434a782018-12-05 18:06:32 -080068using base::StringAppendF;
69
Lloyd Piquef1c675b2018-09-12 20:45:39 -070070std::atomic<int32_t> Layer::sSequence{1};
Mathias Agopian13127d82013-03-05 17:47:11 -080071
Lloyd Pique42ab75e2018-09-12 20:46:03 -070072Layer::Layer(const LayerCreationArgs& args)
Ady Abraham8f1ee7f2019-04-05 10:32:50 -070073 : mFlinger(args.flinger),
74 mName(args.name),
75 mClientRef(args.client),
76 mWindowType(args.metadata.getInt32(METADATA_WINDOW_TYPE, 0)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -070077 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070078
79 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -070080 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
81 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
82 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070083
Dan Stozaf7ba41a2017-05-10 15:11:11 -070084 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070085
Lloyd Pique0449b0f2018-12-20 16:23:45 -080086 mCurrentState.active_legacy.w = args.w;
87 mCurrentState.active_legacy.h = args.h;
88 mCurrentState.flags = layerFlags;
89 mCurrentState.active_legacy.transform.set(0, 0);
90 mCurrentState.crop_legacy.makeInvalid();
91 mCurrentState.requestedCrop_legacy = mCurrentState.crop_legacy;
92 mCurrentState.z = 0;
93 mCurrentState.color.a = 1.0f;
94 mCurrentState.layerStack = 0;
95 mCurrentState.sequence = 0;
96 mCurrentState.requested_legacy = mCurrentState.active_legacy;
Lloyd Pique0449b0f2018-12-20 16:23:45 -080097 mCurrentState.active.w = UINT32_MAX;
98 mCurrentState.active.h = UINT32_MAX;
99 mCurrentState.active.transform.set(0, 0);
100 mCurrentState.transform = 0;
101 mCurrentState.transformToDisplayInverse = false;
102 mCurrentState.crop.makeInvalid();
103 mCurrentState.acquireFence = new Fence(-1);
104 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
105 mCurrentState.hdrMetadata.validTypes = 0;
106 mCurrentState.surfaceDamageRegion.clear();
107 mCurrentState.cornerRadius = 0.0f;
108 mCurrentState.api = -1;
109 mCurrentState.hasColorTransform = false;
Peiyong Linc502cb72019-03-01 15:00:23 -0800110 mCurrentState.colorSpaceAgnostic = false;
Evan Roskya1f1e152019-01-24 16:17:46 -0800111 mCurrentState.metadata = args.metadata;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700112
113 // drawing state & current state are identical
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800114 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700115
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800116 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700117 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800118 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200119 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700120
Ady Abraham8f1ee7f2019-04-05 10:32:50 -0700121 mSchedulerLayerHandle = mFlinger->mScheduler->registerLayer(mName.c_str(), mWindowType);
Ady Abraham09bd3922019-04-08 10:44:56 -0700122
Robert Carr2e102c92018-10-23 12:11:15 -0700123 mFlinger->onLayerCreated();
Dan Stoza436ccf32018-06-21 12:10:12 -0700124}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700125
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700126Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800127 sp<Client> c(mClientRef.promote());
128 if (c != 0) {
129 c->detachLayer(this);
130 }
131
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000132 mFrameTracker.logAndResetStats(mName);
chaviw74d90ad2019-04-26 14:45:26 -0700133 mFlinger->onLayerDestroyed(this);
Mathias Agopian96f08192010-06-02 23:28:45 -0700134}
135
Mathias Agopian13127d82013-03-05 17:47:11 -0800136// ---------------------------------------------------------------------------
137// callbacks
138// ---------------------------------------------------------------------------
139
David Sodmaneb085e02017-10-05 18:49:04 -0700140/*
141 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
142 * Layer. So, the implementation is done in BufferLayer. When called on a
143 * ColorLayer object, it's essentially a NOP.
144 */
David Sodmaneb085e02017-10-05 18:49:04 -0700145void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800146
chaviw43cb3cb2019-05-31 15:23:41 -0700147void Layer::removeRemoteSyncPoints() {
148 for (auto& point : mRemoteSyncPoints) {
149 point->setTransactionApplied();
150 }
151 mRemoteSyncPoints.clear();
152
153 {
154 Mutex::Autolock pendingStateLock(mPendingStateMutex);
155 for (State pendingState : mPendingStates) {
156 pendingState.barrierLayer_legacy = nullptr;
157 }
158 }
159}
160
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700161void Layer::removeRelativeZ(const std::vector<Layer*>& layersInTree) {
162 if (mCurrentState.zOrderRelativeOf == nullptr) {
163 return;
164 }
Robert Carr2e102c92018-10-23 12:11:15 -0700165
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700166 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
167 if (strongRelative == nullptr) {
168 setZOrderRelativeOf(nullptr);
169 return;
170 }
171
172 if (!std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
173 strongRelative->removeZOrderRelative(this);
174 mFlinger->setTransactionFlags(eTraversalNeeded);
chaviw606e5cf2019-03-01 10:12:10 -0800175 setZOrderRelativeOf(nullptr);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700176 }
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700177}
178
179void Layer::removeFromCurrentState() {
180 mRemovedFromCurrentState = true;
Rob Carr4bba3702018-10-08 21:53:30 +0000181
Robert Carr2e102c92018-10-23 12:11:15 -0700182 // Since we are no longer reachable from CurrentState SurfaceFlinger
183 // will no longer invoke doTransaction for us, and so we will
184 // never finish applying transactions. We signal the sync point
185 // now so that another layer will not become indefinitely
186 // blocked.
chaviw43cb3cb2019-05-31 15:23:41 -0700187 removeRemoteSyncPoints();
Robert Carr2e102c92018-10-23 12:11:15 -0700188
189 {
190 Mutex::Autolock syncLock(mLocalSyncPointMutex);
191 for (auto& point : mLocalSyncPoints) {
192 point->setFrameAvailable();
193 }
194 mLocalSyncPoints.clear();
195 }
196
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800197 mFlinger->markLayerPendingRemovalLocked(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700198}
Chia-I Wu38512252017-05-17 14:36:16 -0700199
Vishnu Nairda9c85a2019-06-03 17:26:48 -0700200void Layer::onRemovedFromCurrentState() {
201 auto layersInTree = getLayersInTree(LayerVector::StateSet::Current);
202 std::sort(layersInTree.begin(), layersInTree.end());
203 for (const auto& layer : layersInTree) {
204 layer->removeFromCurrentState();
205 layer->removeRelativeZ(layersInTree);
206 }
207}
208
chaviw61626f22018-11-15 16:26:27 -0800209void Layer::addToCurrentState() {
210 mRemovedFromCurrentState = false;
211
212 for (const auto& child : mCurrentChildren) {
213 child->addToCurrentState();
214 }
215}
216
Mathias Agopian13127d82013-03-05 17:47:11 -0800217// ---------------------------------------------------------------------------
218// set-up
219// ---------------------------------------------------------------------------
220
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700221const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800222 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800223}
224
chaviw13fdc492017-06-27 12:40:18 -0700225bool Layer::getPremultipledAlpha() const {
226 return mPremultipliedAlpha;
227}
228
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700229sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800230 Mutex::Autolock _l(mLock);
Robert Carrc0df3122019-04-11 13:18:21 -0700231 if (mGetHandleCalled) {
232 ALOGE("Get handle called twice" );
233 return nullptr;
234 }
235 mGetHandleCalled = true;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700236 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800237}
238
239// ---------------------------------------------------------------------------
240// h/w composer set-up
241// ---------------------------------------------------------------------------
242
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800243bool Layer::hasHwcLayer(const sp<const DisplayDevice>& displayDevice) {
244 auto outputLayer = findOutputLayerForDisplay(displayDevice);
245 LOG_FATAL_IF(!outputLayer);
246 return outputLayer->getState().hwc && (*outputLayer->getState().hwc).hwcLayer != nullptr;
247}
248
249HWC2::Layer* Layer::getHwcLayer(const sp<const DisplayDevice>& displayDevice) {
250 auto outputLayer = findOutputLayerForDisplay(displayDevice);
251 if (!outputLayer || !outputLayer->getState().hwc) {
252 return nullptr;
253 }
254 return (*outputLayer->getState().hwc).hwcLayer.get();
Steven Thomasb02664d2017-07-26 18:48:28 -0700255}
Steven Thomasb02664d2017-07-26 18:48:28 -0700256
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800257Rect Layer::getContentCrop() const {
258 // this is the crop rectangle that applies to the buffer
259 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700260 Rect crop;
261 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800262 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700263 crop = mCurrentCrop;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800264 } else if (mActiveBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800265 // otherwise we use the whole buffer
Lloyd Pique0b785d82018-12-04 17:25:27 -0800266 crop = mActiveBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700267 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800268 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700269 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700270 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700271 return crop;
272}
273
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700274static Rect reduce(const Rect& win, const Region& exclude) {
275 if (CC_LIKELY(exclude.isEmpty())) {
276 return win;
277 }
278 if (exclude.isRect()) {
279 return win.reduce(exclude.getBounds());
280 }
281 return Region(win).subtract(exclude).getBounds();
282}
283
Dan Stoza80d61162017-12-20 15:57:52 -0800284static FloatRect reduce(const FloatRect& win, const Region& exclude) {
285 if (CC_LIKELY(exclude.isEmpty())) {
286 return win;
287 }
288 // Convert through Rect (by rounding) for lack of FloatRegion
289 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
290}
291
Vishnu Nair4351ad52019-02-11 14:13:02 -0800292Rect Layer::getScreenBounds(bool reduceTransparentRegion) const {
Vishnu Nairf0c28512019-02-08 12:40:28 -0800293 if (!reduceTransparentRegion) {
294 return Rect{mScreenBounds};
295 }
296
297 FloatRect bounds = getBounds();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800298 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800299 // Transform to screen space.
300 bounds = t.transform(bounds);
301 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700302}
303
Vishnu Nair4351ad52019-02-11 14:13:02 -0800304FloatRect Layer::getBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000305 const State& s(getDrawingState());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800306 return getBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700307}
308
Vishnu Nairf0c28512019-02-08 12:40:28 -0800309FloatRect Layer::getBounds(const Region& activeTransparentRegion) const {
310 // Subtract the transparent region and snap to the bounds.
311 return reduce(mBounds, activeTransparentRegion);
312}
313
Vishnu Nairc652ff82019-03-15 12:48:54 -0700314ui::Transform Layer::getBufferScaleTransform() const {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800315 // If the layer is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
316 // it isFixedSize) then there may be additional scaling not accounted
Vishnu Nairc652ff82019-03-15 12:48:54 -0700317 // for in the layer transform.
Lloyd Pique0b785d82018-12-04 17:25:27 -0800318 if (!isFixedSize() || !mActiveBuffer) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700319 return {};
Vishnu Nair4351ad52019-02-11 14:13:02 -0800320 }
321
Marissa Wall290ad082019-03-06 13:23:47 -0800322 // If the layer is a buffer state layer, the active width and height
323 // could be infinite. In that case, return the effective transform.
324 const uint32_t activeWidth = getActiveWidth(getDrawingState());
325 const uint32_t activeHeight = getActiveHeight(getDrawingState());
326 if (activeWidth >= UINT32_MAX && activeHeight >= UINT32_MAX) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700327 return {};
Marissa Wall290ad082019-03-06 13:23:47 -0800328 }
329
Vishnu Nairc652ff82019-03-15 12:48:54 -0700330 int bufferWidth = mActiveBuffer->getWidth();
331 int bufferHeight = mActiveBuffer->getHeight();
332
333 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
334 std::swap(bufferWidth, bufferHeight);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800335 }
Vishnu Nairc652ff82019-03-15 12:48:54 -0700336
Marissa Wall290ad082019-03-06 13:23:47 -0800337 float sx = activeWidth / static_cast<float>(bufferWidth);
338 float sy = activeHeight / static_cast<float>(bufferHeight);
339
Vishnu Nair4351ad52019-02-11 14:13:02 -0800340 ui::Transform extraParentScaling;
341 extraParentScaling.set(sx, 0, 0, sy);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700342 return extraParentScaling;
343}
344
345ui::Transform Layer::getTransformWithScale(const ui::Transform& bufferScaleTransform) const {
346 // We need to mirror this scaling to child surfaces or we will break the contract where WM can
347 // treat child surfaces as pixels in the parent surface.
348 if (!isFixedSize() || !mActiveBuffer) {
349 return mEffectiveTransform;
350 }
351 return mEffectiveTransform * bufferScaleTransform;
352}
353
354FloatRect Layer::getBoundsPreScaling(const ui::Transform& bufferScaleTransform) const {
355 // We need the pre scaled layer bounds when computing child bounds to make sure the child is
356 // cropped to its parent layer after any buffer transform scaling is applied.
357 if (!isFixedSize() || !mActiveBuffer) {
358 return mBounds;
359 }
360 return bufferScaleTransform.inverse().transform(mBounds);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800361}
362
363void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform) {
364 const State& s(getDrawingState());
365
366 // Calculate effective layer transform
367 mEffectiveTransform = parentTransform * getActiveTransform(s);
368
369 // Transform parent bounds to layer space
370 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
371
Vishnu Nairc652ff82019-03-15 12:48:54 -0700372 // Calculate source bounds
Vishnu Nair4351ad52019-02-11 14:13:02 -0800373 mSourceBounds = computeSourceBounds(parentBounds);
374
375 // Calculate bounds by croping diplay frame with layer crop and parent bounds
376 FloatRect bounds = mSourceBounds;
377 const Rect layerCrop = getCrop(s);
378 if (!layerCrop.isEmpty()) {
379 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
380 }
381 bounds = bounds.intersect(parentBounds);
382
383 mBounds = bounds;
384 mScreenBounds = mEffectiveTransform.transform(mBounds);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700385
386 // Add any buffer scaling to the layer's children.
387 ui::Transform bufferScaleTransform = getBufferScaleTransform();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800388 for (const sp<Layer>& child : mDrawingChildren) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700389 child->computeBounds(getBoundsPreScaling(bufferScaleTransform),
390 getTransformWithScale(bufferScaleTransform));
Vishnu Nair4351ad52019-02-11 14:13:02 -0800391 }
392}
393
Vishnu Nair60356342018-11-13 13:00:45 -0800394Rect Layer::getCroppedBufferSize(const State& s) const {
395 Rect size = getBufferSize(s);
396 Rect crop = getCrop(s);
397 if (!crop.isEmpty() && size.isValid()) {
398 size.intersect(crop, &size);
399 } else if (!crop.isEmpty()) {
400 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700401 }
Vishnu Nair60356342018-11-13 13:00:45 -0800402 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800403}
404
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700405void Layer::setupRoundedCornersCropCoordinates(Rect win,
406 const FloatRect& roundedCornersCrop) const {
407 // Translate win by the rounded corners rect coordinates, to have all values in
408 // layer coordinate space.
409 win.left -= roundedCornersCrop.left;
410 win.right -= roundedCornersCrop.left;
411 win.top -= roundedCornersCrop.top;
412 win.bottom -= roundedCornersCrop.top;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700413}
414
Lloyd Piquea83776c2019-01-29 18:42:32 -0800415void Layer::latchGeometry(compositionengine::LayerFECompositionState& compositionState) const {
416 const auto& drawingState{getDrawingState()};
417 auto alpha = static_cast<float>(getAlpha());
David Revemanecf0fa52017-03-03 11:32:44 -0500418 auto blendMode = HWC2::BlendMode::None;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800419 if (!isOpaque(drawingState) || alpha != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800420 blendMode =
421 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800422 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800423
Lloyd Piquea83776c2019-01-29 18:42:32 -0800424 int type = drawingState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
425 int appId = drawingState.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700426 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400427 if (parent.get()) {
428 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800429 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
430 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
431 if (parentType >= 0 || parentAppId >= 0) {
432 type = parentType;
433 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700434 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400435 }
436
Lloyd Piquea83776c2019-01-29 18:42:32 -0800437 compositionState.geomLayerTransform = getTransform();
438 compositionState.geomInverseLayerTransform = compositionState.geomLayerTransform.inverse();
439 compositionState.geomBufferSize = getBufferSize(drawingState);
440 compositionState.geomContentCrop = getContentCrop();
441 compositionState.geomCrop = getCrop(drawingState);
442 compositionState.geomBufferTransform = mCurrentTransform;
443 compositionState.geomBufferUsesDisplayInverseTransform = getTransformToDisplayInverse();
444 compositionState.geomActiveTransparentRegion = getActiveTransparentRegion(drawingState);
445 compositionState.geomLayerBounds = mBounds;
446 compositionState.geomUsesSourceCrop = usesSourceCrop();
447 compositionState.isSecure = isSecure();
David Sodman15094112018-10-11 09:39:37 -0700448
Lloyd Piquea83776c2019-01-29 18:42:32 -0800449 compositionState.blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
450 compositionState.alpha = alpha;
451 compositionState.type = type;
452 compositionState.appId = appId;
453}
David Sodmanba340492018-08-05 21:51:33 -0700454
Lloyd Piquea83776c2019-01-29 18:42:32 -0800455void Layer::latchCompositionState(compositionengine::LayerFECompositionState& compositionState,
456 bool includeGeometry) const {
457 if (includeGeometry) {
458 latchGeometry(compositionState);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700459 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800460}
Mathias Agopian29a367b2011-07-12 14:51:45 -0700461
Lloyd Piquea83776c2019-01-29 18:42:32 -0800462const char* Layer::getDebugName() const {
463 return mName.string();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800464}
465
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800466void Layer::forceClientComposition(const sp<DisplayDevice>& display) {
467 const auto outputLayer = findOutputLayerForDisplay(display);
468 LOG_FATAL_IF(!outputLayer);
469 outputLayer->editState().forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800470}
Dan Stozaee44edd2015-03-23 15:50:23 -0700471
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800472bool Layer::getForceClientComposition(const sp<DisplayDevice>& display) {
473 const auto outputLayer = findOutputLayerForDisplay(display);
474 LOG_FATAL_IF(!outputLayer);
475 return outputLayer->getState().forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800476}
477
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700478void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800479 const auto outputLayer = findOutputLayerForDisplay(display);
480 LOG_FATAL_IF(!outputLayer);
481
482 if (!outputLayer->getState().hwc ||
483 (*outputLayer->getState().hwc).hwcCompositionType !=
484 Hwc2::IComposerClient::Composition::CURSOR) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800485 return;
486 }
487
488 // This gives us only the "orientation" component of the transform
Vishnu Nair33a6eee2019-02-06 13:48:06 -0800489 const State& s(getDrawingState());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800490
491 // Apply the layer's transform, followed by the display's global transform
492 // Here we're guaranteed that the layer's transform preserves rects
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800493 Rect win = getCroppedBufferSize(s);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800494 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700495 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800496 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700497 frame.intersect(display->getViewport(), &frame);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700498 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800499 auto position = displayTransform.transform(frame);
500
Dominik Laskowski7e045462018-05-30 13:02:02 -0700501 auto error =
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800502 (*outputLayer->getState().hwc).hwcLayer->setCursorPosition(position.left, position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800503 ALOGE_IF(error != HWC2::Error::None,
504 "[%s] Failed to set cursor position "
505 "to (%d, %d): %s (%d)",
506 mName.string(), position.left, position.top, to_string(error).c_str(),
507 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800508}
Riley Andrews03414a12014-07-01 14:22:59 -0700509
Mathias Agopian13127d82013-03-05 17:47:11 -0800510// ---------------------------------------------------------------------------
511// drawing...
512// ---------------------------------------------------------------------------
513
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000514bool Layer::prepareClientLayer(const RenderArea& renderArea, const Region& clip,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800515 Region& clearRegion, const bool supportProtectedContent,
516 renderengine::LayerSettings& layer) {
517 return prepareClientLayer(renderArea, clip, false, clearRegion, supportProtectedContent, layer);
Mathias Agopian13127d82013-03-05 17:47:11 -0800518}
519
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000520bool Layer::prepareClientLayer(const RenderArea& renderArea, bool useIdentityTransform,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800521 Region& clearRegion, const bool supportProtectedContent,
522 renderengine::LayerSettings& layer) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000523 return prepareClientLayer(renderArea, Region(renderArea.getBounds()), useIdentityTransform,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800524 clearRegion, supportProtectedContent, layer);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000525}
526
527bool Layer::prepareClientLayer(const RenderArea& /*renderArea*/, const Region& /*clip*/,
528 bool useIdentityTransform, Region& /*clearRegion*/,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800529 const bool /*supportProtectedContent*/,
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000530 renderengine::LayerSettings& layer) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800531 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000532 half alpha = getAlpha();
533 layer.geometry.boundaries = bounds;
534 if (useIdentityTransform) {
535 layer.geometry.positionTransform = mat4();
536 } else {
537 const ui::Transform transform = getTransform();
538 mat4 m;
539 m[0][0] = transform[0][0];
540 m[0][1] = transform[0][1];
541 m[0][3] = transform[0][2];
542 m[1][0] = transform[1][0];
543 m[1][1] = transform[1][1];
544 m[1][3] = transform[1][2];
545 m[3][0] = transform[2][0];
546 m[3][1] = transform[2][1];
547 m[3][3] = transform[2][2];
548 layer.geometry.positionTransform = m;
549 }
550
551 if (hasColorTransform()) {
552 layer.colorTransform = getColorTransform();
553 }
554
555 const auto roundedCornerState = getRoundedCornerState();
556 layer.geometry.roundedCornersRadius = roundedCornerState.radius;
557 layer.geometry.roundedCornersCrop = roundedCornerState.cropRect;
558
559 layer.alpha = alpha;
560 layer.sourceDataspace = mCurrentDataSpace;
561 return true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800562}
563
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800564void Layer::setCompositionType(const sp<const DisplayDevice>& display,
565 Hwc2::IComposerClient::Composition type) {
566 const auto outputLayer = findOutputLayerForDisplay(display);
567 LOG_FATAL_IF(!outputLayer);
568 LOG_FATAL_IF(!outputLayer->getState().hwc);
569 auto& compositionState = outputLayer->editState();
570
571 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", ((*compositionState.hwc).hwcLayer)->getId(),
572 toString(type).c_str(), 1);
573 if ((*compositionState.hwc).hwcCompositionType != type) {
David Sodman15094112018-10-11 09:39:37 -0700574 ALOGV(" actually setting");
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800575 (*compositionState.hwc).hwcCompositionType = type;
576
577 auto error = (*compositionState.hwc)
578 .hwcLayer->setCompositionType(static_cast<HWC2::Composition>(type));
579 ALOGE_IF(error != HWC2::Error::None,
580 "[%s] Failed to set "
581 "composition type %s: %s (%d)",
582 mName.string(), toString(type).c_str(), to_string(error).c_str(),
583 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800584 }
585}
586
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800587Hwc2::IComposerClient::Composition Layer::getCompositionType(
588 const sp<const DisplayDevice>& display) const {
589 const auto outputLayer = findOutputLayerForDisplay(display);
590 LOG_FATAL_IF(!outputLayer);
591 return outputLayer->getState().hwc ? (*outputLayer->getState().hwc).hwcCompositionType
592 : Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800593}
594
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800595bool Layer::getClearClientTarget(const sp<const DisplayDevice>& display) const {
596 const auto outputLayer = findOutputLayerForDisplay(display);
597 LOG_FATAL_IF(!outputLayer);
598 return outputLayer->getState().clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800599}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800600
Dan Stozacac35382016-01-27 12:21:06 -0800601bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
602 if (point->getFrameNumber() <= mCurrentFrameNumber) {
603 // Don't bother with a SyncPoint, since we've already latched the
604 // relevant frame
605 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700606 }
Robert Carr2e102c92018-10-23 12:11:15 -0700607 if (isRemovedFromCurrentState()) {
608 return false;
609 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700610
Dan Stozacac35382016-01-27 12:21:06 -0800611 Mutex::Autolock lock(mLocalSyncPointMutex);
612 mLocalSyncPoints.push_back(point);
613 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700614}
615
Mathias Agopian13127d82013-03-05 17:47:11 -0800616// ----------------------------------------------------------------------------
617// local state
618// ----------------------------------------------------------------------------
619
Peiyong Lin833074a2018-08-28 11:53:54 -0700620void Layer::computeGeometry(const RenderArea& renderArea,
621 renderengine::Mesh& mesh,
chaviwa76b2712017-09-20 12:02:26 -0700622 bool useIdentityTransform) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700623 const ui::Transform renderAreaTransform(renderArea.getTransform());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800624 FloatRect win = getBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700625
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000626 vec2 lt = vec2(win.left, win.top);
627 vec2 lb = vec2(win.left, win.bottom);
628 vec2 rb = vec2(win.right, win.bottom);
629 vec2 rt = vec2(win.right, win.top);
630
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800631 ui::Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000632 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700633 lt = layerTransform.transform(lt);
634 lb = layerTransform.transform(lb);
635 rb = layerTransform.transform(rb);
636 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000637 }
638
Peiyong Lin833074a2018-08-28 11:53:54 -0700639 renderengine::Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700640 position[0] = renderAreaTransform.transform(lt);
641 position[1] = renderAreaTransform.transform(lb);
642 position[2] = renderAreaTransform.transform(rb);
643 position[3] = renderAreaTransform.transform(rt);
Mathias Agopian13127d82013-03-05 17:47:11 -0800644}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800645
David Sodman41fdfc92017-11-06 16:09:56 -0800646bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800647 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700648 return (s.flags & layer_state_t::eLayerSecure);
649}
650
Mathias Agopian13127d82013-03-05 17:47:11 -0800651void Layer::setVisibleRegion(const Region& visibleRegion) {
652 // always called from main thread
653 this->visibleRegion = visibleRegion;
654}
655
656void Layer::setCoveredRegion(const Region& coveredRegion) {
657 // always called from main thread
658 this->coveredRegion = coveredRegion;
659}
660
David Sodman41fdfc92017-11-06 16:09:56 -0800661void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800662 // always called from main thread
663 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
664}
665
Robert Carre5f4f692018-01-12 13:12:28 -0800666void Layer::clearVisibilityRegions() {
667 visibleRegion.clear();
668 visibleNonTransparentRegion.clear();
669 coveredRegion.clear();
670}
671
Mathias Agopian13127d82013-03-05 17:47:11 -0800672// ----------------------------------------------------------------------------
673// transaction
674// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800675
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800676void Layer::pushPendingState() {
677 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700678 return;
679 }
680
Dan Stoza7dde5992015-05-22 09:51:44 -0700681 // If this transaction is waiting on the receipt of a frame, generate a sync
682 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700683 // We don't allow installing sync points after we are removed from the current state
684 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800685 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
686 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800687 if (barrierLayer == nullptr) {
688 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700689 // If we can't promote the layer we are intended to wait on,
690 // then it is expired or otherwise invalid. Allow this transaction
691 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800692 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800693 } else {
chaviw43cb3cb2019-05-31 15:23:41 -0700694 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy, this);
Robert Carr0d480722017-01-10 16:42:54 -0800695 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800696 mRemoteSyncPoints.push_back(std::move(syncPoint));
697 } else {
698 // We already missed the frame we're supposed to synchronize
699 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800700 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800701 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700702 }
703
Dan Stoza7dde5992015-05-22 09:51:44 -0700704 // Wake us up to check if the frame has been received
705 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700706 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700707 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800708 mPendingStates.push_back(mCurrentState);
709 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700710}
711
Pablo Ceballos05289c22016-04-14 15:49:55 -0700712void Layer::popPendingState(State* stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800713 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700714
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800715 mPendingStates.removeAt(0);
716 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700717}
718
Pablo Ceballos05289c22016-04-14 15:49:55 -0700719bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700720 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800721 while (!mPendingStates.empty()) {
722 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700723 if (mRemoteSyncPoints.empty()) {
724 // If we don't have a sync point for this, apply it anyway. It
725 // will be visually wrong, but it should keep us from getting
726 // into too much trouble.
727 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700728 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700729 stateUpdateAvailable = true;
730 continue;
731 }
732
Marissa Wallf58c14b2018-07-24 10:50:43 -0700733 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800734 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800735 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800736
737 // Signal our end of the sync point and then dispose of it
738 mRemoteSyncPoints.front()->setTransactionApplied();
739 mRemoteSyncPoints.pop_front();
740 continue;
741 }
742
Dan Stoza7dde5992015-05-22 09:51:44 -0700743 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
744 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700745 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700746 stateUpdateAvailable = true;
747
748 // Signal our end of the sync point and then dispose of it
749 mRemoteSyncPoints.front()->setTransactionApplied();
750 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800751 } else {
752 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700753 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700754 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700755 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700756 stateUpdateAvailable = true;
757 }
758 }
759
760 // If we still have pending updates, wake SurfaceFlinger back up and point
761 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800762 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700763 setTransactionFlags(eTransactionNeeded);
764 mFlinger->setTransactionFlags(eTraversalNeeded);
765 }
766
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800767 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700768 return stateUpdateAvailable;
769}
770
Marissa Wall61c58622018-07-18 10:12:20 -0700771uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000772 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800773
Marissa Wall61c58622018-07-18 10:12:20 -0700774 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
775 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700776
David Sodmaneb085e02017-10-05 18:49:04 -0700777 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700778 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000779 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800780 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
781 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
782 " requested={ wh={%4u,%4u} }}\n"
783 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
784 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700785 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700786 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
787 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
788 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
789 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
790 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700791 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
792 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
793 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800794 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700795
Robert Carre392b552017-09-19 12:16:05 -0700796 // Don't let Layer::doTransaction update the drawing state
797 // if we have a pending resize, unless we are in fixed-size mode.
798 // the drawing state will be updated only once we receive a buffer
799 // with the correct size.
800 //
801 // In particular, we want to make sure the clip (which is part
802 // of the geometry state) is latched together with the size but is
803 // latched immediately when no resizing is involved.
804 //
805 // If a sideband stream is attached, however, we want to skip this
806 // optimization so that transactions aren't missed when a buffer
807 // never arrives
808 //
809 // In the case that we don't have a buffer we ignore other factors
810 // and avoid entering the resizePending state. At a high level the
811 // resizePending state is to avoid applying the state of the new buffer
812 // to the old buffer. However in the state where we don't have an old buffer
813 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700814 const bool resizePending =
815 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
816 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
Lloyd Pique0b785d82018-12-04 17:25:27 -0800817 (mActiveBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700818 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800819 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700820 flags |= eDontUpdateGeometryState;
821 }
822 }
823
Robert Carr7bf247e2017-05-18 14:02:49 -0700824 // Here we apply various requested geometry states, depending on our
825 // latching configuration. See Layer.h for a detailed discussion of
826 // how geometry latching is controlled.
827 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000828 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700829
830 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
831 // mode, which causes attributes which normally latch regardless of scaling mode,
832 // to be delayed. We copy the requested state to the active state making sure
833 // to respect these rules (again see Layer.h for a detailed discussion).
834 //
835 // There is an awkward asymmetry in the handling of the crop states in the position
836 // states, as can be seen below. Largely this arises from position and transform
837 // being stored in the same data structure while having different latching rules.
838 // b/38182305
839 //
Marissa Wall61c58622018-07-18 10:12:20 -0700840 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700841 // applyPendingStates in the presence of deferred transactions.
842 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -0700843 float tx = stateToCommit->active_legacy.transform.tx();
844 float ty = stateToCommit->active_legacy.transform.ty();
845 stateToCommit->active_legacy = stateToCommit->requested_legacy;
846 stateToCommit->active_legacy.transform.set(tx, ty);
847 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700848 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700849 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -0700850 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700851 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800852 }
853
Marissa Wall61c58622018-07-18 10:12:20 -0700854 return flags;
855}
856
857uint32_t Layer::doTransaction(uint32_t flags) {
858 ATRACE_CALL();
859
chaviw5aedec92018-10-22 10:40:38 -0700860 if (mLayerDetached) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800861 return flags;
862 }
863
864 if (mChildrenChanged) {
865 flags |= eVisibleRegion;
866 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -0700867 }
868
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800869 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +0000870 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -0700871 if (!applyPendingStates(&c)) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800872 return flags;
Marissa Wall61c58622018-07-18 10:12:20 -0700873 }
874
875 flags = doTransactionResize(flags, &c);
876
Alec Mourib416efd2018-09-06 21:01:59 +0000877 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700878
879 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800880 // invalidate and recompute the visible regions if needed
881 flags |= Layer::eVisibleRegion;
882 }
883
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700884 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800885 // invalidate and recompute the visible regions if needed
886 flags |= eVisibleRegion;
887 this->contentDirty = true;
888
889 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -0700890 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -0700891 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -0800892 }
893
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800894 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -0700895 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800896 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -0700897 }
898
Mathias Agopian13127d82013-03-05 17:47:11 -0800899 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -0700900 commitTransaction(c);
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800901 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -0800902 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800903}
904
Pablo Ceballos05289c22016-04-14 15:49:55 -0700905void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800906 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700907}
908
Mathias Agopian13127d82013-03-05 17:47:11 -0800909uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800910 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800911}
912
913uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800914 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -0800915}
916
Robert Carr82364e32016-05-15 11:27:47 -0700917bool Layer::setPosition(float x, float y, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800918 if (mCurrentState.requested_legacy.transform.tx() == x &&
919 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -0800920 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800921 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -0700922
923 // We update the requested and active position simultaneously because
924 // we want to apply the position portion of the transform matrix immediately,
925 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800926 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -0700927 if (immediate && !mFreezeGeometryUpdates) {
928 // Here we directly update the active state
929 // unlike other setters, because we store it within
930 // the transform, but use different latching rules.
931 // b/38182305
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800932 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -0700933 }
Robert Carr7bf247e2017-05-18 14:02:49 -0700934 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -0700935
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800936 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800937 setTransactionFlags(eTransactionNeeded);
938 return true;
939}
Robert Carr82364e32016-05-15 11:27:47 -0700940
Robert Carr1f0a16a2016-10-24 16:27:39 -0700941bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
942 ssize_t idx = mCurrentChildren.indexOf(childLayer);
943 if (idx < 0) {
944 return false;
945 }
946 if (childLayer->setLayer(z)) {
947 mCurrentChildren.removeAt(idx);
948 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800949 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700950 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800951 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700952}
953
Robert Carr503c7042017-09-27 15:06:08 -0700954bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
955 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
956 ssize_t idx = mCurrentChildren.indexOf(childLayer);
957 if (idx < 0) {
958 return false;
959 }
960 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
961 mCurrentChildren.removeAt(idx);
962 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800963 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700964 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800965 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700966}
967
Robert Carrae060832016-11-28 10:51:00 -0800968bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800969 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
970 mCurrentState.sequence++;
971 mCurrentState.z = z;
972 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700973
974 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800975 if (mCurrentState.zOrderRelativeOf != nullptr) {
976 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700977 if (strongRelative != nullptr) {
978 strongRelative->removeZOrderRelative(this);
979 }
chaviw606e5cf2019-03-01 10:12:10 -0800980 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -0700981 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800982 setTransactionFlags(eTransactionNeeded);
983 return true;
984}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700985
Robert Carrdb66e622017-04-10 16:55:57 -0700986void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800987 mCurrentState.zOrderRelatives.remove(relative);
988 mCurrentState.sequence++;
989 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700990 setTransactionFlags(eTransactionNeeded);
991}
992
993void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800994 mCurrentState.zOrderRelatives.add(relative);
995 mCurrentState.modified = true;
996 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -0700997 setTransactionFlags(eTransactionNeeded);
998}
999
chaviw606e5cf2019-03-01 10:12:10 -08001000void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
1001 mCurrentState.zOrderRelativeOf = relativeOf;
1002 mCurrentState.sequence++;
1003 mCurrentState.modified = true;
1004 setTransactionFlags(eTransactionNeeded);
1005}
1006
Robert Carr503d2bd2017-12-04 15:49:47 -08001007bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001008 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1009 if (handle == nullptr) {
1010 return false;
1011 }
1012 sp<Layer> relative = handle->owner.promote();
1013 if (relative == nullptr) {
1014 return false;
1015 }
1016
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001017 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1018 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001019 return false;
1020 }
1021
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001022 mCurrentState.sequence++;
1023 mCurrentState.modified = true;
1024 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001025
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001026 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -07001027 if (oldZOrderRelativeOf != nullptr) {
1028 oldZOrderRelativeOf->removeZOrderRelative(this);
1029 }
chaviw606e5cf2019-03-01 10:12:10 -08001030 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -07001031 relative->addZOrderRelative(this);
1032
1033 setTransactionFlags(eTransactionNeeded);
1034
1035 return true;
1036}
1037
Mathias Agopian13127d82013-03-05 17:47:11 -08001038bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001039 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001040 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001041 mCurrentState.requested_legacy.w = w;
1042 mCurrentState.requested_legacy.h = h;
1043 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001044 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001045
1046 // record the new size, from this point on, when the client request
1047 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001048 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001049 return true;
1050}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001051bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001052 if (mCurrentState.color.a == alpha) return false;
1053 mCurrentState.sequence++;
1054 mCurrentState.color.a = alpha;
1055 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001056 setTransactionFlags(eTransactionNeeded);
1057 return true;
1058}
chaviw13fdc492017-06-27 12:40:18 -07001059
Valerie Haudd0b7572019-01-29 14:59:27 -08001060bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1061 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001062 return false;
Valerie Hauaa194562019-02-05 16:21:38 -08001063 }
1064 mCurrentState.sequence++;
1065 mCurrentState.modified = true;
1066 setTransactionFlags(eTransactionNeeded);
1067
1068 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001069 // create background color layer if one does not yet exist
1070 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
1071 const String8& name = mName + "BackgroundColorLayer";
Evan Roskya1f1e152019-01-24 16:17:46 -08001072 mCurrentState.bgColorLayer = new ColorLayer(
1073 LayerCreationArgs(mFlinger.get(), nullptr, name, 0, 0, flags, LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -07001074
Valerie Haudd0b7572019-01-29 14:59:27 -08001075 // add to child list
1076 addChild(mCurrentState.bgColorLayer);
1077 mFlinger->mLayersAdded = true;
1078 // set up SF to handle added color layer
1079 if (isRemovedFromCurrentState()) {
1080 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1081 }
1082 mFlinger->setTransactionFlags(eTransactionNeeded);
1083 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1084 mCurrentState.bgColorLayer->reparent(nullptr);
1085 mCurrentState.bgColorLayer = nullptr;
1086 return true;
1087 }
1088
1089 mCurrentState.bgColorLayer->setColor(color);
1090 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1091 mCurrentState.bgColorLayer->setAlpha(alpha);
1092 mCurrentState.bgColorLayer->setDataspace(dataspace);
1093
chaviw13fdc492017-06-27 12:40:18 -07001094 return true;
1095}
1096
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001097bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001098 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001099
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001100 mCurrentState.sequence++;
1101 mCurrentState.cornerRadius = cornerRadius;
1102 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001103 setTransactionFlags(eTransactionNeeded);
1104 return true;
1105}
1106
Robert Carrd4ae7f32018-06-07 16:10:57 -07001107bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1108 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001109 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001110 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1111
1112 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1113 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1114 return false;
1115 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001116 mCurrentState.sequence++;
1117 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1118 matrix.dsdy);
1119 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001120 setTransactionFlags(eTransactionNeeded);
1121 return true;
1122}
Marissa Wall61c58622018-07-18 10:12:20 -07001123
Mathias Agopian13127d82013-03-05 17:47:11 -08001124bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001125 mCurrentState.requestedTransparentRegion_legacy = transparent;
1126 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001127 setTransactionFlags(eTransactionNeeded);
1128 return true;
1129}
1130bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001131 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1132 if (mCurrentState.flags == newFlags) return false;
1133 mCurrentState.sequence++;
1134 mCurrentState.flags = newFlags;
1135 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001136 setTransactionFlags(eTransactionNeeded);
1137 return true;
1138}
Robert Carr99e27f02016-06-16 15:18:02 -07001139
Marissa Wallf58c14b2018-07-24 10:50:43 -07001140bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001141 if (mCurrentState.requestedCrop_legacy == crop) return false;
1142 mCurrentState.sequence++;
1143 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001144 if (immediate && !mFreezeGeometryUpdates) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001145 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001146 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001147 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1148
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001149 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001150 setTransactionFlags(eTransactionNeeded);
1151 return true;
1152}
Robert Carr8d5227b2017-03-16 15:41:03 -07001153
Robert Carrc3574f72016-03-24 12:19:32 -07001154bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001155 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001156 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001157 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001158 return true;
1159}
1160
Evan Roskyef876c92019-01-25 17:46:06 -08001161bool Layer::setMetadata(const LayerMetadata& data) {
1162 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001163 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001164 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001165 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001166 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001167}
1168
Mathias Agopian13127d82013-03-05 17:47:11 -08001169bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001170 if (mCurrentState.layerStack == layerStack) return false;
1171 mCurrentState.sequence++;
1172 mCurrentState.layerStack = layerStack;
1173 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001174 setTransactionFlags(eTransactionNeeded);
1175 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001176}
1177
Peiyong Linc502cb72019-03-01 15:00:23 -08001178bool Layer::setColorSpaceAgnostic(const bool agnostic) {
1179 if (mCurrentState.colorSpaceAgnostic == agnostic) {
1180 return false;
1181 }
1182 mCurrentState.sequence++;
1183 mCurrentState.colorSpaceAgnostic = agnostic;
1184 mCurrentState.modified = true;
1185 setTransactionFlags(eTransactionNeeded);
1186 return true;
1187}
1188
Robert Carr1f0a16a2016-10-24 16:27:39 -07001189uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001190 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001191 if (p == nullptr) {
1192 return getDrawingState().layerStack;
1193 }
1194 return p->getLayerStack();
1195}
1196
Marissa Wallf58c14b2018-07-24 10:50:43 -07001197void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001198 mCurrentState.barrierLayer_legacy = barrierLayer;
1199 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001200 // We don't set eTransactionNeeded, because just receiving a deferral
1201 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001202 mCurrentState.modified = true;
1203 pushPendingState();
1204 mCurrentState.barrierLayer_legacy = nullptr;
1205 mCurrentState.frameNumber_legacy = 0;
1206 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001207}
1208
Marissa Wallf58c14b2018-07-24 10:50:43 -07001209void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001210 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001211 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001212}
1213
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001214// ----------------------------------------------------------------------------
1215// pageflip handling...
1216// ----------------------------------------------------------------------------
1217
Robert Carr1f0a16a2016-10-24 16:27:39 -07001218bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001219 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001220 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001221 if (parent != nullptr && parent->isHiddenByPolicy()) {
1222 return true;
1223 }
1224 return s.flags & layer_state_t::eLayerHidden;
1225}
1226
David Sodman41fdfc92017-11-06 16:09:56 -08001227uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001228 // TODO: should we do something special if mSecure is set?
1229 if (mProtectedByApp) {
1230 // need a hardware-protected path to external video sink
1231 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001232 }
Riley Andrews03414a12014-07-01 14:22:59 -07001233 if (mPotentialCursor) {
1234 usage |= GraphicBuffer::USAGE_CURSOR;
1235 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001236 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001237 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001238}
1239
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001240void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001241 uint32_t orientation = 0;
Vishnu Nair5eb3f062019-04-08 08:21:03 -07001242 // Disable setting transform hint if the debug flag is set.
1243 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001244 // The transform hint is used to improve performance, but we can
1245 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001246 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001247 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001248 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001249 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001250 orientation = 0;
1251 }
1252 }
David Sodmaneb085e02017-10-05 18:49:04 -07001253 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001254}
1255
Mathias Agopian13127d82013-03-05 17:47:11 -08001256// ----------------------------------------------------------------------------
1257// debugging
1258// ----------------------------------------------------------------------------
1259
Marissa Wall61c58622018-07-18 10:12:20 -07001260// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001261LayerDebugInfo Layer::getLayerDebugInfo() const {
1262 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001263 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001264 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001265 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001266 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
Yiwei Zhang5434a782018-12-05 18:06:32 -08001267 info.mType = std::string(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001268 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001269 info.mVisibleRegion = visibleRegion;
1270 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001271 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001272 info.mX = ds.active_legacy.transform.tx();
1273 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001274 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001275 info.mWidth = ds.active_legacy.w;
1276 info.mHeight = ds.active_legacy.h;
1277 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001278 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001279 info.mFlags = ds.flags;
1280 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001281 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001282 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1283 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1284 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1285 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001286 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001287 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001288 if (buffer != 0) {
1289 info.mActiveBufferWidth = buffer->getWidth();
1290 info.mActiveBufferHeight = buffer->getHeight();
1291 info.mActiveBufferStride = buffer->getStride();
1292 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001293 } else {
1294 info.mActiveBufferWidth = 0;
1295 info.mActiveBufferHeight = 0;
1296 info.mActiveBufferStride = 0;
1297 info.mActiveBufferFormat = 0;
1298 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001299 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001300 info.mNumQueuedFrames = getQueuedFrameCount();
1301 info.mRefreshPending = isBufferLatched();
1302 info.mIsOpaque = isOpaque(ds);
1303 info.mContentDirty = contentDirty;
1304 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001305}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001306
Yiwei Zhang5434a782018-12-05 18:06:32 -08001307void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001308 result.append("-------------------------------");
1309 result.append("-------------------------------");
1310 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001311 result.append(" Layer name\n");
1312 result.append(" Z | ");
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001313 result.append(" Window Type | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001314 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001315 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001316 result.append(" Disp Frame (LTRB) | ");
1317 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001318 result.append("-------------------------------");
1319 result.append("-------------------------------");
1320 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001321}
1322
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001323void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1324 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1325 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001326 return;
1327 }
1328
Yiwei Zhang5434a782018-12-05 18:06:32 -08001329 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001330 if (mName.length() > 77) {
1331 std::string shortened;
1332 shortened.append(mName.string(), 36);
1333 shortened.append("[...]");
1334 shortened.append(mName.string() + (mName.length() - 36), 36);
Yiwei Zhang5434a782018-12-05 18:06:32 -08001335 name = shortened;
Dan Stozae22aec72016-08-01 13:20:59 -07001336 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001337 name = std::string(mName.string(), mName.size());
Dan Stozae22aec72016-08-01 13:20:59 -07001338 }
1339
Yiwei Zhang5434a782018-12-05 18:06:32 -08001340 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001341
Alec Mourib416efd2018-09-06 21:01:59 +00001342 const State& layerState(getDrawingState());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001343 const auto& compositionState = outputLayer->getState();
1344
Chia-I Wu1e043612018-03-01 09:45:09 -08001345 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001346 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001347 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001348 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001349 }
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001350 StringAppendF(&result, " %10d | ", mWindowType);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001351 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
1352 StringAppendF(&result, "%10s | ",
1353 toString(getCompositionLayer() ? compositionState.bufferTransform
1354 : static_cast<Hwc2::Transform>(0))
1355 .c_str());
1356 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001357 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001358 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001359 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1360 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001361
Yichi Chen6ca35192018-05-29 12:20:43 +08001362 result.append("- - - - - - - - - - - - - - - -");
1363 result.append("- - - - - - - - - - - - - - - -");
1364 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001365}
Dan Stozae22aec72016-08-01 13:20:59 -07001366
Yiwei Zhang5434a782018-12-05 18:06:32 -08001367void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001368 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001369}
1370
Svetoslavd85084b2014-03-20 10:28:31 -07001371void Layer::clearFrameStats() {
1372 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001373}
1374
Jamie Gennis6547ff42013-07-16 20:12:42 -07001375void Layer::logFrameStats() {
1376 mFrameTracker.logAndResetStats(mName);
1377}
1378
Svetoslavd85084b2014-03-20 10:28:31 -07001379void Layer::getFrameStats(FrameStats* outStats) const {
1380 mFrameTracker.getStats(outStats);
1381}
1382
Yiwei Zhang5434a782018-12-05 18:06:32 -08001383void Layer::dumpFrameEvents(std::string& result) {
1384 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001385 Mutex::Autolock lock(mFrameEventHistoryMutex);
1386 mFrameEventHistory.checkFencesForCompletion();
1387 mFrameEventHistory.dump(result);
1388}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001389
Brian Anderson5ea5e592016-12-01 16:54:33 -08001390void Layer::onDisconnect() {
1391 Mutex::Autolock lock(mFrameEventHistoryMutex);
1392 mFrameEventHistory.onDisconnect();
Yiwei Zhangaf8ee942018-11-22 00:15:23 -08001393 mFlinger->mTimeStats->onDestroy(getSequence());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001394}
1395
Brian Anderson3890c392016-07-25 12:48:08 -07001396void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001397 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001398 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001399 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1400 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001401 }
1402
Brian Andersond6927fb2016-07-23 23:37:30 -07001403 Mutex::Autolock lock(mFrameEventHistoryMutex);
1404 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001405 // If there are any unsignaled fences in the aquire timeline at this
1406 // point, the previously queued frame hasn't been latched yet. Go ahead
1407 // and try to get the signal time here so the syscall is taken out of
1408 // the main thread's critical path.
1409 mAcquireTimeline.updateSignalTimes();
1410 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001411 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001412 mFrameEventHistory.addQueue(*newTimestamps);
1413 }
1414
Brian Anderson3890c392016-07-25 12:48:08 -07001415 if (outDelta) {
1416 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001417 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001418}
Dan Stozae77c7662016-05-13 11:37:28 -07001419
Chia-I Wu98f1c102017-05-30 14:54:08 -07001420size_t Layer::getChildrenCount() const {
1421 size_t count = 0;
1422 for (const sp<Layer>& child : mCurrentChildren) {
1423 count += 1 + child->getChildrenCount();
1424 }
1425 return count;
1426}
1427
Robert Carr1f0a16a2016-10-24 16:27:39 -07001428void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001429 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001430 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001431
Robert Carr1f0a16a2016-10-24 16:27:39 -07001432 mCurrentChildren.add(layer);
1433 layer->setParent(this);
1434}
1435
1436ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001437 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001438 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001439
Robert Carr1323c952019-01-28 18:13:27 -08001440 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001441 return mCurrentChildren.remove(layer);
1442}
1443
Robert Carr1db73f62016-12-21 12:58:51 -08001444bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1445 sp<Handle> handle = nullptr;
1446 sp<Layer> newParent = nullptr;
1447 if (newParentHandle == nullptr) {
1448 return false;
1449 }
1450 handle = static_cast<Handle*>(newParentHandle.get());
1451 newParent = handle->owner.promote();
1452 if (newParent == nullptr) {
1453 ALOGE("Unable to promote Layer handle");
1454 return false;
1455 }
1456
chaviw5aedec92018-10-22 10:40:38 -07001457 if (attachChildren()) {
1458 setTransactionFlags(eTransactionNeeded);
1459 }
Robert Carr1db73f62016-12-21 12:58:51 -08001460 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001461 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001462 }
1463 mCurrentChildren.clear();
1464
1465 return true;
1466}
1467
Robert Carr15eae092018-03-23 13:43:53 -07001468void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001469 for (const sp<Layer>& child : mDrawingChildren) {
1470 child->mDrawingParent = newParent;
Vishnu Nairc652ff82019-03-15 12:48:54 -07001471 child->computeBounds(newParent->mBounds,
1472 newParent->getTransformWithScale(
1473 newParent->getBufferScaleTransform()));
Robert Carr578038f2018-03-09 12:25:24 -08001474 }
1475}
1476
chaviwf1961f72017-09-18 16:41:07 -07001477bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001478 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001479
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001480 // While layers are detached, we allow most operations
1481 // and simply halt performing the actual transaction. However
1482 // for reparent != null we would enter the mRemovedFromCurrentState
1483 // state, regardless of whether doTransaction was called, and
1484 // so we need to prevent the update here.
1485 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001486 return false;
1487 }
1488
Robert Carr54cf5b12019-01-25 14:02:28 -08001489 sp<Layer> newParent;
1490 if (newParentHandle != nullptr) {
1491 auto handle = static_cast<Handle*>(newParentHandle.get());
1492 newParent = handle->owner.promote();
1493 if (newParent == nullptr) {
1494 ALOGE("Unable to promote Layer handle");
1495 return false;
1496 }
1497 if (newParent == this) {
1498 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1499 return false;
1500 }
1501 }
1502
chaviwf1961f72017-09-18 16:41:07 -07001503 sp<Layer> parent = getParent();
1504 if (parent != nullptr) {
1505 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001506 }
1507
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001508 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001509 newParent->addChild(this);
1510 if (!newParent->isRemovedFromCurrentState()) {
1511 addToCurrentState();
1512 } else {
1513 onRemovedFromCurrentState();
1514 }
1515
1516 if (mLayerDetached) {
1517 mLayerDetached = false;
1518 callSetTransactionFlags = true;
1519 }
1520 } else {
1521 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001522 }
1523
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001524 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001525 setTransactionFlags(eTransactionNeeded);
1526 }
chaviw06178942017-07-27 10:25:59 -07001527 return true;
1528}
1529
Robert Carr9524cb32017-02-13 11:32:32 -08001530bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001531 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001532 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001533 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001534 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001535 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001536 child->detachChildren();
chaviw43cb3cb2019-05-31 15:23:41 -07001537 child->removeRemoteSyncPoints();
Robert Carr9524cb32017-02-13 11:32:32 -08001538 }
Robert Carr7f619b22017-11-06 12:56:35 -08001539 }
Robert Carr9524cb32017-02-13 11:32:32 -08001540
1541 return true;
1542}
1543
chaviw5aedec92018-10-22 10:40:38 -07001544bool Layer::attachChildren() {
1545 bool changed = false;
1546 for (const sp<Layer>& child : mCurrentChildren) {
1547 sp<Client> parentClient = mClientRef.promote();
1548 sp<Client> client(child->mClientRef.promote());
1549 if (client != nullptr && parentClient != client) {
1550 if (child->mLayerDetached) {
1551 child->mLayerDetached = false;
1552 changed = true;
1553 }
1554 changed |= child->attachChildren();
1555 }
1556 }
1557
1558 return changed;
1559}
1560
Peiyong Lind3788632018-09-18 16:01:31 -07001561bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001562 static const mat4 identityMatrix = mat4();
1563
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001564 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001565 return false;
1566 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001567 ++mCurrentState.sequence;
1568 mCurrentState.colorTransform = matrix;
1569 mCurrentState.hasColorTransform = matrix != identityMatrix;
1570 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001571 setTransactionFlags(eTransactionNeeded);
1572 return true;
1573}
1574
chaviwf66724d2018-11-28 16:35:21 -08001575mat4 Layer::getColorTransform() const {
1576 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1577 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1578 colorTransform = parent->getColorTransform() * colorTransform;
1579 }
1580 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001581}
1582
1583bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001584 bool hasColorTransform = getDrawingState().hasColorTransform;
1585 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1586 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1587 }
1588 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001589}
1590
Chia-I Wu11481472018-05-04 10:43:19 -07001591bool Layer::isLegacyDataSpace() const {
1592 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001593 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001594 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001595}
1596
Robert Carr1f0a16a2016-10-24 16:27:39 -07001597void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001598 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001599}
1600
Robert Carr1f0a16a2016-10-24 16:27:39 -07001601int32_t Layer::getZ() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001602 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001603}
1604
Robert Carr29abff82017-12-04 13:51:20 -08001605bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1606 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001607 const State& state = useDrawing ? mDrawingState : mCurrentState;
Robert Carr29abff82017-12-04 13:51:20 -08001608 return state.zOrderRelativeOf != nullptr;
1609}
1610
David Sodman41fdfc92017-11-06 16:09:56 -08001611__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001612 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001613 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1614 "makeTraversalList received invalid stateSet");
1615 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1616 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001617 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001618
Robert Carr29abff82017-12-04 13:51:20 -08001619 if (state.zOrderRelatives.size() == 0) {
1620 *outSkipRelativeZUsers = true;
1621 return children;
1622 }
1623
chaviwfd462612018-05-31 16:11:27 -07001624 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001625 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001626 sp<Layer> strongRelative = weakRelative.promote();
1627 if (strongRelative != nullptr) {
1628 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001629 }
1630 }
1631
Dan Stoza412903f2017-04-27 13:42:17 -07001632 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001633 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
Robert Carr503c7042017-09-27 15:06:08 -07001634 if (childState.zOrderRelativeOf != nullptr) {
1635 continue;
1636 }
Robert Carrdb66e622017-04-10 16:55:57 -07001637 traverse.add(child);
1638 }
1639
1640 return traverse;
1641}
1642
Robert Carr1f0a16a2016-10-24 16:27:39 -07001643/**
Robert Carrdb66e622017-04-10 16:55:57 -07001644 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001645 */
Dan Stoza412903f2017-04-27 13:42:17 -07001646void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001647 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1648 // produce a new list for traversing, including our relatives, and not including our children
1649 // who are relatives of another surface. In the case that there are no relative Z,
1650 // makeTraversalList returns our children directly to avoid significant overhead.
1651 // However in this case we need to take the responsibility for filtering children which
1652 // are relatives of another surface here.
1653 bool skipRelativeZUsers = false;
1654 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001655
Robert Carr1f0a16a2016-10-24 16:27:39 -07001656 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001657 for (; i < list.size(); i++) {
1658 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001659 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1660 continue;
1661 }
1662
Robert Carrdb66e622017-04-10 16:55:57 -07001663 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001664 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001665 }
Dan Stoza412903f2017-04-27 13:42:17 -07001666 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001667 }
Robert Carr29abff82017-12-04 13:51:20 -08001668
Dan Stoza412903f2017-04-27 13:42:17 -07001669 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001670 for (; i < list.size(); i++) {
1671 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001672
Robert Carr29abff82017-12-04 13:51:20 -08001673 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1674 continue;
1675 }
Dan Stoza412903f2017-04-27 13:42:17 -07001676 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001677 }
1678}
1679
1680/**
Robert Carrdb66e622017-04-10 16:55:57 -07001681 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001682 */
Dan Stoza412903f2017-04-27 13:42:17 -07001683void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1684 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001685 // See traverseInZOrder for documentation.
1686 bool skipRelativeZUsers = false;
1687 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001688
Robert Carr1f0a16a2016-10-24 16:27:39 -07001689 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001690 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001691 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001692
1693 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1694 continue;
1695 }
1696
Robert Carrdb66e622017-04-10 16:55:57 -07001697 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001698 break;
1699 }
Dan Stoza412903f2017-04-27 13:42:17 -07001700 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001701 }
Dan Stoza412903f2017-04-27 13:42:17 -07001702 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001703 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001704 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001705
1706 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1707 continue;
1708 }
1709
Dan Stoza412903f2017-04-27 13:42:17 -07001710 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001711 }
1712}
1713
chaviw4b129c22018-04-09 16:19:43 -07001714LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1715 const std::vector<Layer*>& layersInTree) {
1716 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1717 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001718 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1719 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001720 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001721
chaviwfd462612018-05-31 16:11:27 -07001722 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001723 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1724 sp<Layer> strongRelative = weakRelative.promote();
1725 // Only add relative layers that are also descendents of the top most parent of the tree.
1726 // If a relative layer is not a descendent, then it should be ignored.
1727 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1728 traverse.add(strongRelative);
1729 }
1730 }
1731
1732 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001733 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001734 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1735 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1736 // the child here since it won't be added later as a relative.
1737 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1738 childState.zOrderRelativeOf.promote().get())) {
1739 continue;
1740 }
1741 traverse.add(child);
1742 }
1743
1744 return traverse;
1745}
1746
1747void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1748 LayerVector::StateSet stateSet,
1749 const LayerVector::Visitor& visitor) {
1750 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001751
1752 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001753 for (; i < list.size(); i++) {
1754 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001755 if (relative->getZ() >= 0) {
1756 break;
1757 }
chaviw4b129c22018-04-09 16:19:43 -07001758 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001759 }
chaviw4b129c22018-04-09 16:19:43 -07001760
chaviwa76b2712017-09-20 12:02:26 -07001761 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001762 for (; i < list.size(); i++) {
1763 const auto& relative = list[i];
1764 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001765 }
1766}
1767
chaviw4b129c22018-04-09 16:19:43 -07001768std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1769 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1770 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1771
1772 std::vector<Layer*> layersInTree = {this};
1773 for (size_t i = 0; i < children.size(); i++) {
1774 const auto& child = children[i];
1775 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1776 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1777 }
1778
1779 return layersInTree;
1780}
1781
1782void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1783 const LayerVector::Visitor& visitor) {
1784 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1785 std::sort(layersInTree.begin(), layersInTree.end());
1786 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1787}
1788
Peiyong Linefefaac2018-08-17 12:27:51 -07001789ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001790 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001791}
1792
chaviw13fdc492017-06-27 12:40:18 -07001793half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001794 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001795
chaviw13fdc492017-06-27 12:40:18 -07001796 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1797 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001798}
Robert Carr6452f122017-03-21 10:41:29 -07001799
chaviw13fdc492017-06-27 12:40:18 -07001800half4 Layer::getColor() const {
1801 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001802 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001803}
Robert Carr6452f122017-03-21 10:41:29 -07001804
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001805Layer::RoundedCornerState Layer::getRoundedCornerState() const {
1806 const auto& p = mDrawingParent.promote();
1807 if (p != nullptr) {
Peiyong Lin27016a92019-03-29 17:36:08 +00001808 RoundedCornerState parentState = p->getRoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001809 if (parentState.radius > 0) {
1810 ui::Transform t = getActiveTransform(getDrawingState());
1811 t = t.inverse();
1812 parentState.cropRect = t.transform(parentState.cropRect);
1813 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1814 // but a transform matrix can define horizontal and vertical scales.
1815 // Let's take the average between both of them and pass into the shader, practically we
1816 // never do this type of transformation on windows anyway.
1817 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
1818 return parentState;
1819 }
1820 }
1821 const float radius = getDrawingState().cornerRadius;
Peiyong Linb9ff23e2019-04-08 11:04:59 -07001822 return radius > 0 && getCrop(getDrawingState()).isValid()
1823 ? RoundedCornerState(getCrop(getDrawingState()).toFloatRect(), radius)
1824 : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001825}
1826
Robert Carr1f0a16a2016-10-24 16:27:39 -07001827void Layer::commitChildList() {
1828 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1829 const auto& child = mCurrentChildren[i];
1830 child->commitChildList();
1831 }
1832 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001833 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001834}
1835
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001836static wp<Layer> extractLayerFromBinder(const wp<IBinder>& weakBinderHandle) {
1837 if (weakBinderHandle == nullptr) {
1838 return nullptr;
1839 }
1840 sp<IBinder> binderHandle = weakBinderHandle.promote();
1841 if (binderHandle == nullptr) {
1842 return nullptr;
1843 }
1844 sp<Layer::Handle> handle = static_cast<Layer::Handle*>(binderHandle.get());
1845 if (handle == nullptr) {
1846 return nullptr;
1847 }
1848 return handle->owner;
1849}
1850
Robert Carr720e5062018-07-30 17:45:14 -07001851void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001852 mCurrentState.inputInfo = info;
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001853 mCurrentState.touchableRegionCrop = extractLayerFromBinder(info.touchableRegionCropHandle);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001854 mCurrentState.modified = true;
1855 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07001856 setTransactionFlags(eTransactionNeeded);
1857}
1858
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001859void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet,
1860 uint32_t traceFlags) {
chaviw1d044282017-09-27 12:19:28 -07001861 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1862 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001863 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07001864
Peiyong Linefefaac2018-08-17 12:27:51 -07001865 ui::Transform requestedTransform = state.active_legacy.transform;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001866 ui::Transform transform = getTransform();
chaviw1d044282017-09-27 12:19:28 -07001867
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001868 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
1869 layerInfo->set_id(sequence);
1870 layerInfo->set_name(getName().c_str());
1871 layerInfo->set_type(String8(getTypeId()));
chaviw1d044282017-09-27 12:19:28 -07001872
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001873 for (const auto& child : children) {
1874 layerInfo->add_children(child->sequence);
chaviw1d044282017-09-27 12:19:28 -07001875 }
chaviw1d044282017-09-27 12:19:28 -07001876
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001877 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1878 sp<Layer> strongRelative = weakRelative.promote();
1879 if (strongRelative != nullptr) {
1880 layerInfo->add_relatives(strongRelative->sequence);
1881 }
chaviwadc40c22018-07-10 16:57:27 -07001882 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001883
1884 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
1885 [&]() { return layerInfo->mutable_transparent_region(); });
1886 LayerProtoHelper::writeToProto(visibleRegion,
1887 [&]() { return layerInfo->mutable_visible_region(); });
1888 LayerProtoHelper::writeToProto(surfaceDamageRegion,
1889 [&]() { return layerInfo->mutable_damage_region(); });
1890
1891 layerInfo->set_layer_stack(getLayerStack());
1892 layerInfo->set_z(state.z);
1893
1894 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
1895 [&]() { return layerInfo->mutable_position(); });
1896
1897 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(),
1898 [&]() {
1899 return layerInfo->mutable_requested_position();
1900 });
1901
1902 LayerProtoHelper::writeSizeToProto(state.active_legacy.w, state.active_legacy.h,
1903 [&]() { return layerInfo->mutable_size(); });
1904
1905 LayerProtoHelper::writeToProto(state.crop_legacy,
1906 [&]() { return layerInfo->mutable_crop(); });
1907 layerInfo->set_corner_radius(getRoundedCornerState().radius);
1908
1909 layerInfo->set_is_opaque(isOpaque(state));
1910 layerInfo->set_invalidate(contentDirty);
Peiyong Lin51598552019-04-17 14:09:22 -07001911 layerInfo->set_is_protected(isProtected());
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001912
1913 // XXX (b/79210409) mCurrentDataSpace is not protected
1914 layerInfo->set_dataspace(
1915 dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
1916
1917 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1918 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
1919 LayerProtoHelper::writeToProto(state.color,
1920 [&]() { return layerInfo->mutable_requested_color(); });
1921 layerInfo->set_flags(state.flags);
1922
1923 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1924 LayerProtoHelper::writeToProto(requestedTransform,
1925 layerInfo->mutable_requested_transform());
1926
1927 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
1928 if (parent != nullptr) {
1929 layerInfo->set_parent(parent->sequence);
1930 } else {
1931 layerInfo->set_parent(-1);
1932 }
1933
1934 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1935 if (zOrderRelativeOf != nullptr) {
1936 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1937 } else {
1938 layerInfo->set_z_order_relative_of(-1);
1939 }
1940
1941 auto buffer = mActiveBuffer;
1942 if (buffer != nullptr) {
1943 LayerProtoHelper::writeToProto(buffer,
1944 [&]() { return layerInfo->mutable_active_buffer(); });
1945 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
1946 layerInfo->mutable_buffer_transform());
1947 }
1948
1949 layerInfo->set_queued_frames(getQueuedFrameCount());
1950 layerInfo->set_refresh_pending(isBufferLatched());
1951 layerInfo->set_curr_frame(mCurrentFrameNumber);
1952 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
1953
1954 for (const auto& pendingState : mPendingStates) {
1955 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
1956 if (barrierLayer != nullptr) {
1957 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
1958 barrierLayerProto->set_id(barrierLayer->sequence);
1959 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
1960 }
1961 }
1962 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
chaviwadc40c22018-07-10 16:57:27 -07001963 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001964
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001965 if (traceFlags & SurfaceTracing::TRACE_INPUT) {
1966 LayerProtoHelper::writeToProto(state.inputInfo, state.touchableRegionCrop,
1967 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001968 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001969
1970 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
1971 auto protoMap = layerInfo->mutable_metadata();
1972 for (const auto& entry : state.metadata.mMap) {
1973 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
1974 }
1975 LayerProtoHelper::writeToProto(mEffectiveTransform,
1976 layerInfo->mutable_effective_transform());
1977 LayerProtoHelper::writeToProto(mSourceBounds,
1978 [&]() { return layerInfo->mutable_source_bounds(); });
1979 LayerProtoHelper::writeToProto(mScreenBounds,
1980 [&]() { return layerInfo->mutable_screen_bounds(); });
1981 }
chaviw1d044282017-09-27 12:19:28 -07001982}
1983
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001984void Layer::writeToProto(LayerProto* layerInfo, const sp<DisplayDevice>& displayDevice,
1985 uint32_t traceFlags) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001986 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1987 if (!outputLayer) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07001988 return;
1989 }
1990
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001991 writeToProto(layerInfo, LayerVector::StateSet::Drawing, traceFlags);
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001992
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001993 const auto& compositionState = outputLayer->getState();
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001994
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001995 const Rect& frame = compositionState.displayFrame;
Nataniel Borges797b0e42019-02-15 14:11:58 -08001996 LayerProtoHelper::writeToProto(frame, [&]() { return layerInfo->mutable_hwc_frame(); });
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001997
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001998 const FloatRect& crop = compositionState.sourceCrop;
Nataniel Borges797b0e42019-02-15 14:11:58 -08001999 LayerProtoHelper::writeToProto(crop, [&]() { return layerInfo->mutable_hwc_crop(); });
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002000
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002001 const int32_t transform =
2002 getCompositionLayer() ? static_cast<int32_t>(compositionState.bufferTransform) : 0;
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002003 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002004
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002005 const int32_t compositionType =
2006 static_cast<int32_t>(compositionState.hwc ? (*compositionState.hwc).hwcCompositionType
2007 : Hwc2::IComposerClient::Composition::CLIENT);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002008 layerInfo->set_hwc_composition_type(compositionType);
2009
2010 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
2011 static_cast<BufferLayer*>(this)->isProtected()) {
2012 layerInfo->set_is_protected(true);
2013 } else {
2014 layerInfo->set_is_protected(false);
2015 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002016}
2017
Robert Carr2e102c92018-10-23 12:11:15 -07002018bool Layer::isRemovedFromCurrentState() const {
2019 return mRemovedFromCurrentState;
2020}
2021
Arthur Hungd20b2702019-01-14 18:16:16 +08002022InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002023 InputWindowInfo info = mDrawingState.inputInfo;
Robert Carr720e5062018-07-30 17:45:14 -07002024
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002025 if (info.displayId == ADISPLAY_ID_NONE) {
2026 info.displayId = mDrawingState.layerStack;
2027 }
2028
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002029 ui::Transform t = getTransform();
2030 const float xScale = t.sx();
2031 const float yScale = t.sy();
Arthur Hung118b1142019-05-08 21:25:59 +08002032 float xSurfaceInset = info.surfaceInset;
2033 float ySurfaceInset = info.surfaceInset;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002034 if (xScale != 1.0f || yScale != 1.0f) {
2035 info.windowXScale *= 1.0f / xScale;
2036 info.windowYScale *= 1.0f / yScale;
2037 info.touchableRegion.scaleSelf(xScale, yScale);
Arthur Hung118b1142019-05-08 21:25:59 +08002038 xSurfaceInset *= xScale;
2039 ySurfaceInset *= yScale;
Riddle Hsu39d4aa52018-11-30 20:46:53 +08002040 }
Robert Carre07e1032018-11-26 12:55:53 -08002041
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002042 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002043 // If this is a portal window, set the touchableRegion to the layerBounds.
2044 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
2045 ? getBufferSize(getDrawingState())
2046 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002047 if (!layerBounds.isValid()) {
2048 layerBounds = getCroppedBufferSize(getDrawingState());
2049 }
Vishnu Nair8033a492018-12-05 07:27:23 -08002050 layerBounds = t.transform(layerBounds);
Arthur Hung118b1142019-05-08 21:25:59 +08002051 layerBounds.inset(xSurfaceInset, ySurfaceInset, xSurfaceInset, ySurfaceInset);
Vishnu Nair8033a492018-12-05 07:27:23 -08002052
Arthur Hungd20b2702019-01-14 18:16:16 +08002053 // Input coordinate should match the layer bounds.
2054 info.frameLeft = layerBounds.left;
2055 info.frameTop = layerBounds.top;
2056 info.frameRight = layerBounds.right;
2057 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08002058
2059 // Position the touchable region relative to frame screen location and restrict it to frame
2060 // bounds.
2061 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08002062 info.visible = canReceiveInput();
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002063
2064 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2065 if (info.replaceTouchableRegionWithCrop) {
2066 if (cropLayer == nullptr) {
2067 info.touchableRegion = Region(Rect{mScreenBounds});
2068 } else {
2069 info.touchableRegion = Region(Rect{cropLayer->mScreenBounds});
2070 }
2071 } else if (cropLayer != nullptr) {
2072 info.touchableRegion = info.touchableRegion.intersect(Rect{cropLayer->mScreenBounds});
2073 }
2074
Robert Carr720e5062018-07-30 17:45:14 -07002075 return info;
2076}
2077
2078bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002079 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002080}
2081
Lloyd Piquefeb73d72018-12-04 17:23:44 -08002082std::shared_ptr<compositionengine::Layer> Layer::getCompositionLayer() const {
2083 return nullptr;
2084}
2085
chaviw3e727cd2019-01-31 13:41:05 -08002086bool Layer::canReceiveInput() const {
2087 return isVisible();
2088}
2089
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002090compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
2091 const sp<const DisplayDevice>& display) const {
2092 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionLayer().get());
2093}
2094
Mathias Agopian13127d82013-03-05 17:47:11 -08002095// ---------------------------------------------------------------------------
2096
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002097}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002098
2099#if defined(__gl_h_)
2100#error "don't include gl/gl.h in this file"
2101#endif
2102
2103#if defined(__gl2_h_)
2104#error "don't include gl2/gl2.h in this file"
2105#endif