blob: 7c2776e0265dbf3c077fd6dc51ccb478cc1996d1 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dan Stoza9e56aa02015-11-02 13:00:03 -080017//#define LOG_NDEBUG 0
18#undef LOG_TAG
19#define LOG_TAG "Layer"
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080020#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21
Mathias Agopian13127d82013-03-05 17:47:11 -080022#include <math.h>
David Sodman41fdfc92017-11-06 16:09:56 -080023#include <stdint.h>
24#include <stdlib.h>
25#include <sys/types.h>
chaviw4b129c22018-04-09 16:19:43 -070026#include <algorithm>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080027#include <mutex>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080028
Yiwei Zhang5434a782018-12-05 18:06:32 -080029#include <android-base/stringprintf.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080030#include <compositionengine/Display.h>
Lloyd Pique0b785d82018-12-04 17:25:27 -080031#include <compositionengine/Layer.h>
Lloyd Piquea83776c2019-01-29 18:42:32 -080032#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080033#include <compositionengine/OutputLayer.h>
Lloyd Pique0b785d82018-12-04 17:25:27 -080034#include <compositionengine/impl/LayerCompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080035#include <compositionengine/impl/OutputLayerCompositionState.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070036#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070037#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070038#include <cutils/properties.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080039#include <gui/BufferItem.h>
40#include <gui/LayerDebugInfo.h>
41#include <gui/Surface.h>
42#include <renderengine/RenderEngine.h>
43#include <ui/DebugUtils.h>
44#include <ui/GraphicBuffer.h>
45#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046#include <utils/Errors.h>
47#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080048#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080050#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051
Yiwei Zhang60d1a192018-03-07 14:52:28 -080052#include "BufferLayer.h"
Valerie Haudd0b7572019-01-29 14:59:27 -080053#include "ColorLayer.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020054#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070055#include "DisplayDevice.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080056#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057#include "Layer.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080058#include "LayerProtoHelper.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070059#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070060#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061#include "SurfaceFlinger.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080062#include "TimeStats/TimeStats.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070063
David Sodman41fdfc92017-11-06 16:09:56 -080064#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066namespace android {
67
Yiwei Zhang5434a782018-12-05 18:06:32 -080068using base::StringAppendF;
69
Lloyd Piquef1c675b2018-09-12 20:45:39 -070070std::atomic<int32_t> Layer::sSequence{1};
Mathias Agopian13127d82013-03-05 17:47:11 -080071
Lloyd Pique42ab75e2018-09-12 20:46:03 -070072Layer::Layer(const LayerCreationArgs& args)
Lloyd Piquefa8fd6b2019-01-29 18:42:24 -080073 : mFlinger(args.flinger), mName(args.name), mClientRef(args.client) {
Mathias Agopiana67932f2011-04-20 14:20:59 -070074 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070075
76 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -070077 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
78 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
79 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070080
Dan Stozaf7ba41a2017-05-10 15:11:11 -070081 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070082
Lloyd Pique0449b0f2018-12-20 16:23:45 -080083 mCurrentState.active_legacy.w = args.w;
84 mCurrentState.active_legacy.h = args.h;
85 mCurrentState.flags = layerFlags;
86 mCurrentState.active_legacy.transform.set(0, 0);
87 mCurrentState.crop_legacy.makeInvalid();
88 mCurrentState.requestedCrop_legacy = mCurrentState.crop_legacy;
89 mCurrentState.z = 0;
90 mCurrentState.color.a = 1.0f;
91 mCurrentState.layerStack = 0;
92 mCurrentState.sequence = 0;
93 mCurrentState.requested_legacy = mCurrentState.active_legacy;
Lloyd Pique0449b0f2018-12-20 16:23:45 -080094 mCurrentState.active.w = UINT32_MAX;
95 mCurrentState.active.h = UINT32_MAX;
96 mCurrentState.active.transform.set(0, 0);
97 mCurrentState.transform = 0;
98 mCurrentState.transformToDisplayInverse = false;
99 mCurrentState.crop.makeInvalid();
100 mCurrentState.acquireFence = new Fence(-1);
101 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
102 mCurrentState.hdrMetadata.validTypes = 0;
103 mCurrentState.surfaceDamageRegion.clear();
104 mCurrentState.cornerRadius = 0.0f;
105 mCurrentState.api = -1;
106 mCurrentState.hasColorTransform = false;
Peiyong Linc502cb72019-03-01 15:00:23 -0800107 mCurrentState.colorSpaceAgnostic = false;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700108
109 // drawing state & current state are identical
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800110 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700111
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800112 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700113 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800114 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200115 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700116
117 mFlinger->onLayerCreated();
Dan Stoza436ccf32018-06-21 12:10:12 -0700118}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700119
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700120Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800121 sp<Client> c(mClientRef.promote());
122 if (c != 0) {
123 c->detachLayer(this);
124 }
125
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000126 mFrameTracker.logAndResetStats(mName);
Robert Carr2e102c92018-10-23 12:11:15 -0700127
Robert Carr2e102c92018-10-23 12:11:15 -0700128 mFlinger->onLayerDestroyed();
Mathias Agopian96f08192010-06-02 23:28:45 -0700129}
130
Mathias Agopian13127d82013-03-05 17:47:11 -0800131// ---------------------------------------------------------------------------
132// callbacks
133// ---------------------------------------------------------------------------
134
David Sodmaneb085e02017-10-05 18:49:04 -0700135/*
136 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
137 * Layer. So, the implementation is done in BufferLayer. When called on a
138 * ColorLayer object, it's essentially a NOP.
139 */
David Sodmaneb085e02017-10-05 18:49:04 -0700140void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800141
Chia-I Wuc6657022017-08-15 11:18:17 -0700142void Layer::onRemovedFromCurrentState() {
Robert Carr2e102c92018-10-23 12:11:15 -0700143 mRemovedFromCurrentState = true;
144
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800145 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
146 if (mCurrentState.zOrderRelativeOf != nullptr) {
147 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
148 if (strongRelative != nullptr) {
149 strongRelative->removeZOrderRelative(this);
150 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700151 }
chaviw606e5cf2019-03-01 10:12:10 -0800152 setZOrderRelativeOf(nullptr);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700153 }
Rob Carr4bba3702018-10-08 21:53:30 +0000154
Robert Carr2e102c92018-10-23 12:11:15 -0700155 // Since we are no longer reachable from CurrentState SurfaceFlinger
156 // will no longer invoke doTransaction for us, and so we will
157 // never finish applying transactions. We signal the sync point
158 // now so that another layer will not become indefinitely
159 // blocked.
160 for (auto& point: mRemoteSyncPoints) {
161 point->setTransactionApplied();
162 }
163 mRemoteSyncPoints.clear();
164
165 {
166 Mutex::Autolock syncLock(mLocalSyncPointMutex);
167 for (auto& point : mLocalSyncPoints) {
168 point->setFrameAvailable();
169 }
170 mLocalSyncPoints.clear();
171 }
172
Chia-I Wuc6657022017-08-15 11:18:17 -0700173 for (const auto& child : mCurrentChildren) {
174 child->onRemovedFromCurrentState();
175 }
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800176
177 mFlinger->markLayerPendingRemovalLocked(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700178}
Chia-I Wu38512252017-05-17 14:36:16 -0700179
chaviw61626f22018-11-15 16:26:27 -0800180void Layer::addToCurrentState() {
181 mRemovedFromCurrentState = false;
182
183 for (const auto& child : mCurrentChildren) {
184 child->addToCurrentState();
185 }
186}
187
Mathias Agopian13127d82013-03-05 17:47:11 -0800188// ---------------------------------------------------------------------------
189// set-up
190// ---------------------------------------------------------------------------
191
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700192const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800193 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800194}
195
chaviw13fdc492017-06-27 12:40:18 -0700196bool Layer::getPremultipledAlpha() const {
197 return mPremultipliedAlpha;
198}
199
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700200sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800201 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700202 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800203}
204
205// ---------------------------------------------------------------------------
206// h/w composer set-up
207// ---------------------------------------------------------------------------
208
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800209bool Layer::hasHwcLayer(const sp<const DisplayDevice>& displayDevice) {
210 auto outputLayer = findOutputLayerForDisplay(displayDevice);
211 LOG_FATAL_IF(!outputLayer);
212 return outputLayer->getState().hwc && (*outputLayer->getState().hwc).hwcLayer != nullptr;
213}
214
215HWC2::Layer* Layer::getHwcLayer(const sp<const DisplayDevice>& displayDevice) {
216 auto outputLayer = findOutputLayerForDisplay(displayDevice);
217 if (!outputLayer || !outputLayer->getState().hwc) {
218 return nullptr;
219 }
220 return (*outputLayer->getState().hwc).hwcLayer.get();
Steven Thomasb02664d2017-07-26 18:48:28 -0700221}
Steven Thomasb02664d2017-07-26 18:48:28 -0700222
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800223Rect Layer::getContentCrop() const {
224 // this is the crop rectangle that applies to the buffer
225 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700226 Rect crop;
227 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800228 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700229 crop = mCurrentCrop;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800230 } else if (mActiveBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800231 // otherwise we use the whole buffer
Lloyd Pique0b785d82018-12-04 17:25:27 -0800232 crop = mActiveBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700233 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800234 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700235 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700236 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700237 return crop;
238}
239
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700240static Rect reduce(const Rect& win, const Region& exclude) {
241 if (CC_LIKELY(exclude.isEmpty())) {
242 return win;
243 }
244 if (exclude.isRect()) {
245 return win.reduce(exclude.getBounds());
246 }
247 return Region(win).subtract(exclude).getBounds();
248}
249
Dan Stoza80d61162017-12-20 15:57:52 -0800250static FloatRect reduce(const FloatRect& win, const Region& exclude) {
251 if (CC_LIKELY(exclude.isEmpty())) {
252 return win;
253 }
254 // Convert through Rect (by rounding) for lack of FloatRegion
255 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
256}
257
Vishnu Nair4351ad52019-02-11 14:13:02 -0800258Rect Layer::getScreenBounds(bool reduceTransparentRegion) const {
Vishnu Nairf0c28512019-02-08 12:40:28 -0800259 if (!reduceTransparentRegion) {
260 return Rect{mScreenBounds};
261 }
262
263 FloatRect bounds = getBounds();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800264 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800265 // Transform to screen space.
266 bounds = t.transform(bounds);
267 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700268}
269
Vishnu Nair4351ad52019-02-11 14:13:02 -0800270FloatRect Layer::getBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000271 const State& s(getDrawingState());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800272 return getBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700273}
274
Vishnu Nairf0c28512019-02-08 12:40:28 -0800275FloatRect Layer::getBounds(const Region& activeTransparentRegion) const {
276 // Subtract the transparent region and snap to the bounds.
277 return reduce(mBounds, activeTransparentRegion);
278}
279
Vishnu Nair4351ad52019-02-11 14:13:02 -0800280ui::Transform Layer::getTransformWithScale() const {
281 // If the layer is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
282 // it isFixedSize) then there may be additional scaling not accounted
283 // for in the transform. We need to mirror this scaling to child surfaces
284 // or we will break the contract where WM can treat child surfaces as
285 // pixels in the parent surface.
Lloyd Pique0b785d82018-12-04 17:25:27 -0800286 if (!isFixedSize() || !mActiveBuffer) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800287 return mEffectiveTransform;
288 }
289
Marissa Wall290ad082019-03-06 13:23:47 -0800290 // If the layer is a buffer state layer, the active width and height
291 // could be infinite. In that case, return the effective transform.
292 const uint32_t activeWidth = getActiveWidth(getDrawingState());
293 const uint32_t activeHeight = getActiveHeight(getDrawingState());
294 if (activeWidth >= UINT32_MAX && activeHeight >= UINT32_MAX) {
295 return mEffectiveTransform;
296 }
297
Vishnu Nair4351ad52019-02-11 14:13:02 -0800298 int bufferWidth;
299 int bufferHeight;
300 if ((mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800301 bufferWidth = mActiveBuffer->getWidth();
302 bufferHeight = mActiveBuffer->getHeight();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800303 } else {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800304 bufferHeight = mActiveBuffer->getWidth();
305 bufferWidth = mActiveBuffer->getHeight();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800306 }
Marissa Wall290ad082019-03-06 13:23:47 -0800307 float sx = activeWidth / static_cast<float>(bufferWidth);
308 float sy = activeHeight / static_cast<float>(bufferHeight);
309
Vishnu Nair4351ad52019-02-11 14:13:02 -0800310 ui::Transform extraParentScaling;
311 extraParentScaling.set(sx, 0, 0, sy);
312 return mEffectiveTransform * extraParentScaling;
313}
314
315void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform) {
316 const State& s(getDrawingState());
317
318 // Calculate effective layer transform
319 mEffectiveTransform = parentTransform * getActiveTransform(s);
320
321 // Transform parent bounds to layer space
322 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
323
324 // Calculate display frame
325 mSourceBounds = computeSourceBounds(parentBounds);
326
327 // Calculate bounds by croping diplay frame with layer crop and parent bounds
328 FloatRect bounds = mSourceBounds;
329 const Rect layerCrop = getCrop(s);
330 if (!layerCrop.isEmpty()) {
331 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
332 }
333 bounds = bounds.intersect(parentBounds);
334
335 mBounds = bounds;
336 mScreenBounds = mEffectiveTransform.transform(mBounds);
337 for (const sp<Layer>& child : mDrawingChildren) {
338 child->computeBounds(mBounds, getTransformWithScale());
339 }
340}
341
Vishnu Nair60356342018-11-13 13:00:45 -0800342Rect Layer::getCroppedBufferSize(const State& s) const {
343 Rect size = getBufferSize(s);
344 Rect crop = getCrop(s);
345 if (!crop.isEmpty() && size.isValid()) {
346 size.intersect(crop, &size);
347 } else if (!crop.isEmpty()) {
348 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700349 }
Vishnu Nair60356342018-11-13 13:00:45 -0800350 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800351}
352
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700353void Layer::setupRoundedCornersCropCoordinates(Rect win,
354 const FloatRect& roundedCornersCrop) const {
355 // Translate win by the rounded corners rect coordinates, to have all values in
356 // layer coordinate space.
357 win.left -= roundedCornersCrop.left;
358 win.right -= roundedCornersCrop.left;
359 win.top -= roundedCornersCrop.top;
360 win.bottom -= roundedCornersCrop.top;
361
Lloyd Pique0b785d82018-12-04 17:25:27 -0800362 renderengine::Mesh::VertexArray<vec2> cropCoords(
363 getCompositionLayer()->editState().reMesh.getCropCoordArray<vec2>());
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700364 cropCoords[0] = vec2(win.left, win.top);
365 cropCoords[1] = vec2(win.left, win.top + win.getHeight());
366 cropCoords[2] = vec2(win.right, win.top + win.getHeight());
367 cropCoords[3] = vec2(win.right, win.top);
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700368}
369
Lloyd Piquea83776c2019-01-29 18:42:32 -0800370void Layer::latchGeometry(compositionengine::LayerFECompositionState& compositionState) const {
371 const auto& drawingState{getDrawingState()};
372 auto alpha = static_cast<float>(getAlpha());
David Revemanecf0fa52017-03-03 11:32:44 -0500373 auto blendMode = HWC2::BlendMode::None;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800374 if (!isOpaque(drawingState) || alpha != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800375 blendMode =
376 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800377 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800378
Lloyd Piquea83776c2019-01-29 18:42:32 -0800379 int type = drawingState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
380 int appId = drawingState.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700381 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400382 if (parent.get()) {
383 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800384 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
385 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
386 if (parentType >= 0 || parentAppId >= 0) {
387 type = parentType;
388 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700389 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400390 }
391
Lloyd Piquea83776c2019-01-29 18:42:32 -0800392 compositionState.geomLayerTransform = getTransform();
393 compositionState.geomInverseLayerTransform = compositionState.geomLayerTransform.inverse();
394 compositionState.geomBufferSize = getBufferSize(drawingState);
395 compositionState.geomContentCrop = getContentCrop();
396 compositionState.geomCrop = getCrop(drawingState);
397 compositionState.geomBufferTransform = mCurrentTransform;
398 compositionState.geomBufferUsesDisplayInverseTransform = getTransformToDisplayInverse();
399 compositionState.geomActiveTransparentRegion = getActiveTransparentRegion(drawingState);
400 compositionState.geomLayerBounds = mBounds;
401 compositionState.geomUsesSourceCrop = usesSourceCrop();
402 compositionState.isSecure = isSecure();
David Sodman15094112018-10-11 09:39:37 -0700403
Lloyd Piquea83776c2019-01-29 18:42:32 -0800404 compositionState.blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
405 compositionState.alpha = alpha;
406 compositionState.type = type;
407 compositionState.appId = appId;
408}
David Sodmanba340492018-08-05 21:51:33 -0700409
Lloyd Piquea83776c2019-01-29 18:42:32 -0800410void Layer::latchCompositionState(compositionengine::LayerFECompositionState& compositionState,
411 bool includeGeometry) const {
412 if (includeGeometry) {
413 latchGeometry(compositionState);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700414 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800415}
Mathias Agopian29a367b2011-07-12 14:51:45 -0700416
Lloyd Piquea83776c2019-01-29 18:42:32 -0800417const char* Layer::getDebugName() const {
418 return mName.string();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800419}
420
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800421void Layer::forceClientComposition(const sp<DisplayDevice>& display) {
422 const auto outputLayer = findOutputLayerForDisplay(display);
423 LOG_FATAL_IF(!outputLayer);
424 outputLayer->editState().forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800425}
Dan Stozaee44edd2015-03-23 15:50:23 -0700426
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800427bool Layer::getForceClientComposition(const sp<DisplayDevice>& display) {
428 const auto outputLayer = findOutputLayerForDisplay(display);
429 LOG_FATAL_IF(!outputLayer);
430 return outputLayer->getState().forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800431}
432
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700433void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800434 const auto outputLayer = findOutputLayerForDisplay(display);
435 LOG_FATAL_IF(!outputLayer);
436
437 if (!outputLayer->getState().hwc ||
438 (*outputLayer->getState().hwc).hwcCompositionType !=
439 Hwc2::IComposerClient::Composition::CURSOR) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800440 return;
441 }
442
443 // This gives us only the "orientation" component of the transform
Vishnu Nair33a6eee2019-02-06 13:48:06 -0800444 const State& s(getDrawingState());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800445
446 // Apply the layer's transform, followed by the display's global transform
447 // Here we're guaranteed that the layer's transform preserves rects
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800448 Rect win = getCroppedBufferSize(s);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800449 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700450 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800451 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700452 frame.intersect(display->getViewport(), &frame);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700453 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800454 auto position = displayTransform.transform(frame);
455
Dominik Laskowski7e045462018-05-30 13:02:02 -0700456 auto error =
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800457 (*outputLayer->getState().hwc).hwcLayer->setCursorPosition(position.left, position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800458 ALOGE_IF(error != HWC2::Error::None,
459 "[%s] Failed to set cursor position "
460 "to (%d, %d): %s (%d)",
461 mName.string(), position.left, position.top, to_string(error).c_str(),
462 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800463}
Riley Andrews03414a12014-07-01 14:22:59 -0700464
Mathias Agopian13127d82013-03-05 17:47:11 -0800465// ---------------------------------------------------------------------------
466// drawing...
467// ---------------------------------------------------------------------------
468
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000469bool Layer::prepareClientLayer(const RenderArea& renderArea, const Region& clip,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800470 Region& clearRegion, const bool supportProtectedContent,
471 renderengine::LayerSettings& layer) {
472 return prepareClientLayer(renderArea, clip, false, clearRegion, supportProtectedContent, layer);
Mathias Agopian13127d82013-03-05 17:47:11 -0800473}
474
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000475bool Layer::prepareClientLayer(const RenderArea& renderArea, bool useIdentityTransform,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800476 Region& clearRegion, const bool supportProtectedContent,
477 renderengine::LayerSettings& layer) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000478 return prepareClientLayer(renderArea, Region(renderArea.getBounds()), useIdentityTransform,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800479 clearRegion, supportProtectedContent, layer);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000480}
481
482bool Layer::prepareClientLayer(const RenderArea& /*renderArea*/, const Region& /*clip*/,
483 bool useIdentityTransform, Region& /*clearRegion*/,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800484 const bool /*supportProtectedContent*/,
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000485 renderengine::LayerSettings& layer) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800486 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000487 half alpha = getAlpha();
488 layer.geometry.boundaries = bounds;
489 if (useIdentityTransform) {
490 layer.geometry.positionTransform = mat4();
491 } else {
492 const ui::Transform transform = getTransform();
493 mat4 m;
494 m[0][0] = transform[0][0];
495 m[0][1] = transform[0][1];
496 m[0][3] = transform[0][2];
497 m[1][0] = transform[1][0];
498 m[1][1] = transform[1][1];
499 m[1][3] = transform[1][2];
500 m[3][0] = transform[2][0];
501 m[3][1] = transform[2][1];
502 m[3][3] = transform[2][2];
503 layer.geometry.positionTransform = m;
504 }
505
506 if (hasColorTransform()) {
507 layer.colorTransform = getColorTransform();
508 }
509
510 const auto roundedCornerState = getRoundedCornerState();
511 layer.geometry.roundedCornersRadius = roundedCornerState.radius;
512 layer.geometry.roundedCornersCrop = roundedCornerState.cropRect;
513
514 layer.alpha = alpha;
515 layer.sourceDataspace = mCurrentDataSpace;
516 return true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800517}
518
David Sodman41fdfc92017-11-06 16:09:56 -0800519void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
520 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800521 auto& engine(mFlinger->getRenderEngine());
Lloyd Pique0b785d82018-12-04 17:25:27 -0800522 computeGeometry(renderArea, getCompositionLayer()->editState().reMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700523 engine.setupFillWithColor(red, green, blue, alpha);
Lloyd Pique0b785d82018-12-04 17:25:27 -0800524 engine.drawMesh(getCompositionLayer()->getState().reMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800525}
526
David Sodmanc1498e62018-09-12 14:36:26 -0700527void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
528 clearWithOpenGL(renderArea, 0, 0, 0, 0);
529}
530
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800531void Layer::setCompositionType(const sp<const DisplayDevice>& display,
532 Hwc2::IComposerClient::Composition type) {
533 const auto outputLayer = findOutputLayerForDisplay(display);
534 LOG_FATAL_IF(!outputLayer);
535 LOG_FATAL_IF(!outputLayer->getState().hwc);
536 auto& compositionState = outputLayer->editState();
537
538 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", ((*compositionState.hwc).hwcLayer)->getId(),
539 toString(type).c_str(), 1);
540 if ((*compositionState.hwc).hwcCompositionType != type) {
David Sodman15094112018-10-11 09:39:37 -0700541 ALOGV(" actually setting");
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800542 (*compositionState.hwc).hwcCompositionType = type;
543
544 auto error = (*compositionState.hwc)
545 .hwcLayer->setCompositionType(static_cast<HWC2::Composition>(type));
546 ALOGE_IF(error != HWC2::Error::None,
547 "[%s] Failed to set "
548 "composition type %s: %s (%d)",
549 mName.string(), toString(type).c_str(), to_string(error).c_str(),
550 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800551 }
552}
553
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800554Hwc2::IComposerClient::Composition Layer::getCompositionType(
555 const sp<const DisplayDevice>& display) const {
556 const auto outputLayer = findOutputLayerForDisplay(display);
557 LOG_FATAL_IF(!outputLayer);
558 return outputLayer->getState().hwc ? (*outputLayer->getState().hwc).hwcCompositionType
559 : Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800560}
561
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800562bool Layer::getClearClientTarget(const sp<const DisplayDevice>& display) const {
563 const auto outputLayer = findOutputLayerForDisplay(display);
564 LOG_FATAL_IF(!outputLayer);
565 return outputLayer->getState().clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800566}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800567
Dan Stozacac35382016-01-27 12:21:06 -0800568bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
569 if (point->getFrameNumber() <= mCurrentFrameNumber) {
570 // Don't bother with a SyncPoint, since we've already latched the
571 // relevant frame
572 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700573 }
Robert Carr2e102c92018-10-23 12:11:15 -0700574 if (isRemovedFromCurrentState()) {
575 return false;
576 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700577
Dan Stozacac35382016-01-27 12:21:06 -0800578 Mutex::Autolock lock(mLocalSyncPointMutex);
579 mLocalSyncPoints.push_back(point);
580 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700581}
582
Mathias Agopian13127d82013-03-05 17:47:11 -0800583// ----------------------------------------------------------------------------
584// local state
585// ----------------------------------------------------------------------------
586
Peiyong Lin833074a2018-08-28 11:53:54 -0700587void Layer::computeGeometry(const RenderArea& renderArea,
588 renderengine::Mesh& mesh,
chaviwa76b2712017-09-20 12:02:26 -0700589 bool useIdentityTransform) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700590 const ui::Transform renderAreaTransform(renderArea.getTransform());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800591 FloatRect win = getBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700592
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000593 vec2 lt = vec2(win.left, win.top);
594 vec2 lb = vec2(win.left, win.bottom);
595 vec2 rb = vec2(win.right, win.bottom);
596 vec2 rt = vec2(win.right, win.top);
597
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800598 ui::Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000599 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700600 lt = layerTransform.transform(lt);
601 lb = layerTransform.transform(lb);
602 rb = layerTransform.transform(rb);
603 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000604 }
605
Peiyong Lin833074a2018-08-28 11:53:54 -0700606 renderengine::Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700607 position[0] = renderAreaTransform.transform(lt);
608 position[1] = renderAreaTransform.transform(lb);
609 position[2] = renderAreaTransform.transform(rb);
610 position[3] = renderAreaTransform.transform(rt);
Mathias Agopian13127d82013-03-05 17:47:11 -0800611}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800612
David Sodman41fdfc92017-11-06 16:09:56 -0800613bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800614 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700615 return (s.flags & layer_state_t::eLayerSecure);
616}
617
Mathias Agopian13127d82013-03-05 17:47:11 -0800618void Layer::setVisibleRegion(const Region& visibleRegion) {
619 // always called from main thread
620 this->visibleRegion = visibleRegion;
621}
622
623void Layer::setCoveredRegion(const Region& coveredRegion) {
624 // always called from main thread
625 this->coveredRegion = coveredRegion;
626}
627
David Sodman41fdfc92017-11-06 16:09:56 -0800628void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800629 // always called from main thread
630 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
631}
632
Robert Carre5f4f692018-01-12 13:12:28 -0800633void Layer::clearVisibilityRegions() {
634 visibleRegion.clear();
635 visibleNonTransparentRegion.clear();
636 coveredRegion.clear();
637}
638
Mathias Agopian13127d82013-03-05 17:47:11 -0800639// ----------------------------------------------------------------------------
640// transaction
641// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800642
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800643void Layer::pushPendingState() {
644 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700645 return;
646 }
647
Dan Stoza7dde5992015-05-22 09:51:44 -0700648 // If this transaction is waiting on the receipt of a frame, generate a sync
649 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700650 // We don't allow installing sync points after we are removed from the current state
651 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800652 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
653 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800654 if (barrierLayer == nullptr) {
655 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700656 // If we can't promote the layer we are intended to wait on,
657 // then it is expired or otherwise invalid. Allow this transaction
658 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800659 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800660 } else {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800661 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy);
Robert Carr0d480722017-01-10 16:42:54 -0800662 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800663 mRemoteSyncPoints.push_back(std::move(syncPoint));
664 } else {
665 // We already missed the frame we're supposed to synchronize
666 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800667 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800668 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700669 }
670
Dan Stoza7dde5992015-05-22 09:51:44 -0700671 // Wake us up to check if the frame has been received
672 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700673 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700674 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800675 mPendingStates.push_back(mCurrentState);
676 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700677}
678
Pablo Ceballos05289c22016-04-14 15:49:55 -0700679void Layer::popPendingState(State* stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800680 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700681
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800682 mPendingStates.removeAt(0);
683 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700684}
685
Pablo Ceballos05289c22016-04-14 15:49:55 -0700686bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700687 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800688 while (!mPendingStates.empty()) {
689 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700690 if (mRemoteSyncPoints.empty()) {
691 // If we don't have a sync point for this, apply it anyway. It
692 // will be visually wrong, but it should keep us from getting
693 // into too much trouble.
694 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700695 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700696 stateUpdateAvailable = true;
697 continue;
698 }
699
Marissa Wallf58c14b2018-07-24 10:50:43 -0700700 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800701 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800702 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800703
704 // Signal our end of the sync point and then dispose of it
705 mRemoteSyncPoints.front()->setTransactionApplied();
706 mRemoteSyncPoints.pop_front();
707 continue;
708 }
709
Dan Stoza7dde5992015-05-22 09:51:44 -0700710 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
711 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700712 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700713 stateUpdateAvailable = true;
714
715 // Signal our end of the sync point and then dispose of it
716 mRemoteSyncPoints.front()->setTransactionApplied();
717 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800718 } else {
719 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700720 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700721 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700722 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700723 stateUpdateAvailable = true;
724 }
725 }
726
727 // If we still have pending updates, wake SurfaceFlinger back up and point
728 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800729 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700730 setTransactionFlags(eTransactionNeeded);
731 mFlinger->setTransactionFlags(eTraversalNeeded);
732 }
733
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800734 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700735 return stateUpdateAvailable;
736}
737
Marissa Wall61c58622018-07-18 10:12:20 -0700738uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000739 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800740
Marissa Wall61c58622018-07-18 10:12:20 -0700741 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
742 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700743
David Sodmaneb085e02017-10-05 18:49:04 -0700744 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700745 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000746 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800747 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
748 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
749 " requested={ wh={%4u,%4u} }}\n"
750 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
751 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700752 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700753 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
754 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
755 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
756 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
757 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700758 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
759 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
760 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800761 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700762
Robert Carre392b552017-09-19 12:16:05 -0700763 // Don't let Layer::doTransaction update the drawing state
764 // if we have a pending resize, unless we are in fixed-size mode.
765 // the drawing state will be updated only once we receive a buffer
766 // with the correct size.
767 //
768 // In particular, we want to make sure the clip (which is part
769 // of the geometry state) is latched together with the size but is
770 // latched immediately when no resizing is involved.
771 //
772 // If a sideband stream is attached, however, we want to skip this
773 // optimization so that transactions aren't missed when a buffer
774 // never arrives
775 //
776 // In the case that we don't have a buffer we ignore other factors
777 // and avoid entering the resizePending state. At a high level the
778 // resizePending state is to avoid applying the state of the new buffer
779 // to the old buffer. However in the state where we don't have an old buffer
780 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700781 const bool resizePending =
782 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
783 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
Lloyd Pique0b785d82018-12-04 17:25:27 -0800784 (mActiveBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700785 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800786 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700787 flags |= eDontUpdateGeometryState;
788 }
789 }
790
Robert Carr7bf247e2017-05-18 14:02:49 -0700791 // Here we apply various requested geometry states, depending on our
792 // latching configuration. See Layer.h for a detailed discussion of
793 // how geometry latching is controlled.
794 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000795 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700796
797 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
798 // mode, which causes attributes which normally latch regardless of scaling mode,
799 // to be delayed. We copy the requested state to the active state making sure
800 // to respect these rules (again see Layer.h for a detailed discussion).
801 //
802 // There is an awkward asymmetry in the handling of the crop states in the position
803 // states, as can be seen below. Largely this arises from position and transform
804 // being stored in the same data structure while having different latching rules.
805 // b/38182305
806 //
Marissa Wall61c58622018-07-18 10:12:20 -0700807 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700808 // applyPendingStates in the presence of deferred transactions.
809 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -0700810 float tx = stateToCommit->active_legacy.transform.tx();
811 float ty = stateToCommit->active_legacy.transform.ty();
812 stateToCommit->active_legacy = stateToCommit->requested_legacy;
813 stateToCommit->active_legacy.transform.set(tx, ty);
814 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700815 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700816 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -0700817 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700818 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800819 }
820
Marissa Wall61c58622018-07-18 10:12:20 -0700821 return flags;
822}
823
824uint32_t Layer::doTransaction(uint32_t flags) {
825 ATRACE_CALL();
826
chaviw5aedec92018-10-22 10:40:38 -0700827 if (mLayerDetached) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800828 return flags;
829 }
830
831 if (mChildrenChanged) {
832 flags |= eVisibleRegion;
833 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -0700834 }
835
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800836 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +0000837 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -0700838 if (!applyPendingStates(&c)) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800839 return flags;
Marissa Wall61c58622018-07-18 10:12:20 -0700840 }
841
842 flags = doTransactionResize(flags, &c);
843
Alec Mourib416efd2018-09-06 21:01:59 +0000844 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700845
846 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800847 // invalidate and recompute the visible regions if needed
848 flags |= Layer::eVisibleRegion;
849 }
850
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700851 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800852 // invalidate and recompute the visible regions if needed
853 flags |= eVisibleRegion;
854 this->contentDirty = true;
855
856 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -0700857 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -0700858 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -0800859 }
860
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800861 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -0700862 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800863 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -0700864 }
865
Mathias Agopian13127d82013-03-05 17:47:11 -0800866 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -0700867 commitTransaction(c);
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800868 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -0800869 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800870}
871
Pablo Ceballos05289c22016-04-14 15:49:55 -0700872void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800873 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700874}
875
Mathias Agopian13127d82013-03-05 17:47:11 -0800876uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800877 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800878}
879
880uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800881 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -0800882}
883
Robert Carr82364e32016-05-15 11:27:47 -0700884bool Layer::setPosition(float x, float y, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800885 if (mCurrentState.requested_legacy.transform.tx() == x &&
886 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -0800887 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800888 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -0700889
890 // We update the requested and active position simultaneously because
891 // we want to apply the position portion of the transform matrix immediately,
892 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800893 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -0700894 if (immediate && !mFreezeGeometryUpdates) {
895 // Here we directly update the active state
896 // unlike other setters, because we store it within
897 // the transform, but use different latching rules.
898 // b/38182305
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800899 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -0700900 }
Robert Carr7bf247e2017-05-18 14:02:49 -0700901 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -0700902
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800903 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800904 setTransactionFlags(eTransactionNeeded);
905 return true;
906}
Robert Carr82364e32016-05-15 11:27:47 -0700907
Robert Carr1f0a16a2016-10-24 16:27:39 -0700908bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
909 ssize_t idx = mCurrentChildren.indexOf(childLayer);
910 if (idx < 0) {
911 return false;
912 }
913 if (childLayer->setLayer(z)) {
914 mCurrentChildren.removeAt(idx);
915 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800916 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700917 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800918 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700919}
920
Robert Carr503c7042017-09-27 15:06:08 -0700921bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
922 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
923 ssize_t idx = mCurrentChildren.indexOf(childLayer);
924 if (idx < 0) {
925 return false;
926 }
927 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
928 mCurrentChildren.removeAt(idx);
929 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800930 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700931 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800932 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700933}
934
Robert Carrae060832016-11-28 10:51:00 -0800935bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800936 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
937 mCurrentState.sequence++;
938 mCurrentState.z = z;
939 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700940
941 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800942 if (mCurrentState.zOrderRelativeOf != nullptr) {
943 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700944 if (strongRelative != nullptr) {
945 strongRelative->removeZOrderRelative(this);
946 }
chaviw606e5cf2019-03-01 10:12:10 -0800947 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -0700948 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800949 setTransactionFlags(eTransactionNeeded);
950 return true;
951}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700952
Robert Carrdb66e622017-04-10 16:55:57 -0700953void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800954 mCurrentState.zOrderRelatives.remove(relative);
955 mCurrentState.sequence++;
956 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700957 setTransactionFlags(eTransactionNeeded);
958}
959
960void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800961 mCurrentState.zOrderRelatives.add(relative);
962 mCurrentState.modified = true;
963 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -0700964 setTransactionFlags(eTransactionNeeded);
965}
966
chaviw606e5cf2019-03-01 10:12:10 -0800967void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
968 mCurrentState.zOrderRelativeOf = relativeOf;
969 mCurrentState.sequence++;
970 mCurrentState.modified = true;
971 setTransactionFlags(eTransactionNeeded);
972}
973
Robert Carr503d2bd2017-12-04 15:49:47 -0800974bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -0700975 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
976 if (handle == nullptr) {
977 return false;
978 }
979 sp<Layer> relative = handle->owner.promote();
980 if (relative == nullptr) {
981 return false;
982 }
983
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800984 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
985 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -0800986 return false;
987 }
988
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800989 mCurrentState.sequence++;
990 mCurrentState.modified = true;
991 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -0700992
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800993 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -0700994 if (oldZOrderRelativeOf != nullptr) {
995 oldZOrderRelativeOf->removeZOrderRelative(this);
996 }
chaviw606e5cf2019-03-01 10:12:10 -0800997 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -0700998 relative->addZOrderRelative(this);
999
1000 setTransactionFlags(eTransactionNeeded);
1001
1002 return true;
1003}
1004
Mathias Agopian13127d82013-03-05 17:47:11 -08001005bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001006 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001007 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001008 mCurrentState.requested_legacy.w = w;
1009 mCurrentState.requested_legacy.h = h;
1010 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001011 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001012
1013 // record the new size, from this point on, when the client request
1014 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001015 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001016 return true;
1017}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001018bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001019 if (mCurrentState.color.a == alpha) return false;
1020 mCurrentState.sequence++;
1021 mCurrentState.color.a = alpha;
1022 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001023 setTransactionFlags(eTransactionNeeded);
1024 return true;
1025}
chaviw13fdc492017-06-27 12:40:18 -07001026
Valerie Haudd0b7572019-01-29 14:59:27 -08001027bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1028 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001029 return false;
Valerie Hauaa194562019-02-05 16:21:38 -08001030 }
1031 mCurrentState.sequence++;
1032 mCurrentState.modified = true;
1033 setTransactionFlags(eTransactionNeeded);
1034
1035 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001036 // create background color layer if one does not yet exist
1037 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
1038 const String8& name = mName + "BackgroundColorLayer";
1039 mCurrentState.bgColorLayer =
1040 new ColorLayer(LayerCreationArgs(mFlinger.get(), nullptr, name, 0, 0, flags));
chaviw13fdc492017-06-27 12:40:18 -07001041
Valerie Haudd0b7572019-01-29 14:59:27 -08001042 // add to child list
1043 addChild(mCurrentState.bgColorLayer);
1044 mFlinger->mLayersAdded = true;
1045 // set up SF to handle added color layer
1046 if (isRemovedFromCurrentState()) {
1047 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1048 }
1049 mFlinger->setTransactionFlags(eTransactionNeeded);
1050 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1051 mCurrentState.bgColorLayer->reparent(nullptr);
1052 mCurrentState.bgColorLayer = nullptr;
1053 return true;
1054 }
1055
1056 mCurrentState.bgColorLayer->setColor(color);
1057 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1058 mCurrentState.bgColorLayer->setAlpha(alpha);
1059 mCurrentState.bgColorLayer->setDataspace(dataspace);
1060
chaviw13fdc492017-06-27 12:40:18 -07001061 return true;
1062}
1063
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001064bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001065 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001066
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001067 mCurrentState.sequence++;
1068 mCurrentState.cornerRadius = cornerRadius;
1069 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001070 setTransactionFlags(eTransactionNeeded);
1071 return true;
1072}
1073
Robert Carrd4ae7f32018-06-07 16:10:57 -07001074bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1075 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001076 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001077 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1078
1079 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1080 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1081 return false;
1082 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001083 mCurrentState.sequence++;
1084 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1085 matrix.dsdy);
1086 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001087 setTransactionFlags(eTransactionNeeded);
1088 return true;
1089}
Marissa Wall61c58622018-07-18 10:12:20 -07001090
Mathias Agopian13127d82013-03-05 17:47:11 -08001091bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001092 mCurrentState.requestedTransparentRegion_legacy = transparent;
1093 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001094 setTransactionFlags(eTransactionNeeded);
1095 return true;
1096}
1097bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001098 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1099 if (mCurrentState.flags == newFlags) return false;
1100 mCurrentState.sequence++;
1101 mCurrentState.flags = newFlags;
1102 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001103 setTransactionFlags(eTransactionNeeded);
1104 return true;
1105}
Robert Carr99e27f02016-06-16 15:18:02 -07001106
Marissa Wallf58c14b2018-07-24 10:50:43 -07001107bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001108 if (mCurrentState.requestedCrop_legacy == crop) return false;
1109 mCurrentState.sequence++;
1110 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001111 if (immediate && !mFreezeGeometryUpdates) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001112 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001113 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001114 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1115
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001116 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001117 setTransactionFlags(eTransactionNeeded);
1118 return true;
1119}
Robert Carr8d5227b2017-03-16 15:41:03 -07001120
Robert Carrc3574f72016-03-24 12:19:32 -07001121bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001122 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001123 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001124 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001125 return true;
1126}
1127
Evan Roskyef876c92019-01-25 17:46:06 -08001128bool Layer::setMetadata(const LayerMetadata& data) {
1129 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001130 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001131 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001132 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001133 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001134}
1135
Mathias Agopian13127d82013-03-05 17:47:11 -08001136bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001137 if (mCurrentState.layerStack == layerStack) return false;
1138 mCurrentState.sequence++;
1139 mCurrentState.layerStack = layerStack;
1140 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001141 setTransactionFlags(eTransactionNeeded);
1142 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001143}
1144
Peiyong Linc502cb72019-03-01 15:00:23 -08001145bool Layer::setColorSpaceAgnostic(const bool agnostic) {
1146 if (mCurrentState.colorSpaceAgnostic == agnostic) {
1147 return false;
1148 }
1149 mCurrentState.sequence++;
1150 mCurrentState.colorSpaceAgnostic = agnostic;
1151 mCurrentState.modified = true;
1152 setTransactionFlags(eTransactionNeeded);
1153 return true;
1154}
1155
Robert Carr1f0a16a2016-10-24 16:27:39 -07001156uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001157 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001158 if (p == nullptr) {
1159 return getDrawingState().layerStack;
1160 }
1161 return p->getLayerStack();
1162}
1163
Marissa Wallf58c14b2018-07-24 10:50:43 -07001164void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001165 mCurrentState.barrierLayer_legacy = barrierLayer;
1166 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001167 // We don't set eTransactionNeeded, because just receiving a deferral
1168 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001169 mCurrentState.modified = true;
1170 pushPendingState();
1171 mCurrentState.barrierLayer_legacy = nullptr;
1172 mCurrentState.frameNumber_legacy = 0;
1173 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001174}
1175
Marissa Wallf58c14b2018-07-24 10:50:43 -07001176void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001177 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001178 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001179}
1180
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001181// ----------------------------------------------------------------------------
1182// pageflip handling...
1183// ----------------------------------------------------------------------------
1184
Robert Carr1f0a16a2016-10-24 16:27:39 -07001185bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001186 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001187 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001188 if (parent != nullptr && parent->isHiddenByPolicy()) {
1189 return true;
1190 }
1191 return s.flags & layer_state_t::eLayerHidden;
1192}
1193
David Sodman41fdfc92017-11-06 16:09:56 -08001194uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001195 // TODO: should we do something special if mSecure is set?
1196 if (mProtectedByApp) {
1197 // need a hardware-protected path to external video sink
1198 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001199 }
Riley Andrews03414a12014-07-01 14:22:59 -07001200 if (mPotentialCursor) {
1201 usage |= GraphicBuffer::USAGE_CURSOR;
1202 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001203 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001204 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001205}
1206
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001207void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001208 uint32_t orientation = 0;
Vishnu Nairf2deb822018-11-12 17:53:48 -08001209 // Disable setting transform hint if the debug flag is set or if the
1210 // getTransformToDisplayInverse flag is set and the client wants to submit buffers
1211 // in one orientation.
1212 if (!mFlinger->mDebugDisableTransformHint && !getTransformToDisplayInverse()) {
Mathias Agopian84300952012-11-21 16:02:13 -08001213 // The transform hint is used to improve performance, but we can
1214 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001215 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001216 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001217 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001218 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001219 orientation = 0;
1220 }
1221 }
David Sodmaneb085e02017-10-05 18:49:04 -07001222 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001223}
1224
Mathias Agopian13127d82013-03-05 17:47:11 -08001225// ----------------------------------------------------------------------------
1226// debugging
1227// ----------------------------------------------------------------------------
1228
Marissa Wall61c58622018-07-18 10:12:20 -07001229// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001230LayerDebugInfo Layer::getLayerDebugInfo() const {
1231 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001232 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001233 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001234 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001235 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
Yiwei Zhang5434a782018-12-05 18:06:32 -08001236 info.mType = std::string(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001237 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001238 info.mVisibleRegion = visibleRegion;
1239 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001240 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001241 info.mX = ds.active_legacy.transform.tx();
1242 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001243 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001244 info.mWidth = ds.active_legacy.w;
1245 info.mHeight = ds.active_legacy.h;
1246 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001247 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001248 info.mFlags = ds.flags;
1249 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001250 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001251 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1252 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1253 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1254 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001255 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001256 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001257 if (buffer != 0) {
1258 info.mActiveBufferWidth = buffer->getWidth();
1259 info.mActiveBufferHeight = buffer->getHeight();
1260 info.mActiveBufferStride = buffer->getStride();
1261 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001262 } else {
1263 info.mActiveBufferWidth = 0;
1264 info.mActiveBufferHeight = 0;
1265 info.mActiveBufferStride = 0;
1266 info.mActiveBufferFormat = 0;
1267 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001268 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001269 info.mNumQueuedFrames = getQueuedFrameCount();
1270 info.mRefreshPending = isBufferLatched();
1271 info.mIsOpaque = isOpaque(ds);
1272 info.mContentDirty = contentDirty;
1273 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001274}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001275
Yiwei Zhang5434a782018-12-05 18:06:32 -08001276void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001277 result.append("-------------------------------");
1278 result.append("-------------------------------");
1279 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001280 result.append(" Layer name\n");
1281 result.append(" Z | ");
1282 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001283 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001284 result.append(" Disp Frame (LTRB) | ");
1285 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001286 result.append("-------------------------------");
1287 result.append("-------------------------------");
1288 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001289}
1290
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001291void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1292 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1293 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001294 return;
1295 }
1296
Yiwei Zhang5434a782018-12-05 18:06:32 -08001297 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001298 if (mName.length() > 77) {
1299 std::string shortened;
1300 shortened.append(mName.string(), 36);
1301 shortened.append("[...]");
1302 shortened.append(mName.string() + (mName.length() - 36), 36);
Yiwei Zhang5434a782018-12-05 18:06:32 -08001303 name = shortened;
Dan Stozae22aec72016-08-01 13:20:59 -07001304 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001305 name = std::string(mName.string(), mName.size());
Dan Stozae22aec72016-08-01 13:20:59 -07001306 }
1307
Yiwei Zhang5434a782018-12-05 18:06:32 -08001308 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001309
Alec Mourib416efd2018-09-06 21:01:59 +00001310 const State& layerState(getDrawingState());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001311 const auto& compositionState = outputLayer->getState();
1312
Chia-I Wu1e043612018-03-01 09:45:09 -08001313 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001314 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001315 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001316 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001317 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001318 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
1319 StringAppendF(&result, "%10s | ",
1320 toString(getCompositionLayer() ? compositionState.bufferTransform
1321 : static_cast<Hwc2::Transform>(0))
1322 .c_str());
1323 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001324 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001325 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001326 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1327 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001328
Yichi Chen6ca35192018-05-29 12:20:43 +08001329 result.append("- - - - - - - - - - - - - - - -");
1330 result.append("- - - - - - - - - - - - - - - -");
1331 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001332}
Dan Stozae22aec72016-08-01 13:20:59 -07001333
Yiwei Zhang5434a782018-12-05 18:06:32 -08001334void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001335 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001336}
1337
Svetoslavd85084b2014-03-20 10:28:31 -07001338void Layer::clearFrameStats() {
1339 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001340}
1341
Jamie Gennis6547ff42013-07-16 20:12:42 -07001342void Layer::logFrameStats() {
1343 mFrameTracker.logAndResetStats(mName);
1344}
1345
Svetoslavd85084b2014-03-20 10:28:31 -07001346void Layer::getFrameStats(FrameStats* outStats) const {
1347 mFrameTracker.getStats(outStats);
1348}
1349
Yiwei Zhang5434a782018-12-05 18:06:32 -08001350void Layer::dumpFrameEvents(std::string& result) {
1351 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001352 Mutex::Autolock lock(mFrameEventHistoryMutex);
1353 mFrameEventHistory.checkFencesForCompletion();
1354 mFrameEventHistory.dump(result);
1355}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001356
Brian Anderson5ea5e592016-12-01 16:54:33 -08001357void Layer::onDisconnect() {
1358 Mutex::Autolock lock(mFrameEventHistoryMutex);
1359 mFrameEventHistory.onDisconnect();
Yiwei Zhangaf8ee942018-11-22 00:15:23 -08001360 mFlinger->mTimeStats->onDestroy(getSequence());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001361}
1362
Brian Anderson3890c392016-07-25 12:48:08 -07001363void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001364 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001365 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001366 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1367 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001368 }
1369
Brian Andersond6927fb2016-07-23 23:37:30 -07001370 Mutex::Autolock lock(mFrameEventHistoryMutex);
1371 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001372 // If there are any unsignaled fences in the aquire timeline at this
1373 // point, the previously queued frame hasn't been latched yet. Go ahead
1374 // and try to get the signal time here so the syscall is taken out of
1375 // the main thread's critical path.
1376 mAcquireTimeline.updateSignalTimes();
1377 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001378 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001379 mFrameEventHistory.addQueue(*newTimestamps);
1380 }
1381
Brian Anderson3890c392016-07-25 12:48:08 -07001382 if (outDelta) {
1383 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001384 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001385}
Dan Stozae77c7662016-05-13 11:37:28 -07001386
Chia-I Wu98f1c102017-05-30 14:54:08 -07001387size_t Layer::getChildrenCount() const {
1388 size_t count = 0;
1389 for (const sp<Layer>& child : mCurrentChildren) {
1390 count += 1 + child->getChildrenCount();
1391 }
1392 return count;
1393}
1394
Robert Carr1f0a16a2016-10-24 16:27:39 -07001395void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001396 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001397 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001398
Robert Carr1f0a16a2016-10-24 16:27:39 -07001399 mCurrentChildren.add(layer);
1400 layer->setParent(this);
1401}
1402
1403ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001404 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001405 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001406
Robert Carr1323c952019-01-28 18:13:27 -08001407 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001408 return mCurrentChildren.remove(layer);
1409}
1410
Robert Carr1db73f62016-12-21 12:58:51 -08001411bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1412 sp<Handle> handle = nullptr;
1413 sp<Layer> newParent = nullptr;
1414 if (newParentHandle == nullptr) {
1415 return false;
1416 }
1417 handle = static_cast<Handle*>(newParentHandle.get());
1418 newParent = handle->owner.promote();
1419 if (newParent == nullptr) {
1420 ALOGE("Unable to promote Layer handle");
1421 return false;
1422 }
1423
chaviw5aedec92018-10-22 10:40:38 -07001424 if (attachChildren()) {
1425 setTransactionFlags(eTransactionNeeded);
1426 }
Robert Carr1db73f62016-12-21 12:58:51 -08001427 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001428 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001429 }
1430 mCurrentChildren.clear();
1431
1432 return true;
1433}
1434
Robert Carr15eae092018-03-23 13:43:53 -07001435void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001436 for (const sp<Layer>& child : mDrawingChildren) {
1437 child->mDrawingParent = newParent;
Vishnu Nair4351ad52019-02-11 14:13:02 -08001438 child->computeBounds(newParent->mBounds, newParent->getTransformWithScale());
Robert Carr578038f2018-03-09 12:25:24 -08001439 }
1440}
1441
chaviwf1961f72017-09-18 16:41:07 -07001442bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001443 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001444
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001445 // While layers are detached, we allow most operations
1446 // and simply halt performing the actual transaction. However
1447 // for reparent != null we would enter the mRemovedFromCurrentState
1448 // state, regardless of whether doTransaction was called, and
1449 // so we need to prevent the update here.
1450 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001451 return false;
1452 }
1453
Robert Carr54cf5b12019-01-25 14:02:28 -08001454 sp<Layer> newParent;
1455 if (newParentHandle != nullptr) {
1456 auto handle = static_cast<Handle*>(newParentHandle.get());
1457 newParent = handle->owner.promote();
1458 if (newParent == nullptr) {
1459 ALOGE("Unable to promote Layer handle");
1460 return false;
1461 }
1462 if (newParent == this) {
1463 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1464 return false;
1465 }
1466 }
1467
chaviwf1961f72017-09-18 16:41:07 -07001468 sp<Layer> parent = getParent();
1469 if (parent != nullptr) {
1470 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001471 }
1472
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001473 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001474 newParent->addChild(this);
1475 if (!newParent->isRemovedFromCurrentState()) {
1476 addToCurrentState();
1477 } else {
1478 onRemovedFromCurrentState();
1479 }
1480
1481 if (mLayerDetached) {
1482 mLayerDetached = false;
1483 callSetTransactionFlags = true;
1484 }
1485 } else {
1486 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001487 }
1488
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001489 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001490 setTransactionFlags(eTransactionNeeded);
1491 }
chaviw06178942017-07-27 10:25:59 -07001492 return true;
1493}
1494
Robert Carr9524cb32017-02-13 11:32:32 -08001495bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001496 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001497 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001498 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001499 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001500 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001501 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001502 }
Robert Carr7f619b22017-11-06 12:56:35 -08001503 }
Robert Carr9524cb32017-02-13 11:32:32 -08001504
1505 return true;
1506}
1507
chaviw5aedec92018-10-22 10:40:38 -07001508bool Layer::attachChildren() {
1509 bool changed = false;
1510 for (const sp<Layer>& child : mCurrentChildren) {
1511 sp<Client> parentClient = mClientRef.promote();
1512 sp<Client> client(child->mClientRef.promote());
1513 if (client != nullptr && parentClient != client) {
1514 if (child->mLayerDetached) {
1515 child->mLayerDetached = false;
1516 changed = true;
1517 }
1518 changed |= child->attachChildren();
1519 }
1520 }
1521
1522 return changed;
1523}
1524
Peiyong Lind3788632018-09-18 16:01:31 -07001525bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001526 static const mat4 identityMatrix = mat4();
1527
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001528 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001529 return false;
1530 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001531 ++mCurrentState.sequence;
1532 mCurrentState.colorTransform = matrix;
1533 mCurrentState.hasColorTransform = matrix != identityMatrix;
1534 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001535 setTransactionFlags(eTransactionNeeded);
1536 return true;
1537}
1538
chaviwf66724d2018-11-28 16:35:21 -08001539mat4 Layer::getColorTransform() const {
1540 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1541 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1542 colorTransform = parent->getColorTransform() * colorTransform;
1543 }
1544 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001545}
1546
1547bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001548 bool hasColorTransform = getDrawingState().hasColorTransform;
1549 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1550 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1551 }
1552 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001553}
1554
Chia-I Wu11481472018-05-04 10:43:19 -07001555bool Layer::isLegacyDataSpace() const {
1556 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001557 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001558 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001559}
1560
Robert Carr1f0a16a2016-10-24 16:27:39 -07001561void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001562 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001563}
1564
Robert Carr1f0a16a2016-10-24 16:27:39 -07001565int32_t Layer::getZ() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001566 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001567}
1568
Robert Carr29abff82017-12-04 13:51:20 -08001569bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1570 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001571 const State& state = useDrawing ? mDrawingState : mCurrentState;
Robert Carr29abff82017-12-04 13:51:20 -08001572 return state.zOrderRelativeOf != nullptr;
1573}
1574
David Sodman41fdfc92017-11-06 16:09:56 -08001575__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001576 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001577 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1578 "makeTraversalList received invalid stateSet");
1579 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1580 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001581 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001582
Robert Carr29abff82017-12-04 13:51:20 -08001583 if (state.zOrderRelatives.size() == 0) {
1584 *outSkipRelativeZUsers = true;
1585 return children;
1586 }
1587
chaviwfd462612018-05-31 16:11:27 -07001588 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001589 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001590 sp<Layer> strongRelative = weakRelative.promote();
1591 if (strongRelative != nullptr) {
1592 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001593 }
1594 }
1595
Dan Stoza412903f2017-04-27 13:42:17 -07001596 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001597 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
Robert Carr503c7042017-09-27 15:06:08 -07001598 if (childState.zOrderRelativeOf != nullptr) {
1599 continue;
1600 }
Robert Carrdb66e622017-04-10 16:55:57 -07001601 traverse.add(child);
1602 }
1603
1604 return traverse;
1605}
1606
Robert Carr1f0a16a2016-10-24 16:27:39 -07001607/**
Robert Carrdb66e622017-04-10 16:55:57 -07001608 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001609 */
Dan Stoza412903f2017-04-27 13:42:17 -07001610void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001611 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1612 // produce a new list for traversing, including our relatives, and not including our children
1613 // who are relatives of another surface. In the case that there are no relative Z,
1614 // makeTraversalList returns our children directly to avoid significant overhead.
1615 // However in this case we need to take the responsibility for filtering children which
1616 // are relatives of another surface here.
1617 bool skipRelativeZUsers = false;
1618 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001619
Robert Carr1f0a16a2016-10-24 16:27:39 -07001620 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001621 for (; i < list.size(); i++) {
1622 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001623 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1624 continue;
1625 }
1626
Robert Carrdb66e622017-04-10 16:55:57 -07001627 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001628 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001629 }
Dan Stoza412903f2017-04-27 13:42:17 -07001630 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001631 }
Robert Carr29abff82017-12-04 13:51:20 -08001632
Dan Stoza412903f2017-04-27 13:42:17 -07001633 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001634 for (; i < list.size(); i++) {
1635 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001636
Robert Carr29abff82017-12-04 13:51:20 -08001637 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1638 continue;
1639 }
Dan Stoza412903f2017-04-27 13:42:17 -07001640 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001641 }
1642}
1643
1644/**
Robert Carrdb66e622017-04-10 16:55:57 -07001645 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001646 */
Dan Stoza412903f2017-04-27 13:42:17 -07001647void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1648 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001649 // See traverseInZOrder for documentation.
1650 bool skipRelativeZUsers = false;
1651 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001652
Robert Carr1f0a16a2016-10-24 16:27:39 -07001653 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001654 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001655 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001656
1657 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1658 continue;
1659 }
1660
Robert Carrdb66e622017-04-10 16:55:57 -07001661 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001662 break;
1663 }
Dan Stoza412903f2017-04-27 13:42:17 -07001664 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001665 }
Dan Stoza412903f2017-04-27 13:42:17 -07001666 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001667 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001668 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001669
1670 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1671 continue;
1672 }
1673
Dan Stoza412903f2017-04-27 13:42:17 -07001674 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001675 }
1676}
1677
chaviw4b129c22018-04-09 16:19:43 -07001678LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1679 const std::vector<Layer*>& layersInTree) {
1680 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1681 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001682 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1683 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001684 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001685
chaviwfd462612018-05-31 16:11:27 -07001686 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001687 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1688 sp<Layer> strongRelative = weakRelative.promote();
1689 // Only add relative layers that are also descendents of the top most parent of the tree.
1690 // If a relative layer is not a descendent, then it should be ignored.
1691 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1692 traverse.add(strongRelative);
1693 }
1694 }
1695
1696 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001697 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001698 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1699 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1700 // the child here since it won't be added later as a relative.
1701 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1702 childState.zOrderRelativeOf.promote().get())) {
1703 continue;
1704 }
1705 traverse.add(child);
1706 }
1707
1708 return traverse;
1709}
1710
1711void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1712 LayerVector::StateSet stateSet,
1713 const LayerVector::Visitor& visitor) {
1714 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001715
1716 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001717 for (; i < list.size(); i++) {
1718 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001719 if (relative->getZ() >= 0) {
1720 break;
1721 }
chaviw4b129c22018-04-09 16:19:43 -07001722 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001723 }
chaviw4b129c22018-04-09 16:19:43 -07001724
chaviwa76b2712017-09-20 12:02:26 -07001725 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001726 for (; i < list.size(); i++) {
1727 const auto& relative = list[i];
1728 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001729 }
1730}
1731
chaviw4b129c22018-04-09 16:19:43 -07001732std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1733 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1734 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1735
1736 std::vector<Layer*> layersInTree = {this};
1737 for (size_t i = 0; i < children.size(); i++) {
1738 const auto& child = children[i];
1739 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1740 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1741 }
1742
1743 return layersInTree;
1744}
1745
1746void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1747 const LayerVector::Visitor& visitor) {
1748 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1749 std::sort(layersInTree.begin(), layersInTree.end());
1750 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1751}
1752
Peiyong Linefefaac2018-08-17 12:27:51 -07001753ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001754 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001755}
1756
chaviw13fdc492017-06-27 12:40:18 -07001757half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001758 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001759
chaviw13fdc492017-06-27 12:40:18 -07001760 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1761 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001762}
Robert Carr6452f122017-03-21 10:41:29 -07001763
chaviw13fdc492017-06-27 12:40:18 -07001764half4 Layer::getColor() const {
1765 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001766 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001767}
Robert Carr6452f122017-03-21 10:41:29 -07001768
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001769Layer::RoundedCornerState Layer::getRoundedCornerState() const {
1770 const auto& p = mDrawingParent.promote();
1771 if (p != nullptr) {
1772 RoundedCornerState parentState = p->getRoundedCornerState();
1773 if (parentState.radius > 0) {
1774 ui::Transform t = getActiveTransform(getDrawingState());
1775 t = t.inverse();
1776 parentState.cropRect = t.transform(parentState.cropRect);
1777 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1778 // but a transform matrix can define horizontal and vertical scales.
1779 // Let's take the average between both of them and pass into the shader, practically we
1780 // never do this type of transformation on windows anyway.
1781 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
1782 return parentState;
1783 }
1784 }
1785 const float radius = getDrawingState().cornerRadius;
Vishnu Nair4351ad52019-02-11 14:13:02 -08001786 return radius > 0 ? RoundedCornerState(getBounds(), radius) : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001787}
1788
Robert Carr1f0a16a2016-10-24 16:27:39 -07001789void Layer::commitChildList() {
1790 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1791 const auto& child = mCurrentChildren[i];
1792 child->commitChildList();
1793 }
1794 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001795 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001796}
1797
Robert Carr720e5062018-07-30 17:45:14 -07001798void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001799 mCurrentState.inputInfo = info;
1800 mCurrentState.modified = true;
1801 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07001802 setTransactionFlags(eTransactionNeeded);
1803}
1804
chaviw1d044282017-09-27 12:19:28 -07001805void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
1806 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1807 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001808 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07001809
Peiyong Linefefaac2018-08-17 12:27:51 -07001810 ui::Transform requestedTransform = state.active_legacy.transform;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001811 ui::Transform transform = getTransform();
chaviw1d044282017-09-27 12:19:28 -07001812
1813 layerInfo->set_id(sequence);
1814 layerInfo->set_name(getName().c_str());
1815 layerInfo->set_type(String8(getTypeId()));
1816
1817 for (const auto& child : children) {
1818 layerInfo->add_children(child->sequence);
1819 }
1820
1821 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1822 sp<Layer> strongRelative = weakRelative.promote();
1823 if (strongRelative != nullptr) {
1824 layerInfo->add_relatives(strongRelative->sequence);
1825 }
1826 }
1827
Marissa Wallf58c14b2018-07-24 10:50:43 -07001828 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
Nataniel Borges797b0e42019-02-15 14:11:58 -08001829 [&]() { return layerInfo->mutable_transparent_region(); });
1830 LayerProtoHelper::writeToProto(visibleRegion,
1831 [&]() { return layerInfo->mutable_visible_region(); });
1832 LayerProtoHelper::writeToProto(surfaceDamageRegion,
1833 [&]() { return layerInfo->mutable_damage_region(); });
chaviw1d044282017-09-27 12:19:28 -07001834
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001835 layerInfo->set_layer_stack(getLayerStack());
chaviw1d044282017-09-27 12:19:28 -07001836 layerInfo->set_z(state.z);
1837
Nataniel Borges797b0e42019-02-15 14:11:58 -08001838 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
1839 [&]() { return layerInfo->mutable_position(); });
chaviw1d044282017-09-27 12:19:28 -07001840
Nataniel Borges797b0e42019-02-15 14:11:58 -08001841 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(), [&]() {
1842 return layerInfo->mutable_requested_position();
1843 });
chaviw1d044282017-09-27 12:19:28 -07001844
Nataniel Borges797b0e42019-02-15 14:11:58 -08001845 LayerProtoHelper::writeSizeToProto(state.active_legacy.w, state.active_legacy.h,
1846 [&]() { return layerInfo->mutable_size(); });
chaviw1d044282017-09-27 12:19:28 -07001847
Nataniel Borges797b0e42019-02-15 14:11:58 -08001848 LayerProtoHelper::writeToProto(state.crop_legacy, [&]() { return layerInfo->mutable_crop(); });
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001849 layerInfo->set_corner_radius(getRoundedCornerState().radius);
chaviw1d044282017-09-27 12:19:28 -07001850
1851 layerInfo->set_is_opaque(isOpaque(state));
1852 layerInfo->set_invalidate(contentDirty);
Chia-I Wu01591c92018-05-22 12:03:00 -07001853
1854 // XXX (b/79210409) mCurrentDataSpace is not protected
1855 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
1856
chaviw1d044282017-09-27 12:19:28 -07001857 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
Nataniel Borges797b0e42019-02-15 14:11:58 -08001858 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
1859 LayerProtoHelper::writeToProto(state.color,
1860 [&]() { return layerInfo->mutable_requested_color(); });
chaviw1d044282017-09-27 12:19:28 -07001861 layerInfo->set_flags(state.flags);
1862
1863 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1864 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
1865
Jorim Jaggi8e0af362017-11-14 16:28:28 +01001866 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07001867 if (parent != nullptr) {
1868 layerInfo->set_parent(parent->sequence);
Nataniel Borgesdcc0bab2019-02-15 13:22:03 -08001869 } else {
1870 layerInfo->set_parent(-1);
chaviw1d044282017-09-27 12:19:28 -07001871 }
1872
1873 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1874 if (zOrderRelativeOf != nullptr) {
1875 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
Nataniel Borgesdcc0bab2019-02-15 13:22:03 -08001876 } else {
1877 layerInfo->set_z_order_relative_of(-1);
chaviw1d044282017-09-27 12:19:28 -07001878 }
1879
Lloyd Pique0b785d82018-12-04 17:25:27 -08001880 auto buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001881 if (buffer != nullptr) {
Nataniel Borges797b0e42019-02-15 14:11:58 -08001882 LayerProtoHelper::writeToProto(buffer,
1883 [&]() { return layerInfo->mutable_active_buffer(); });
Peiyong Linefefaac2018-08-17 12:27:51 -07001884 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
Yichi Chen6ca35192018-05-29 12:20:43 +08001885 layerInfo->mutable_buffer_transform());
chaviw1d044282017-09-27 12:19:28 -07001886 }
1887
1888 layerInfo->set_queued_frames(getQueuedFrameCount());
1889 layerInfo->set_refresh_pending(isBufferLatched());
chaviwadc40c22018-07-10 16:57:27 -07001890 layerInfo->set_curr_frame(mCurrentFrameNumber);
Vishnu Nairf2deb822018-11-12 17:53:48 -08001891 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
chaviwadc40c22018-07-10 16:57:27 -07001892
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001893 for (const auto& pendingState : mPendingStates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001894 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
chaviwadc40c22018-07-10 16:57:27 -07001895 if (barrierLayer != nullptr) {
1896 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
1897 barrierLayerProto->set_id(barrierLayer->sequence);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001898 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
chaviwadc40c22018-07-10 16:57:27 -07001899 }
1900 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001901
1902 auto protoMap = layerInfo->mutable_metadata();
1903 for (const auto& entry : state.metadata.mMap) {
1904 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
1905 }
Vishnu Nair4351ad52019-02-11 14:13:02 -08001906 LayerProtoHelper::writeToProto(mEffectiveTransform, layerInfo->mutable_effective_transform());
Nataniel Borges797b0e42019-02-15 14:11:58 -08001907 LayerProtoHelper::writeToProto(mSourceBounds,
1908 [&]() { return layerInfo->mutable_source_bounds(); });
1909 LayerProtoHelper::writeToProto(mScreenBounds,
1910 [&]() { return layerInfo->mutable_screen_bounds(); });
1911 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
chaviw1d044282017-09-27 12:19:28 -07001912}
1913
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001914void Layer::writeToProto(LayerProto* layerInfo, const sp<DisplayDevice>& displayDevice) {
1915 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1916 if (!outputLayer) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07001917 return;
1918 }
1919
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001920 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
1921
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001922 const auto& compositionState = outputLayer->getState();
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001923
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001924 const Rect& frame = compositionState.displayFrame;
Nataniel Borges797b0e42019-02-15 14:11:58 -08001925 LayerProtoHelper::writeToProto(frame, [&]() { return layerInfo->mutable_hwc_frame(); });
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001926
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001927 const FloatRect& crop = compositionState.sourceCrop;
Nataniel Borges797b0e42019-02-15 14:11:58 -08001928 LayerProtoHelper::writeToProto(crop, [&]() { return layerInfo->mutable_hwc_crop(); });
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001929
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001930 const int32_t transform =
1931 getCompositionLayer() ? static_cast<int32_t>(compositionState.bufferTransform) : 0;
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001932 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08001933
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001934 const int32_t compositionType =
1935 static_cast<int32_t>(compositionState.hwc ? (*compositionState.hwc).hwcCompositionType
1936 : Hwc2::IComposerClient::Composition::CLIENT);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08001937 layerInfo->set_hwc_composition_type(compositionType);
1938
1939 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
1940 static_cast<BufferLayer*>(this)->isProtected()) {
1941 layerInfo->set_is_protected(true);
1942 } else {
1943 layerInfo->set_is_protected(false);
1944 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001945}
1946
Robert Carr2e102c92018-10-23 12:11:15 -07001947bool Layer::isRemovedFromCurrentState() const {
1948 return mRemovedFromCurrentState;
1949}
1950
Arthur Hungd20b2702019-01-14 18:16:16 +08001951InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001952 InputWindowInfo info = mDrawingState.inputInfo;
Robert Carr720e5062018-07-30 17:45:14 -07001953
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001954 if (info.displayId == ADISPLAY_ID_NONE) {
1955 info.displayId = mDrawingState.layerStack;
1956 }
1957
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001958 ui::Transform t = getTransform();
1959 const float xScale = t.sx();
1960 const float yScale = t.sy();
1961 if (xScale != 1.0f || yScale != 1.0f) {
1962 info.windowXScale *= 1.0f / xScale;
1963 info.windowYScale *= 1.0f / yScale;
1964 info.touchableRegion.scaleSelf(xScale, yScale);
Riddle Hsu39d4aa52018-11-30 20:46:53 +08001965 }
Robert Carre07e1032018-11-26 12:55:53 -08001966
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001967 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001968 // If this is a portal window, set the touchableRegion to the layerBounds.
1969 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
1970 ? getBufferSize(getDrawingState())
1971 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08001972 if (!layerBounds.isValid()) {
1973 layerBounds = getCroppedBufferSize(getDrawingState());
1974 }
Vishnu Nair8033a492018-12-05 07:27:23 -08001975 layerBounds = t.transform(layerBounds);
1976 layerBounds.inset(info.surfaceInset, info.surfaceInset, info.surfaceInset, info.surfaceInset);
1977
Arthur Hungd20b2702019-01-14 18:16:16 +08001978 // Input coordinate should match the layer bounds.
1979 info.frameLeft = layerBounds.left;
1980 info.frameTop = layerBounds.top;
1981 info.frameRight = layerBounds.right;
1982 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08001983
1984 // Position the touchable region relative to frame screen location and restrict it to frame
1985 // bounds.
1986 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08001987 info.visible = canReceiveInput();
Robert Carr720e5062018-07-30 17:45:14 -07001988 return info;
1989}
1990
1991bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001992 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07001993}
1994
Lloyd Piquefeb73d72018-12-04 17:23:44 -08001995std::shared_ptr<compositionengine::Layer> Layer::getCompositionLayer() const {
1996 return nullptr;
1997}
1998
chaviw3e727cd2019-01-31 13:41:05 -08001999bool Layer::canReceiveInput() const {
2000 return isVisible();
2001}
2002
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002003compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
2004 const sp<const DisplayDevice>& display) const {
2005 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionLayer().get());
2006}
2007
Mathias Agopian13127d82013-03-05 17:47:11 -08002008// ---------------------------------------------------------------------------
2009
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002010}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002011
2012#if defined(__gl_h_)
2013#error "don't include gl/gl.h in this file"
2014#endif
2015
2016#if defined(__gl2_h_)
2017#error "don't include gl2/gl2.h in this file"
2018#endif