blob: b47497a33ae335b445ddfeb8ca1635172ba1bf0a [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);
Robert Carr2e102c92018-10-23 12:11:15 -0700133
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 Nairf2deb822018-11-12 17:53:48 -08001233 // Disable setting transform hint if the debug flag is set or if the
1234 // getTransformToDisplayInverse flag is set and the client wants to submit buffers
1235 // in one orientation.
1236 if (!mFlinger->mDebugDisableTransformHint && !getTransformToDisplayInverse()) {
Mathias Agopian84300952012-11-21 16:02:13 -08001237 // The transform hint is used to improve performance, but we can
1238 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001239 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001240 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001241 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001242 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001243 orientation = 0;
1244 }
1245 }
David Sodmaneb085e02017-10-05 18:49:04 -07001246 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001247}
1248
Mathias Agopian13127d82013-03-05 17:47:11 -08001249// ----------------------------------------------------------------------------
1250// debugging
1251// ----------------------------------------------------------------------------
1252
Marissa Wall61c58622018-07-18 10:12:20 -07001253// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001254LayerDebugInfo Layer::getLayerDebugInfo() const {
1255 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001256 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001257 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001258 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001259 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
Yiwei Zhang5434a782018-12-05 18:06:32 -08001260 info.mType = std::string(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001261 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001262 info.mVisibleRegion = visibleRegion;
1263 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001264 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001265 info.mX = ds.active_legacy.transform.tx();
1266 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001267 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001268 info.mWidth = ds.active_legacy.w;
1269 info.mHeight = ds.active_legacy.h;
1270 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001271 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001272 info.mFlags = ds.flags;
1273 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001274 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001275 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1276 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1277 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1278 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001279 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001280 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001281 if (buffer != 0) {
1282 info.mActiveBufferWidth = buffer->getWidth();
1283 info.mActiveBufferHeight = buffer->getHeight();
1284 info.mActiveBufferStride = buffer->getStride();
1285 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001286 } else {
1287 info.mActiveBufferWidth = 0;
1288 info.mActiveBufferHeight = 0;
1289 info.mActiveBufferStride = 0;
1290 info.mActiveBufferFormat = 0;
1291 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001292 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001293 info.mNumQueuedFrames = getQueuedFrameCount();
1294 info.mRefreshPending = isBufferLatched();
1295 info.mIsOpaque = isOpaque(ds);
1296 info.mContentDirty = contentDirty;
1297 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001298}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001299
Yiwei Zhang5434a782018-12-05 18:06:32 -08001300void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001301 result.append("-------------------------------");
1302 result.append("-------------------------------");
1303 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001304 result.append(" Layer name\n");
1305 result.append(" Z | ");
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001306 result.append(" Window Type | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001307 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001308 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001309 result.append(" Disp Frame (LTRB) | ");
1310 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001311 result.append("-------------------------------");
1312 result.append("-------------------------------");
1313 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001314}
1315
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001316void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1317 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1318 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001319 return;
1320 }
1321
Yiwei Zhang5434a782018-12-05 18:06:32 -08001322 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001323 if (mName.length() > 77) {
1324 std::string shortened;
1325 shortened.append(mName.string(), 36);
1326 shortened.append("[...]");
1327 shortened.append(mName.string() + (mName.length() - 36), 36);
Yiwei Zhang5434a782018-12-05 18:06:32 -08001328 name = shortened;
Dan Stozae22aec72016-08-01 13:20:59 -07001329 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001330 name = std::string(mName.string(), mName.size());
Dan Stozae22aec72016-08-01 13:20:59 -07001331 }
1332
Yiwei Zhang5434a782018-12-05 18:06:32 -08001333 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001334
Alec Mourib416efd2018-09-06 21:01:59 +00001335 const State& layerState(getDrawingState());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001336 const auto& compositionState = outputLayer->getState();
1337
Chia-I Wu1e043612018-03-01 09:45:09 -08001338 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001339 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001340 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001341 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001342 }
Ady Abraham8f1ee7f2019-04-05 10:32:50 -07001343 StringAppendF(&result, " %10d | ", mWindowType);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001344 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
1345 StringAppendF(&result, "%10s | ",
1346 toString(getCompositionLayer() ? compositionState.bufferTransform
1347 : static_cast<Hwc2::Transform>(0))
1348 .c_str());
1349 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001350 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001351 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001352 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1353 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001354
Yichi Chen6ca35192018-05-29 12:20:43 +08001355 result.append("- - - - - - - - - - - - - - - -");
1356 result.append("- - - - - - - - - - - - - - - -");
1357 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001358}
Dan Stozae22aec72016-08-01 13:20:59 -07001359
Yiwei Zhang5434a782018-12-05 18:06:32 -08001360void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001361 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001362}
1363
Svetoslavd85084b2014-03-20 10:28:31 -07001364void Layer::clearFrameStats() {
1365 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001366}
1367
Jamie Gennis6547ff42013-07-16 20:12:42 -07001368void Layer::logFrameStats() {
1369 mFrameTracker.logAndResetStats(mName);
1370}
1371
Svetoslavd85084b2014-03-20 10:28:31 -07001372void Layer::getFrameStats(FrameStats* outStats) const {
1373 mFrameTracker.getStats(outStats);
1374}
1375
Yiwei Zhang5434a782018-12-05 18:06:32 -08001376void Layer::dumpFrameEvents(std::string& result) {
1377 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001378 Mutex::Autolock lock(mFrameEventHistoryMutex);
1379 mFrameEventHistory.checkFencesForCompletion();
1380 mFrameEventHistory.dump(result);
1381}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001382
Brian Anderson5ea5e592016-12-01 16:54:33 -08001383void Layer::onDisconnect() {
1384 Mutex::Autolock lock(mFrameEventHistoryMutex);
1385 mFrameEventHistory.onDisconnect();
Yiwei Zhangaf8ee942018-11-22 00:15:23 -08001386 mFlinger->mTimeStats->onDestroy(getSequence());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001387}
1388
Brian Anderson3890c392016-07-25 12:48:08 -07001389void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001390 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001391 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001392 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1393 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001394 }
1395
Brian Andersond6927fb2016-07-23 23:37:30 -07001396 Mutex::Autolock lock(mFrameEventHistoryMutex);
1397 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001398 // If there are any unsignaled fences in the aquire timeline at this
1399 // point, the previously queued frame hasn't been latched yet. Go ahead
1400 // and try to get the signal time here so the syscall is taken out of
1401 // the main thread's critical path.
1402 mAcquireTimeline.updateSignalTimes();
1403 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001404 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001405 mFrameEventHistory.addQueue(*newTimestamps);
1406 }
1407
Brian Anderson3890c392016-07-25 12:48:08 -07001408 if (outDelta) {
1409 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001410 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001411}
Dan Stozae77c7662016-05-13 11:37:28 -07001412
Chia-I Wu98f1c102017-05-30 14:54:08 -07001413size_t Layer::getChildrenCount() const {
1414 size_t count = 0;
1415 for (const sp<Layer>& child : mCurrentChildren) {
1416 count += 1 + child->getChildrenCount();
1417 }
1418 return count;
1419}
1420
Robert Carr1f0a16a2016-10-24 16:27:39 -07001421void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001422 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001423 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001424
Robert Carr1f0a16a2016-10-24 16:27:39 -07001425 mCurrentChildren.add(layer);
1426 layer->setParent(this);
1427}
1428
1429ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001430 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001431 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001432
Robert Carr1323c952019-01-28 18:13:27 -08001433 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001434 return mCurrentChildren.remove(layer);
1435}
1436
Robert Carr1db73f62016-12-21 12:58:51 -08001437bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1438 sp<Handle> handle = nullptr;
1439 sp<Layer> newParent = nullptr;
1440 if (newParentHandle == nullptr) {
1441 return false;
1442 }
1443 handle = static_cast<Handle*>(newParentHandle.get());
1444 newParent = handle->owner.promote();
1445 if (newParent == nullptr) {
1446 ALOGE("Unable to promote Layer handle");
1447 return false;
1448 }
1449
chaviw5aedec92018-10-22 10:40:38 -07001450 if (attachChildren()) {
1451 setTransactionFlags(eTransactionNeeded);
1452 }
Robert Carr1db73f62016-12-21 12:58:51 -08001453 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001454 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001455 }
1456 mCurrentChildren.clear();
1457
1458 return true;
1459}
1460
Robert Carr15eae092018-03-23 13:43:53 -07001461void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001462 for (const sp<Layer>& child : mDrawingChildren) {
1463 child->mDrawingParent = newParent;
Vishnu Nairc652ff82019-03-15 12:48:54 -07001464 child->computeBounds(newParent->mBounds,
1465 newParent->getTransformWithScale(
1466 newParent->getBufferScaleTransform()));
Robert Carr578038f2018-03-09 12:25:24 -08001467 }
1468}
1469
chaviwf1961f72017-09-18 16:41:07 -07001470bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001471 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001472
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001473 // While layers are detached, we allow most operations
1474 // and simply halt performing the actual transaction. However
1475 // for reparent != null we would enter the mRemovedFromCurrentState
1476 // state, regardless of whether doTransaction was called, and
1477 // so we need to prevent the update here.
1478 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001479 return false;
1480 }
1481
Robert Carr54cf5b12019-01-25 14:02:28 -08001482 sp<Layer> newParent;
1483 if (newParentHandle != nullptr) {
1484 auto handle = static_cast<Handle*>(newParentHandle.get());
1485 newParent = handle->owner.promote();
1486 if (newParent == nullptr) {
1487 ALOGE("Unable to promote Layer handle");
1488 return false;
1489 }
1490 if (newParent == this) {
1491 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1492 return false;
1493 }
1494 }
1495
chaviwf1961f72017-09-18 16:41:07 -07001496 sp<Layer> parent = getParent();
1497 if (parent != nullptr) {
1498 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001499 }
1500
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001501 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001502 newParent->addChild(this);
1503 if (!newParent->isRemovedFromCurrentState()) {
1504 addToCurrentState();
1505 } else {
1506 onRemovedFromCurrentState();
1507 }
1508
1509 if (mLayerDetached) {
1510 mLayerDetached = false;
1511 callSetTransactionFlags = true;
1512 }
1513 } else {
1514 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001515 }
1516
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001517 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001518 setTransactionFlags(eTransactionNeeded);
1519 }
chaviw06178942017-07-27 10:25:59 -07001520 return true;
1521}
1522
Robert Carr9524cb32017-02-13 11:32:32 -08001523bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001524 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001525 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001526 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001527 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001528 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001529 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001530 }
Robert Carr7f619b22017-11-06 12:56:35 -08001531 }
Robert Carr9524cb32017-02-13 11:32:32 -08001532
1533 return true;
1534}
1535
chaviw5aedec92018-10-22 10:40:38 -07001536bool Layer::attachChildren() {
1537 bool changed = false;
1538 for (const sp<Layer>& child : mCurrentChildren) {
1539 sp<Client> parentClient = mClientRef.promote();
1540 sp<Client> client(child->mClientRef.promote());
1541 if (client != nullptr && parentClient != client) {
1542 if (child->mLayerDetached) {
1543 child->mLayerDetached = false;
1544 changed = true;
1545 }
1546 changed |= child->attachChildren();
1547 }
1548 }
1549
1550 return changed;
1551}
1552
Peiyong Lind3788632018-09-18 16:01:31 -07001553bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001554 static const mat4 identityMatrix = mat4();
1555
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001556 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001557 return false;
1558 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001559 ++mCurrentState.sequence;
1560 mCurrentState.colorTransform = matrix;
1561 mCurrentState.hasColorTransform = matrix != identityMatrix;
1562 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001563 setTransactionFlags(eTransactionNeeded);
1564 return true;
1565}
1566
chaviwf66724d2018-11-28 16:35:21 -08001567mat4 Layer::getColorTransform() const {
1568 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1569 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1570 colorTransform = parent->getColorTransform() * colorTransform;
1571 }
1572 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001573}
1574
1575bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001576 bool hasColorTransform = getDrawingState().hasColorTransform;
1577 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1578 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1579 }
1580 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001581}
1582
Chia-I Wu11481472018-05-04 10:43:19 -07001583bool Layer::isLegacyDataSpace() const {
1584 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001585 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001586 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001587}
1588
Robert Carr1f0a16a2016-10-24 16:27:39 -07001589void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001590 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001591}
1592
Robert Carr1f0a16a2016-10-24 16:27:39 -07001593int32_t Layer::getZ() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001594 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001595}
1596
Robert Carr29abff82017-12-04 13:51:20 -08001597bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1598 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001599 const State& state = useDrawing ? mDrawingState : mCurrentState;
Robert Carr29abff82017-12-04 13:51:20 -08001600 return state.zOrderRelativeOf != nullptr;
1601}
1602
David Sodman41fdfc92017-11-06 16:09:56 -08001603__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001604 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001605 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1606 "makeTraversalList received invalid stateSet");
1607 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1608 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001609 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001610
Robert Carr29abff82017-12-04 13:51:20 -08001611 if (state.zOrderRelatives.size() == 0) {
1612 *outSkipRelativeZUsers = true;
1613 return children;
1614 }
1615
chaviwfd462612018-05-31 16:11:27 -07001616 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001617 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001618 sp<Layer> strongRelative = weakRelative.promote();
1619 if (strongRelative != nullptr) {
1620 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001621 }
1622 }
1623
Dan Stoza412903f2017-04-27 13:42:17 -07001624 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001625 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
Robert Carr503c7042017-09-27 15:06:08 -07001626 if (childState.zOrderRelativeOf != nullptr) {
1627 continue;
1628 }
Robert Carrdb66e622017-04-10 16:55:57 -07001629 traverse.add(child);
1630 }
1631
1632 return traverse;
1633}
1634
Robert Carr1f0a16a2016-10-24 16:27:39 -07001635/**
Robert Carrdb66e622017-04-10 16:55:57 -07001636 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001637 */
Dan Stoza412903f2017-04-27 13:42:17 -07001638void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001639 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1640 // produce a new list for traversing, including our relatives, and not including our children
1641 // who are relatives of another surface. In the case that there are no relative Z,
1642 // makeTraversalList returns our children directly to avoid significant overhead.
1643 // However in this case we need to take the responsibility for filtering children which
1644 // are relatives of another surface here.
1645 bool skipRelativeZUsers = false;
1646 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001647
Robert Carr1f0a16a2016-10-24 16:27:39 -07001648 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001649 for (; i < list.size(); i++) {
1650 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001651 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1652 continue;
1653 }
1654
Robert Carrdb66e622017-04-10 16:55:57 -07001655 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001656 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001657 }
Dan Stoza412903f2017-04-27 13:42:17 -07001658 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001659 }
Robert Carr29abff82017-12-04 13:51:20 -08001660
Dan Stoza412903f2017-04-27 13:42:17 -07001661 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001662 for (; i < list.size(); i++) {
1663 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001664
Robert Carr29abff82017-12-04 13:51:20 -08001665 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1666 continue;
1667 }
Dan Stoza412903f2017-04-27 13:42:17 -07001668 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001669 }
1670}
1671
1672/**
Robert Carrdb66e622017-04-10 16:55:57 -07001673 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001674 */
Dan Stoza412903f2017-04-27 13:42:17 -07001675void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1676 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001677 // See traverseInZOrder for documentation.
1678 bool skipRelativeZUsers = false;
1679 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001680
Robert Carr1f0a16a2016-10-24 16:27:39 -07001681 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001682 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001683 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001684
1685 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1686 continue;
1687 }
1688
Robert Carrdb66e622017-04-10 16:55:57 -07001689 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001690 break;
1691 }
Dan Stoza412903f2017-04-27 13:42:17 -07001692 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001693 }
Dan Stoza412903f2017-04-27 13:42:17 -07001694 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001695 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001696 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001697
1698 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1699 continue;
1700 }
1701
Dan Stoza412903f2017-04-27 13:42:17 -07001702 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001703 }
1704}
1705
chaviw4b129c22018-04-09 16:19:43 -07001706LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1707 const std::vector<Layer*>& layersInTree) {
1708 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1709 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001710 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1711 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001712 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001713
chaviwfd462612018-05-31 16:11:27 -07001714 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001715 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1716 sp<Layer> strongRelative = weakRelative.promote();
1717 // Only add relative layers that are also descendents of the top most parent of the tree.
1718 // If a relative layer is not a descendent, then it should be ignored.
1719 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1720 traverse.add(strongRelative);
1721 }
1722 }
1723
1724 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001725 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001726 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1727 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1728 // the child here since it won't be added later as a relative.
1729 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1730 childState.zOrderRelativeOf.promote().get())) {
1731 continue;
1732 }
1733 traverse.add(child);
1734 }
1735
1736 return traverse;
1737}
1738
1739void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1740 LayerVector::StateSet stateSet,
1741 const LayerVector::Visitor& visitor) {
1742 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001743
1744 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001745 for (; i < list.size(); i++) {
1746 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001747 if (relative->getZ() >= 0) {
1748 break;
1749 }
chaviw4b129c22018-04-09 16:19:43 -07001750 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001751 }
chaviw4b129c22018-04-09 16:19:43 -07001752
chaviwa76b2712017-09-20 12:02:26 -07001753 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001754 for (; i < list.size(); i++) {
1755 const auto& relative = list[i];
1756 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001757 }
1758}
1759
chaviw4b129c22018-04-09 16:19:43 -07001760std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1761 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1762 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1763
1764 std::vector<Layer*> layersInTree = {this};
1765 for (size_t i = 0; i < children.size(); i++) {
1766 const auto& child = children[i];
1767 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1768 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1769 }
1770
1771 return layersInTree;
1772}
1773
1774void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1775 const LayerVector::Visitor& visitor) {
1776 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1777 std::sort(layersInTree.begin(), layersInTree.end());
1778 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1779}
1780
Peiyong Linefefaac2018-08-17 12:27:51 -07001781ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001782 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001783}
1784
chaviw13fdc492017-06-27 12:40:18 -07001785half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001786 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001787
chaviw13fdc492017-06-27 12:40:18 -07001788 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1789 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001790}
Robert Carr6452f122017-03-21 10:41:29 -07001791
chaviw13fdc492017-06-27 12:40:18 -07001792half4 Layer::getColor() const {
1793 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001794 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001795}
Robert Carr6452f122017-03-21 10:41:29 -07001796
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001797Layer::RoundedCornerState Layer::getRoundedCornerState() const {
1798 const auto& p = mDrawingParent.promote();
1799 if (p != nullptr) {
Peiyong Lin27016a92019-03-29 17:36:08 +00001800 RoundedCornerState parentState = p->getRoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001801 if (parentState.radius > 0) {
1802 ui::Transform t = getActiveTransform(getDrawingState());
1803 t = t.inverse();
1804 parentState.cropRect = t.transform(parentState.cropRect);
1805 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1806 // but a transform matrix can define horizontal and vertical scales.
1807 // Let's take the average between both of them and pass into the shader, practically we
1808 // never do this type of transformation on windows anyway.
1809 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
1810 return parentState;
1811 }
1812 }
1813 const float radius = getDrawingState().cornerRadius;
Peiyong Lin8f16f412019-04-04 15:41:00 -07001814 return radius > 0 ? RoundedCornerState(getCrop(getDrawingState()).toFloatRect(), radius)
1815 : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001816}
1817
Robert Carr1f0a16a2016-10-24 16:27:39 -07001818void Layer::commitChildList() {
1819 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1820 const auto& child = mCurrentChildren[i];
1821 child->commitChildList();
1822 }
1823 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001824 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001825}
1826
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001827static wp<Layer> extractLayerFromBinder(const wp<IBinder>& weakBinderHandle) {
1828 if (weakBinderHandle == nullptr) {
1829 return nullptr;
1830 }
1831 sp<IBinder> binderHandle = weakBinderHandle.promote();
1832 if (binderHandle == nullptr) {
1833 return nullptr;
1834 }
1835 sp<Layer::Handle> handle = static_cast<Layer::Handle*>(binderHandle.get());
1836 if (handle == nullptr) {
1837 return nullptr;
1838 }
1839 return handle->owner;
1840}
1841
Robert Carr720e5062018-07-30 17:45:14 -07001842void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001843 mCurrentState.inputInfo = info;
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001844 mCurrentState.touchableRegionCrop = extractLayerFromBinder(info.touchableRegionCropHandle);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001845 mCurrentState.modified = true;
1846 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07001847 setTransactionFlags(eTransactionNeeded);
1848}
1849
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001850void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet,
1851 uint32_t traceFlags) {
chaviw1d044282017-09-27 12:19:28 -07001852 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1853 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001854 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07001855
Peiyong Linefefaac2018-08-17 12:27:51 -07001856 ui::Transform requestedTransform = state.active_legacy.transform;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001857 ui::Transform transform = getTransform();
chaviw1d044282017-09-27 12:19:28 -07001858
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001859 if (traceFlags & SurfaceTracing::TRACE_CRITICAL) {
1860 layerInfo->set_id(sequence);
1861 layerInfo->set_name(getName().c_str());
1862 layerInfo->set_type(String8(getTypeId()));
chaviw1d044282017-09-27 12:19:28 -07001863
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001864 for (const auto& child : children) {
1865 layerInfo->add_children(child->sequence);
chaviw1d044282017-09-27 12:19:28 -07001866 }
chaviw1d044282017-09-27 12:19:28 -07001867
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001868 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1869 sp<Layer> strongRelative = weakRelative.promote();
1870 if (strongRelative != nullptr) {
1871 layerInfo->add_relatives(strongRelative->sequence);
1872 }
chaviwadc40c22018-07-10 16:57:27 -07001873 }
Vishnu Nair9245d3b2019-03-22 13:38:56 -07001874
1875 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
1876 [&]() { return layerInfo->mutable_transparent_region(); });
1877 LayerProtoHelper::writeToProto(visibleRegion,
1878 [&]() { return layerInfo->mutable_visible_region(); });
1879 LayerProtoHelper::writeToProto(surfaceDamageRegion,
1880 [&]() { return layerInfo->mutable_damage_region(); });
1881
1882 layerInfo->set_layer_stack(getLayerStack());
1883 layerInfo->set_z(state.z);
1884
1885 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
1886 [&]() { return layerInfo->mutable_position(); });
1887
1888 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(),
1889 [&]() {
1890 return layerInfo->mutable_requested_position();
1891 });
1892
1893 LayerProtoHelper::writeSizeToProto(state.active_legacy.w, state.active_legacy.h,
1894 [&]() { return layerInfo->mutable_size(); });
1895
1896 LayerProtoHelper::writeToProto(state.crop_legacy,
1897 [&]() { return layerInfo->mutable_crop(); });
1898 layerInfo->set_corner_radius(getRoundedCornerState().radius);
1899
1900 layerInfo->set_is_opaque(isOpaque(state));
1901 layerInfo->set_invalidate(contentDirty);
1902
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