blob: c7fb45959a0b3d035f40892774cf53b6d5f5934e [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 Pique37c2c9b2018-12-04 17:25:10 -080032#include <compositionengine/OutputLayer.h>
Lloyd Pique0b785d82018-12-04 17:25:27 -080033#include <compositionengine/impl/LayerCompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080034#include <compositionengine/impl/OutputLayerCompositionState.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070035#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070036#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070037#include <cutils/properties.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080038#include <gui/BufferItem.h>
39#include <gui/LayerDebugInfo.h>
40#include <gui/Surface.h>
41#include <renderengine/RenderEngine.h>
42#include <ui/DebugUtils.h>
43#include <ui/GraphicBuffer.h>
44#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045#include <utils/Errors.h>
46#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080047#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080049#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050
Yiwei Zhang60d1a192018-03-07 14:52:28 -080051#include "BufferLayer.h"
Valerie Haudd0b7572019-01-29 14:59:27 -080052#include "ColorLayer.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020053#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070054#include "DisplayDevice.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080055#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056#include "Layer.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080057#include "LayerProtoHelper.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070058#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070059#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060#include "SurfaceFlinger.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080061#include "TimeStats/TimeStats.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070062
David Sodman41fdfc92017-11-06 16:09:56 -080063#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065namespace android {
66
Yiwei Zhang5434a782018-12-05 18:06:32 -080067using base::StringAppendF;
68
Lloyd Piquef1c675b2018-09-12 20:45:39 -070069std::atomic<int32_t> Layer::sSequence{1};
Mathias Agopian13127d82013-03-05 17:47:11 -080070
Lloyd Pique42ab75e2018-09-12 20:46:03 -070071Layer::Layer(const LayerCreationArgs& args)
Lloyd Piquefa8fd6b2019-01-29 18:42:24 -080072 : mFlinger(args.flinger), mName(args.name), mClientRef(args.client) {
Mathias Agopiana67932f2011-04-20 14:20:59 -070073 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070074
75 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -070076 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
77 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
78 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070079
Dan Stozaf7ba41a2017-05-10 15:11:11 -070080 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070081
Lloyd Pique0449b0f2018-12-20 16:23:45 -080082 mCurrentState.active_legacy.w = args.w;
83 mCurrentState.active_legacy.h = args.h;
84 mCurrentState.flags = layerFlags;
85 mCurrentState.active_legacy.transform.set(0, 0);
86 mCurrentState.crop_legacy.makeInvalid();
87 mCurrentState.requestedCrop_legacy = mCurrentState.crop_legacy;
88 mCurrentState.z = 0;
89 mCurrentState.color.a = 1.0f;
90 mCurrentState.layerStack = 0;
91 mCurrentState.sequence = 0;
92 mCurrentState.requested_legacy = mCurrentState.active_legacy;
Lloyd Pique0449b0f2018-12-20 16:23:45 -080093 mCurrentState.active.w = UINT32_MAX;
94 mCurrentState.active.h = UINT32_MAX;
95 mCurrentState.active.transform.set(0, 0);
96 mCurrentState.transform = 0;
97 mCurrentState.transformToDisplayInverse = false;
98 mCurrentState.crop.makeInvalid();
99 mCurrentState.acquireFence = new Fence(-1);
100 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
101 mCurrentState.hdrMetadata.validTypes = 0;
102 mCurrentState.surfaceDamageRegion.clear();
103 mCurrentState.cornerRadius = 0.0f;
104 mCurrentState.api = -1;
105 mCurrentState.hasColorTransform = false;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700106
107 // drawing state & current state are identical
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800108 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700109
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800110 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700111 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800112 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200113 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700114
115 mFlinger->onLayerCreated();
Dan Stoza436ccf32018-06-21 12:10:12 -0700116}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700117
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700118Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800119 sp<Client> c(mClientRef.promote());
120 if (c != 0) {
121 c->detachLayer(this);
122 }
123
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000124 mFrameTracker.logAndResetStats(mName);
Robert Carr2e102c92018-10-23 12:11:15 -0700125
Robert Carr2e102c92018-10-23 12:11:15 -0700126 mFlinger->onLayerDestroyed();
Mathias Agopian96f08192010-06-02 23:28:45 -0700127}
128
Mathias Agopian13127d82013-03-05 17:47:11 -0800129// ---------------------------------------------------------------------------
130// callbacks
131// ---------------------------------------------------------------------------
132
David Sodmaneb085e02017-10-05 18:49:04 -0700133/*
134 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
135 * Layer. So, the implementation is done in BufferLayer. When called on a
136 * ColorLayer object, it's essentially a NOP.
137 */
David Sodmaneb085e02017-10-05 18:49:04 -0700138void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800139
Chia-I Wuc6657022017-08-15 11:18:17 -0700140void Layer::onRemovedFromCurrentState() {
Robert Carr2e102c92018-10-23 12:11:15 -0700141 mRemovedFromCurrentState = true;
142
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800143 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
144 if (mCurrentState.zOrderRelativeOf != nullptr) {
145 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
146 if (strongRelative != nullptr) {
147 strongRelative->removeZOrderRelative(this);
148 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700149 }
chaviw606e5cf2019-03-01 10:12:10 -0800150 setZOrderRelativeOf(nullptr);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700151 }
Rob Carr4bba3702018-10-08 21:53:30 +0000152
Robert Carr2e102c92018-10-23 12:11:15 -0700153 // Since we are no longer reachable from CurrentState SurfaceFlinger
154 // will no longer invoke doTransaction for us, and so we will
155 // never finish applying transactions. We signal the sync point
156 // now so that another layer will not become indefinitely
157 // blocked.
158 for (auto& point: mRemoteSyncPoints) {
159 point->setTransactionApplied();
160 }
161 mRemoteSyncPoints.clear();
162
163 {
164 Mutex::Autolock syncLock(mLocalSyncPointMutex);
165 for (auto& point : mLocalSyncPoints) {
166 point->setFrameAvailable();
167 }
168 mLocalSyncPoints.clear();
169 }
170
Chia-I Wuc6657022017-08-15 11:18:17 -0700171 for (const auto& child : mCurrentChildren) {
172 child->onRemovedFromCurrentState();
173 }
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800174
175 mFlinger->markLayerPendingRemovalLocked(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700176}
Chia-I Wu38512252017-05-17 14:36:16 -0700177
chaviw61626f22018-11-15 16:26:27 -0800178void Layer::addToCurrentState() {
179 mRemovedFromCurrentState = false;
180
181 for (const auto& child : mCurrentChildren) {
182 child->addToCurrentState();
183 }
184}
185
Mathias Agopian13127d82013-03-05 17:47:11 -0800186// ---------------------------------------------------------------------------
187// set-up
188// ---------------------------------------------------------------------------
189
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700190const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800191 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800192}
193
chaviw13fdc492017-06-27 12:40:18 -0700194bool Layer::getPremultipledAlpha() const {
195 return mPremultipliedAlpha;
196}
197
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700198sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800199 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700200 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800201}
202
203// ---------------------------------------------------------------------------
204// h/w composer set-up
205// ---------------------------------------------------------------------------
206
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800207bool Layer::hasHwcLayer(const sp<const DisplayDevice>& displayDevice) {
208 auto outputLayer = findOutputLayerForDisplay(displayDevice);
209 LOG_FATAL_IF(!outputLayer);
210 return outputLayer->getState().hwc && (*outputLayer->getState().hwc).hwcLayer != nullptr;
211}
212
213HWC2::Layer* Layer::getHwcLayer(const sp<const DisplayDevice>& displayDevice) {
214 auto outputLayer = findOutputLayerForDisplay(displayDevice);
215 if (!outputLayer || !outputLayer->getState().hwc) {
216 return nullptr;
217 }
218 return (*outputLayer->getState().hwc).hwcLayer.get();
Steven Thomasb02664d2017-07-26 18:48:28 -0700219}
Steven Thomasb02664d2017-07-26 18:48:28 -0700220
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800221Rect Layer::getContentCrop() const {
222 // this is the crop rectangle that applies to the buffer
223 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700224 Rect crop;
225 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800226 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700227 crop = mCurrentCrop;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800228 } else if (mActiveBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800229 // otherwise we use the whole buffer
Lloyd Pique0b785d82018-12-04 17:25:27 -0800230 crop = mActiveBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700231 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800232 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700233 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700234 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700235 return crop;
236}
237
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700238static Rect reduce(const Rect& win, const Region& exclude) {
239 if (CC_LIKELY(exclude.isEmpty())) {
240 return win;
241 }
242 if (exclude.isRect()) {
243 return win.reduce(exclude.getBounds());
244 }
245 return Region(win).subtract(exclude).getBounds();
246}
247
Dan Stoza80d61162017-12-20 15:57:52 -0800248static FloatRect reduce(const FloatRect& win, const Region& exclude) {
249 if (CC_LIKELY(exclude.isEmpty())) {
250 return win;
251 }
252 // Convert through Rect (by rounding) for lack of FloatRegion
253 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
254}
255
Vishnu Nair4351ad52019-02-11 14:13:02 -0800256Rect Layer::getScreenBounds(bool reduceTransparentRegion) const {
Vishnu Nairf0c28512019-02-08 12:40:28 -0800257 if (!reduceTransparentRegion) {
258 return Rect{mScreenBounds};
259 }
260
261 FloatRect bounds = getBounds();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800262 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800263 // Transform to screen space.
264 bounds = t.transform(bounds);
265 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700266}
267
Vishnu Nair4351ad52019-02-11 14:13:02 -0800268FloatRect Layer::getBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000269 const State& s(getDrawingState());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800270 return getBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700271}
272
Vishnu Nairf0c28512019-02-08 12:40:28 -0800273FloatRect Layer::getBounds(const Region& activeTransparentRegion) const {
274 // Subtract the transparent region and snap to the bounds.
275 return reduce(mBounds, activeTransparentRegion);
276}
277
Vishnu Nair4351ad52019-02-11 14:13:02 -0800278ui::Transform Layer::getTransformWithScale() const {
279 // If the layer is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
280 // it isFixedSize) then there may be additional scaling not accounted
281 // for in the transform. We need to mirror this scaling to child surfaces
282 // or we will break the contract where WM can treat child surfaces as
283 // pixels in the parent surface.
Lloyd Pique0b785d82018-12-04 17:25:27 -0800284 if (!isFixedSize() || !mActiveBuffer) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800285 return mEffectiveTransform;
286 }
287
Marissa Wall290ad082019-03-06 13:23:47 -0800288 // If the layer is a buffer state layer, the active width and height
289 // could be infinite. In that case, return the effective transform.
290 const uint32_t activeWidth = getActiveWidth(getDrawingState());
291 const uint32_t activeHeight = getActiveHeight(getDrawingState());
292 if (activeWidth >= UINT32_MAX && activeHeight >= UINT32_MAX) {
293 return mEffectiveTransform;
294 }
295
Vishnu Nair4351ad52019-02-11 14:13:02 -0800296 int bufferWidth;
297 int bufferHeight;
298 if ((mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800299 bufferWidth = mActiveBuffer->getWidth();
300 bufferHeight = mActiveBuffer->getHeight();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800301 } else {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800302 bufferHeight = mActiveBuffer->getWidth();
303 bufferWidth = mActiveBuffer->getHeight();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800304 }
Marissa Wall290ad082019-03-06 13:23:47 -0800305 float sx = activeWidth / static_cast<float>(bufferWidth);
306 float sy = activeHeight / static_cast<float>(bufferHeight);
307
Vishnu Nair4351ad52019-02-11 14:13:02 -0800308 ui::Transform extraParentScaling;
309 extraParentScaling.set(sx, 0, 0, sy);
310 return mEffectiveTransform * extraParentScaling;
311}
312
313void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform) {
314 const State& s(getDrawingState());
315
316 // Calculate effective layer transform
317 mEffectiveTransform = parentTransform * getActiveTransform(s);
318
319 // Transform parent bounds to layer space
320 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
321
322 // Calculate display frame
323 mSourceBounds = computeSourceBounds(parentBounds);
324
325 // Calculate bounds by croping diplay frame with layer crop and parent bounds
326 FloatRect bounds = mSourceBounds;
327 const Rect layerCrop = getCrop(s);
328 if (!layerCrop.isEmpty()) {
329 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
330 }
331 bounds = bounds.intersect(parentBounds);
332
333 mBounds = bounds;
334 mScreenBounds = mEffectiveTransform.transform(mBounds);
335 for (const sp<Layer>& child : mDrawingChildren) {
336 child->computeBounds(mBounds, getTransformWithScale());
337 }
338}
339
Robert Carrb5d3d262016-03-25 15:08:13 -0700340
Robert Carr1f0a16a2016-10-24 16:27:39 -0700341
Vishnu Nair60356342018-11-13 13:00:45 -0800342Rect Layer::getCroppedBufferSize(const State& s) const {
343 Rect size = getBufferSize(s);
344 Rect crop = getCrop(s);
345 if (!crop.isEmpty() && size.isValid()) {
346 size.intersect(crop, &size);
347 } else if (!crop.isEmpty()) {
348 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700349 }
Vishnu Nair60356342018-11-13 13:00:45 -0800350 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800351}
352
Alec Mouri1f3bd182019-01-24 15:20:18 +0000353Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& display) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700354 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800355 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700356 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800357
358 // apply the projection's clipping to the window crop in
359 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700360 // if there are no window scaling involved, this operation will map to full
361 // pixels in the buffer.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700362
Vishnu Nair4351ad52019-02-11 14:13:02 -0800363 FloatRect activeCropFloat = getBounds();
Alec Mouri1f3bd182019-01-24 15:20:18 +0000364 ui::Transform t = getTransform();
365 // Transform to screen space.
366 activeCropFloat = t.transform(activeCropFloat);
367 activeCropFloat = activeCropFloat.intersect(display->getViewport().toFloatRect());
368 // Back to layer space to work with the content crop.
369 activeCropFloat = t.inverse().transform(activeCropFloat);
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800370 // This needs to be here as transform.transform(Rect) computes the
371 // transformed rect and then takes the bounding box of the result before
372 // returning. This means
373 // transform.inverse().transform(transform.transform(Rect)) != Rect
374 // in which case we need to make sure the final rect is clipped to the
375 // display bounds.
376 Rect activeCrop{activeCropFloat};
377 if (!activeCrop.intersect(getBufferSize(s), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000378 activeCrop.clear();
379 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700380 return activeCrop;
381}
382
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700383void Layer::setupRoundedCornersCropCoordinates(Rect win,
384 const FloatRect& roundedCornersCrop) const {
385 // Translate win by the rounded corners rect coordinates, to have all values in
386 // layer coordinate space.
387 win.left -= roundedCornersCrop.left;
388 win.right -= roundedCornersCrop.left;
389 win.top -= roundedCornersCrop.top;
390 win.bottom -= roundedCornersCrop.top;
391
Lloyd Pique0b785d82018-12-04 17:25:27 -0800392 renderengine::Mesh::VertexArray<vec2> cropCoords(
393 getCompositionLayer()->editState().reMesh.getCropCoordArray<vec2>());
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700394 cropCoords[0] = vec2(win.left, win.top);
395 cropCoords[1] = vec2(win.left, win.top + win.getHeight());
396 cropCoords[2] = vec2(win.right, win.top + win.getHeight());
397 cropCoords[3] = vec2(win.right, win.top);
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700398}
399
Alec Mouri1f3bd182019-01-24 15:20:18 +0000400FloatRect Layer::computeCrop(const sp<const DisplayDevice>& display) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700401 // the content crop is the area of the content that gets scaled to the
402 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800403 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700404
405 // In addition there is a WM-specified crop we pull from our drawing state.
406 const State& s(getDrawingState());
407
Alec Mouri1f3bd182019-01-24 15:20:18 +0000408 Rect activeCrop = computeInitialCrop(display);
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800409 Rect bufferSize = getBufferSize(s);
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700410
Marissa Wall290ad082019-03-06 13:23:47 -0800411 int32_t winWidth = bufferSize.getWidth();
412 int32_t winHeight = bufferSize.getHeight();
413
414 // The bufferSize for buffer state layers can be unbounded ([0, 0, -1, -1]) if display frame
415 // hasn't been set and the parent is an unbounded layer.
416 if (winWidth < 0 && winHeight < 0) {
417 return crop;
418 }
419
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000420 // Transform the window crop to match the buffer coordinate system,
421 // which means using the inverse of the current transform set on the
422 // SurfaceFlingerConsumer.
423 uint32_t invTransform = mCurrentTransform;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800424 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000425 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700426 * the code below applies the primary display's inverse transform to the
427 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000428 */
David Sodman41fdfc92017-11-06 16:09:56 -0800429 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000430 // calculate the inverse transform
431 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800432 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800433 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000434 // and apply to the current transform
Peiyong Linefefaac2018-08-17 12:27:51 -0700435 invTransform = (ui::Transform(invTransformOrient) *
436 ui::Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800437 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000438
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000439 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
440 // If the activeCrop has been rotate the ends are rotated but not
441 // the space itself so when transforming ends back we can't rely on
442 // a modification of the axes of rotation. To account for this we
443 // need to reorient the inverse rotation in terms of the current
444 // axes of rotation.
445 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
446 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
447 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800448 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000449 }
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800450 std::swap(winWidth, winHeight);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000451 }
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800452 const Rect winCrop =
453 activeCrop.transform(invTransform, bufferSize.getWidth(), bufferSize.getHeight());
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000454
455 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800456 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000457 float yScale = crop.getHeight() / float(winHeight);
458
David Sodman41fdfc92017-11-06 16:09:56 -0800459 float insetL = winCrop.left * xScale;
460 float insetT = winCrop.top * yScale;
461 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000462 float insetB = (winHeight - winCrop.bottom) * yScale;
463
David Sodman41fdfc92017-11-06 16:09:56 -0800464 crop.left += insetL;
465 crop.top += insetT;
466 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000467 crop.bottom -= insetB;
468
Mathias Agopian13127d82013-03-05 17:47:11 -0800469 return crop;
470}
471
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700472void Layer::setGeometry(const sp<const DisplayDevice>& display, uint32_t z) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800473 const auto outputLayer = findOutputLayerForDisplay(display);
474 LOG_FATAL_IF(!outputLayer);
475 LOG_FATAL_IF(!outputLayer->getState().hwc);
476 auto& hwcLayer = (*outputLayer->getState().hwc).hwcLayer;
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700477
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800478 if (!hasHwcLayer(display)) {
479 ALOGE("[%s] failed to setGeometry: no HWC layer found (%s)", mName.string(),
480 display->getDebugName().c_str());
481 return;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800482 }
483
Lloyd Pique0b785d82018-12-04 17:25:27 -0800484 LOG_FATAL_IF(!getCompositionLayer());
485 auto& commonCompositionState = getCompositionLayer()->editState().frontEnd;
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800486 auto& compositionState = outputLayer->editState();
487
488 // enable this layer
489 compositionState.forceClientComposition = false;
490
491 if (isSecure() && !display->isSecure()) {
492 compositionState.forceClientComposition = true;
493 }
David Sodman15094112018-10-11 09:39:37 -0700494
Mathias Agopian13127d82013-03-05 17:47:11 -0800495 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700496 const State& s(getDrawingState());
Vishnu Naira6733b32018-12-06 18:13:16 -0800497 const Rect bufferSize = getBufferSize(s);
David Revemanecf0fa52017-03-03 11:32:44 -0500498 auto blendMode = HWC2::BlendMode::None;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800499 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800500 blendMode =
501 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800502 }
David Sodman15094112018-10-11 09:39:37 -0700503 auto error = hwcLayer->setBlendMode(blendMode);
504 ALOGE_IF(error != HWC2::Error::None,
505 "[%s] Failed to set blend mode %s:"
506 " %s (%d)",
507 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
508 static_cast<int32_t>(error));
Lloyd Pique0b785d82018-12-04 17:25:27 -0800509 commonCompositionState.blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
Mathias Agopian13127d82013-03-05 17:47:11 -0800510
511 // apply the layer's transform, followed by the display's global transform
512 // here we're guaranteed that the layer's transform preserves rects
Marissa Wall61c58622018-07-18 10:12:20 -0700513 Region activeTransparentRegion(getActiveTransparentRegion(s));
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800514 ui::Transform t = getTransform();
Marissa Wall61c58622018-07-18 10:12:20 -0700515 Rect activeCrop = getCrop(s);
Vishnu Naira6733b32018-12-06 18:13:16 -0800516 if (!activeCrop.isEmpty() && bufferSize.isValid()) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700517 activeCrop = t.transform(activeCrop);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700518 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000519 activeCrop.clear();
520 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700521 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800522 // This needs to be here as transform.transform(Rect) computes the
523 // transformed rect and then takes the bounding box of the result before
524 // returning. This means
525 // transform.inverse().transform(transform.transform(Rect)) != Rect
526 // in which case we need to make sure the final rect is clipped to the
527 // display bounds.
Vishnu Naira6733b32018-12-06 18:13:16 -0800528 if (!activeCrop.intersect(bufferSize, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000529 activeCrop.clear();
530 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700531 // mark regions outside the crop as transparent
Vishnu Naira6733b32018-12-06 18:13:16 -0800532 activeTransparentRegion.orSelf(Rect(0, 0, bufferSize.getWidth(), activeCrop.top));
533 activeTransparentRegion.orSelf(
534 Rect(0, activeCrop.bottom, bufferSize.getWidth(), bufferSize.getHeight()));
David Sodman41fdfc92017-11-06 16:09:56 -0800535 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
536 activeTransparentRegion.orSelf(
Vishnu Naira6733b32018-12-06 18:13:16 -0800537 Rect(activeCrop.right, activeCrop.top, bufferSize.getWidth(), activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700538 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700539
Vishnu Nair4351ad52019-02-11 14:13:02 -0800540 // getBounds returns a FloatRect to provide more accuracy during the
Dan Stoza80d61162017-12-20 15:57:52 -0800541 // transformation. We then round upon constructing 'frame'.
Vishnu Nair4351ad52019-02-11 14:13:02 -0800542 Rect frame{t.transform(getBounds(activeTransparentRegion))};
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700543 if (!frame.intersect(display->getViewport(), &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000544 frame.clear();
545 }
Peiyong Linefefaac2018-08-17 12:27:51 -0700546 const ui::Transform& tr = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800547 Rect transformedFrame = tr.transform(frame);
David Sodman15094112018-10-11 09:39:37 -0700548 error = hwcLayer->setDisplayFrame(transformedFrame);
549 if (error != HWC2::Error::None) {
550 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
551 transformedFrame.left, transformedFrame.top, transformedFrame.right,
552 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
553 } else {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800554 compositionState.displayFrame = transformedFrame;
David Sodman15094112018-10-11 09:39:37 -0700555 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800556
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700557 FloatRect sourceCrop = computeCrop(display);
David Sodman15094112018-10-11 09:39:37 -0700558 error = hwcLayer->setSourceCrop(sourceCrop);
559 if (error != HWC2::Error::None) {
560 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
561 "%s (%d)",
562 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
563 to_string(error).c_str(), static_cast<int32_t>(error));
564 } else {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800565 compositionState.sourceCrop = sourceCrop;
David Sodman15094112018-10-11 09:39:37 -0700566 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800567
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800568 float alpha = static_cast<float>(getAlpha());
David Sodman15094112018-10-11 09:39:37 -0700569 error = hwcLayer->setPlaneAlpha(alpha);
570 ALOGE_IF(error != HWC2::Error::None,
571 "[%s] Failed to set plane alpha %.3f: "
572 "%s (%d)",
573 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Pique0b785d82018-12-04 17:25:27 -0800574 commonCompositionState.alpha = alpha;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800575
David Sodman15094112018-10-11 09:39:37 -0700576 error = hwcLayer->setZOrder(z);
577 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
578 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800579 compositionState.z = z;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500580
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800581 int type = s.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
582 int appId = s.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700583 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400584 if (parent.get()) {
585 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800586 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
587 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
588 if (parentType >= 0 || parentAppId >= 0) {
589 type = parentType;
590 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700591 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400592 }
593
David Sodman15094112018-10-11 09:39:37 -0700594 error = hwcLayer->setInfo(type, appId);
595 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
596 static_cast<int32_t>(error));
597
Lloyd Pique0b785d82018-12-04 17:25:27 -0800598 commonCompositionState.type = type;
599 commonCompositionState.appId = appId;
David Sodmanba340492018-08-05 21:51:33 -0700600
Mathias Agopian29a367b2011-07-12 14:51:45 -0700601 /*
602 * Transformations are applied in this order:
603 * 1) buffer orientation/flip/mirror
604 * 2) state transformation (window manager)
605 * 3) layer orientation (screen orientation)
606 * (NOTE: the matrices are multiplied in reverse order)
607 */
608
Peiyong Linefefaac2018-08-17 12:27:51 -0700609 const ui::Transform bufferOrientation(mCurrentTransform);
610 ui::Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700611
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800612 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700613 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700614 * the code below applies the primary display's inverse transform to the
615 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700616 */
David Sodman41fdfc92017-11-06 16:09:56 -0800617 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700618 // calculate the inverse transform
619 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800620 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700621 }
Robert Carrcae605c2017-03-29 12:10:31 -0700622
623 /*
624 * Here we cancel out the orientation component of the WM transform.
625 * The scaling and translate components are already included in our bounds
626 * computation so it's enough to just omit it in the composition.
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000627 * See comment in BufferLayer::prepareClientLayer with ref to b/36727915 for why.
Robert Carrcae605c2017-03-29 12:10:31 -0700628 */
Peiyong Linefefaac2018-08-17 12:27:51 -0700629 transform = ui::Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700630 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700631
632 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800633 const uint32_t orientation = transform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -0700634 if (orientation & ui::Transform::ROT_INVALID) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800635 // we can only handle simple transformation
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800636 compositionState.forceClientComposition = true;
637 (*compositionState.hwc).hwcCompositionType = Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800638 } else {
639 auto transform = static_cast<HWC2::Transform>(orientation);
David Sodman15094112018-10-11 09:39:37 -0700640 auto error = hwcLayer->setTransform(transform);
641 ALOGE_IF(error != HWC2::Error::None,
642 "[%s] Failed to set transform %s: "
643 "%s (%d)",
644 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
645 static_cast<int32_t>(error));
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800646 compositionState.bufferTransform = static_cast<Hwc2::Transform>(transform);
David Sodman4b7c4bc2017-11-17 12:13:59 -0800647 }
648}
649
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800650void Layer::forceClientComposition(const sp<DisplayDevice>& display) {
651 const auto outputLayer = findOutputLayerForDisplay(display);
652 LOG_FATAL_IF(!outputLayer);
653 outputLayer->editState().forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800654}
Dan Stozaee44edd2015-03-23 15:50:23 -0700655
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800656bool Layer::getForceClientComposition(const sp<DisplayDevice>& display) {
657 const auto outputLayer = findOutputLayerForDisplay(display);
658 LOG_FATAL_IF(!outputLayer);
659 return outputLayer->getState().forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800660}
661
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700662void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800663 const auto outputLayer = findOutputLayerForDisplay(display);
664 LOG_FATAL_IF(!outputLayer);
665
666 if (!outputLayer->getState().hwc ||
667 (*outputLayer->getState().hwc).hwcCompositionType !=
668 Hwc2::IComposerClient::Composition::CURSOR) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800669 return;
670 }
671
672 // This gives us only the "orientation" component of the transform
Vishnu Nair33a6eee2019-02-06 13:48:06 -0800673 const State& s(getDrawingState());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800674
675 // Apply the layer's transform, followed by the display's global transform
676 // Here we're guaranteed that the layer's transform preserves rects
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800677 Rect win = getCroppedBufferSize(s);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800678 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700679 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800680 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700681 frame.intersect(display->getViewport(), &frame);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700682 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800683 auto position = displayTransform.transform(frame);
684
Dominik Laskowski7e045462018-05-30 13:02:02 -0700685 auto error =
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800686 (*outputLayer->getState().hwc).hwcLayer->setCursorPosition(position.left, position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800687 ALOGE_IF(error != HWC2::Error::None,
688 "[%s] Failed to set cursor position "
689 "to (%d, %d): %s (%d)",
690 mName.string(), position.left, position.top, to_string(error).c_str(),
691 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800692}
Riley Andrews03414a12014-07-01 14:22:59 -0700693
Mathias Agopian13127d82013-03-05 17:47:11 -0800694// ---------------------------------------------------------------------------
695// drawing...
696// ---------------------------------------------------------------------------
697
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000698bool Layer::prepareClientLayer(const RenderArea& renderArea, const Region& clip,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800699 Region& clearRegion, const bool supportProtectedContent,
700 renderengine::LayerSettings& layer) {
701 return prepareClientLayer(renderArea, clip, false, clearRegion, supportProtectedContent, layer);
Mathias Agopian13127d82013-03-05 17:47:11 -0800702}
703
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000704bool Layer::prepareClientLayer(const RenderArea& renderArea, bool useIdentityTransform,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800705 Region& clearRegion, const bool supportProtectedContent,
706 renderengine::LayerSettings& layer) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000707 return prepareClientLayer(renderArea, Region(renderArea.getBounds()), useIdentityTransform,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800708 clearRegion, supportProtectedContent, layer);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000709}
710
711bool Layer::prepareClientLayer(const RenderArea& /*renderArea*/, const Region& /*clip*/,
712 bool useIdentityTransform, Region& /*clearRegion*/,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800713 const bool /*supportProtectedContent*/,
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000714 renderengine::LayerSettings& layer) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800715 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000716 half alpha = getAlpha();
717 layer.geometry.boundaries = bounds;
718 if (useIdentityTransform) {
719 layer.geometry.positionTransform = mat4();
720 } else {
721 const ui::Transform transform = getTransform();
722 mat4 m;
723 m[0][0] = transform[0][0];
724 m[0][1] = transform[0][1];
725 m[0][3] = transform[0][2];
726 m[1][0] = transform[1][0];
727 m[1][1] = transform[1][1];
728 m[1][3] = transform[1][2];
729 m[3][0] = transform[2][0];
730 m[3][1] = transform[2][1];
731 m[3][3] = transform[2][2];
732 layer.geometry.positionTransform = m;
733 }
734
735 if (hasColorTransform()) {
736 layer.colorTransform = getColorTransform();
737 }
738
739 const auto roundedCornerState = getRoundedCornerState();
740 layer.geometry.roundedCornersRadius = roundedCornerState.radius;
741 layer.geometry.roundedCornersCrop = roundedCornerState.cropRect;
742
743 layer.alpha = alpha;
744 layer.sourceDataspace = mCurrentDataSpace;
745 return true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800746}
747
David Sodman41fdfc92017-11-06 16:09:56 -0800748void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
749 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800750 auto& engine(mFlinger->getRenderEngine());
Lloyd Pique0b785d82018-12-04 17:25:27 -0800751 computeGeometry(renderArea, getCompositionLayer()->editState().reMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700752 engine.setupFillWithColor(red, green, blue, alpha);
Lloyd Pique0b785d82018-12-04 17:25:27 -0800753 engine.drawMesh(getCompositionLayer()->getState().reMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800754}
755
David Sodmanc1498e62018-09-12 14:36:26 -0700756void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
757 clearWithOpenGL(renderArea, 0, 0, 0, 0);
758}
759
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800760void Layer::setCompositionType(const sp<const DisplayDevice>& display,
761 Hwc2::IComposerClient::Composition type) {
762 const auto outputLayer = findOutputLayerForDisplay(display);
763 LOG_FATAL_IF(!outputLayer);
764 LOG_FATAL_IF(!outputLayer->getState().hwc);
765 auto& compositionState = outputLayer->editState();
766
767 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", ((*compositionState.hwc).hwcLayer)->getId(),
768 toString(type).c_str(), 1);
769 if ((*compositionState.hwc).hwcCompositionType != type) {
David Sodman15094112018-10-11 09:39:37 -0700770 ALOGV(" actually setting");
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800771 (*compositionState.hwc).hwcCompositionType = type;
772
773 auto error = (*compositionState.hwc)
774 .hwcLayer->setCompositionType(static_cast<HWC2::Composition>(type));
775 ALOGE_IF(error != HWC2::Error::None,
776 "[%s] Failed to set "
777 "composition type %s: %s (%d)",
778 mName.string(), toString(type).c_str(), to_string(error).c_str(),
779 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800780 }
781}
782
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800783Hwc2::IComposerClient::Composition Layer::getCompositionType(
784 const sp<const DisplayDevice>& display) const {
785 const auto outputLayer = findOutputLayerForDisplay(display);
786 LOG_FATAL_IF(!outputLayer);
787 return outputLayer->getState().hwc ? (*outputLayer->getState().hwc).hwcCompositionType
788 : Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800789}
790
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800791bool Layer::getClearClientTarget(const sp<const DisplayDevice>& display) const {
792 const auto outputLayer = findOutputLayerForDisplay(display);
793 LOG_FATAL_IF(!outputLayer);
794 return outputLayer->getState().clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800795}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800796
Dan Stozacac35382016-01-27 12:21:06 -0800797bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
798 if (point->getFrameNumber() <= mCurrentFrameNumber) {
799 // Don't bother with a SyncPoint, since we've already latched the
800 // relevant frame
801 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700802 }
Robert Carr2e102c92018-10-23 12:11:15 -0700803 if (isRemovedFromCurrentState()) {
804 return false;
805 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700806
Dan Stozacac35382016-01-27 12:21:06 -0800807 Mutex::Autolock lock(mLocalSyncPointMutex);
808 mLocalSyncPoints.push_back(point);
809 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700810}
811
Mathias Agopian13127d82013-03-05 17:47:11 -0800812// ----------------------------------------------------------------------------
813// local state
814// ----------------------------------------------------------------------------
815
Peiyong Lin833074a2018-08-28 11:53:54 -0700816void Layer::computeGeometry(const RenderArea& renderArea,
817 renderengine::Mesh& mesh,
chaviwa76b2712017-09-20 12:02:26 -0700818 bool useIdentityTransform) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700819 const ui::Transform renderAreaTransform(renderArea.getTransform());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800820 FloatRect win = getBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700821
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000822 vec2 lt = vec2(win.left, win.top);
823 vec2 lb = vec2(win.left, win.bottom);
824 vec2 rb = vec2(win.right, win.bottom);
825 vec2 rt = vec2(win.right, win.top);
826
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800827 ui::Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000828 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700829 lt = layerTransform.transform(lt);
830 lb = layerTransform.transform(lb);
831 rb = layerTransform.transform(rb);
832 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000833 }
834
Peiyong Lin833074a2018-08-28 11:53:54 -0700835 renderengine::Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700836 position[0] = renderAreaTransform.transform(lt);
837 position[1] = renderAreaTransform.transform(lb);
838 position[2] = renderAreaTransform.transform(rb);
839 position[3] = renderAreaTransform.transform(rt);
Mathias Agopian13127d82013-03-05 17:47:11 -0800840}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800841
David Sodman41fdfc92017-11-06 16:09:56 -0800842bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800843 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700844 return (s.flags & layer_state_t::eLayerSecure);
845}
846
Mathias Agopian13127d82013-03-05 17:47:11 -0800847void Layer::setVisibleRegion(const Region& visibleRegion) {
848 // always called from main thread
849 this->visibleRegion = visibleRegion;
850}
851
852void Layer::setCoveredRegion(const Region& coveredRegion) {
853 // always called from main thread
854 this->coveredRegion = coveredRegion;
855}
856
David Sodman41fdfc92017-11-06 16:09:56 -0800857void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800858 // always called from main thread
859 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
860}
861
Robert Carre5f4f692018-01-12 13:12:28 -0800862void Layer::clearVisibilityRegions() {
863 visibleRegion.clear();
864 visibleNonTransparentRegion.clear();
865 coveredRegion.clear();
866}
867
Mathias Agopian13127d82013-03-05 17:47:11 -0800868// ----------------------------------------------------------------------------
869// transaction
870// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800871
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800872void Layer::pushPendingState() {
873 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700874 return;
875 }
876
Dan Stoza7dde5992015-05-22 09:51:44 -0700877 // If this transaction is waiting on the receipt of a frame, generate a sync
878 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700879 // We don't allow installing sync points after we are removed from the current state
880 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800881 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
882 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800883 if (barrierLayer == nullptr) {
884 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700885 // If we can't promote the layer we are intended to wait on,
886 // then it is expired or otherwise invalid. Allow this transaction
887 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800888 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800889 } else {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800890 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy);
Robert Carr0d480722017-01-10 16:42:54 -0800891 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800892 mRemoteSyncPoints.push_back(std::move(syncPoint));
893 } else {
894 // We already missed the frame we're supposed to synchronize
895 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800896 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800897 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700898 }
899
Dan Stoza7dde5992015-05-22 09:51:44 -0700900 // Wake us up to check if the frame has been received
901 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700902 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700903 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800904 mPendingStates.push_back(mCurrentState);
905 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700906}
907
Pablo Ceballos05289c22016-04-14 15:49:55 -0700908void Layer::popPendingState(State* stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800909 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700910
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800911 mPendingStates.removeAt(0);
912 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700913}
914
Pablo Ceballos05289c22016-04-14 15:49:55 -0700915bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700916 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800917 while (!mPendingStates.empty()) {
918 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700919 if (mRemoteSyncPoints.empty()) {
920 // If we don't have a sync point for this, apply it anyway. It
921 // will be visually wrong, but it should keep us from getting
922 // into too much trouble.
923 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700924 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700925 stateUpdateAvailable = true;
926 continue;
927 }
928
Marissa Wallf58c14b2018-07-24 10:50:43 -0700929 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800930 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800931 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800932
933 // Signal our end of the sync point and then dispose of it
934 mRemoteSyncPoints.front()->setTransactionApplied();
935 mRemoteSyncPoints.pop_front();
936 continue;
937 }
938
Dan Stoza7dde5992015-05-22 09:51:44 -0700939 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
940 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700941 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700942 stateUpdateAvailable = true;
943
944 // Signal our end of the sync point and then dispose of it
945 mRemoteSyncPoints.front()->setTransactionApplied();
946 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800947 } else {
948 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700949 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700950 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700951 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700952 stateUpdateAvailable = true;
953 }
954 }
955
956 // If we still have pending updates, wake SurfaceFlinger back up and point
957 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800958 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700959 setTransactionFlags(eTransactionNeeded);
960 mFlinger->setTransactionFlags(eTraversalNeeded);
961 }
962
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800963 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700964 return stateUpdateAvailable;
965}
966
Marissa Wall61c58622018-07-18 10:12:20 -0700967uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000968 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800969
Marissa Wall61c58622018-07-18 10:12:20 -0700970 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
971 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700972
David Sodmaneb085e02017-10-05 18:49:04 -0700973 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700974 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000975 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800976 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
977 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
978 " requested={ wh={%4u,%4u} }}\n"
979 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
980 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700981 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700982 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
983 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
984 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
985 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
986 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700987 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
988 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
989 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800990 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700991
Robert Carre392b552017-09-19 12:16:05 -0700992 // Don't let Layer::doTransaction update the drawing state
993 // if we have a pending resize, unless we are in fixed-size mode.
994 // the drawing state will be updated only once we receive a buffer
995 // with the correct size.
996 //
997 // In particular, we want to make sure the clip (which is part
998 // of the geometry state) is latched together with the size but is
999 // latched immediately when no resizing is involved.
1000 //
1001 // If a sideband stream is attached, however, we want to skip this
1002 // optimization so that transactions aren't missed when a buffer
1003 // never arrives
1004 //
1005 // In the case that we don't have a buffer we ignore other factors
1006 // and avoid entering the resizePending state. At a high level the
1007 // resizePending state is to avoid applying the state of the new buffer
1008 // to the old buffer. However in the state where we don't have an old buffer
1009 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -07001010 const bool resizePending =
1011 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
1012 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
Lloyd Pique0b785d82018-12-04 17:25:27 -08001013 (mActiveBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001014 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -08001015 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001016 flags |= eDontUpdateGeometryState;
1017 }
1018 }
1019
Robert Carr7bf247e2017-05-18 14:02:49 -07001020 // Here we apply various requested geometry states, depending on our
1021 // latching configuration. See Layer.h for a detailed discussion of
1022 // how geometry latching is controlled.
1023 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +00001024 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001025
1026 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1027 // mode, which causes attributes which normally latch regardless of scaling mode,
1028 // to be delayed. We copy the requested state to the active state making sure
1029 // to respect these rules (again see Layer.h for a detailed discussion).
1030 //
1031 // There is an awkward asymmetry in the handling of the crop states in the position
1032 // states, as can be seen below. Largely this arises from position and transform
1033 // being stored in the same data structure while having different latching rules.
1034 // b/38182305
1035 //
Marissa Wall61c58622018-07-18 10:12:20 -07001036 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -07001037 // applyPendingStates in the presence of deferred transactions.
1038 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -07001039 float tx = stateToCommit->active_legacy.transform.tx();
1040 float ty = stateToCommit->active_legacy.transform.ty();
1041 stateToCommit->active_legacy = stateToCommit->requested_legacy;
1042 stateToCommit->active_legacy.transform.set(tx, ty);
1043 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001044 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001045 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -07001046 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001047 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001048 }
1049
Marissa Wall61c58622018-07-18 10:12:20 -07001050 return flags;
1051}
1052
1053uint32_t Layer::doTransaction(uint32_t flags) {
1054 ATRACE_CALL();
1055
chaviw5aedec92018-10-22 10:40:38 -07001056 if (mLayerDetached) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001057 return flags;
1058 }
1059
1060 if (mChildrenChanged) {
1061 flags |= eVisibleRegion;
1062 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -07001063 }
1064
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001065 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +00001066 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -07001067 if (!applyPendingStates(&c)) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001068 return flags;
Marissa Wall61c58622018-07-18 10:12:20 -07001069 }
1070
1071 flags = doTransactionResize(flags, &c);
1072
Alec Mourib416efd2018-09-06 21:01:59 +00001073 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -07001074
1075 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001076 // invalidate and recompute the visible regions if needed
1077 flags |= Layer::eVisibleRegion;
1078 }
1079
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001080 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001081 // invalidate and recompute the visible regions if needed
1082 flags |= eVisibleRegion;
1083 this->contentDirty = true;
1084
1085 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -07001086 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -07001087 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -08001088 }
1089
Dan Stozac8145172016-04-28 16:29:06 -07001090 // If the layer is hidden, signal and clear out all local sync points so
1091 // that transactions for layers depending on this layer's frames becoming
1092 // visible are not blocked
1093 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001094 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001095 }
1096
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001097 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -07001098 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001099 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -07001100 }
1101
Mathias Agopian13127d82013-03-05 17:47:11 -08001102 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001103 commitTransaction(c);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001104 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -08001105 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001106}
1107
Pablo Ceballos05289c22016-04-14 15:49:55 -07001108void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001109 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001110}
1111
Mathias Agopian13127d82013-03-05 17:47:11 -08001112uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001113 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -08001114}
1115
1116uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001117 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -08001118}
1119
Robert Carr82364e32016-05-15 11:27:47 -07001120bool Layer::setPosition(float x, float y, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001121 if (mCurrentState.requested_legacy.transform.tx() == x &&
1122 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001123 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001124 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001125
1126 // We update the requested and active position simultaneously because
1127 // we want to apply the position portion of the transform matrix immediately,
1128 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001129 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001130 if (immediate && !mFreezeGeometryUpdates) {
1131 // Here we directly update the active state
1132 // unlike other setters, because we store it within
1133 // the transform, but use different latching rules.
1134 // b/38182305
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001135 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001136 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001137 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001138
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001139 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001140 setTransactionFlags(eTransactionNeeded);
1141 return true;
1142}
Robert Carr82364e32016-05-15 11:27:47 -07001143
Robert Carr1f0a16a2016-10-24 16:27:39 -07001144bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1145 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1146 if (idx < 0) {
1147 return false;
1148 }
1149 if (childLayer->setLayer(z)) {
1150 mCurrentChildren.removeAt(idx);
1151 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001152 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001153 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001154 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001155}
1156
Robert Carr503c7042017-09-27 15:06:08 -07001157bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1158 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1159 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1160 if (idx < 0) {
1161 return false;
1162 }
1163 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1164 mCurrentChildren.removeAt(idx);
1165 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001166 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001167 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001168 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001169}
1170
Robert Carrae060832016-11-28 10:51:00 -08001171bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001172 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
1173 mCurrentState.sequence++;
1174 mCurrentState.z = z;
1175 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001176
1177 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001178 if (mCurrentState.zOrderRelativeOf != nullptr) {
1179 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -07001180 if (strongRelative != nullptr) {
1181 strongRelative->removeZOrderRelative(this);
1182 }
chaviw606e5cf2019-03-01 10:12:10 -08001183 setZOrderRelativeOf(nullptr);
Robert Carrdb66e622017-04-10 16:55:57 -07001184 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001185 setTransactionFlags(eTransactionNeeded);
1186 return true;
1187}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001188
Robert Carrdb66e622017-04-10 16:55:57 -07001189void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001190 mCurrentState.zOrderRelatives.remove(relative);
1191 mCurrentState.sequence++;
1192 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001193 setTransactionFlags(eTransactionNeeded);
1194}
1195
1196void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001197 mCurrentState.zOrderRelatives.add(relative);
1198 mCurrentState.modified = true;
1199 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -07001200 setTransactionFlags(eTransactionNeeded);
1201}
1202
chaviw606e5cf2019-03-01 10:12:10 -08001203void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
1204 mCurrentState.zOrderRelativeOf = relativeOf;
1205 mCurrentState.sequence++;
1206 mCurrentState.modified = true;
1207 setTransactionFlags(eTransactionNeeded);
1208}
1209
Robert Carr503d2bd2017-12-04 15:49:47 -08001210bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001211 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1212 if (handle == nullptr) {
1213 return false;
1214 }
1215 sp<Layer> relative = handle->owner.promote();
1216 if (relative == nullptr) {
1217 return false;
1218 }
1219
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001220 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1221 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001222 return false;
1223 }
1224
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001225 mCurrentState.sequence++;
1226 mCurrentState.modified = true;
1227 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001228
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001229 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -07001230 if (oldZOrderRelativeOf != nullptr) {
1231 oldZOrderRelativeOf->removeZOrderRelative(this);
1232 }
chaviw606e5cf2019-03-01 10:12:10 -08001233 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -07001234 relative->addZOrderRelative(this);
1235
1236 setTransactionFlags(eTransactionNeeded);
1237
1238 return true;
1239}
1240
Mathias Agopian13127d82013-03-05 17:47:11 -08001241bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001242 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001243 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001244 mCurrentState.requested_legacy.w = w;
1245 mCurrentState.requested_legacy.h = h;
1246 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001247 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001248
1249 // record the new size, from this point on, when the client request
1250 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001251 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001252 return true;
1253}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001254bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001255 if (mCurrentState.color.a == alpha) return false;
1256 mCurrentState.sequence++;
1257 mCurrentState.color.a = alpha;
1258 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001259 setTransactionFlags(eTransactionNeeded);
1260 return true;
1261}
chaviw13fdc492017-06-27 12:40:18 -07001262
Valerie Haudd0b7572019-01-29 14:59:27 -08001263bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1264 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001265 return false;
Valerie Hauaa194562019-02-05 16:21:38 -08001266 }
1267 mCurrentState.sequence++;
1268 mCurrentState.modified = true;
1269 setTransactionFlags(eTransactionNeeded);
1270
1271 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001272 // create background color layer if one does not yet exist
1273 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
1274 const String8& name = mName + "BackgroundColorLayer";
1275 mCurrentState.bgColorLayer =
1276 new ColorLayer(LayerCreationArgs(mFlinger.get(), nullptr, name, 0, 0, flags));
chaviw13fdc492017-06-27 12:40:18 -07001277
Valerie Haudd0b7572019-01-29 14:59:27 -08001278 // add to child list
1279 addChild(mCurrentState.bgColorLayer);
1280 mFlinger->mLayersAdded = true;
1281 // set up SF to handle added color layer
1282 if (isRemovedFromCurrentState()) {
1283 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1284 }
1285 mFlinger->setTransactionFlags(eTransactionNeeded);
1286 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1287 mCurrentState.bgColorLayer->reparent(nullptr);
1288 mCurrentState.bgColorLayer = nullptr;
1289 return true;
1290 }
1291
1292 mCurrentState.bgColorLayer->setColor(color);
1293 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1294 mCurrentState.bgColorLayer->setAlpha(alpha);
1295 mCurrentState.bgColorLayer->setDataspace(dataspace);
1296
chaviw13fdc492017-06-27 12:40:18 -07001297 return true;
1298}
1299
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001300bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001301 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001302
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001303 mCurrentState.sequence++;
1304 mCurrentState.cornerRadius = cornerRadius;
1305 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001306 setTransactionFlags(eTransactionNeeded);
1307 return true;
1308}
1309
Robert Carrd4ae7f32018-06-07 16:10:57 -07001310bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1311 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001312 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001313 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1314
1315 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1316 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1317 return false;
1318 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001319 mCurrentState.sequence++;
1320 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1321 matrix.dsdy);
1322 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001323 setTransactionFlags(eTransactionNeeded);
1324 return true;
1325}
Marissa Wall61c58622018-07-18 10:12:20 -07001326
Mathias Agopian13127d82013-03-05 17:47:11 -08001327bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001328 mCurrentState.requestedTransparentRegion_legacy = transparent;
1329 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001330 setTransactionFlags(eTransactionNeeded);
1331 return true;
1332}
1333bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001334 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1335 if (mCurrentState.flags == newFlags) return false;
1336 mCurrentState.sequence++;
1337 mCurrentState.flags = newFlags;
1338 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001339 setTransactionFlags(eTransactionNeeded);
1340 return true;
1341}
Robert Carr99e27f02016-06-16 15:18:02 -07001342
Marissa Wallf58c14b2018-07-24 10:50:43 -07001343bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001344 if (mCurrentState.requestedCrop_legacy == crop) return false;
1345 mCurrentState.sequence++;
1346 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001347 if (immediate && !mFreezeGeometryUpdates) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001348 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001349 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001350 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1351
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001352 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001353 setTransactionFlags(eTransactionNeeded);
1354 return true;
1355}
Robert Carr8d5227b2017-03-16 15:41:03 -07001356
Robert Carrc3574f72016-03-24 12:19:32 -07001357bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001358 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001359 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001360 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001361 return true;
1362}
1363
Evan Roskyef876c92019-01-25 17:46:06 -08001364bool Layer::setMetadata(const LayerMetadata& data) {
1365 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001366 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001367 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001368 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001369 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001370}
1371
Mathias Agopian13127d82013-03-05 17:47:11 -08001372bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001373 if (mCurrentState.layerStack == layerStack) return false;
1374 mCurrentState.sequence++;
1375 mCurrentState.layerStack = layerStack;
1376 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001377 setTransactionFlags(eTransactionNeeded);
1378 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001379}
1380
Robert Carr1f0a16a2016-10-24 16:27:39 -07001381uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001382 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001383 if (p == nullptr) {
1384 return getDrawingState().layerStack;
1385 }
1386 return p->getLayerStack();
1387}
1388
Marissa Wallf58c14b2018-07-24 10:50:43 -07001389void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001390 mCurrentState.barrierLayer_legacy = barrierLayer;
1391 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001392 // We don't set eTransactionNeeded, because just receiving a deferral
1393 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001394 mCurrentState.modified = true;
1395 pushPendingState();
1396 mCurrentState.barrierLayer_legacy = nullptr;
1397 mCurrentState.frameNumber_legacy = 0;
1398 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001399}
1400
Marissa Wallf58c14b2018-07-24 10:50:43 -07001401void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001402 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001403 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001404}
1405
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001406// ----------------------------------------------------------------------------
1407// pageflip handling...
1408// ----------------------------------------------------------------------------
1409
Robert Carr1f0a16a2016-10-24 16:27:39 -07001410bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001411 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001412 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001413 if (parent != nullptr && parent->isHiddenByPolicy()) {
1414 return true;
1415 }
1416 return s.flags & layer_state_t::eLayerHidden;
1417}
1418
David Sodman41fdfc92017-11-06 16:09:56 -08001419uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001420 // TODO: should we do something special if mSecure is set?
1421 if (mProtectedByApp) {
1422 // need a hardware-protected path to external video sink
1423 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001424 }
Riley Andrews03414a12014-07-01 14:22:59 -07001425 if (mPotentialCursor) {
1426 usage |= GraphicBuffer::USAGE_CURSOR;
1427 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001428 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001429 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001430}
1431
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001432void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001433 uint32_t orientation = 0;
Vishnu Nairf2deb822018-11-12 17:53:48 -08001434 // Disable setting transform hint if the debug flag is set or if the
1435 // getTransformToDisplayInverse flag is set and the client wants to submit buffers
1436 // in one orientation.
1437 if (!mFlinger->mDebugDisableTransformHint && !getTransformToDisplayInverse()) {
Mathias Agopian84300952012-11-21 16:02:13 -08001438 // The transform hint is used to improve performance, but we can
1439 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001440 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001441 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001442 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001443 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001444 orientation = 0;
1445 }
1446 }
David Sodmaneb085e02017-10-05 18:49:04 -07001447 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001448}
1449
Mathias Agopian13127d82013-03-05 17:47:11 -08001450// ----------------------------------------------------------------------------
1451// debugging
1452// ----------------------------------------------------------------------------
1453
Marissa Wall61c58622018-07-18 10:12:20 -07001454// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001455LayerDebugInfo Layer::getLayerDebugInfo() const {
1456 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001457 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001458 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001459 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001460 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
Yiwei Zhang5434a782018-12-05 18:06:32 -08001461 info.mType = std::string(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001462 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001463 info.mVisibleRegion = visibleRegion;
1464 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001465 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001466 info.mX = ds.active_legacy.transform.tx();
1467 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001468 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001469 info.mWidth = ds.active_legacy.w;
1470 info.mHeight = ds.active_legacy.h;
1471 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001472 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001473 info.mFlags = ds.flags;
1474 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001475 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001476 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1477 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1478 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1479 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001480 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001481 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001482 if (buffer != 0) {
1483 info.mActiveBufferWidth = buffer->getWidth();
1484 info.mActiveBufferHeight = buffer->getHeight();
1485 info.mActiveBufferStride = buffer->getStride();
1486 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001487 } else {
1488 info.mActiveBufferWidth = 0;
1489 info.mActiveBufferHeight = 0;
1490 info.mActiveBufferStride = 0;
1491 info.mActiveBufferFormat = 0;
1492 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001493 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001494 info.mNumQueuedFrames = getQueuedFrameCount();
1495 info.mRefreshPending = isBufferLatched();
1496 info.mIsOpaque = isOpaque(ds);
1497 info.mContentDirty = contentDirty;
1498 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001499}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001500
Yiwei Zhang5434a782018-12-05 18:06:32 -08001501void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001502 result.append("-------------------------------");
1503 result.append("-------------------------------");
1504 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001505 result.append(" Layer name\n");
1506 result.append(" Z | ");
1507 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001508 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001509 result.append(" Disp Frame (LTRB) | ");
1510 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001511 result.append("-------------------------------");
1512 result.append("-------------------------------");
1513 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001514}
1515
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001516void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1517 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1518 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001519 return;
1520 }
1521
Yiwei Zhang5434a782018-12-05 18:06:32 -08001522 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001523 if (mName.length() > 77) {
1524 std::string shortened;
1525 shortened.append(mName.string(), 36);
1526 shortened.append("[...]");
1527 shortened.append(mName.string() + (mName.length() - 36), 36);
Yiwei Zhang5434a782018-12-05 18:06:32 -08001528 name = shortened;
Dan Stozae22aec72016-08-01 13:20:59 -07001529 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001530 name = std::string(mName.string(), mName.size());
Dan Stozae22aec72016-08-01 13:20:59 -07001531 }
1532
Yiwei Zhang5434a782018-12-05 18:06:32 -08001533 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001534
Alec Mourib416efd2018-09-06 21:01:59 +00001535 const State& layerState(getDrawingState());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001536 const auto& compositionState = outputLayer->getState();
1537
Chia-I Wu1e043612018-03-01 09:45:09 -08001538 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001539 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001540 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001541 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001542 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001543 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
1544 StringAppendF(&result, "%10s | ",
1545 toString(getCompositionLayer() ? compositionState.bufferTransform
1546 : static_cast<Hwc2::Transform>(0))
1547 .c_str());
1548 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001549 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001550 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001551 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1552 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001553
Yichi Chen6ca35192018-05-29 12:20:43 +08001554 result.append("- - - - - - - - - - - - - - - -");
1555 result.append("- - - - - - - - - - - - - - - -");
1556 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001557}
Dan Stozae22aec72016-08-01 13:20:59 -07001558
Yiwei Zhang5434a782018-12-05 18:06:32 -08001559void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001560 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001561}
1562
Svetoslavd85084b2014-03-20 10:28:31 -07001563void Layer::clearFrameStats() {
1564 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001565}
1566
Jamie Gennis6547ff42013-07-16 20:12:42 -07001567void Layer::logFrameStats() {
1568 mFrameTracker.logAndResetStats(mName);
1569}
1570
Svetoslavd85084b2014-03-20 10:28:31 -07001571void Layer::getFrameStats(FrameStats* outStats) const {
1572 mFrameTracker.getStats(outStats);
1573}
1574
Yiwei Zhang5434a782018-12-05 18:06:32 -08001575void Layer::dumpFrameEvents(std::string& result) {
1576 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001577 Mutex::Autolock lock(mFrameEventHistoryMutex);
1578 mFrameEventHistory.checkFencesForCompletion();
1579 mFrameEventHistory.dump(result);
1580}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001581
Brian Anderson5ea5e592016-12-01 16:54:33 -08001582void Layer::onDisconnect() {
1583 Mutex::Autolock lock(mFrameEventHistoryMutex);
1584 mFrameEventHistory.onDisconnect();
Yiwei Zhangaf8ee942018-11-22 00:15:23 -08001585 mFlinger->mTimeStats->onDestroy(getSequence());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001586}
1587
Brian Anderson3890c392016-07-25 12:48:08 -07001588void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001589 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001590 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001591 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1592 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001593 }
1594
Brian Andersond6927fb2016-07-23 23:37:30 -07001595 Mutex::Autolock lock(mFrameEventHistoryMutex);
1596 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001597 // If there are any unsignaled fences in the aquire timeline at this
1598 // point, the previously queued frame hasn't been latched yet. Go ahead
1599 // and try to get the signal time here so the syscall is taken out of
1600 // the main thread's critical path.
1601 mAcquireTimeline.updateSignalTimes();
1602 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001603 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001604 mFrameEventHistory.addQueue(*newTimestamps);
1605 }
1606
Brian Anderson3890c392016-07-25 12:48:08 -07001607 if (outDelta) {
1608 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001609 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001610}
Dan Stozae77c7662016-05-13 11:37:28 -07001611
Chia-I Wu98f1c102017-05-30 14:54:08 -07001612size_t Layer::getChildrenCount() const {
1613 size_t count = 0;
1614 for (const sp<Layer>& child : mCurrentChildren) {
1615 count += 1 + child->getChildrenCount();
1616 }
1617 return count;
1618}
1619
Robert Carr1f0a16a2016-10-24 16:27:39 -07001620void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001621 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001622 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001623
Robert Carr1f0a16a2016-10-24 16:27:39 -07001624 mCurrentChildren.add(layer);
1625 layer->setParent(this);
1626}
1627
1628ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001629 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001630 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001631
Robert Carr1323c952019-01-28 18:13:27 -08001632 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001633 return mCurrentChildren.remove(layer);
1634}
1635
Robert Carr1db73f62016-12-21 12:58:51 -08001636bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1637 sp<Handle> handle = nullptr;
1638 sp<Layer> newParent = nullptr;
1639 if (newParentHandle == nullptr) {
1640 return false;
1641 }
1642 handle = static_cast<Handle*>(newParentHandle.get());
1643 newParent = handle->owner.promote();
1644 if (newParent == nullptr) {
1645 ALOGE("Unable to promote Layer handle");
1646 return false;
1647 }
1648
chaviw5aedec92018-10-22 10:40:38 -07001649 if (attachChildren()) {
1650 setTransactionFlags(eTransactionNeeded);
1651 }
Robert Carr1db73f62016-12-21 12:58:51 -08001652 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001653 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001654 }
1655 mCurrentChildren.clear();
1656
1657 return true;
1658}
1659
Robert Carr15eae092018-03-23 13:43:53 -07001660void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001661 for (const sp<Layer>& child : mDrawingChildren) {
1662 child->mDrawingParent = newParent;
Vishnu Nair4351ad52019-02-11 14:13:02 -08001663 child->computeBounds(newParent->mBounds, newParent->getTransformWithScale());
Robert Carr578038f2018-03-09 12:25:24 -08001664 }
1665}
1666
chaviwf1961f72017-09-18 16:41:07 -07001667bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001668 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001669
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001670 // While layers are detached, we allow most operations
1671 // and simply halt performing the actual transaction. However
1672 // for reparent != null we would enter the mRemovedFromCurrentState
1673 // state, regardless of whether doTransaction was called, and
1674 // so we need to prevent the update here.
1675 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001676 return false;
1677 }
1678
Robert Carr54cf5b12019-01-25 14:02:28 -08001679 sp<Layer> newParent;
1680 if (newParentHandle != nullptr) {
1681 auto handle = static_cast<Handle*>(newParentHandle.get());
1682 newParent = handle->owner.promote();
1683 if (newParent == nullptr) {
1684 ALOGE("Unable to promote Layer handle");
1685 return false;
1686 }
1687 if (newParent == this) {
1688 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1689 return false;
1690 }
1691 }
1692
chaviwf1961f72017-09-18 16:41:07 -07001693 sp<Layer> parent = getParent();
1694 if (parent != nullptr) {
1695 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001696 }
1697
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001698 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001699 newParent->addChild(this);
1700 if (!newParent->isRemovedFromCurrentState()) {
1701 addToCurrentState();
1702 } else {
1703 onRemovedFromCurrentState();
1704 }
1705
1706 if (mLayerDetached) {
1707 mLayerDetached = false;
1708 callSetTransactionFlags = true;
1709 }
1710 } else {
1711 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001712 }
1713
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001714 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001715 setTransactionFlags(eTransactionNeeded);
1716 }
chaviw06178942017-07-27 10:25:59 -07001717 return true;
1718}
1719
Robert Carr9524cb32017-02-13 11:32:32 -08001720bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001721 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001722 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001723 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001724 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001725 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001726 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001727 }
Robert Carr7f619b22017-11-06 12:56:35 -08001728 }
Robert Carr9524cb32017-02-13 11:32:32 -08001729
1730 return true;
1731}
1732
chaviw5aedec92018-10-22 10:40:38 -07001733bool Layer::attachChildren() {
1734 bool changed = false;
1735 for (const sp<Layer>& child : mCurrentChildren) {
1736 sp<Client> parentClient = mClientRef.promote();
1737 sp<Client> client(child->mClientRef.promote());
1738 if (client != nullptr && parentClient != client) {
1739 if (child->mLayerDetached) {
1740 child->mLayerDetached = false;
1741 changed = true;
1742 }
1743 changed |= child->attachChildren();
1744 }
1745 }
1746
1747 return changed;
1748}
1749
Peiyong Lind3788632018-09-18 16:01:31 -07001750bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001751 static const mat4 identityMatrix = mat4();
1752
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001753 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001754 return false;
1755 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001756 ++mCurrentState.sequence;
1757 mCurrentState.colorTransform = matrix;
1758 mCurrentState.hasColorTransform = matrix != identityMatrix;
1759 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001760 setTransactionFlags(eTransactionNeeded);
1761 return true;
1762}
1763
chaviwf66724d2018-11-28 16:35:21 -08001764mat4 Layer::getColorTransform() const {
1765 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1766 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1767 colorTransform = parent->getColorTransform() * colorTransform;
1768 }
1769 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001770}
1771
1772bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001773 bool hasColorTransform = getDrawingState().hasColorTransform;
1774 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1775 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1776 }
1777 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001778}
1779
Chia-I Wu11481472018-05-04 10:43:19 -07001780bool Layer::isLegacyDataSpace() const {
1781 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001782 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001783 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001784}
1785
Robert Carr1f0a16a2016-10-24 16:27:39 -07001786void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001787 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001788}
1789
1790void Layer::clearSyncPoints() {
1791 for (const auto& child : mCurrentChildren) {
1792 child->clearSyncPoints();
1793 }
1794
1795 Mutex::Autolock lock(mLocalSyncPointMutex);
1796 for (auto& point : mLocalSyncPoints) {
1797 point->setFrameAvailable();
1798 }
1799 mLocalSyncPoints.clear();
1800}
1801
1802int32_t Layer::getZ() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001803 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001804}
1805
Robert Carr29abff82017-12-04 13:51:20 -08001806bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1807 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001808 const State& state = useDrawing ? mDrawingState : mCurrentState;
Robert Carr29abff82017-12-04 13:51:20 -08001809 return state.zOrderRelativeOf != nullptr;
1810}
1811
David Sodman41fdfc92017-11-06 16:09:56 -08001812__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001813 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001814 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1815 "makeTraversalList received invalid stateSet");
1816 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1817 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001818 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001819
Robert Carr29abff82017-12-04 13:51:20 -08001820 if (state.zOrderRelatives.size() == 0) {
1821 *outSkipRelativeZUsers = true;
1822 return children;
1823 }
1824
chaviwfd462612018-05-31 16:11:27 -07001825 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001826 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001827 sp<Layer> strongRelative = weakRelative.promote();
1828 if (strongRelative != nullptr) {
1829 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001830 }
1831 }
1832
Dan Stoza412903f2017-04-27 13:42:17 -07001833 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001834 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
Robert Carr503c7042017-09-27 15:06:08 -07001835 if (childState.zOrderRelativeOf != nullptr) {
1836 continue;
1837 }
Robert Carrdb66e622017-04-10 16:55:57 -07001838 traverse.add(child);
1839 }
1840
1841 return traverse;
1842}
1843
Robert Carr1f0a16a2016-10-24 16:27:39 -07001844/**
Robert Carrdb66e622017-04-10 16:55:57 -07001845 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001846 */
Dan Stoza412903f2017-04-27 13:42:17 -07001847void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001848 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1849 // produce a new list for traversing, including our relatives, and not including our children
1850 // who are relatives of another surface. In the case that there are no relative Z,
1851 // makeTraversalList returns our children directly to avoid significant overhead.
1852 // However in this case we need to take the responsibility for filtering children which
1853 // are relatives of another surface here.
1854 bool skipRelativeZUsers = false;
1855 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001856
Robert Carr1f0a16a2016-10-24 16:27:39 -07001857 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001858 for (; i < list.size(); i++) {
1859 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001860 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1861 continue;
1862 }
1863
Robert Carrdb66e622017-04-10 16:55:57 -07001864 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001865 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001866 }
Dan Stoza412903f2017-04-27 13:42:17 -07001867 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001868 }
Robert Carr29abff82017-12-04 13:51:20 -08001869
Dan Stoza412903f2017-04-27 13:42:17 -07001870 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001871 for (; i < list.size(); i++) {
1872 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001873
Robert Carr29abff82017-12-04 13:51:20 -08001874 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1875 continue;
1876 }
Dan Stoza412903f2017-04-27 13:42:17 -07001877 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001878 }
1879}
1880
1881/**
Robert Carrdb66e622017-04-10 16:55:57 -07001882 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001883 */
Dan Stoza412903f2017-04-27 13:42:17 -07001884void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1885 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001886 // See traverseInZOrder for documentation.
1887 bool skipRelativeZUsers = false;
1888 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001889
Robert Carr1f0a16a2016-10-24 16:27:39 -07001890 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001891 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001892 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001893
1894 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1895 continue;
1896 }
1897
Robert Carrdb66e622017-04-10 16:55:57 -07001898 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001899 break;
1900 }
Dan Stoza412903f2017-04-27 13:42:17 -07001901 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001902 }
Dan Stoza412903f2017-04-27 13:42:17 -07001903 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001904 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001905 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001906
1907 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1908 continue;
1909 }
1910
Dan Stoza412903f2017-04-27 13:42:17 -07001911 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001912 }
1913}
1914
chaviw4b129c22018-04-09 16:19:43 -07001915LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1916 const std::vector<Layer*>& layersInTree) {
1917 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1918 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001919 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1920 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001921 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001922
chaviwfd462612018-05-31 16:11:27 -07001923 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001924 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1925 sp<Layer> strongRelative = weakRelative.promote();
1926 // Only add relative layers that are also descendents of the top most parent of the tree.
1927 // If a relative layer is not a descendent, then it should be ignored.
1928 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1929 traverse.add(strongRelative);
1930 }
1931 }
1932
1933 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001934 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001935 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1936 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1937 // the child here since it won't be added later as a relative.
1938 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1939 childState.zOrderRelativeOf.promote().get())) {
1940 continue;
1941 }
1942 traverse.add(child);
1943 }
1944
1945 return traverse;
1946}
1947
1948void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1949 LayerVector::StateSet stateSet,
1950 const LayerVector::Visitor& visitor) {
1951 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001952
1953 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001954 for (; i < list.size(); i++) {
1955 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001956 if (relative->getZ() >= 0) {
1957 break;
1958 }
chaviw4b129c22018-04-09 16:19:43 -07001959 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001960 }
chaviw4b129c22018-04-09 16:19:43 -07001961
chaviwa76b2712017-09-20 12:02:26 -07001962 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001963 for (; i < list.size(); i++) {
1964 const auto& relative = list[i];
1965 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001966 }
1967}
1968
chaviw4b129c22018-04-09 16:19:43 -07001969std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1970 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1971 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1972
1973 std::vector<Layer*> layersInTree = {this};
1974 for (size_t i = 0; i < children.size(); i++) {
1975 const auto& child = children[i];
1976 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1977 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1978 }
1979
1980 return layersInTree;
1981}
1982
1983void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1984 const LayerVector::Visitor& visitor) {
1985 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1986 std::sort(layersInTree.begin(), layersInTree.end());
1987 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1988}
1989
Peiyong Linefefaac2018-08-17 12:27:51 -07001990ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001991 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001992}
1993
chaviw13fdc492017-06-27 12:40:18 -07001994half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001995 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001996
chaviw13fdc492017-06-27 12:40:18 -07001997 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1998 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001999}
Robert Carr6452f122017-03-21 10:41:29 -07002000
chaviw13fdc492017-06-27 12:40:18 -07002001half4 Layer::getColor() const {
2002 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002003 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07002004}
Robert Carr6452f122017-03-21 10:41:29 -07002005
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002006Layer::RoundedCornerState Layer::getRoundedCornerState() const {
2007 const auto& p = mDrawingParent.promote();
2008 if (p != nullptr) {
2009 RoundedCornerState parentState = p->getRoundedCornerState();
2010 if (parentState.radius > 0) {
2011 ui::Transform t = getActiveTransform(getDrawingState());
2012 t = t.inverse();
2013 parentState.cropRect = t.transform(parentState.cropRect);
2014 // The rounded corners shader only accepts 1 corner radius for performance reasons,
2015 // but a transform matrix can define horizontal and vertical scales.
2016 // Let's take the average between both of them and pass into the shader, practically we
2017 // never do this type of transformation on windows anyway.
2018 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
2019 return parentState;
2020 }
2021 }
2022 const float radius = getDrawingState().cornerRadius;
Vishnu Nair4351ad52019-02-11 14:13:02 -08002023 return radius > 0 ? RoundedCornerState(getBounds(), radius) : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002024}
2025
Robert Carr1f0a16a2016-10-24 16:27:39 -07002026void Layer::commitChildList() {
2027 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
2028 const auto& child = mCurrentChildren[i];
2029 child->commitChildList();
2030 }
2031 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07002032 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002033}
2034
Robert Carr720e5062018-07-30 17:45:14 -07002035void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002036 mCurrentState.inputInfo = info;
2037 mCurrentState.modified = true;
2038 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07002039 setTransactionFlags(eTransactionNeeded);
2040}
2041
chaviw1d044282017-09-27 12:19:28 -07002042void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
2043 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2044 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002045 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07002046
Peiyong Linefefaac2018-08-17 12:27:51 -07002047 ui::Transform requestedTransform = state.active_legacy.transform;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002048 ui::Transform transform = getTransform();
chaviw1d044282017-09-27 12:19:28 -07002049
2050 layerInfo->set_id(sequence);
2051 layerInfo->set_name(getName().c_str());
2052 layerInfo->set_type(String8(getTypeId()));
2053
2054 for (const auto& child : children) {
2055 layerInfo->add_children(child->sequence);
2056 }
2057
2058 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
2059 sp<Layer> strongRelative = weakRelative.promote();
2060 if (strongRelative != nullptr) {
2061 layerInfo->add_relatives(strongRelative->sequence);
2062 }
2063 }
2064
Marissa Wallf58c14b2018-07-24 10:50:43 -07002065 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
Nataniel Borges797b0e42019-02-15 14:11:58 -08002066 [&]() { return layerInfo->mutable_transparent_region(); });
2067 LayerProtoHelper::writeToProto(visibleRegion,
2068 [&]() { return layerInfo->mutable_visible_region(); });
2069 LayerProtoHelper::writeToProto(surfaceDamageRegion,
2070 [&]() { return layerInfo->mutable_damage_region(); });
chaviw1d044282017-09-27 12:19:28 -07002071
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002072 layerInfo->set_layer_stack(getLayerStack());
chaviw1d044282017-09-27 12:19:28 -07002073 layerInfo->set_z(state.z);
2074
Nataniel Borges797b0e42019-02-15 14:11:58 -08002075 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
2076 [&]() { return layerInfo->mutable_position(); });
chaviw1d044282017-09-27 12:19:28 -07002077
Nataniel Borges797b0e42019-02-15 14:11:58 -08002078 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(), [&]() {
2079 return layerInfo->mutable_requested_position();
2080 });
chaviw1d044282017-09-27 12:19:28 -07002081
Nataniel Borges797b0e42019-02-15 14:11:58 -08002082 LayerProtoHelper::writeSizeToProto(state.active_legacy.w, state.active_legacy.h,
2083 [&]() { return layerInfo->mutable_size(); });
chaviw1d044282017-09-27 12:19:28 -07002084
Nataniel Borges797b0e42019-02-15 14:11:58 -08002085 LayerProtoHelper::writeToProto(state.crop_legacy, [&]() { return layerInfo->mutable_crop(); });
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002086 layerInfo->set_corner_radius(getRoundedCornerState().radius);
chaviw1d044282017-09-27 12:19:28 -07002087
2088 layerInfo->set_is_opaque(isOpaque(state));
2089 layerInfo->set_invalidate(contentDirty);
Chia-I Wu01591c92018-05-22 12:03:00 -07002090
2091 // XXX (b/79210409) mCurrentDataSpace is not protected
2092 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
2093
chaviw1d044282017-09-27 12:19:28 -07002094 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
Nataniel Borges797b0e42019-02-15 14:11:58 -08002095 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
2096 LayerProtoHelper::writeToProto(state.color,
2097 [&]() { return layerInfo->mutable_requested_color(); });
chaviw1d044282017-09-27 12:19:28 -07002098 layerInfo->set_flags(state.flags);
2099
2100 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
2101 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
2102
Jorim Jaggi8e0af362017-11-14 16:28:28 +01002103 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07002104 if (parent != nullptr) {
2105 layerInfo->set_parent(parent->sequence);
Nataniel Borgesdcc0bab2019-02-15 13:22:03 -08002106 } else {
2107 layerInfo->set_parent(-1);
chaviw1d044282017-09-27 12:19:28 -07002108 }
2109
2110 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2111 if (zOrderRelativeOf != nullptr) {
2112 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
Nataniel Borgesdcc0bab2019-02-15 13:22:03 -08002113 } else {
2114 layerInfo->set_z_order_relative_of(-1);
chaviw1d044282017-09-27 12:19:28 -07002115 }
2116
Lloyd Pique0b785d82018-12-04 17:25:27 -08002117 auto buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08002118 if (buffer != nullptr) {
Nataniel Borges797b0e42019-02-15 14:11:58 -08002119 LayerProtoHelper::writeToProto(buffer,
2120 [&]() { return layerInfo->mutable_active_buffer(); });
Peiyong Linefefaac2018-08-17 12:27:51 -07002121 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
Yichi Chen6ca35192018-05-29 12:20:43 +08002122 layerInfo->mutable_buffer_transform());
chaviw1d044282017-09-27 12:19:28 -07002123 }
2124
2125 layerInfo->set_queued_frames(getQueuedFrameCount());
2126 layerInfo->set_refresh_pending(isBufferLatched());
chaviwadc40c22018-07-10 16:57:27 -07002127 layerInfo->set_curr_frame(mCurrentFrameNumber);
Vishnu Nairf2deb822018-11-12 17:53:48 -08002128 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
chaviwadc40c22018-07-10 16:57:27 -07002129
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002130 for (const auto& pendingState : mPendingStates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07002131 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
chaviwadc40c22018-07-10 16:57:27 -07002132 if (barrierLayer != nullptr) {
2133 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
2134 barrierLayerProto->set_id(barrierLayer->sequence);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002135 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
chaviwadc40c22018-07-10 16:57:27 -07002136 }
2137 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002138
2139 auto protoMap = layerInfo->mutable_metadata();
2140 for (const auto& entry : state.metadata.mMap) {
2141 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
2142 }
Vishnu Nair4351ad52019-02-11 14:13:02 -08002143 LayerProtoHelper::writeToProto(mEffectiveTransform, layerInfo->mutable_effective_transform());
Nataniel Borges797b0e42019-02-15 14:11:58 -08002144 LayerProtoHelper::writeToProto(mSourceBounds,
2145 [&]() { return layerInfo->mutable_source_bounds(); });
2146 LayerProtoHelper::writeToProto(mScreenBounds,
2147 [&]() { return layerInfo->mutable_screen_bounds(); });
2148 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
chaviw1d044282017-09-27 12:19:28 -07002149}
2150
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002151void Layer::writeToProto(LayerProto* layerInfo, const sp<DisplayDevice>& displayDevice) {
2152 auto outputLayer = findOutputLayerForDisplay(displayDevice);
2153 if (!outputLayer) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07002154 return;
2155 }
2156
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002157 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
2158
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002159 const auto& compositionState = outputLayer->getState();
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002160
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002161 const Rect& frame = compositionState.displayFrame;
Nataniel Borges797b0e42019-02-15 14:11:58 -08002162 LayerProtoHelper::writeToProto(frame, [&]() { return layerInfo->mutable_hwc_frame(); });
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002163
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002164 const FloatRect& crop = compositionState.sourceCrop;
Nataniel Borges797b0e42019-02-15 14:11:58 -08002165 LayerProtoHelper::writeToProto(crop, [&]() { return layerInfo->mutable_hwc_crop(); });
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002166
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002167 const int32_t transform =
2168 getCompositionLayer() ? static_cast<int32_t>(compositionState.bufferTransform) : 0;
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002169 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002170
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002171 const int32_t compositionType =
2172 static_cast<int32_t>(compositionState.hwc ? (*compositionState.hwc).hwcCompositionType
2173 : Hwc2::IComposerClient::Composition::CLIENT);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002174 layerInfo->set_hwc_composition_type(compositionType);
2175
2176 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
2177 static_cast<BufferLayer*>(this)->isProtected()) {
2178 layerInfo->set_is_protected(true);
2179 } else {
2180 layerInfo->set_is_protected(false);
2181 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002182}
2183
Robert Carr2e102c92018-10-23 12:11:15 -07002184bool Layer::isRemovedFromCurrentState() const {
2185 return mRemovedFromCurrentState;
2186}
2187
Arthur Hungd20b2702019-01-14 18:16:16 +08002188InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002189 InputWindowInfo info = mDrawingState.inputInfo;
Robert Carr720e5062018-07-30 17:45:14 -07002190
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002191 if (info.displayId == ADISPLAY_ID_NONE) {
2192 info.displayId = mDrawingState.layerStack;
2193 }
2194
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002195 ui::Transform t = getTransform();
2196 const float xScale = t.sx();
2197 const float yScale = t.sy();
2198 if (xScale != 1.0f || yScale != 1.0f) {
2199 info.windowXScale *= 1.0f / xScale;
2200 info.windowYScale *= 1.0f / yScale;
2201 info.touchableRegion.scaleSelf(xScale, yScale);
Riddle Hsu39d4aa52018-11-30 20:46:53 +08002202 }
Robert Carre07e1032018-11-26 12:55:53 -08002203
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002204 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002205 // If this is a portal window, set the touchableRegion to the layerBounds.
2206 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
2207 ? getBufferSize(getDrawingState())
2208 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002209 if (!layerBounds.isValid()) {
2210 layerBounds = getCroppedBufferSize(getDrawingState());
2211 }
Vishnu Nair8033a492018-12-05 07:27:23 -08002212 layerBounds = t.transform(layerBounds);
2213 layerBounds.inset(info.surfaceInset, info.surfaceInset, info.surfaceInset, info.surfaceInset);
2214
Arthur Hungd20b2702019-01-14 18:16:16 +08002215 // Input coordinate should match the layer bounds.
2216 info.frameLeft = layerBounds.left;
2217 info.frameTop = layerBounds.top;
2218 info.frameRight = layerBounds.right;
2219 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08002220
2221 // Position the touchable region relative to frame screen location and restrict it to frame
2222 // bounds.
2223 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08002224 info.visible = canReceiveInput();
Robert Carr720e5062018-07-30 17:45:14 -07002225 return info;
2226}
2227
2228bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002229 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002230}
2231
Lloyd Piquefeb73d72018-12-04 17:23:44 -08002232std::shared_ptr<compositionengine::Layer> Layer::getCompositionLayer() const {
2233 return nullptr;
2234}
2235
chaviw3e727cd2019-01-31 13:41:05 -08002236bool Layer::canReceiveInput() const {
2237 return isVisible();
2238}
2239
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002240compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
2241 const sp<const DisplayDevice>& display) const {
2242 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionLayer().get());
2243}
2244
Mathias Agopian13127d82013-03-05 17:47:11 -08002245// ---------------------------------------------------------------------------
2246
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002247}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002248
2249#if defined(__gl_h_)
2250#error "don't include gl/gl.h in this file"
2251#endif
2252
2253#if defined(__gl2_h_)
2254#error "don't include gl2/gl2.h in this file"
2255#endif