blob: 16e69b9b2613407ccdca49815ebec568b6292027 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dan Stoza9e56aa02015-11-02 13:00:03 -080017//#define LOG_NDEBUG 0
18#undef LOG_TAG
19#define LOG_TAG "Layer"
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080020#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21
Mathias Agopian13127d82013-03-05 17:47:11 -080022#include <math.h>
David Sodman41fdfc92017-11-06 16:09:56 -080023#include <stdint.h>
24#include <stdlib.h>
25#include <sys/types.h>
chaviw4b129c22018-04-09 16:19:43 -070026#include <algorithm>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080027#include <mutex>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080028
Yiwei Zhang5434a782018-12-05 18:06:32 -080029#include <android-base/stringprintf.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080030#include <compositionengine/Display.h>
Lloyd Pique0b785d82018-12-04 17:25:27 -080031#include <compositionengine/Layer.h>
Lloyd Piquea83776c2019-01-29 18:42:32 -080032#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080033#include <compositionengine/OutputLayer.h>
Lloyd Pique0b785d82018-12-04 17:25:27 -080034#include <compositionengine/impl/LayerCompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080035#include <compositionengine/impl/OutputLayerCompositionState.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070036#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070037#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070038#include <cutils/properties.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080039#include <gui/BufferItem.h>
40#include <gui/LayerDebugInfo.h>
41#include <gui/Surface.h>
42#include <renderengine/RenderEngine.h>
43#include <ui/DebugUtils.h>
44#include <ui/GraphicBuffer.h>
45#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046#include <utils/Errors.h>
47#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080048#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080050#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051
Yiwei Zhang60d1a192018-03-07 14:52:28 -080052#include "BufferLayer.h"
Valerie Haudd0b7572019-01-29 14:59:27 -080053#include "ColorLayer.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020054#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070055#include "DisplayDevice.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080056#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057#include "Layer.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080058#include "LayerProtoHelper.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070059#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070060#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061#include "SurfaceFlinger.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080062#include "TimeStats/TimeStats.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070063
David Sodman41fdfc92017-11-06 16:09:56 -080064#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066namespace android {
67
Yiwei Zhang5434a782018-12-05 18:06:32 -080068using base::StringAppendF;
69
Lloyd Piquef1c675b2018-09-12 20:45:39 -070070std::atomic<int32_t> Layer::sSequence{1};
Mathias Agopian13127d82013-03-05 17:47:11 -080071
Lloyd Pique42ab75e2018-09-12 20:46:03 -070072Layer::Layer(const LayerCreationArgs& args)
Lloyd Piquefa8fd6b2019-01-29 18:42:24 -080073 : mFlinger(args.flinger), mName(args.name), mClientRef(args.client) {
Mathias Agopiana67932f2011-04-20 14:20:59 -070074 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070075
76 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -070077 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
78 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
79 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070080
Dan Stozaf7ba41a2017-05-10 15:11:11 -070081 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070082
Lloyd Pique0449b0f2018-12-20 16:23:45 -080083 mCurrentState.active_legacy.w = args.w;
84 mCurrentState.active_legacy.h = args.h;
85 mCurrentState.flags = layerFlags;
86 mCurrentState.active_legacy.transform.set(0, 0);
87 mCurrentState.crop_legacy.makeInvalid();
88 mCurrentState.requestedCrop_legacy = mCurrentState.crop_legacy;
89 mCurrentState.z = 0;
90 mCurrentState.color.a = 1.0f;
91 mCurrentState.layerStack = 0;
92 mCurrentState.sequence = 0;
93 mCurrentState.requested_legacy = mCurrentState.active_legacy;
Lloyd Pique0449b0f2018-12-20 16:23:45 -080094 mCurrentState.active.w = UINT32_MAX;
95 mCurrentState.active.h = UINT32_MAX;
96 mCurrentState.active.transform.set(0, 0);
97 mCurrentState.transform = 0;
98 mCurrentState.transformToDisplayInverse = false;
99 mCurrentState.crop.makeInvalid();
100 mCurrentState.acquireFence = new Fence(-1);
101 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
102 mCurrentState.hdrMetadata.validTypes = 0;
103 mCurrentState.surfaceDamageRegion.clear();
104 mCurrentState.cornerRadius = 0.0f;
105 mCurrentState.api = -1;
106 mCurrentState.hasColorTransform = false;
Peiyong Linc502cb72019-03-01 15:00:23 -0800107 mCurrentState.colorSpaceAgnostic = false;
Evan Roskya1f1e152019-01-24 16:17:46 -0800108 mCurrentState.metadata = args.metadata;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700109
110 // drawing state & current state are identical
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800111 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700112
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800113 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700114 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800115 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200116 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700117
118 mFlinger->onLayerCreated();
Dan Stoza436ccf32018-06-21 12:10:12 -0700119}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700120
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700121Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800122 sp<Client> c(mClientRef.promote());
123 if (c != 0) {
124 c->detachLayer(this);
125 }
126
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000127 mFrameTracker.logAndResetStats(mName);
Robert Carr2e102c92018-10-23 12:11:15 -0700128
Robert Carr2e102c92018-10-23 12:11:15 -0700129 mFlinger->onLayerDestroyed();
Mathias Agopian96f08192010-06-02 23:28:45 -0700130}
131
Mathias Agopian13127d82013-03-05 17:47:11 -0800132// ---------------------------------------------------------------------------
133// callbacks
134// ---------------------------------------------------------------------------
135
David Sodmaneb085e02017-10-05 18:49:04 -0700136/*
137 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
138 * Layer. So, the implementation is done in BufferLayer. When called on a
139 * ColorLayer object, it's essentially a NOP.
140 */
David Sodmaneb085e02017-10-05 18:49:04 -0700141void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800142
Chia-I Wuc6657022017-08-15 11:18:17 -0700143void Layer::onRemovedFromCurrentState() {
Robert Carr2e102c92018-10-23 12:11:15 -0700144 mRemovedFromCurrentState = true;
145
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800146 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
147 if (mCurrentState.zOrderRelativeOf != nullptr) {
148 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
149 if (strongRelative != nullptr) {
150 strongRelative->removeZOrderRelative(this);
151 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700152 }
chaviw606e5cf2019-03-01 10:12:10 -0800153 setZOrderRelativeOf(nullptr);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700154 }
Rob Carr4bba3702018-10-08 21:53:30 +0000155
Robert Carr2e102c92018-10-23 12:11:15 -0700156 // Since we are no longer reachable from CurrentState SurfaceFlinger
157 // will no longer invoke doTransaction for us, and so we will
158 // never finish applying transactions. We signal the sync point
159 // now so that another layer will not become indefinitely
160 // blocked.
161 for (auto& point: mRemoteSyncPoints) {
162 point->setTransactionApplied();
163 }
164 mRemoteSyncPoints.clear();
165
166 {
167 Mutex::Autolock syncLock(mLocalSyncPointMutex);
168 for (auto& point : mLocalSyncPoints) {
169 point->setFrameAvailable();
170 }
171 mLocalSyncPoints.clear();
172 }
173
Chia-I Wuc6657022017-08-15 11:18:17 -0700174 for (const auto& child : mCurrentChildren) {
175 child->onRemovedFromCurrentState();
176 }
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800177
178 mFlinger->markLayerPendingRemovalLocked(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700179}
Chia-I Wu38512252017-05-17 14:36:16 -0700180
chaviw61626f22018-11-15 16:26:27 -0800181void Layer::addToCurrentState() {
182 mRemovedFromCurrentState = false;
183
184 for (const auto& child : mCurrentChildren) {
185 child->addToCurrentState();
186 }
187}
188
Mathias Agopian13127d82013-03-05 17:47:11 -0800189// ---------------------------------------------------------------------------
190// set-up
191// ---------------------------------------------------------------------------
192
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700193const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800194 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800195}
196
chaviw13fdc492017-06-27 12:40:18 -0700197bool Layer::getPremultipledAlpha() const {
198 return mPremultipliedAlpha;
199}
200
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700201sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800202 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700203 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800204}
205
206// ---------------------------------------------------------------------------
207// h/w composer set-up
208// ---------------------------------------------------------------------------
209
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800210bool Layer::hasHwcLayer(const sp<const DisplayDevice>& displayDevice) {
211 auto outputLayer = findOutputLayerForDisplay(displayDevice);
212 LOG_FATAL_IF(!outputLayer);
213 return outputLayer->getState().hwc && (*outputLayer->getState().hwc).hwcLayer != nullptr;
214}
215
216HWC2::Layer* Layer::getHwcLayer(const sp<const DisplayDevice>& displayDevice) {
217 auto outputLayer = findOutputLayerForDisplay(displayDevice);
218 if (!outputLayer || !outputLayer->getState().hwc) {
219 return nullptr;
220 }
221 return (*outputLayer->getState().hwc).hwcLayer.get();
Steven Thomasb02664d2017-07-26 18:48:28 -0700222}
Steven Thomasb02664d2017-07-26 18:48:28 -0700223
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800224Rect Layer::getContentCrop() const {
225 // this is the crop rectangle that applies to the buffer
226 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700227 Rect crop;
228 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800229 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700230 crop = mCurrentCrop;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800231 } else if (mActiveBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800232 // otherwise we use the whole buffer
Lloyd Pique0b785d82018-12-04 17:25:27 -0800233 crop = mActiveBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700234 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800235 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700236 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700237 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700238 return crop;
239}
240
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700241static Rect reduce(const Rect& win, const Region& exclude) {
242 if (CC_LIKELY(exclude.isEmpty())) {
243 return win;
244 }
245 if (exclude.isRect()) {
246 return win.reduce(exclude.getBounds());
247 }
248 return Region(win).subtract(exclude).getBounds();
249}
250
Dan Stoza80d61162017-12-20 15:57:52 -0800251static FloatRect reduce(const FloatRect& win, const Region& exclude) {
252 if (CC_LIKELY(exclude.isEmpty())) {
253 return win;
254 }
255 // Convert through Rect (by rounding) for lack of FloatRegion
256 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
257}
258
Vishnu Nair4351ad52019-02-11 14:13:02 -0800259Rect Layer::getScreenBounds(bool reduceTransparentRegion) const {
Vishnu Nairf0c28512019-02-08 12:40:28 -0800260 if (!reduceTransparentRegion) {
261 return Rect{mScreenBounds};
262 }
263
264 FloatRect bounds = getBounds();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800265 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800266 // Transform to screen space.
267 bounds = t.transform(bounds);
268 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700269}
270
Vishnu Nair4351ad52019-02-11 14:13:02 -0800271FloatRect Layer::getBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000272 const State& s(getDrawingState());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800273 return getBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700274}
275
Vishnu Nairf0c28512019-02-08 12:40:28 -0800276FloatRect Layer::getBounds(const Region& activeTransparentRegion) const {
277 // Subtract the transparent region and snap to the bounds.
278 return reduce(mBounds, activeTransparentRegion);
279}
280
Vishnu Nair4351ad52019-02-11 14:13:02 -0800281ui::Transform Layer::getTransformWithScale() const {
282 // If the layer is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
283 // it isFixedSize) then there may be additional scaling not accounted
284 // for in the transform. We need to mirror this scaling to child surfaces
285 // or we will break the contract where WM can treat child surfaces as
286 // pixels in the parent surface.
Lloyd Pique0b785d82018-12-04 17:25:27 -0800287 if (!isFixedSize() || !mActiveBuffer) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800288 return mEffectiveTransform;
289 }
290
Marissa Wall290ad082019-03-06 13:23:47 -0800291 // If the layer is a buffer state layer, the active width and height
292 // could be infinite. In that case, return the effective transform.
293 const uint32_t activeWidth = getActiveWidth(getDrawingState());
294 const uint32_t activeHeight = getActiveHeight(getDrawingState());
295 if (activeWidth >= UINT32_MAX && activeHeight >= UINT32_MAX) {
296 return mEffectiveTransform;
297 }
298
Vishnu Nair4351ad52019-02-11 14:13:02 -0800299 int bufferWidth;
300 int bufferHeight;
301 if ((mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800302 bufferWidth = mActiveBuffer->getWidth();
303 bufferHeight = mActiveBuffer->getHeight();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800304 } else {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800305 bufferHeight = mActiveBuffer->getWidth();
306 bufferWidth = mActiveBuffer->getHeight();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800307 }
Marissa Wall290ad082019-03-06 13:23:47 -0800308 float sx = activeWidth / static_cast<float>(bufferWidth);
309 float sy = activeHeight / static_cast<float>(bufferHeight);
310
Vishnu Nair4351ad52019-02-11 14:13:02 -0800311 ui::Transform extraParentScaling;
312 extraParentScaling.set(sx, 0, 0, sy);
313 return mEffectiveTransform * extraParentScaling;
314}
315
316void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform) {
317 const State& s(getDrawingState());
318
319 // Calculate effective layer transform
320 mEffectiveTransform = parentTransform * getActiveTransform(s);
321
322 // Transform parent bounds to layer space
323 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
324
325 // Calculate display frame
326 mSourceBounds = computeSourceBounds(parentBounds);
327
328 // Calculate bounds by croping diplay frame with layer crop and parent bounds
329 FloatRect bounds = mSourceBounds;
330 const Rect layerCrop = getCrop(s);
331 if (!layerCrop.isEmpty()) {
332 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
333 }
334 bounds = bounds.intersect(parentBounds);
335
336 mBounds = bounds;
337 mScreenBounds = mEffectiveTransform.transform(mBounds);
338 for (const sp<Layer>& child : mDrawingChildren) {
339 child->computeBounds(mBounds, getTransformWithScale());
340 }
341}
342
Vishnu Nair60356342018-11-13 13:00:45 -0800343Rect Layer::getCroppedBufferSize(const State& s) const {
344 Rect size = getBufferSize(s);
345 Rect crop = getCrop(s);
346 if (!crop.isEmpty() && size.isValid()) {
347 size.intersect(crop, &size);
348 } else if (!crop.isEmpty()) {
349 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700350 }
Vishnu Nair60356342018-11-13 13:00:45 -0800351 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800352}
353
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700354void Layer::setupRoundedCornersCropCoordinates(Rect win,
355 const FloatRect& roundedCornersCrop) const {
356 // Translate win by the rounded corners rect coordinates, to have all values in
357 // layer coordinate space.
358 win.left -= roundedCornersCrop.left;
359 win.right -= roundedCornersCrop.left;
360 win.top -= roundedCornersCrop.top;
361 win.bottom -= roundedCornersCrop.top;
362
Lloyd Pique0b785d82018-12-04 17:25:27 -0800363 renderengine::Mesh::VertexArray<vec2> cropCoords(
364 getCompositionLayer()->editState().reMesh.getCropCoordArray<vec2>());
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700365 cropCoords[0] = vec2(win.left, win.top);
366 cropCoords[1] = vec2(win.left, win.top + win.getHeight());
367 cropCoords[2] = vec2(win.right, win.top + win.getHeight());
368 cropCoords[3] = vec2(win.right, win.top);
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700369}
370
Lloyd Piquea83776c2019-01-29 18:42:32 -0800371void Layer::latchGeometry(compositionengine::LayerFECompositionState& compositionState) const {
372 const auto& drawingState{getDrawingState()};
373 auto alpha = static_cast<float>(getAlpha());
David Revemanecf0fa52017-03-03 11:32:44 -0500374 auto blendMode = HWC2::BlendMode::None;
Lloyd Piquea83776c2019-01-29 18:42:32 -0800375 if (!isOpaque(drawingState) || alpha != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800376 blendMode =
377 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800378 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800379
Lloyd Piquea83776c2019-01-29 18:42:32 -0800380 int type = drawingState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
381 int appId = drawingState.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700382 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400383 if (parent.get()) {
384 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800385 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
386 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
387 if (parentType >= 0 || parentAppId >= 0) {
388 type = parentType;
389 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700390 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400391 }
392
Lloyd Piquea83776c2019-01-29 18:42:32 -0800393 compositionState.geomLayerTransform = getTransform();
394 compositionState.geomInverseLayerTransform = compositionState.geomLayerTransform.inverse();
395 compositionState.geomBufferSize = getBufferSize(drawingState);
396 compositionState.geomContentCrop = getContentCrop();
397 compositionState.geomCrop = getCrop(drawingState);
398 compositionState.geomBufferTransform = mCurrentTransform;
399 compositionState.geomBufferUsesDisplayInverseTransform = getTransformToDisplayInverse();
400 compositionState.geomActiveTransparentRegion = getActiveTransparentRegion(drawingState);
401 compositionState.geomLayerBounds = mBounds;
402 compositionState.geomUsesSourceCrop = usesSourceCrop();
403 compositionState.isSecure = isSecure();
David Sodman15094112018-10-11 09:39:37 -0700404
Lloyd Piquea83776c2019-01-29 18:42:32 -0800405 compositionState.blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
406 compositionState.alpha = alpha;
407 compositionState.type = type;
408 compositionState.appId = appId;
409}
David Sodmanba340492018-08-05 21:51:33 -0700410
Lloyd Piquea83776c2019-01-29 18:42:32 -0800411void Layer::latchCompositionState(compositionengine::LayerFECompositionState& compositionState,
412 bool includeGeometry) const {
413 if (includeGeometry) {
414 latchGeometry(compositionState);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700415 }
Lloyd Piquea83776c2019-01-29 18:42:32 -0800416}
Mathias Agopian29a367b2011-07-12 14:51:45 -0700417
Lloyd Piquea83776c2019-01-29 18:42:32 -0800418const char* Layer::getDebugName() const {
419 return mName.string();
David Sodman4b7c4bc2017-11-17 12:13:59 -0800420}
421
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800422void Layer::forceClientComposition(const sp<DisplayDevice>& display) {
423 const auto outputLayer = findOutputLayerForDisplay(display);
424 LOG_FATAL_IF(!outputLayer);
425 outputLayer->editState().forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800426}
Dan Stozaee44edd2015-03-23 15:50:23 -0700427
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800428bool Layer::getForceClientComposition(const sp<DisplayDevice>& display) {
429 const auto outputLayer = findOutputLayerForDisplay(display);
430 LOG_FATAL_IF(!outputLayer);
431 return outputLayer->getState().forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800432}
433
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700434void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800435 const auto outputLayer = findOutputLayerForDisplay(display);
436 LOG_FATAL_IF(!outputLayer);
437
438 if (!outputLayer->getState().hwc ||
439 (*outputLayer->getState().hwc).hwcCompositionType !=
440 Hwc2::IComposerClient::Composition::CURSOR) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800441 return;
442 }
443
444 // This gives us only the "orientation" component of the transform
Vishnu Nair33a6eee2019-02-06 13:48:06 -0800445 const State& s(getDrawingState());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800446
447 // Apply the layer's transform, followed by the display's global transform
448 // Here we're guaranteed that the layer's transform preserves rects
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800449 Rect win = getCroppedBufferSize(s);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800450 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700451 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800452 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700453 frame.intersect(display->getViewport(), &frame);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700454 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800455 auto position = displayTransform.transform(frame);
456
Dominik Laskowski7e045462018-05-30 13:02:02 -0700457 auto error =
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800458 (*outputLayer->getState().hwc).hwcLayer->setCursorPosition(position.left, position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800459 ALOGE_IF(error != HWC2::Error::None,
460 "[%s] Failed to set cursor position "
461 "to (%d, %d): %s (%d)",
462 mName.string(), position.left, position.top, to_string(error).c_str(),
463 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800464}
Riley Andrews03414a12014-07-01 14:22:59 -0700465
Mathias Agopian13127d82013-03-05 17:47:11 -0800466// ---------------------------------------------------------------------------
467// drawing...
468// ---------------------------------------------------------------------------
469
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000470bool Layer::prepareClientLayer(const RenderArea& renderArea, const Region& clip,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800471 Region& clearRegion, const bool supportProtectedContent,
472 renderengine::LayerSettings& layer) {
473 return prepareClientLayer(renderArea, clip, false, clearRegion, supportProtectedContent, layer);
Mathias Agopian13127d82013-03-05 17:47:11 -0800474}
475
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000476bool Layer::prepareClientLayer(const RenderArea& renderArea, bool useIdentityTransform,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800477 Region& clearRegion, const bool supportProtectedContent,
478 renderengine::LayerSettings& layer) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000479 return prepareClientLayer(renderArea, Region(renderArea.getBounds()), useIdentityTransform,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800480 clearRegion, supportProtectedContent, layer);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000481}
482
483bool Layer::prepareClientLayer(const RenderArea& /*renderArea*/, const Region& /*clip*/,
484 bool useIdentityTransform, Region& /*clearRegion*/,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800485 const bool /*supportProtectedContent*/,
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000486 renderengine::LayerSettings& layer) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800487 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000488 half alpha = getAlpha();
489 layer.geometry.boundaries = bounds;
490 if (useIdentityTransform) {
491 layer.geometry.positionTransform = mat4();
492 } else {
493 const ui::Transform transform = getTransform();
494 mat4 m;
495 m[0][0] = transform[0][0];
496 m[0][1] = transform[0][1];
497 m[0][3] = transform[0][2];
498 m[1][0] = transform[1][0];
499 m[1][1] = transform[1][1];
500 m[1][3] = transform[1][2];
501 m[3][0] = transform[2][0];
502 m[3][1] = transform[2][1];
503 m[3][3] = transform[2][2];
504 layer.geometry.positionTransform = m;
505 }
506
507 if (hasColorTransform()) {
508 layer.colorTransform = getColorTransform();
509 }
510
511 const auto roundedCornerState = getRoundedCornerState();
512 layer.geometry.roundedCornersRadius = roundedCornerState.radius;
513 layer.geometry.roundedCornersCrop = roundedCornerState.cropRect;
514
515 layer.alpha = alpha;
516 layer.sourceDataspace = mCurrentDataSpace;
517 return true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800518}
519
David Sodman41fdfc92017-11-06 16:09:56 -0800520void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
521 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800522 auto& engine(mFlinger->getRenderEngine());
Lloyd Pique0b785d82018-12-04 17:25:27 -0800523 computeGeometry(renderArea, getCompositionLayer()->editState().reMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700524 engine.setupFillWithColor(red, green, blue, alpha);
Lloyd Pique0b785d82018-12-04 17:25:27 -0800525 engine.drawMesh(getCompositionLayer()->getState().reMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800526}
527
David Sodmanc1498e62018-09-12 14:36:26 -0700528void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
529 clearWithOpenGL(renderArea, 0, 0, 0, 0);
530}
531
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800532void Layer::setCompositionType(const sp<const DisplayDevice>& display,
533 Hwc2::IComposerClient::Composition type) {
534 const auto outputLayer = findOutputLayerForDisplay(display);
535 LOG_FATAL_IF(!outputLayer);
536 LOG_FATAL_IF(!outputLayer->getState().hwc);
537 auto& compositionState = outputLayer->editState();
538
539 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", ((*compositionState.hwc).hwcLayer)->getId(),
540 toString(type).c_str(), 1);
541 if ((*compositionState.hwc).hwcCompositionType != type) {
David Sodman15094112018-10-11 09:39:37 -0700542 ALOGV(" actually setting");
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800543 (*compositionState.hwc).hwcCompositionType = type;
544
545 auto error = (*compositionState.hwc)
546 .hwcLayer->setCompositionType(static_cast<HWC2::Composition>(type));
547 ALOGE_IF(error != HWC2::Error::None,
548 "[%s] Failed to set "
549 "composition type %s: %s (%d)",
550 mName.string(), toString(type).c_str(), to_string(error).c_str(),
551 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800552 }
553}
554
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800555Hwc2::IComposerClient::Composition Layer::getCompositionType(
556 const sp<const DisplayDevice>& display) const {
557 const auto outputLayer = findOutputLayerForDisplay(display);
558 LOG_FATAL_IF(!outputLayer);
559 return outputLayer->getState().hwc ? (*outputLayer->getState().hwc).hwcCompositionType
560 : Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800561}
562
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800563bool Layer::getClearClientTarget(const sp<const DisplayDevice>& display) const {
564 const auto outputLayer = findOutputLayerForDisplay(display);
565 LOG_FATAL_IF(!outputLayer);
566 return outputLayer->getState().clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800567}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800568
Dan Stozacac35382016-01-27 12:21:06 -0800569bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
570 if (point->getFrameNumber() <= mCurrentFrameNumber) {
571 // Don't bother with a SyncPoint, since we've already latched the
572 // relevant frame
573 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700574 }
Robert Carr2e102c92018-10-23 12:11:15 -0700575 if (isRemovedFromCurrentState()) {
576 return false;
577 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700578
Dan Stozacac35382016-01-27 12:21:06 -0800579 Mutex::Autolock lock(mLocalSyncPointMutex);
580 mLocalSyncPoints.push_back(point);
581 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700582}
583
Mathias Agopian13127d82013-03-05 17:47:11 -0800584// ----------------------------------------------------------------------------
585// local state
586// ----------------------------------------------------------------------------
587
Peiyong Lin833074a2018-08-28 11:53:54 -0700588void Layer::computeGeometry(const RenderArea& renderArea,
589 renderengine::Mesh& mesh,
chaviwa76b2712017-09-20 12:02:26 -0700590 bool useIdentityTransform) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700591 const ui::Transform renderAreaTransform(renderArea.getTransform());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800592 FloatRect win = getBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700593
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000594 vec2 lt = vec2(win.left, win.top);
595 vec2 lb = vec2(win.left, win.bottom);
596 vec2 rb = vec2(win.right, win.bottom);
597 vec2 rt = vec2(win.right, win.top);
598
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800599 ui::Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000600 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700601 lt = layerTransform.transform(lt);
602 lb = layerTransform.transform(lb);
603 rb = layerTransform.transform(rb);
604 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000605 }
606
Peiyong Lin833074a2018-08-28 11:53:54 -0700607 renderengine::Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700608 position[0] = renderAreaTransform.transform(lt);
609 position[1] = renderAreaTransform.transform(lb);
610 position[2] = renderAreaTransform.transform(rb);
611 position[3] = renderAreaTransform.transform(rt);
Mathias Agopian13127d82013-03-05 17:47:11 -0800612}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800613
David Sodman41fdfc92017-11-06 16:09:56 -0800614bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800615 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700616 return (s.flags & layer_state_t::eLayerSecure);
617}
618
Mathias Agopian13127d82013-03-05 17:47:11 -0800619void Layer::setVisibleRegion(const Region& visibleRegion) {
620 // always called from main thread
621 this->visibleRegion = visibleRegion;
622}
623
624void Layer::setCoveredRegion(const Region& coveredRegion) {
625 // always called from main thread
626 this->coveredRegion = coveredRegion;
627}
628
David Sodman41fdfc92017-11-06 16:09:56 -0800629void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800630 // always called from main thread
631 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
632}
633
Robert Carre5f4f692018-01-12 13:12:28 -0800634void Layer::clearVisibilityRegions() {
635 visibleRegion.clear();
636 visibleNonTransparentRegion.clear();
637 coveredRegion.clear();
638}
639
Mathias Agopian13127d82013-03-05 17:47:11 -0800640// ----------------------------------------------------------------------------
641// transaction
642// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800643
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800644void Layer::pushPendingState() {
645 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700646 return;
647 }
648
Dan Stoza7dde5992015-05-22 09:51:44 -0700649 // If this transaction is waiting on the receipt of a frame, generate a sync
650 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700651 // We don't allow installing sync points after we are removed from the current state
652 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800653 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
654 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800655 if (barrierLayer == nullptr) {
656 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700657 // If we can't promote the layer we are intended to wait on,
658 // then it is expired or otherwise invalid. Allow this transaction
659 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800660 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800661 } else {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800662 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy);
Robert Carr0d480722017-01-10 16:42:54 -0800663 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800664 mRemoteSyncPoints.push_back(std::move(syncPoint));
665 } else {
666 // We already missed the frame we're supposed to synchronize
667 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800668 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800669 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700670 }
671
Dan Stoza7dde5992015-05-22 09:51:44 -0700672 // Wake us up to check if the frame has been received
673 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700674 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700675 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800676 mPendingStates.push_back(mCurrentState);
677 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700678}
679
Pablo Ceballos05289c22016-04-14 15:49:55 -0700680void Layer::popPendingState(State* stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800681 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700682
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800683 mPendingStates.removeAt(0);
684 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700685}
686
Pablo Ceballos05289c22016-04-14 15:49:55 -0700687bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700688 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800689 while (!mPendingStates.empty()) {
690 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700691 if (mRemoteSyncPoints.empty()) {
692 // If we don't have a sync point for this, apply it anyway. It
693 // will be visually wrong, but it should keep us from getting
694 // into too much trouble.
695 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700696 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700697 stateUpdateAvailable = true;
698 continue;
699 }
700
Marissa Wallf58c14b2018-07-24 10:50:43 -0700701 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800702 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800703 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800704
705 // Signal our end of the sync point and then dispose of it
706 mRemoteSyncPoints.front()->setTransactionApplied();
707 mRemoteSyncPoints.pop_front();
708 continue;
709 }
710
Dan Stoza7dde5992015-05-22 09:51:44 -0700711 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
712 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700713 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700714 stateUpdateAvailable = true;
715
716 // Signal our end of the sync point and then dispose of it
717 mRemoteSyncPoints.front()->setTransactionApplied();
718 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800719 } else {
720 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700721 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700722 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700723 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700724 stateUpdateAvailable = true;
725 }
726 }
727
728 // If we still have pending updates, wake SurfaceFlinger back up and point
729 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800730 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700731 setTransactionFlags(eTransactionNeeded);
732 mFlinger->setTransactionFlags(eTraversalNeeded);
733 }
734
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800735 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700736 return stateUpdateAvailable;
737}
738
Marissa Wall61c58622018-07-18 10:12:20 -0700739uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000740 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800741
Marissa Wall61c58622018-07-18 10:12:20 -0700742 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
743 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700744
David Sodmaneb085e02017-10-05 18:49:04 -0700745 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700746 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000747 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800748 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
749 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
750 " requested={ wh={%4u,%4u} }}\n"
751 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
752 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700753 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700754 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
755 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
756 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
757 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
758 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700759 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
760 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
761 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800762 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700763
Robert Carre392b552017-09-19 12:16:05 -0700764 // Don't let Layer::doTransaction update the drawing state
765 // if we have a pending resize, unless we are in fixed-size mode.
766 // the drawing state will be updated only once we receive a buffer
767 // with the correct size.
768 //
769 // In particular, we want to make sure the clip (which is part
770 // of the geometry state) is latched together with the size but is
771 // latched immediately when no resizing is involved.
772 //
773 // If a sideband stream is attached, however, we want to skip this
774 // optimization so that transactions aren't missed when a buffer
775 // never arrives
776 //
777 // In the case that we don't have a buffer we ignore other factors
778 // and avoid entering the resizePending state. At a high level the
779 // resizePending state is to avoid applying the state of the new buffer
780 // to the old buffer. However in the state where we don't have an old buffer
781 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700782 const bool resizePending =
783 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
784 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
Lloyd Pique0b785d82018-12-04 17:25:27 -0800785 (mActiveBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700786 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800787 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700788 flags |= eDontUpdateGeometryState;
789 }
790 }
791
Robert Carr7bf247e2017-05-18 14:02:49 -0700792 // Here we apply various requested geometry states, depending on our
793 // latching configuration. See Layer.h for a detailed discussion of
794 // how geometry latching is controlled.
795 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000796 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700797
798 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
799 // mode, which causes attributes which normally latch regardless of scaling mode,
800 // to be delayed. We copy the requested state to the active state making sure
801 // to respect these rules (again see Layer.h for a detailed discussion).
802 //
803 // There is an awkward asymmetry in the handling of the crop states in the position
804 // states, as can be seen below. Largely this arises from position and transform
805 // being stored in the same data structure while having different latching rules.
806 // b/38182305
807 //
Marissa Wall61c58622018-07-18 10:12:20 -0700808 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700809 // applyPendingStates in the presence of deferred transactions.
810 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -0700811 float tx = stateToCommit->active_legacy.transform.tx();
812 float ty = stateToCommit->active_legacy.transform.ty();
813 stateToCommit->active_legacy = stateToCommit->requested_legacy;
814 stateToCommit->active_legacy.transform.set(tx, ty);
815 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700816 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700817 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -0700818 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700819 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800820 }
821
Marissa Wall61c58622018-07-18 10:12:20 -0700822 return flags;
823}
824
825uint32_t Layer::doTransaction(uint32_t flags) {
826 ATRACE_CALL();
827
chaviw5aedec92018-10-22 10:40:38 -0700828 if (mLayerDetached) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800829 return flags;
830 }
831
832 if (mChildrenChanged) {
833 flags |= eVisibleRegion;
834 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -0700835 }
836
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800837 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +0000838 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -0700839 if (!applyPendingStates(&c)) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -0800840 return flags;
Marissa Wall61c58622018-07-18 10:12:20 -0700841 }
842
843 flags = doTransactionResize(flags, &c);
844
Alec Mourib416efd2018-09-06 21:01:59 +0000845 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700846
847 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800848 // invalidate and recompute the visible regions if needed
849 flags |= Layer::eVisibleRegion;
850 }
851
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700852 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800853 // invalidate and recompute the visible regions if needed
854 flags |= eVisibleRegion;
855 this->contentDirty = true;
856
857 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -0700858 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -0700859 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -0800860 }
861
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800862 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -0700863 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800864 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -0700865 }
866
Mathias Agopian13127d82013-03-05 17:47:11 -0800867 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -0700868 commitTransaction(c);
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800869 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -0800870 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800871}
872
Pablo Ceballos05289c22016-04-14 15:49:55 -0700873void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800874 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700875}
876
Mathias Agopian13127d82013-03-05 17:47:11 -0800877uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800878 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -0800879}
880
881uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800882 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -0800883}
884
Robert Carr82364e32016-05-15 11:27:47 -0700885bool Layer::setPosition(float x, float y, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800886 if (mCurrentState.requested_legacy.transform.tx() == x &&
887 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -0800888 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800889 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -0700890
891 // We update the requested and active position simultaneously because
892 // we want to apply the position portion of the transform matrix immediately,
893 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800894 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -0700895 if (immediate && !mFreezeGeometryUpdates) {
896 // Here we directly update the active state
897 // unlike other setters, because we store it within
898 // the transform, but use different latching rules.
899 // b/38182305
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800900 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -0700901 }
Robert Carr7bf247e2017-05-18 14:02:49 -0700902 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -0700903
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800904 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800905 setTransactionFlags(eTransactionNeeded);
906 return true;
907}
Robert Carr82364e32016-05-15 11:27:47 -0700908
Robert Carr1f0a16a2016-10-24 16:27:39 -0700909bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
910 ssize_t idx = mCurrentChildren.indexOf(childLayer);
911 if (idx < 0) {
912 return false;
913 }
914 if (childLayer->setLayer(z)) {
915 mCurrentChildren.removeAt(idx);
916 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800917 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700918 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800919 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700920}
921
Robert Carr503c7042017-09-27 15:06:08 -0700922bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
923 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
924 ssize_t idx = mCurrentChildren.indexOf(childLayer);
925 if (idx < 0) {
926 return false;
927 }
928 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
929 mCurrentChildren.removeAt(idx);
930 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -0800931 return true;
Robert Carr503c7042017-09-27 15:06:08 -0700932 }
Robert Carr503d2bd2017-12-04 15:49:47 -0800933 return false;
Robert Carr503c7042017-09-27 15:06:08 -0700934}
935
Robert Carrae060832016-11-28 10:51:00 -0800936bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800937 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
938 mCurrentState.sequence++;
939 mCurrentState.z = z;
940 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700941
942 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800943 if (mCurrentState.zOrderRelativeOf != nullptr) {
944 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -0700945 if (strongRelative != nullptr) {
946 strongRelative->removeZOrderRelative(this);
947 }
chaviw606e5cf2019-03-01 10:12:10 -0800948 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -0700949 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800950 setTransactionFlags(eTransactionNeeded);
951 return true;
952}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700953
Robert Carrdb66e622017-04-10 16:55:57 -0700954void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800955 mCurrentState.zOrderRelatives.remove(relative);
956 mCurrentState.sequence++;
957 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -0700958 setTransactionFlags(eTransactionNeeded);
959}
960
961void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800962 mCurrentState.zOrderRelatives.add(relative);
963 mCurrentState.modified = true;
964 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -0700965 setTransactionFlags(eTransactionNeeded);
966}
967
chaviw606e5cf2019-03-01 10:12:10 -0800968void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
969 mCurrentState.zOrderRelativeOf = relativeOf;
970 mCurrentState.sequence++;
971 mCurrentState.modified = true;
972 setTransactionFlags(eTransactionNeeded);
973}
974
Robert Carr503d2bd2017-12-04 15:49:47 -0800975bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -0700976 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
977 if (handle == nullptr) {
978 return false;
979 }
980 sp<Layer> relative = handle->owner.promote();
981 if (relative == nullptr) {
982 return false;
983 }
984
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800985 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
986 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -0800987 return false;
988 }
989
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800990 mCurrentState.sequence++;
991 mCurrentState.modified = true;
992 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -0700993
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800994 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -0700995 if (oldZOrderRelativeOf != nullptr) {
996 oldZOrderRelativeOf->removeZOrderRelative(this);
997 }
chaviw606e5cf2019-03-01 10:12:10 -0800998 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -0700999 relative->addZOrderRelative(this);
1000
1001 setTransactionFlags(eTransactionNeeded);
1002
1003 return true;
1004}
1005
Mathias Agopian13127d82013-03-05 17:47:11 -08001006bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001007 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001008 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001009 mCurrentState.requested_legacy.w = w;
1010 mCurrentState.requested_legacy.h = h;
1011 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001012 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001013
1014 // record the new size, from this point on, when the client request
1015 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001016 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001017 return true;
1018}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001019bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001020 if (mCurrentState.color.a == alpha) return false;
1021 mCurrentState.sequence++;
1022 mCurrentState.color.a = alpha;
1023 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001024 setTransactionFlags(eTransactionNeeded);
1025 return true;
1026}
chaviw13fdc492017-06-27 12:40:18 -07001027
Valerie Haudd0b7572019-01-29 14:59:27 -08001028bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1029 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001030 return false;
Valerie Hauaa194562019-02-05 16:21:38 -08001031 }
1032 mCurrentState.sequence++;
1033 mCurrentState.modified = true;
1034 setTransactionFlags(eTransactionNeeded);
1035
1036 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001037 // create background color layer if one does not yet exist
1038 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
1039 const String8& name = mName + "BackgroundColorLayer";
Evan Roskya1f1e152019-01-24 16:17:46 -08001040 mCurrentState.bgColorLayer = new ColorLayer(
1041 LayerCreationArgs(mFlinger.get(), nullptr, name, 0, 0, flags, LayerMetadata()));
chaviw13fdc492017-06-27 12:40:18 -07001042
Valerie Haudd0b7572019-01-29 14:59:27 -08001043 // add to child list
1044 addChild(mCurrentState.bgColorLayer);
1045 mFlinger->mLayersAdded = true;
1046 // set up SF to handle added color layer
1047 if (isRemovedFromCurrentState()) {
1048 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1049 }
1050 mFlinger->setTransactionFlags(eTransactionNeeded);
1051 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1052 mCurrentState.bgColorLayer->reparent(nullptr);
1053 mCurrentState.bgColorLayer = nullptr;
1054 return true;
1055 }
1056
1057 mCurrentState.bgColorLayer->setColor(color);
1058 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1059 mCurrentState.bgColorLayer->setAlpha(alpha);
1060 mCurrentState.bgColorLayer->setDataspace(dataspace);
1061
chaviw13fdc492017-06-27 12:40:18 -07001062 return true;
1063}
1064
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001065bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001066 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001067
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001068 mCurrentState.sequence++;
1069 mCurrentState.cornerRadius = cornerRadius;
1070 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001071 setTransactionFlags(eTransactionNeeded);
1072 return true;
1073}
1074
Robert Carrd4ae7f32018-06-07 16:10:57 -07001075bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1076 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001077 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001078 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1079
1080 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1081 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1082 return false;
1083 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001084 mCurrentState.sequence++;
1085 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1086 matrix.dsdy);
1087 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001088 setTransactionFlags(eTransactionNeeded);
1089 return true;
1090}
Marissa Wall61c58622018-07-18 10:12:20 -07001091
Mathias Agopian13127d82013-03-05 17:47:11 -08001092bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001093 mCurrentState.requestedTransparentRegion_legacy = transparent;
1094 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001095 setTransactionFlags(eTransactionNeeded);
1096 return true;
1097}
1098bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001099 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1100 if (mCurrentState.flags == newFlags) return false;
1101 mCurrentState.sequence++;
1102 mCurrentState.flags = newFlags;
1103 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001104 setTransactionFlags(eTransactionNeeded);
1105 return true;
1106}
Robert Carr99e27f02016-06-16 15:18:02 -07001107
Marissa Wallf58c14b2018-07-24 10:50:43 -07001108bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001109 if (mCurrentState.requestedCrop_legacy == crop) return false;
1110 mCurrentState.sequence++;
1111 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001112 if (immediate && !mFreezeGeometryUpdates) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001113 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001114 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001115 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1116
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001117 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001118 setTransactionFlags(eTransactionNeeded);
1119 return true;
1120}
Robert Carr8d5227b2017-03-16 15:41:03 -07001121
Robert Carrc3574f72016-03-24 12:19:32 -07001122bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001123 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001124 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001125 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001126 return true;
1127}
1128
Evan Roskyef876c92019-01-25 17:46:06 -08001129bool Layer::setMetadata(const LayerMetadata& data) {
1130 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001131 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001132 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001133 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001134 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001135}
1136
Mathias Agopian13127d82013-03-05 17:47:11 -08001137bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001138 if (mCurrentState.layerStack == layerStack) return false;
1139 mCurrentState.sequence++;
1140 mCurrentState.layerStack = layerStack;
1141 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001142 setTransactionFlags(eTransactionNeeded);
1143 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001144}
1145
Peiyong Linc502cb72019-03-01 15:00:23 -08001146bool Layer::setColorSpaceAgnostic(const bool agnostic) {
1147 if (mCurrentState.colorSpaceAgnostic == agnostic) {
1148 return false;
1149 }
1150 mCurrentState.sequence++;
1151 mCurrentState.colorSpaceAgnostic = agnostic;
1152 mCurrentState.modified = true;
1153 setTransactionFlags(eTransactionNeeded);
1154 return true;
1155}
1156
Robert Carr1f0a16a2016-10-24 16:27:39 -07001157uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001158 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001159 if (p == nullptr) {
1160 return getDrawingState().layerStack;
1161 }
1162 return p->getLayerStack();
1163}
1164
Marissa Wallf58c14b2018-07-24 10:50:43 -07001165void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001166 mCurrentState.barrierLayer_legacy = barrierLayer;
1167 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001168 // We don't set eTransactionNeeded, because just receiving a deferral
1169 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001170 mCurrentState.modified = true;
1171 pushPendingState();
1172 mCurrentState.barrierLayer_legacy = nullptr;
1173 mCurrentState.frameNumber_legacy = 0;
1174 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001175}
1176
Marissa Wallf58c14b2018-07-24 10:50:43 -07001177void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001178 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001179 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001180}
1181
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001182// ----------------------------------------------------------------------------
1183// pageflip handling...
1184// ----------------------------------------------------------------------------
1185
Robert Carr1f0a16a2016-10-24 16:27:39 -07001186bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001187 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001188 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001189 if (parent != nullptr && parent->isHiddenByPolicy()) {
1190 return true;
1191 }
1192 return s.flags & layer_state_t::eLayerHidden;
1193}
1194
David Sodman41fdfc92017-11-06 16:09:56 -08001195uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001196 // TODO: should we do something special if mSecure is set?
1197 if (mProtectedByApp) {
1198 // need a hardware-protected path to external video sink
1199 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001200 }
Riley Andrews03414a12014-07-01 14:22:59 -07001201 if (mPotentialCursor) {
1202 usage |= GraphicBuffer::USAGE_CURSOR;
1203 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001204 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001205 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001206}
1207
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001208void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001209 uint32_t orientation = 0;
Vishnu Nairf2deb822018-11-12 17:53:48 -08001210 // Disable setting transform hint if the debug flag is set or if the
1211 // getTransformToDisplayInverse flag is set and the client wants to submit buffers
1212 // in one orientation.
1213 if (!mFlinger->mDebugDisableTransformHint && !getTransformToDisplayInverse()) {
Mathias Agopian84300952012-11-21 16:02:13 -08001214 // The transform hint is used to improve performance, but we can
1215 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001216 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001217 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001218 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001219 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001220 orientation = 0;
1221 }
1222 }
David Sodmaneb085e02017-10-05 18:49:04 -07001223 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001224}
1225
Mathias Agopian13127d82013-03-05 17:47:11 -08001226// ----------------------------------------------------------------------------
1227// debugging
1228// ----------------------------------------------------------------------------
1229
Marissa Wall61c58622018-07-18 10:12:20 -07001230// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001231LayerDebugInfo Layer::getLayerDebugInfo() const {
1232 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001233 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001234 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001235 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001236 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
Yiwei Zhang5434a782018-12-05 18:06:32 -08001237 info.mType = std::string(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001238 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001239 info.mVisibleRegion = visibleRegion;
1240 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001241 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001242 info.mX = ds.active_legacy.transform.tx();
1243 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001244 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001245 info.mWidth = ds.active_legacy.w;
1246 info.mHeight = ds.active_legacy.h;
1247 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001248 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001249 info.mFlags = ds.flags;
1250 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001251 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001252 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1253 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1254 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1255 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001256 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001257 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001258 if (buffer != 0) {
1259 info.mActiveBufferWidth = buffer->getWidth();
1260 info.mActiveBufferHeight = buffer->getHeight();
1261 info.mActiveBufferStride = buffer->getStride();
1262 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001263 } else {
1264 info.mActiveBufferWidth = 0;
1265 info.mActiveBufferHeight = 0;
1266 info.mActiveBufferStride = 0;
1267 info.mActiveBufferFormat = 0;
1268 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001269 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001270 info.mNumQueuedFrames = getQueuedFrameCount();
1271 info.mRefreshPending = isBufferLatched();
1272 info.mIsOpaque = isOpaque(ds);
1273 info.mContentDirty = contentDirty;
1274 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001275}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001276
Yiwei Zhang5434a782018-12-05 18:06:32 -08001277void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001278 result.append("-------------------------------");
1279 result.append("-------------------------------");
1280 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001281 result.append(" Layer name\n");
1282 result.append(" Z | ");
1283 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001284 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001285 result.append(" Disp Frame (LTRB) | ");
1286 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001287 result.append("-------------------------------");
1288 result.append("-------------------------------");
1289 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001290}
1291
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001292void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1293 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1294 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001295 return;
1296 }
1297
Yiwei Zhang5434a782018-12-05 18:06:32 -08001298 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001299 if (mName.length() > 77) {
1300 std::string shortened;
1301 shortened.append(mName.string(), 36);
1302 shortened.append("[...]");
1303 shortened.append(mName.string() + (mName.length() - 36), 36);
Yiwei Zhang5434a782018-12-05 18:06:32 -08001304 name = shortened;
Dan Stozae22aec72016-08-01 13:20:59 -07001305 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001306 name = std::string(mName.string(), mName.size());
Dan Stozae22aec72016-08-01 13:20:59 -07001307 }
1308
Yiwei Zhang5434a782018-12-05 18:06:32 -08001309 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001310
Alec Mourib416efd2018-09-06 21:01:59 +00001311 const State& layerState(getDrawingState());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001312 const auto& compositionState = outputLayer->getState();
1313
Chia-I Wu1e043612018-03-01 09:45:09 -08001314 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001315 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001316 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001317 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001318 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001319 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
1320 StringAppendF(&result, "%10s | ",
1321 toString(getCompositionLayer() ? compositionState.bufferTransform
1322 : static_cast<Hwc2::Transform>(0))
1323 .c_str());
1324 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001325 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001326 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001327 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1328 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001329
Yichi Chen6ca35192018-05-29 12:20:43 +08001330 result.append("- - - - - - - - - - - - - - - -");
1331 result.append("- - - - - - - - - - - - - - - -");
1332 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001333}
Dan Stozae22aec72016-08-01 13:20:59 -07001334
Yiwei Zhang5434a782018-12-05 18:06:32 -08001335void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001336 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001337}
1338
Svetoslavd85084b2014-03-20 10:28:31 -07001339void Layer::clearFrameStats() {
1340 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001341}
1342
Jamie Gennis6547ff42013-07-16 20:12:42 -07001343void Layer::logFrameStats() {
1344 mFrameTracker.logAndResetStats(mName);
1345}
1346
Svetoslavd85084b2014-03-20 10:28:31 -07001347void Layer::getFrameStats(FrameStats* outStats) const {
1348 mFrameTracker.getStats(outStats);
1349}
1350
Yiwei Zhang5434a782018-12-05 18:06:32 -08001351void Layer::dumpFrameEvents(std::string& result) {
1352 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001353 Mutex::Autolock lock(mFrameEventHistoryMutex);
1354 mFrameEventHistory.checkFencesForCompletion();
1355 mFrameEventHistory.dump(result);
1356}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001357
Brian Anderson5ea5e592016-12-01 16:54:33 -08001358void Layer::onDisconnect() {
1359 Mutex::Autolock lock(mFrameEventHistoryMutex);
1360 mFrameEventHistory.onDisconnect();
Yiwei Zhangaf8ee942018-11-22 00:15:23 -08001361 mFlinger->mTimeStats->onDestroy(getSequence());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001362}
1363
Brian Anderson3890c392016-07-25 12:48:08 -07001364void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001365 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001366 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001367 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1368 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001369 }
1370
Brian Andersond6927fb2016-07-23 23:37:30 -07001371 Mutex::Autolock lock(mFrameEventHistoryMutex);
1372 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001373 // If there are any unsignaled fences in the aquire timeline at this
1374 // point, the previously queued frame hasn't been latched yet. Go ahead
1375 // and try to get the signal time here so the syscall is taken out of
1376 // the main thread's critical path.
1377 mAcquireTimeline.updateSignalTimes();
1378 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001379 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001380 mFrameEventHistory.addQueue(*newTimestamps);
1381 }
1382
Brian Anderson3890c392016-07-25 12:48:08 -07001383 if (outDelta) {
1384 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001385 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001386}
Dan Stozae77c7662016-05-13 11:37:28 -07001387
Chia-I Wu98f1c102017-05-30 14:54:08 -07001388size_t Layer::getChildrenCount() const {
1389 size_t count = 0;
1390 for (const sp<Layer>& child : mCurrentChildren) {
1391 count += 1 + child->getChildrenCount();
1392 }
1393 return count;
1394}
1395
Robert Carr1f0a16a2016-10-24 16:27:39 -07001396void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001397 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001398 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001399
Robert Carr1f0a16a2016-10-24 16:27:39 -07001400 mCurrentChildren.add(layer);
1401 layer->setParent(this);
1402}
1403
1404ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001405 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001406 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001407
Robert Carr1323c952019-01-28 18:13:27 -08001408 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001409 return mCurrentChildren.remove(layer);
1410}
1411
Robert Carr1db73f62016-12-21 12:58:51 -08001412bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1413 sp<Handle> handle = nullptr;
1414 sp<Layer> newParent = nullptr;
1415 if (newParentHandle == nullptr) {
1416 return false;
1417 }
1418 handle = static_cast<Handle*>(newParentHandle.get());
1419 newParent = handle->owner.promote();
1420 if (newParent == nullptr) {
1421 ALOGE("Unable to promote Layer handle");
1422 return false;
1423 }
1424
chaviw5aedec92018-10-22 10:40:38 -07001425 if (attachChildren()) {
1426 setTransactionFlags(eTransactionNeeded);
1427 }
Robert Carr1db73f62016-12-21 12:58:51 -08001428 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001429 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001430 }
1431 mCurrentChildren.clear();
1432
1433 return true;
1434}
1435
Robert Carr15eae092018-03-23 13:43:53 -07001436void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001437 for (const sp<Layer>& child : mDrawingChildren) {
1438 child->mDrawingParent = newParent;
Vishnu Nair4351ad52019-02-11 14:13:02 -08001439 child->computeBounds(newParent->mBounds, newParent->getTransformWithScale());
Robert Carr578038f2018-03-09 12:25:24 -08001440 }
1441}
1442
chaviwf1961f72017-09-18 16:41:07 -07001443bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001444 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001445
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001446 // While layers are detached, we allow most operations
1447 // and simply halt performing the actual transaction. However
1448 // for reparent != null we would enter the mRemovedFromCurrentState
1449 // state, regardless of whether doTransaction was called, and
1450 // so we need to prevent the update here.
1451 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001452 return false;
1453 }
1454
Robert Carr54cf5b12019-01-25 14:02:28 -08001455 sp<Layer> newParent;
1456 if (newParentHandle != nullptr) {
1457 auto handle = static_cast<Handle*>(newParentHandle.get());
1458 newParent = handle->owner.promote();
1459 if (newParent == nullptr) {
1460 ALOGE("Unable to promote Layer handle");
1461 return false;
1462 }
1463 if (newParent == this) {
1464 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1465 return false;
1466 }
1467 }
1468
chaviwf1961f72017-09-18 16:41:07 -07001469 sp<Layer> parent = getParent();
1470 if (parent != nullptr) {
1471 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001472 }
1473
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001474 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001475 newParent->addChild(this);
1476 if (!newParent->isRemovedFromCurrentState()) {
1477 addToCurrentState();
1478 } else {
1479 onRemovedFromCurrentState();
1480 }
1481
1482 if (mLayerDetached) {
1483 mLayerDetached = false;
1484 callSetTransactionFlags = true;
1485 }
1486 } else {
1487 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001488 }
1489
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001490 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001491 setTransactionFlags(eTransactionNeeded);
1492 }
chaviw06178942017-07-27 10:25:59 -07001493 return true;
1494}
1495
Robert Carr9524cb32017-02-13 11:32:32 -08001496bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001497 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001498 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001499 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001500 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001501 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001502 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001503 }
Robert Carr7f619b22017-11-06 12:56:35 -08001504 }
Robert Carr9524cb32017-02-13 11:32:32 -08001505
1506 return true;
1507}
1508
chaviw5aedec92018-10-22 10:40:38 -07001509bool Layer::attachChildren() {
1510 bool changed = false;
1511 for (const sp<Layer>& child : mCurrentChildren) {
1512 sp<Client> parentClient = mClientRef.promote();
1513 sp<Client> client(child->mClientRef.promote());
1514 if (client != nullptr && parentClient != client) {
1515 if (child->mLayerDetached) {
1516 child->mLayerDetached = false;
1517 changed = true;
1518 }
1519 changed |= child->attachChildren();
1520 }
1521 }
1522
1523 return changed;
1524}
1525
Peiyong Lind3788632018-09-18 16:01:31 -07001526bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001527 static const mat4 identityMatrix = mat4();
1528
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001529 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001530 return false;
1531 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001532 ++mCurrentState.sequence;
1533 mCurrentState.colorTransform = matrix;
1534 mCurrentState.hasColorTransform = matrix != identityMatrix;
1535 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001536 setTransactionFlags(eTransactionNeeded);
1537 return true;
1538}
1539
chaviwf66724d2018-11-28 16:35:21 -08001540mat4 Layer::getColorTransform() const {
1541 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1542 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1543 colorTransform = parent->getColorTransform() * colorTransform;
1544 }
1545 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001546}
1547
1548bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001549 bool hasColorTransform = getDrawingState().hasColorTransform;
1550 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1551 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1552 }
1553 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001554}
1555
Chia-I Wu11481472018-05-04 10:43:19 -07001556bool Layer::isLegacyDataSpace() const {
1557 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001558 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001559 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001560}
1561
Robert Carr1f0a16a2016-10-24 16:27:39 -07001562void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001563 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001564}
1565
Robert Carr1f0a16a2016-10-24 16:27:39 -07001566int32_t Layer::getZ() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001567 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001568}
1569
Robert Carr29abff82017-12-04 13:51:20 -08001570bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1571 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001572 const State& state = useDrawing ? mDrawingState : mCurrentState;
Robert Carr29abff82017-12-04 13:51:20 -08001573 return state.zOrderRelativeOf != nullptr;
1574}
1575
David Sodman41fdfc92017-11-06 16:09:56 -08001576__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001577 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001578 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1579 "makeTraversalList received invalid stateSet");
1580 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1581 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001582 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001583
Robert Carr29abff82017-12-04 13:51:20 -08001584 if (state.zOrderRelatives.size() == 0) {
1585 *outSkipRelativeZUsers = true;
1586 return children;
1587 }
1588
chaviwfd462612018-05-31 16:11:27 -07001589 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001590 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001591 sp<Layer> strongRelative = weakRelative.promote();
1592 if (strongRelative != nullptr) {
1593 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001594 }
1595 }
1596
Dan Stoza412903f2017-04-27 13:42:17 -07001597 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001598 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
Robert Carr503c7042017-09-27 15:06:08 -07001599 if (childState.zOrderRelativeOf != nullptr) {
1600 continue;
1601 }
Robert Carrdb66e622017-04-10 16:55:57 -07001602 traverse.add(child);
1603 }
1604
1605 return traverse;
1606}
1607
Robert Carr1f0a16a2016-10-24 16:27:39 -07001608/**
Robert Carrdb66e622017-04-10 16:55:57 -07001609 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001610 */
Dan Stoza412903f2017-04-27 13:42:17 -07001611void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001612 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1613 // produce a new list for traversing, including our relatives, and not including our children
1614 // who are relatives of another surface. In the case that there are no relative Z,
1615 // makeTraversalList returns our children directly to avoid significant overhead.
1616 // However in this case we need to take the responsibility for filtering children which
1617 // are relatives of another surface here.
1618 bool skipRelativeZUsers = false;
1619 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001620
Robert Carr1f0a16a2016-10-24 16:27:39 -07001621 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001622 for (; i < list.size(); i++) {
1623 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001624 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1625 continue;
1626 }
1627
Robert Carrdb66e622017-04-10 16:55:57 -07001628 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001629 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001630 }
Dan Stoza412903f2017-04-27 13:42:17 -07001631 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001632 }
Robert Carr29abff82017-12-04 13:51:20 -08001633
Dan Stoza412903f2017-04-27 13:42:17 -07001634 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001635 for (; i < list.size(); i++) {
1636 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001637
Robert Carr29abff82017-12-04 13:51:20 -08001638 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1639 continue;
1640 }
Dan Stoza412903f2017-04-27 13:42:17 -07001641 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001642 }
1643}
1644
1645/**
Robert Carrdb66e622017-04-10 16:55:57 -07001646 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001647 */
Dan Stoza412903f2017-04-27 13:42:17 -07001648void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1649 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001650 // See traverseInZOrder for documentation.
1651 bool skipRelativeZUsers = false;
1652 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001653
Robert Carr1f0a16a2016-10-24 16:27:39 -07001654 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001655 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001656 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001657
1658 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1659 continue;
1660 }
1661
Robert Carrdb66e622017-04-10 16:55:57 -07001662 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001663 break;
1664 }
Dan Stoza412903f2017-04-27 13:42:17 -07001665 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001666 }
Dan Stoza412903f2017-04-27 13:42:17 -07001667 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001668 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001669 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001670
1671 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1672 continue;
1673 }
1674
Dan Stoza412903f2017-04-27 13:42:17 -07001675 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001676 }
1677}
1678
chaviw4b129c22018-04-09 16:19:43 -07001679LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1680 const std::vector<Layer*>& layersInTree) {
1681 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1682 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001683 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1684 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001685 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001686
chaviwfd462612018-05-31 16:11:27 -07001687 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001688 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1689 sp<Layer> strongRelative = weakRelative.promote();
1690 // Only add relative layers that are also descendents of the top most parent of the tree.
1691 // If a relative layer is not a descendent, then it should be ignored.
1692 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1693 traverse.add(strongRelative);
1694 }
1695 }
1696
1697 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001698 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001699 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1700 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1701 // the child here since it won't be added later as a relative.
1702 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1703 childState.zOrderRelativeOf.promote().get())) {
1704 continue;
1705 }
1706 traverse.add(child);
1707 }
1708
1709 return traverse;
1710}
1711
1712void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1713 LayerVector::StateSet stateSet,
1714 const LayerVector::Visitor& visitor) {
1715 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001716
1717 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001718 for (; i < list.size(); i++) {
1719 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001720 if (relative->getZ() >= 0) {
1721 break;
1722 }
chaviw4b129c22018-04-09 16:19:43 -07001723 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001724 }
chaviw4b129c22018-04-09 16:19:43 -07001725
chaviwa76b2712017-09-20 12:02:26 -07001726 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001727 for (; i < list.size(); i++) {
1728 const auto& relative = list[i];
1729 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001730 }
1731}
1732
chaviw4b129c22018-04-09 16:19:43 -07001733std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1734 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1735 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1736
1737 std::vector<Layer*> layersInTree = {this};
1738 for (size_t i = 0; i < children.size(); i++) {
1739 const auto& child = children[i];
1740 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1741 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1742 }
1743
1744 return layersInTree;
1745}
1746
1747void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1748 const LayerVector::Visitor& visitor) {
1749 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1750 std::sort(layersInTree.begin(), layersInTree.end());
1751 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1752}
1753
Peiyong Linefefaac2018-08-17 12:27:51 -07001754ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001755 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001756}
1757
chaviw13fdc492017-06-27 12:40:18 -07001758half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001759 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001760
chaviw13fdc492017-06-27 12:40:18 -07001761 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1762 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001763}
Robert Carr6452f122017-03-21 10:41:29 -07001764
chaviw13fdc492017-06-27 12:40:18 -07001765half4 Layer::getColor() const {
1766 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001767 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001768}
Robert Carr6452f122017-03-21 10:41:29 -07001769
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001770Layer::RoundedCornerState Layer::getRoundedCornerState() const {
1771 const auto& p = mDrawingParent.promote();
1772 if (p != nullptr) {
Peiyong Lin27016a92019-03-29 17:36:08 +00001773 RoundedCornerState parentState = p->getRoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001774 if (parentState.radius > 0) {
1775 ui::Transform t = getActiveTransform(getDrawingState());
1776 t = t.inverse();
1777 parentState.cropRect = t.transform(parentState.cropRect);
1778 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1779 // but a transform matrix can define horizontal and vertical scales.
1780 // Let's take the average between both of them and pass into the shader, practically we
1781 // never do this type of transformation on windows anyway.
1782 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
1783 return parentState;
1784 }
1785 }
1786 const float radius = getDrawingState().cornerRadius;
Peiyong Lin27016a92019-03-29 17:36:08 +00001787 return radius > 0 ? RoundedCornerState(getBounds(), radius) : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001788}
1789
Robert Carr1f0a16a2016-10-24 16:27:39 -07001790void Layer::commitChildList() {
1791 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1792 const auto& child = mCurrentChildren[i];
1793 child->commitChildList();
1794 }
1795 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001796 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001797}
1798
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001799static wp<Layer> extractLayerFromBinder(const wp<IBinder>& weakBinderHandle) {
1800 if (weakBinderHandle == nullptr) {
1801 return nullptr;
1802 }
1803 sp<IBinder> binderHandle = weakBinderHandle.promote();
1804 if (binderHandle == nullptr) {
1805 return nullptr;
1806 }
1807 sp<Layer::Handle> handle = static_cast<Layer::Handle*>(binderHandle.get());
1808 if (handle == nullptr) {
1809 return nullptr;
1810 }
1811 return handle->owner;
1812}
1813
Robert Carr720e5062018-07-30 17:45:14 -07001814void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001815 mCurrentState.inputInfo = info;
Vishnu Nair6fabeec2019-03-12 13:42:49 -07001816 mCurrentState.touchableRegionCrop = extractLayerFromBinder(info.touchableRegionCropHandle);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001817 mCurrentState.modified = true;
1818 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07001819 setTransactionFlags(eTransactionNeeded);
1820}
1821
chaviw1d044282017-09-27 12:19:28 -07001822void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
1823 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1824 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001825 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07001826
Peiyong Linefefaac2018-08-17 12:27:51 -07001827 ui::Transform requestedTransform = state.active_legacy.transform;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001828 ui::Transform transform = getTransform();
chaviw1d044282017-09-27 12:19:28 -07001829
1830 layerInfo->set_id(sequence);
1831 layerInfo->set_name(getName().c_str());
1832 layerInfo->set_type(String8(getTypeId()));
1833
1834 for (const auto& child : children) {
1835 layerInfo->add_children(child->sequence);
1836 }
1837
1838 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1839 sp<Layer> strongRelative = weakRelative.promote();
1840 if (strongRelative != nullptr) {
1841 layerInfo->add_relatives(strongRelative->sequence);
1842 }
1843 }
1844
Marissa Wallf58c14b2018-07-24 10:50:43 -07001845 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
Nataniel Borges797b0e42019-02-15 14:11:58 -08001846 [&]() { return layerInfo->mutable_transparent_region(); });
1847 LayerProtoHelper::writeToProto(visibleRegion,
1848 [&]() { return layerInfo->mutable_visible_region(); });
1849 LayerProtoHelper::writeToProto(surfaceDamageRegion,
1850 [&]() { return layerInfo->mutable_damage_region(); });
chaviw1d044282017-09-27 12:19:28 -07001851
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001852 layerInfo->set_layer_stack(getLayerStack());
chaviw1d044282017-09-27 12:19:28 -07001853 layerInfo->set_z(state.z);
1854
Nataniel Borges797b0e42019-02-15 14:11:58 -08001855 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
1856 [&]() { return layerInfo->mutable_position(); });
chaviw1d044282017-09-27 12:19:28 -07001857
Nataniel Borges797b0e42019-02-15 14:11:58 -08001858 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(), [&]() {
1859 return layerInfo->mutable_requested_position();
1860 });
chaviw1d044282017-09-27 12:19:28 -07001861
Nataniel Borges797b0e42019-02-15 14:11:58 -08001862 LayerProtoHelper::writeSizeToProto(state.active_legacy.w, state.active_legacy.h,
1863 [&]() { return layerInfo->mutable_size(); });
chaviw1d044282017-09-27 12:19:28 -07001864
Nataniel Borges797b0e42019-02-15 14:11:58 -08001865 LayerProtoHelper::writeToProto(state.crop_legacy, [&]() { return layerInfo->mutable_crop(); });
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001866 layerInfo->set_corner_radius(getRoundedCornerState().radius);
chaviw1d044282017-09-27 12:19:28 -07001867
1868 layerInfo->set_is_opaque(isOpaque(state));
1869 layerInfo->set_invalidate(contentDirty);
Chia-I Wu01591c92018-05-22 12:03:00 -07001870
1871 // XXX (b/79210409) mCurrentDataSpace is not protected
1872 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
1873
chaviw1d044282017-09-27 12:19:28 -07001874 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
Nataniel Borges797b0e42019-02-15 14:11:58 -08001875 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
1876 LayerProtoHelper::writeToProto(state.color,
1877 [&]() { return layerInfo->mutable_requested_color(); });
chaviw1d044282017-09-27 12:19:28 -07001878 layerInfo->set_flags(state.flags);
1879
1880 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1881 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
1882
Jorim Jaggi8e0af362017-11-14 16:28:28 +01001883 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07001884 if (parent != nullptr) {
1885 layerInfo->set_parent(parent->sequence);
Nataniel Borgesdcc0bab2019-02-15 13:22:03 -08001886 } else {
1887 layerInfo->set_parent(-1);
chaviw1d044282017-09-27 12:19:28 -07001888 }
1889
1890 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1891 if (zOrderRelativeOf != nullptr) {
1892 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
Nataniel Borgesdcc0bab2019-02-15 13:22:03 -08001893 } else {
1894 layerInfo->set_z_order_relative_of(-1);
chaviw1d044282017-09-27 12:19:28 -07001895 }
1896
Lloyd Pique0b785d82018-12-04 17:25:27 -08001897 auto buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001898 if (buffer != nullptr) {
Nataniel Borges797b0e42019-02-15 14:11:58 -08001899 LayerProtoHelper::writeToProto(buffer,
1900 [&]() { return layerInfo->mutable_active_buffer(); });
Peiyong Linefefaac2018-08-17 12:27:51 -07001901 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
Yichi Chen6ca35192018-05-29 12:20:43 +08001902 layerInfo->mutable_buffer_transform());
chaviw1d044282017-09-27 12:19:28 -07001903 }
1904
1905 layerInfo->set_queued_frames(getQueuedFrameCount());
1906 layerInfo->set_refresh_pending(isBufferLatched());
chaviwadc40c22018-07-10 16:57:27 -07001907 layerInfo->set_curr_frame(mCurrentFrameNumber);
Vishnu Nairf2deb822018-11-12 17:53:48 -08001908 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
chaviwadc40c22018-07-10 16:57:27 -07001909
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001910 for (const auto& pendingState : mPendingStates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001911 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
chaviwadc40c22018-07-10 16:57:27 -07001912 if (barrierLayer != nullptr) {
1913 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
1914 barrierLayerProto->set_id(barrierLayer->sequence);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001915 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
chaviwadc40c22018-07-10 16:57:27 -07001916 }
1917 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001918
1919 auto protoMap = layerInfo->mutable_metadata();
1920 for (const auto& entry : state.metadata.mMap) {
1921 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
1922 }
Vishnu Nair4351ad52019-02-11 14:13:02 -08001923 LayerProtoHelper::writeToProto(mEffectiveTransform, layerInfo->mutable_effective_transform());
Nataniel Borges797b0e42019-02-15 14:11:58 -08001924 LayerProtoHelper::writeToProto(mSourceBounds,
1925 [&]() { return layerInfo->mutable_source_bounds(); });
1926 LayerProtoHelper::writeToProto(mScreenBounds,
1927 [&]() { return layerInfo->mutable_screen_bounds(); });
1928 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
chaviw1d044282017-09-27 12:19:28 -07001929}
1930
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001931void Layer::writeToProto(LayerProto* layerInfo, const sp<DisplayDevice>& displayDevice) {
1932 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1933 if (!outputLayer) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07001934 return;
1935 }
1936
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001937 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
1938
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001939 const auto& compositionState = outputLayer->getState();
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001940
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001941 const Rect& frame = compositionState.displayFrame;
Nataniel Borges797b0e42019-02-15 14:11:58 -08001942 LayerProtoHelper::writeToProto(frame, [&]() { return layerInfo->mutable_hwc_frame(); });
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001943
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001944 const FloatRect& crop = compositionState.sourceCrop;
Nataniel Borges797b0e42019-02-15 14:11:58 -08001945 LayerProtoHelper::writeToProto(crop, [&]() { return layerInfo->mutable_hwc_crop(); });
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001946
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001947 const int32_t transform =
1948 getCompositionLayer() ? static_cast<int32_t>(compositionState.bufferTransform) : 0;
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001949 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08001950
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001951 const int32_t compositionType =
1952 static_cast<int32_t>(compositionState.hwc ? (*compositionState.hwc).hwcCompositionType
1953 : Hwc2::IComposerClient::Composition::CLIENT);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08001954 layerInfo->set_hwc_composition_type(compositionType);
1955
1956 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
1957 static_cast<BufferLayer*>(this)->isProtected()) {
1958 layerInfo->set_is_protected(true);
1959 } else {
1960 layerInfo->set_is_protected(false);
1961 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001962}
1963
Robert Carr2e102c92018-10-23 12:11:15 -07001964bool Layer::isRemovedFromCurrentState() const {
1965 return mRemovedFromCurrentState;
1966}
1967
Arthur Hungd20b2702019-01-14 18:16:16 +08001968InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001969 InputWindowInfo info = mDrawingState.inputInfo;
Robert Carr720e5062018-07-30 17:45:14 -07001970
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001971 if (info.displayId == ADISPLAY_ID_NONE) {
1972 info.displayId = mDrawingState.layerStack;
1973 }
1974
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001975 ui::Transform t = getTransform();
1976 const float xScale = t.sx();
1977 const float yScale = t.sy();
1978 if (xScale != 1.0f || yScale != 1.0f) {
1979 info.windowXScale *= 1.0f / xScale;
1980 info.windowYScale *= 1.0f / yScale;
1981 info.touchableRegion.scaleSelf(xScale, yScale);
Riddle Hsu39d4aa52018-11-30 20:46:53 +08001982 }
Robert Carre07e1032018-11-26 12:55:53 -08001983
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001984 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08001985 // If this is a portal window, set the touchableRegion to the layerBounds.
1986 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
1987 ? getBufferSize(getDrawingState())
1988 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08001989 if (!layerBounds.isValid()) {
1990 layerBounds = getCroppedBufferSize(getDrawingState());
1991 }
Vishnu Nair8033a492018-12-05 07:27:23 -08001992 layerBounds = t.transform(layerBounds);
1993 layerBounds.inset(info.surfaceInset, info.surfaceInset, info.surfaceInset, info.surfaceInset);
1994
Arthur Hungd20b2702019-01-14 18:16:16 +08001995 // Input coordinate should match the layer bounds.
1996 info.frameLeft = layerBounds.left;
1997 info.frameTop = layerBounds.top;
1998 info.frameRight = layerBounds.right;
1999 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08002000
2001 // Position the touchable region relative to frame screen location and restrict it to frame
2002 // bounds.
2003 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08002004 info.visible = canReceiveInput();
Vishnu Nair6fabeec2019-03-12 13:42:49 -07002005
2006 auto cropLayer = mDrawingState.touchableRegionCrop.promote();
2007 if (info.replaceTouchableRegionWithCrop) {
2008 if (cropLayer == nullptr) {
2009 info.touchableRegion = Region(Rect{mScreenBounds});
2010 } else {
2011 info.touchableRegion = Region(Rect{cropLayer->mScreenBounds});
2012 }
2013 } else if (cropLayer != nullptr) {
2014 info.touchableRegion = info.touchableRegion.intersect(Rect{cropLayer->mScreenBounds});
2015 }
2016
Robert Carr720e5062018-07-30 17:45:14 -07002017 return info;
2018}
2019
2020bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002021 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002022}
2023
Lloyd Piquefeb73d72018-12-04 17:23:44 -08002024std::shared_ptr<compositionengine::Layer> Layer::getCompositionLayer() const {
2025 return nullptr;
2026}
2027
chaviw3e727cd2019-01-31 13:41:05 -08002028bool Layer::canReceiveInput() const {
2029 return isVisible();
2030}
2031
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002032compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
2033 const sp<const DisplayDevice>& display) const {
2034 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionLayer().get());
2035}
2036
Mathias Agopian13127d82013-03-05 17:47:11 -08002037// ---------------------------------------------------------------------------
2038
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002039}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002040
2041#if defined(__gl_h_)
2042#error "don't include gl/gl.h in this file"
2043#endif
2044
2045#if defined(__gl2_h_)
2046#error "don't include gl2/gl2.h in this file"
2047#endif