blob: de05f74a9b859eb5350674228e7af699b27b682f [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)
Lloyd Piquefa8fd6b2019-01-29 18:42:24 -080073 : mFlinger(args.flinger), mName(args.name), mClientRef(args.client) {
Mathias Agopiana67932f2011-04-20 14:20:59 -070074 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070075
76 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -070077 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
78 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
79 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070080
Dan Stozaf7ba41a2017-05-10 15:11:11 -070081 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070082
Lloyd Pique0449b0f2018-12-20 16:23:45 -080083 mCurrentState.active_legacy.w = args.w;
84 mCurrentState.active_legacy.h = args.h;
85 mCurrentState.flags = layerFlags;
86 mCurrentState.active_legacy.transform.set(0, 0);
87 mCurrentState.crop_legacy.makeInvalid();
88 mCurrentState.requestedCrop_legacy = mCurrentState.crop_legacy;
89 mCurrentState.z = 0;
90 mCurrentState.color.a = 1.0f;
91 mCurrentState.layerStack = 0;
92 mCurrentState.sequence = 0;
93 mCurrentState.requested_legacy = mCurrentState.active_legacy;
Lloyd Pique0449b0f2018-12-20 16:23:45 -080094 mCurrentState.active.w = UINT32_MAX;
95 mCurrentState.active.h = UINT32_MAX;
96 mCurrentState.active.transform.set(0, 0);
97 mCurrentState.transform = 0;
98 mCurrentState.transformToDisplayInverse = false;
99 mCurrentState.crop.makeInvalid();
100 mCurrentState.acquireFence = new Fence(-1);
101 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
102 mCurrentState.hdrMetadata.validTypes = 0;
103 mCurrentState.surfaceDamageRegion.clear();
104 mCurrentState.cornerRadius = 0.0f;
105 mCurrentState.api = -1;
106 mCurrentState.hasColorTransform = false;
Peiyong Linc502cb72019-03-01 15:00:23 -0800107 mCurrentState.colorSpaceAgnostic = false;
Evan Roskya1f1e152019-01-24 16:17:46 -0800108 mCurrentState.metadata = args.metadata;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700109
110 // drawing state & current state are identical
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800111 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700112
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800113 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700114 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800115 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200116 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700117
Ady Abraham09bd3922019-04-08 10:44:56 -0700118 mSchedulerLayerHandle = mFlinger->mScheduler->registerLayer(mName.c_str());
119
Robert Carr2e102c92018-10-23 12:11:15 -0700120 mFlinger->onLayerCreated();
Dan Stoza436ccf32018-06-21 12:10:12 -0700121}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700122
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700123Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800124 sp<Client> c(mClientRef.promote());
125 if (c != 0) {
126 c->detachLayer(this);
127 }
128
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000129 mFrameTracker.logAndResetStats(mName);
Robert Carr2e102c92018-10-23 12:11:15 -0700130
Robert Carr2e102c92018-10-23 12:11:15 -0700131 mFlinger->onLayerDestroyed();
Mathias Agopian96f08192010-06-02 23:28:45 -0700132}
133
Mathias Agopian13127d82013-03-05 17:47:11 -0800134// ---------------------------------------------------------------------------
135// callbacks
136// ---------------------------------------------------------------------------
137
David Sodmaneb085e02017-10-05 18:49:04 -0700138/*
139 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
140 * Layer. So, the implementation is done in BufferLayer. When called on a
141 * ColorLayer object, it's essentially a NOP.
142 */
David Sodmaneb085e02017-10-05 18:49:04 -0700143void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800144
Chia-I Wuc6657022017-08-15 11:18:17 -0700145void Layer::onRemovedFromCurrentState() {
Robert Carr2e102c92018-10-23 12:11:15 -0700146 mRemovedFromCurrentState = true;
147
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800148 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
149 if (mCurrentState.zOrderRelativeOf != nullptr) {
150 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
151 if (strongRelative != nullptr) {
152 strongRelative->removeZOrderRelative(this);
153 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700154 }
chaviw606e5cf2019-03-01 10:12:10 -0800155 setZOrderRelativeOf(nullptr);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700156 }
Rob Carr4bba3702018-10-08 21:53:30 +0000157
Robert Carr2e102c92018-10-23 12:11:15 -0700158 // Since we are no longer reachable from CurrentState SurfaceFlinger
159 // will no longer invoke doTransaction for us, and so we will
160 // never finish applying transactions. We signal the sync point
161 // now so that another layer will not become indefinitely
162 // blocked.
163 for (auto& point: mRemoteSyncPoints) {
164 point->setTransactionApplied();
165 }
166 mRemoteSyncPoints.clear();
167
168 {
169 Mutex::Autolock syncLock(mLocalSyncPointMutex);
170 for (auto& point : mLocalSyncPoints) {
171 point->setFrameAvailable();
172 }
173 mLocalSyncPoints.clear();
174 }
175
Chia-I Wuc6657022017-08-15 11:18:17 -0700176 for (const auto& child : mCurrentChildren) {
177 child->onRemovedFromCurrentState();
178 }
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800179
180 mFlinger->markLayerPendingRemovalLocked(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700181}
Chia-I Wu38512252017-05-17 14:36:16 -0700182
chaviw61626f22018-11-15 16:26:27 -0800183void Layer::addToCurrentState() {
184 mRemovedFromCurrentState = false;
185
186 for (const auto& child : mCurrentChildren) {
187 child->addToCurrentState();
188 }
189}
190
Mathias Agopian13127d82013-03-05 17:47:11 -0800191// ---------------------------------------------------------------------------
192// set-up
193// ---------------------------------------------------------------------------
194
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700195const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800196 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800197}
198
chaviw13fdc492017-06-27 12:40:18 -0700199bool Layer::getPremultipledAlpha() const {
200 return mPremultipliedAlpha;
201}
202
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700203sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800204 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700205 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800206}
207
208// ---------------------------------------------------------------------------
209// h/w composer set-up
210// ---------------------------------------------------------------------------
211
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800212bool Layer::hasHwcLayer(const sp<const DisplayDevice>& displayDevice) {
213 auto outputLayer = findOutputLayerForDisplay(displayDevice);
214 LOG_FATAL_IF(!outputLayer);
215 return outputLayer->getState().hwc && (*outputLayer->getState().hwc).hwcLayer != nullptr;
216}
217
218HWC2::Layer* Layer::getHwcLayer(const sp<const DisplayDevice>& displayDevice) {
219 auto outputLayer = findOutputLayerForDisplay(displayDevice);
220 if (!outputLayer || !outputLayer->getState().hwc) {
221 return nullptr;
222 }
223 return (*outputLayer->getState().hwc).hwcLayer.get();
Steven Thomasb02664d2017-07-26 18:48:28 -0700224}
Steven Thomasb02664d2017-07-26 18:48:28 -0700225
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800226Rect Layer::getContentCrop() const {
227 // this is the crop rectangle that applies to the buffer
228 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700229 Rect crop;
230 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800231 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700232 crop = mCurrentCrop;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800233 } else if (mActiveBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800234 // otherwise we use the whole buffer
Lloyd Pique0b785d82018-12-04 17:25:27 -0800235 crop = mActiveBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700236 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800237 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700238 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700239 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700240 return crop;
241}
242
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700243static Rect reduce(const Rect& win, const Region& exclude) {
244 if (CC_LIKELY(exclude.isEmpty())) {
245 return win;
246 }
247 if (exclude.isRect()) {
248 return win.reduce(exclude.getBounds());
249 }
250 return Region(win).subtract(exclude).getBounds();
251}
252
Dan Stoza80d61162017-12-20 15:57:52 -0800253static FloatRect reduce(const FloatRect& win, const Region& exclude) {
254 if (CC_LIKELY(exclude.isEmpty())) {
255 return win;
256 }
257 // Convert through Rect (by rounding) for lack of FloatRegion
258 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
259}
260
Vishnu Nair4351ad52019-02-11 14:13:02 -0800261Rect Layer::getScreenBounds(bool reduceTransparentRegion) const {
Vishnu Nairf0c28512019-02-08 12:40:28 -0800262 if (!reduceTransparentRegion) {
263 return Rect{mScreenBounds};
264 }
265
266 FloatRect bounds = getBounds();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800267 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800268 // Transform to screen space.
269 bounds = t.transform(bounds);
270 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700271}
272
Vishnu Nair4351ad52019-02-11 14:13:02 -0800273FloatRect Layer::getBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000274 const State& s(getDrawingState());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800275 return getBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700276}
277
Vishnu Nairf0c28512019-02-08 12:40:28 -0800278FloatRect Layer::getBounds(const Region& activeTransparentRegion) const {
279 // Subtract the transparent region and snap to the bounds.
280 return reduce(mBounds, activeTransparentRegion);
281}
282
Vishnu Nairc652ff82019-03-15 12:48:54 -0700283ui::Transform Layer::getBufferScaleTransform() const {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800284 // If the layer is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
285 // it isFixedSize) then there may be additional scaling not accounted
Vishnu Nairc652ff82019-03-15 12:48:54 -0700286 // for in the layer transform.
Lloyd Pique0b785d82018-12-04 17:25:27 -0800287 if (!isFixedSize() || !mActiveBuffer) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700288 return {};
Vishnu Nair4351ad52019-02-11 14:13:02 -0800289 }
290
Marissa Wall290ad082019-03-06 13:23:47 -0800291 // If the layer is a buffer state layer, the active width and height
292 // could be infinite. In that case, return the effective transform.
293 const uint32_t activeWidth = getActiveWidth(getDrawingState());
294 const uint32_t activeHeight = getActiveHeight(getDrawingState());
295 if (activeWidth >= UINT32_MAX && activeHeight >= UINT32_MAX) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700296 return {};
Marissa Wall290ad082019-03-06 13:23:47 -0800297 }
298
Vishnu Nairc652ff82019-03-15 12:48:54 -0700299 int bufferWidth = mActiveBuffer->getWidth();
300 int bufferHeight = mActiveBuffer->getHeight();
301
302 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
303 std::swap(bufferWidth, bufferHeight);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800304 }
Vishnu Nairc652ff82019-03-15 12:48:54 -0700305
Marissa Wall290ad082019-03-06 13:23:47 -0800306 float sx = activeWidth / static_cast<float>(bufferWidth);
307 float sy = activeHeight / static_cast<float>(bufferHeight);
308
Vishnu Nair4351ad52019-02-11 14:13:02 -0800309 ui::Transform extraParentScaling;
310 extraParentScaling.set(sx, 0, 0, sy);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700311 return extraParentScaling;
312}
313
314ui::Transform Layer::getTransformWithScale(const ui::Transform& bufferScaleTransform) const {
315 // We need to mirror this scaling to child surfaces or we will break the contract where WM can
316 // treat child surfaces as pixels in the parent surface.
317 if (!isFixedSize() || !mActiveBuffer) {
318 return mEffectiveTransform;
319 }
320 return mEffectiveTransform * bufferScaleTransform;
321}
322
323FloatRect Layer::getBoundsPreScaling(const ui::Transform& bufferScaleTransform) const {
324 // We need the pre scaled layer bounds when computing child bounds to make sure the child is
325 // cropped to its parent layer after any buffer transform scaling is applied.
326 if (!isFixedSize() || !mActiveBuffer) {
327 return mBounds;
328 }
329 return bufferScaleTransform.inverse().transform(mBounds);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800330}
331
332void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform) {
333 const State& s(getDrawingState());
334
335 // Calculate effective layer transform
336 mEffectiveTransform = parentTransform * getActiveTransform(s);
337
338 // Transform parent bounds to layer space
339 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
340
Vishnu Nairc652ff82019-03-15 12:48:54 -0700341 // Calculate source bounds
Vishnu Nair4351ad52019-02-11 14:13:02 -0800342 mSourceBounds = computeSourceBounds(parentBounds);
343
344 // Calculate bounds by croping diplay frame with layer crop and parent bounds
345 FloatRect bounds = mSourceBounds;
346 const Rect layerCrop = getCrop(s);
347 if (!layerCrop.isEmpty()) {
348 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
349 }
350 bounds = bounds.intersect(parentBounds);
351
352 mBounds = bounds;
353 mScreenBounds = mEffectiveTransform.transform(mBounds);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700354
355 // Add any buffer scaling to the layer's children.
356 ui::Transform bufferScaleTransform = getBufferScaleTransform();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800357 for (const sp<Layer>& child : mDrawingChildren) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700358 child->computeBounds(getBoundsPreScaling(bufferScaleTransform),
359 getTransformWithScale(bufferScaleTransform));
Vishnu Nair4351ad52019-02-11 14:13:02 -0800360 }
361}
362
Vishnu Nair60356342018-11-13 13:00:45 -0800363Rect Layer::getCroppedBufferSize(const State& s) const {
364 Rect size = getBufferSize(s);
365 Rect crop = getCrop(s);
366 if (!crop.isEmpty() && size.isValid()) {
367 size.intersect(crop, &size);
368 } else if (!crop.isEmpty()) {
369 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700370 }
Vishnu Nair60356342018-11-13 13:00:45 -0800371 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800372}
373
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700374void Layer::setupRoundedCornersCropCoordinates(Rect win,
375 const FloatRect& roundedCornersCrop) const {
376 // Translate win by the rounded corners rect coordinates, to have all values in
377 // layer coordinate space.
378 win.left -= roundedCornersCrop.left;
379 win.right -= roundedCornersCrop.left;
380 win.top -= roundedCornersCrop.top;
381 win.bottom -= roundedCornersCrop.top;
382
Lloyd Pique0b785d82018-12-04 17:25:27 -0800383 renderengine::Mesh::VertexArray<vec2> cropCoords(
384 getCompositionLayer()->editState().reMesh.getCropCoordArray<vec2>());
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700385 cropCoords[0] = vec2(win.left, win.top);
386 cropCoords[1] = vec2(win.left, win.top + win.getHeight());
387 cropCoords[2] = vec2(win.right, win.top + win.getHeight());
388 cropCoords[3] = vec2(win.right, win.top);
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700389}
390
Lloyd Piquea83776c2019-01-29 18:42:32 -0800391void Layer::latchGeometry(compositionengine::LayerFECompositionState& compositionState) const {
392 const auto& drawingState{getDrawingState()};
393 auto alpha = static_cast<float>(getAlpha());
David Revemanecf0fa52017-03-03 11:32:44 -0500394 auto blendMode = HWC2::BlendMode::None;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800395 if (!isOpaque(drawingState) || alpha != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800396 blendMode =
397 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800398 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800399
Lloyd Piquea83776c2019-01-29 18:42:32 -0800400 int type = drawingState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
401 int appId = drawingState.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700402 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400403 if (parent.get()) {
404 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800405 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
406 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
407 if (parentType >= 0 || parentAppId >= 0) {
408 type = parentType;
409 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700410 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400411 }
412
Lloyd Piquea83776c2019-01-29 18:42:32 -0800413 compositionState.geomLayerTransform = getTransform();
414 compositionState.geomInverseLayerTransform = compositionState.geomLayerTransform.inverse();
415 compositionState.geomBufferSize = getBufferSize(drawingState);
416 compositionState.geomContentCrop = getContentCrop();
417 compositionState.geomCrop = getCrop(drawingState);
418 compositionState.geomBufferTransform = mCurrentTransform;
419 compositionState.geomBufferUsesDisplayInverseTransform = getTransformToDisplayInverse();
420 compositionState.geomActiveTransparentRegion = getActiveTransparentRegion(drawingState);
421 compositionState.geomLayerBounds = mBounds;
422 compositionState.geomUsesSourceCrop = usesSourceCrop();
423 compositionState.isSecure = isSecure();
David Sodman15094112018-10-11 09:39:37 -0700424
Lloyd Piquea83776c2019-01-29 18:42:32 -0800425 compositionState.blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
426 compositionState.alpha = alpha;
427 compositionState.type = type;
428 compositionState.appId = appId;
429}
David Sodmanba340492018-08-05 21:51:33 -0700430
Lloyd Piquea83776c2019-01-29 18:42:32 -0800431void Layer::latchCompositionState(compositionengine::LayerFECompositionState& compositionState,
432 bool includeGeometry) const {
433 if (includeGeometry) {
434 latchGeometry(compositionState);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700435 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800436}
Mathias Agopian29a367b2011-07-12 14:51:45 -0700437
Lloyd Piquea83776c2019-01-29 18:42:32 -0800438const char* Layer::getDebugName() const {
439 return mName.string();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800440}
441
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800442void Layer::forceClientComposition(const sp<DisplayDevice>& display) {
443 const auto outputLayer = findOutputLayerForDisplay(display);
444 LOG_FATAL_IF(!outputLayer);
445 outputLayer->editState().forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800446}
Dan Stozaee44edd2015-03-23 15:50:23 -0700447
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800448bool Layer::getForceClientComposition(const sp<DisplayDevice>& display) {
449 const auto outputLayer = findOutputLayerForDisplay(display);
450 LOG_FATAL_IF(!outputLayer);
451 return outputLayer->getState().forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800452}
453
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700454void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800455 const auto outputLayer = findOutputLayerForDisplay(display);
456 LOG_FATAL_IF(!outputLayer);
457
458 if (!outputLayer->getState().hwc ||
459 (*outputLayer->getState().hwc).hwcCompositionType !=
460 Hwc2::IComposerClient::Composition::CURSOR) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800461 return;
462 }
463
464 // This gives us only the "orientation" component of the transform
Vishnu Nair33a6eee2019-02-06 13:48:06 -0800465 const State& s(getDrawingState());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800466
467 // Apply the layer's transform, followed by the display's global transform
468 // Here we're guaranteed that the layer's transform preserves rects
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800469 Rect win = getCroppedBufferSize(s);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800470 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700471 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800472 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700473 frame.intersect(display->getViewport(), &frame);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700474 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800475 auto position = displayTransform.transform(frame);
476
Dominik Laskowski7e045462018-05-30 13:02:02 -0700477 auto error =
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800478 (*outputLayer->getState().hwc).hwcLayer->setCursorPosition(position.left, position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800479 ALOGE_IF(error != HWC2::Error::None,
480 "[%s] Failed to set cursor position "
481 "to (%d, %d): %s (%d)",
482 mName.string(), position.left, position.top, to_string(error).c_str(),
483 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800484}
Riley Andrews03414a12014-07-01 14:22:59 -0700485
Mathias Agopian13127d82013-03-05 17:47:11 -0800486// ---------------------------------------------------------------------------
487// drawing...
488// ---------------------------------------------------------------------------
489
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000490bool Layer::prepareClientLayer(const RenderArea& renderArea, const Region& clip,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800491 Region& clearRegion, const bool supportProtectedContent,
492 renderengine::LayerSettings& layer) {
493 return prepareClientLayer(renderArea, clip, false, clearRegion, supportProtectedContent, layer);
Mathias Agopian13127d82013-03-05 17:47:11 -0800494}
495
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000496bool Layer::prepareClientLayer(const RenderArea& renderArea, bool useIdentityTransform,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800497 Region& clearRegion, const bool supportProtectedContent,
498 renderengine::LayerSettings& layer) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000499 return prepareClientLayer(renderArea, Region(renderArea.getBounds()), useIdentityTransform,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800500 clearRegion, supportProtectedContent, layer);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000501}
502
503bool Layer::prepareClientLayer(const RenderArea& /*renderArea*/, const Region& /*clip*/,
504 bool useIdentityTransform, Region& /*clearRegion*/,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800505 const bool /*supportProtectedContent*/,
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000506 renderengine::LayerSettings& layer) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800507 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000508 half alpha = getAlpha();
509 layer.geometry.boundaries = bounds;
510 if (useIdentityTransform) {
511 layer.geometry.positionTransform = mat4();
512 } else {
513 const ui::Transform transform = getTransform();
514 mat4 m;
515 m[0][0] = transform[0][0];
516 m[0][1] = transform[0][1];
517 m[0][3] = transform[0][2];
518 m[1][0] = transform[1][0];
519 m[1][1] = transform[1][1];
520 m[1][3] = transform[1][2];
521 m[3][0] = transform[2][0];
522 m[3][1] = transform[2][1];
523 m[3][3] = transform[2][2];
524 layer.geometry.positionTransform = m;
525 }
526
527 if (hasColorTransform()) {
528 layer.colorTransform = getColorTransform();
529 }
530
531 const auto roundedCornerState = getRoundedCornerState();
532 layer.geometry.roundedCornersRadius = roundedCornerState.radius;
533 layer.geometry.roundedCornersCrop = roundedCornerState.cropRect;
534
535 layer.alpha = alpha;
536 layer.sourceDataspace = mCurrentDataSpace;
537 return true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800538}
539
David Sodman41fdfc92017-11-06 16:09:56 -0800540void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
541 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800542 auto& engine(mFlinger->getRenderEngine());
Lloyd Pique0b785d82018-12-04 17:25:27 -0800543 computeGeometry(renderArea, getCompositionLayer()->editState().reMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700544 engine.setupFillWithColor(red, green, blue, alpha);
Lloyd Pique0b785d82018-12-04 17:25:27 -0800545 engine.drawMesh(getCompositionLayer()->getState().reMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800546}
547
David Sodmanc1498e62018-09-12 14:36:26 -0700548void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
549 clearWithOpenGL(renderArea, 0, 0, 0, 0);
550}
551
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800552void Layer::setCompositionType(const sp<const DisplayDevice>& display,
553 Hwc2::IComposerClient::Composition type) {
554 const auto outputLayer = findOutputLayerForDisplay(display);
555 LOG_FATAL_IF(!outputLayer);
556 LOG_FATAL_IF(!outputLayer->getState().hwc);
557 auto& compositionState = outputLayer->editState();
558
559 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", ((*compositionState.hwc).hwcLayer)->getId(),
560 toString(type).c_str(), 1);
561 if ((*compositionState.hwc).hwcCompositionType != type) {
David Sodman15094112018-10-11 09:39:37 -0700562 ALOGV(" actually setting");
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800563 (*compositionState.hwc).hwcCompositionType = type;
564
565 auto error = (*compositionState.hwc)
566 .hwcLayer->setCompositionType(static_cast<HWC2::Composition>(type));
567 ALOGE_IF(error != HWC2::Error::None,
568 "[%s] Failed to set "
569 "composition type %s: %s (%d)",
570 mName.string(), toString(type).c_str(), to_string(error).c_str(),
571 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800572 }
573}
574
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800575Hwc2::IComposerClient::Composition Layer::getCompositionType(
576 const sp<const DisplayDevice>& display) const {
577 const auto outputLayer = findOutputLayerForDisplay(display);
578 LOG_FATAL_IF(!outputLayer);
579 return outputLayer->getState().hwc ? (*outputLayer->getState().hwc).hwcCompositionType
580 : Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800581}
582
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800583bool Layer::getClearClientTarget(const sp<const DisplayDevice>& display) const {
584 const auto outputLayer = findOutputLayerForDisplay(display);
585 LOG_FATAL_IF(!outputLayer);
586 return outputLayer->getState().clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800587}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800588
Dan Stozacac35382016-01-27 12:21:06 -0800589bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
590 if (point->getFrameNumber() <= mCurrentFrameNumber) {
591 // Don't bother with a SyncPoint, since we've already latched the
592 // relevant frame
593 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700594 }
Robert Carr2e102c92018-10-23 12:11:15 -0700595 if (isRemovedFromCurrentState()) {
596 return false;
597 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700598
Dan Stozacac35382016-01-27 12:21:06 -0800599 Mutex::Autolock lock(mLocalSyncPointMutex);
600 mLocalSyncPoints.push_back(point);
601 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700602}
603
Mathias Agopian13127d82013-03-05 17:47:11 -0800604// ----------------------------------------------------------------------------
605// local state
606// ----------------------------------------------------------------------------
607
Peiyong Lin833074a2018-08-28 11:53:54 -0700608void Layer::computeGeometry(const RenderArea& renderArea,
609 renderengine::Mesh& mesh,
chaviwa76b2712017-09-20 12:02:26 -0700610 bool useIdentityTransform) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700611 const ui::Transform renderAreaTransform(renderArea.getTransform());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800612 FloatRect win = getBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700613
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000614 vec2 lt = vec2(win.left, win.top);
615 vec2 lb = vec2(win.left, win.bottom);
616 vec2 rb = vec2(win.right, win.bottom);
617 vec2 rt = vec2(win.right, win.top);
618
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800619 ui::Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000620 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700621 lt = layerTransform.transform(lt);
622 lb = layerTransform.transform(lb);
623 rb = layerTransform.transform(rb);
624 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000625 }
626
Peiyong Lin833074a2018-08-28 11:53:54 -0700627 renderengine::Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700628 position[0] = renderAreaTransform.transform(lt);
629 position[1] = renderAreaTransform.transform(lb);
630 position[2] = renderAreaTransform.transform(rb);
631 position[3] = renderAreaTransform.transform(rt);
Mathias Agopian13127d82013-03-05 17:47:11 -0800632}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800633
David Sodman41fdfc92017-11-06 16:09:56 -0800634bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800635 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700636 return (s.flags & layer_state_t::eLayerSecure);
637}
638
Mathias Agopian13127d82013-03-05 17:47:11 -0800639void Layer::setVisibleRegion(const Region& visibleRegion) {
640 // always called from main thread
641 this->visibleRegion = visibleRegion;
642}
643
644void Layer::setCoveredRegion(const Region& coveredRegion) {
645 // always called from main thread
646 this->coveredRegion = coveredRegion;
647}
648
David Sodman41fdfc92017-11-06 16:09:56 -0800649void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800650 // always called from main thread
651 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
652}
653
Robert Carre5f4f692018-01-12 13:12:28 -0800654void Layer::clearVisibilityRegions() {
655 visibleRegion.clear();
656 visibleNonTransparentRegion.clear();
657 coveredRegion.clear();
658}
659
Mathias Agopian13127d82013-03-05 17:47:11 -0800660// ----------------------------------------------------------------------------
661// transaction
662// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800663
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800664void Layer::pushPendingState() {
665 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700666 return;
667 }
668
Dan Stoza7dde5992015-05-22 09:51:44 -0700669 // If this transaction is waiting on the receipt of a frame, generate a sync
670 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700671 // We don't allow installing sync points after we are removed from the current state
672 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800673 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
674 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800675 if (barrierLayer == nullptr) {
676 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700677 // If we can't promote the layer we are intended to wait on,
678 // then it is expired or otherwise invalid. Allow this transaction
679 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800680 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800681 } else {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800682 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy);
Robert Carr0d480722017-01-10 16:42:54 -0800683 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800684 mRemoteSyncPoints.push_back(std::move(syncPoint));
685 } else {
686 // We already missed the frame we're supposed to synchronize
687 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800688 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800689 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700690 }
691
Dan Stoza7dde5992015-05-22 09:51:44 -0700692 // Wake us up to check if the frame has been received
693 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700694 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700695 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800696 mPendingStates.push_back(mCurrentState);
697 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700698}
699
Pablo Ceballos05289c22016-04-14 15:49:55 -0700700void Layer::popPendingState(State* stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800701 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700702
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800703 mPendingStates.removeAt(0);
704 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700705}
706
Pablo Ceballos05289c22016-04-14 15:49:55 -0700707bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700708 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800709 while (!mPendingStates.empty()) {
710 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700711 if (mRemoteSyncPoints.empty()) {
712 // If we don't have a sync point for this, apply it anyway. It
713 // will be visually wrong, but it should keep us from getting
714 // into too much trouble.
715 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700716 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700717 stateUpdateAvailable = true;
718 continue;
719 }
720
Marissa Wallf58c14b2018-07-24 10:50:43 -0700721 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800722 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800723 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800724
725 // Signal our end of the sync point and then dispose of it
726 mRemoteSyncPoints.front()->setTransactionApplied();
727 mRemoteSyncPoints.pop_front();
728 continue;
729 }
730
Dan Stoza7dde5992015-05-22 09:51:44 -0700731 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
732 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700733 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700734 stateUpdateAvailable = true;
735
736 // Signal our end of the sync point and then dispose of it
737 mRemoteSyncPoints.front()->setTransactionApplied();
738 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800739 } else {
740 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700741 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700742 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700743 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700744 stateUpdateAvailable = true;
745 }
746 }
747
748 // If we still have pending updates, wake SurfaceFlinger back up and point
749 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800750 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700751 setTransactionFlags(eTransactionNeeded);
752 mFlinger->setTransactionFlags(eTraversalNeeded);
753 }
754
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800755 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700756 return stateUpdateAvailable;
757}
758
Marissa Wall61c58622018-07-18 10:12:20 -0700759uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000760 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800761
Marissa Wall61c58622018-07-18 10:12:20 -0700762 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
763 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700764
David Sodmaneb085e02017-10-05 18:49:04 -0700765 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700766 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000767 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800768 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
769 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
770 " requested={ wh={%4u,%4u} }}\n"
771 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
772 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700773 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700774 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
775 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
776 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
777 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
778 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700779 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
780 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
781 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800782 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700783
Robert Carre392b552017-09-19 12:16:05 -0700784 // Don't let Layer::doTransaction update the drawing state
785 // if we have a pending resize, unless we are in fixed-size mode.
786 // the drawing state will be updated only once we receive a buffer
787 // with the correct size.
788 //
789 // In particular, we want to make sure the clip (which is part
790 // of the geometry state) is latched together with the size but is
791 // latched immediately when no resizing is involved.
792 //
793 // If a sideband stream is attached, however, we want to skip this
794 // optimization so that transactions aren't missed when a buffer
795 // never arrives
796 //
797 // In the case that we don't have a buffer we ignore other factors
798 // and avoid entering the resizePending state. At a high level the
799 // resizePending state is to avoid applying the state of the new buffer
800 // to the old buffer. However in the state where we don't have an old buffer
801 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700802 const bool resizePending =
803 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
804 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
Lloyd Pique0b785d82018-12-04 17:25:27 -0800805 (mActiveBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700806 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800807 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700808 flags |= eDontUpdateGeometryState;
809 }
810 }
811
Robert Carr7bf247e2017-05-18 14:02:49 -0700812 // Here we apply various requested geometry states, depending on our
813 // latching configuration. See Layer.h for a detailed discussion of
814 // how geometry latching is controlled.
815 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000816 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700817
818 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
819 // mode, which causes attributes which normally latch regardless of scaling mode,
820 // to be delayed. We copy the requested state to the active state making sure
821 // to respect these rules (again see Layer.h for a detailed discussion).
822 //
823 // There is an awkward asymmetry in the handling of the crop states in the position
824 // states, as can be seen below. Largely this arises from position and transform
825 // being stored in the same data structure while having different latching rules.
826 // b/38182305
827 //
Marissa Wall61c58622018-07-18 10:12:20 -0700828 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700829 // applyPendingStates in the presence of deferred transactions.
830 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -0700831 float tx = stateToCommit->active_legacy.transform.tx();
832 float ty = stateToCommit->active_legacy.transform.ty();
833 stateToCommit->active_legacy = stateToCommit->requested_legacy;
834 stateToCommit->active_legacy.transform.set(tx, ty);
835 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700836 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700837 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -0700838 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700839 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800840 }
841
Marissa Wall61c58622018-07-18 10:12:20 -0700842 return flags;
843}
844
845uint32_t Layer::doTransaction(uint32_t flags) {
846 ATRACE_CALL();
847
chaviw5aedec92018-10-22 10:40:38 -0700848 if (mLayerDetached) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800849 return flags;
850 }
851
852 if (mChildrenChanged) {
853 flags |= eVisibleRegion;
854 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -0700855 }
856
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800857 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +0000858 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -0700859 if (!applyPendingStates(&c)) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800860 return flags;
Marissa Wall61c58622018-07-18 10:12:20 -0700861 }
862
863 flags = doTransactionResize(flags, &c);
864
Alec Mourib416efd2018-09-06 21:01:59 +0000865 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700866
867 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800868 // invalidate and recompute the visible regions if needed
869 flags |= Layer::eVisibleRegion;
870 }
871
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700872 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800873 // invalidate and recompute the visible regions if needed
874 flags |= eVisibleRegion;
875 this->contentDirty = true;
876
877 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -0700878 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -0700879 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -0800880 }
881
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800882 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -0700883 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800884 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -0700885 }
886
Mathias Agopian13127d82013-03-05 17:47:11 -0800887 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -0700888 commitTransaction(c);
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800889 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -0800890 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800891}
892
Pablo Ceballos05289c22016-04-14 15:49:55 -0700893void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800894 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700895}
896
Mathias Agopian13127d82013-03-05 17:47:11 -0800897uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800898 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800899}
900
901uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800902 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -0800903}
904
Robert Carr82364e32016-05-15 11:27:47 -0700905bool Layer::setPosition(float x, float y, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800906 if (mCurrentState.requested_legacy.transform.tx() == x &&
907 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -0800908 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800909 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -0700910
911 // We update the requested and active position simultaneously because
912 // we want to apply the position portion of the transform matrix immediately,
913 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800914 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -0700915 if (immediate && !mFreezeGeometryUpdates) {
916 // Here we directly update the active state
917 // unlike other setters, because we store it within
918 // the transform, but use different latching rules.
919 // b/38182305
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800920 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -0700921 }
Robert Carr7bf247e2017-05-18 14:02:49 -0700922 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -0700923
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800924 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800925 setTransactionFlags(eTransactionNeeded);
926 return true;
927}
Robert Carr82364e32016-05-15 11:27:47 -0700928
Robert Carr1f0a16a2016-10-24 16:27:39 -0700929bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
930 ssize_t idx = mCurrentChildren.indexOf(childLayer);
931 if (idx < 0) {
932 return false;
933 }
934 if (childLayer->setLayer(z)) {
935 mCurrentChildren.removeAt(idx);
936 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800937 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700938 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800939 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700940}
941
Robert Carr503c7042017-09-27 15:06:08 -0700942bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
943 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
944 ssize_t idx = mCurrentChildren.indexOf(childLayer);
945 if (idx < 0) {
946 return false;
947 }
948 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
949 mCurrentChildren.removeAt(idx);
950 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800951 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700952 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800953 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700954}
955
Robert Carrae060832016-11-28 10:51:00 -0800956bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800957 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
958 mCurrentState.sequence++;
959 mCurrentState.z = z;
960 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700961
962 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800963 if (mCurrentState.zOrderRelativeOf != nullptr) {
964 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700965 if (strongRelative != nullptr) {
966 strongRelative->removeZOrderRelative(this);
967 }
chaviw606e5cf2019-03-01 10:12:10 -0800968 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -0700969 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800970 setTransactionFlags(eTransactionNeeded);
971 return true;
972}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700973
Robert Carrdb66e622017-04-10 16:55:57 -0700974void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800975 mCurrentState.zOrderRelatives.remove(relative);
976 mCurrentState.sequence++;
977 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700978 setTransactionFlags(eTransactionNeeded);
979}
980
981void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800982 mCurrentState.zOrderRelatives.add(relative);
983 mCurrentState.modified = true;
984 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -0700985 setTransactionFlags(eTransactionNeeded);
986}
987
chaviw606e5cf2019-03-01 10:12:10 -0800988void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
989 mCurrentState.zOrderRelativeOf = relativeOf;
990 mCurrentState.sequence++;
991 mCurrentState.modified = true;
992 setTransactionFlags(eTransactionNeeded);
993}
994
Robert Carr503d2bd2017-12-04 15:49:47 -0800995bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -0700996 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
997 if (handle == nullptr) {
998 return false;
999 }
1000 sp<Layer> relative = handle->owner.promote();
1001 if (relative == nullptr) {
1002 return false;
1003 }
1004
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001005 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1006 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001007 return false;
1008 }
1009
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001010 mCurrentState.sequence++;
1011 mCurrentState.modified = true;
1012 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001013
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001014 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -07001015 if (oldZOrderRelativeOf != nullptr) {
1016 oldZOrderRelativeOf->removeZOrderRelative(this);
1017 }
chaviw606e5cf2019-03-01 10:12:10 -08001018 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -07001019 relative->addZOrderRelative(this);
1020
1021 setTransactionFlags(eTransactionNeeded);
1022
1023 return true;
1024}
1025
Mathias Agopian13127d82013-03-05 17:47:11 -08001026bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001027 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001028 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001029 mCurrentState.requested_legacy.w = w;
1030 mCurrentState.requested_legacy.h = h;
1031 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001032 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001033
1034 // record the new size, from this point on, when the client request
1035 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001036 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001037 return true;
1038}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001039bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001040 if (mCurrentState.color.a == alpha) return false;
1041 mCurrentState.sequence++;
1042 mCurrentState.color.a = alpha;
1043 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001044 setTransactionFlags(eTransactionNeeded);
1045 return true;
1046}
chaviw13fdc492017-06-27 12:40:18 -07001047
Valerie Haudd0b7572019-01-29 14:59:27 -08001048bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1049 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001050 return false;
Valerie Hauaa194562019-02-05 16:21:38 -08001051 }
1052 mCurrentState.sequence++;
1053 mCurrentState.modified = true;
1054 setTransactionFlags(eTransactionNeeded);
1055
1056 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001057 // create background color layer if one does not yet exist
1058 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
1059 const String8& name = mName + "BackgroundColorLayer";
Evan Roskya1f1e152019-01-24 16:17:46 -08001060 mCurrentState.bgColorLayer = new ColorLayer(
1061 LayerCreationArgs(mFlinger.get(), nullptr, name, 0, 0, flags, LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -07001062
Valerie Haudd0b7572019-01-29 14:59:27 -08001063 // add to child list
1064 addChild(mCurrentState.bgColorLayer);
1065 mFlinger->mLayersAdded = true;
1066 // set up SF to handle added color layer
1067 if (isRemovedFromCurrentState()) {
1068 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1069 }
1070 mFlinger->setTransactionFlags(eTransactionNeeded);
1071 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1072 mCurrentState.bgColorLayer->reparent(nullptr);
1073 mCurrentState.bgColorLayer = nullptr;
1074 return true;
1075 }
1076
1077 mCurrentState.bgColorLayer->setColor(color);
1078 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1079 mCurrentState.bgColorLayer->setAlpha(alpha);
1080 mCurrentState.bgColorLayer->setDataspace(dataspace);
1081
chaviw13fdc492017-06-27 12:40:18 -07001082 return true;
1083}
1084
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001085bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001086 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001087
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001088 mCurrentState.sequence++;
1089 mCurrentState.cornerRadius = cornerRadius;
1090 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001091 setTransactionFlags(eTransactionNeeded);
1092 return true;
1093}
1094
Robert Carrd4ae7f32018-06-07 16:10:57 -07001095bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1096 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001097 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001098 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1099
1100 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1101 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1102 return false;
1103 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001104 mCurrentState.sequence++;
1105 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1106 matrix.dsdy);
1107 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001108 setTransactionFlags(eTransactionNeeded);
1109 return true;
1110}
Marissa Wall61c58622018-07-18 10:12:20 -07001111
Mathias Agopian13127d82013-03-05 17:47:11 -08001112bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001113 mCurrentState.requestedTransparentRegion_legacy = transparent;
1114 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001115 setTransactionFlags(eTransactionNeeded);
1116 return true;
1117}
1118bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001119 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1120 if (mCurrentState.flags == newFlags) return false;
1121 mCurrentState.sequence++;
1122 mCurrentState.flags = newFlags;
1123 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001124 setTransactionFlags(eTransactionNeeded);
1125 return true;
1126}
Robert Carr99e27f02016-06-16 15:18:02 -07001127
Marissa Wallf58c14b2018-07-24 10:50:43 -07001128bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001129 if (mCurrentState.requestedCrop_legacy == crop) return false;
1130 mCurrentState.sequence++;
1131 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001132 if (immediate && !mFreezeGeometryUpdates) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001133 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001134 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001135 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1136
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001137 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001138 setTransactionFlags(eTransactionNeeded);
1139 return true;
1140}
Robert Carr8d5227b2017-03-16 15:41:03 -07001141
Robert Carrc3574f72016-03-24 12:19:32 -07001142bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001143 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001144 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001145 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001146 return true;
1147}
1148
Evan Roskyef876c92019-01-25 17:46:06 -08001149bool Layer::setMetadata(const LayerMetadata& data) {
1150 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001151 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001152 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001153 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001154 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001155}
1156
Mathias Agopian13127d82013-03-05 17:47:11 -08001157bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001158 if (mCurrentState.layerStack == layerStack) return false;
1159 mCurrentState.sequence++;
1160 mCurrentState.layerStack = layerStack;
1161 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001162 setTransactionFlags(eTransactionNeeded);
1163 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001164}
1165
Peiyong Linc502cb72019-03-01 15:00:23 -08001166bool Layer::setColorSpaceAgnostic(const bool agnostic) {
1167 if (mCurrentState.colorSpaceAgnostic == agnostic) {
1168 return false;
1169 }
1170 mCurrentState.sequence++;
1171 mCurrentState.colorSpaceAgnostic = agnostic;
1172 mCurrentState.modified = true;
1173 setTransactionFlags(eTransactionNeeded);
1174 return true;
1175}
1176
Robert Carr1f0a16a2016-10-24 16:27:39 -07001177uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001178 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001179 if (p == nullptr) {
1180 return getDrawingState().layerStack;
1181 }
1182 return p->getLayerStack();
1183}
1184
Marissa Wallf58c14b2018-07-24 10:50:43 -07001185void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001186 mCurrentState.barrierLayer_legacy = barrierLayer;
1187 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001188 // We don't set eTransactionNeeded, because just receiving a deferral
1189 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001190 mCurrentState.modified = true;
1191 pushPendingState();
1192 mCurrentState.barrierLayer_legacy = nullptr;
1193 mCurrentState.frameNumber_legacy = 0;
1194 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001195}
1196
Marissa Wallf58c14b2018-07-24 10:50:43 -07001197void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001198 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001199 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001200}
1201
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001202// ----------------------------------------------------------------------------
1203// pageflip handling...
1204// ----------------------------------------------------------------------------
1205
Robert Carr1f0a16a2016-10-24 16:27:39 -07001206bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001207 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001208 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001209 if (parent != nullptr && parent->isHiddenByPolicy()) {
1210 return true;
1211 }
1212 return s.flags & layer_state_t::eLayerHidden;
1213}
1214
David Sodman41fdfc92017-11-06 16:09:56 -08001215uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001216 // TODO: should we do something special if mSecure is set?
1217 if (mProtectedByApp) {
1218 // need a hardware-protected path to external video sink
1219 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001220 }
Riley Andrews03414a12014-07-01 14:22:59 -07001221 if (mPotentialCursor) {
1222 usage |= GraphicBuffer::USAGE_CURSOR;
1223 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001224 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001225 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001226}
1227
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001228void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001229 uint32_t orientation = 0;
Vishnu Nairf2deb822018-11-12 17:53:48 -08001230 // Disable setting transform hint if the debug flag is set or if the
1231 // getTransformToDisplayInverse flag is set and the client wants to submit buffers
1232 // in one orientation.
1233 if (!mFlinger->mDebugDisableTransformHint && !getTransformToDisplayInverse()) {
Mathias Agopian84300952012-11-21 16:02:13 -08001234 // The transform hint is used to improve performance, but we can
1235 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001236 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001237 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001238 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001239 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001240 orientation = 0;
1241 }
1242 }
David Sodmaneb085e02017-10-05 18:49:04 -07001243 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001244}
1245
Mathias Agopian13127d82013-03-05 17:47:11 -08001246// ----------------------------------------------------------------------------
1247// debugging
1248// ----------------------------------------------------------------------------
1249
Marissa Wall61c58622018-07-18 10:12:20 -07001250// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001251LayerDebugInfo Layer::getLayerDebugInfo() const {
1252 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001253 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001254 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001255 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001256 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
Yiwei Zhang5434a782018-12-05 18:06:32 -08001257 info.mType = std::string(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001258 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001259 info.mVisibleRegion = visibleRegion;
1260 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001261 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001262 info.mX = ds.active_legacy.transform.tx();
1263 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001264 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001265 info.mWidth = ds.active_legacy.w;
1266 info.mHeight = ds.active_legacy.h;
1267 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001268 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001269 info.mFlags = ds.flags;
1270 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001271 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001272 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1273 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1274 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1275 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001276 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001277 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001278 if (buffer != 0) {
1279 info.mActiveBufferWidth = buffer->getWidth();
1280 info.mActiveBufferHeight = buffer->getHeight();
1281 info.mActiveBufferStride = buffer->getStride();
1282 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001283 } else {
1284 info.mActiveBufferWidth = 0;
1285 info.mActiveBufferHeight = 0;
1286 info.mActiveBufferStride = 0;
1287 info.mActiveBufferFormat = 0;
1288 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001289 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001290 info.mNumQueuedFrames = getQueuedFrameCount();
1291 info.mRefreshPending = isBufferLatched();
1292 info.mIsOpaque = isOpaque(ds);
1293 info.mContentDirty = contentDirty;
1294 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001295}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001296
Yiwei Zhang5434a782018-12-05 18:06:32 -08001297void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001298 result.append("-------------------------------");
1299 result.append("-------------------------------");
1300 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001301 result.append(" Layer name\n");
1302 result.append(" Z | ");
1303 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001304 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001305 result.append(" Disp Frame (LTRB) | ");
1306 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001307 result.append("-------------------------------");
1308 result.append("-------------------------------");
1309 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001310}
1311
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001312void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1313 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1314 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001315 return;
1316 }
1317
Yiwei Zhang5434a782018-12-05 18:06:32 -08001318 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001319 if (mName.length() > 77) {
1320 std::string shortened;
1321 shortened.append(mName.string(), 36);
1322 shortened.append("[...]");
1323 shortened.append(mName.string() + (mName.length() - 36), 36);
Yiwei Zhang5434a782018-12-05 18:06:32 -08001324 name = shortened;
Dan Stozae22aec72016-08-01 13:20:59 -07001325 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001326 name = std::string(mName.string(), mName.size());
Dan Stozae22aec72016-08-01 13:20:59 -07001327 }
1328
Yiwei Zhang5434a782018-12-05 18:06:32 -08001329 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001330
Alec Mourib416efd2018-09-06 21:01:59 +00001331 const State& layerState(getDrawingState());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001332 const auto& compositionState = outputLayer->getState();
1333
Chia-I Wu1e043612018-03-01 09:45:09 -08001334 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001335 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001336 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001337 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001338 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001339 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
1340 StringAppendF(&result, "%10s | ",
1341 toString(getCompositionLayer() ? compositionState.bufferTransform
1342 : static_cast<Hwc2::Transform>(0))
1343 .c_str());
1344 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001345 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001346 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001347 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1348 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001349
Yichi Chen6ca35192018-05-29 12:20:43 +08001350 result.append("- - - - - - - - - - - - - - - -");
1351 result.append("- - - - - - - - - - - - - - - -");
1352 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001353}
Dan Stozae22aec72016-08-01 13:20:59 -07001354
Yiwei Zhang5434a782018-12-05 18:06:32 -08001355void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001356 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001357}
1358
Svetoslavd85084b2014-03-20 10:28:31 -07001359void Layer::clearFrameStats() {
1360 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001361}
1362
Jamie Gennis6547ff42013-07-16 20:12:42 -07001363void Layer::logFrameStats() {
1364 mFrameTracker.logAndResetStats(mName);
1365}
1366
Svetoslavd85084b2014-03-20 10:28:31 -07001367void Layer::getFrameStats(FrameStats* outStats) const {
1368 mFrameTracker.getStats(outStats);
1369}
1370
Yiwei Zhang5434a782018-12-05 18:06:32 -08001371void Layer::dumpFrameEvents(std::string& result) {
1372 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001373 Mutex::Autolock lock(mFrameEventHistoryMutex);
1374 mFrameEventHistory.checkFencesForCompletion();
1375 mFrameEventHistory.dump(result);
1376}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001377
Brian Anderson5ea5e592016-12-01 16:54:33 -08001378void Layer::onDisconnect() {
1379 Mutex::Autolock lock(mFrameEventHistoryMutex);
1380 mFrameEventHistory.onDisconnect();
Yiwei Zhangaf8ee942018-11-22 00:15:23 -08001381 mFlinger->mTimeStats->onDestroy(getSequence());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001382}
1383
Brian Anderson3890c392016-07-25 12:48:08 -07001384void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001385 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001386 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001387 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1388 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001389 }
1390
Brian Andersond6927fb2016-07-23 23:37:30 -07001391 Mutex::Autolock lock(mFrameEventHistoryMutex);
1392 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001393 // If there are any unsignaled fences in the aquire timeline at this
1394 // point, the previously queued frame hasn't been latched yet. Go ahead
1395 // and try to get the signal time here so the syscall is taken out of
1396 // the main thread's critical path.
1397 mAcquireTimeline.updateSignalTimes();
1398 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001399 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001400 mFrameEventHistory.addQueue(*newTimestamps);
1401 }
1402
Brian Anderson3890c392016-07-25 12:48:08 -07001403 if (outDelta) {
1404 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001405 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001406}
Dan Stozae77c7662016-05-13 11:37:28 -07001407
Chia-I Wu98f1c102017-05-30 14:54:08 -07001408size_t Layer::getChildrenCount() const {
1409 size_t count = 0;
1410 for (const sp<Layer>& child : mCurrentChildren) {
1411 count += 1 + child->getChildrenCount();
1412 }
1413 return count;
1414}
1415
Robert Carr1f0a16a2016-10-24 16:27:39 -07001416void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001417 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001418 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001419
Robert Carr1f0a16a2016-10-24 16:27:39 -07001420 mCurrentChildren.add(layer);
1421 layer->setParent(this);
1422}
1423
1424ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001425 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001426 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001427
Robert Carr1323c952019-01-28 18:13:27 -08001428 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001429 return mCurrentChildren.remove(layer);
1430}
1431
Robert Carr1db73f62016-12-21 12:58:51 -08001432bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1433 sp<Handle> handle = nullptr;
1434 sp<Layer> newParent = nullptr;
1435 if (newParentHandle == nullptr) {
1436 return false;
1437 }
1438 handle = static_cast<Handle*>(newParentHandle.get());
1439 newParent = handle->owner.promote();
1440 if (newParent == nullptr) {
1441 ALOGE("Unable to promote Layer handle");
1442 return false;
1443 }
1444
chaviw5aedec92018-10-22 10:40:38 -07001445 if (attachChildren()) {
1446 setTransactionFlags(eTransactionNeeded);
1447 }
Robert Carr1db73f62016-12-21 12:58:51 -08001448 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001449 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001450 }
1451 mCurrentChildren.clear();
1452
1453 return true;
1454}
1455
Robert Carr15eae092018-03-23 13:43:53 -07001456void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001457 for (const sp<Layer>& child : mDrawingChildren) {
1458 child->mDrawingParent = newParent;
Vishnu Nairc652ff82019-03-15 12:48:54 -07001459 child->computeBounds(newParent->mBounds,
1460 newParent->getTransformWithScale(
1461 newParent->getBufferScaleTransform()));
Robert Carr578038f2018-03-09 12:25:24 -08001462 }
1463}
1464
chaviwf1961f72017-09-18 16:41:07 -07001465bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001466 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001467
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001468 // While layers are detached, we allow most operations
1469 // and simply halt performing the actual transaction. However
1470 // for reparent != null we would enter the mRemovedFromCurrentState
1471 // state, regardless of whether doTransaction was called, and
1472 // so we need to prevent the update here.
1473 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001474 return false;
1475 }
1476
Robert Carr54cf5b12019-01-25 14:02:28 -08001477 sp<Layer> newParent;
1478 if (newParentHandle != nullptr) {
1479 auto handle = static_cast<Handle*>(newParentHandle.get());
1480 newParent = handle->owner.promote();
1481 if (newParent == nullptr) {
1482 ALOGE("Unable to promote Layer handle");
1483 return false;
1484 }
1485 if (newParent == this) {
1486 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1487 return false;
1488 }
1489 }
1490
chaviwf1961f72017-09-18 16:41:07 -07001491 sp<Layer> parent = getParent();
1492 if (parent != nullptr) {
1493 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001494 }
1495
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001496 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001497 newParent->addChild(this);
1498 if (!newParent->isRemovedFromCurrentState()) {
1499 addToCurrentState();
1500 } else {
1501 onRemovedFromCurrentState();
1502 }
1503
1504 if (mLayerDetached) {
1505 mLayerDetached = false;
1506 callSetTransactionFlags = true;
1507 }
1508 } else {
1509 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001510 }
1511
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001512 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001513 setTransactionFlags(eTransactionNeeded);
1514 }
chaviw06178942017-07-27 10:25:59 -07001515 return true;
1516}
1517
Robert Carr9524cb32017-02-13 11:32:32 -08001518bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001519 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001520 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001521 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001522 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001523 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001524 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001525 }
Robert Carr7f619b22017-11-06 12:56:35 -08001526 }
Robert Carr9524cb32017-02-13 11:32:32 -08001527
1528 return true;
1529}
1530
chaviw5aedec92018-10-22 10:40:38 -07001531bool Layer::attachChildren() {
1532 bool changed = false;
1533 for (const sp<Layer>& child : mCurrentChildren) {
1534 sp<Client> parentClient = mClientRef.promote();
1535 sp<Client> client(child->mClientRef.promote());
1536 if (client != nullptr && parentClient != client) {
1537 if (child->mLayerDetached) {
1538 child->mLayerDetached = false;
1539 changed = true;
1540 }
1541 changed |= child->attachChildren();
1542 }
1543 }
1544
1545 return changed;
1546}
1547
Peiyong Lind3788632018-09-18 16:01:31 -07001548bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001549 static const mat4 identityMatrix = mat4();
1550
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001551 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001552 return false;
1553 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001554 ++mCurrentState.sequence;
1555 mCurrentState.colorTransform = matrix;
1556 mCurrentState.hasColorTransform = matrix != identityMatrix;
1557 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001558 setTransactionFlags(eTransactionNeeded);
1559 return true;
1560}
1561
chaviwf66724d2018-11-28 16:35:21 -08001562mat4 Layer::getColorTransform() const {
1563 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1564 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1565 colorTransform = parent->getColorTransform() * colorTransform;
1566 }
1567 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001568}
1569
1570bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001571 bool hasColorTransform = getDrawingState().hasColorTransform;
1572 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1573 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1574 }
1575 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001576}
1577
Chia-I Wu11481472018-05-04 10:43:19 -07001578bool Layer::isLegacyDataSpace() const {
1579 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001580 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001581 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001582}
1583
Robert Carr1f0a16a2016-10-24 16:27:39 -07001584void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001585 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001586}
1587
Robert Carr1f0a16a2016-10-24 16:27:39 -07001588int32_t Layer::getZ() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001589 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001590}
1591
Robert Carr29abff82017-12-04 13:51:20 -08001592bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1593 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001594 const State& state = useDrawing ? mDrawingState : mCurrentState;
Robert Carr29abff82017-12-04 13:51:20 -08001595 return state.zOrderRelativeOf != nullptr;
1596}
1597
David Sodman41fdfc92017-11-06 16:09:56 -08001598__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001599 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001600 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1601 "makeTraversalList received invalid stateSet");
1602 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1603 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001604 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001605
Robert Carr29abff82017-12-04 13:51:20 -08001606 if (state.zOrderRelatives.size() == 0) {
1607 *outSkipRelativeZUsers = true;
1608 return children;
1609 }
1610
chaviwfd462612018-05-31 16:11:27 -07001611 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001612 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001613 sp<Layer> strongRelative = weakRelative.promote();
1614 if (strongRelative != nullptr) {
1615 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001616 }
1617 }
1618
Dan Stoza412903f2017-04-27 13:42:17 -07001619 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001620 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
Robert Carr503c7042017-09-27 15:06:08 -07001621 if (childState.zOrderRelativeOf != nullptr) {
1622 continue;
1623 }
Robert Carrdb66e622017-04-10 16:55:57 -07001624 traverse.add(child);
1625 }
1626
1627 return traverse;
1628}
1629
Robert Carr1f0a16a2016-10-24 16:27:39 -07001630/**
Robert Carrdb66e622017-04-10 16:55:57 -07001631 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001632 */
Dan Stoza412903f2017-04-27 13:42:17 -07001633void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001634 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1635 // produce a new list for traversing, including our relatives, and not including our children
1636 // who are relatives of another surface. In the case that there are no relative Z,
1637 // makeTraversalList returns our children directly to avoid significant overhead.
1638 // However in this case we need to take the responsibility for filtering children which
1639 // are relatives of another surface here.
1640 bool skipRelativeZUsers = false;
1641 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001642
Robert Carr1f0a16a2016-10-24 16:27:39 -07001643 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001644 for (; i < list.size(); i++) {
1645 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001646 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1647 continue;
1648 }
1649
Robert Carrdb66e622017-04-10 16:55:57 -07001650 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001651 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001652 }
Dan Stoza412903f2017-04-27 13:42:17 -07001653 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001654 }
Robert Carr29abff82017-12-04 13:51:20 -08001655
Dan Stoza412903f2017-04-27 13:42:17 -07001656 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001657 for (; i < list.size(); i++) {
1658 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001659
Robert Carr29abff82017-12-04 13:51:20 -08001660 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1661 continue;
1662 }
Dan Stoza412903f2017-04-27 13:42:17 -07001663 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001664 }
1665}
1666
1667/**
Robert Carrdb66e622017-04-10 16:55:57 -07001668 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001669 */
Dan Stoza412903f2017-04-27 13:42:17 -07001670void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1671 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001672 // See traverseInZOrder for documentation.
1673 bool skipRelativeZUsers = false;
1674 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001675
Robert Carr1f0a16a2016-10-24 16:27:39 -07001676 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001677 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001678 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001679
1680 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1681 continue;
1682 }
1683
Robert Carrdb66e622017-04-10 16:55:57 -07001684 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001685 break;
1686 }
Dan Stoza412903f2017-04-27 13:42:17 -07001687 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001688 }
Dan Stoza412903f2017-04-27 13:42:17 -07001689 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001690 for (; 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
Dan Stoza412903f2017-04-27 13:42:17 -07001697 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001698 }
1699}
1700
chaviw4b129c22018-04-09 16:19:43 -07001701LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1702 const std::vector<Layer*>& layersInTree) {
1703 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1704 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001705 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1706 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001707 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001708
chaviwfd462612018-05-31 16:11:27 -07001709 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001710 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1711 sp<Layer> strongRelative = weakRelative.promote();
1712 // Only add relative layers that are also descendents of the top most parent of the tree.
1713 // If a relative layer is not a descendent, then it should be ignored.
1714 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1715 traverse.add(strongRelative);
1716 }
1717 }
1718
1719 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001720 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001721 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1722 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1723 // the child here since it won't be added later as a relative.
1724 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1725 childState.zOrderRelativeOf.promote().get())) {
1726 continue;
1727 }
1728 traverse.add(child);
1729 }
1730
1731 return traverse;
1732}
1733
1734void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1735 LayerVector::StateSet stateSet,
1736 const LayerVector::Visitor& visitor) {
1737 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001738
1739 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001740 for (; i < list.size(); i++) {
1741 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001742 if (relative->getZ() >= 0) {
1743 break;
1744 }
chaviw4b129c22018-04-09 16:19:43 -07001745 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001746 }
chaviw4b129c22018-04-09 16:19:43 -07001747
chaviwa76b2712017-09-20 12:02:26 -07001748 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001749 for (; i < list.size(); i++) {
1750 const auto& relative = list[i];
1751 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001752 }
1753}
1754
chaviw4b129c22018-04-09 16:19:43 -07001755std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1756 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1757 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1758
1759 std::vector<Layer*> layersInTree = {this};
1760 for (size_t i = 0; i < children.size(); i++) {
1761 const auto& child = children[i];
1762 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1763 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1764 }
1765
1766 return layersInTree;
1767}
1768
1769void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1770 const LayerVector::Visitor& visitor) {
1771 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1772 std::sort(layersInTree.begin(), layersInTree.end());
1773 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1774}
1775
Peiyong Linefefaac2018-08-17 12:27:51 -07001776ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001777 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001778}
1779
chaviw13fdc492017-06-27 12:40:18 -07001780half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001781 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001782
chaviw13fdc492017-06-27 12:40:18 -07001783 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1784 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001785}
Robert Carr6452f122017-03-21 10:41:29 -07001786
chaviw13fdc492017-06-27 12:40:18 -07001787half4 Layer::getColor() const {
1788 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001789 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001790}
Robert Carr6452f122017-03-21 10:41:29 -07001791
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001792Layer::RoundedCornerState Layer::getRoundedCornerState() const {
1793 const auto& p = mDrawingParent.promote();
1794 if (p != nullptr) {
Peiyong Lin27016a92019-03-29 17:36:08 +00001795 RoundedCornerState parentState = p->getRoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001796 if (parentState.radius > 0) {
1797 ui::Transform t = getActiveTransform(getDrawingState());
1798 t = t.inverse();
1799 parentState.cropRect = t.transform(parentState.cropRect);
1800 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1801 // but a transform matrix can define horizontal and vertical scales.
1802 // Let's take the average between both of them and pass into the shader, practically we
1803 // never do this type of transformation on windows anyway.
1804 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
1805 return parentState;
1806 }
1807 }
1808 const float radius = getDrawingState().cornerRadius;
Peiyong Lin8f16f412019-04-04 15:41:00 -07001809 return radius > 0 ? RoundedCornerState(getCrop(getDrawingState()).toFloatRect(), radius)
1810 : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001811}
1812
Robert Carr1f0a16a2016-10-24 16:27:39 -07001813void Layer::commitChildList() {
1814 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1815 const auto& child = mCurrentChildren[i];
1816 child->commitChildList();
1817 }
1818 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001819 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001820}
1821
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001822static wp<Layer> extractLayerFromBinder(const wp<IBinder>& weakBinderHandle) {
1823 if (weakBinderHandle == nullptr) {
1824 return nullptr;
1825 }
1826 sp<IBinder> binderHandle = weakBinderHandle.promote();
1827 if (binderHandle == nullptr) {
1828 return nullptr;
1829 }
1830 sp<Layer::Handle> handle = static_cast<Layer::Handle*>(binderHandle.get());
1831 if (handle == nullptr) {
1832 return nullptr;
1833 }
1834 return handle->owner;
1835}
1836
Robert Carr720e5062018-07-30 17:45:14 -07001837void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001838 mCurrentState.inputInfo = info;
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001839 mCurrentState.touchableRegionCrop = extractLayerFromBinder(info.touchableRegionCropHandle);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001840 mCurrentState.modified = true;
1841 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07001842 setTransactionFlags(eTransactionNeeded);
1843}
1844
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001845void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet,
1846 uint32_t traceFlags) {
chaviw1d044282017-09-27 12:19:28 -07001847 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1848 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001849 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07001850
Peiyong Linefefaac2018-08-17 12:27:51 -07001851 ui::Transform requestedTransform = state.active_legacy.transform;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001852 ui::Transform transform = getTransform();
chaviw1d044282017-09-27 12:19:28 -07001853
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001854 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
1855 layerInfo->set_id(sequence);
1856 layerInfo->set_name(getName().c_str());
1857 layerInfo->set_type(String8(getTypeId()));
chaviw1d044282017-09-27 12:19:28 -07001858
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001859 for (const auto& child : children) {
1860 layerInfo->add_children(child->sequence);
chaviw1d044282017-09-27 12:19:28 -07001861 }
chaviw1d044282017-09-27 12:19:28 -07001862
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001863 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1864 sp<Layer> strongRelative = weakRelative.promote();
1865 if (strongRelative != nullptr) {
1866 layerInfo->add_relatives(strongRelative->sequence);
1867 }
chaviwadc40c22018-07-10 16:57:27 -07001868 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001869
1870 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
1871 [&]() { return layerInfo->mutable_transparent_region(); });
1872 LayerProtoHelper::writeToProto(visibleRegion,
1873 [&]() { return layerInfo->mutable_visible_region(); });
1874 LayerProtoHelper::writeToProto(surfaceDamageRegion,
1875 [&]() { return layerInfo->mutable_damage_region(); });
1876
1877 layerInfo->set_layer_stack(getLayerStack());
1878 layerInfo->set_z(state.z);
1879
1880 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
1881 [&]() { return layerInfo->mutable_position(); });
1882
1883 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(),
1884 [&]() {
1885 return layerInfo->mutable_requested_position();
1886 });
1887
1888 LayerProtoHelper::writeSizeToProto(state.active_legacy.w, state.active_legacy.h,
1889 [&]() { return layerInfo->mutable_size(); });
1890
1891 LayerProtoHelper::writeToProto(state.crop_legacy,
1892 [&]() { return layerInfo->mutable_crop(); });
1893 layerInfo->set_corner_radius(getRoundedCornerState().radius);
1894
1895 layerInfo->set_is_opaque(isOpaque(state));
1896 layerInfo->set_invalidate(contentDirty);
1897
1898 // XXX (b/79210409) mCurrentDataSpace is not protected
1899 layerInfo->set_dataspace(
1900 dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
1901
1902 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1903 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
1904 LayerProtoHelper::writeToProto(state.color,
1905 [&]() { return layerInfo->mutable_requested_color(); });
1906 layerInfo->set_flags(state.flags);
1907
1908 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1909 LayerProtoHelper::writeToProto(requestedTransform,
1910 layerInfo->mutable_requested_transform());
1911
1912 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
1913 if (parent != nullptr) {
1914 layerInfo->set_parent(parent->sequence);
1915 } else {
1916 layerInfo->set_parent(-1);
1917 }
1918
1919 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1920 if (zOrderRelativeOf != nullptr) {
1921 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1922 } else {
1923 layerInfo->set_z_order_relative_of(-1);
1924 }
1925
1926 auto buffer = mActiveBuffer;
1927 if (buffer != nullptr) {
1928 LayerProtoHelper::writeToProto(buffer,
1929 [&]() { return layerInfo->mutable_active_buffer(); });
1930 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
1931 layerInfo->mutable_buffer_transform());
1932 }
1933
1934 layerInfo->set_queued_frames(getQueuedFrameCount());
1935 layerInfo->set_refresh_pending(isBufferLatched());
1936 layerInfo->set_curr_frame(mCurrentFrameNumber);
1937 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
1938
1939 for (const auto& pendingState : mPendingStates) {
1940 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
1941 if (barrierLayer != nullptr) {
1942 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
1943 barrierLayerProto->set_id(barrierLayer->sequence);
1944 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
1945 }
1946 }
1947 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
chaviwadc40c22018-07-10 16:57:27 -07001948 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001949
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001950 if (traceFlags & SurfaceTracing::TRACE_INPUT) {
1951 LayerProtoHelper::writeToProto(state.inputInfo, state.touchableRegionCrop,
1952 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001953 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001954
1955 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
1956 auto protoMap = layerInfo->mutable_metadata();
1957 for (const auto& entry : state.metadata.mMap) {
1958 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
1959 }
1960 LayerProtoHelper::writeToProto(mEffectiveTransform,
1961 layerInfo->mutable_effective_transform());
1962 LayerProtoHelper::writeToProto(mSourceBounds,
1963 [&]() { return layerInfo->mutable_source_bounds(); });
1964 LayerProtoHelper::writeToProto(mScreenBounds,
1965 [&]() { return layerInfo->mutable_screen_bounds(); });
1966 }
chaviw1d044282017-09-27 12:19:28 -07001967}
1968
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001969void Layer::writeToProto(LayerProto* layerInfo, const sp<DisplayDevice>& displayDevice,
1970 uint32_t traceFlags) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001971 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1972 if (!outputLayer) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07001973 return;
1974 }
1975
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001976 writeToProto(layerInfo, LayerVector::StateSet::Drawing, traceFlags);
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001977
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001978 const auto& compositionState = outputLayer->getState();
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001979
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001980 const Rect& frame = compositionState.displayFrame;
Nataniel Borges797b0e42019-02-15 14:11:58 -08001981 LayerProtoHelper::writeToProto(frame, [&]() { return layerInfo->mutable_hwc_frame(); });
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001982
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001983 const FloatRect& crop = compositionState.sourceCrop;
Nataniel Borges797b0e42019-02-15 14:11:58 -08001984 LayerProtoHelper::writeToProto(crop, [&]() { return layerInfo->mutable_hwc_crop(); });
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001985
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001986 const int32_t transform =
1987 getCompositionLayer() ? static_cast<int32_t>(compositionState.bufferTransform) : 0;
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001988 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08001989
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001990 const int32_t compositionType =
1991 static_cast<int32_t>(compositionState.hwc ? (*compositionState.hwc).hwcCompositionType
1992 : Hwc2::IComposerClient::Composition::CLIENT);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08001993 layerInfo->set_hwc_composition_type(compositionType);
1994
1995 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
1996 static_cast<BufferLayer*>(this)->isProtected()) {
1997 layerInfo->set_is_protected(true);
1998 } else {
1999 layerInfo->set_is_protected(false);
2000 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002001}
2002
Robert Carr2e102c92018-10-23 12:11:15 -07002003bool Layer::isRemovedFromCurrentState() const {
2004 return mRemovedFromCurrentState;
2005}
2006
Arthur Hungd20b2702019-01-14 18:16:16 +08002007InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002008 InputWindowInfo info = mDrawingState.inputInfo;
Robert Carr720e5062018-07-30 17:45:14 -07002009
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002010 if (info.displayId == ADISPLAY_ID_NONE) {
2011 info.displayId = mDrawingState.layerStack;
2012 }
2013
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002014 ui::Transform t = getTransform();
2015 const float xScale = t.sx();
2016 const float yScale = t.sy();
2017 if (xScale != 1.0f || yScale != 1.0f) {
2018 info.windowXScale *= 1.0f / xScale;
2019 info.windowYScale *= 1.0f / yScale;
2020 info.touchableRegion.scaleSelf(xScale, yScale);
Riddle Hsu39d4aa52018-11-30 20:46:53 +08002021 }
Robert Carre07e1032018-11-26 12:55:53 -08002022
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002023 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002024 // If this is a portal window, set the touchableRegion to the layerBounds.
2025 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
2026 ? getBufferSize(getDrawingState())
2027 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002028 if (!layerBounds.isValid()) {
2029 layerBounds = getCroppedBufferSize(getDrawingState());
2030 }
Vishnu Nair8033a492018-12-05 07:27:23 -08002031 layerBounds = t.transform(layerBounds);
2032 layerBounds.inset(info.surfaceInset, info.surfaceInset, info.surfaceInset, info.surfaceInset);
2033
Arthur Hungd20b2702019-01-14 18:16:16 +08002034 // Input coordinate should match the layer bounds.
2035 info.frameLeft = layerBounds.left;
2036 info.frameTop = layerBounds.top;
2037 info.frameRight = layerBounds.right;
2038 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08002039
2040 // Position the touchable region relative to frame screen location and restrict it to frame
2041 // bounds.
2042 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08002043 info.visible = canReceiveInput();
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002044
2045 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2046 if (info.replaceTouchableRegionWithCrop) {
2047 if (cropLayer == nullptr) {
2048 info.touchableRegion = Region(Rect{mScreenBounds});
2049 } else {
2050 info.touchableRegion = Region(Rect{cropLayer->mScreenBounds});
2051 }
2052 } else if (cropLayer != nullptr) {
2053 info.touchableRegion = info.touchableRegion.intersect(Rect{cropLayer->mScreenBounds});
2054 }
2055
Robert Carr720e5062018-07-30 17:45:14 -07002056 return info;
2057}
2058
2059bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002060 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002061}
2062
Lloyd Piquefeb73d72018-12-04 17:23:44 -08002063std::shared_ptr<compositionengine::Layer> Layer::getCompositionLayer() const {
2064 return nullptr;
2065}
2066
chaviw3e727cd2019-01-31 13:41:05 -08002067bool Layer::canReceiveInput() const {
2068 return isVisible();
2069}
2070
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002071compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
2072 const sp<const DisplayDevice>& display) const {
2073 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionLayer().get());
2074}
2075
Mathias Agopian13127d82013-03-05 17:47:11 -08002076// ---------------------------------------------------------------------------
2077
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002078}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002079
2080#if defined(__gl_h_)
2081#error "don't include gl/gl.h in this file"
2082#endif
2083
2084#if defined(__gl2_h_)
2085#error "don't include gl2/gl2.h in this file"
2086#endif