blob: 45679166bac79534a7e5d0d83679adfe11fa612d [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dan Stoza9e56aa02015-11-02 13:00:03 -080017//#define LOG_NDEBUG 0
18#undef LOG_TAG
19#define LOG_TAG "Layer"
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080020#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21
Mathias Agopian13127d82013-03-05 17:47:11 -080022#include <math.h>
David Sodman41fdfc92017-11-06 16:09:56 -080023#include <stdint.h>
24#include <stdlib.h>
25#include <sys/types.h>
chaviw4b129c22018-04-09 16:19:43 -070026#include <algorithm>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080027#include <mutex>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080028
Yiwei Zhang5434a782018-12-05 18:06:32 -080029#include <android-base/stringprintf.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080030#include <compositionengine/Display.h>
Lloyd Pique0b785d82018-12-04 17:25:27 -080031#include <compositionengine/Layer.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080032#include <compositionengine/OutputLayer.h>
Lloyd Pique0b785d82018-12-04 17:25:27 -080033#include <compositionengine/impl/LayerCompositionState.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080034#include <compositionengine/impl/OutputLayerCompositionState.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070035#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070036#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070037#include <cutils/properties.h>
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080038#include <gui/BufferItem.h>
39#include <gui/LayerDebugInfo.h>
40#include <gui/Surface.h>
41#include <renderengine/RenderEngine.h>
42#include <ui/DebugUtils.h>
43#include <ui/GraphicBuffer.h>
44#include <ui/PixelFormat.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045#include <utils/Errors.h>
46#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080047#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080049#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050
Yiwei Zhang60d1a192018-03-07 14:52:28 -080051#include "BufferLayer.h"
Valerie Haudd0b7572019-01-29 14:59:27 -080052#include "ColorLayer.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020053#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070054#include "DisplayDevice.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080055#include "DisplayHardware/HWComposer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056#include "Layer.h"
Lloyd Pique37c2c9b2018-12-04 17:25:10 -080057#include "LayerProtoHelper.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070058#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070059#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060#include "SurfaceFlinger.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080061#include "TimeStats/TimeStats.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070062
David Sodman41fdfc92017-11-06 16:09:56 -080063#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065namespace android {
66
Yiwei Zhang5434a782018-12-05 18:06:32 -080067using base::StringAppendF;
68
Lloyd Piquef1c675b2018-09-12 20:45:39 -070069std::atomic<int32_t> Layer::sSequence{1};
Mathias Agopian13127d82013-03-05 17:47:11 -080070
Lloyd Pique42ab75e2018-09-12 20:46:03 -070071Layer::Layer(const LayerCreationArgs& args)
Lloyd Piquefa8fd6b2019-01-29 18:42:24 -080072 : mFlinger(args.flinger), mName(args.name), mClientRef(args.client) {
Mathias Agopiana67932f2011-04-20 14:20:59 -070073 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070074
75 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -070076 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
77 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
78 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070079
Dan Stozaf7ba41a2017-05-10 15:11:11 -070080 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070081
Lloyd Pique0449b0f2018-12-20 16:23:45 -080082 mCurrentState.active_legacy.w = args.w;
83 mCurrentState.active_legacy.h = args.h;
84 mCurrentState.flags = layerFlags;
85 mCurrentState.active_legacy.transform.set(0, 0);
86 mCurrentState.crop_legacy.makeInvalid();
87 mCurrentState.requestedCrop_legacy = mCurrentState.crop_legacy;
88 mCurrentState.z = 0;
89 mCurrentState.color.a = 1.0f;
90 mCurrentState.layerStack = 0;
91 mCurrentState.sequence = 0;
92 mCurrentState.requested_legacy = mCurrentState.active_legacy;
Lloyd Pique0449b0f2018-12-20 16:23:45 -080093 mCurrentState.active.w = UINT32_MAX;
94 mCurrentState.active.h = UINT32_MAX;
95 mCurrentState.active.transform.set(0, 0);
96 mCurrentState.transform = 0;
97 mCurrentState.transformToDisplayInverse = false;
98 mCurrentState.crop.makeInvalid();
99 mCurrentState.acquireFence = new Fence(-1);
100 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
101 mCurrentState.hdrMetadata.validTypes = 0;
102 mCurrentState.surfaceDamageRegion.clear();
103 mCurrentState.cornerRadius = 0.0f;
104 mCurrentState.api = -1;
105 mCurrentState.hasColorTransform = false;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700106
107 // drawing state & current state are identical
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800108 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700109
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800110 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700111 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800112 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200113 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700114
115 mFlinger->onLayerCreated();
Dan Stoza436ccf32018-06-21 12:10:12 -0700116}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700117
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700118Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800119 sp<Client> c(mClientRef.promote());
120 if (c != 0) {
121 c->detachLayer(this);
122 }
123
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000124 mFrameTracker.logAndResetStats(mName);
Robert Carr2e102c92018-10-23 12:11:15 -0700125
Robert Carr2e102c92018-10-23 12:11:15 -0700126 mFlinger->onLayerDestroyed();
Mathias Agopian96f08192010-06-02 23:28:45 -0700127}
128
Mathias Agopian13127d82013-03-05 17:47:11 -0800129// ---------------------------------------------------------------------------
130// callbacks
131// ---------------------------------------------------------------------------
132
David Sodmaneb085e02017-10-05 18:49:04 -0700133/*
134 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
135 * Layer. So, the implementation is done in BufferLayer. When called on a
136 * ColorLayer object, it's essentially a NOP.
137 */
David Sodmaneb085e02017-10-05 18:49:04 -0700138void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800139
Chia-I Wuc6657022017-08-15 11:18:17 -0700140void Layer::onRemovedFromCurrentState() {
Robert Carr2e102c92018-10-23 12:11:15 -0700141 mRemovedFromCurrentState = true;
142
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800143 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
144 if (mCurrentState.zOrderRelativeOf != nullptr) {
145 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
146 if (strongRelative != nullptr) {
147 strongRelative->removeZOrderRelative(this);
148 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700149 }
chaviw606e5cf2019-03-01 10:12:10 -0800150 setZOrderRelativeOf(nullptr);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700151 }
Rob Carr4bba3702018-10-08 21:53:30 +0000152
Robert Carr2e102c92018-10-23 12:11:15 -0700153 // Since we are no longer reachable from CurrentState SurfaceFlinger
154 // will no longer invoke doTransaction for us, and so we will
155 // never finish applying transactions. We signal the sync point
156 // now so that another layer will not become indefinitely
157 // blocked.
158 for (auto& point: mRemoteSyncPoints) {
159 point->setTransactionApplied();
160 }
161 mRemoteSyncPoints.clear();
162
163 {
164 Mutex::Autolock syncLock(mLocalSyncPointMutex);
165 for (auto& point : mLocalSyncPoints) {
166 point->setFrameAvailable();
167 }
168 mLocalSyncPoints.clear();
169 }
170
Chia-I Wuc6657022017-08-15 11:18:17 -0700171 for (const auto& child : mCurrentChildren) {
172 child->onRemovedFromCurrentState();
173 }
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800174
175 mFlinger->markLayerPendingRemovalLocked(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700176}
Chia-I Wu38512252017-05-17 14:36:16 -0700177
chaviw61626f22018-11-15 16:26:27 -0800178void Layer::addToCurrentState() {
179 mRemovedFromCurrentState = false;
180
181 for (const auto& child : mCurrentChildren) {
182 child->addToCurrentState();
183 }
184}
185
Mathias Agopian13127d82013-03-05 17:47:11 -0800186// ---------------------------------------------------------------------------
187// set-up
188// ---------------------------------------------------------------------------
189
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700190const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800191 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800192}
193
chaviw13fdc492017-06-27 12:40:18 -0700194bool Layer::getPremultipledAlpha() const {
195 return mPremultipliedAlpha;
196}
197
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700198sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800199 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700200 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800201}
202
203// ---------------------------------------------------------------------------
204// h/w composer set-up
205// ---------------------------------------------------------------------------
206
Lloyd Pique37c2c9b2018-12-04 17:25:10 -0800207bool Layer::hasHwcLayer(const sp<const DisplayDevice>& displayDevice) {
208 auto outputLayer = findOutputLayerForDisplay(displayDevice);
209 LOG_FATAL_IF(!outputLayer);
210 return outputLayer->getState().hwc && (*outputLayer->getState().hwc).hwcLayer != nullptr;
211}
212
213HWC2::Layer* Layer::getHwcLayer(const sp<const DisplayDevice>& displayDevice) {
214 auto outputLayer = findOutputLayerForDisplay(displayDevice);
215 if (!outputLayer || !outputLayer->getState().hwc) {
216 return nullptr;
217 }
218 return (*outputLayer->getState().hwc).hwcLayer.get();
Steven Thomasb02664d2017-07-26 18:48:28 -0700219}
Steven Thomasb02664d2017-07-26 18:48:28 -0700220
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800221Rect Layer::getContentCrop() const {
222 // this is the crop rectangle that applies to the buffer
223 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700224 Rect crop;
225 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800226 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700227 crop = mCurrentCrop;
Lloyd Pique0b785d82018-12-04 17:25:27 -0800228 } else if (mActiveBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800229 // otherwise we use the whole buffer
Lloyd Pique0b785d82018-12-04 17:25:27 -0800230 crop = mActiveBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700231 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800232 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700233 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700234 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700235 return crop;
236}
237
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700238static Rect reduce(const Rect& win, const Region& exclude) {
239 if (CC_LIKELY(exclude.isEmpty())) {
240 return win;
241 }
242 if (exclude.isRect()) {
243 return win.reduce(exclude.getBounds());
244 }
245 return Region(win).subtract(exclude).getBounds();
246}
247
Dan Stoza80d61162017-12-20 15:57:52 -0800248static FloatRect reduce(const FloatRect& win, const Region& exclude) {
249 if (CC_LIKELY(exclude.isEmpty())) {
250 return win;
251 }
252 // Convert through Rect (by rounding) for lack of FloatRegion
253 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
254}
255
Vishnu Nair4351ad52019-02-11 14:13:02 -0800256Rect Layer::getScreenBounds(bool reduceTransparentRegion) const {
Vishnu Nairf0c28512019-02-08 12:40:28 -0800257 if (!reduceTransparentRegion) {
258 return Rect{mScreenBounds};
259 }
260
261 FloatRect bounds = getBounds();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800262 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800263 // Transform to screen space.
264 bounds = t.transform(bounds);
265 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700266}
267
Vishnu Nair4351ad52019-02-11 14:13:02 -0800268FloatRect Layer::getBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000269 const State& s(getDrawingState());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800270 return getBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700271}
272
Vishnu Nairf0c28512019-02-08 12:40:28 -0800273FloatRect Layer::getBounds(const Region& activeTransparentRegion) const {
274 // Subtract the transparent region and snap to the bounds.
275 return reduce(mBounds, activeTransparentRegion);
276}
277
Vishnu Nair4351ad52019-02-11 14:13:02 -0800278ui::Transform Layer::getTransformWithScale() const {
279 // If the layer is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
280 // it isFixedSize) then there may be additional scaling not accounted
281 // for in the transform. We need to mirror this scaling to child surfaces
282 // or we will break the contract where WM can treat child surfaces as
283 // pixels in the parent surface.
Lloyd Pique0b785d82018-12-04 17:25:27 -0800284 if (!isFixedSize() || !mActiveBuffer) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800285 return mEffectiveTransform;
286 }
287
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 }
chaviw606e5cf2019-03-01 10:12:10 -08001167 setZOrderRelativeOf(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
chaviw606e5cf2019-03-01 10:12:10 -08001187void Layer::setZOrderRelativeOf(const wp<Layer>& relativeOf) {
1188 mCurrentState.zOrderRelativeOf = relativeOf;
1189 mCurrentState.sequence++;
1190 mCurrentState.modified = true;
1191 setTransactionFlags(eTransactionNeeded);
1192}
1193
Robert Carr503d2bd2017-12-04 15:49:47 -08001194bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001195 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1196 if (handle == nullptr) {
1197 return false;
1198 }
1199 sp<Layer> relative = handle->owner.promote();
1200 if (relative == nullptr) {
1201 return false;
1202 }
1203
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001204 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1205 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001206 return false;
1207 }
1208
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001209 mCurrentState.sequence++;
1210 mCurrentState.modified = true;
1211 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001212
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001213 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -07001214 if (oldZOrderRelativeOf != nullptr) {
1215 oldZOrderRelativeOf->removeZOrderRelative(this);
1216 }
chaviw606e5cf2019-03-01 10:12:10 -08001217 setZOrderRelativeOf(relative);
Robert Carrdb66e622017-04-10 16:55:57 -07001218 relative->addZOrderRelative(this);
1219
1220 setTransactionFlags(eTransactionNeeded);
1221
1222 return true;
1223}
1224
Mathias Agopian13127d82013-03-05 17:47:11 -08001225bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001226 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001227 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001228 mCurrentState.requested_legacy.w = w;
1229 mCurrentState.requested_legacy.h = h;
1230 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001231 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001232
1233 // record the new size, from this point on, when the client request
1234 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001235 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001236 return true;
1237}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001238bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001239 if (mCurrentState.color.a == alpha) return false;
1240 mCurrentState.sequence++;
1241 mCurrentState.color.a = alpha;
1242 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001243 setTransactionFlags(eTransactionNeeded);
1244 return true;
1245}
chaviw13fdc492017-06-27 12:40:18 -07001246
Valerie Haudd0b7572019-01-29 14:59:27 -08001247bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1248 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001249 return false;
Valerie Hauaa194562019-02-05 16:21:38 -08001250 }
1251 mCurrentState.sequence++;
1252 mCurrentState.modified = true;
1253 setTransactionFlags(eTransactionNeeded);
1254
1255 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001256 // create background color layer if one does not yet exist
1257 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
1258 const String8& name = mName + "BackgroundColorLayer";
1259 mCurrentState.bgColorLayer =
1260 new ColorLayer(LayerCreationArgs(mFlinger.get(), nullptr, name, 0, 0, flags));
chaviw13fdc492017-06-27 12:40:18 -07001261
Valerie Haudd0b7572019-01-29 14:59:27 -08001262 // add to child list
1263 addChild(mCurrentState.bgColorLayer);
1264 mFlinger->mLayersAdded = true;
1265 // set up SF to handle added color layer
1266 if (isRemovedFromCurrentState()) {
1267 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1268 }
1269 mFlinger->setTransactionFlags(eTransactionNeeded);
1270 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1271 mCurrentState.bgColorLayer->reparent(nullptr);
1272 mCurrentState.bgColorLayer = nullptr;
1273 return true;
1274 }
1275
1276 mCurrentState.bgColorLayer->setColor(color);
1277 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1278 mCurrentState.bgColorLayer->setAlpha(alpha);
1279 mCurrentState.bgColorLayer->setDataspace(dataspace);
1280
chaviw13fdc492017-06-27 12:40:18 -07001281 return true;
1282}
1283
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001284bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001285 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001286
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001287 mCurrentState.sequence++;
1288 mCurrentState.cornerRadius = cornerRadius;
1289 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001290 setTransactionFlags(eTransactionNeeded);
1291 return true;
1292}
1293
Robert Carrd4ae7f32018-06-07 16:10:57 -07001294bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1295 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001296 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001297 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1298
1299 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1300 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1301 return false;
1302 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001303 mCurrentState.sequence++;
1304 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1305 matrix.dsdy);
1306 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001307 setTransactionFlags(eTransactionNeeded);
1308 return true;
1309}
Marissa Wall61c58622018-07-18 10:12:20 -07001310
Mathias Agopian13127d82013-03-05 17:47:11 -08001311bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001312 mCurrentState.requestedTransparentRegion_legacy = transparent;
1313 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001314 setTransactionFlags(eTransactionNeeded);
1315 return true;
1316}
1317bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001318 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1319 if (mCurrentState.flags == newFlags) return false;
1320 mCurrentState.sequence++;
1321 mCurrentState.flags = newFlags;
1322 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001323 setTransactionFlags(eTransactionNeeded);
1324 return true;
1325}
Robert Carr99e27f02016-06-16 15:18:02 -07001326
Marissa Wallf58c14b2018-07-24 10:50:43 -07001327bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001328 if (mCurrentState.requestedCrop_legacy == crop) return false;
1329 mCurrentState.sequence++;
1330 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001331 if (immediate && !mFreezeGeometryUpdates) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001332 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001333 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001334 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1335
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001336 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001337 setTransactionFlags(eTransactionNeeded);
1338 return true;
1339}
Robert Carr8d5227b2017-03-16 15:41:03 -07001340
Robert Carrc3574f72016-03-24 12:19:32 -07001341bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001342 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001343 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001344 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001345 return true;
1346}
1347
Evan Roskyef876c92019-01-25 17:46:06 -08001348bool Layer::setMetadata(const LayerMetadata& data) {
1349 if (!mCurrentState.metadata.merge(data, true /* eraseEmpty */)) return false;
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001350 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001351 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001352 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001353 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001354}
1355
Mathias Agopian13127d82013-03-05 17:47:11 -08001356bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001357 if (mCurrentState.layerStack == layerStack) return false;
1358 mCurrentState.sequence++;
1359 mCurrentState.layerStack = layerStack;
1360 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001361 setTransactionFlags(eTransactionNeeded);
1362 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001363}
1364
Robert Carr1f0a16a2016-10-24 16:27:39 -07001365uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001366 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001367 if (p == nullptr) {
1368 return getDrawingState().layerStack;
1369 }
1370 return p->getLayerStack();
1371}
1372
Marissa Wallf58c14b2018-07-24 10:50:43 -07001373void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001374 mCurrentState.barrierLayer_legacy = barrierLayer;
1375 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001376 // We don't set eTransactionNeeded, because just receiving a deferral
1377 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001378 mCurrentState.modified = true;
1379 pushPendingState();
1380 mCurrentState.barrierLayer_legacy = nullptr;
1381 mCurrentState.frameNumber_legacy = 0;
1382 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001383}
1384
Marissa Wallf58c14b2018-07-24 10:50:43 -07001385void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001386 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001387 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001388}
1389
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001390// ----------------------------------------------------------------------------
1391// pageflip handling...
1392// ----------------------------------------------------------------------------
1393
Robert Carr1f0a16a2016-10-24 16:27:39 -07001394bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001395 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001396 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001397 if (parent != nullptr && parent->isHiddenByPolicy()) {
1398 return true;
1399 }
1400 return s.flags & layer_state_t::eLayerHidden;
1401}
1402
David Sodman41fdfc92017-11-06 16:09:56 -08001403uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001404 // TODO: should we do something special if mSecure is set?
1405 if (mProtectedByApp) {
1406 // need a hardware-protected path to external video sink
1407 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001408 }
Riley Andrews03414a12014-07-01 14:22:59 -07001409 if (mPotentialCursor) {
1410 usage |= GraphicBuffer::USAGE_CURSOR;
1411 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001412 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001413 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001414}
1415
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001416void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001417 uint32_t orientation = 0;
Vishnu Nairf2deb822018-11-12 17:53:48 -08001418 // Disable setting transform hint if the debug flag is set or if the
1419 // getTransformToDisplayInverse flag is set and the client wants to submit buffers
1420 // in one orientation.
1421 if (!mFlinger->mDebugDisableTransformHint && !getTransformToDisplayInverse()) {
Mathias Agopian84300952012-11-21 16:02:13 -08001422 // The transform hint is used to improve performance, but we can
1423 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001424 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001425 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001426 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001427 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001428 orientation = 0;
1429 }
1430 }
David Sodmaneb085e02017-10-05 18:49:04 -07001431 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001432}
1433
Mathias Agopian13127d82013-03-05 17:47:11 -08001434// ----------------------------------------------------------------------------
1435// debugging
1436// ----------------------------------------------------------------------------
1437
Marissa Wall61c58622018-07-18 10:12:20 -07001438// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001439LayerDebugInfo Layer::getLayerDebugInfo() const {
1440 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001441 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001442 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001443 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001444 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
Yiwei Zhang5434a782018-12-05 18:06:32 -08001445 info.mType = std::string(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001446 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001447 info.mVisibleRegion = visibleRegion;
1448 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001449 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001450 info.mX = ds.active_legacy.transform.tx();
1451 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001452 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001453 info.mWidth = ds.active_legacy.w;
1454 info.mHeight = ds.active_legacy.h;
1455 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001456 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001457 info.mFlags = ds.flags;
1458 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001459 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001460 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1461 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1462 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1463 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001464 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001465 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001466 if (buffer != 0) {
1467 info.mActiveBufferWidth = buffer->getWidth();
1468 info.mActiveBufferHeight = buffer->getHeight();
1469 info.mActiveBufferStride = buffer->getStride();
1470 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001471 } else {
1472 info.mActiveBufferWidth = 0;
1473 info.mActiveBufferHeight = 0;
1474 info.mActiveBufferStride = 0;
1475 info.mActiveBufferFormat = 0;
1476 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001477 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001478 info.mNumQueuedFrames = getQueuedFrameCount();
1479 info.mRefreshPending = isBufferLatched();
1480 info.mIsOpaque = isOpaque(ds);
1481 info.mContentDirty = contentDirty;
1482 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001483}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001484
Yiwei Zhang5434a782018-12-05 18:06:32 -08001485void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001486 result.append("-------------------------------");
1487 result.append("-------------------------------");
1488 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001489 result.append(" Layer name\n");
1490 result.append(" Z | ");
1491 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001492 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001493 result.append(" Disp Frame (LTRB) | ");
1494 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001495 result.append("-------------------------------");
1496 result.append("-------------------------------");
1497 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001498}
1499
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001500void Layer::miniDump(std::string& result, const sp<DisplayDevice>& displayDevice) const {
1501 auto outputLayer = findOutputLayerForDisplay(displayDevice);
1502 if (!outputLayer) {
Dan Stozae22aec72016-08-01 13:20:59 -07001503 return;
1504 }
1505
Yiwei Zhang5434a782018-12-05 18:06:32 -08001506 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001507 if (mName.length() > 77) {
1508 std::string shortened;
1509 shortened.append(mName.string(), 36);
1510 shortened.append("[...]");
1511 shortened.append(mName.string() + (mName.length() - 36), 36);
Yiwei Zhang5434a782018-12-05 18:06:32 -08001512 name = shortened;
Dan Stozae22aec72016-08-01 13:20:59 -07001513 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001514 name = std::string(mName.string(), mName.size());
Dan Stozae22aec72016-08-01 13:20:59 -07001515 }
1516
Yiwei Zhang5434a782018-12-05 18:06:32 -08001517 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001518
Alec Mourib416efd2018-09-06 21:01:59 +00001519 const State& layerState(getDrawingState());
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001520 const auto& compositionState = outputLayer->getState();
1521
Chia-I Wu1e043612018-03-01 09:45:09 -08001522 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001523 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001524 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001525 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001526 }
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001527 StringAppendF(&result, "%10s | ", toString(getCompositionType(displayDevice)).c_str());
1528 StringAppendF(&result, "%10s | ",
1529 toString(getCompositionLayer() ? compositionState.bufferTransform
1530 : static_cast<Hwc2::Transform>(0))
1531 .c_str());
1532 const Rect& frame = compositionState.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001533 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08001534 const FloatRect& crop = compositionState.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001535 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1536 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001537
Yichi Chen6ca35192018-05-29 12:20:43 +08001538 result.append("- - - - - - - - - - - - - - - -");
1539 result.append("- - - - - - - - - - - - - - - -");
1540 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001541}
Dan Stozae22aec72016-08-01 13:20:59 -07001542
Yiwei Zhang5434a782018-12-05 18:06:32 -08001543void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001544 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001545}
1546
Svetoslavd85084b2014-03-20 10:28:31 -07001547void Layer::clearFrameStats() {
1548 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001549}
1550
Jamie Gennis6547ff42013-07-16 20:12:42 -07001551void Layer::logFrameStats() {
1552 mFrameTracker.logAndResetStats(mName);
1553}
1554
Svetoslavd85084b2014-03-20 10:28:31 -07001555void Layer::getFrameStats(FrameStats* outStats) const {
1556 mFrameTracker.getStats(outStats);
1557}
1558
Yiwei Zhang5434a782018-12-05 18:06:32 -08001559void Layer::dumpFrameEvents(std::string& result) {
1560 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001561 Mutex::Autolock lock(mFrameEventHistoryMutex);
1562 mFrameEventHistory.checkFencesForCompletion();
1563 mFrameEventHistory.dump(result);
1564}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001565
Brian Anderson5ea5e592016-12-01 16:54:33 -08001566void Layer::onDisconnect() {
1567 Mutex::Autolock lock(mFrameEventHistoryMutex);
1568 mFrameEventHistory.onDisconnect();
Yiwei Zhangaf8ee942018-11-22 00:15:23 -08001569 mFlinger->mTimeStats->onDestroy(getSequence());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001570}
1571
Brian Anderson3890c392016-07-25 12:48:08 -07001572void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001573 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001574 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001575 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1576 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001577 }
1578
Brian Andersond6927fb2016-07-23 23:37:30 -07001579 Mutex::Autolock lock(mFrameEventHistoryMutex);
1580 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001581 // If there are any unsignaled fences in the aquire timeline at this
1582 // point, the previously queued frame hasn't been latched yet. Go ahead
1583 // and try to get the signal time here so the syscall is taken out of
1584 // the main thread's critical path.
1585 mAcquireTimeline.updateSignalTimes();
1586 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001587 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001588 mFrameEventHistory.addQueue(*newTimestamps);
1589 }
1590
Brian Anderson3890c392016-07-25 12:48:08 -07001591 if (outDelta) {
1592 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001593 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001594}
Dan Stozae77c7662016-05-13 11:37:28 -07001595
Chia-I Wu98f1c102017-05-30 14:54:08 -07001596size_t Layer::getChildrenCount() const {
1597 size_t count = 0;
1598 for (const sp<Layer>& child : mCurrentChildren) {
1599 count += 1 + child->getChildrenCount();
1600 }
1601 return count;
1602}
1603
Robert Carr1f0a16a2016-10-24 16:27:39 -07001604void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001605 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001606 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001607
Robert Carr1f0a16a2016-10-24 16:27:39 -07001608 mCurrentChildren.add(layer);
1609 layer->setParent(this);
1610}
1611
1612ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001613 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001614 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001615
Robert Carr1323c952019-01-28 18:13:27 -08001616 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001617 return mCurrentChildren.remove(layer);
1618}
1619
Robert Carr1db73f62016-12-21 12:58:51 -08001620bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1621 sp<Handle> handle = nullptr;
1622 sp<Layer> newParent = nullptr;
1623 if (newParentHandle == nullptr) {
1624 return false;
1625 }
1626 handle = static_cast<Handle*>(newParentHandle.get());
1627 newParent = handle->owner.promote();
1628 if (newParent == nullptr) {
1629 ALOGE("Unable to promote Layer handle");
1630 return false;
1631 }
1632
chaviw5aedec92018-10-22 10:40:38 -07001633 if (attachChildren()) {
1634 setTransactionFlags(eTransactionNeeded);
1635 }
Robert Carr1db73f62016-12-21 12:58:51 -08001636 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001637 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001638 }
1639 mCurrentChildren.clear();
1640
1641 return true;
1642}
1643
Robert Carr15eae092018-03-23 13:43:53 -07001644void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001645 for (const sp<Layer>& child : mDrawingChildren) {
1646 child->mDrawingParent = newParent;
Vishnu Nair4351ad52019-02-11 14:13:02 -08001647 child->computeBounds(newParent->mBounds, newParent->getTransformWithScale());
Robert Carr578038f2018-03-09 12:25:24 -08001648 }
1649}
1650
chaviwf1961f72017-09-18 16:41:07 -07001651bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001652 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001653
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001654 // While layers are detached, we allow most operations
1655 // and simply halt performing the actual transaction. However
1656 // for reparent != null we would enter the mRemovedFromCurrentState
1657 // state, regardless of whether doTransaction was called, and
1658 // so we need to prevent the update here.
1659 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001660 return false;
1661 }
1662
Robert Carr54cf5b12019-01-25 14:02:28 -08001663 sp<Layer> newParent;
1664 if (newParentHandle != nullptr) {
1665 auto handle = static_cast<Handle*>(newParentHandle.get());
1666 newParent = handle->owner.promote();
1667 if (newParent == nullptr) {
1668 ALOGE("Unable to promote Layer handle");
1669 return false;
1670 }
1671 if (newParent == this) {
1672 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1673 return false;
1674 }
1675 }
1676
chaviwf1961f72017-09-18 16:41:07 -07001677 sp<Layer> parent = getParent();
1678 if (parent != nullptr) {
1679 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001680 }
1681
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001682 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001683 newParent->addChild(this);
1684 if (!newParent->isRemovedFromCurrentState()) {
1685 addToCurrentState();
1686 } else {
1687 onRemovedFromCurrentState();
1688 }
1689
1690 if (mLayerDetached) {
1691 mLayerDetached = false;
1692 callSetTransactionFlags = true;
1693 }
1694 } else {
1695 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001696 }
1697
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001698 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001699 setTransactionFlags(eTransactionNeeded);
1700 }
chaviw06178942017-07-27 10:25:59 -07001701 return true;
1702}
1703
Robert Carr9524cb32017-02-13 11:32:32 -08001704bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001705 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001706 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001707 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001708 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001709 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001710 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001711 }
Robert Carr7f619b22017-11-06 12:56:35 -08001712 }
Robert Carr9524cb32017-02-13 11:32:32 -08001713
1714 return true;
1715}
1716
chaviw5aedec92018-10-22 10:40:38 -07001717bool Layer::attachChildren() {
1718 bool changed = false;
1719 for (const sp<Layer>& child : mCurrentChildren) {
1720 sp<Client> parentClient = mClientRef.promote();
1721 sp<Client> client(child->mClientRef.promote());
1722 if (client != nullptr && parentClient != client) {
1723 if (child->mLayerDetached) {
1724 child->mLayerDetached = false;
1725 changed = true;
1726 }
1727 changed |= child->attachChildren();
1728 }
1729 }
1730
1731 return changed;
1732}
1733
Peiyong Lind3788632018-09-18 16:01:31 -07001734bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001735 static const mat4 identityMatrix = mat4();
1736
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001737 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001738 return false;
1739 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001740 ++mCurrentState.sequence;
1741 mCurrentState.colorTransform = matrix;
1742 mCurrentState.hasColorTransform = matrix != identityMatrix;
1743 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001744 setTransactionFlags(eTransactionNeeded);
1745 return true;
1746}
1747
chaviwf66724d2018-11-28 16:35:21 -08001748mat4 Layer::getColorTransform() const {
1749 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1750 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1751 colorTransform = parent->getColorTransform() * colorTransform;
1752 }
1753 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001754}
1755
1756bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001757 bool hasColorTransform = getDrawingState().hasColorTransform;
1758 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1759 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1760 }
1761 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001762}
1763
Chia-I Wu11481472018-05-04 10:43:19 -07001764bool Layer::isLegacyDataSpace() const {
1765 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001766 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001767 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001768}
1769
Robert Carr1f0a16a2016-10-24 16:27:39 -07001770void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001771 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001772}
1773
1774void Layer::clearSyncPoints() {
1775 for (const auto& child : mCurrentChildren) {
1776 child->clearSyncPoints();
1777 }
1778
1779 Mutex::Autolock lock(mLocalSyncPointMutex);
1780 for (auto& point : mLocalSyncPoints) {
1781 point->setFrameAvailable();
1782 }
1783 mLocalSyncPoints.clear();
1784}
1785
1786int32_t Layer::getZ() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001787 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001788}
1789
Robert Carr29abff82017-12-04 13:51:20 -08001790bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1791 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001792 const State& state = useDrawing ? mDrawingState : mCurrentState;
Robert Carr29abff82017-12-04 13:51:20 -08001793 return state.zOrderRelativeOf != nullptr;
1794}
1795
David Sodman41fdfc92017-11-06 16:09:56 -08001796__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001797 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001798 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1799 "makeTraversalList received invalid stateSet");
1800 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1801 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001802 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001803
Robert Carr29abff82017-12-04 13:51:20 -08001804 if (state.zOrderRelatives.size() == 0) {
1805 *outSkipRelativeZUsers = true;
1806 return children;
1807 }
1808
chaviwfd462612018-05-31 16:11:27 -07001809 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001810 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001811 sp<Layer> strongRelative = weakRelative.promote();
1812 if (strongRelative != nullptr) {
1813 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001814 }
1815 }
1816
Dan Stoza412903f2017-04-27 13:42:17 -07001817 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001818 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
Robert Carr503c7042017-09-27 15:06:08 -07001819 if (childState.zOrderRelativeOf != nullptr) {
1820 continue;
1821 }
Robert Carrdb66e622017-04-10 16:55:57 -07001822 traverse.add(child);
1823 }
1824
1825 return traverse;
1826}
1827
Robert Carr1f0a16a2016-10-24 16:27:39 -07001828/**
Robert Carrdb66e622017-04-10 16:55:57 -07001829 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001830 */
Dan Stoza412903f2017-04-27 13:42:17 -07001831void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001832 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1833 // produce a new list for traversing, including our relatives, and not including our children
1834 // who are relatives of another surface. In the case that there are no relative Z,
1835 // makeTraversalList returns our children directly to avoid significant overhead.
1836 // However in this case we need to take the responsibility for filtering children which
1837 // are relatives of another surface here.
1838 bool skipRelativeZUsers = false;
1839 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001840
Robert Carr1f0a16a2016-10-24 16:27:39 -07001841 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001842 for (; i < list.size(); i++) {
1843 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001844 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1845 continue;
1846 }
1847
Robert Carrdb66e622017-04-10 16:55:57 -07001848 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001849 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001850 }
Dan Stoza412903f2017-04-27 13:42:17 -07001851 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001852 }
Robert Carr29abff82017-12-04 13:51:20 -08001853
Dan Stoza412903f2017-04-27 13:42:17 -07001854 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001855 for (; i < list.size(); i++) {
1856 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001857
Robert Carr29abff82017-12-04 13:51:20 -08001858 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1859 continue;
1860 }
Dan Stoza412903f2017-04-27 13:42:17 -07001861 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001862 }
1863}
1864
1865/**
Robert Carrdb66e622017-04-10 16:55:57 -07001866 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001867 */
Dan Stoza412903f2017-04-27 13:42:17 -07001868void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1869 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001870 // See traverseInZOrder for documentation.
1871 bool skipRelativeZUsers = false;
1872 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001873
Robert Carr1f0a16a2016-10-24 16:27:39 -07001874 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001875 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001876 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001877
1878 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1879 continue;
1880 }
1881
Robert Carrdb66e622017-04-10 16:55:57 -07001882 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001883 break;
1884 }
Dan Stoza412903f2017-04-27 13:42:17 -07001885 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001886 }
Dan Stoza412903f2017-04-27 13:42:17 -07001887 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001888 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001889 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001890
1891 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1892 continue;
1893 }
1894
Dan Stoza412903f2017-04-27 13:42:17 -07001895 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001896 }
1897}
1898
chaviw4b129c22018-04-09 16:19:43 -07001899LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1900 const std::vector<Layer*>& layersInTree) {
1901 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1902 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001903 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1904 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001905 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001906
chaviwfd462612018-05-31 16:11:27 -07001907 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001908 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1909 sp<Layer> strongRelative = weakRelative.promote();
1910 // Only add relative layers that are also descendents of the top most parent of the tree.
1911 // If a relative layer is not a descendent, then it should be ignored.
1912 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1913 traverse.add(strongRelative);
1914 }
1915 }
1916
1917 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001918 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001919 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1920 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1921 // the child here since it won't be added later as a relative.
1922 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1923 childState.zOrderRelativeOf.promote().get())) {
1924 continue;
1925 }
1926 traverse.add(child);
1927 }
1928
1929 return traverse;
1930}
1931
1932void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1933 LayerVector::StateSet stateSet,
1934 const LayerVector::Visitor& visitor) {
1935 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001936
1937 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001938 for (; i < list.size(); i++) {
1939 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001940 if (relative->getZ() >= 0) {
1941 break;
1942 }
chaviw4b129c22018-04-09 16:19:43 -07001943 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001944 }
chaviw4b129c22018-04-09 16:19:43 -07001945
chaviwa76b2712017-09-20 12:02:26 -07001946 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001947 for (; i < list.size(); i++) {
1948 const auto& relative = list[i];
1949 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001950 }
1951}
1952
chaviw4b129c22018-04-09 16:19:43 -07001953std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1954 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1955 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1956
1957 std::vector<Layer*> layersInTree = {this};
1958 for (size_t i = 0; i < children.size(); i++) {
1959 const auto& child = children[i];
1960 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1961 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1962 }
1963
1964 return layersInTree;
1965}
1966
1967void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1968 const LayerVector::Visitor& visitor) {
1969 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1970 std::sort(layersInTree.begin(), layersInTree.end());
1971 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1972}
1973
Peiyong Linefefaac2018-08-17 12:27:51 -07001974ui::Transform Layer::getTransform() const {
Vishnu Nairf0c28512019-02-08 12:40:28 -08001975 return mEffectiveTransform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001976}
1977
chaviw13fdc492017-06-27 12:40:18 -07001978half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001979 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001980
chaviw13fdc492017-06-27 12:40:18 -07001981 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1982 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001983}
Robert Carr6452f122017-03-21 10:41:29 -07001984
chaviw13fdc492017-06-27 12:40:18 -07001985half4 Layer::getColor() const {
1986 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001987 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001988}
Robert Carr6452f122017-03-21 10:41:29 -07001989
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001990Layer::RoundedCornerState Layer::getRoundedCornerState() const {
1991 const auto& p = mDrawingParent.promote();
1992 if (p != nullptr) {
1993 RoundedCornerState parentState = p->getRoundedCornerState();
1994 if (parentState.radius > 0) {
1995 ui::Transform t = getActiveTransform(getDrawingState());
1996 t = t.inverse();
1997 parentState.cropRect = t.transform(parentState.cropRect);
1998 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1999 // but a transform matrix can define horizontal and vertical scales.
2000 // Let's take the average between both of them and pass into the shader, practically we
2001 // never do this type of transformation on windows anyway.
2002 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
2003 return parentState;
2004 }
2005 }
2006 const float radius = getDrawingState().cornerRadius;
Vishnu Nair4351ad52019-02-11 14:13:02 -08002007 return radius > 0 ? RoundedCornerState(getBounds(), radius) : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002008}
2009
Robert Carr1f0a16a2016-10-24 16:27:39 -07002010void Layer::commitChildList() {
2011 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
2012 const auto& child = mCurrentChildren[i];
2013 child->commitChildList();
2014 }
2015 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07002016 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002017}
2018
Robert Carr720e5062018-07-30 17:45:14 -07002019void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002020 mCurrentState.inputInfo = info;
2021 mCurrentState.modified = true;
2022 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07002023 setTransactionFlags(eTransactionNeeded);
2024}
2025
chaviw1d044282017-09-27 12:19:28 -07002026void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
2027 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2028 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002029 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07002030
Peiyong Linefefaac2018-08-17 12:27:51 -07002031 ui::Transform requestedTransform = state.active_legacy.transform;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002032 ui::Transform transform = getTransform();
chaviw1d044282017-09-27 12:19:28 -07002033
2034 layerInfo->set_id(sequence);
2035 layerInfo->set_name(getName().c_str());
2036 layerInfo->set_type(String8(getTypeId()));
2037
2038 for (const auto& child : children) {
2039 layerInfo->add_children(child->sequence);
2040 }
2041
2042 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
2043 sp<Layer> strongRelative = weakRelative.promote();
2044 if (strongRelative != nullptr) {
2045 layerInfo->add_relatives(strongRelative->sequence);
2046 }
2047 }
2048
Marissa Wallf58c14b2018-07-24 10:50:43 -07002049 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
Nataniel Borges797b0e42019-02-15 14:11:58 -08002050 [&]() { return layerInfo->mutable_transparent_region(); });
2051 LayerProtoHelper::writeToProto(visibleRegion,
2052 [&]() { return layerInfo->mutable_visible_region(); });
2053 LayerProtoHelper::writeToProto(surfaceDamageRegion,
2054 [&]() { return layerInfo->mutable_damage_region(); });
chaviw1d044282017-09-27 12:19:28 -07002055
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002056 layerInfo->set_layer_stack(getLayerStack());
chaviw1d044282017-09-27 12:19:28 -07002057 layerInfo->set_z(state.z);
2058
Nataniel Borges797b0e42019-02-15 14:11:58 -08002059 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
2060 [&]() { return layerInfo->mutable_position(); });
chaviw1d044282017-09-27 12:19:28 -07002061
Nataniel Borges797b0e42019-02-15 14:11:58 -08002062 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(), [&]() {
2063 return layerInfo->mutable_requested_position();
2064 });
chaviw1d044282017-09-27 12:19:28 -07002065
Nataniel Borges797b0e42019-02-15 14:11:58 -08002066 LayerProtoHelper::writeSizeToProto(state.active_legacy.w, state.active_legacy.h,
2067 [&]() { return layerInfo->mutable_size(); });
chaviw1d044282017-09-27 12:19:28 -07002068
Nataniel Borges797b0e42019-02-15 14:11:58 -08002069 LayerProtoHelper::writeToProto(state.crop_legacy, [&]() { return layerInfo->mutable_crop(); });
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002070 layerInfo->set_corner_radius(getRoundedCornerState().radius);
chaviw1d044282017-09-27 12:19:28 -07002071
2072 layerInfo->set_is_opaque(isOpaque(state));
2073 layerInfo->set_invalidate(contentDirty);
Chia-I Wu01591c92018-05-22 12:03:00 -07002074
2075 // XXX (b/79210409) mCurrentDataSpace is not protected
2076 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
2077
chaviw1d044282017-09-27 12:19:28 -07002078 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
Nataniel Borges797b0e42019-02-15 14:11:58 -08002079 LayerProtoHelper::writeToProto(getColor(), [&]() { return layerInfo->mutable_color(); });
2080 LayerProtoHelper::writeToProto(state.color,
2081 [&]() { return layerInfo->mutable_requested_color(); });
chaviw1d044282017-09-27 12:19:28 -07002082 layerInfo->set_flags(state.flags);
2083
2084 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
2085 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
2086
Jorim Jaggi8e0af362017-11-14 16:28:28 +01002087 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07002088 if (parent != nullptr) {
2089 layerInfo->set_parent(parent->sequence);
Nataniel Borgesdcc0bab2019-02-15 13:22:03 -08002090 } else {
2091 layerInfo->set_parent(-1);
chaviw1d044282017-09-27 12:19:28 -07002092 }
2093
2094 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2095 if (zOrderRelativeOf != nullptr) {
2096 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
Nataniel Borgesdcc0bab2019-02-15 13:22:03 -08002097 } else {
2098 layerInfo->set_z_order_relative_of(-1);
chaviw1d044282017-09-27 12:19:28 -07002099 }
2100
Lloyd Pique0b785d82018-12-04 17:25:27 -08002101 auto buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08002102 if (buffer != nullptr) {
Nataniel Borges797b0e42019-02-15 14:11:58 -08002103 LayerProtoHelper::writeToProto(buffer,
2104 [&]() { return layerInfo->mutable_active_buffer(); });
Peiyong Linefefaac2018-08-17 12:27:51 -07002105 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
Yichi Chen6ca35192018-05-29 12:20:43 +08002106 layerInfo->mutable_buffer_transform());
chaviw1d044282017-09-27 12:19:28 -07002107 }
2108
2109 layerInfo->set_queued_frames(getQueuedFrameCount());
2110 layerInfo->set_refresh_pending(isBufferLatched());
chaviwadc40c22018-07-10 16:57:27 -07002111 layerInfo->set_curr_frame(mCurrentFrameNumber);
Vishnu Nairf2deb822018-11-12 17:53:48 -08002112 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
chaviwadc40c22018-07-10 16:57:27 -07002113
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002114 for (const auto& pendingState : mPendingStates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07002115 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
chaviwadc40c22018-07-10 16:57:27 -07002116 if (barrierLayer != nullptr) {
2117 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
2118 barrierLayerProto->set_id(barrierLayer->sequence);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002119 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
chaviwadc40c22018-07-10 16:57:27 -07002120 }
2121 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002122
2123 auto protoMap = layerInfo->mutable_metadata();
2124 for (const auto& entry : state.metadata.mMap) {
2125 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
2126 }
Vishnu Nair4351ad52019-02-11 14:13:02 -08002127 LayerProtoHelper::writeToProto(mEffectiveTransform, layerInfo->mutable_effective_transform());
Nataniel Borges797b0e42019-02-15 14:11:58 -08002128 LayerProtoHelper::writeToProto(mSourceBounds,
2129 [&]() { return layerInfo->mutable_source_bounds(); });
2130 LayerProtoHelper::writeToProto(mScreenBounds,
2131 [&]() { return layerInfo->mutable_screen_bounds(); });
2132 LayerProtoHelper::writeToProto(mBounds, [&]() { return layerInfo->mutable_bounds(); });
chaviw1d044282017-09-27 12:19:28 -07002133}
2134
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002135void Layer::writeToProto(LayerProto* layerInfo, const sp<DisplayDevice>& displayDevice) {
2136 auto outputLayer = findOutputLayerForDisplay(displayDevice);
2137 if (!outputLayer) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07002138 return;
2139 }
2140
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002141 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
2142
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002143 const auto& compositionState = outputLayer->getState();
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002144
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002145 const Rect& frame = compositionState.displayFrame;
Nataniel Borges797b0e42019-02-15 14:11:58 -08002146 LayerProtoHelper::writeToProto(frame, [&]() { return layerInfo->mutable_hwc_frame(); });
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002147
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002148 const FloatRect& crop = compositionState.sourceCrop;
Nataniel Borges797b0e42019-02-15 14:11:58 -08002149 LayerProtoHelper::writeToProto(crop, [&]() { return layerInfo->mutable_hwc_crop(); });
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002150
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002151 const int32_t transform =
2152 getCompositionLayer() ? static_cast<int32_t>(compositionState.bufferTransform) : 0;
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002153 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002154
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002155 const int32_t compositionType =
2156 static_cast<int32_t>(compositionState.hwc ? (*compositionState.hwc).hwcCompositionType
2157 : Hwc2::IComposerClient::Composition::CLIENT);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002158 layerInfo->set_hwc_composition_type(compositionType);
2159
2160 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
2161 static_cast<BufferLayer*>(this)->isProtected()) {
2162 layerInfo->set_is_protected(true);
2163 } else {
2164 layerInfo->set_is_protected(false);
2165 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002166}
2167
Robert Carr2e102c92018-10-23 12:11:15 -07002168bool Layer::isRemovedFromCurrentState() const {
2169 return mRemovedFromCurrentState;
2170}
2171
Arthur Hungd20b2702019-01-14 18:16:16 +08002172InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002173 InputWindowInfo info = mDrawingState.inputInfo;
Robert Carr720e5062018-07-30 17:45:14 -07002174
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002175 if (info.displayId == ADISPLAY_ID_NONE) {
2176 info.displayId = mDrawingState.layerStack;
2177 }
2178
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002179 ui::Transform t = getTransform();
2180 const float xScale = t.sx();
2181 const float yScale = t.sy();
2182 if (xScale != 1.0f || yScale != 1.0f) {
2183 info.windowXScale *= 1.0f / xScale;
2184 info.windowYScale *= 1.0f / yScale;
2185 info.touchableRegion.scaleSelf(xScale, yScale);
Riddle Hsu39d4aa52018-11-30 20:46:53 +08002186 }
Robert Carre07e1032018-11-26 12:55:53 -08002187
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002188 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002189 // If this is a portal window, set the touchableRegion to the layerBounds.
2190 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
2191 ? getBufferSize(getDrawingState())
2192 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002193 if (!layerBounds.isValid()) {
2194 layerBounds = getCroppedBufferSize(getDrawingState());
2195 }
Vishnu Nair8033a492018-12-05 07:27:23 -08002196 layerBounds = t.transform(layerBounds);
2197 layerBounds.inset(info.surfaceInset, info.surfaceInset, info.surfaceInset, info.surfaceInset);
2198
Arthur Hungd20b2702019-01-14 18:16:16 +08002199 // Input coordinate should match the layer bounds.
2200 info.frameLeft = layerBounds.left;
2201 info.frameTop = layerBounds.top;
2202 info.frameRight = layerBounds.right;
2203 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08002204
2205 // Position the touchable region relative to frame screen location and restrict it to frame
2206 // bounds.
2207 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08002208 info.visible = canReceiveInput();
Robert Carr720e5062018-07-30 17:45:14 -07002209 return info;
2210}
2211
2212bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002213 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002214}
2215
Lloyd Piquefeb73d72018-12-04 17:23:44 -08002216std::shared_ptr<compositionengine::Layer> Layer::getCompositionLayer() const {
2217 return nullptr;
2218}
2219
chaviw3e727cd2019-01-31 13:41:05 -08002220bool Layer::canReceiveInput() const {
2221 return isVisible();
2222}
2223
Lloyd Pique37c2c9b2018-12-04 17:25:10 -08002224compositionengine::OutputLayer* Layer::findOutputLayerForDisplay(
2225 const sp<const DisplayDevice>& display) const {
2226 return display->getCompositionDisplay()->getOutputLayerForLayer(getCompositionLayer().get());
2227}
2228
Mathias Agopian13127d82013-03-05 17:47:11 -08002229// ---------------------------------------------------------------------------
2230
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002231}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002232
2233#if defined(__gl_h_)
2234#error "don't include gl/gl.h in this file"
2235#endif
2236
2237#if defined(__gl2_h_)
2238#error "don't include gl2/gl2.h in this file"
2239#endif