blob: b6323a245d1479d95d0a838803b11f14e940a0e1 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dan Stoza9e56aa02015-11-02 13:00:03 -080017//#define LOG_NDEBUG 0
18#undef LOG_TAG
19#define LOG_TAG "Layer"
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080020#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21
Mathias Agopian13127d82013-03-05 17:47:11 -080022#include <math.h>
David Sodman41fdfc92017-11-06 16:09:56 -080023#include <stdint.h>
24#include <stdlib.h>
25#include <sys/types.h>
chaviw4b129c22018-04-09 16:19:43 -070026#include <algorithm>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080027#include <mutex>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080028
Yiwei Zhang5434a782018-12-05 18:06:32 -080029#include <android-base/stringprintf.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080030#include <compositionengine/Display.h>
Lloyd Pique0b785d82018-12-04 17:25:27 -080031#include <compositionengine/Layer.h>
Lloyd Piquea83776c2019-01-29 18:42:32 -080032#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080033#include <compositionengine/OutputLayer.h>
Lloyd Pique0b785d82018-12-04 17:25:27 -080034#include <compositionengine/impl/LayerCompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080035#include <compositionengine/impl/OutputLayerCompositionState.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070036#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070037#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070038#include <cutils/properties.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080039#include <gui/BufferItem.h>
40#include <gui/LayerDebugInfo.h>
41#include <gui/Surface.h>
42#include <renderengine/RenderEngine.h>
43#include <ui/DebugUtils.h>
44#include <ui/GraphicBuffer.h>
45#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046#include <utils/Errors.h>
47#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080048#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080050#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051
Yiwei Zhang60d1a192018-03-07 14:52:28 -080052#include "BufferLayer.h"
Valerie Haudd0b7572019-01-29 14:59:27 -080053#include "ColorLayer.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020054#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070055#include "DisplayDevice.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080056#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057#include "Layer.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080058#include "LayerProtoHelper.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070059#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070060#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061#include "SurfaceFlinger.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080062#include "TimeStats/TimeStats.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070063
David Sodman41fdfc92017-11-06 16:09:56 -080064#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066namespace android {
67
Yiwei Zhang5434a782018-12-05 18:06:32 -080068using base::StringAppendF;
69
Lloyd Piquef1c675b2018-09-12 20:45:39 -070070std::atomic<int32_t> Layer::sSequence{1};
Mathias Agopian13127d82013-03-05 17:47:11 -080071
Lloyd Pique42ab75e2018-09-12 20:46:03 -070072Layer::Layer(const LayerCreationArgs& args)
Ady Abraham8f1ee7f2019-04-05 10:32:50 -070073 : mFlinger(args.flinger),
74 mName(args.name),
75 mClientRef(args.client),
76 mWindowType(args.metadata.getInt32(METADATA_WINDOW_TYPE, 0)) {
Mathias Agopiana67932f2011-04-20 14:20:59 -070077 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070078
79 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -070080 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
81 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
82 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070083
Dan Stozaf7ba41a2017-05-10 15:11:11 -070084 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070085
Lloyd Pique0449b0f2018-12-20 16:23:45 -080086 mCurrentState.active_legacy.w = args.w;
87 mCurrentState.active_legacy.h = args.h;
88 mCurrentState.flags = layerFlags;
89 mCurrentState.active_legacy.transform.set(0, 0);
90 mCurrentState.crop_legacy.makeInvalid();
91 mCurrentState.requestedCrop_legacy = mCurrentState.crop_legacy;
92 mCurrentState.z = 0;
93 mCurrentState.color.a = 1.0f;
94 mCurrentState.layerStack = 0;
95 mCurrentState.sequence = 0;
96 mCurrentState.requested_legacy = mCurrentState.active_legacy;
Lloyd Pique0449b0f2018-12-20 16:23:45 -080097 mCurrentState.active.w = UINT32_MAX;
98 mCurrentState.active.h = UINT32_MAX;
99 mCurrentState.active.transform.set(0, 0);
100 mCurrentState.transform = 0;
101 mCurrentState.transformToDisplayInverse = false;
102 mCurrentState.crop.makeInvalid();
103 mCurrentState.acquireFence = new Fence(-1);
104 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
105 mCurrentState.hdrMetadata.validTypes = 0;
106 mCurrentState.surfaceDamageRegion.clear();
107 mCurrentState.cornerRadius = 0.0f;
108 mCurrentState.api = -1;
109 mCurrentState.hasColorTransform = false;
Peiyong Linc502cb72019-03-01 15:00:23 -0800110 mCurrentState.colorSpaceAgnostic = false;
Evan Roskya1f1e152019-01-24 16:17:46 -0800111 mCurrentState.metadata = args.metadata;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700112
113 // drawing state & current state are identical
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800114 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700115
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800116 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700117 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800118 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200119 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700120
Ady Abraham8f1ee7f2019-04-05 10:32:50 -0700121 mSchedulerLayerHandle = mFlinger->mScheduler->registerLayer(mName.c_str(), mWindowType);
Ady Abraham09bd3922019-04-08 10:44:56 -0700122
Robert Carr2e102c92018-10-23 12:11:15 -0700123 mFlinger->onLayerCreated();
Dan Stoza436ccf32018-06-21 12:10:12 -0700124}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700125
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700126Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800127 sp<Client> c(mClientRef.promote());
128 if (c != 0) {
129 c->detachLayer(this);
130 }
131
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000132 mFrameTracker.logAndResetStats(mName);
Marissa Wall2d814fb2019-04-09 18:52:57 +0000133
Robert Carr2e102c92018-10-23 12:11:15 -0700134 mFlinger->onLayerDestroyed();
Mathias Agopian96f08192010-06-02 23:28:45 -0700135}
136
Mathias Agopian13127d82013-03-05 17:47:11 -0800137// ---------------------------------------------------------------------------
138// callbacks
139// ---------------------------------------------------------------------------
140
David Sodmaneb085e02017-10-05 18:49:04 -0700141/*
142 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
143 * Layer. So, the implementation is done in BufferLayer. When called on a
144 * ColorLayer object, it's essentially a NOP.
145 */
David Sodmaneb085e02017-10-05 18:49:04 -0700146void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800147
Chia-I Wuc6657022017-08-15 11:18:17 -0700148void Layer::onRemovedFromCurrentState() {
Robert Carr2e102c92018-10-23 12:11:15 -0700149 mRemovedFromCurrentState = true;
150
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800151 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
152 if (mCurrentState.zOrderRelativeOf != nullptr) {
153 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
154 if (strongRelative != nullptr) {
155 strongRelative->removeZOrderRelative(this);
156 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700157 }
chaviw606e5cf2019-03-01 10:12:10 -0800158 setZOrderRelativeOf(nullptr);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700159 }
Rob Carr4bba3702018-10-08 21:53:30 +0000160
Robert Carr2e102c92018-10-23 12:11:15 -0700161 // Since we are no longer reachable from CurrentState SurfaceFlinger
162 // will no longer invoke doTransaction for us, and so we will
163 // never finish applying transactions. We signal the sync point
164 // now so that another layer will not become indefinitely
165 // blocked.
166 for (auto& point: mRemoteSyncPoints) {
167 point->setTransactionApplied();
168 }
169 mRemoteSyncPoints.clear();
170
171 {
172 Mutex::Autolock syncLock(mLocalSyncPointMutex);
173 for (auto& point : mLocalSyncPoints) {
174 point->setFrameAvailable();
175 }
176 mLocalSyncPoints.clear();
177 }
178
Chia-I Wuc6657022017-08-15 11:18:17 -0700179 for (const auto& child : mCurrentChildren) {
180 child->onRemovedFromCurrentState();
181 }
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800182
183 mFlinger->markLayerPendingRemovalLocked(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700184}
Chia-I Wu38512252017-05-17 14:36:16 -0700185
chaviw61626f22018-11-15 16:26:27 -0800186void Layer::addToCurrentState() {
187 mRemovedFromCurrentState = false;
188
189 for (const auto& child : mCurrentChildren) {
190 child->addToCurrentState();
191 }
192}
193
Mathias Agopian13127d82013-03-05 17:47:11 -0800194// ---------------------------------------------------------------------------
195// set-up
196// ---------------------------------------------------------------------------
197
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700198const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800199 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800200}
201
chaviw13fdc492017-06-27 12:40:18 -0700202bool Layer::getPremultipledAlpha() const {
203 return mPremultipliedAlpha;
204}
205
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700206sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800207 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700208 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800209}
210
211// ---------------------------------------------------------------------------
212// h/w composer set-up
213// ---------------------------------------------------------------------------
214
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800215bool Layer::hasHwcLayer(const sp<const DisplayDevice>& displayDevice) {
216 auto outputLayer = findOutputLayerForDisplay(displayDevice);
217 LOG_FATAL_IF(!outputLayer);
218 return outputLayer->getState().hwc && (*outputLayer->getState().hwc).hwcLayer != nullptr;
219}
220
221HWC2::Layer* Layer::getHwcLayer(const sp<const DisplayDevice>& displayDevice) {
222 auto outputLayer = findOutputLayerForDisplay(displayDevice);
223 if (!outputLayer || !outputLayer->getState().hwc) {
224 return nullptr;
225 }
226 return (*outputLayer->getState().hwc).hwcLayer.get();
Steven Thomasb02664d2017-07-26 18:48:28 -0700227}
Steven Thomasb02664d2017-07-26 18:48:28 -0700228
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800229Rect Layer::getContentCrop() const {
230 // this is the crop rectangle that applies to the buffer
231 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700232 Rect crop;
233 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800234 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700235 crop = mCurrentCrop;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800236 } else if (mActiveBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800237 // otherwise we use the whole buffer
Lloyd Pique0b785d82018-12-04 17:25:27 -0800238 crop = mActiveBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700239 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800240 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700241 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700242 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700243 return crop;
244}
245
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700246static Rect reduce(const Rect& win, const Region& exclude) {
247 if (CC_LIKELY(exclude.isEmpty())) {
248 return win;
249 }
250 if (exclude.isRect()) {
251 return win.reduce(exclude.getBounds());
252 }
253 return Region(win).subtract(exclude).getBounds();
254}
255
Dan Stoza80d61162017-12-20 15:57:52 -0800256static FloatRect reduce(const FloatRect& win, const Region& exclude) {
257 if (CC_LIKELY(exclude.isEmpty())) {
258 return win;
259 }
260 // Convert through Rect (by rounding) for lack of FloatRegion
261 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
262}
263
Vishnu Nair4351ad52019-02-11 14:13:02 -0800264Rect Layer::getScreenBounds(bool reduceTransparentRegion) const {
Vishnu Nairf0c28512019-02-08 12:40:28 -0800265 if (!reduceTransparentRegion) {
266 return Rect{mScreenBounds};
267 }
268
269 FloatRect bounds = getBounds();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800270 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800271 // Transform to screen space.
272 bounds = t.transform(bounds);
273 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700274}
275
Vishnu Nair4351ad52019-02-11 14:13:02 -0800276FloatRect Layer::getBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000277 const State& s(getDrawingState());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800278 return getBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700279}
280
Vishnu Nairf0c28512019-02-08 12:40:28 -0800281FloatRect Layer::getBounds(const Region& activeTransparentRegion) const {
282 // Subtract the transparent region and snap to the bounds.
283 return reduce(mBounds, activeTransparentRegion);
284}
285
Vishnu Nairc652ff82019-03-15 12:48:54 -0700286ui::Transform Layer::getBufferScaleTransform() const {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800287 // If the layer is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
288 // it isFixedSize) then there may be additional scaling not accounted
Vishnu Nairc652ff82019-03-15 12:48:54 -0700289 // for in the layer transform.
Lloyd Pique0b785d82018-12-04 17:25:27 -0800290 if (!isFixedSize() || !mActiveBuffer) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700291 return {};
Vishnu Nair4351ad52019-02-11 14:13:02 -0800292 }
293
Marissa Wall290ad082019-03-06 13:23:47 -0800294 // If the layer is a buffer state layer, the active width and height
295 // could be infinite. In that case, return the effective transform.
296 const uint32_t activeWidth = getActiveWidth(getDrawingState());
297 const uint32_t activeHeight = getActiveHeight(getDrawingState());
298 if (activeWidth >= UINT32_MAX && activeHeight >= UINT32_MAX) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700299 return {};
Marissa Wall290ad082019-03-06 13:23:47 -0800300 }
301
Vishnu Nairc652ff82019-03-15 12:48:54 -0700302 int bufferWidth = mActiveBuffer->getWidth();
303 int bufferHeight = mActiveBuffer->getHeight();
304
305 if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
306 std::swap(bufferWidth, bufferHeight);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800307 }
Vishnu Nairc652ff82019-03-15 12:48:54 -0700308
Marissa Wall290ad082019-03-06 13:23:47 -0800309 float sx = activeWidth / static_cast<float>(bufferWidth);
310 float sy = activeHeight / static_cast<float>(bufferHeight);
311
Vishnu Nair4351ad52019-02-11 14:13:02 -0800312 ui::Transform extraParentScaling;
313 extraParentScaling.set(sx, 0, 0, sy);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700314 return extraParentScaling;
315}
316
317ui::Transform Layer::getTransformWithScale(const ui::Transform& bufferScaleTransform) const {
318 // We need to mirror this scaling to child surfaces or we will break the contract where WM can
319 // treat child surfaces as pixels in the parent surface.
320 if (!isFixedSize() || !mActiveBuffer) {
321 return mEffectiveTransform;
322 }
323 return mEffectiveTransform * bufferScaleTransform;
324}
325
326FloatRect Layer::getBoundsPreScaling(const ui::Transform& bufferScaleTransform) const {
327 // We need the pre scaled layer bounds when computing child bounds to make sure the child is
328 // cropped to its parent layer after any buffer transform scaling is applied.
329 if (!isFixedSize() || !mActiveBuffer) {
330 return mBounds;
331 }
332 return bufferScaleTransform.inverse().transform(mBounds);
Vishnu Nair4351ad52019-02-11 14:13:02 -0800333}
334
335void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform) {
336 const State& s(getDrawingState());
337
338 // Calculate effective layer transform
339 mEffectiveTransform = parentTransform * getActiveTransform(s);
340
341 // Transform parent bounds to layer space
342 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
343
Vishnu Nairc652ff82019-03-15 12:48:54 -0700344 // Calculate source bounds
Vishnu Nair4351ad52019-02-11 14:13:02 -0800345 mSourceBounds = computeSourceBounds(parentBounds);
346
347 // Calculate bounds by croping diplay frame with layer crop and parent bounds
348 FloatRect bounds = mSourceBounds;
349 const Rect layerCrop = getCrop(s);
350 if (!layerCrop.isEmpty()) {
351 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
352 }
353 bounds = bounds.intersect(parentBounds);
354
355 mBounds = bounds;
356 mScreenBounds = mEffectiveTransform.transform(mBounds);
Vishnu Nairc652ff82019-03-15 12:48:54 -0700357
358 // Add any buffer scaling to the layer's children.
359 ui::Transform bufferScaleTransform = getBufferScaleTransform();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800360 for (const sp<Layer>& child : mDrawingChildren) {
Vishnu Nairc652ff82019-03-15 12:48:54 -0700361 child->computeBounds(getBoundsPreScaling(bufferScaleTransform),
362 getTransformWithScale(bufferScaleTransform));
Vishnu Nair4351ad52019-02-11 14:13:02 -0800363 }
364}
365
Vishnu Nair60356342018-11-13 13:00:45 -0800366Rect Layer::getCroppedBufferSize(const State& s) const {
367 Rect size = getBufferSize(s);
368 Rect crop = getCrop(s);
369 if (!crop.isEmpty() && size.isValid()) {
370 size.intersect(crop, &size);
371 } else if (!crop.isEmpty()) {
372 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700373 }
Vishnu Nair60356342018-11-13 13:00:45 -0800374 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800375}
376
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700377void Layer::setupRoundedCornersCropCoordinates(Rect win,
378 const FloatRect& roundedCornersCrop) const {
379 // Translate win by the rounded corners rect coordinates, to have all values in
380 // layer coordinate space.
381 win.left -= roundedCornersCrop.left;
382 win.right -= roundedCornersCrop.left;
383 win.top -= roundedCornersCrop.top;
384 win.bottom -= roundedCornersCrop.top;
385
Lloyd Pique0b785d82018-12-04 17:25:27 -0800386 renderengine::Mesh::VertexArray<vec2> cropCoords(
387 getCompositionLayer()->editState().reMesh.getCropCoordArray<vec2>());
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700388 cropCoords[0] = vec2(win.left, win.top);
389 cropCoords[1] = vec2(win.left, win.top + win.getHeight());
390 cropCoords[2] = vec2(win.right, win.top + win.getHeight());
391 cropCoords[3] = vec2(win.right, win.top);
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700392}
393
Lloyd Piquea83776c2019-01-29 18:42:32 -0800394void Layer::latchGeometry(compositionengine::LayerFECompositionState& compositionState) const {
395 const auto& drawingState{getDrawingState()};
396 auto alpha = static_cast<float>(getAlpha());
David Revemanecf0fa52017-03-03 11:32:44 -0500397 auto blendMode = HWC2::BlendMode::None;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800398 if (!isOpaque(drawingState) || alpha != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800399 blendMode =
400 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800401 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800402
Lloyd Piquea83776c2019-01-29 18:42:32 -0800403 int type = drawingState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
404 int appId = drawingState.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700405 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400406 if (parent.get()) {
407 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800408 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
409 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
410 if (parentType >= 0 || parentAppId >= 0) {
411 type = parentType;
412 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700413 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400414 }
415
Lloyd Piquea83776c2019-01-29 18:42:32 -0800416 compositionState.geomLayerTransform = getTransform();
417 compositionState.geomInverseLayerTransform = compositionState.geomLayerTransform.inverse();
418 compositionState.geomBufferSize = getBufferSize(drawingState);
419 compositionState.geomContentCrop = getContentCrop();
420 compositionState.geomCrop = getCrop(drawingState);
421 compositionState.geomBufferTransform = mCurrentTransform;
422 compositionState.geomBufferUsesDisplayInverseTransform = getTransformToDisplayInverse();
423 compositionState.geomActiveTransparentRegion = getActiveTransparentRegion(drawingState);
424 compositionState.geomLayerBounds = mBounds;
425 compositionState.geomUsesSourceCrop = usesSourceCrop();
426 compositionState.isSecure = isSecure();
David Sodman15094112018-10-11 09:39:37 -0700427
Lloyd Piquea83776c2019-01-29 18:42:32 -0800428 compositionState.blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
429 compositionState.alpha = alpha;
430 compositionState.type = type;
431 compositionState.appId = appId;
432}
David Sodmanba340492018-08-05 21:51:33 -0700433
Lloyd Piquea83776c2019-01-29 18:42:32 -0800434void Layer::latchCompositionState(compositionengine::LayerFECompositionState& compositionState,
435 bool includeGeometry) const {
436 if (includeGeometry) {
437 latchGeometry(compositionState);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700438 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800439}
Mathias Agopian29a367b2011-07-12 14:51:45 -0700440
Lloyd Piquea83776c2019-01-29 18:42:32 -0800441const char* Layer::getDebugName() const {
442 return mName.string();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800443}
444
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800445void Layer::forceClientComposition(const sp<DisplayDevice>& display) {
446 const auto outputLayer = findOutputLayerForDisplay(display);
447 LOG_FATAL_IF(!outputLayer);
448 outputLayer->editState().forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800449}
Dan Stozaee44edd2015-03-23 15:50:23 -0700450
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800451bool Layer::getForceClientComposition(const sp<DisplayDevice>& display) {
452 const auto outputLayer = findOutputLayerForDisplay(display);
453 LOG_FATAL_IF(!outputLayer);
454 return outputLayer->getState().forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800455}
456
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700457void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800458 const auto outputLayer = findOutputLayerForDisplay(display);
459 LOG_FATAL_IF(!outputLayer);
460
461 if (!outputLayer->getState().hwc ||
462 (*outputLayer->getState().hwc).hwcCompositionType !=
463 Hwc2::IComposerClient::Composition::CURSOR) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800464 return;
465 }
466
467 // This gives us only the "orientation" component of the transform
Vishnu Nair33a6eee2019-02-06 13:48:06 -0800468 const State& s(getDrawingState());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800469
470 // Apply the layer's transform, followed by the display's global transform
471 // Here we're guaranteed that the layer's transform preserves rects
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800472 Rect win = getCroppedBufferSize(s);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800473 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700474 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800475 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700476 frame.intersect(display->getViewport(), &frame);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700477 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800478 auto position = displayTransform.transform(frame);
479
Dominik Laskowski7e045462018-05-30 13:02:02 -0700480 auto error =
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800481 (*outputLayer->getState().hwc).hwcLayer->setCursorPosition(position.left, position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800482 ALOGE_IF(error != HWC2::Error::None,
483 "[%s] Failed to set cursor position "
484 "to (%d, %d): %s (%d)",
485 mName.string(), position.left, position.top, to_string(error).c_str(),
486 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800487}
Riley Andrews03414a12014-07-01 14:22:59 -0700488
Mathias Agopian13127d82013-03-05 17:47:11 -0800489// ---------------------------------------------------------------------------
490// drawing...
491// ---------------------------------------------------------------------------
492
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000493bool Layer::prepareClientLayer(const RenderArea& renderArea, const Region& clip,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800494 Region& clearRegion, const bool supportProtectedContent,
495 renderengine::LayerSettings& layer) {
496 return prepareClientLayer(renderArea, clip, false, clearRegion, supportProtectedContent, layer);
Mathias Agopian13127d82013-03-05 17:47:11 -0800497}
498
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000499bool Layer::prepareClientLayer(const RenderArea& renderArea, bool useIdentityTransform,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800500 Region& clearRegion, const bool supportProtectedContent,
501 renderengine::LayerSettings& layer) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000502 return prepareClientLayer(renderArea, Region(renderArea.getBounds()), useIdentityTransform,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800503 clearRegion, supportProtectedContent, layer);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000504}
505
506bool Layer::prepareClientLayer(const RenderArea& /*renderArea*/, const Region& /*clip*/,
507 bool useIdentityTransform, Region& /*clearRegion*/,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800508 const bool /*supportProtectedContent*/,
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000509 renderengine::LayerSettings& layer) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800510 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000511 half alpha = getAlpha();
512 layer.geometry.boundaries = bounds;
513 if (useIdentityTransform) {
514 layer.geometry.positionTransform = mat4();
515 } else {
516 const ui::Transform transform = getTransform();
517 mat4 m;
518 m[0][0] = transform[0][0];
519 m[0][1] = transform[0][1];
520 m[0][3] = transform[0][2];
521 m[1][0] = transform[1][0];
522 m[1][1] = transform[1][1];
523 m[1][3] = transform[1][2];
524 m[3][0] = transform[2][0];
525 m[3][1] = transform[2][1];
526 m[3][3] = transform[2][2];
527 layer.geometry.positionTransform = m;
528 }
529
530 if (hasColorTransform()) {
531 layer.colorTransform = getColorTransform();
532 }
533
534 const auto roundedCornerState = getRoundedCornerState();
535 layer.geometry.roundedCornersRadius = roundedCornerState.radius;
536 layer.geometry.roundedCornersCrop = roundedCornerState.cropRect;
537
538 layer.alpha = alpha;
539 layer.sourceDataspace = mCurrentDataSpace;
540 return true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800541}
542
David Sodman41fdfc92017-11-06 16:09:56 -0800543void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
544 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800545 auto& engine(mFlinger->getRenderEngine());
Lloyd Pique0b785d82018-12-04 17:25:27 -0800546 computeGeometry(renderArea, getCompositionLayer()->editState().reMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700547 engine.setupFillWithColor(red, green, blue, alpha);
Lloyd Pique0b785d82018-12-04 17:25:27 -0800548 engine.drawMesh(getCompositionLayer()->getState().reMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800549}
550
David Sodmanc1498e62018-09-12 14:36:26 -0700551void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
552 clearWithOpenGL(renderArea, 0, 0, 0, 0);
553}
554
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800555void Layer::setCompositionType(const sp<const DisplayDevice>& display,
556 Hwc2::IComposerClient::Composition type) {
557 const auto outputLayer = findOutputLayerForDisplay(display);
558 LOG_FATAL_IF(!outputLayer);
559 LOG_FATAL_IF(!outputLayer->getState().hwc);
560 auto& compositionState = outputLayer->editState();
561
562 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", ((*compositionState.hwc).hwcLayer)->getId(),
563 toString(type).c_str(), 1);
564 if ((*compositionState.hwc).hwcCompositionType != type) {
David Sodman15094112018-10-11 09:39:37 -0700565 ALOGV(" actually setting");
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800566 (*compositionState.hwc).hwcCompositionType = type;
567
568 auto error = (*compositionState.hwc)
569 .hwcLayer->setCompositionType(static_cast<HWC2::Composition>(type));
570 ALOGE_IF(error != HWC2::Error::None,
571 "[%s] Failed to set "
572 "composition type %s: %s (%d)",
573 mName.string(), toString(type).c_str(), to_string(error).c_str(),
574 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800575 }
576}
577
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800578Hwc2::IComposerClient::Composition Layer::getCompositionType(
579 const sp<const DisplayDevice>& display) const {
580 const auto outputLayer = findOutputLayerForDisplay(display);
581 LOG_FATAL_IF(!outputLayer);
582 return outputLayer->getState().hwc ? (*outputLayer->getState().hwc).hwcCompositionType
583 : Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800584}
585
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800586bool Layer::getClearClientTarget(const sp<const DisplayDevice>& display) const {
587 const auto outputLayer = findOutputLayerForDisplay(display);
588 LOG_FATAL_IF(!outputLayer);
589 return outputLayer->getState().clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800590}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800591
Dan Stozacac35382016-01-27 12:21:06 -0800592bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
593 if (point->getFrameNumber() <= mCurrentFrameNumber) {
594 // Don't bother with a SyncPoint, since we've already latched the
595 // relevant frame
596 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700597 }
Robert Carr2e102c92018-10-23 12:11:15 -0700598 if (isRemovedFromCurrentState()) {
599 return false;
600 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700601
Dan Stozacac35382016-01-27 12:21:06 -0800602 Mutex::Autolock lock(mLocalSyncPointMutex);
603 mLocalSyncPoints.push_back(point);
604 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700605}
606
Mathias Agopian13127d82013-03-05 17:47:11 -0800607// ----------------------------------------------------------------------------
608// local state
609// ----------------------------------------------------------------------------
610
Peiyong Lin833074a2018-08-28 11:53:54 -0700611void Layer::computeGeometry(const RenderArea& renderArea,
612 renderengine::Mesh& mesh,
chaviwa76b2712017-09-20 12:02:26 -0700613 bool useIdentityTransform) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700614 const ui::Transform renderAreaTransform(renderArea.getTransform());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800615 FloatRect win = getBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700616
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000617 vec2 lt = vec2(win.left, win.top);
618 vec2 lb = vec2(win.left, win.bottom);
619 vec2 rb = vec2(win.right, win.bottom);
620 vec2 rt = vec2(win.right, win.top);
621
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800622 ui::Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000623 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700624 lt = layerTransform.transform(lt);
625 lb = layerTransform.transform(lb);
626 rb = layerTransform.transform(rb);
627 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000628 }
629
Peiyong Lin833074a2018-08-28 11:53:54 -0700630 renderengine::Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700631 position[0] = renderAreaTransform.transform(lt);
632 position[1] = renderAreaTransform.transform(lb);
633 position[2] = renderAreaTransform.transform(rb);
634 position[3] = renderAreaTransform.transform(rt);
Mathias Agopian13127d82013-03-05 17:47:11 -0800635}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800636
David Sodman41fdfc92017-11-06 16:09:56 -0800637bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800638 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700639 return (s.flags & layer_state_t::eLayerSecure);
640}
641
Mathias Agopian13127d82013-03-05 17:47:11 -0800642void Layer::setVisibleRegion(const Region& visibleRegion) {
643 // always called from main thread
644 this->visibleRegion = visibleRegion;
645}
646
647void Layer::setCoveredRegion(const Region& coveredRegion) {
648 // always called from main thread
649 this->coveredRegion = coveredRegion;
650}
651
David Sodman41fdfc92017-11-06 16:09:56 -0800652void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800653 // always called from main thread
654 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
655}
656
Robert Carre5f4f692018-01-12 13:12:28 -0800657void Layer::clearVisibilityRegions() {
658 visibleRegion.clear();
659 visibleNonTransparentRegion.clear();
660 coveredRegion.clear();
661}
662
Mathias Agopian13127d82013-03-05 17:47:11 -0800663// ----------------------------------------------------------------------------
664// transaction
665// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800666
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800667void Layer::pushPendingState() {
668 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700669 return;
670 }
671
Dan Stoza7dde5992015-05-22 09:51:44 -0700672 // If this transaction is waiting on the receipt of a frame, generate a sync
673 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700674 // We don't allow installing sync points after we are removed from the current state
675 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800676 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
677 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800678 if (barrierLayer == nullptr) {
679 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700680 // If we can't promote the layer we are intended to wait on,
681 // then it is expired or otherwise invalid. Allow this transaction
682 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800683 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800684 } else {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800685 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy);
Robert Carr0d480722017-01-10 16:42:54 -0800686 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800687 mRemoteSyncPoints.push_back(std::move(syncPoint));
688 } else {
689 // We already missed the frame we're supposed to synchronize
690 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800691 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800692 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700693 }
694
Dan Stoza7dde5992015-05-22 09:51:44 -0700695 // Wake us up to check if the frame has been received
696 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700697 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700698 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800699 mPendingStates.push_back(mCurrentState);
700 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700701}
702
Pablo Ceballos05289c22016-04-14 15:49:55 -0700703void Layer::popPendingState(State* stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800704 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700705
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800706 mPendingStates.removeAt(0);
707 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700708}
709
Pablo Ceballos05289c22016-04-14 15:49:55 -0700710bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700711 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800712 while (!mPendingStates.empty()) {
713 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700714 if (mRemoteSyncPoints.empty()) {
715 // If we don't have a sync point for this, apply it anyway. It
716 // will be visually wrong, but it should keep us from getting
717 // into too much trouble.
718 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700719 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700720 stateUpdateAvailable = true;
721 continue;
722 }
723
Marissa Wallf58c14b2018-07-24 10:50:43 -0700724 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800725 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800726 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800727
728 // Signal our end of the sync point and then dispose of it
729 mRemoteSyncPoints.front()->setTransactionApplied();
730 mRemoteSyncPoints.pop_front();
731 continue;
732 }
733
Dan Stoza7dde5992015-05-22 09:51:44 -0700734 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
735 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700736 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700737 stateUpdateAvailable = true;
738
739 // Signal our end of the sync point and then dispose of it
740 mRemoteSyncPoints.front()->setTransactionApplied();
741 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800742 } else {
743 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700744 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700745 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700746 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700747 stateUpdateAvailable = true;
748 }
749 }
750
751 // If we still have pending updates, wake SurfaceFlinger back up and point
752 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800753 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700754 setTransactionFlags(eTransactionNeeded);
755 mFlinger->setTransactionFlags(eTraversalNeeded);
756 }
757
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800758 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700759 return stateUpdateAvailable;
760}
761
Marissa Wall61c58622018-07-18 10:12:20 -0700762uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000763 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800764
Marissa Wall61c58622018-07-18 10:12:20 -0700765 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
766 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700767
David Sodmaneb085e02017-10-05 18:49:04 -0700768 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700769 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000770 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800771 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
772 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
773 " requested={ wh={%4u,%4u} }}\n"
774 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
775 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700776 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700777 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
778 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
779 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
780 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
781 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700782 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
783 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
784 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800785 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700786
Robert Carre392b552017-09-19 12:16:05 -0700787 // Don't let Layer::doTransaction update the drawing state
788 // if we have a pending resize, unless we are in fixed-size mode.
789 // the drawing state will be updated only once we receive a buffer
790 // with the correct size.
791 //
792 // In particular, we want to make sure the clip (which is part
793 // of the geometry state) is latched together with the size but is
794 // latched immediately when no resizing is involved.
795 //
796 // If a sideband stream is attached, however, we want to skip this
797 // optimization so that transactions aren't missed when a buffer
798 // never arrives
799 //
800 // In the case that we don't have a buffer we ignore other factors
801 // and avoid entering the resizePending state. At a high level the
802 // resizePending state is to avoid applying the state of the new buffer
803 // to the old buffer. However in the state where we don't have an old buffer
804 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700805 const bool resizePending =
806 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
807 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
Lloyd Pique0b785d82018-12-04 17:25:27 -0800808 (mActiveBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700809 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800810 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700811 flags |= eDontUpdateGeometryState;
812 }
813 }
814
Robert Carr7bf247e2017-05-18 14:02:49 -0700815 // Here we apply various requested geometry states, depending on our
816 // latching configuration. See Layer.h for a detailed discussion of
817 // how geometry latching is controlled.
818 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000819 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700820
821 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
822 // mode, which causes attributes which normally latch regardless of scaling mode,
823 // to be delayed. We copy the requested state to the active state making sure
824 // to respect these rules (again see Layer.h for a detailed discussion).
825 //
826 // There is an awkward asymmetry in the handling of the crop states in the position
827 // states, as can be seen below. Largely this arises from position and transform
828 // being stored in the same data structure while having different latching rules.
829 // b/38182305
830 //
Marissa Wall61c58622018-07-18 10:12:20 -0700831 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700832 // applyPendingStates in the presence of deferred transactions.
833 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -0700834 float tx = stateToCommit->active_legacy.transform.tx();
835 float ty = stateToCommit->active_legacy.transform.ty();
836 stateToCommit->active_legacy = stateToCommit->requested_legacy;
837 stateToCommit->active_legacy.transform.set(tx, ty);
838 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700839 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700840 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -0700841 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700842 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800843 }
844
Marissa Wall61c58622018-07-18 10:12:20 -0700845 return flags;
846}
847
848uint32_t Layer::doTransaction(uint32_t flags) {
849 ATRACE_CALL();
850
chaviw5aedec92018-10-22 10:40:38 -0700851 if (mLayerDetached) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800852 return flags;
853 }
854
855 if (mChildrenChanged) {
856 flags |= eVisibleRegion;
857 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -0700858 }
859
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800860 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +0000861 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -0700862 if (!applyPendingStates(&c)) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800863 return flags;
Marissa Wall61c58622018-07-18 10:12:20 -0700864 }
865
866 flags = doTransactionResize(flags, &c);
867
Alec Mourib416efd2018-09-06 21:01:59 +0000868 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700869
870 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800871 // invalidate and recompute the visible regions if needed
872 flags |= Layer::eVisibleRegion;
873 }
874
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700875 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800876 // invalidate and recompute the visible regions if needed
877 flags |= eVisibleRegion;
878 this->contentDirty = true;
879
880 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -0700881 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -0700882 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -0800883 }
884
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800885 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -0700886 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800887 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -0700888 }
889
Mathias Agopian13127d82013-03-05 17:47:11 -0800890 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -0700891 commitTransaction(c);
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800892 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -0800893 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800894}
895
Pablo Ceballos05289c22016-04-14 15:49:55 -0700896void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800897 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700898}
899
Mathias Agopian13127d82013-03-05 17:47:11 -0800900uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800901 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800902}
903
904uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800905 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -0800906}
907
Robert Carr82364e32016-05-15 11:27:47 -0700908bool Layer::setPosition(float x, float y, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800909 if (mCurrentState.requested_legacy.transform.tx() == x &&
910 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -0800911 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800912 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -0700913
914 // We update the requested and active position simultaneously because
915 // we want to apply the position portion of the transform matrix immediately,
916 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800917 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -0700918 if (immediate && !mFreezeGeometryUpdates) {
919 // Here we directly update the active state
920 // unlike other setters, because we store it within
921 // the transform, but use different latching rules.
922 // b/38182305
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800923 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -0700924 }
Robert Carr7bf247e2017-05-18 14:02:49 -0700925 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -0700926
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800927 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800928 setTransactionFlags(eTransactionNeeded);
929 return true;
930}
Robert Carr82364e32016-05-15 11:27:47 -0700931
Robert Carr1f0a16a2016-10-24 16:27:39 -0700932bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
933 ssize_t idx = mCurrentChildren.indexOf(childLayer);
934 if (idx < 0) {
935 return false;
936 }
937 if (childLayer->setLayer(z)) {
938 mCurrentChildren.removeAt(idx);
939 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800940 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700941 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800942 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700943}
944
Robert Carr503c7042017-09-27 15:06:08 -0700945bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
946 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
947 ssize_t idx = mCurrentChildren.indexOf(childLayer);
948 if (idx < 0) {
949 return false;
950 }
951 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
952 mCurrentChildren.removeAt(idx);
953 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800954 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700955 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800956 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700957}
958
Robert Carrae060832016-11-28 10:51:00 -0800959bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800960 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
961 mCurrentState.sequence++;
962 mCurrentState.z = z;
963 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700964
965 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800966 if (mCurrentState.zOrderRelativeOf != nullptr) {
967 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700968 if (strongRelative != nullptr) {
969 strongRelative->removeZOrderRelative(this);
970 }
chaviw606e5cf2019-03-01 10:12:10 -0800971 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -0700972 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800973 setTransactionFlags(eTransactionNeeded);
974 return true;
975}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700976
Robert Carrdb66e622017-04-10 16:55:57 -0700977void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800978 mCurrentState.zOrderRelatives.remove(relative);
979 mCurrentState.sequence++;
980 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700981 setTransactionFlags(eTransactionNeeded);
982}
983
984void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800985 mCurrentState.zOrderRelatives.add(relative);
986 mCurrentState.modified = true;
987 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -0700988 setTransactionFlags(eTransactionNeeded);
989}
990
chaviw606e5cf2019-03-01 10:12:10 -0800991void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
992 mCurrentState.zOrderRelativeOf = relativeOf;
993 mCurrentState.sequence++;
994 mCurrentState.modified = true;
995 setTransactionFlags(eTransactionNeeded);
996}
997
Robert Carr503d2bd2017-12-04 15:49:47 -0800998bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -0700999 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1000 if (handle == nullptr) {
1001 return false;
1002 }
1003 sp<Layer> relative = handle->owner.promote();
1004 if (relative == nullptr) {
1005 return false;
1006 }
1007
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001008 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1009 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001010 return false;
1011 }
1012
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001013 mCurrentState.sequence++;
1014 mCurrentState.modified = true;
1015 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001016
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001017 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -07001018 if (oldZOrderRelativeOf != nullptr) {
1019 oldZOrderRelativeOf->removeZOrderRelative(this);
1020 }
chaviw606e5cf2019-03-01 10:12:10 -08001021 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -07001022 relative->addZOrderRelative(this);
1023
1024 setTransactionFlags(eTransactionNeeded);
1025
1026 return true;
1027}
1028
Mathias Agopian13127d82013-03-05 17:47:11 -08001029bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001030 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001031 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001032 mCurrentState.requested_legacy.w = w;
1033 mCurrentState.requested_legacy.h = h;
1034 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001035 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001036
1037 // record the new size, from this point on, when the client request
1038 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001039 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001040 return true;
1041}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001042bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001043 if (mCurrentState.color.a == alpha) return false;
1044 mCurrentState.sequence++;
1045 mCurrentState.color.a = alpha;
1046 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001047 setTransactionFlags(eTransactionNeeded);
1048 return true;
1049}
chaviw13fdc492017-06-27 12:40:18 -07001050
Valerie Haudd0b7572019-01-29 14:59:27 -08001051bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1052 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001053 return false;
Valerie Hauaa194562019-02-05 16:21:38 -08001054 }
1055 mCurrentState.sequence++;
1056 mCurrentState.modified = true;
1057 setTransactionFlags(eTransactionNeeded);
1058
1059 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001060 // create background color layer if one does not yet exist
1061 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
1062 const String8& name = mName + "BackgroundColorLayer";
Evan Roskya1f1e152019-01-24 16:17:46 -08001063 mCurrentState.bgColorLayer = new ColorLayer(
1064 LayerCreationArgs(mFlinger.get(), nullptr, name, 0, 0, flags, LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -07001065
Valerie Haudd0b7572019-01-29 14:59:27 -08001066 // add to child list
1067 addChild(mCurrentState.bgColorLayer);
1068 mFlinger->mLayersAdded = true;
1069 // set up SF to handle added color layer
1070 if (isRemovedFromCurrentState()) {
1071 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1072 }
1073 mFlinger->setTransactionFlags(eTransactionNeeded);
1074 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1075 mCurrentState.bgColorLayer->reparent(nullptr);
1076 mCurrentState.bgColorLayer = nullptr;
1077 return true;
1078 }
1079
1080 mCurrentState.bgColorLayer->setColor(color);
1081 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1082 mCurrentState.bgColorLayer->setAlpha(alpha);
1083 mCurrentState.bgColorLayer->setDataspace(dataspace);
1084
chaviw13fdc492017-06-27 12:40:18 -07001085 return true;
1086}
1087
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001088bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001089 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001090
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001091 mCurrentState.sequence++;
1092 mCurrentState.cornerRadius = cornerRadius;
1093 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001094 setTransactionFlags(eTransactionNeeded);
1095 return true;
1096}
1097
Robert Carrd4ae7f32018-06-07 16:10:57 -07001098bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1099 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001100 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001101 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1102
1103 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1104 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1105 return false;
1106 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001107 mCurrentState.sequence++;
1108 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1109 matrix.dsdy);
1110 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001111 setTransactionFlags(eTransactionNeeded);
1112 return true;
1113}
Marissa Wall61c58622018-07-18 10:12:20 -07001114
Mathias Agopian13127d82013-03-05 17:47:11 -08001115bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001116 mCurrentState.requestedTransparentRegion_legacy = transparent;
1117 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001118 setTransactionFlags(eTransactionNeeded);
1119 return true;
1120}
1121bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001122 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1123 if (mCurrentState.flags == newFlags) return false;
1124 mCurrentState.sequence++;
1125 mCurrentState.flags = newFlags;
1126 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001127 setTransactionFlags(eTransactionNeeded);
1128 return true;
1129}
Robert Carr99e27f02016-06-16 15:18:02 -07001130
Marissa Wallf58c14b2018-07-24 10:50:43 -07001131bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001132 if (mCurrentState.requestedCrop_legacy == crop) return false;
1133 mCurrentState.sequence++;
1134 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001135 if (immediate && !mFreezeGeometryUpdates) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001136 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001137 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001138 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1139
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001140 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001141 setTransactionFlags(eTransactionNeeded);
1142 return true;
1143}
Robert Carr8d5227b2017-03-16 15:41:03 -07001144
Robert Carrc3574f72016-03-24 12:19:32 -07001145bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001146 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001147 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001148 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001149 return true;
1150}
1151
Evan Roskyef876c92019-01-25 17:46:06 -08001152bool Layer::setMetadata(const LayerMetadata& data) {
1153 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001154 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001155 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001156 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001157 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001158}
1159
Mathias Agopian13127d82013-03-05 17:47:11 -08001160bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001161 if (mCurrentState.layerStack == layerStack) return false;
1162 mCurrentState.sequence++;
1163 mCurrentState.layerStack = layerStack;
1164 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001165 setTransactionFlags(eTransactionNeeded);
1166 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001167}
1168
Peiyong Linc502cb72019-03-01 15:00:23 -08001169bool Layer::setColorSpaceAgnostic(const bool agnostic) {
1170 if (mCurrentState.colorSpaceAgnostic == agnostic) {
1171 return false;
1172 }
1173 mCurrentState.sequence++;
1174 mCurrentState.colorSpaceAgnostic = agnostic;
1175 mCurrentState.modified = true;
1176 setTransactionFlags(eTransactionNeeded);
1177 return true;
1178}
1179
Robert Carr1f0a16a2016-10-24 16:27:39 -07001180uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001181 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001182 if (p == nullptr) {
1183 return getDrawingState().layerStack;
1184 }
1185 return p->getLayerStack();
1186}
1187
Marissa Wallf58c14b2018-07-24 10:50:43 -07001188void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001189 mCurrentState.barrierLayer_legacy = barrierLayer;
1190 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001191 // We don't set eTransactionNeeded, because just receiving a deferral
1192 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001193 mCurrentState.modified = true;
1194 pushPendingState();
1195 mCurrentState.barrierLayer_legacy = nullptr;
1196 mCurrentState.frameNumber_legacy = 0;
1197 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001198}
1199
Marissa Wallf58c14b2018-07-24 10:50:43 -07001200void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001201 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001202 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001203}
1204
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001205// ----------------------------------------------------------------------------
1206// pageflip handling...
1207// ----------------------------------------------------------------------------
1208
Robert Carr1f0a16a2016-10-24 16:27:39 -07001209bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001210 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001211 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001212 if (parent != nullptr && parent->isHiddenByPolicy()) {
1213 return true;
1214 }
1215 return s.flags & layer_state_t::eLayerHidden;
1216}
1217
David Sodman41fdfc92017-11-06 16:09:56 -08001218uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001219 // TODO: should we do something special if mSecure is set?
1220 if (mProtectedByApp) {
1221 // need a hardware-protected path to external video sink
1222 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001223 }
Riley Andrews03414a12014-07-01 14:22:59 -07001224 if (mPotentialCursor) {
1225 usage |= GraphicBuffer::USAGE_CURSOR;
1226 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001227 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001228 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001229}
1230
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001231void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001232 uint32_t orientation = 0;
Vishnu Nair5eb3f062019-04-08 08:21:03 -07001233 // Disable setting transform hint if the debug flag is set.
1234 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001235 // The transform hint is used to improve performance, but we can
1236 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001237 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001238 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001239 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001240 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001241 orientation = 0;
1242 }
1243 }
David Sodmaneb085e02017-10-05 18:49:04 -07001244 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001245}
1246
Mathias Agopian13127d82013-03-05 17:47:11 -08001247// ----------------------------------------------------------------------------
1248// debugging
1249// ----------------------------------------------------------------------------
1250
Marissa Wall61c58622018-07-18 10:12:20 -07001251// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001252LayerDebugInfo Layer::getLayerDebugInfo() const {
1253 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001254 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001255 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001256 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001257 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
Yiwei Zhang5434a782018-12-05 18:06:32 -08001258 info.mType = std::string(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001259 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001260 info.mVisibleRegion = visibleRegion;
1261 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001262 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001263 info.mX = ds.active_legacy.transform.tx();
1264 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001265 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001266 info.mWidth = ds.active_legacy.w;
1267 info.mHeight = ds.active_legacy.h;
1268 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001269 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001270 info.mFlags = ds.flags;
1271 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001272 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001273 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1274 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1275 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1276 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001277 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001278 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001279 if (buffer != 0) {
1280 info.mActiveBufferWidth = buffer->getWidth();
1281 info.mActiveBufferHeight = buffer->getHeight();
1282 info.mActiveBufferStride = buffer->getStride();
1283 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001284 } else {
1285 info.mActiveBufferWidth = 0;
1286 info.mActiveBufferHeight = 0;
1287 info.mActiveBufferStride = 0;
1288 info.mActiveBufferFormat = 0;
1289 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001290 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001291 info.mNumQueuedFrames = getQueuedFrameCount();
1292 info.mRefreshPending = isBufferLatched();
1293 info.mIsOpaque = isOpaque(ds);
1294 info.mContentDirty = contentDirty;
1295 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001296}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001297
Yiwei Zhang5434a782018-12-05 18:06:32 -08001298void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001299 result.append("-------------------------------");
1300 result.append("-------------------------------");
1301 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001302 result.append(" Layer name\n");
1303 result.append(" Z | ");
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001304 result.append(" Window Type | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001305 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001306 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001307 result.append(" Disp Frame (LTRB) | ");
1308 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001309 result.append("-------------------------------");
1310 result.append("-------------------------------");
1311 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001312}
1313
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001314void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1315 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1316 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001317 return;
1318 }
1319
Yiwei Zhang5434a782018-12-05 18:06:32 -08001320 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001321 if (mName.length() > 77) {
1322 std::string shortened;
1323 shortened.append(mName.string(), 36);
1324 shortened.append("[...]");
1325 shortened.append(mName.string() + (mName.length() - 36), 36);
Yiwei Zhang5434a782018-12-05 18:06:32 -08001326 name = shortened;
Dan Stozae22aec72016-08-01 13:20:59 -07001327 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001328 name = std::string(mName.string(), mName.size());
Dan Stozae22aec72016-08-01 13:20:59 -07001329 }
1330
Yiwei Zhang5434a782018-12-05 18:06:32 -08001331 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001332
Alec Mourib416efd2018-09-06 21:01:59 +00001333 const State& layerState(getDrawingState());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001334 const auto& compositionState = outputLayer->getState();
1335
Chia-I Wu1e043612018-03-01 09:45:09 -08001336 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001337 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001338 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001339 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001340 }
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001341 StringAppendF(&result, " %10d | ", mWindowType);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001342 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
1343 StringAppendF(&result, "%10s | ",
1344 toString(getCompositionLayer() ? compositionState.bufferTransform
1345 : static_cast<Hwc2::Transform>(0))
1346 .c_str());
1347 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001348 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001349 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001350 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1351 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001352
Yichi Chen6ca35192018-05-29 12:20:43 +08001353 result.append("- - - - - - - - - - - - - - - -");
1354 result.append("- - - - - - - - - - - - - - - -");
1355 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001356}
Dan Stozae22aec72016-08-01 13:20:59 -07001357
Yiwei Zhang5434a782018-12-05 18:06:32 -08001358void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001359 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001360}
1361
Svetoslavd85084b2014-03-20 10:28:31 -07001362void Layer::clearFrameStats() {
1363 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001364}
1365
Jamie Gennis6547ff42013-07-16 20:12:42 -07001366void Layer::logFrameStats() {
1367 mFrameTracker.logAndResetStats(mName);
1368}
1369
Svetoslavd85084b2014-03-20 10:28:31 -07001370void Layer::getFrameStats(FrameStats* outStats) const {
1371 mFrameTracker.getStats(outStats);
1372}
1373
Yiwei Zhang5434a782018-12-05 18:06:32 -08001374void Layer::dumpFrameEvents(std::string& result) {
1375 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001376 Mutex::Autolock lock(mFrameEventHistoryMutex);
1377 mFrameEventHistory.checkFencesForCompletion();
1378 mFrameEventHistory.dump(result);
1379}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001380
Brian Anderson5ea5e592016-12-01 16:54:33 -08001381void Layer::onDisconnect() {
1382 Mutex::Autolock lock(mFrameEventHistoryMutex);
1383 mFrameEventHistory.onDisconnect();
Yiwei Zhangaf8ee942018-11-22 00:15:23 -08001384 mFlinger->mTimeStats->onDestroy(getSequence());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001385}
1386
Brian Anderson3890c392016-07-25 12:48:08 -07001387void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001388 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001389 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001390 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1391 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001392 }
1393
Brian Andersond6927fb2016-07-23 23:37:30 -07001394 Mutex::Autolock lock(mFrameEventHistoryMutex);
1395 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001396 // If there are any unsignaled fences in the aquire timeline at this
1397 // point, the previously queued frame hasn't been latched yet. Go ahead
1398 // and try to get the signal time here so the syscall is taken out of
1399 // the main thread's critical path.
1400 mAcquireTimeline.updateSignalTimes();
1401 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001402 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001403 mFrameEventHistory.addQueue(*newTimestamps);
1404 }
1405
Brian Anderson3890c392016-07-25 12:48:08 -07001406 if (outDelta) {
1407 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001408 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001409}
Dan Stozae77c7662016-05-13 11:37:28 -07001410
Chia-I Wu98f1c102017-05-30 14:54:08 -07001411size_t Layer::getChildrenCount() const {
1412 size_t count = 0;
1413 for (const sp<Layer>& child : mCurrentChildren) {
1414 count += 1 + child->getChildrenCount();
1415 }
1416 return count;
1417}
1418
Robert Carr1f0a16a2016-10-24 16:27:39 -07001419void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001420 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001421 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001422
Robert Carr1f0a16a2016-10-24 16:27:39 -07001423 mCurrentChildren.add(layer);
1424 layer->setParent(this);
1425}
1426
1427ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001428 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001429 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001430
Robert Carr1323c952019-01-28 18:13:27 -08001431 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001432 return mCurrentChildren.remove(layer);
1433}
1434
Robert Carr1db73f62016-12-21 12:58:51 -08001435bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1436 sp<Handle> handle = nullptr;
1437 sp<Layer> newParent = nullptr;
1438 if (newParentHandle == nullptr) {
1439 return false;
1440 }
1441 handle = static_cast<Handle*>(newParentHandle.get());
1442 newParent = handle->owner.promote();
1443 if (newParent == nullptr) {
1444 ALOGE("Unable to promote Layer handle");
1445 return false;
1446 }
1447
chaviw5aedec92018-10-22 10:40:38 -07001448 if (attachChildren()) {
1449 setTransactionFlags(eTransactionNeeded);
1450 }
Robert Carr1db73f62016-12-21 12:58:51 -08001451 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001452 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001453 }
1454 mCurrentChildren.clear();
1455
1456 return true;
1457}
1458
Robert Carr15eae092018-03-23 13:43:53 -07001459void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001460 for (const sp<Layer>& child : mDrawingChildren) {
1461 child->mDrawingParent = newParent;
Vishnu Nairc652ff82019-03-15 12:48:54 -07001462 child->computeBounds(newParent->mBounds,
1463 newParent->getTransformWithScale(
1464 newParent->getBufferScaleTransform()));
Robert Carr578038f2018-03-09 12:25:24 -08001465 }
1466}
1467
chaviwf1961f72017-09-18 16:41:07 -07001468bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001469 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001470
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001471 // While layers are detached, we allow most operations
1472 // and simply halt performing the actual transaction. However
1473 // for reparent != null we would enter the mRemovedFromCurrentState
1474 // state, regardless of whether doTransaction was called, and
1475 // so we need to prevent the update here.
1476 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001477 return false;
1478 }
1479
Robert Carr54cf5b12019-01-25 14:02:28 -08001480 sp<Layer> newParent;
1481 if (newParentHandle != nullptr) {
1482 auto handle = static_cast<Handle*>(newParentHandle.get());
1483 newParent = handle->owner.promote();
1484 if (newParent == nullptr) {
1485 ALOGE("Unable to promote Layer handle");
1486 return false;
1487 }
1488 if (newParent == this) {
1489 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1490 return false;
1491 }
1492 }
1493
chaviwf1961f72017-09-18 16:41:07 -07001494 sp<Layer> parent = getParent();
1495 if (parent != nullptr) {
1496 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001497 }
1498
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001499 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001500 newParent->addChild(this);
1501 if (!newParent->isRemovedFromCurrentState()) {
1502 addToCurrentState();
1503 } else {
1504 onRemovedFromCurrentState();
1505 }
1506
1507 if (mLayerDetached) {
1508 mLayerDetached = false;
1509 callSetTransactionFlags = true;
1510 }
1511 } else {
1512 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001513 }
1514
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001515 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001516 setTransactionFlags(eTransactionNeeded);
1517 }
chaviw06178942017-07-27 10:25:59 -07001518 return true;
1519}
1520
Robert Carr9524cb32017-02-13 11:32:32 -08001521bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001522 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001523 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001524 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001525 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001526 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001527 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001528 }
Robert Carr7f619b22017-11-06 12:56:35 -08001529 }
Robert Carr9524cb32017-02-13 11:32:32 -08001530
1531 return true;
1532}
1533
chaviw5aedec92018-10-22 10:40:38 -07001534bool Layer::attachChildren() {
1535 bool changed = false;
1536 for (const sp<Layer>& child : mCurrentChildren) {
1537 sp<Client> parentClient = mClientRef.promote();
1538 sp<Client> client(child->mClientRef.promote());
1539 if (client != nullptr && parentClient != client) {
1540 if (child->mLayerDetached) {
1541 child->mLayerDetached = false;
1542 changed = true;
1543 }
1544 changed |= child->attachChildren();
1545 }
1546 }
1547
1548 return changed;
1549}
1550
Peiyong Lind3788632018-09-18 16:01:31 -07001551bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001552 static const mat4 identityMatrix = mat4();
1553
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001554 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001555 return false;
1556 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001557 ++mCurrentState.sequence;
1558 mCurrentState.colorTransform = matrix;
1559 mCurrentState.hasColorTransform = matrix != identityMatrix;
1560 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001561 setTransactionFlags(eTransactionNeeded);
1562 return true;
1563}
1564
chaviwf66724d2018-11-28 16:35:21 -08001565mat4 Layer::getColorTransform() const {
1566 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1567 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1568 colorTransform = parent->getColorTransform() * colorTransform;
1569 }
1570 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001571}
1572
1573bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001574 bool hasColorTransform = getDrawingState().hasColorTransform;
1575 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1576 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1577 }
1578 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001579}
1580
Chia-I Wu11481472018-05-04 10:43:19 -07001581bool Layer::isLegacyDataSpace() const {
1582 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001583 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001584 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001585}
1586
Robert Carr1f0a16a2016-10-24 16:27:39 -07001587void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001588 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001589}
1590
Robert Carr1f0a16a2016-10-24 16:27:39 -07001591int32_t Layer::getZ() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001592 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001593}
1594
Robert Carr29abff82017-12-04 13:51:20 -08001595bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1596 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001597 const State& state = useDrawing ? mDrawingState : mCurrentState;
Robert Carr29abff82017-12-04 13:51:20 -08001598 return state.zOrderRelativeOf != nullptr;
1599}
1600
David Sodman41fdfc92017-11-06 16:09:56 -08001601__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001602 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001603 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1604 "makeTraversalList received invalid stateSet");
1605 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1606 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001607 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001608
Robert Carr29abff82017-12-04 13:51:20 -08001609 if (state.zOrderRelatives.size() == 0) {
1610 *outSkipRelativeZUsers = true;
1611 return children;
1612 }
1613
chaviwfd462612018-05-31 16:11:27 -07001614 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001615 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001616 sp<Layer> strongRelative = weakRelative.promote();
1617 if (strongRelative != nullptr) {
1618 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001619 }
1620 }
1621
Dan Stoza412903f2017-04-27 13:42:17 -07001622 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001623 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
Robert Carr503c7042017-09-27 15:06:08 -07001624 if (childState.zOrderRelativeOf != nullptr) {
1625 continue;
1626 }
Robert Carrdb66e622017-04-10 16:55:57 -07001627 traverse.add(child);
1628 }
1629
1630 return traverse;
1631}
1632
Robert Carr1f0a16a2016-10-24 16:27:39 -07001633/**
Robert Carrdb66e622017-04-10 16:55:57 -07001634 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001635 */
Dan Stoza412903f2017-04-27 13:42:17 -07001636void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001637 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1638 // produce a new list for traversing, including our relatives, and not including our children
1639 // who are relatives of another surface. In the case that there are no relative Z,
1640 // makeTraversalList returns our children directly to avoid significant overhead.
1641 // However in this case we need to take the responsibility for filtering children which
1642 // are relatives of another surface here.
1643 bool skipRelativeZUsers = false;
1644 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001645
Robert Carr1f0a16a2016-10-24 16:27:39 -07001646 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001647 for (; i < list.size(); i++) {
1648 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001649 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1650 continue;
1651 }
1652
Robert Carrdb66e622017-04-10 16:55:57 -07001653 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001654 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001655 }
Dan Stoza412903f2017-04-27 13:42:17 -07001656 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001657 }
Robert Carr29abff82017-12-04 13:51:20 -08001658
Dan Stoza412903f2017-04-27 13:42:17 -07001659 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001660 for (; i < list.size(); i++) {
1661 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001662
Robert Carr29abff82017-12-04 13:51:20 -08001663 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1664 continue;
1665 }
Dan Stoza412903f2017-04-27 13:42:17 -07001666 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001667 }
1668}
1669
1670/**
Robert Carrdb66e622017-04-10 16:55:57 -07001671 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001672 */
Dan Stoza412903f2017-04-27 13:42:17 -07001673void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1674 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001675 // See traverseInZOrder for documentation.
1676 bool skipRelativeZUsers = false;
1677 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001678
Robert Carr1f0a16a2016-10-24 16:27:39 -07001679 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001680 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001681 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001682
1683 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1684 continue;
1685 }
1686
Robert Carrdb66e622017-04-10 16:55:57 -07001687 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001688 break;
1689 }
Dan Stoza412903f2017-04-27 13:42:17 -07001690 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001691 }
Dan Stoza412903f2017-04-27 13:42:17 -07001692 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001693 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001694 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001695
1696 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1697 continue;
1698 }
1699
Dan Stoza412903f2017-04-27 13:42:17 -07001700 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001701 }
1702}
1703
chaviw4b129c22018-04-09 16:19:43 -07001704LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1705 const std::vector<Layer*>& layersInTree) {
1706 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1707 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001708 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1709 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001710 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001711
chaviwfd462612018-05-31 16:11:27 -07001712 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001713 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1714 sp<Layer> strongRelative = weakRelative.promote();
1715 // Only add relative layers that are also descendents of the top most parent of the tree.
1716 // If a relative layer is not a descendent, then it should be ignored.
1717 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1718 traverse.add(strongRelative);
1719 }
1720 }
1721
1722 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001723 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001724 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1725 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1726 // the child here since it won't be added later as a relative.
1727 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1728 childState.zOrderRelativeOf.promote().get())) {
1729 continue;
1730 }
1731 traverse.add(child);
1732 }
1733
1734 return traverse;
1735}
1736
1737void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1738 LayerVector::StateSet stateSet,
1739 const LayerVector::Visitor& visitor) {
1740 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001741
1742 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001743 for (; i < list.size(); i++) {
1744 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001745 if (relative->getZ() >= 0) {
1746 break;
1747 }
chaviw4b129c22018-04-09 16:19:43 -07001748 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001749 }
chaviw4b129c22018-04-09 16:19:43 -07001750
chaviwa76b2712017-09-20 12:02:26 -07001751 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001752 for (; i < list.size(); i++) {
1753 const auto& relative = list[i];
1754 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001755 }
1756}
1757
chaviw4b129c22018-04-09 16:19:43 -07001758std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1759 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1760 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1761
1762 std::vector<Layer*> layersInTree = {this};
1763 for (size_t i = 0; i < children.size(); i++) {
1764 const auto& child = children[i];
1765 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1766 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1767 }
1768
1769 return layersInTree;
1770}
1771
1772void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1773 const LayerVector::Visitor& visitor) {
1774 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1775 std::sort(layersInTree.begin(), layersInTree.end());
1776 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1777}
1778
Peiyong Linefefaac2018-08-17 12:27:51 -07001779ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001780 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001781}
1782
chaviw13fdc492017-06-27 12:40:18 -07001783half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001784 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001785
chaviw13fdc492017-06-27 12:40:18 -07001786 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1787 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001788}
Robert Carr6452f122017-03-21 10:41:29 -07001789
chaviw13fdc492017-06-27 12:40:18 -07001790half4 Layer::getColor() const {
1791 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001792 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001793}
Robert Carr6452f122017-03-21 10:41:29 -07001794
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001795Layer::RoundedCornerState Layer::getRoundedCornerState() const {
1796 const auto& p = mDrawingParent.promote();
1797 if (p != nullptr) {
Peiyong Lin27016a92019-03-29 17:36:08 +00001798 RoundedCornerState parentState = p->getRoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001799 if (parentState.radius > 0) {
1800 ui::Transform t = getActiveTransform(getDrawingState());
1801 t = t.inverse();
1802 parentState.cropRect = t.transform(parentState.cropRect);
1803 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1804 // but a transform matrix can define horizontal and vertical scales.
1805 // Let's take the average between both of them and pass into the shader, practically we
1806 // never do this type of transformation on windows anyway.
1807 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
1808 return parentState;
1809 }
1810 }
1811 const float radius = getDrawingState().cornerRadius;
Peiyong Linb9ff23e2019-04-08 11:04:59 -07001812 return radius > 0 && getCrop(getDrawingState()).isValid()
1813 ? RoundedCornerState(getCrop(getDrawingState()).toFloatRect(), radius)
1814 : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001815}
1816
Robert Carr1f0a16a2016-10-24 16:27:39 -07001817void Layer::commitChildList() {
1818 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1819 const auto& child = mCurrentChildren[i];
1820 child->commitChildList();
1821 }
1822 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001823 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001824}
1825
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001826static wp<Layer> extractLayerFromBinder(const wp<IBinder>& weakBinderHandle) {
1827 if (weakBinderHandle == nullptr) {
1828 return nullptr;
1829 }
1830 sp<IBinder> binderHandle = weakBinderHandle.promote();
1831 if (binderHandle == nullptr) {
1832 return nullptr;
1833 }
1834 sp<Layer::Handle> handle = static_cast<Layer::Handle*>(binderHandle.get());
1835 if (handle == nullptr) {
1836 return nullptr;
1837 }
1838 return handle->owner;
1839}
1840
Robert Carr720e5062018-07-30 17:45:14 -07001841void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001842 mCurrentState.inputInfo = info;
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001843 mCurrentState.touchableRegionCrop = extractLayerFromBinder(info.touchableRegionCropHandle);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001844 mCurrentState.modified = true;
1845 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07001846 setTransactionFlags(eTransactionNeeded);
1847}
1848
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001849void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet,
1850 uint32_t traceFlags) {
chaviw1d044282017-09-27 12:19:28 -07001851 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1852 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001853 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07001854
Peiyong Linefefaac2018-08-17 12:27:51 -07001855 ui::Transform requestedTransform = state.active_legacy.transform;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001856 ui::Transform transform = getTransform();
chaviw1d044282017-09-27 12:19:28 -07001857
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001858 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
1859 layerInfo->set_id(sequence);
1860 layerInfo->set_name(getName().c_str());
1861 layerInfo->set_type(String8(getTypeId()));
chaviw1d044282017-09-27 12:19:28 -07001862
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001863 for (const auto& child : children) {
1864 layerInfo->add_children(child->sequence);
chaviw1d044282017-09-27 12:19:28 -07001865 }
chaviw1d044282017-09-27 12:19:28 -07001866
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001867 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1868 sp<Layer> strongRelative = weakRelative.promote();
1869 if (strongRelative != nullptr) {
1870 layerInfo->add_relatives(strongRelative->sequence);
1871 }
chaviwadc40c22018-07-10 16:57:27 -07001872 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001873
1874 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
1875 [&]() { return layerInfo->mutable_transparent_region(); });
1876 LayerProtoHelper::writeToProto(visibleRegion,
1877 [&]() { return layerInfo->mutable_visible_region(); });
1878 LayerProtoHelper::writeToProto(surfaceDamageRegion,
1879 [&]() { return layerInfo->mutable_damage_region(); });
1880
1881 layerInfo->set_layer_stack(getLayerStack());
1882 layerInfo->set_z(state.z);
1883
1884 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
1885 [&]() { return layerInfo->mutable_position(); });
1886
1887 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(),
1888 [&]() {
1889 return layerInfo->mutable_requested_position();
1890 });
1891
1892 LayerProtoHelper::writeSizeToProto(state.active_legacy.w, state.active_legacy.h,
1893 [&]() { return layerInfo->mutable_size(); });
1894
1895 LayerProtoHelper::writeToProto(state.crop_legacy,
1896 [&]() { return layerInfo->mutable_crop(); });
1897 layerInfo->set_corner_radius(getRoundedCornerState().radius);
1898
1899 layerInfo->set_is_opaque(isOpaque(state));
1900 layerInfo->set_invalidate(contentDirty);
Peiyong Lin51598552019-04-17 14:09:22 -07001901 layerInfo->set_is_protected(isProtected());
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001902
1903 // XXX (b/79210409) mCurrentDataSpace is not protected
1904 layerInfo->set_dataspace(
1905 dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
1906
1907 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1908 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
1909 LayerProtoHelper::writeToProto(state.color,
1910 [&]() { return layerInfo->mutable_requested_color(); });
1911 layerInfo->set_flags(state.flags);
1912
1913 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1914 LayerProtoHelper::writeToProto(requestedTransform,
1915 layerInfo->mutable_requested_transform());
1916
1917 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
1918 if (parent != nullptr) {
1919 layerInfo->set_parent(parent->sequence);
1920 } else {
1921 layerInfo->set_parent(-1);
1922 }
1923
1924 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1925 if (zOrderRelativeOf != nullptr) {
1926 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1927 } else {
1928 layerInfo->set_z_order_relative_of(-1);
1929 }
1930
1931 auto buffer = mActiveBuffer;
1932 if (buffer != nullptr) {
1933 LayerProtoHelper::writeToProto(buffer,
1934 [&]() { return layerInfo->mutable_active_buffer(); });
1935 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
1936 layerInfo->mutable_buffer_transform());
1937 }
1938
1939 layerInfo->set_queued_frames(getQueuedFrameCount());
1940 layerInfo->set_refresh_pending(isBufferLatched());
1941 layerInfo->set_curr_frame(mCurrentFrameNumber);
1942 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
1943
1944 for (const auto& pendingState : mPendingStates) {
1945 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
1946 if (barrierLayer != nullptr) {
1947 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
1948 barrierLayerProto->set_id(barrierLayer->sequence);
1949 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
1950 }
1951 }
1952 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
chaviwadc40c22018-07-10 16:57:27 -07001953 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001954
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001955 if (traceFlags & SurfaceTracing::TRACE_INPUT) {
1956 LayerProtoHelper::writeToProto(state.inputInfo, state.touchableRegionCrop,
1957 [&]() { return layerInfo->mutable_input_window_info(); });
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001958 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001959
1960 if (traceFlags & SurfaceTracing::TRACE_EXTRA) {
1961 auto protoMap = layerInfo->mutable_metadata();
1962 for (const auto& entry : state.metadata.mMap) {
1963 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
1964 }
1965 LayerProtoHelper::writeToProto(mEffectiveTransform,
1966 layerInfo->mutable_effective_transform());
1967 LayerProtoHelper::writeToProto(mSourceBounds,
1968 [&]() { return layerInfo->mutable_source_bounds(); });
1969 LayerProtoHelper::writeToProto(mScreenBounds,
1970 [&]() { return layerInfo->mutable_screen_bounds(); });
1971 }
chaviw1d044282017-09-27 12:19:28 -07001972}
1973
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001974void Layer::writeToProto(LayerProto* layerInfo, const sp<DisplayDevice>& displayDevice,
1975 uint32_t traceFlags) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001976 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1977 if (!outputLayer) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07001978 return;
1979 }
1980
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001981 writeToProto(layerInfo, LayerVector::StateSet::Drawing, traceFlags);
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001982
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001983 const auto& compositionState = outputLayer->getState();
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001984
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001985 const Rect& frame = compositionState.displayFrame;
Nataniel Borges797b0e42019-02-15 14:11:58 -08001986 LayerProtoHelper::writeToProto(frame, [&]() { return layerInfo->mutable_hwc_frame(); });
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001987
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001988 const FloatRect& crop = compositionState.sourceCrop;
Nataniel Borges797b0e42019-02-15 14:11:58 -08001989 LayerProtoHelper::writeToProto(crop, [&]() { return layerInfo->mutable_hwc_crop(); });
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001990
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001991 const int32_t transform =
1992 getCompositionLayer() ? static_cast<int32_t>(compositionState.bufferTransform) : 0;
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001993 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08001994
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001995 const int32_t compositionType =
1996 static_cast<int32_t>(compositionState.hwc ? (*compositionState.hwc).hwcCompositionType
1997 : Hwc2::IComposerClient::Composition::CLIENT);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08001998 layerInfo->set_hwc_composition_type(compositionType);
1999
2000 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
2001 static_cast<BufferLayer*>(this)->isProtected()) {
2002 layerInfo->set_is_protected(true);
2003 } else {
2004 layerInfo->set_is_protected(false);
2005 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002006}
2007
Robert Carr2e102c92018-10-23 12:11:15 -07002008bool Layer::isRemovedFromCurrentState() const {
2009 return mRemovedFromCurrentState;
2010}
2011
Arthur Hungd20b2702019-01-14 18:16:16 +08002012InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002013 InputWindowInfo info = mDrawingState.inputInfo;
Robert Carr720e5062018-07-30 17:45:14 -07002014
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002015 if (info.displayId == ADISPLAY_ID_NONE) {
2016 info.displayId = mDrawingState.layerStack;
2017 }
2018
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002019 ui::Transform t = getTransform();
2020 const float xScale = t.sx();
2021 const float yScale = t.sy();
2022 if (xScale != 1.0f || yScale != 1.0f) {
2023 info.windowXScale *= 1.0f / xScale;
2024 info.windowYScale *= 1.0f / yScale;
2025 info.touchableRegion.scaleSelf(xScale, yScale);
Riddle Hsu39d4aa52018-11-30 20:46:53 +08002026 }
Robert Carre07e1032018-11-26 12:55:53 -08002027
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002028 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002029 // If this is a portal window, set the touchableRegion to the layerBounds.
2030 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
2031 ? getBufferSize(getDrawingState())
2032 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002033 if (!layerBounds.isValid()) {
2034 layerBounds = getCroppedBufferSize(getDrawingState());
2035 }
Vishnu Nair8033a492018-12-05 07:27:23 -08002036 layerBounds = t.transform(layerBounds);
2037 layerBounds.inset(info.surfaceInset, info.surfaceInset, info.surfaceInset, info.surfaceInset);
2038
Arthur Hungd20b2702019-01-14 18:16:16 +08002039 // Input coordinate should match the layer bounds.
2040 info.frameLeft = layerBounds.left;
2041 info.frameTop = layerBounds.top;
2042 info.frameRight = layerBounds.right;
2043 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08002044
2045 // Position the touchable region relative to frame screen location and restrict it to frame
2046 // bounds.
2047 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08002048 info.visible = canReceiveInput();
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002049
2050 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2051 if (info.replaceTouchableRegionWithCrop) {
2052 if (cropLayer == nullptr) {
2053 info.touchableRegion = Region(Rect{mScreenBounds});
2054 } else {
2055 info.touchableRegion = Region(Rect{cropLayer->mScreenBounds});
2056 }
2057 } else if (cropLayer != nullptr) {
2058 info.touchableRegion = info.touchableRegion.intersect(Rect{cropLayer->mScreenBounds});
2059 }
2060
Robert Carr720e5062018-07-30 17:45:14 -07002061 return info;
2062}
2063
2064bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002065 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002066}
2067
Lloyd Piquefeb73d72018-12-04 17:23:44 -08002068std::shared_ptr<compositionengine::Layer> Layer::getCompositionLayer() const {
2069 return nullptr;
2070}
2071
chaviw3e727cd2019-01-31 13:41:05 -08002072bool Layer::canReceiveInput() const {
2073 return isVisible();
2074}
2075
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002076compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
2077 const sp<const DisplayDevice>& display) const {
2078 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionLayer().get());
2079}
2080
Mathias Agopian13127d82013-03-05 17:47:11 -08002081// ---------------------------------------------------------------------------
2082
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002083}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002084
2085#if defined(__gl_h_)
2086#error "don't include gl/gl.h in this file"
2087#endif
2088
2089#if defined(__gl2_h_)
2090#error "don't include gl2/gl2.h in this file"
2091#endif