blob: a6adeb3e2fbec1c9c573a1a32cfa769216456faf [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,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800683 Region& clearRegion, const bool supportProtectedContent,
684 renderengine::LayerSettings& layer) {
685 return prepareClientLayer(renderArea, clip, false, clearRegion, supportProtectedContent, layer);
Mathias Agopian13127d82013-03-05 17:47:11 -0800686}
687
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000688bool Layer::prepareClientLayer(const RenderArea& renderArea, bool useIdentityTransform,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800689 Region& clearRegion, const bool supportProtectedContent,
690 renderengine::LayerSettings& layer) {
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000691 return prepareClientLayer(renderArea, Region(renderArea.getBounds()), useIdentityTransform,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800692 clearRegion, supportProtectedContent, layer);
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000693}
694
695bool Layer::prepareClientLayer(const RenderArea& /*renderArea*/, const Region& /*clip*/,
696 bool useIdentityTransform, Region& /*clearRegion*/,
Peiyong Lin8f28a1d2019-02-07 17:25:12 -0800697 const bool /*supportProtectedContent*/,
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000698 renderengine::LayerSettings& layer) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800699 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000700 half alpha = getAlpha();
701 layer.geometry.boundaries = bounds;
702 if (useIdentityTransform) {
703 layer.geometry.positionTransform = mat4();
704 } else {
705 const ui::Transform transform = getTransform();
706 mat4 m;
707 m[0][0] = transform[0][0];
708 m[0][1] = transform[0][1];
709 m[0][3] = transform[0][2];
710 m[1][0] = transform[1][0];
711 m[1][1] = transform[1][1];
712 m[1][3] = transform[1][2];
713 m[3][0] = transform[2][0];
714 m[3][1] = transform[2][1];
715 m[3][3] = transform[2][2];
716 layer.geometry.positionTransform = m;
717 }
718
719 if (hasColorTransform()) {
720 layer.colorTransform = getColorTransform();
721 }
722
723 const auto roundedCornerState = getRoundedCornerState();
724 layer.geometry.roundedCornersRadius = roundedCornerState.radius;
725 layer.geometry.roundedCornersCrop = roundedCornerState.cropRect;
726
727 layer.alpha = alpha;
728 layer.sourceDataspace = mCurrentDataSpace;
729 return true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800730}
731
David Sodman41fdfc92017-11-06 16:09:56 -0800732void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
733 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800734 auto& engine(mFlinger->getRenderEngine());
Lloyd Pique0b785d82018-12-04 17:25:27 -0800735 computeGeometry(renderArea, getCompositionLayer()->editState().reMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700736 engine.setupFillWithColor(red, green, blue, alpha);
Lloyd Pique0b785d82018-12-04 17:25:27 -0800737 engine.drawMesh(getCompositionLayer()->getState().reMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800738}
739
David Sodmanc1498e62018-09-12 14:36:26 -0700740void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
741 clearWithOpenGL(renderArea, 0, 0, 0, 0);
742}
743
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800744void Layer::setCompositionType(const sp<const DisplayDevice>& display,
745 Hwc2::IComposerClient::Composition type) {
746 const auto outputLayer = findOutputLayerForDisplay(display);
747 LOG_FATAL_IF(!outputLayer);
748 LOG_FATAL_IF(!outputLayer->getState().hwc);
749 auto& compositionState = outputLayer->editState();
750
751 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", ((*compositionState.hwc).hwcLayer)->getId(),
752 toString(type).c_str(), 1);
753 if ((*compositionState.hwc).hwcCompositionType != type) {
David Sodman15094112018-10-11 09:39:37 -0700754 ALOGV(" actually setting");
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800755 (*compositionState.hwc).hwcCompositionType = type;
756
757 auto error = (*compositionState.hwc)
758 .hwcLayer->setCompositionType(static_cast<HWC2::Composition>(type));
759 ALOGE_IF(error != HWC2::Error::None,
760 "[%s] Failed to set "
761 "composition type %s: %s (%d)",
762 mName.string(), toString(type).c_str(), to_string(error).c_str(),
763 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800764 }
765}
766
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800767Hwc2::IComposerClient::Composition Layer::getCompositionType(
768 const sp<const DisplayDevice>& display) const {
769 const auto outputLayer = findOutputLayerForDisplay(display);
770 LOG_FATAL_IF(!outputLayer);
771 return outputLayer->getState().hwc ? (*outputLayer->getState().hwc).hwcCompositionType
772 : Hwc2::IComposerClient::Composition::CLIENT;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800773}
774
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800775bool Layer::getClearClientTarget(const sp<const DisplayDevice>& display) const {
776 const auto outputLayer = findOutputLayerForDisplay(display);
777 LOG_FATAL_IF(!outputLayer);
778 return outputLayer->getState().clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800779}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800780
Dan Stozacac35382016-01-27 12:21:06 -0800781bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
782 if (point->getFrameNumber() <= mCurrentFrameNumber) {
783 // Don't bother with a SyncPoint, since we've already latched the
784 // relevant frame
785 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700786 }
Robert Carr2e102c92018-10-23 12:11:15 -0700787 if (isRemovedFromCurrentState()) {
788 return false;
789 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700790
Dan Stozacac35382016-01-27 12:21:06 -0800791 Mutex::Autolock lock(mLocalSyncPointMutex);
792 mLocalSyncPoints.push_back(point);
793 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700794}
795
Mathias Agopian13127d82013-03-05 17:47:11 -0800796// ----------------------------------------------------------------------------
797// local state
798// ----------------------------------------------------------------------------
799
Peiyong Lin833074a2018-08-28 11:53:54 -0700800void Layer::computeGeometry(const RenderArea& renderArea,
801 renderengine::Mesh& mesh,
chaviwa76b2712017-09-20 12:02:26 -0700802 bool useIdentityTransform) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700803 const ui::Transform renderAreaTransform(renderArea.getTransform());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800804 FloatRect win = getBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700805
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000806 vec2 lt = vec2(win.left, win.top);
807 vec2 lb = vec2(win.left, win.bottom);
808 vec2 rb = vec2(win.right, win.bottom);
809 vec2 rt = vec2(win.right, win.top);
810
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800811 ui::Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000812 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700813 lt = layerTransform.transform(lt);
814 lb = layerTransform.transform(lb);
815 rb = layerTransform.transform(rb);
816 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000817 }
818
Peiyong Lin833074a2018-08-28 11:53:54 -0700819 renderengine::Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700820 position[0] = renderAreaTransform.transform(lt);
821 position[1] = renderAreaTransform.transform(lb);
822 position[2] = renderAreaTransform.transform(rb);
823 position[3] = renderAreaTransform.transform(rt);
Mathias Agopian13127d82013-03-05 17:47:11 -0800824}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800825
David Sodman41fdfc92017-11-06 16:09:56 -0800826bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800827 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700828 return (s.flags & layer_state_t::eLayerSecure);
829}
830
Mathias Agopian13127d82013-03-05 17:47:11 -0800831void Layer::setVisibleRegion(const Region& visibleRegion) {
832 // always called from main thread
833 this->visibleRegion = visibleRegion;
834}
835
836void Layer::setCoveredRegion(const Region& coveredRegion) {
837 // always called from main thread
838 this->coveredRegion = coveredRegion;
839}
840
David Sodman41fdfc92017-11-06 16:09:56 -0800841void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800842 // always called from main thread
843 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
844}
845
Robert Carre5f4f692018-01-12 13:12:28 -0800846void Layer::clearVisibilityRegions() {
847 visibleRegion.clear();
848 visibleNonTransparentRegion.clear();
849 coveredRegion.clear();
850}
851
Mathias Agopian13127d82013-03-05 17:47:11 -0800852// ----------------------------------------------------------------------------
853// transaction
854// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800855
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800856void Layer::pushPendingState() {
857 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700858 return;
859 }
860
Dan Stoza7dde5992015-05-22 09:51:44 -0700861 // If this transaction is waiting on the receipt of a frame, generate a sync
862 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700863 // We don't allow installing sync points after we are removed from the current state
864 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800865 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
866 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800867 if (barrierLayer == nullptr) {
868 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700869 // If we can't promote the layer we are intended to wait on,
870 // then it is expired or otherwise invalid. Allow this transaction
871 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800872 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800873 } else {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800874 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy);
Robert Carr0d480722017-01-10 16:42:54 -0800875 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800876 mRemoteSyncPoints.push_back(std::move(syncPoint));
877 } else {
878 // We already missed the frame we're supposed to synchronize
879 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800880 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800881 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700882 }
883
Dan Stoza7dde5992015-05-22 09:51:44 -0700884 // Wake us up to check if the frame has been received
885 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700886 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700887 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800888 mPendingStates.push_back(mCurrentState);
889 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700890}
891
Pablo Ceballos05289c22016-04-14 15:49:55 -0700892void Layer::popPendingState(State* stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800893 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700894
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800895 mPendingStates.removeAt(0);
896 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700897}
898
Pablo Ceballos05289c22016-04-14 15:49:55 -0700899bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700900 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800901 while (!mPendingStates.empty()) {
902 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700903 if (mRemoteSyncPoints.empty()) {
904 // If we don't have a sync point for this, apply it anyway. It
905 // will be visually wrong, but it should keep us from getting
906 // into too much trouble.
907 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700908 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700909 stateUpdateAvailable = true;
910 continue;
911 }
912
Marissa Wallf58c14b2018-07-24 10:50:43 -0700913 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800914 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800915 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800916
917 // Signal our end of the sync point and then dispose of it
918 mRemoteSyncPoints.front()->setTransactionApplied();
919 mRemoteSyncPoints.pop_front();
920 continue;
921 }
922
Dan Stoza7dde5992015-05-22 09:51:44 -0700923 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
924 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700925 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700926 stateUpdateAvailable = true;
927
928 // Signal our end of the sync point and then dispose of it
929 mRemoteSyncPoints.front()->setTransactionApplied();
930 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800931 } else {
932 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700933 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700934 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700935 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700936 stateUpdateAvailable = true;
937 }
938 }
939
940 // If we still have pending updates, wake SurfaceFlinger back up and point
941 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800942 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700943 setTransactionFlags(eTransactionNeeded);
944 mFlinger->setTransactionFlags(eTraversalNeeded);
945 }
946
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800947 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700948 return stateUpdateAvailable;
949}
950
Marissa Wall61c58622018-07-18 10:12:20 -0700951uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000952 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800953
Marissa Wall61c58622018-07-18 10:12:20 -0700954 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
955 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700956
David Sodmaneb085e02017-10-05 18:49:04 -0700957 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700958 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000959 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800960 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
961 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
962 " requested={ wh={%4u,%4u} }}\n"
963 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
964 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700965 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700966 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
967 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
968 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
969 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
970 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700971 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
972 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
973 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800974 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700975
Robert Carre392b552017-09-19 12:16:05 -0700976 // Don't let Layer::doTransaction update the drawing state
977 // if we have a pending resize, unless we are in fixed-size mode.
978 // the drawing state will be updated only once we receive a buffer
979 // with the correct size.
980 //
981 // In particular, we want to make sure the clip (which is part
982 // of the geometry state) is latched together with the size but is
983 // latched immediately when no resizing is involved.
984 //
985 // If a sideband stream is attached, however, we want to skip this
986 // optimization so that transactions aren't missed when a buffer
987 // never arrives
988 //
989 // In the case that we don't have a buffer we ignore other factors
990 // and avoid entering the resizePending state. At a high level the
991 // resizePending state is to avoid applying the state of the new buffer
992 // to the old buffer. However in the state where we don't have an old buffer
993 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700994 const bool resizePending =
995 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
996 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
Lloyd Pique0b785d82018-12-04 17:25:27 -0800997 (mActiveBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700998 if (!isFixedSize()) {
Lloyd Pique0b785d82018-12-04 17:25:27 -0800999 if (resizePending && mSidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001000 flags |= eDontUpdateGeometryState;
1001 }
1002 }
1003
Robert Carr7bf247e2017-05-18 14:02:49 -07001004 // Here we apply various requested geometry states, depending on our
1005 // latching configuration. See Layer.h for a detailed discussion of
1006 // how geometry latching is controlled.
1007 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +00001008 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001009
1010 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1011 // mode, which causes attributes which normally latch regardless of scaling mode,
1012 // to be delayed. We copy the requested state to the active state making sure
1013 // to respect these rules (again see Layer.h for a detailed discussion).
1014 //
1015 // There is an awkward asymmetry in the handling of the crop states in the position
1016 // states, as can be seen below. Largely this arises from position and transform
1017 // being stored in the same data structure while having different latching rules.
1018 // b/38182305
1019 //
Marissa Wall61c58622018-07-18 10:12:20 -07001020 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -07001021 // applyPendingStates in the presence of deferred transactions.
1022 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -07001023 float tx = stateToCommit->active_legacy.transform.tx();
1024 float ty = stateToCommit->active_legacy.transform.ty();
1025 stateToCommit->active_legacy = stateToCommit->requested_legacy;
1026 stateToCommit->active_legacy.transform.set(tx, ty);
1027 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001028 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001029 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -07001030 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001031 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001032 }
1033
Marissa Wall61c58622018-07-18 10:12:20 -07001034 return flags;
1035}
1036
1037uint32_t Layer::doTransaction(uint32_t flags) {
1038 ATRACE_CALL();
1039
chaviw5aedec92018-10-22 10:40:38 -07001040 if (mLayerDetached) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001041 return flags;
1042 }
1043
1044 if (mChildrenChanged) {
1045 flags |= eVisibleRegion;
1046 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -07001047 }
1048
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001049 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +00001050 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -07001051 if (!applyPendingStates(&c)) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001052 return flags;
Marissa Wall61c58622018-07-18 10:12:20 -07001053 }
1054
1055 flags = doTransactionResize(flags, &c);
1056
Alec Mourib416efd2018-09-06 21:01:59 +00001057 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -07001058
1059 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001060 // invalidate and recompute the visible regions if needed
1061 flags |= Layer::eVisibleRegion;
1062 }
1063
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001064 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001065 // invalidate and recompute the visible regions if needed
1066 flags |= eVisibleRegion;
1067 this->contentDirty = true;
1068
1069 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -07001070 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -07001071 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -08001072 }
1073
Dan Stozac8145172016-04-28 16:29:06 -07001074 // If the layer is hidden, signal and clear out all local sync points so
1075 // that transactions for layers depending on this layer's frames becoming
1076 // visible are not blocked
1077 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001078 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001079 }
1080
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001081 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -07001082 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001083 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -07001084 }
1085
Mathias Agopian13127d82013-03-05 17:47:11 -08001086 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001087 commitTransaction(c);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001088 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -08001089 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001090}
1091
Pablo Ceballos05289c22016-04-14 15:49:55 -07001092void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001093 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001094}
1095
Mathias Agopian13127d82013-03-05 17:47:11 -08001096uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001097 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -08001098}
1099
1100uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001101 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -08001102}
1103
Robert Carr82364e32016-05-15 11:27:47 -07001104bool Layer::setPosition(float x, float y, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001105 if (mCurrentState.requested_legacy.transform.tx() == x &&
1106 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001107 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001108 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001109
1110 // We update the requested and active position simultaneously because
1111 // we want to apply the position portion of the transform matrix immediately,
1112 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001113 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001114 if (immediate && !mFreezeGeometryUpdates) {
1115 // Here we directly update the active state
1116 // unlike other setters, because we store it within
1117 // the transform, but use different latching rules.
1118 // b/38182305
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001119 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001120 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001121 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001122
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001123 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001124 setTransactionFlags(eTransactionNeeded);
1125 return true;
1126}
Robert Carr82364e32016-05-15 11:27:47 -07001127
Robert Carr1f0a16a2016-10-24 16:27:39 -07001128bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1129 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1130 if (idx < 0) {
1131 return false;
1132 }
1133 if (childLayer->setLayer(z)) {
1134 mCurrentChildren.removeAt(idx);
1135 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001136 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001137 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001138 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001139}
1140
Robert Carr503c7042017-09-27 15:06:08 -07001141bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1142 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1143 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1144 if (idx < 0) {
1145 return false;
1146 }
1147 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1148 mCurrentChildren.removeAt(idx);
1149 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001150 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001151 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001152 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001153}
1154
Robert Carrae060832016-11-28 10:51:00 -08001155bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001156 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
1157 mCurrentState.sequence++;
1158 mCurrentState.z = z;
1159 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001160
1161 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001162 if (mCurrentState.zOrderRelativeOf != nullptr) {
1163 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -07001164 if (strongRelative != nullptr) {
1165 strongRelative->removeZOrderRelative(this);
1166 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001167 mCurrentState.zOrderRelativeOf = nullptr;
Robert Carrdb66e622017-04-10 16:55:57 -07001168 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001169 setTransactionFlags(eTransactionNeeded);
1170 return true;
1171}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001172
Robert Carrdb66e622017-04-10 16:55:57 -07001173void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001174 mCurrentState.zOrderRelatives.remove(relative);
1175 mCurrentState.sequence++;
1176 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001177 setTransactionFlags(eTransactionNeeded);
1178}
1179
1180void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001181 mCurrentState.zOrderRelatives.add(relative);
1182 mCurrentState.modified = true;
1183 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -07001184 setTransactionFlags(eTransactionNeeded);
1185}
1186
Robert Carr503d2bd2017-12-04 15:49:47 -08001187bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001188 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1189 if (handle == nullptr) {
1190 return false;
1191 }
1192 sp<Layer> relative = handle->owner.promote();
1193 if (relative == nullptr) {
1194 return false;
1195 }
1196
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001197 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1198 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001199 return false;
1200 }
1201
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001202 mCurrentState.sequence++;
1203 mCurrentState.modified = true;
1204 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001205
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001206 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -07001207 if (oldZOrderRelativeOf != nullptr) {
1208 oldZOrderRelativeOf->removeZOrderRelative(this);
1209 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001210 mCurrentState.zOrderRelativeOf = relative;
Robert Carrdb66e622017-04-10 16:55:57 -07001211 relative->addZOrderRelative(this);
1212
1213 setTransactionFlags(eTransactionNeeded);
1214
1215 return true;
1216}
1217
Mathias Agopian13127d82013-03-05 17:47:11 -08001218bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001219 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001220 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001221 mCurrentState.requested_legacy.w = w;
1222 mCurrentState.requested_legacy.h = h;
1223 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001224 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001225
1226 // record the new size, from this point on, when the client request
1227 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001228 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001229 return true;
1230}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001231bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001232 if (mCurrentState.color.a == alpha) return false;
1233 mCurrentState.sequence++;
1234 mCurrentState.color.a = alpha;
1235 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001236 setTransactionFlags(eTransactionNeeded);
1237 return true;
1238}
chaviw13fdc492017-06-27 12:40:18 -07001239
Valerie Haudd0b7572019-01-29 14:59:27 -08001240bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1241 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001242 return false;
Valerie Hauaa194562019-02-05 16:21:38 -08001243 }
1244 mCurrentState.sequence++;
1245 mCurrentState.modified = true;
1246 setTransactionFlags(eTransactionNeeded);
1247
1248 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001249 // create background color layer if one does not yet exist
1250 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
1251 const String8& name = mName + "BackgroundColorLayer";
1252 mCurrentState.bgColorLayer =
1253 new ColorLayer(LayerCreationArgs(mFlinger.get(), nullptr, name, 0, 0, flags));
chaviw13fdc492017-06-27 12:40:18 -07001254
Valerie Haudd0b7572019-01-29 14:59:27 -08001255 // add to child list
1256 addChild(mCurrentState.bgColorLayer);
1257 mFlinger->mLayersAdded = true;
1258 // set up SF to handle added color layer
1259 if (isRemovedFromCurrentState()) {
1260 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1261 }
1262 mFlinger->setTransactionFlags(eTransactionNeeded);
1263 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1264 mCurrentState.bgColorLayer->reparent(nullptr);
1265 mCurrentState.bgColorLayer = nullptr;
1266 return true;
1267 }
1268
1269 mCurrentState.bgColorLayer->setColor(color);
1270 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1271 mCurrentState.bgColorLayer->setAlpha(alpha);
1272 mCurrentState.bgColorLayer->setDataspace(dataspace);
1273
chaviw13fdc492017-06-27 12:40:18 -07001274 return true;
1275}
1276
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001277bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001278 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001279
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001280 mCurrentState.sequence++;
1281 mCurrentState.cornerRadius = cornerRadius;
1282 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001283 setTransactionFlags(eTransactionNeeded);
1284 return true;
1285}
1286
Robert Carrd4ae7f32018-06-07 16:10:57 -07001287bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1288 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001289 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001290 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1291
1292 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1293 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1294 return false;
1295 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001296 mCurrentState.sequence++;
1297 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1298 matrix.dsdy);
1299 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001300 setTransactionFlags(eTransactionNeeded);
1301 return true;
1302}
Marissa Wall61c58622018-07-18 10:12:20 -07001303
Mathias Agopian13127d82013-03-05 17:47:11 -08001304bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001305 mCurrentState.requestedTransparentRegion_legacy = transparent;
1306 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001307 setTransactionFlags(eTransactionNeeded);
1308 return true;
1309}
1310bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001311 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1312 if (mCurrentState.flags == newFlags) return false;
1313 mCurrentState.sequence++;
1314 mCurrentState.flags = newFlags;
1315 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001316 setTransactionFlags(eTransactionNeeded);
1317 return true;
1318}
Robert Carr99e27f02016-06-16 15:18:02 -07001319
Marissa Wallf58c14b2018-07-24 10:50:43 -07001320bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001321 if (mCurrentState.requestedCrop_legacy == crop) return false;
1322 mCurrentState.sequence++;
1323 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001324 if (immediate && !mFreezeGeometryUpdates) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001325 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001326 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001327 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1328
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001329 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001330 setTransactionFlags(eTransactionNeeded);
1331 return true;
1332}
Robert Carr8d5227b2017-03-16 15:41:03 -07001333
Robert Carrc3574f72016-03-24 12:19:32 -07001334bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001335 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001336 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001337 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001338 return true;
1339}
1340
Evan Roskyef876c92019-01-25 17:46:06 -08001341bool Layer::setMetadata(const LayerMetadata& data) {
1342 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001343 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001344 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001345 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001346 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001347}
1348
Mathias Agopian13127d82013-03-05 17:47:11 -08001349bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001350 if (mCurrentState.layerStack == layerStack) return false;
1351 mCurrentState.sequence++;
1352 mCurrentState.layerStack = layerStack;
1353 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001354 setTransactionFlags(eTransactionNeeded);
1355 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001356}
1357
Robert Carr1f0a16a2016-10-24 16:27:39 -07001358uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001359 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001360 if (p == nullptr) {
1361 return getDrawingState().layerStack;
1362 }
1363 return p->getLayerStack();
1364}
1365
Marissa Wallf58c14b2018-07-24 10:50:43 -07001366void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001367 mCurrentState.barrierLayer_legacy = barrierLayer;
1368 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001369 // We don't set eTransactionNeeded, because just receiving a deferral
1370 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001371 mCurrentState.modified = true;
1372 pushPendingState();
1373 mCurrentState.barrierLayer_legacy = nullptr;
1374 mCurrentState.frameNumber_legacy = 0;
1375 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001376}
1377
Marissa Wallf58c14b2018-07-24 10:50:43 -07001378void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001379 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001380 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001381}
1382
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001383// ----------------------------------------------------------------------------
1384// pageflip handling...
1385// ----------------------------------------------------------------------------
1386
Robert Carr1f0a16a2016-10-24 16:27:39 -07001387bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001388 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001389 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001390 if (parent != nullptr && parent->isHiddenByPolicy()) {
1391 return true;
1392 }
1393 return s.flags & layer_state_t::eLayerHidden;
1394}
1395
David Sodman41fdfc92017-11-06 16:09:56 -08001396uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001397 // TODO: should we do something special if mSecure is set?
1398 if (mProtectedByApp) {
1399 // need a hardware-protected path to external video sink
1400 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001401 }
Riley Andrews03414a12014-07-01 14:22:59 -07001402 if (mPotentialCursor) {
1403 usage |= GraphicBuffer::USAGE_CURSOR;
1404 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001405 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001406 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001407}
1408
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001409void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001410 uint32_t orientation = 0;
Vishnu Nairf2deb822018-11-12 17:53:48 -08001411 // Disable setting transform hint if the debug flag is set or if the
1412 // getTransformToDisplayInverse flag is set and the client wants to submit buffers
1413 // in one orientation.
1414 if (!mFlinger->mDebugDisableTransformHint && !getTransformToDisplayInverse()) {
Mathias Agopian84300952012-11-21 16:02:13 -08001415 // The transform hint is used to improve performance, but we can
1416 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001417 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001418 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001419 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001420 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001421 orientation = 0;
1422 }
1423 }
David Sodmaneb085e02017-10-05 18:49:04 -07001424 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001425}
1426
Mathias Agopian13127d82013-03-05 17:47:11 -08001427// ----------------------------------------------------------------------------
1428// debugging
1429// ----------------------------------------------------------------------------
1430
Marissa Wall61c58622018-07-18 10:12:20 -07001431// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001432LayerDebugInfo Layer::getLayerDebugInfo() const {
1433 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001434 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001435 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001436 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001437 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
Yiwei Zhang5434a782018-12-05 18:06:32 -08001438 info.mType = std::string(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001439 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001440 info.mVisibleRegion = visibleRegion;
1441 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001442 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001443 info.mX = ds.active_legacy.transform.tx();
1444 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001445 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001446 info.mWidth = ds.active_legacy.w;
1447 info.mHeight = ds.active_legacy.h;
1448 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001449 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001450 info.mFlags = ds.flags;
1451 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001452 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001453 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1454 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1455 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1456 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001457 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001458 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001459 if (buffer != 0) {
1460 info.mActiveBufferWidth = buffer->getWidth();
1461 info.mActiveBufferHeight = buffer->getHeight();
1462 info.mActiveBufferStride = buffer->getStride();
1463 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001464 } else {
1465 info.mActiveBufferWidth = 0;
1466 info.mActiveBufferHeight = 0;
1467 info.mActiveBufferStride = 0;
1468 info.mActiveBufferFormat = 0;
1469 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001470 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001471 info.mNumQueuedFrames = getQueuedFrameCount();
1472 info.mRefreshPending = isBufferLatched();
1473 info.mIsOpaque = isOpaque(ds);
1474 info.mContentDirty = contentDirty;
1475 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001476}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001477
Yiwei Zhang5434a782018-12-05 18:06:32 -08001478void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001479 result.append("-------------------------------");
1480 result.append("-------------------------------");
1481 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001482 result.append(" Layer name\n");
1483 result.append(" Z | ");
1484 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001485 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001486 result.append(" Disp Frame (LTRB) | ");
1487 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001488 result.append("-------------------------------");
1489 result.append("-------------------------------");
1490 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001491}
1492
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001493void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1494 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1495 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001496 return;
1497 }
1498
Yiwei Zhang5434a782018-12-05 18:06:32 -08001499 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001500 if (mName.length() > 77) {
1501 std::string shortened;
1502 shortened.append(mName.string(), 36);
1503 shortened.append("[...]");
1504 shortened.append(mName.string() + (mName.length() - 36), 36);
Yiwei Zhang5434a782018-12-05 18:06:32 -08001505 name = shortened;
Dan Stozae22aec72016-08-01 13:20:59 -07001506 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001507 name = std::string(mName.string(), mName.size());
Dan Stozae22aec72016-08-01 13:20:59 -07001508 }
1509
Yiwei Zhang5434a782018-12-05 18:06:32 -08001510 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001511
Alec Mourib416efd2018-09-06 21:01:59 +00001512 const State& layerState(getDrawingState());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001513 const auto& compositionState = outputLayer->getState();
1514
Chia-I Wu1e043612018-03-01 09:45:09 -08001515 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001516 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001517 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001518 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001519 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001520 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
1521 StringAppendF(&result, "%10s | ",
1522 toString(getCompositionLayer() ? compositionState.bufferTransform
1523 : static_cast<Hwc2::Transform>(0))
1524 .c_str());
1525 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001526 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001527 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001528 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1529 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001530
Yichi Chen6ca35192018-05-29 12:20:43 +08001531 result.append("- - - - - - - - - - - - - - - -");
1532 result.append("- - - - - - - - - - - - - - - -");
1533 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001534}
Dan Stozae22aec72016-08-01 13:20:59 -07001535
Yiwei Zhang5434a782018-12-05 18:06:32 -08001536void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001537 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001538}
1539
Svetoslavd85084b2014-03-20 10:28:31 -07001540void Layer::clearFrameStats() {
1541 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001542}
1543
Jamie Gennis6547ff42013-07-16 20:12:42 -07001544void Layer::logFrameStats() {
1545 mFrameTracker.logAndResetStats(mName);
1546}
1547
Svetoslavd85084b2014-03-20 10:28:31 -07001548void Layer::getFrameStats(FrameStats* outStats) const {
1549 mFrameTracker.getStats(outStats);
1550}
1551
Yiwei Zhang5434a782018-12-05 18:06:32 -08001552void Layer::dumpFrameEvents(std::string& result) {
1553 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001554 Mutex::Autolock lock(mFrameEventHistoryMutex);
1555 mFrameEventHistory.checkFencesForCompletion();
1556 mFrameEventHistory.dump(result);
1557}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001558
Brian Anderson5ea5e592016-12-01 16:54:33 -08001559void Layer::onDisconnect() {
1560 Mutex::Autolock lock(mFrameEventHistoryMutex);
1561 mFrameEventHistory.onDisconnect();
Yiwei Zhangaf8ee942018-11-22 00:15:23 -08001562 mFlinger->mTimeStats->onDestroy(getSequence());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001563}
1564
Brian Anderson3890c392016-07-25 12:48:08 -07001565void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001566 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001567 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001568 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1569 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001570 }
1571
Brian Andersond6927fb2016-07-23 23:37:30 -07001572 Mutex::Autolock lock(mFrameEventHistoryMutex);
1573 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001574 // If there are any unsignaled fences in the aquire timeline at this
1575 // point, the previously queued frame hasn't been latched yet. Go ahead
1576 // and try to get the signal time here so the syscall is taken out of
1577 // the main thread's critical path.
1578 mAcquireTimeline.updateSignalTimes();
1579 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001580 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001581 mFrameEventHistory.addQueue(*newTimestamps);
1582 }
1583
Brian Anderson3890c392016-07-25 12:48:08 -07001584 if (outDelta) {
1585 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001586 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001587}
Dan Stozae77c7662016-05-13 11:37:28 -07001588
Chia-I Wu98f1c102017-05-30 14:54:08 -07001589size_t Layer::getChildrenCount() const {
1590 size_t count = 0;
1591 for (const sp<Layer>& child : mCurrentChildren) {
1592 count += 1 + child->getChildrenCount();
1593 }
1594 return count;
1595}
1596
Robert Carr1f0a16a2016-10-24 16:27:39 -07001597void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001598 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001599 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001600
Robert Carr1f0a16a2016-10-24 16:27:39 -07001601 mCurrentChildren.add(layer);
1602 layer->setParent(this);
1603}
1604
1605ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001606 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001607 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001608
Robert Carr1323c952019-01-28 18:13:27 -08001609 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001610 return mCurrentChildren.remove(layer);
1611}
1612
Robert Carr1db73f62016-12-21 12:58:51 -08001613bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1614 sp<Handle> handle = nullptr;
1615 sp<Layer> newParent = nullptr;
1616 if (newParentHandle == nullptr) {
1617 return false;
1618 }
1619 handle = static_cast<Handle*>(newParentHandle.get());
1620 newParent = handle->owner.promote();
1621 if (newParent == nullptr) {
1622 ALOGE("Unable to promote Layer handle");
1623 return false;
1624 }
1625
chaviw5aedec92018-10-22 10:40:38 -07001626 if (attachChildren()) {
1627 setTransactionFlags(eTransactionNeeded);
1628 }
Robert Carr1db73f62016-12-21 12:58:51 -08001629 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001630 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001631 }
1632 mCurrentChildren.clear();
1633
1634 return true;
1635}
1636
Robert Carr15eae092018-03-23 13:43:53 -07001637void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001638 for (const sp<Layer>& child : mDrawingChildren) {
1639 child->mDrawingParent = newParent;
Vishnu Nair4351ad52019-02-11 14:13:02 -08001640 child->computeBounds(newParent->mBounds, newParent->getTransformWithScale());
Robert Carr578038f2018-03-09 12:25:24 -08001641 }
1642}
1643
chaviwf1961f72017-09-18 16:41:07 -07001644bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001645 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001646
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001647 // While layers are detached, we allow most operations
1648 // and simply halt performing the actual transaction. However
1649 // for reparent != null we would enter the mRemovedFromCurrentState
1650 // state, regardless of whether doTransaction was called, and
1651 // so we need to prevent the update here.
1652 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001653 return false;
1654 }
1655
Robert Carr54cf5b12019-01-25 14:02:28 -08001656 sp<Layer> newParent;
1657 if (newParentHandle != nullptr) {
1658 auto handle = static_cast<Handle*>(newParentHandle.get());
1659 newParent = handle->owner.promote();
1660 if (newParent == nullptr) {
1661 ALOGE("Unable to promote Layer handle");
1662 return false;
1663 }
1664 if (newParent == this) {
1665 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1666 return false;
1667 }
1668 }
1669
chaviwf1961f72017-09-18 16:41:07 -07001670 sp<Layer> parent = getParent();
1671 if (parent != nullptr) {
1672 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001673 }
1674
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001675 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001676 newParent->addChild(this);
1677 if (!newParent->isRemovedFromCurrentState()) {
1678 addToCurrentState();
1679 } else {
1680 onRemovedFromCurrentState();
1681 }
1682
1683 if (mLayerDetached) {
1684 mLayerDetached = false;
1685 callSetTransactionFlags = true;
1686 }
1687 } else {
1688 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001689 }
1690
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001691 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001692 setTransactionFlags(eTransactionNeeded);
1693 }
chaviw06178942017-07-27 10:25:59 -07001694 return true;
1695}
1696
Robert Carr9524cb32017-02-13 11:32:32 -08001697bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001698 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001699 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001700 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001701 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001702 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001703 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001704 }
Robert Carr7f619b22017-11-06 12:56:35 -08001705 }
Robert Carr9524cb32017-02-13 11:32:32 -08001706
1707 return true;
1708}
1709
chaviw5aedec92018-10-22 10:40:38 -07001710bool Layer::attachChildren() {
1711 bool changed = false;
1712 for (const sp<Layer>& child : mCurrentChildren) {
1713 sp<Client> parentClient = mClientRef.promote();
1714 sp<Client> client(child->mClientRef.promote());
1715 if (client != nullptr && parentClient != client) {
1716 if (child->mLayerDetached) {
1717 child->mLayerDetached = false;
1718 changed = true;
1719 }
1720 changed |= child->attachChildren();
1721 }
1722 }
1723
1724 return changed;
1725}
1726
Peiyong Lind3788632018-09-18 16:01:31 -07001727bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001728 static const mat4 identityMatrix = mat4();
1729
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001730 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001731 return false;
1732 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001733 ++mCurrentState.sequence;
1734 mCurrentState.colorTransform = matrix;
1735 mCurrentState.hasColorTransform = matrix != identityMatrix;
1736 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001737 setTransactionFlags(eTransactionNeeded);
1738 return true;
1739}
1740
chaviwf66724d2018-11-28 16:35:21 -08001741mat4 Layer::getColorTransform() const {
1742 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1743 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1744 colorTransform = parent->getColorTransform() * colorTransform;
1745 }
1746 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001747}
1748
1749bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001750 bool hasColorTransform = getDrawingState().hasColorTransform;
1751 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1752 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1753 }
1754 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001755}
1756
Chia-I Wu11481472018-05-04 10:43:19 -07001757bool Layer::isLegacyDataSpace() const {
1758 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001759 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001760 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001761}
1762
Robert Carr1f0a16a2016-10-24 16:27:39 -07001763void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001764 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001765}
1766
1767void Layer::clearSyncPoints() {
1768 for (const auto& child : mCurrentChildren) {
1769 child->clearSyncPoints();
1770 }
1771
1772 Mutex::Autolock lock(mLocalSyncPointMutex);
1773 for (auto& point : mLocalSyncPoints) {
1774 point->setFrameAvailable();
1775 }
1776 mLocalSyncPoints.clear();
1777}
1778
1779int32_t Layer::getZ() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001780 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001781}
1782
Robert Carr29abff82017-12-04 13:51:20 -08001783bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1784 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001785 const State& state = useDrawing ? mDrawingState : mCurrentState;
Robert Carr29abff82017-12-04 13:51:20 -08001786 return state.zOrderRelativeOf != nullptr;
1787}
1788
David Sodman41fdfc92017-11-06 16:09:56 -08001789__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001790 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001791 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1792 "makeTraversalList received invalid stateSet");
1793 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1794 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001795 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001796
Robert Carr29abff82017-12-04 13:51:20 -08001797 if (state.zOrderRelatives.size() == 0) {
1798 *outSkipRelativeZUsers = true;
1799 return children;
1800 }
1801
chaviwfd462612018-05-31 16:11:27 -07001802 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001803 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001804 sp<Layer> strongRelative = weakRelative.promote();
1805 if (strongRelative != nullptr) {
1806 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001807 }
1808 }
1809
Dan Stoza412903f2017-04-27 13:42:17 -07001810 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001811 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
Robert Carr503c7042017-09-27 15:06:08 -07001812 if (childState.zOrderRelativeOf != nullptr) {
1813 continue;
1814 }
Robert Carrdb66e622017-04-10 16:55:57 -07001815 traverse.add(child);
1816 }
1817
1818 return traverse;
1819}
1820
Robert Carr1f0a16a2016-10-24 16:27:39 -07001821/**
Robert Carrdb66e622017-04-10 16:55:57 -07001822 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001823 */
Dan Stoza412903f2017-04-27 13:42:17 -07001824void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001825 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1826 // produce a new list for traversing, including our relatives, and not including our children
1827 // who are relatives of another surface. In the case that there are no relative Z,
1828 // makeTraversalList returns our children directly to avoid significant overhead.
1829 // However in this case we need to take the responsibility for filtering children which
1830 // are relatives of another surface here.
1831 bool skipRelativeZUsers = false;
1832 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001833
Robert Carr1f0a16a2016-10-24 16:27:39 -07001834 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001835 for (; i < list.size(); i++) {
1836 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001837 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1838 continue;
1839 }
1840
Robert Carrdb66e622017-04-10 16:55:57 -07001841 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001842 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001843 }
Dan Stoza412903f2017-04-27 13:42:17 -07001844 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001845 }
Robert Carr29abff82017-12-04 13:51:20 -08001846
Dan Stoza412903f2017-04-27 13:42:17 -07001847 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001848 for (; i < list.size(); i++) {
1849 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001850
Robert Carr29abff82017-12-04 13:51:20 -08001851 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1852 continue;
1853 }
Dan Stoza412903f2017-04-27 13:42:17 -07001854 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001855 }
1856}
1857
1858/**
Robert Carrdb66e622017-04-10 16:55:57 -07001859 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001860 */
Dan Stoza412903f2017-04-27 13:42:17 -07001861void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1862 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001863 // See traverseInZOrder for documentation.
1864 bool skipRelativeZUsers = false;
1865 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001866
Robert Carr1f0a16a2016-10-24 16:27:39 -07001867 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001868 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001869 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001870
1871 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1872 continue;
1873 }
1874
Robert Carrdb66e622017-04-10 16:55:57 -07001875 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001876 break;
1877 }
Dan Stoza412903f2017-04-27 13:42:17 -07001878 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001879 }
Dan Stoza412903f2017-04-27 13:42:17 -07001880 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001881 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001882 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001883
1884 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1885 continue;
1886 }
1887
Dan Stoza412903f2017-04-27 13:42:17 -07001888 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001889 }
1890}
1891
chaviw4b129c22018-04-09 16:19:43 -07001892LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1893 const std::vector<Layer*>& layersInTree) {
1894 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1895 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001896 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1897 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001898 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001899
chaviwfd462612018-05-31 16:11:27 -07001900 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001901 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1902 sp<Layer> strongRelative = weakRelative.promote();
1903 // Only add relative layers that are also descendents of the top most parent of the tree.
1904 // If a relative layer is not a descendent, then it should be ignored.
1905 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1906 traverse.add(strongRelative);
1907 }
1908 }
1909
1910 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001911 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001912 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1913 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1914 // the child here since it won't be added later as a relative.
1915 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1916 childState.zOrderRelativeOf.promote().get())) {
1917 continue;
1918 }
1919 traverse.add(child);
1920 }
1921
1922 return traverse;
1923}
1924
1925void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1926 LayerVector::StateSet stateSet,
1927 const LayerVector::Visitor& visitor) {
1928 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001929
1930 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001931 for (; i < list.size(); i++) {
1932 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001933 if (relative->getZ() >= 0) {
1934 break;
1935 }
chaviw4b129c22018-04-09 16:19:43 -07001936 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001937 }
chaviw4b129c22018-04-09 16:19:43 -07001938
chaviwa76b2712017-09-20 12:02:26 -07001939 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001940 for (; i < list.size(); i++) {
1941 const auto& relative = list[i];
1942 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001943 }
1944}
1945
chaviw4b129c22018-04-09 16:19:43 -07001946std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1947 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1948 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1949
1950 std::vector<Layer*> layersInTree = {this};
1951 for (size_t i = 0; i < children.size(); i++) {
1952 const auto& child = children[i];
1953 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1954 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1955 }
1956
1957 return layersInTree;
1958}
1959
1960void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1961 const LayerVector::Visitor& visitor) {
1962 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1963 std::sort(layersInTree.begin(), layersInTree.end());
1964 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1965}
1966
Peiyong Linefefaac2018-08-17 12:27:51 -07001967ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001968 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001969}
1970
chaviw13fdc492017-06-27 12:40:18 -07001971half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001972 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001973
chaviw13fdc492017-06-27 12:40:18 -07001974 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1975 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001976}
Robert Carr6452f122017-03-21 10:41:29 -07001977
chaviw13fdc492017-06-27 12:40:18 -07001978half4 Layer::getColor() const {
1979 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001980 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001981}
Robert Carr6452f122017-03-21 10:41:29 -07001982
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001983Layer::RoundedCornerState Layer::getRoundedCornerState() const {
1984 const auto& p = mDrawingParent.promote();
1985 if (p != nullptr) {
1986 RoundedCornerState parentState = p->getRoundedCornerState();
1987 if (parentState.radius > 0) {
1988 ui::Transform t = getActiveTransform(getDrawingState());
1989 t = t.inverse();
1990 parentState.cropRect = t.transform(parentState.cropRect);
1991 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1992 // but a transform matrix can define horizontal and vertical scales.
1993 // Let's take the average between both of them and pass into the shader, practically we
1994 // never do this type of transformation on windows anyway.
1995 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
1996 return parentState;
1997 }
1998 }
1999 const float radius = getDrawingState().cornerRadius;
Vishnu Nair4351ad52019-02-11 14:13:02 -08002000 return radius > 0 ? RoundedCornerState(getBounds(), radius) : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002001}
2002
Robert Carr1f0a16a2016-10-24 16:27:39 -07002003void Layer::commitChildList() {
2004 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
2005 const auto& child = mCurrentChildren[i];
2006 child->commitChildList();
2007 }
2008 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07002009 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002010}
2011
Robert Carr720e5062018-07-30 17:45:14 -07002012void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002013 mCurrentState.inputInfo = info;
2014 mCurrentState.modified = true;
2015 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07002016 setTransactionFlags(eTransactionNeeded);
2017}
2018
chaviw1d044282017-09-27 12:19:28 -07002019void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
2020 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2021 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002022 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07002023
Peiyong Linefefaac2018-08-17 12:27:51 -07002024 ui::Transform requestedTransform = state.active_legacy.transform;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002025 ui::Transform transform = getTransform();
chaviw1d044282017-09-27 12:19:28 -07002026
2027 layerInfo->set_id(sequence);
2028 layerInfo->set_name(getName().c_str());
2029 layerInfo->set_type(String8(getTypeId()));
2030
2031 for (const auto& child : children) {
2032 layerInfo->add_children(child->sequence);
2033 }
2034
2035 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
2036 sp<Layer> strongRelative = weakRelative.promote();
2037 if (strongRelative != nullptr) {
2038 layerInfo->add_relatives(strongRelative->sequence);
2039 }
2040 }
2041
Marissa Wallf58c14b2018-07-24 10:50:43 -07002042 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
Nataniel Borges797b0e42019-02-15 14:11:58 -08002043 [&]() { return layerInfo->mutable_transparent_region(); });
2044 LayerProtoHelper::writeToProto(visibleRegion,
2045 [&]() { return layerInfo->mutable_visible_region(); });
2046 LayerProtoHelper::writeToProto(surfaceDamageRegion,
2047 [&]() { return layerInfo->mutable_damage_region(); });
chaviw1d044282017-09-27 12:19:28 -07002048
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002049 layerInfo->set_layer_stack(getLayerStack());
chaviw1d044282017-09-27 12:19:28 -07002050 layerInfo->set_z(state.z);
2051
Nataniel Borges797b0e42019-02-15 14:11:58 -08002052 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
2053 [&]() { return layerInfo->mutable_position(); });
chaviw1d044282017-09-27 12:19:28 -07002054
Nataniel Borges797b0e42019-02-15 14:11:58 -08002055 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(), [&]() {
2056 return layerInfo->mutable_requested_position();
2057 });
chaviw1d044282017-09-27 12:19:28 -07002058
Nataniel Borges797b0e42019-02-15 14:11:58 -08002059 LayerProtoHelper::writeSizeToProto(state.active_legacy.w, state.active_legacy.h,
2060 [&]() { return layerInfo->mutable_size(); });
chaviw1d044282017-09-27 12:19:28 -07002061
Nataniel Borges797b0e42019-02-15 14:11:58 -08002062 LayerProtoHelper::writeToProto(state.crop_legacy, [&]() { return layerInfo->mutable_crop(); });
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002063 layerInfo->set_corner_radius(getRoundedCornerState().radius);
chaviw1d044282017-09-27 12:19:28 -07002064
2065 layerInfo->set_is_opaque(isOpaque(state));
2066 layerInfo->set_invalidate(contentDirty);
Chia-I Wu01591c92018-05-22 12:03:00 -07002067
2068 // XXX (b/79210409) mCurrentDataSpace is not protected
2069 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
2070
chaviw1d044282017-09-27 12:19:28 -07002071 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
Nataniel Borges797b0e42019-02-15 14:11:58 -08002072 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
2073 LayerProtoHelper::writeToProto(state.color,
2074 [&]() { return layerInfo->mutable_requested_color(); });
chaviw1d044282017-09-27 12:19:28 -07002075 layerInfo->set_flags(state.flags);
2076
2077 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
2078 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
2079
Jorim Jaggi8e0af362017-11-14 16:28:28 +01002080 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07002081 if (parent != nullptr) {
2082 layerInfo->set_parent(parent->sequence);
Nataniel Borgesdcc0bab2019-02-15 13:22:03 -08002083 } else {
2084 layerInfo->set_parent(-1);
chaviw1d044282017-09-27 12:19:28 -07002085 }
2086
2087 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2088 if (zOrderRelativeOf != nullptr) {
2089 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
Nataniel Borgesdcc0bab2019-02-15 13:22:03 -08002090 } else {
2091 layerInfo->set_z_order_relative_of(-1);
chaviw1d044282017-09-27 12:19:28 -07002092 }
2093
Lloyd Pique0b785d82018-12-04 17:25:27 -08002094 auto buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08002095 if (buffer != nullptr) {
Nataniel Borges797b0e42019-02-15 14:11:58 -08002096 LayerProtoHelper::writeToProto(buffer,
2097 [&]() { return layerInfo->mutable_active_buffer(); });
Peiyong Linefefaac2018-08-17 12:27:51 -07002098 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
Yichi Chen6ca35192018-05-29 12:20:43 +08002099 layerInfo->mutable_buffer_transform());
chaviw1d044282017-09-27 12:19:28 -07002100 }
2101
2102 layerInfo->set_queued_frames(getQueuedFrameCount());
2103 layerInfo->set_refresh_pending(isBufferLatched());
chaviwadc40c22018-07-10 16:57:27 -07002104 layerInfo->set_curr_frame(mCurrentFrameNumber);
Vishnu Nairf2deb822018-11-12 17:53:48 -08002105 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
chaviwadc40c22018-07-10 16:57:27 -07002106
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002107 for (const auto& pendingState : mPendingStates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07002108 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
chaviwadc40c22018-07-10 16:57:27 -07002109 if (barrierLayer != nullptr) {
2110 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
2111 barrierLayerProto->set_id(barrierLayer->sequence);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002112 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
chaviwadc40c22018-07-10 16:57:27 -07002113 }
2114 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002115
2116 auto protoMap = layerInfo->mutable_metadata();
2117 for (const auto& entry : state.metadata.mMap) {
2118 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
2119 }
Vishnu Nair4351ad52019-02-11 14:13:02 -08002120 LayerProtoHelper::writeToProto(mEffectiveTransform, layerInfo->mutable_effective_transform());
Nataniel Borges797b0e42019-02-15 14:11:58 -08002121 LayerProtoHelper::writeToProto(mSourceBounds,
2122 [&]() { return layerInfo->mutable_source_bounds(); });
2123 LayerProtoHelper::writeToProto(mScreenBounds,
2124 [&]() { return layerInfo->mutable_screen_bounds(); });
2125 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
chaviw1d044282017-09-27 12:19:28 -07002126}
2127
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002128void Layer::writeToProto(LayerProto* layerInfo, const sp<DisplayDevice>& displayDevice) {
2129 auto outputLayer = findOutputLayerForDisplay(displayDevice);
2130 if (!outputLayer) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07002131 return;
2132 }
2133
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002134 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
2135
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002136 const auto& compositionState = outputLayer->getState();
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002137
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002138 const Rect& frame = compositionState.displayFrame;
Nataniel Borges797b0e42019-02-15 14:11:58 -08002139 LayerProtoHelper::writeToProto(frame, [&]() { return layerInfo->mutable_hwc_frame(); });
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002140
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002141 const FloatRect& crop = compositionState.sourceCrop;
Nataniel Borges797b0e42019-02-15 14:11:58 -08002142 LayerProtoHelper::writeToProto(crop, [&]() { return layerInfo->mutable_hwc_crop(); });
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002143
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002144 const int32_t transform =
2145 getCompositionLayer() ? static_cast<int32_t>(compositionState.bufferTransform) : 0;
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002146 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002147
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002148 const int32_t compositionType =
2149 static_cast<int32_t>(compositionState.hwc ? (*compositionState.hwc).hwcCompositionType
2150 : Hwc2::IComposerClient::Composition::CLIENT);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002151 layerInfo->set_hwc_composition_type(compositionType);
2152
2153 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
2154 static_cast<BufferLayer*>(this)->isProtected()) {
2155 layerInfo->set_is_protected(true);
2156 } else {
2157 layerInfo->set_is_protected(false);
2158 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002159}
2160
Robert Carr2e102c92018-10-23 12:11:15 -07002161bool Layer::isRemovedFromCurrentState() const {
2162 return mRemovedFromCurrentState;
2163}
2164
Arthur Hungd20b2702019-01-14 18:16:16 +08002165InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002166 InputWindowInfo info = mDrawingState.inputInfo;
Robert Carr720e5062018-07-30 17:45:14 -07002167
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002168 if (info.displayId == ADISPLAY_ID_NONE) {
2169 info.displayId = mDrawingState.layerStack;
2170 }
2171
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002172 ui::Transform t = getTransform();
2173 const float xScale = t.sx();
2174 const float yScale = t.sy();
2175 if (xScale != 1.0f || yScale != 1.0f) {
2176 info.windowXScale *= 1.0f / xScale;
2177 info.windowYScale *= 1.0f / yScale;
2178 info.touchableRegion.scaleSelf(xScale, yScale);
Riddle Hsu39d4aa52018-11-30 20:46:53 +08002179 }
Robert Carre07e1032018-11-26 12:55:53 -08002180
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002181 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002182 // If this is a portal window, set the touchableRegion to the layerBounds.
2183 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
2184 ? getBufferSize(getDrawingState())
2185 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002186 if (!layerBounds.isValid()) {
2187 layerBounds = getCroppedBufferSize(getDrawingState());
2188 }
Vishnu Nair8033a492018-12-05 07:27:23 -08002189 layerBounds = t.transform(layerBounds);
2190 layerBounds.inset(info.surfaceInset, info.surfaceInset, info.surfaceInset, info.surfaceInset);
2191
Arthur Hungd20b2702019-01-14 18:16:16 +08002192 // Input coordinate should match the layer bounds.
2193 info.frameLeft = layerBounds.left;
2194 info.frameTop = layerBounds.top;
2195 info.frameRight = layerBounds.right;
2196 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08002197
2198 // Position the touchable region relative to frame screen location and restrict it to frame
2199 // bounds.
2200 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08002201 info.visible = canReceiveInput();
Robert Carr720e5062018-07-30 17:45:14 -07002202 return info;
2203}
2204
2205bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002206 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002207}
2208
Lloyd Piquefeb73d72018-12-04 17:23:44 -08002209std::shared_ptr<compositionengine::Layer> Layer::getCompositionLayer() const {
2210 return nullptr;
2211}
2212
chaviw3e727cd2019-01-31 13:41:05 -08002213bool Layer::canReceiveInput() const {
2214 return isVisible();
2215}
2216
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002217compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
2218 const sp<const DisplayDevice>& display) const {
2219 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionLayer().get());
2220}
2221
Mathias Agopian13127d82013-03-05 17:47:11 -08002222// ---------------------------------------------------------------------------
2223
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002224}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002225
2226#if defined(__gl_h_)
2227#error "don't include gl/gl.h in this file"
2228#endif
2229
2230#if defined(__gl2_h_)
2231#error "don't include gl2/gl2.h in this file"
2232#endif