blob: 5fb8e53ace46393c99427db588a82f469b3b80ca [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 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800150 mCurrentState.zOrderRelativeOf = 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
288 int bufferWidth;
289 int bufferHeight;
290 if ((mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800291 bufferWidth = mActiveBuffer->getWidth();
292 bufferHeight = mActiveBuffer->getHeight();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800293 } else {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800294 bufferHeight = mActiveBuffer->getWidth();
295 bufferWidth = mActiveBuffer->getHeight();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800296 }
297 float sx = getActiveWidth(getDrawingState()) / static_cast<float>(bufferWidth);
298 float sy = getActiveHeight(getDrawingState()) / static_cast<float>(bufferHeight);
299 ui::Transform extraParentScaling;
300 extraParentScaling.set(sx, 0, 0, sy);
301 return mEffectiveTransform * extraParentScaling;
302}
303
304void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform) {
305 const State& s(getDrawingState());
306
307 // Calculate effective layer transform
308 mEffectiveTransform = parentTransform * getActiveTransform(s);
309
310 // Transform parent bounds to layer space
311 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
312
313 // Calculate display frame
314 mSourceBounds = computeSourceBounds(parentBounds);
315
316 // Calculate bounds by croping diplay frame with layer crop and parent bounds
317 FloatRect bounds = mSourceBounds;
318 const Rect layerCrop = getCrop(s);
319 if (!layerCrop.isEmpty()) {
320 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
321 }
322 bounds = bounds.intersect(parentBounds);
323
324 mBounds = bounds;
325 mScreenBounds = mEffectiveTransform.transform(mBounds);
326 for (const sp<Layer>& child : mDrawingChildren) {
327 child->computeBounds(mBounds, getTransformWithScale());
328 }
329}
330
Robert Carrb5d3d262016-03-25 15:08:13 -0700331
Robert Carr1f0a16a2016-10-24 16:27:39 -0700332
Vishnu Nair60356342018-11-13 13:00:45 -0800333Rect Layer::getCroppedBufferSize(const State& s) const {
334 Rect size = getBufferSize(s);
335 Rect crop = getCrop(s);
336 if (!crop.isEmpty() && size.isValid()) {
337 size.intersect(crop, &size);
338 } else if (!crop.isEmpty()) {
339 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700340 }
Vishnu Nair60356342018-11-13 13:00:45 -0800341 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800342}
343
Alec Mouri1f3bd182019-01-24 15:20:18 +0000344Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& display) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700345 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800346 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700347 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800348
349 // apply the projection's clipping to the window crop in
350 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700351 // if there are no window scaling involved, this operation will map to full
352 // pixels in the buffer.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700353
Vishnu Nair4351ad52019-02-11 14:13:02 -0800354 FloatRect activeCropFloat = getBounds();
Alec Mouri1f3bd182019-01-24 15:20:18 +0000355 ui::Transform t = getTransform();
356 // Transform to screen space.
357 activeCropFloat = t.transform(activeCropFloat);
358 activeCropFloat = activeCropFloat.intersect(display->getViewport().toFloatRect());
359 // Back to layer space to work with the content crop.
360 activeCropFloat = t.inverse().transform(activeCropFloat);
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800361 // This needs to be here as transform.transform(Rect) computes the
362 // transformed rect and then takes the bounding box of the result before
363 // returning. This means
364 // transform.inverse().transform(transform.transform(Rect)) != Rect
365 // in which case we need to make sure the final rect is clipped to the
366 // display bounds.
367 Rect activeCrop{activeCropFloat};
368 if (!activeCrop.intersect(getBufferSize(s), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000369 activeCrop.clear();
370 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700371 return activeCrop;
372}
373
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700374void Layer::setupRoundedCornersCropCoordinates(Rect win,
375 const FloatRect& roundedCornersCrop) const {
376 // Translate win by the rounded corners rect coordinates, to have all values in
377 // layer coordinate space.
378 win.left -= roundedCornersCrop.left;
379 win.right -= roundedCornersCrop.left;
380 win.top -= roundedCornersCrop.top;
381 win.bottom -= roundedCornersCrop.top;
382
Lloyd Pique0b785d82018-12-04 17:25:27 -0800383 renderengine::Mesh::VertexArray<vec2> cropCoords(
384 getCompositionLayer()->editState().reMesh.getCropCoordArray<vec2>());
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700385 cropCoords[0] = vec2(win.left, win.top);
386 cropCoords[1] = vec2(win.left, win.top + win.getHeight());
387 cropCoords[2] = vec2(win.right, win.top + win.getHeight());
388 cropCoords[3] = vec2(win.right, win.top);
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700389}
390
Alec Mouri1f3bd182019-01-24 15:20:18 +0000391FloatRect Layer::computeCrop(const sp<const DisplayDevice>& display) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700392 // the content crop is the area of the content that gets scaled to the
393 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800394 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700395
396 // In addition there is a WM-specified crop we pull from our drawing state.
397 const State& s(getDrawingState());
398
Alec Mouri1f3bd182019-01-24 15:20:18 +0000399 Rect activeCrop = computeInitialCrop(display);
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800400 Rect bufferSize = getBufferSize(s);
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700401
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000402 // Transform the window crop to match the buffer coordinate system,
403 // which means using the inverse of the current transform set on the
404 // SurfaceFlingerConsumer.
405 uint32_t invTransform = mCurrentTransform;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800406 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000407 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700408 * the code below applies the primary display's inverse transform to the
409 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000410 */
David Sodman41fdfc92017-11-06 16:09:56 -0800411 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000412 // calculate the inverse transform
413 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800414 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800415 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000416 // and apply to the current transform
Peiyong Linefefaac2018-08-17 12:27:51 -0700417 invTransform = (ui::Transform(invTransformOrient) *
418 ui::Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800419 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000420
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800421 int winWidth = bufferSize.getWidth();
422 int winHeight = bufferSize.getHeight();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000423 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
424 // If the activeCrop has been rotate the ends are rotated but not
425 // the space itself so when transforming ends back we can't rely on
426 // a modification of the axes of rotation. To account for this we
427 // need to reorient the inverse rotation in terms of the current
428 // axes of rotation.
429 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
430 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
431 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800432 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000433 }
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800434 std::swap(winWidth, winHeight);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000435 }
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800436 const Rect winCrop =
437 activeCrop.transform(invTransform, bufferSize.getWidth(), bufferSize.getHeight());
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000438
439 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800440 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000441 float yScale = crop.getHeight() / float(winHeight);
442
David Sodman41fdfc92017-11-06 16:09:56 -0800443 float insetL = winCrop.left * xScale;
444 float insetT = winCrop.top * yScale;
445 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000446 float insetB = (winHeight - winCrop.bottom) * yScale;
447
David Sodman41fdfc92017-11-06 16:09:56 -0800448 crop.left += insetL;
449 crop.top += insetT;
450 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000451 crop.bottom -= insetB;
452
Mathias Agopian13127d82013-03-05 17:47:11 -0800453 return crop;
454}
455
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700456void Layer::setGeometry(const sp<const DisplayDevice>& display, uint32_t z) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800457 const auto outputLayer = findOutputLayerForDisplay(display);
458 LOG_FATAL_IF(!outputLayer);
459 LOG_FATAL_IF(!outputLayer->getState().hwc);
460 auto& hwcLayer = (*outputLayer->getState().hwc).hwcLayer;
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700461
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800462 if (!hasHwcLayer(display)) {
463 ALOGE("[%s] failed to setGeometry: no HWC layer found (%s)", mName.string(),
464 display->getDebugName().c_str());
465 return;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800466 }
467
Lloyd Pique0b785d82018-12-04 17:25:27 -0800468 LOG_FATAL_IF(!getCompositionLayer());
469 auto& commonCompositionState = getCompositionLayer()->editState().frontEnd;
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800470 auto& compositionState = outputLayer->editState();
471
472 // enable this layer
473 compositionState.forceClientComposition = false;
474
475 if (isSecure() && !display->isSecure()) {
476 compositionState.forceClientComposition = true;
477 }
David Sodman15094112018-10-11 09:39:37 -0700478
Mathias Agopian13127d82013-03-05 17:47:11 -0800479 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700480 const State& s(getDrawingState());
Vishnu Naira6733b32018-12-06 18:13:16 -0800481 const Rect bufferSize = getBufferSize(s);
David Revemanecf0fa52017-03-03 11:32:44 -0500482 auto blendMode = HWC2::BlendMode::None;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800483 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800484 blendMode =
485 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800486 }
David Sodman15094112018-10-11 09:39:37 -0700487 auto error = hwcLayer->setBlendMode(blendMode);
488 ALOGE_IF(error != HWC2::Error::None,
489 "[%s] Failed to set blend mode %s:"
490 " %s (%d)",
491 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
492 static_cast<int32_t>(error));
Lloyd Pique0b785d82018-12-04 17:25:27 -0800493 commonCompositionState.blendMode = static_cast<Hwc2::IComposerClient::BlendMode>(blendMode);
Mathias Agopian13127d82013-03-05 17:47:11 -0800494
495 // apply the layer's transform, followed by the display's global transform
496 // here we're guaranteed that the layer's transform preserves rects
Marissa Wall61c58622018-07-18 10:12:20 -0700497 Region activeTransparentRegion(getActiveTransparentRegion(s));
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800498 ui::Transform t = getTransform();
Marissa Wall61c58622018-07-18 10:12:20 -0700499 Rect activeCrop = getCrop(s);
Vishnu Naira6733b32018-12-06 18:13:16 -0800500 if (!activeCrop.isEmpty() && bufferSize.isValid()) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700501 activeCrop = t.transform(activeCrop);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700502 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000503 activeCrop.clear();
504 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700505 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800506 // This needs to be here as transform.transform(Rect) computes the
507 // transformed rect and then takes the bounding box of the result before
508 // returning. This means
509 // transform.inverse().transform(transform.transform(Rect)) != Rect
510 // in which case we need to make sure the final rect is clipped to the
511 // display bounds.
Vishnu Naira6733b32018-12-06 18:13:16 -0800512 if (!activeCrop.intersect(bufferSize, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000513 activeCrop.clear();
514 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700515 // mark regions outside the crop as transparent
Vishnu Naira6733b32018-12-06 18:13:16 -0800516 activeTransparentRegion.orSelf(Rect(0, 0, bufferSize.getWidth(), activeCrop.top));
517 activeTransparentRegion.orSelf(
518 Rect(0, activeCrop.bottom, bufferSize.getWidth(), bufferSize.getHeight()));
David Sodman41fdfc92017-11-06 16:09:56 -0800519 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
520 activeTransparentRegion.orSelf(
Vishnu Naira6733b32018-12-06 18:13:16 -0800521 Rect(activeCrop.right, activeCrop.top, bufferSize.getWidth(), activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700522 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700523
Vishnu Nair4351ad52019-02-11 14:13:02 -0800524 // getBounds returns a FloatRect to provide more accuracy during the
Dan Stoza80d61162017-12-20 15:57:52 -0800525 // transformation. We then round upon constructing 'frame'.
Vishnu Nair4351ad52019-02-11 14:13:02 -0800526 Rect frame{t.transform(getBounds(activeTransparentRegion))};
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700527 if (!frame.intersect(display->getViewport(), &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000528 frame.clear();
529 }
Peiyong Linefefaac2018-08-17 12:27:51 -0700530 const ui::Transform& tr = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800531 Rect transformedFrame = tr.transform(frame);
David Sodman15094112018-10-11 09:39:37 -0700532 error = hwcLayer->setDisplayFrame(transformedFrame);
533 if (error != HWC2::Error::None) {
534 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
535 transformedFrame.left, transformedFrame.top, transformedFrame.right,
536 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
537 } else {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800538 compositionState.displayFrame = transformedFrame;
David Sodman15094112018-10-11 09:39:37 -0700539 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800540
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700541 FloatRect sourceCrop = computeCrop(display);
David Sodman15094112018-10-11 09:39:37 -0700542 error = hwcLayer->setSourceCrop(sourceCrop);
543 if (error != HWC2::Error::None) {
544 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
545 "%s (%d)",
546 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
547 to_string(error).c_str(), static_cast<int32_t>(error));
548 } else {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800549 compositionState.sourceCrop = sourceCrop;
David Sodman15094112018-10-11 09:39:37 -0700550 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800551
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800552 float alpha = static_cast<float>(getAlpha());
David Sodman15094112018-10-11 09:39:37 -0700553 error = hwcLayer->setPlaneAlpha(alpha);
554 ALOGE_IF(error != HWC2::Error::None,
555 "[%s] Failed to set plane alpha %.3f: "
556 "%s (%d)",
557 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Pique0b785d82018-12-04 17:25:27 -0800558 commonCompositionState.alpha = alpha;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800559
David Sodman15094112018-10-11 09:39:37 -0700560 error = hwcLayer->setZOrder(z);
561 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
562 to_string(error).c_str(), static_cast<int32_t>(error));
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800563 compositionState.z = z;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500564
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800565 int type = s.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
566 int appId = s.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700567 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400568 if (parent.get()) {
569 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800570 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
571 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
572 if (parentType >= 0 || parentAppId >= 0) {
573 type = parentType;
574 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700575 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400576 }
577
David Sodman15094112018-10-11 09:39:37 -0700578 error = hwcLayer->setInfo(type, appId);
579 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
580 static_cast<int32_t>(error));
581
Lloyd Pique0b785d82018-12-04 17:25:27 -0800582 commonCompositionState.type = type;
583 commonCompositionState.appId = appId;
David Sodmanba340492018-08-05 21:51:33 -0700584
Mathias Agopian29a367b2011-07-12 14:51:45 -0700585 /*
586 * Transformations are applied in this order:
587 * 1) buffer orientation/flip/mirror
588 * 2) state transformation (window manager)
589 * 3) layer orientation (screen orientation)
590 * (NOTE: the matrices are multiplied in reverse order)
591 */
592
Peiyong Linefefaac2018-08-17 12:27:51 -0700593 const ui::Transform bufferOrientation(mCurrentTransform);
594 ui::Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700595
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800596 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700597 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700598 * the code below applies the primary display's inverse transform to the
599 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700600 */
David Sodman41fdfc92017-11-06 16:09:56 -0800601 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700602 // calculate the inverse transform
603 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800604 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700605 }
Robert Carrcae605c2017-03-29 12:10:31 -0700606
607 /*
608 * Here we cancel out the orientation component of the WM transform.
609 * The scaling and translate components are already included in our bounds
610 * computation so it's enough to just omit it in the composition.
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000611 * See comment in BufferLayer::prepareClientLayer with ref to b/36727915 for why.
Robert Carrcae605c2017-03-29 12:10:31 -0700612 */
Peiyong Linefefaac2018-08-17 12:27:51 -0700613 transform = ui::Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700614 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700615
616 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800617 const uint32_t orientation = transform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -0700618 if (orientation & ui::Transform::ROT_INVALID) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800619 // we can only handle simple transformation
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800620 compositionState.forceClientComposition = true;
621 (*compositionState.hwc).hwcCompositionType = Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800622 } else {
623 auto transform = static_cast<HWC2::Transform>(orientation);
David Sodman15094112018-10-11 09:39:37 -0700624 auto error = hwcLayer->setTransform(transform);
625 ALOGE_IF(error != HWC2::Error::None,
626 "[%s] Failed to set transform %s: "
627 "%s (%d)",
628 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
629 static_cast<int32_t>(error));
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800630 compositionState.bufferTransform = static_cast<Hwc2::Transform>(transform);
David Sodman4b7c4bc2017-11-17 12:13:59 -0800631 }
632}
633
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800634void Layer::forceClientComposition(const sp<DisplayDevice>& display) {
635 const auto outputLayer = findOutputLayerForDisplay(display);
636 LOG_FATAL_IF(!outputLayer);
637 outputLayer->editState().forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800638}
Dan Stozaee44edd2015-03-23 15:50:23 -0700639
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800640bool Layer::getForceClientComposition(const sp<DisplayDevice>& display) {
641 const auto outputLayer = findOutputLayerForDisplay(display);
642 LOG_FATAL_IF(!outputLayer);
643 return outputLayer->getState().forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800644}
645
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700646void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800647 const auto outputLayer = findOutputLayerForDisplay(display);
648 LOG_FATAL_IF(!outputLayer);
649
650 if (!outputLayer->getState().hwc ||
651 (*outputLayer->getState().hwc).hwcCompositionType !=
652 Hwc2::IComposerClient::Composition::CURSOR) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800653 return;
654 }
655
656 // This gives us only the "orientation" component of the transform
Vishnu Nair33a6eee2019-02-06 13:48:06 -0800657 const State& s(getDrawingState());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800658
659 // Apply the layer's transform, followed by the display's global transform
660 // Here we're guaranteed that the layer's transform preserves rects
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800661 Rect win = getCroppedBufferSize(s);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800662 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700663 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800664 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700665 frame.intersect(display->getViewport(), &frame);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700666 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800667 auto position = displayTransform.transform(frame);
668
Dominik Laskowski7e045462018-05-30 13:02:02 -0700669 auto error =
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800670 (*outputLayer->getState().hwc).hwcLayer->setCursorPosition(position.left, position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800671 ALOGE_IF(error != HWC2::Error::None,
672 "[%s] Failed to set cursor position "
673 "to (%d, %d): %s (%d)",
674 mName.string(), position.left, position.top, to_string(error).c_str(),
675 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800676}
Riley Andrews03414a12014-07-01 14:22:59 -0700677
Mathias Agopian13127d82013-03-05 17:47:11 -0800678// ---------------------------------------------------------------------------
679// drawing...
680// ---------------------------------------------------------------------------
681
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000682bool Layer::prepareClientLayer(const RenderArea& renderArea, const Region& clip,
683 Region& clearRegion, renderengine::LayerSettings& layer) {
684 return prepareClientLayer(renderArea, clip, false, clearRegion, layer);
Mathias Agopian13127d82013-03-05 17:47:11 -0800685}
686
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000687bool Layer::prepareClientLayer(const RenderArea& renderArea, bool useIdentityTransform,
688 Region& clearRegion, renderengine::LayerSettings& layer) {
689 return prepareClientLayer(renderArea, Region(renderArea.getBounds()), useIdentityTransform,
690 clearRegion, layer);
691}
692
693bool Layer::prepareClientLayer(const RenderArea& /*renderArea*/, const Region& /*clip*/,
694 bool useIdentityTransform, Region& /*clearRegion*/,
695 renderengine::LayerSettings& layer) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800696 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000697 half alpha = getAlpha();
698 layer.geometry.boundaries = bounds;
699 if (useIdentityTransform) {
700 layer.geometry.positionTransform = mat4();
701 } else {
702 const ui::Transform transform = getTransform();
703 mat4 m;
704 m[0][0] = transform[0][0];
705 m[0][1] = transform[0][1];
706 m[0][3] = transform[0][2];
707 m[1][0] = transform[1][0];
708 m[1][1] = transform[1][1];
709 m[1][3] = transform[1][2];
710 m[3][0] = transform[2][0];
711 m[3][1] = transform[2][1];
712 m[3][3] = transform[2][2];
713 layer.geometry.positionTransform = m;
714 }
715
716 if (hasColorTransform()) {
717 layer.colorTransform = getColorTransform();
718 }
719
720 const auto roundedCornerState = getRoundedCornerState();
721 layer.geometry.roundedCornersRadius = roundedCornerState.radius;
722 layer.geometry.roundedCornersCrop = roundedCornerState.cropRect;
723
724 layer.alpha = alpha;
725 layer.sourceDataspace = mCurrentDataSpace;
726 return true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800727}
728
David Sodman41fdfc92017-11-06 16:09:56 -0800729void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
730 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800731 auto& engine(mFlinger->getRenderEngine());
Lloyd Pique0b785d82018-12-04 17:25:27 -0800732 computeGeometry(renderArea, getCompositionLayer()->editState().reMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700733 engine.setupFillWithColor(red, green, blue, alpha);
Lloyd Pique0b785d82018-12-04 17:25:27 -0800734 engine.drawMesh(getCompositionLayer()->getState().reMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800735}
736
David Sodmanc1498e62018-09-12 14:36:26 -0700737void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
738 clearWithOpenGL(renderArea, 0, 0, 0, 0);
739}
740
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800741void Layer::setCompositionType(const sp<const DisplayDevice>& display,
742 Hwc2::IComposerClient::Composition type) {
743 const auto outputLayer = findOutputLayerForDisplay(display);
744 LOG_FATAL_IF(!outputLayer);
745 LOG_FATAL_IF(!outputLayer->getState().hwc);
746 auto& compositionState = outputLayer->editState();
747
748 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", ((*compositionState.hwc).hwcLayer)->getId(),
749 toString(type).c_str(), 1);
750 if ((*compositionState.hwc).hwcCompositionType != type) {
David Sodman15094112018-10-11 09:39:37 -0700751 ALOGV(" actually setting");
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800752 (*compositionState.hwc).hwcCompositionType = type;
753
754 auto error = (*compositionState.hwc)
755 .hwcLayer->setCompositionType(static_cast<HWC2::Composition>(type));
756 ALOGE_IF(error != HWC2::Error::None,
757 "[%s] Failed to set "
758 "composition type %s: %s (%d)",
759 mName.string(), toString(type).c_str(), to_string(error).c_str(),
760 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800761 }
762}
763
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800764Hwc2::IComposerClient::Composition Layer::getCompositionType(
765 const sp<const DisplayDevice>& display) const {
766 const auto outputLayer = findOutputLayerForDisplay(display);
767 LOG_FATAL_IF(!outputLayer);
768 return outputLayer->getState().hwc ? (*outputLayer->getState().hwc).hwcCompositionType
769 : Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800770}
771
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800772bool Layer::getClearClientTarget(const sp<const DisplayDevice>& display) const {
773 const auto outputLayer = findOutputLayerForDisplay(display);
774 LOG_FATAL_IF(!outputLayer);
775 return outputLayer->getState().clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800776}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800777
Dan Stozacac35382016-01-27 12:21:06 -0800778bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
779 if (point->getFrameNumber() <= mCurrentFrameNumber) {
780 // Don't bother with a SyncPoint, since we've already latched the
781 // relevant frame
782 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700783 }
Robert Carr2e102c92018-10-23 12:11:15 -0700784 if (isRemovedFromCurrentState()) {
785 return false;
786 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700787
Dan Stozacac35382016-01-27 12:21:06 -0800788 Mutex::Autolock lock(mLocalSyncPointMutex);
789 mLocalSyncPoints.push_back(point);
790 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700791}
792
Mathias Agopian13127d82013-03-05 17:47:11 -0800793// ----------------------------------------------------------------------------
794// local state
795// ----------------------------------------------------------------------------
796
Peiyong Lin833074a2018-08-28 11:53:54 -0700797void Layer::computeGeometry(const RenderArea& renderArea,
798 renderengine::Mesh& mesh,
chaviwa76b2712017-09-20 12:02:26 -0700799 bool useIdentityTransform) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700800 const ui::Transform renderAreaTransform(renderArea.getTransform());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800801 FloatRect win = getBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700802
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000803 vec2 lt = vec2(win.left, win.top);
804 vec2 lb = vec2(win.left, win.bottom);
805 vec2 rb = vec2(win.right, win.bottom);
806 vec2 rt = vec2(win.right, win.top);
807
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800808 ui::Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000809 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700810 lt = layerTransform.transform(lt);
811 lb = layerTransform.transform(lb);
812 rb = layerTransform.transform(rb);
813 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000814 }
815
Peiyong Lin833074a2018-08-28 11:53:54 -0700816 renderengine::Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700817 position[0] = renderAreaTransform.transform(lt);
818 position[1] = renderAreaTransform.transform(lb);
819 position[2] = renderAreaTransform.transform(rb);
820 position[3] = renderAreaTransform.transform(rt);
Mathias Agopian13127d82013-03-05 17:47:11 -0800821}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800822
David Sodman41fdfc92017-11-06 16:09:56 -0800823bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800824 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700825 return (s.flags & layer_state_t::eLayerSecure);
826}
827
Mathias Agopian13127d82013-03-05 17:47:11 -0800828void Layer::setVisibleRegion(const Region& visibleRegion) {
829 // always called from main thread
830 this->visibleRegion = visibleRegion;
831}
832
833void Layer::setCoveredRegion(const Region& coveredRegion) {
834 // always called from main thread
835 this->coveredRegion = coveredRegion;
836}
837
David Sodman41fdfc92017-11-06 16:09:56 -0800838void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800839 // always called from main thread
840 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
841}
842
Robert Carre5f4f692018-01-12 13:12:28 -0800843void Layer::clearVisibilityRegions() {
844 visibleRegion.clear();
845 visibleNonTransparentRegion.clear();
846 coveredRegion.clear();
847}
848
Mathias Agopian13127d82013-03-05 17:47:11 -0800849// ----------------------------------------------------------------------------
850// transaction
851// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800852
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800853void Layer::pushPendingState() {
854 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700855 return;
856 }
857
Dan Stoza7dde5992015-05-22 09:51:44 -0700858 // If this transaction is waiting on the receipt of a frame, generate a sync
859 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700860 // We don't allow installing sync points after we are removed from the current state
861 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800862 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
863 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800864 if (barrierLayer == nullptr) {
865 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700866 // If we can't promote the layer we are intended to wait on,
867 // then it is expired or otherwise invalid. Allow this transaction
868 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800869 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800870 } else {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800871 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy);
Robert Carr0d480722017-01-10 16:42:54 -0800872 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800873 mRemoteSyncPoints.push_back(std::move(syncPoint));
874 } else {
875 // We already missed the frame we're supposed to synchronize
876 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800877 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800878 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700879 }
880
Dan Stoza7dde5992015-05-22 09:51:44 -0700881 // Wake us up to check if the frame has been received
882 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700883 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700884 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800885 mPendingStates.push_back(mCurrentState);
886 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700887}
888
Pablo Ceballos05289c22016-04-14 15:49:55 -0700889void Layer::popPendingState(State* stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800890 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700891
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800892 mPendingStates.removeAt(0);
893 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700894}
895
Pablo Ceballos05289c22016-04-14 15:49:55 -0700896bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700897 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800898 while (!mPendingStates.empty()) {
899 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700900 if (mRemoteSyncPoints.empty()) {
901 // If we don't have a sync point for this, apply it anyway. It
902 // will be visually wrong, but it should keep us from getting
903 // into too much trouble.
904 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700905 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700906 stateUpdateAvailable = true;
907 continue;
908 }
909
Marissa Wallf58c14b2018-07-24 10:50:43 -0700910 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800911 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800912 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800913
914 // Signal our end of the sync point and then dispose of it
915 mRemoteSyncPoints.front()->setTransactionApplied();
916 mRemoteSyncPoints.pop_front();
917 continue;
918 }
919
Dan Stoza7dde5992015-05-22 09:51:44 -0700920 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
921 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700922 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700923 stateUpdateAvailable = true;
924
925 // Signal our end of the sync point and then dispose of it
926 mRemoteSyncPoints.front()->setTransactionApplied();
927 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800928 } else {
929 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700930 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700931 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700932 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700933 stateUpdateAvailable = true;
934 }
935 }
936
937 // If we still have pending updates, wake SurfaceFlinger back up and point
938 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800939 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700940 setTransactionFlags(eTransactionNeeded);
941 mFlinger->setTransactionFlags(eTraversalNeeded);
942 }
943
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800944 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700945 return stateUpdateAvailable;
946}
947
Marissa Wall61c58622018-07-18 10:12:20 -0700948uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000949 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800950
Marissa Wall61c58622018-07-18 10:12:20 -0700951 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
952 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700953
David Sodmaneb085e02017-10-05 18:49:04 -0700954 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700955 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000956 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800957 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
958 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
959 " requested={ wh={%4u,%4u} }}\n"
960 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
961 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700962 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700963 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
964 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
965 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
966 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
967 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700968 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
969 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
970 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800971 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700972
Robert Carre392b552017-09-19 12:16:05 -0700973 // Don't let Layer::doTransaction update the drawing state
974 // if we have a pending resize, unless we are in fixed-size mode.
975 // the drawing state will be updated only once we receive a buffer
976 // with the correct size.
977 //
978 // In particular, we want to make sure the clip (which is part
979 // of the geometry state) is latched together with the size but is
980 // latched immediately when no resizing is involved.
981 //
982 // If a sideband stream is attached, however, we want to skip this
983 // optimization so that transactions aren't missed when a buffer
984 // never arrives
985 //
986 // In the case that we don't have a buffer we ignore other factors
987 // and avoid entering the resizePending state. At a high level the
988 // resizePending state is to avoid applying the state of the new buffer
989 // to the old buffer. However in the state where we don't have an old buffer
990 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700991 const bool resizePending =
992 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
993 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
Lloyd Pique0b785d82018-12-04 17:25:27 -0800994 (mActiveBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700995 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800996 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700997 flags |= eDontUpdateGeometryState;
998 }
999 }
1000
Robert Carr7bf247e2017-05-18 14:02:49 -07001001 // Here we apply various requested geometry states, depending on our
1002 // latching configuration. See Layer.h for a detailed discussion of
1003 // how geometry latching is controlled.
1004 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +00001005 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001006
1007 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1008 // mode, which causes attributes which normally latch regardless of scaling mode,
1009 // to be delayed. We copy the requested state to the active state making sure
1010 // to respect these rules (again see Layer.h for a detailed discussion).
1011 //
1012 // There is an awkward asymmetry in the handling of the crop states in the position
1013 // states, as can be seen below. Largely this arises from position and transform
1014 // being stored in the same data structure while having different latching rules.
1015 // b/38182305
1016 //
Marissa Wall61c58622018-07-18 10:12:20 -07001017 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -07001018 // applyPendingStates in the presence of deferred transactions.
1019 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -07001020 float tx = stateToCommit->active_legacy.transform.tx();
1021 float ty = stateToCommit->active_legacy.transform.ty();
1022 stateToCommit->active_legacy = stateToCommit->requested_legacy;
1023 stateToCommit->active_legacy.transform.set(tx, ty);
1024 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001025 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001026 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -07001027 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001028 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001029 }
1030
Marissa Wall61c58622018-07-18 10:12:20 -07001031 return flags;
1032}
1033
1034uint32_t Layer::doTransaction(uint32_t flags) {
1035 ATRACE_CALL();
1036
chaviw5aedec92018-10-22 10:40:38 -07001037 if (mLayerDetached) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001038 return flags;
1039 }
1040
1041 if (mChildrenChanged) {
1042 flags |= eVisibleRegion;
1043 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -07001044 }
1045
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001046 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +00001047 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -07001048 if (!applyPendingStates(&c)) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001049 return flags;
Marissa Wall61c58622018-07-18 10:12:20 -07001050 }
1051
1052 flags = doTransactionResize(flags, &c);
1053
Alec Mourib416efd2018-09-06 21:01:59 +00001054 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -07001055
1056 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001057 // invalidate and recompute the visible regions if needed
1058 flags |= Layer::eVisibleRegion;
1059 }
1060
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001061 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001062 // invalidate and recompute the visible regions if needed
1063 flags |= eVisibleRegion;
1064 this->contentDirty = true;
1065
1066 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -07001067 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -07001068 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -08001069 }
1070
Dan Stozac8145172016-04-28 16:29:06 -07001071 // If the layer is hidden, signal and clear out all local sync points so
1072 // that transactions for layers depending on this layer's frames becoming
1073 // visible are not blocked
1074 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001075 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001076 }
1077
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001078 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -07001079 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001080 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -07001081 }
1082
Mathias Agopian13127d82013-03-05 17:47:11 -08001083 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001084 commitTransaction(c);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001085 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -08001086 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001087}
1088
Pablo Ceballos05289c22016-04-14 15:49:55 -07001089void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001090 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001091}
1092
Mathias Agopian13127d82013-03-05 17:47:11 -08001093uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001094 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -08001095}
1096
1097uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001098 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -08001099}
1100
Robert Carr82364e32016-05-15 11:27:47 -07001101bool Layer::setPosition(float x, float y, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001102 if (mCurrentState.requested_legacy.transform.tx() == x &&
1103 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001104 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001105 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001106
1107 // We update the requested and active position simultaneously because
1108 // we want to apply the position portion of the transform matrix immediately,
1109 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001110 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001111 if (immediate && !mFreezeGeometryUpdates) {
1112 // Here we directly update the active state
1113 // unlike other setters, because we store it within
1114 // the transform, but use different latching rules.
1115 // b/38182305
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001116 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001117 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001118 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001119
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001120 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001121 setTransactionFlags(eTransactionNeeded);
1122 return true;
1123}
Robert Carr82364e32016-05-15 11:27:47 -07001124
Robert Carr1f0a16a2016-10-24 16:27:39 -07001125bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1126 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1127 if (idx < 0) {
1128 return false;
1129 }
1130 if (childLayer->setLayer(z)) {
1131 mCurrentChildren.removeAt(idx);
1132 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001133 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001134 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001135 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001136}
1137
Robert Carr503c7042017-09-27 15:06:08 -07001138bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1139 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1140 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1141 if (idx < 0) {
1142 return false;
1143 }
1144 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1145 mCurrentChildren.removeAt(idx);
1146 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001147 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001148 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001149 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001150}
1151
Robert Carrae060832016-11-28 10:51:00 -08001152bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001153 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
1154 mCurrentState.sequence++;
1155 mCurrentState.z = z;
1156 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001157
1158 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001159 if (mCurrentState.zOrderRelativeOf != nullptr) {
1160 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -07001161 if (strongRelative != nullptr) {
1162 strongRelative->removeZOrderRelative(this);
1163 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001164 mCurrentState.zOrderRelativeOf = nullptr;
Robert Carrdb66e622017-04-10 16:55:57 -07001165 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001166 setTransactionFlags(eTransactionNeeded);
1167 return true;
1168}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001169
Robert Carrdb66e622017-04-10 16:55:57 -07001170void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001171 mCurrentState.zOrderRelatives.remove(relative);
1172 mCurrentState.sequence++;
1173 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001174 setTransactionFlags(eTransactionNeeded);
1175}
1176
1177void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001178 mCurrentState.zOrderRelatives.add(relative);
1179 mCurrentState.modified = true;
1180 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -07001181 setTransactionFlags(eTransactionNeeded);
1182}
1183
Robert Carr503d2bd2017-12-04 15:49:47 -08001184bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001185 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1186 if (handle == nullptr) {
1187 return false;
1188 }
1189 sp<Layer> relative = handle->owner.promote();
1190 if (relative == nullptr) {
1191 return false;
1192 }
1193
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001194 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1195 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001196 return false;
1197 }
1198
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001199 mCurrentState.sequence++;
1200 mCurrentState.modified = true;
1201 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001202
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001203 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -07001204 if (oldZOrderRelativeOf != nullptr) {
1205 oldZOrderRelativeOf->removeZOrderRelative(this);
1206 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001207 mCurrentState.zOrderRelativeOf = relative;
Robert Carrdb66e622017-04-10 16:55:57 -07001208 relative->addZOrderRelative(this);
1209
1210 setTransactionFlags(eTransactionNeeded);
1211
1212 return true;
1213}
1214
Mathias Agopian13127d82013-03-05 17:47:11 -08001215bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001216 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001217 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001218 mCurrentState.requested_legacy.w = w;
1219 mCurrentState.requested_legacy.h = h;
1220 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001221 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001222
1223 // record the new size, from this point on, when the client request
1224 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001225 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001226 return true;
1227}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001228bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001229 if (mCurrentState.color.a == alpha) return false;
1230 mCurrentState.sequence++;
1231 mCurrentState.color.a = alpha;
1232 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001233 setTransactionFlags(eTransactionNeeded);
1234 return true;
1235}
chaviw13fdc492017-06-27 12:40:18 -07001236
Valerie Haudd0b7572019-01-29 14:59:27 -08001237bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1238 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001239 return false;
Valerie Hauaa194562019-02-05 16:21:38 -08001240 }
1241 mCurrentState.sequence++;
1242 mCurrentState.modified = true;
1243 setTransactionFlags(eTransactionNeeded);
1244
1245 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001246 // create background color layer if one does not yet exist
1247 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
1248 const String8& name = mName + "BackgroundColorLayer";
1249 mCurrentState.bgColorLayer =
1250 new ColorLayer(LayerCreationArgs(mFlinger.get(), nullptr, name, 0, 0, flags));
chaviw13fdc492017-06-27 12:40:18 -07001251
Valerie Haudd0b7572019-01-29 14:59:27 -08001252 // add to child list
1253 addChild(mCurrentState.bgColorLayer);
1254 mFlinger->mLayersAdded = true;
1255 // set up SF to handle added color layer
1256 if (isRemovedFromCurrentState()) {
1257 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1258 }
1259 mFlinger->setTransactionFlags(eTransactionNeeded);
1260 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1261 mCurrentState.bgColorLayer->reparent(nullptr);
1262 mCurrentState.bgColorLayer = nullptr;
1263 return true;
1264 }
1265
1266 mCurrentState.bgColorLayer->setColor(color);
1267 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1268 mCurrentState.bgColorLayer->setAlpha(alpha);
1269 mCurrentState.bgColorLayer->setDataspace(dataspace);
1270
chaviw13fdc492017-06-27 12:40:18 -07001271 return true;
1272}
1273
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001274bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001275 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001276
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001277 mCurrentState.sequence++;
1278 mCurrentState.cornerRadius = cornerRadius;
1279 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001280 setTransactionFlags(eTransactionNeeded);
1281 return true;
1282}
1283
Robert Carrd4ae7f32018-06-07 16:10:57 -07001284bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1285 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001286 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001287 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1288
1289 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1290 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1291 return false;
1292 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001293 mCurrentState.sequence++;
1294 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1295 matrix.dsdy);
1296 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001297 setTransactionFlags(eTransactionNeeded);
1298 return true;
1299}
Marissa Wall61c58622018-07-18 10:12:20 -07001300
Mathias Agopian13127d82013-03-05 17:47:11 -08001301bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001302 mCurrentState.requestedTransparentRegion_legacy = transparent;
1303 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001304 setTransactionFlags(eTransactionNeeded);
1305 return true;
1306}
1307bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001308 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1309 if (mCurrentState.flags == newFlags) return false;
1310 mCurrentState.sequence++;
1311 mCurrentState.flags = newFlags;
1312 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001313 setTransactionFlags(eTransactionNeeded);
1314 return true;
1315}
Robert Carr99e27f02016-06-16 15:18:02 -07001316
Marissa Wallf58c14b2018-07-24 10:50:43 -07001317bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001318 if (mCurrentState.requestedCrop_legacy == crop) return false;
1319 mCurrentState.sequence++;
1320 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001321 if (immediate && !mFreezeGeometryUpdates) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001322 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001323 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001324 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1325
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001326 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001327 setTransactionFlags(eTransactionNeeded);
1328 return true;
1329}
Robert Carr8d5227b2017-03-16 15:41:03 -07001330
Robert Carrc3574f72016-03-24 12:19:32 -07001331bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001332 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001333 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001334 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001335 return true;
1336}
1337
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001338bool Layer::setMetadata(LayerMetadata data) {
1339 bool changed = data.mMap != mCurrentState.metadata.mMap;
1340 if (!changed) return false;
1341 mCurrentState.metadata = std::move(data);
1342 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001343 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001344 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001345 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001346}
1347
Mathias Agopian13127d82013-03-05 17:47:11 -08001348bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001349 if (mCurrentState.layerStack == layerStack) return false;
1350 mCurrentState.sequence++;
1351 mCurrentState.layerStack = layerStack;
1352 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001353 setTransactionFlags(eTransactionNeeded);
1354 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001355}
1356
Robert Carr1f0a16a2016-10-24 16:27:39 -07001357uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001358 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001359 if (p == nullptr) {
1360 return getDrawingState().layerStack;
1361 }
1362 return p->getLayerStack();
1363}
1364
Marissa Wallf58c14b2018-07-24 10:50:43 -07001365void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001366 mCurrentState.barrierLayer_legacy = barrierLayer;
1367 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001368 // We don't set eTransactionNeeded, because just receiving a deferral
1369 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001370 mCurrentState.modified = true;
1371 pushPendingState();
1372 mCurrentState.barrierLayer_legacy = nullptr;
1373 mCurrentState.frameNumber_legacy = 0;
1374 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001375}
1376
Marissa Wallf58c14b2018-07-24 10:50:43 -07001377void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001378 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001379 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001380}
1381
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001382// ----------------------------------------------------------------------------
1383// pageflip handling...
1384// ----------------------------------------------------------------------------
1385
Robert Carr1f0a16a2016-10-24 16:27:39 -07001386bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001387 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001388 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001389 if (parent != nullptr && parent->isHiddenByPolicy()) {
1390 return true;
1391 }
1392 return s.flags & layer_state_t::eLayerHidden;
1393}
1394
David Sodman41fdfc92017-11-06 16:09:56 -08001395uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001396 // TODO: should we do something special if mSecure is set?
1397 if (mProtectedByApp) {
1398 // need a hardware-protected path to external video sink
1399 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001400 }
Riley Andrews03414a12014-07-01 14:22:59 -07001401 if (mPotentialCursor) {
1402 usage |= GraphicBuffer::USAGE_CURSOR;
1403 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001404 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001405 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001406}
1407
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001408void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001409 uint32_t orientation = 0;
Vishnu Nairf2deb822018-11-12 17:53:48 -08001410 // Disable setting transform hint if the debug flag is set or if the
1411 // getTransformToDisplayInverse flag is set and the client wants to submit buffers
1412 // in one orientation.
1413 if (!mFlinger->mDebugDisableTransformHint && !getTransformToDisplayInverse()) {
Mathias Agopian84300952012-11-21 16:02:13 -08001414 // The transform hint is used to improve performance, but we can
1415 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001416 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001417 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001418 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001419 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001420 orientation = 0;
1421 }
1422 }
David Sodmaneb085e02017-10-05 18:49:04 -07001423 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001424}
1425
Mathias Agopian13127d82013-03-05 17:47:11 -08001426// ----------------------------------------------------------------------------
1427// debugging
1428// ----------------------------------------------------------------------------
1429
Marissa Wall61c58622018-07-18 10:12:20 -07001430// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001431LayerDebugInfo Layer::getLayerDebugInfo() const {
1432 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001433 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001434 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001435 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001436 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
Yiwei Zhang5434a782018-12-05 18:06:32 -08001437 info.mType = std::string(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001438 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001439 info.mVisibleRegion = visibleRegion;
1440 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001441 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001442 info.mX = ds.active_legacy.transform.tx();
1443 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001444 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001445 info.mWidth = ds.active_legacy.w;
1446 info.mHeight = ds.active_legacy.h;
1447 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001448 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001449 info.mFlags = ds.flags;
1450 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001451 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001452 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1453 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1454 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1455 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001456 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001457 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001458 if (buffer != 0) {
1459 info.mActiveBufferWidth = buffer->getWidth();
1460 info.mActiveBufferHeight = buffer->getHeight();
1461 info.mActiveBufferStride = buffer->getStride();
1462 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001463 } else {
1464 info.mActiveBufferWidth = 0;
1465 info.mActiveBufferHeight = 0;
1466 info.mActiveBufferStride = 0;
1467 info.mActiveBufferFormat = 0;
1468 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001469 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001470 info.mNumQueuedFrames = getQueuedFrameCount();
1471 info.mRefreshPending = isBufferLatched();
1472 info.mIsOpaque = isOpaque(ds);
1473 info.mContentDirty = contentDirty;
1474 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001475}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001476
Yiwei Zhang5434a782018-12-05 18:06:32 -08001477void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001478 result.append("-------------------------------");
1479 result.append("-------------------------------");
1480 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001481 result.append(" Layer name\n");
1482 result.append(" Z | ");
1483 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001484 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001485 result.append(" Disp Frame (LTRB) | ");
1486 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001487 result.append("-------------------------------");
1488 result.append("-------------------------------");
1489 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001490}
1491
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001492void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1493 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1494 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001495 return;
1496 }
1497
Yiwei Zhang5434a782018-12-05 18:06:32 -08001498 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001499 if (mName.length() > 77) {
1500 std::string shortened;
1501 shortened.append(mName.string(), 36);
1502 shortened.append("[...]");
1503 shortened.append(mName.string() + (mName.length() - 36), 36);
Yiwei Zhang5434a782018-12-05 18:06:32 -08001504 name = shortened;
Dan Stozae22aec72016-08-01 13:20:59 -07001505 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001506 name = std::string(mName.string(), mName.size());
Dan Stozae22aec72016-08-01 13:20:59 -07001507 }
1508
Yiwei Zhang5434a782018-12-05 18:06:32 -08001509 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001510
Alec Mourib416efd2018-09-06 21:01:59 +00001511 const State& layerState(getDrawingState());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001512 const auto& compositionState = outputLayer->getState();
1513
Chia-I Wu1e043612018-03-01 09:45:09 -08001514 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001515 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001516 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001517 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001518 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001519 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
1520 StringAppendF(&result, "%10s | ",
1521 toString(getCompositionLayer() ? compositionState.bufferTransform
1522 : static_cast<Hwc2::Transform>(0))
1523 .c_str());
1524 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001525 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001526 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001527 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1528 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001529
Yichi Chen6ca35192018-05-29 12:20:43 +08001530 result.append("- - - - - - - - - - - - - - - -");
1531 result.append("- - - - - - - - - - - - - - - -");
1532 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001533}
Dan Stozae22aec72016-08-01 13:20:59 -07001534
Yiwei Zhang5434a782018-12-05 18:06:32 -08001535void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001536 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001537}
1538
Svetoslavd85084b2014-03-20 10:28:31 -07001539void Layer::clearFrameStats() {
1540 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001541}
1542
Jamie Gennis6547ff42013-07-16 20:12:42 -07001543void Layer::logFrameStats() {
1544 mFrameTracker.logAndResetStats(mName);
1545}
1546
Svetoslavd85084b2014-03-20 10:28:31 -07001547void Layer::getFrameStats(FrameStats* outStats) const {
1548 mFrameTracker.getStats(outStats);
1549}
1550
Yiwei Zhang5434a782018-12-05 18:06:32 -08001551void Layer::dumpFrameEvents(std::string& result) {
1552 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001553 Mutex::Autolock lock(mFrameEventHistoryMutex);
1554 mFrameEventHistory.checkFencesForCompletion();
1555 mFrameEventHistory.dump(result);
1556}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001557
Brian Anderson5ea5e592016-12-01 16:54:33 -08001558void Layer::onDisconnect() {
1559 Mutex::Autolock lock(mFrameEventHistoryMutex);
1560 mFrameEventHistory.onDisconnect();
Yiwei Zhangaf8ee942018-11-22 00:15:23 -08001561 mFlinger->mTimeStats->onDestroy(getSequence());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001562}
1563
Brian Anderson3890c392016-07-25 12:48:08 -07001564void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001565 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001566 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001567 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1568 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001569 }
1570
Brian Andersond6927fb2016-07-23 23:37:30 -07001571 Mutex::Autolock lock(mFrameEventHistoryMutex);
1572 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001573 // If there are any unsignaled fences in the aquire timeline at this
1574 // point, the previously queued frame hasn't been latched yet. Go ahead
1575 // and try to get the signal time here so the syscall is taken out of
1576 // the main thread's critical path.
1577 mAcquireTimeline.updateSignalTimes();
1578 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001579 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001580 mFrameEventHistory.addQueue(*newTimestamps);
1581 }
1582
Brian Anderson3890c392016-07-25 12:48:08 -07001583 if (outDelta) {
1584 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001585 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001586}
Dan Stozae77c7662016-05-13 11:37:28 -07001587
Chia-I Wu98f1c102017-05-30 14:54:08 -07001588size_t Layer::getChildrenCount() const {
1589 size_t count = 0;
1590 for (const sp<Layer>& child : mCurrentChildren) {
1591 count += 1 + child->getChildrenCount();
1592 }
1593 return count;
1594}
1595
Robert Carr1f0a16a2016-10-24 16:27:39 -07001596void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001597 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001598 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001599
Robert Carr1f0a16a2016-10-24 16:27:39 -07001600 mCurrentChildren.add(layer);
1601 layer->setParent(this);
1602}
1603
1604ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001605 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001606 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001607
Robert Carr1323c952019-01-28 18:13:27 -08001608 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001609 return mCurrentChildren.remove(layer);
1610}
1611
Robert Carr1db73f62016-12-21 12:58:51 -08001612bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1613 sp<Handle> handle = nullptr;
1614 sp<Layer> newParent = nullptr;
1615 if (newParentHandle == nullptr) {
1616 return false;
1617 }
1618 handle = static_cast<Handle*>(newParentHandle.get());
1619 newParent = handle->owner.promote();
1620 if (newParent == nullptr) {
1621 ALOGE("Unable to promote Layer handle");
1622 return false;
1623 }
1624
chaviw5aedec92018-10-22 10:40:38 -07001625 if (attachChildren()) {
1626 setTransactionFlags(eTransactionNeeded);
1627 }
Robert Carr1db73f62016-12-21 12:58:51 -08001628 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001629 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001630 }
1631 mCurrentChildren.clear();
1632
1633 return true;
1634}
1635
Robert Carr15eae092018-03-23 13:43:53 -07001636void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001637 for (const sp<Layer>& child : mDrawingChildren) {
1638 child->mDrawingParent = newParent;
Vishnu Nair4351ad52019-02-11 14:13:02 -08001639 child->computeBounds(newParent->mBounds, newParent->getTransformWithScale());
Robert Carr578038f2018-03-09 12:25:24 -08001640 }
1641}
1642
chaviwf1961f72017-09-18 16:41:07 -07001643bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001644 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001645
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001646 // While layers are detached, we allow most operations
1647 // and simply halt performing the actual transaction. However
1648 // for reparent != null we would enter the mRemovedFromCurrentState
1649 // state, regardless of whether doTransaction was called, and
1650 // so we need to prevent the update here.
1651 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001652 return false;
1653 }
1654
Robert Carr54cf5b12019-01-25 14:02:28 -08001655 sp<Layer> newParent;
1656 if (newParentHandle != nullptr) {
1657 auto handle = static_cast<Handle*>(newParentHandle.get());
1658 newParent = handle->owner.promote();
1659 if (newParent == nullptr) {
1660 ALOGE("Unable to promote Layer handle");
1661 return false;
1662 }
1663 if (newParent == this) {
1664 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1665 return false;
1666 }
1667 }
1668
chaviwf1961f72017-09-18 16:41:07 -07001669 sp<Layer> parent = getParent();
1670 if (parent != nullptr) {
1671 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001672 }
1673
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001674 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001675 newParent->addChild(this);
1676 if (!newParent->isRemovedFromCurrentState()) {
1677 addToCurrentState();
1678 } else {
1679 onRemovedFromCurrentState();
1680 }
1681
1682 if (mLayerDetached) {
1683 mLayerDetached = false;
1684 callSetTransactionFlags = true;
1685 }
1686 } else {
1687 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001688 }
1689
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001690 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001691 setTransactionFlags(eTransactionNeeded);
1692 }
chaviw06178942017-07-27 10:25:59 -07001693 return true;
1694}
1695
Robert Carr9524cb32017-02-13 11:32:32 -08001696bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001697 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001698 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001699 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001700 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001701 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001702 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001703 }
Robert Carr7f619b22017-11-06 12:56:35 -08001704 }
Robert Carr9524cb32017-02-13 11:32:32 -08001705
1706 return true;
1707}
1708
chaviw5aedec92018-10-22 10:40:38 -07001709bool Layer::attachChildren() {
1710 bool changed = false;
1711 for (const sp<Layer>& child : mCurrentChildren) {
1712 sp<Client> parentClient = mClientRef.promote();
1713 sp<Client> client(child->mClientRef.promote());
1714 if (client != nullptr && parentClient != client) {
1715 if (child->mLayerDetached) {
1716 child->mLayerDetached = false;
1717 changed = true;
1718 }
1719 changed |= child->attachChildren();
1720 }
1721 }
1722
1723 return changed;
1724}
1725
Peiyong Lind3788632018-09-18 16:01:31 -07001726bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001727 static const mat4 identityMatrix = mat4();
1728
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001729 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001730 return false;
1731 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001732 ++mCurrentState.sequence;
1733 mCurrentState.colorTransform = matrix;
1734 mCurrentState.hasColorTransform = matrix != identityMatrix;
1735 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001736 setTransactionFlags(eTransactionNeeded);
1737 return true;
1738}
1739
chaviwf66724d2018-11-28 16:35:21 -08001740mat4 Layer::getColorTransform() const {
1741 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1742 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1743 colorTransform = parent->getColorTransform() * colorTransform;
1744 }
1745 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001746}
1747
1748bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001749 bool hasColorTransform = getDrawingState().hasColorTransform;
1750 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1751 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1752 }
1753 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001754}
1755
Chia-I Wu11481472018-05-04 10:43:19 -07001756bool Layer::isLegacyDataSpace() const {
1757 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001758 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001759 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001760}
1761
Robert Carr1f0a16a2016-10-24 16:27:39 -07001762void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001763 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001764}
1765
1766void Layer::clearSyncPoints() {
1767 for (const auto& child : mCurrentChildren) {
1768 child->clearSyncPoints();
1769 }
1770
1771 Mutex::Autolock lock(mLocalSyncPointMutex);
1772 for (auto& point : mLocalSyncPoints) {
1773 point->setFrameAvailable();
1774 }
1775 mLocalSyncPoints.clear();
1776}
1777
1778int32_t Layer::getZ() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001779 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001780}
1781
Robert Carr29abff82017-12-04 13:51:20 -08001782bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1783 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001784 const State& state = useDrawing ? mDrawingState : mCurrentState;
Robert Carr29abff82017-12-04 13:51:20 -08001785 return state.zOrderRelativeOf != nullptr;
1786}
1787
David Sodman41fdfc92017-11-06 16:09:56 -08001788__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001789 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001790 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1791 "makeTraversalList received invalid stateSet");
1792 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1793 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001794 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001795
Robert Carr29abff82017-12-04 13:51:20 -08001796 if (state.zOrderRelatives.size() == 0) {
1797 *outSkipRelativeZUsers = true;
1798 return children;
1799 }
1800
chaviwfd462612018-05-31 16:11:27 -07001801 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001802 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001803 sp<Layer> strongRelative = weakRelative.promote();
1804 if (strongRelative != nullptr) {
1805 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001806 }
1807 }
1808
Dan Stoza412903f2017-04-27 13:42:17 -07001809 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001810 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
Robert Carr503c7042017-09-27 15:06:08 -07001811 if (childState.zOrderRelativeOf != nullptr) {
1812 continue;
1813 }
Robert Carrdb66e622017-04-10 16:55:57 -07001814 traverse.add(child);
1815 }
1816
1817 return traverse;
1818}
1819
Robert Carr1f0a16a2016-10-24 16:27:39 -07001820/**
Robert Carrdb66e622017-04-10 16:55:57 -07001821 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001822 */
Dan Stoza412903f2017-04-27 13:42:17 -07001823void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001824 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1825 // produce a new list for traversing, including our relatives, and not including our children
1826 // who are relatives of another surface. In the case that there are no relative Z,
1827 // makeTraversalList returns our children directly to avoid significant overhead.
1828 // However in this case we need to take the responsibility for filtering children which
1829 // are relatives of another surface here.
1830 bool skipRelativeZUsers = false;
1831 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001832
Robert Carr1f0a16a2016-10-24 16:27:39 -07001833 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001834 for (; i < list.size(); i++) {
1835 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001836 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1837 continue;
1838 }
1839
Robert Carrdb66e622017-04-10 16:55:57 -07001840 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001841 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001842 }
Dan Stoza412903f2017-04-27 13:42:17 -07001843 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001844 }
Robert Carr29abff82017-12-04 13:51:20 -08001845
Dan Stoza412903f2017-04-27 13:42:17 -07001846 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001847 for (; i < list.size(); i++) {
1848 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001849
Robert Carr29abff82017-12-04 13:51:20 -08001850 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1851 continue;
1852 }
Dan Stoza412903f2017-04-27 13:42:17 -07001853 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001854 }
1855}
1856
1857/**
Robert Carrdb66e622017-04-10 16:55:57 -07001858 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001859 */
Dan Stoza412903f2017-04-27 13:42:17 -07001860void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1861 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001862 // See traverseInZOrder for documentation.
1863 bool skipRelativeZUsers = false;
1864 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001865
Robert Carr1f0a16a2016-10-24 16:27:39 -07001866 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001867 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001868 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001869
1870 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1871 continue;
1872 }
1873
Robert Carrdb66e622017-04-10 16:55:57 -07001874 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001875 break;
1876 }
Dan Stoza412903f2017-04-27 13:42:17 -07001877 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001878 }
Dan Stoza412903f2017-04-27 13:42:17 -07001879 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001880 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001881 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001882
1883 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1884 continue;
1885 }
1886
Dan Stoza412903f2017-04-27 13:42:17 -07001887 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001888 }
1889}
1890
chaviw4b129c22018-04-09 16:19:43 -07001891LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1892 const std::vector<Layer*>& layersInTree) {
1893 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1894 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001895 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1896 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001897 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001898
chaviwfd462612018-05-31 16:11:27 -07001899 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001900 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1901 sp<Layer> strongRelative = weakRelative.promote();
1902 // Only add relative layers that are also descendents of the top most parent of the tree.
1903 // If a relative layer is not a descendent, then it should be ignored.
1904 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1905 traverse.add(strongRelative);
1906 }
1907 }
1908
1909 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001910 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001911 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1912 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1913 // the child here since it won't be added later as a relative.
1914 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1915 childState.zOrderRelativeOf.promote().get())) {
1916 continue;
1917 }
1918 traverse.add(child);
1919 }
1920
1921 return traverse;
1922}
1923
1924void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1925 LayerVector::StateSet stateSet,
1926 const LayerVector::Visitor& visitor) {
1927 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001928
1929 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001930 for (; i < list.size(); i++) {
1931 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001932 if (relative->getZ() >= 0) {
1933 break;
1934 }
chaviw4b129c22018-04-09 16:19:43 -07001935 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001936 }
chaviw4b129c22018-04-09 16:19:43 -07001937
chaviwa76b2712017-09-20 12:02:26 -07001938 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001939 for (; i < list.size(); i++) {
1940 const auto& relative = list[i];
1941 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001942 }
1943}
1944
chaviw4b129c22018-04-09 16:19:43 -07001945std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1946 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1947 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1948
1949 std::vector<Layer*> layersInTree = {this};
1950 for (size_t i = 0; i < children.size(); i++) {
1951 const auto& child = children[i];
1952 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1953 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1954 }
1955
1956 return layersInTree;
1957}
1958
1959void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1960 const LayerVector::Visitor& visitor) {
1961 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1962 std::sort(layersInTree.begin(), layersInTree.end());
1963 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1964}
1965
Peiyong Linefefaac2018-08-17 12:27:51 -07001966ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001967 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001968}
1969
chaviw13fdc492017-06-27 12:40:18 -07001970half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001971 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001972
chaviw13fdc492017-06-27 12:40:18 -07001973 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1974 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001975}
Robert Carr6452f122017-03-21 10:41:29 -07001976
chaviw13fdc492017-06-27 12:40:18 -07001977half4 Layer::getColor() const {
1978 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001979 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001980}
Robert Carr6452f122017-03-21 10:41:29 -07001981
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001982Layer::RoundedCornerState Layer::getRoundedCornerState() const {
1983 const auto& p = mDrawingParent.promote();
1984 if (p != nullptr) {
1985 RoundedCornerState parentState = p->getRoundedCornerState();
1986 if (parentState.radius > 0) {
1987 ui::Transform t = getActiveTransform(getDrawingState());
1988 t = t.inverse();
1989 parentState.cropRect = t.transform(parentState.cropRect);
1990 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1991 // but a transform matrix can define horizontal and vertical scales.
1992 // Let's take the average between both of them and pass into the shader, practically we
1993 // never do this type of transformation on windows anyway.
1994 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
1995 return parentState;
1996 }
1997 }
1998 const float radius = getDrawingState().cornerRadius;
Vishnu Nair4351ad52019-02-11 14:13:02 -08001999 return radius > 0 ? RoundedCornerState(getBounds(), radius) : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002000}
2001
Robert Carr1f0a16a2016-10-24 16:27:39 -07002002void Layer::commitChildList() {
2003 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
2004 const auto& child = mCurrentChildren[i];
2005 child->commitChildList();
2006 }
2007 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07002008 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002009}
2010
Robert Carr720e5062018-07-30 17:45:14 -07002011void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002012 mCurrentState.inputInfo = info;
2013 mCurrentState.modified = true;
2014 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07002015 setTransactionFlags(eTransactionNeeded);
2016}
2017
chaviw1d044282017-09-27 12:19:28 -07002018void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
2019 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2020 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002021 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07002022
Peiyong Linefefaac2018-08-17 12:27:51 -07002023 ui::Transform requestedTransform = state.active_legacy.transform;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002024 ui::Transform transform = getTransform();
chaviw1d044282017-09-27 12:19:28 -07002025
2026 layerInfo->set_id(sequence);
2027 layerInfo->set_name(getName().c_str());
2028 layerInfo->set_type(String8(getTypeId()));
2029
2030 for (const auto& child : children) {
2031 layerInfo->add_children(child->sequence);
2032 }
2033
2034 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
2035 sp<Layer> strongRelative = weakRelative.promote();
2036 if (strongRelative != nullptr) {
2037 layerInfo->add_relatives(strongRelative->sequence);
2038 }
2039 }
2040
Marissa Wallf58c14b2018-07-24 10:50:43 -07002041 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
chaviw1d044282017-09-27 12:19:28 -07002042 layerInfo->mutable_transparent_region());
2043 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
2044 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
2045
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002046 layerInfo->set_layer_stack(getLayerStack());
chaviw1d044282017-09-27 12:19:28 -07002047 layerInfo->set_z(state.z);
2048
2049 PositionProto* position = layerInfo->mutable_position();
2050 position->set_x(transform.tx());
2051 position->set_y(transform.ty());
2052
2053 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
2054 requestedPosition->set_x(requestedTransform.tx());
2055 requestedPosition->set_y(requestedTransform.ty());
2056
2057 SizeProto* size = layerInfo->mutable_size();
Marissa Wallf58c14b2018-07-24 10:50:43 -07002058 size->set_w(state.active_legacy.w);
2059 size->set_h(state.active_legacy.h);
chaviw1d044282017-09-27 12:19:28 -07002060
Marissa Wallf58c14b2018-07-24 10:50:43 -07002061 LayerProtoHelper::writeToProto(state.crop_legacy, layerInfo->mutable_crop());
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002062 layerInfo->set_corner_radius(getRoundedCornerState().radius);
chaviw1d044282017-09-27 12:19:28 -07002063
2064 layerInfo->set_is_opaque(isOpaque(state));
2065 layerInfo->set_invalidate(contentDirty);
Chia-I Wu01591c92018-05-22 12:03:00 -07002066
2067 // XXX (b/79210409) mCurrentDataSpace is not protected
2068 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
2069
chaviw1d044282017-09-27 12:19:28 -07002070 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
2071 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
2072 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
2073 layerInfo->set_flags(state.flags);
2074
2075 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
2076 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
2077
Jorim Jaggi8e0af362017-11-14 16:28:28 +01002078 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07002079 if (parent != nullptr) {
2080 layerInfo->set_parent(parent->sequence);
Nataniel Borgesdcc0bab2019-02-15 13:22:03 -08002081 } else {
2082 layerInfo->set_parent(-1);
chaviw1d044282017-09-27 12:19:28 -07002083 }
2084
2085 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2086 if (zOrderRelativeOf != nullptr) {
2087 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
Nataniel Borgesdcc0bab2019-02-15 13:22:03 -08002088 } else {
2089 layerInfo->set_z_order_relative_of(-1);
chaviw1d044282017-09-27 12:19:28 -07002090 }
2091
Lloyd Pique0b785d82018-12-04 17:25:27 -08002092 auto buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08002093 if (buffer != nullptr) {
2094 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
Peiyong Linefefaac2018-08-17 12:27:51 -07002095 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
Yichi Chen6ca35192018-05-29 12:20:43 +08002096 layerInfo->mutable_buffer_transform());
chaviw1d044282017-09-27 12:19:28 -07002097 }
2098
2099 layerInfo->set_queued_frames(getQueuedFrameCount());
2100 layerInfo->set_refresh_pending(isBufferLatched());
chaviwadc40c22018-07-10 16:57:27 -07002101 layerInfo->set_curr_frame(mCurrentFrameNumber);
Vishnu Nairf2deb822018-11-12 17:53:48 -08002102 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
chaviwadc40c22018-07-10 16:57:27 -07002103
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002104 for (const auto& pendingState : mPendingStates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07002105 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
chaviwadc40c22018-07-10 16:57:27 -07002106 if (barrierLayer != nullptr) {
2107 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
2108 barrierLayerProto->set_id(barrierLayer->sequence);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002109 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
chaviwadc40c22018-07-10 16:57:27 -07002110 }
2111 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002112
2113 auto protoMap = layerInfo->mutable_metadata();
2114 for (const auto& entry : state.metadata.mMap) {
2115 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
2116 }
Vishnu Nair4351ad52019-02-11 14:13:02 -08002117 LayerProtoHelper::writeToProto(mEffectiveTransform, layerInfo->mutable_effective_transform());
2118 LayerProtoHelper::writeToProto(mSourceBounds, layerInfo->mutable_source_bounds());
2119 LayerProtoHelper::writeToProto(mScreenBounds, layerInfo->mutable_screen_bounds());
2120 LayerProtoHelper::writeToProto(mBounds, layerInfo->mutable_bounds());
chaviw1d044282017-09-27 12:19:28 -07002121}
2122
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002123void Layer::writeToProto(LayerProto* layerInfo, const sp<DisplayDevice>& displayDevice) {
2124 auto outputLayer = findOutputLayerForDisplay(displayDevice);
2125 if (!outputLayer) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07002126 return;
2127 }
2128
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002129 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
2130
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002131 const auto& compositionState = outputLayer->getState();
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002132
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002133 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002134 LayerProtoHelper::writeToProto(frame, layerInfo->mutable_hwc_frame());
2135
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002136 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002137 LayerProtoHelper::writeToProto(crop, layerInfo->mutable_hwc_crop());
2138
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002139 const int32_t transform =
2140 getCompositionLayer() ? static_cast<int32_t>(compositionState.bufferTransform) : 0;
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002141 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002142
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002143 const int32_t compositionType =
2144 static_cast<int32_t>(compositionState.hwc ? (*compositionState.hwc).hwcCompositionType
2145 : Hwc2::IComposerClient::Composition::CLIENT);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002146 layerInfo->set_hwc_composition_type(compositionType);
2147
2148 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
2149 static_cast<BufferLayer*>(this)->isProtected()) {
2150 layerInfo->set_is_protected(true);
2151 } else {
2152 layerInfo->set_is_protected(false);
2153 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002154}
2155
Robert Carr2e102c92018-10-23 12:11:15 -07002156bool Layer::isRemovedFromCurrentState() const {
2157 return mRemovedFromCurrentState;
2158}
2159
Arthur Hungd20b2702019-01-14 18:16:16 +08002160InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002161 InputWindowInfo info = mDrawingState.inputInfo;
Robert Carr720e5062018-07-30 17:45:14 -07002162
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002163 if (info.displayId == ADISPLAY_ID_NONE) {
2164 info.displayId = mDrawingState.layerStack;
2165 }
2166
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002167 ui::Transform t = getTransform();
2168 const float xScale = t.sx();
2169 const float yScale = t.sy();
2170 if (xScale != 1.0f || yScale != 1.0f) {
2171 info.windowXScale *= 1.0f / xScale;
2172 info.windowYScale *= 1.0f / yScale;
2173 info.touchableRegion.scaleSelf(xScale, yScale);
Riddle Hsu39d4aa52018-11-30 20:46:53 +08002174 }
Robert Carre07e1032018-11-26 12:55:53 -08002175
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002176 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002177 // If this is a portal window, set the touchableRegion to the layerBounds.
2178 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
2179 ? getBufferSize(getDrawingState())
2180 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002181 if (!layerBounds.isValid()) {
2182 layerBounds = getCroppedBufferSize(getDrawingState());
2183 }
Vishnu Nair8033a492018-12-05 07:27:23 -08002184 layerBounds = t.transform(layerBounds);
2185 layerBounds.inset(info.surfaceInset, info.surfaceInset, info.surfaceInset, info.surfaceInset);
2186
Arthur Hungd20b2702019-01-14 18:16:16 +08002187 // Input coordinate should match the layer bounds.
2188 info.frameLeft = layerBounds.left;
2189 info.frameTop = layerBounds.top;
2190 info.frameRight = layerBounds.right;
2191 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08002192
2193 // Position the touchable region relative to frame screen location and restrict it to frame
2194 // bounds.
2195 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08002196 info.visible = canReceiveInput();
Robert Carr720e5062018-07-30 17:45:14 -07002197 return info;
2198}
2199
2200bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002201 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002202}
2203
Lloyd Piquefeb73d72018-12-04 17:23:44 -08002204std::shared_ptr<compositionengine::Layer> Layer::getCompositionLayer() const {
2205 return nullptr;
2206}
2207
chaviw3e727cd2019-01-31 13:41:05 -08002208bool Layer::canReceiveInput() const {
2209 return isVisible();
2210}
2211
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002212compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
2213 const sp<const DisplayDevice>& display) const {
2214 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionLayer().get());
2215}
2216
Mathias Agopian13127d82013-03-05 17:47:11 -08002217// ---------------------------------------------------------------------------
2218
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002219}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002220
2221#if defined(__gl_h_)
2222#error "don't include gl/gl.h in this file"
2223#endif
2224
2225#if defined(__gl2_h_)
2226#error "don't include gl2/gl2.h in this file"
2227#endif