blob: 8ff26e13107f335528330568fe16b763454f2017 [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>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027
Yiwei Zhang5434a782018-12-05 18:06:32 -080028#include <android-base/stringprintf.h>
29
Mathias Agopiana67932f2011-04-20 14:20:59 -070030#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070031#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070032#include <cutils/properties.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033
34#include <utils/Errors.h>
35#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080036#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080038#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039
Courtney Goeltzenleuchter36c44dc2017-04-14 09:33:16 -060040#include <ui/DebugUtils.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070041#include <ui/GraphicBuffer.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042#include <ui/PixelFormat.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080043
Dan Stoza6b9454d2014-11-07 16:00:59 -080044#include <gui/BufferItem.h>
Kalle Raitaa099a242017-01-11 11:17:29 -080045#include <gui/LayerDebugInfo.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080046#include <gui/Surface.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047
Yiwei Zhang60d1a192018-03-07 14:52:28 -080048#include "BufferLayer.h"
Valerie Haudd0b7572019-01-29 14:59:27 -080049#include "ColorLayer.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020050#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070051#include "DisplayDevice.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052#include "Layer.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070053#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070054#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056
Mathias Agopian1b031492012-06-20 17:51:20 -070057#include "DisplayHardware/HWComposer.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080058#include "TimeStats/TimeStats.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070059
Peiyong Lincbc184f2018-08-22 13:24:10 -070060#include <renderengine/RenderEngine.h>
Mathias Agopian875d8e12013-06-07 15:35:48 -070061
Dan Stozac5da2712016-07-20 15:38:12 -070062#include <mutex>
chaviw1d044282017-09-27 12:19:28 -070063#include "LayerProtoHelper.h"
Dan Stozac5da2712016-07-20 15:38:12 -070064
David Sodman41fdfc92017-11-06 16:09:56 -080065#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067namespace android {
68
Yiwei Zhang5434a782018-12-05 18:06:32 -080069using base::StringAppendF;
70
Lloyd Piquef1c675b2018-09-12 20:45:39 -070071std::atomic<int32_t> Layer::sSequence{1};
Mathias Agopian13127d82013-03-05 17:47:11 -080072
Lloyd Pique42ab75e2018-09-12 20:46:03 -070073Layer::Layer(const LayerCreationArgs& args)
74 : mFlinger(args.flinger),
75 mName(args.name),
76 mClientRef(args.client),
77 mBE{this, args.name.string()} {
Mathias Agopiana67932f2011-04-20 14:20:59 -070078 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070079
80 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -070081 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
82 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
83 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070084
Dan Stozaf7ba41a2017-05-10 15:11:11 -070085 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070086
Lloyd Pique0449b0f2018-12-20 16:23:45 -080087 mCurrentState.active_legacy.w = args.w;
88 mCurrentState.active_legacy.h = args.h;
89 mCurrentState.flags = layerFlags;
90 mCurrentState.active_legacy.transform.set(0, 0);
91 mCurrentState.crop_legacy.makeInvalid();
92 mCurrentState.requestedCrop_legacy = mCurrentState.crop_legacy;
93 mCurrentState.z = 0;
94 mCurrentState.color.a = 1.0f;
95 mCurrentState.layerStack = 0;
96 mCurrentState.sequence = 0;
97 mCurrentState.requested_legacy = mCurrentState.active_legacy;
Lloyd Pique0449b0f2018-12-20 16:23:45 -080098 mCurrentState.active.w = UINT32_MAX;
99 mCurrentState.active.h = UINT32_MAX;
100 mCurrentState.active.transform.set(0, 0);
101 mCurrentState.transform = 0;
102 mCurrentState.transformToDisplayInverse = false;
103 mCurrentState.crop.makeInvalid();
104 mCurrentState.acquireFence = new Fence(-1);
105 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
106 mCurrentState.hdrMetadata.validTypes = 0;
107 mCurrentState.surfaceDamageRegion.clear();
108 mCurrentState.cornerRadius = 0.0f;
109 mCurrentState.api = -1;
110 mCurrentState.hasColorTransform = false;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700111
112 // drawing state & current state are identical
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800113 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700114
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800115 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700116 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800117 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200118 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700119
120 mFlinger->onLayerCreated();
Dan Stoza436ccf32018-06-21 12:10:12 -0700121}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700122
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700123Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800124 sp<Client> c(mClientRef.promote());
125 if (c != 0) {
126 c->detachLayer(this);
127 }
128
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000129 mFrameTracker.logAndResetStats(mName);
Robert Carr2e102c92018-10-23 12:11:15 -0700130
chaviw61626f22018-11-15 16:26:27 -0800131 destroyAllHwcLayersPlusChildren();
Robert Carr2e102c92018-10-23 12:11:15 -0700132
133 mFlinger->onLayerDestroyed();
Mathias Agopian96f08192010-06-02 23:28:45 -0700134}
135
Mathias Agopian13127d82013-03-05 17:47:11 -0800136// ---------------------------------------------------------------------------
137// callbacks
138// ---------------------------------------------------------------------------
139
David Sodmaneb085e02017-10-05 18:49:04 -0700140/*
141 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
142 * Layer. So, the implementation is done in BufferLayer. When called on a
143 * ColorLayer object, it's essentially a NOP.
144 */
David Sodmaneb085e02017-10-05 18:49:04 -0700145void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800146
Chia-I Wuc6657022017-08-15 11:18:17 -0700147void Layer::onRemovedFromCurrentState() {
Robert Carr2e102c92018-10-23 12:11:15 -0700148 mRemovedFromCurrentState = true;
149
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800150 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
151 if (mCurrentState.zOrderRelativeOf != nullptr) {
152 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
153 if (strongRelative != nullptr) {
154 strongRelative->removeZOrderRelative(this);
155 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700156 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800157 mCurrentState.zOrderRelativeOf = nullptr;
Robert Carr5edb1ad2017-04-25 10:54:24 -0700158 }
Rob Carr4bba3702018-10-08 21:53:30 +0000159
Robert Carr2e102c92018-10-23 12:11:15 -0700160 // Since we are no longer reachable from CurrentState SurfaceFlinger
161 // will no longer invoke doTransaction for us, and so we will
162 // never finish applying transactions. We signal the sync point
163 // now so that another layer will not become indefinitely
164 // blocked.
165 for (auto& point: mRemoteSyncPoints) {
166 point->setTransactionApplied();
167 }
168 mRemoteSyncPoints.clear();
169
170 {
171 Mutex::Autolock syncLock(mLocalSyncPointMutex);
172 for (auto& point : mLocalSyncPoints) {
173 point->setFrameAvailable();
174 }
175 mLocalSyncPoints.clear();
176 }
177
Chia-I Wuc6657022017-08-15 11:18:17 -0700178 for (const auto& child : mCurrentChildren) {
179 child->onRemovedFromCurrentState();
180 }
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800181
182 mFlinger->markLayerPendingRemovalLocked(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700183}
Chia-I Wu38512252017-05-17 14:36:16 -0700184
chaviw61626f22018-11-15 16:26:27 -0800185void Layer::addToCurrentState() {
186 mRemovedFromCurrentState = false;
187
188 for (const auto& child : mCurrentChildren) {
189 child->addToCurrentState();
190 }
191}
192
Mathias Agopian13127d82013-03-05 17:47:11 -0800193// ---------------------------------------------------------------------------
194// set-up
195// ---------------------------------------------------------------------------
196
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700197const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800198 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800199}
200
chaviw13fdc492017-06-27 12:40:18 -0700201bool Layer::getPremultipledAlpha() const {
202 return mPremultipliedAlpha;
203}
204
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700205sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800206 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700207 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800208}
209
210// ---------------------------------------------------------------------------
211// h/w composer set-up
212// ---------------------------------------------------------------------------
213
Dominik Laskowski075d3172018-05-24 15:50:06 -0700214bool Layer::createHwcLayer(HWComposer* hwc, DisplayId displayId) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700215 LOG_ALWAYS_FATAL_IF(hasHwcLayer(displayId), "Already have a layer for display %s",
216 to_string(displayId).c_str());
David Sodmanb8aaea12017-12-14 15:54:51 -0800217 auto layer = std::shared_ptr<HWC2::Layer>(
218 hwc->createLayer(displayId),
219 [hwc, displayId](HWC2::Layer* layer) {
220 hwc->destroyLayer(displayId, layer); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700221 if (!layer) {
222 return false;
223 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700224 LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers[displayId];
Steven Thomasb02664d2017-07-26 18:48:28 -0700225 hwcInfo.hwc = hwc;
226 hwcInfo.layer = layer;
David Sodmanf6a38932018-05-25 15:27:50 -0700227 layer->setLayerDestroyedListener(
Dominik Laskowski7e045462018-05-30 13:02:02 -0700228 [this, displayId](HWC2::Layer* /*layer*/) { getBE().mHwcLayers.erase(displayId); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700229 return true;
230}
231
Dominik Laskowski075d3172018-05-24 15:50:06 -0700232bool Layer::destroyHwcLayer(DisplayId displayId) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700233 if (!hasHwcLayer(displayId)) {
Chia-I Wu83806892017-11-16 10:50:20 -0800234 return false;
Steven Thomasb02664d2017-07-26 18:48:28 -0700235 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700236 auto& hwcInfo = getBE().mHwcLayers[displayId];
David Sodman41fdfc92017-11-06 16:09:56 -0800237 LOG_ALWAYS_FATAL_IF(hwcInfo.layer == nullptr, "Attempt to destroy null layer");
Steven Thomasb02664d2017-07-26 18:48:28 -0700238 LOG_ALWAYS_FATAL_IF(hwcInfo.hwc == nullptr, "Missing HWComposer");
David Sodmanb8aaea12017-12-14 15:54:51 -0800239 hwcInfo.layer = nullptr;
240
Chia-I Wu83806892017-11-16 10:50:20 -0800241 return true;
Steven Thomasb02664d2017-07-26 18:48:28 -0700242}
243
chaviw61626f22018-11-15 16:26:27 -0800244void Layer::destroyHwcLayersForAllDisplays() {
David Sodman6f65f3e2017-11-03 14:28:09 -0700245 size_t numLayers = getBE().mHwcLayers.size();
Steven Thomasb02664d2017-07-26 18:48:28 -0700246 for (size_t i = 0; i < numLayers; ++i) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700247 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.empty(), "destroyAllHwcLayers failed");
248 destroyHwcLayer(getBE().mHwcLayers.begin()->first);
Steven Thomasb02664d2017-07-26 18:48:28 -0700249 }
chaviw61626f22018-11-15 16:26:27 -0800250}
251
252void Layer::destroyAllHwcLayersPlusChildren() {
253 destroyHwcLayersForAllDisplays();
David Sodman6f65f3e2017-11-03 14:28:09 -0700254 LOG_ALWAYS_FATAL_IF(!getBE().mHwcLayers.empty(),
David Sodman41fdfc92017-11-06 16:09:56 -0800255 "All hardware composer layers should have been destroyed");
Robert Carr2e102c92018-10-23 12:11:15 -0700256
257 for (const sp<Layer>& child : mDrawingChildren) {
chaviw61626f22018-11-15 16:26:27 -0800258 child->destroyAllHwcLayersPlusChildren();
Robert Carr2e102c92018-10-23 12:11:15 -0700259 }
Steven Thomasb02664d2017-07-26 18:48:28 -0700260}
Steven Thomasb02664d2017-07-26 18:48:28 -0700261
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800262Rect Layer::getContentCrop() const {
263 // this is the crop rectangle that applies to the buffer
264 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700265 Rect crop;
266 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800267 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700268 crop = mCurrentCrop;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800269 } else if (getBE().compositionInfo.mBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800270 // otherwise we use the whole buffer
David Sodman0cc69182017-11-17 12:12:07 -0800271 crop = getBE().compositionInfo.mBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700272 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800273 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700274 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700275 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700276 return crop;
277}
278
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700279static Rect reduce(const Rect& win, const Region& exclude) {
280 if (CC_LIKELY(exclude.isEmpty())) {
281 return win;
282 }
283 if (exclude.isRect()) {
284 return win.reduce(exclude.getBounds());
285 }
286 return Region(win).subtract(exclude).getBounds();
287}
288
Dan Stoza80d61162017-12-20 15:57:52 -0800289static FloatRect reduce(const FloatRect& win, const Region& exclude) {
290 if (CC_LIKELY(exclude.isEmpty())) {
291 return win;
292 }
293 // Convert through Rect (by rounding) for lack of FloatRegion
294 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
295}
296
Vishnu Nair4351ad52019-02-11 14:13:02 -0800297Rect Layer::getScreenBounds(bool reduceTransparentRegion) const {
Vishnu Nair51625fa2018-12-06 13:54:33 -0800298 const State& s(getDrawingState());
299 Region transparentRegion = reduceTransparentRegion ? getActiveTransparentRegion(s) : Region();
Vishnu Nair4351ad52019-02-11 14:13:02 -0800300 FloatRect bounds = getBounds(transparentRegion);
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800301 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800302 // Transform to screen space.
303 bounds = t.transform(bounds);
304 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700305}
306
Vishnu Nair4351ad52019-02-11 14:13:02 -0800307FloatRect Layer::getBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000308 const State& s(getDrawingState());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800309 return getBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700310}
311
Vishnu Nair4351ad52019-02-11 14:13:02 -0800312ui::Transform Layer::getTransformWithScale() const {
313 // If the layer is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
314 // it isFixedSize) then there may be additional scaling not accounted
315 // for in the transform. We need to mirror this scaling to child surfaces
316 // or we will break the contract where WM can treat child surfaces as
317 // pixels in the parent surface.
318 if (!isFixedSize() || !getBE().compositionInfo.mBuffer) {
319 return mEffectiveTransform;
320 }
321
322 int bufferWidth;
323 int bufferHeight;
324 if ((mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
325 bufferWidth = getBE().compositionInfo.mBuffer->getWidth();
326 bufferHeight = getBE().compositionInfo.mBuffer->getHeight();
327 } else {
328 bufferHeight = getBE().compositionInfo.mBuffer->getWidth();
329 bufferWidth = getBE().compositionInfo.mBuffer->getHeight();
330 }
331 float sx = getActiveWidth(getDrawingState()) / static_cast<float>(bufferWidth);
332 float sy = getActiveHeight(getDrawingState()) / static_cast<float>(bufferHeight);
333 ui::Transform extraParentScaling;
334 extraParentScaling.set(sx, 0, 0, sy);
335 return mEffectiveTransform * extraParentScaling;
336}
337
338void Layer::computeBounds(FloatRect parentBounds, ui::Transform parentTransform) {
339 const State& s(getDrawingState());
340
341 // Calculate effective layer transform
342 mEffectiveTransform = parentTransform * getActiveTransform(s);
343
344 // Transform parent bounds to layer space
345 parentBounds = getActiveTransform(s).inverse().transform(parentBounds);
346
347 // Calculate display frame
348 mSourceBounds = computeSourceBounds(parentBounds);
349
350 // Calculate bounds by croping diplay frame with layer crop and parent bounds
351 FloatRect bounds = mSourceBounds;
352 const Rect layerCrop = getCrop(s);
353 if (!layerCrop.isEmpty()) {
354 bounds = mSourceBounds.intersect(layerCrop.toFloatRect());
355 }
356 bounds = bounds.intersect(parentBounds);
357
358 mBounds = bounds;
359 mScreenBounds = mEffectiveTransform.transform(mBounds);
360 for (const sp<Layer>& child : mDrawingChildren) {
361 child->computeBounds(mBounds, getTransformWithScale());
362 }
363}
364
365FloatRect Layer::getBounds(const Region& activeTransparentRegion) const {
Alec Mourib416efd2018-09-06 21:01:59 +0000366 const State& s(getDrawingState());
Vishnu Nair60356342018-11-13 13:00:45 -0800367 Rect bounds = getCroppedBufferSize(s);
368 FloatRect floatBounds = bounds.toFloatRect();
369 if (bounds.isValid()) {
370 // Layer has bounds. Pass in our bounds as a special case. Then pass on to our parents so
371 // that they can clip it.
372 floatBounds = cropChildBounds(floatBounds);
373 } else {
374 // Layer does not have bounds, so we fill to our parent bounds. This is done by getting our
375 // parent bounds and inverting the transform to get the maximum bounds we can have that
376 // will fit within our parent bounds.
377 const auto& p = mDrawingParent.promote();
378 if (p != nullptr) {
379 ui::Transform t = s.active_legacy.transform;
380 // When calculating the parent bounds for purposes of clipping, we don't need to
381 // constrain the parent to its transparent region. The transparent region is an
382 // optimization based on the buffer contents of the layer, but does not affect the
383 // space allocated to it by policy, and thus children should be allowed to extend into
384 // the parent's transparent region.
385 // One of the main uses is a parent window with a child sitting behind the parent
386 // window, marked by a transparent region. When computing the parent bounds from the
387 // parent's perspective we pass in the transparent region to reduce buffer allocation
388 // size. When computing the parent bounds from the child's perspective, we pass in an
389 // empty transparent region in order to extend into the the parent bounds.
Vishnu Nair4351ad52019-02-11 14:13:02 -0800390 floatBounds = p->getBounds(Region());
Vishnu Nair60356342018-11-13 13:00:45 -0800391 // Transform back to layer space.
392 floatBounds = t.inverse().transform(floatBounds);
393 }
394 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700395
Vishnu Nair60356342018-11-13 13:00:45 -0800396 // Subtract the transparent region and snap to the bounds.
397 return reduce(floatBounds, activeTransparentRegion);
398}
399
400FloatRect Layer::cropChildBounds(const FloatRect& childBounds) const {
401 const State& s(getDrawingState());
402 Rect bounds = getCroppedBufferSize(s);
403 FloatRect croppedBounds = childBounds;
404
405 // If the layer has bounds, then crop the passed in child bounds and pass
406 // it to our parents so they can crop it as well. If the layer has no bounds,
407 // then pass on the child bounds.
408 if (bounds.isValid()) {
409 croppedBounds = croppedBounds.intersect(bounds.toFloatRect());
Mathias Agopian13127d82013-03-05 17:47:11 -0800410 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700411
Chia-I Wue41dbe62017-06-13 14:10:56 -0700412 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700413 if (p != nullptr) {
Vishnu Nair60356342018-11-13 13:00:45 -0800414 // Transform to parent space and allow parent layer to crop the
415 // child bounds as well.
416 ui::Transform t = s.active_legacy.transform;
417 croppedBounds = t.transform(croppedBounds);
418 croppedBounds = p->cropChildBounds(croppedBounds);
419 croppedBounds = t.inverse().transform(croppedBounds);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700420 }
Vishnu Nair60356342018-11-13 13:00:45 -0800421 return croppedBounds;
422}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700423
Vishnu Nair60356342018-11-13 13:00:45 -0800424Rect Layer::getCroppedBufferSize(const State& s) const {
425 Rect size = getBufferSize(s);
426 Rect crop = getCrop(s);
427 if (!crop.isEmpty() && size.isValid()) {
428 size.intersect(crop, &size);
429 } else if (!crop.isEmpty()) {
430 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700431 }
Vishnu Nair60356342018-11-13 13:00:45 -0800432 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800433}
434
Alec Mouri1f3bd182019-01-24 15:20:18 +0000435Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& display) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700436 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800437 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700438 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800439
440 // apply the projection's clipping to the window crop in
441 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700442 // if there are no window scaling involved, this operation will map to full
443 // pixels in the buffer.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700444
Vishnu Nair4351ad52019-02-11 14:13:02 -0800445 FloatRect activeCropFloat = getBounds();
Alec Mouri1f3bd182019-01-24 15:20:18 +0000446 ui::Transform t = getTransform();
447 // Transform to screen space.
448 activeCropFloat = t.transform(activeCropFloat);
449 activeCropFloat = activeCropFloat.intersect(display->getViewport().toFloatRect());
450 // Back to layer space to work with the content crop.
451 activeCropFloat = t.inverse().transform(activeCropFloat);
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800452 // This needs to be here as transform.transform(Rect) computes the
453 // transformed rect and then takes the bounding box of the result before
454 // returning. This means
455 // transform.inverse().transform(transform.transform(Rect)) != Rect
456 // in which case we need to make sure the final rect is clipped to the
457 // display bounds.
458 Rect activeCrop{activeCropFloat};
459 if (!activeCrop.intersect(getBufferSize(s), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000460 activeCrop.clear();
461 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700462 return activeCrop;
463}
464
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700465void Layer::setupRoundedCornersCropCoordinates(Rect win,
466 const FloatRect& roundedCornersCrop) const {
467 // Translate win by the rounded corners rect coordinates, to have all values in
468 // layer coordinate space.
469 win.left -= roundedCornersCrop.left;
470 win.right -= roundedCornersCrop.left;
471 win.top -= roundedCornersCrop.top;
472 win.bottom -= roundedCornersCrop.top;
473
474 renderengine::Mesh::VertexArray<vec2> cropCoords(getBE().mMesh.getCropCoordArray<vec2>());
475 cropCoords[0] = vec2(win.left, win.top);
476 cropCoords[1] = vec2(win.left, win.top + win.getHeight());
477 cropCoords[2] = vec2(win.right, win.top + win.getHeight());
478 cropCoords[3] = vec2(win.right, win.top);
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700479}
480
Alec Mouri1f3bd182019-01-24 15:20:18 +0000481FloatRect Layer::computeCrop(const sp<const DisplayDevice>& display) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700482 // the content crop is the area of the content that gets scaled to the
483 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800484 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700485
486 // In addition there is a WM-specified crop we pull from our drawing state.
487 const State& s(getDrawingState());
488
Alec Mouri1f3bd182019-01-24 15:20:18 +0000489 Rect activeCrop = computeInitialCrop(display);
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800490 Rect bufferSize = getBufferSize(s);
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700491
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000492 // Transform the window crop to match the buffer coordinate system,
493 // which means using the inverse of the current transform set on the
494 // SurfaceFlingerConsumer.
495 uint32_t invTransform = mCurrentTransform;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800496 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000497 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700498 * the code below applies the primary display's inverse transform to the
499 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000500 */
David Sodman41fdfc92017-11-06 16:09:56 -0800501 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000502 // calculate the inverse transform
503 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800504 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800505 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000506 // and apply to the current transform
Peiyong Linefefaac2018-08-17 12:27:51 -0700507 invTransform = (ui::Transform(invTransformOrient) *
508 ui::Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800509 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000510
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800511 int winWidth = bufferSize.getWidth();
512 int winHeight = bufferSize.getHeight();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000513 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
514 // If the activeCrop has been rotate the ends are rotated but not
515 // the space itself so when transforming ends back we can't rely on
516 // a modification of the axes of rotation. To account for this we
517 // need to reorient the inverse rotation in terms of the current
518 // axes of rotation.
519 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
520 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
521 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800522 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000523 }
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800524 std::swap(winWidth, winHeight);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000525 }
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800526 const Rect winCrop =
527 activeCrop.transform(invTransform, bufferSize.getWidth(), bufferSize.getHeight());
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000528
529 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800530 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000531 float yScale = crop.getHeight() / float(winHeight);
532
David Sodman41fdfc92017-11-06 16:09:56 -0800533 float insetL = winCrop.left * xScale;
534 float insetT = winCrop.top * yScale;
535 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000536 float insetB = (winHeight - winCrop.bottom) * yScale;
537
David Sodman41fdfc92017-11-06 16:09:56 -0800538 crop.left += insetL;
539 crop.top += insetT;
540 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000541 crop.bottom -= insetB;
542
Mathias Agopian13127d82013-03-05 17:47:11 -0800543 return crop;
544}
545
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700546void Layer::setGeometry(const sp<const DisplayDevice>& display, uint32_t z) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700547 const auto displayId = display->getId();
Dominik Laskowski075d3172018-05-24 15:50:06 -0700548 LOG_ALWAYS_FATAL_IF(!displayId);
Dominik Laskowski34157762018-10-31 13:07:19 -0700549 RETURN_IF_NO_HWC_LAYER(*displayId);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700550 auto& hwcInfo = getBE().mHwcLayers[*displayId];
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700551
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700552 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800553 hwcInfo.forceClientComposition = false;
554
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800555 if (isSecure() && !display->isSecure()) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800556 hwcInfo.forceClientComposition = true;
557 }
558
David Sodman15094112018-10-11 09:39:37 -0700559 auto& hwcLayer = hwcInfo.layer;
560
Mathias Agopian13127d82013-03-05 17:47:11 -0800561 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700562 const State& s(getDrawingState());
Vishnu Naira6733b32018-12-06 18:13:16 -0800563 const Rect bufferSize = getBufferSize(s);
David Revemanecf0fa52017-03-03 11:32:44 -0500564 auto blendMode = HWC2::BlendMode::None;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800565 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800566 blendMode =
567 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800568 }
David Sodman15094112018-10-11 09:39:37 -0700569 auto error = hwcLayer->setBlendMode(blendMode);
570 ALOGE_IF(error != HWC2::Error::None,
571 "[%s] Failed to set blend mode %s:"
572 " %s (%d)",
573 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
574 static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700575 getBE().compositionInfo.hwc.blendMode = blendMode;
Mathias Agopian13127d82013-03-05 17:47:11 -0800576
577 // apply the layer's transform, followed by the display's global transform
578 // here we're guaranteed that the layer's transform preserves rects
Marissa Wall61c58622018-07-18 10:12:20 -0700579 Region activeTransparentRegion(getActiveTransparentRegion(s));
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800580 ui::Transform t = getTransform();
Marissa Wall61c58622018-07-18 10:12:20 -0700581 Rect activeCrop = getCrop(s);
Vishnu Naira6733b32018-12-06 18:13:16 -0800582 if (!activeCrop.isEmpty() && bufferSize.isValid()) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700583 activeCrop = t.transform(activeCrop);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700584 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000585 activeCrop.clear();
586 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700587 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800588 // This needs to be here as transform.transform(Rect) computes the
589 // transformed rect and then takes the bounding box of the result before
590 // returning. This means
591 // transform.inverse().transform(transform.transform(Rect)) != Rect
592 // in which case we need to make sure the final rect is clipped to the
593 // display bounds.
Vishnu Naira6733b32018-12-06 18:13:16 -0800594 if (!activeCrop.intersect(bufferSize, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000595 activeCrop.clear();
596 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700597 // mark regions outside the crop as transparent
Vishnu Naira6733b32018-12-06 18:13:16 -0800598 activeTransparentRegion.orSelf(Rect(0, 0, bufferSize.getWidth(), activeCrop.top));
599 activeTransparentRegion.orSelf(
600 Rect(0, activeCrop.bottom, bufferSize.getWidth(), bufferSize.getHeight()));
David Sodman41fdfc92017-11-06 16:09:56 -0800601 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
602 activeTransparentRegion.orSelf(
Vishnu Naira6733b32018-12-06 18:13:16 -0800603 Rect(activeCrop.right, activeCrop.top, bufferSize.getWidth(), activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700604 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700605
Vishnu Nair4351ad52019-02-11 14:13:02 -0800606 // getBounds returns a FloatRect to provide more accuracy during the
Dan Stoza80d61162017-12-20 15:57:52 -0800607 // transformation. We then round upon constructing 'frame'.
Vishnu Nair4351ad52019-02-11 14:13:02 -0800608 Rect frame{t.transform(getBounds(activeTransparentRegion))};
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700609 if (!frame.intersect(display->getViewport(), &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000610 frame.clear();
611 }
Peiyong Linefefaac2018-08-17 12:27:51 -0700612 const ui::Transform& tr = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800613 Rect transformedFrame = tr.transform(frame);
David Sodman15094112018-10-11 09:39:37 -0700614 error = hwcLayer->setDisplayFrame(transformedFrame);
615 if (error != HWC2::Error::None) {
616 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
617 transformedFrame.left, transformedFrame.top, transformedFrame.right,
618 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
619 } else {
620 hwcInfo.displayFrame = transformedFrame;
621 }
David Sodmanba340492018-08-05 21:51:33 -0700622 getBE().compositionInfo.hwc.displayFrame = transformedFrame;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800623
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700624 FloatRect sourceCrop = computeCrop(display);
David Sodman15094112018-10-11 09:39:37 -0700625 error = hwcLayer->setSourceCrop(sourceCrop);
626 if (error != HWC2::Error::None) {
627 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
628 "%s (%d)",
629 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
630 to_string(error).c_str(), static_cast<int32_t>(error));
631 } else {
632 hwcInfo.sourceCrop = sourceCrop;
633 }
David Sodmanba340492018-08-05 21:51:33 -0700634 getBE().compositionInfo.hwc.sourceCrop = sourceCrop;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800635
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800636 float alpha = static_cast<float>(getAlpha());
David Sodman15094112018-10-11 09:39:37 -0700637 error = hwcLayer->setPlaneAlpha(alpha);
638 ALOGE_IF(error != HWC2::Error::None,
639 "[%s] Failed to set plane alpha %.3f: "
640 "%s (%d)",
641 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700642 getBE().compositionInfo.hwc.alpha = alpha;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800643
David Sodman15094112018-10-11 09:39:37 -0700644 error = hwcLayer->setZOrder(z);
645 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
646 to_string(error).c_str(), static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700647 getBE().compositionInfo.hwc.z = z;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500648
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800649 int type = s.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
650 int appId = s.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700651 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400652 if (parent.get()) {
653 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800654 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
655 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
656 if (parentType >= 0 || parentAppId >= 0) {
657 type = parentType;
658 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700659 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400660 }
661
David Sodman15094112018-10-11 09:39:37 -0700662 error = hwcLayer->setInfo(type, appId);
663 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
664 static_cast<int32_t>(error));
665
David Sodmanba340492018-08-05 21:51:33 -0700666 getBE().compositionInfo.hwc.type = type;
667 getBE().compositionInfo.hwc.appId = appId;
668
Mathias Agopian29a367b2011-07-12 14:51:45 -0700669 /*
670 * Transformations are applied in this order:
671 * 1) buffer orientation/flip/mirror
672 * 2) state transformation (window manager)
673 * 3) layer orientation (screen orientation)
674 * (NOTE: the matrices are multiplied in reverse order)
675 */
676
Peiyong Linefefaac2018-08-17 12:27:51 -0700677 const ui::Transform bufferOrientation(mCurrentTransform);
678 ui::Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700679
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800680 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700681 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700682 * the code below applies the primary display's inverse transform to the
683 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700684 */
David Sodman41fdfc92017-11-06 16:09:56 -0800685 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700686 // calculate the inverse transform
687 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800688 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700689 }
Robert Carrcae605c2017-03-29 12:10:31 -0700690
691 /*
692 * Here we cancel out the orientation component of the WM transform.
693 * The scaling and translate components are already included in our bounds
694 * computation so it's enough to just omit it in the composition.
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000695 * See comment in BufferLayer::prepareClientLayer with ref to b/36727915 for why.
Robert Carrcae605c2017-03-29 12:10:31 -0700696 */
Peiyong Linefefaac2018-08-17 12:27:51 -0700697 transform = ui::Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700698 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700699
700 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800701 const uint32_t orientation = transform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -0700702 if (orientation & ui::Transform::ROT_INVALID) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800703 // we can only handle simple transformation
Lloyd Pique074e8122018-07-26 12:57:23 -0700704 hwcInfo.forceClientComposition = true;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700705 getBE().mHwcLayers[*displayId].compositionType = HWC2::Composition::Client;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800706 } else {
707 auto transform = static_cast<HWC2::Transform>(orientation);
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800708 hwcInfo.transform = transform;
David Sodman15094112018-10-11 09:39:37 -0700709 auto error = hwcLayer->setTransform(transform);
710 ALOGE_IF(error != HWC2::Error::None,
711 "[%s] Failed to set transform %s: "
712 "%s (%d)",
713 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
714 static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700715 getBE().compositionInfo.hwc.transform = transform;
David Sodman4b7c4bc2017-11-17 12:13:59 -0800716 }
717}
718
Dominik Laskowski075d3172018-05-24 15:50:06 -0700719void Layer::forceClientComposition(DisplayId displayId) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700720 RETURN_IF_NO_HWC_LAYER(displayId);
Dominik Laskowski7e045462018-05-30 13:02:02 -0700721 getBE().mHwcLayers[displayId].forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800722}
Dan Stozaee44edd2015-03-23 15:50:23 -0700723
Dominik Laskowski075d3172018-05-24 15:50:06 -0700724bool Layer::getForceClientComposition(DisplayId displayId) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700725 RETURN_IF_NO_HWC_LAYER(displayId, false);
Dominik Laskowski7e045462018-05-30 13:02:02 -0700726 return getBE().mHwcLayers[displayId].forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800727}
728
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700729void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700730 const auto displayId = display->getId();
Dominik Laskowski34157762018-10-31 13:07:19 -0700731 LOG_ALWAYS_FATAL_IF(!displayId);
732 if (!hasHwcLayer(*displayId) || getCompositionType(displayId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800733 return;
734 }
735
736 // This gives us only the "orientation" component of the transform
Vishnu Nair33a6eee2019-02-06 13:48:06 -0800737 const State& s(getDrawingState());
Dan Stoza9e56aa02015-11-02 13:00:03 -0800738
739 // Apply the layer's transform, followed by the display's global transform
740 // Here we're guaranteed that the layer's transform preserves rects
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800741 Rect win = getCroppedBufferSize(s);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800742 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700743 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800744 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700745 frame.intersect(display->getViewport(), &frame);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700746 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800747 auto position = displayTransform.transform(frame);
748
Dominik Laskowski7e045462018-05-30 13:02:02 -0700749 auto error =
Dominik Laskowski075d3172018-05-24 15:50:06 -0700750 getBE().mHwcLayers[*displayId].layer->setCursorPosition(position.left, position.top);
751
David Sodman41fdfc92017-11-06 16:09:56 -0800752 ALOGE_IF(error != HWC2::Error::None,
753 "[%s] Failed to set cursor position "
754 "to (%d, %d): %s (%d)",
755 mName.string(), position.left, position.top, to_string(error).c_str(),
756 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800757}
Riley Andrews03414a12014-07-01 14:22:59 -0700758
Mathias Agopian13127d82013-03-05 17:47:11 -0800759// ---------------------------------------------------------------------------
760// drawing...
761// ---------------------------------------------------------------------------
762
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000763bool Layer::prepareClientLayer(const RenderArea& renderArea, const Region& clip,
764 Region& clearRegion, renderengine::LayerSettings& layer) {
765 return prepareClientLayer(renderArea, clip, false, clearRegion, layer);
Mathias Agopian13127d82013-03-05 17:47:11 -0800766}
767
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000768bool Layer::prepareClientLayer(const RenderArea& renderArea, bool useIdentityTransform,
769 Region& clearRegion, renderengine::LayerSettings& layer) {
770 return prepareClientLayer(renderArea, Region(renderArea.getBounds()), useIdentityTransform,
771 clearRegion, layer);
772}
773
774bool Layer::prepareClientLayer(const RenderArea& /*renderArea*/, const Region& /*clip*/,
775 bool useIdentityTransform, Region& /*clearRegion*/,
776 renderengine::LayerSettings& layer) {
Vishnu Nair4351ad52019-02-11 14:13:02 -0800777 FloatRect bounds = getBounds();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000778 half alpha = getAlpha();
779 layer.geometry.boundaries = bounds;
780 if (useIdentityTransform) {
781 layer.geometry.positionTransform = mat4();
782 } else {
783 const ui::Transform transform = getTransform();
784 mat4 m;
785 m[0][0] = transform[0][0];
786 m[0][1] = transform[0][1];
787 m[0][3] = transform[0][2];
788 m[1][0] = transform[1][0];
789 m[1][1] = transform[1][1];
790 m[1][3] = transform[1][2];
791 m[3][0] = transform[2][0];
792 m[3][1] = transform[2][1];
793 m[3][3] = transform[2][2];
794 layer.geometry.positionTransform = m;
795 }
796
797 if (hasColorTransform()) {
798 layer.colorTransform = getColorTransform();
799 }
800
801 const auto roundedCornerState = getRoundedCornerState();
802 layer.geometry.roundedCornersRadius = roundedCornerState.radius;
803 layer.geometry.roundedCornersCrop = roundedCornerState.cropRect;
804
805 layer.alpha = alpha;
806 layer.sourceDataspace = mCurrentDataSpace;
807 return true;
Mathias Agopian13127d82013-03-05 17:47:11 -0800808}
809
David Sodman41fdfc92017-11-06 16:09:56 -0800810void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
811 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800812 auto& engine(mFlinger->getRenderEngine());
David Sodman9eeae692017-11-02 10:53:32 -0700813 computeGeometry(renderArea, getBE().mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700814 engine.setupFillWithColor(red, green, blue, alpha);
David Sodman9eeae692017-11-02 10:53:32 -0700815 engine.drawMesh(getBE().mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800816}
817
David Sodmanc1498e62018-09-12 14:36:26 -0700818void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
819 clearWithOpenGL(renderArea, 0, 0, 0, 0);
820}
821
Dominik Laskowski075d3172018-05-24 15:50:06 -0700822void Layer::setCompositionType(DisplayId displayId, HWC2::Composition type, bool callIntoHwc) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700823 if (getBE().mHwcLayers.count(displayId) == 0) {
Chia-I Wu30505fb2018-03-26 16:20:31 -0700824 ALOGE("setCompositionType called without a valid HWC layer");
825 return;
David Sodman15094112018-10-11 09:39:37 -0700826 }
827 auto& hwcInfo = getBE().mHwcLayers[displayId];
828 auto& hwcLayer = hwcInfo.layer;
829 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", (hwcLayer)->getId(), to_string(type).c_str(),
830 static_cast<int>(callIntoHwc));
831 if (hwcInfo.compositionType != type) {
832 ALOGV(" actually setting");
833 hwcInfo.compositionType = type;
834 if (callIntoHwc) {
835 auto error = (hwcLayer)->setCompositionType(type);
836 ALOGE_IF(error != HWC2::Error::None,
837 "[%s] Failed to set "
838 "composition type %s: %s (%d)",
839 mName.string(), to_string(type).c_str(), to_string(error).c_str(),
840 static_cast<int32_t>(error));
Chia-I Wu30505fb2018-03-26 16:20:31 -0700841 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800842 }
843}
844
Dominik Laskowski075d3172018-05-24 15:50:06 -0700845HWC2::Composition Layer::getCompositionType(const std::optional<DisplayId>& displayId) const {
846 if (!displayId) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700847 // If we're querying the composition type for a display that does not
848 // have a HWC counterpart, then it will always be Client
849 return HWC2::Composition::Client;
850 }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700851 if (getBE().mHwcLayers.count(*displayId) == 0) {
852 ALOGE("getCompositionType called with an invalid HWC layer");
853 return HWC2::Composition::Invalid;
854 }
855 return getBE().mHwcLayers.at(*displayId).compositionType;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800856}
857
Dominik Laskowski075d3172018-05-24 15:50:06 -0700858void Layer::setClearClientTarget(DisplayId displayId, bool clear) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700859 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800860 ALOGE("setClearClientTarget called without a valid HWC layer");
861 return;
862 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700863 getBE().mHwcLayers[displayId].clearClientTarget = clear;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800864}
865
Dominik Laskowski075d3172018-05-24 15:50:06 -0700866bool Layer::getClearClientTarget(DisplayId displayId) const {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700867 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800868 ALOGE("getClearClientTarget called without a valid HWC layer");
869 return false;
870 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700871 return getBE().mHwcLayers.at(displayId).clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800872}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800873
Dan Stozacac35382016-01-27 12:21:06 -0800874bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
875 if (point->getFrameNumber() <= mCurrentFrameNumber) {
876 // Don't bother with a SyncPoint, since we've already latched the
877 // relevant frame
878 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700879 }
Robert Carr2e102c92018-10-23 12:11:15 -0700880 if (isRemovedFromCurrentState()) {
881 return false;
882 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700883
Dan Stozacac35382016-01-27 12:21:06 -0800884 Mutex::Autolock lock(mLocalSyncPointMutex);
885 mLocalSyncPoints.push_back(point);
886 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700887}
888
Mathias Agopian13127d82013-03-05 17:47:11 -0800889// ----------------------------------------------------------------------------
890// local state
891// ----------------------------------------------------------------------------
892
Peiyong Lin833074a2018-08-28 11:53:54 -0700893void Layer::computeGeometry(const RenderArea& renderArea,
894 renderengine::Mesh& mesh,
chaviwa76b2712017-09-20 12:02:26 -0700895 bool useIdentityTransform) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700896 const ui::Transform renderAreaTransform(renderArea.getTransform());
Vishnu Nair4351ad52019-02-11 14:13:02 -0800897 FloatRect win = getBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700898
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000899 vec2 lt = vec2(win.left, win.top);
900 vec2 lb = vec2(win.left, win.bottom);
901 vec2 rb = vec2(win.right, win.bottom);
902 vec2 rt = vec2(win.right, win.top);
903
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800904 ui::Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000905 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700906 lt = layerTransform.transform(lt);
907 lb = layerTransform.transform(lb);
908 rb = layerTransform.transform(rb);
909 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000910 }
911
Peiyong Lin833074a2018-08-28 11:53:54 -0700912 renderengine::Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700913 position[0] = renderAreaTransform.transform(lt);
914 position[1] = renderAreaTransform.transform(lb);
915 position[2] = renderAreaTransform.transform(rb);
916 position[3] = renderAreaTransform.transform(rt);
Mathias Agopian13127d82013-03-05 17:47:11 -0800917}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800918
David Sodman41fdfc92017-11-06 16:09:56 -0800919bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800920 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700921 return (s.flags & layer_state_t::eLayerSecure);
922}
923
Mathias Agopian13127d82013-03-05 17:47:11 -0800924void Layer::setVisibleRegion(const Region& visibleRegion) {
925 // always called from main thread
926 this->visibleRegion = visibleRegion;
927}
928
929void Layer::setCoveredRegion(const Region& coveredRegion) {
930 // always called from main thread
931 this->coveredRegion = coveredRegion;
932}
933
David Sodman41fdfc92017-11-06 16:09:56 -0800934void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800935 // always called from main thread
936 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
937}
938
Robert Carre5f4f692018-01-12 13:12:28 -0800939void Layer::clearVisibilityRegions() {
940 visibleRegion.clear();
941 visibleNonTransparentRegion.clear();
942 coveredRegion.clear();
943}
944
Mathias Agopian13127d82013-03-05 17:47:11 -0800945// ----------------------------------------------------------------------------
946// transaction
947// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800948
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800949void Layer::pushPendingState() {
950 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700951 return;
952 }
953
Dan Stoza7dde5992015-05-22 09:51:44 -0700954 // If this transaction is waiting on the receipt of a frame, generate a sync
955 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700956 // We don't allow installing sync points after we are removed from the current state
957 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800958 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
959 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800960 if (barrierLayer == nullptr) {
961 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700962 // If we can't promote the layer we are intended to wait on,
963 // then it is expired or otherwise invalid. Allow this transaction
964 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800965 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800966 } else {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800967 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy);
Robert Carr0d480722017-01-10 16:42:54 -0800968 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800969 mRemoteSyncPoints.push_back(std::move(syncPoint));
970 } else {
971 // We already missed the frame we're supposed to synchronize
972 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800973 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800974 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700975 }
976
Dan Stoza7dde5992015-05-22 09:51:44 -0700977 // Wake us up to check if the frame has been received
978 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700979 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700980 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800981 mPendingStates.push_back(mCurrentState);
982 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700983}
984
Pablo Ceballos05289c22016-04-14 15:49:55 -0700985void Layer::popPendingState(State* stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800986 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700987
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800988 mPendingStates.removeAt(0);
989 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700990}
991
Pablo Ceballos05289c22016-04-14 15:49:55 -0700992bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700993 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800994 while (!mPendingStates.empty()) {
995 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700996 if (mRemoteSyncPoints.empty()) {
997 // If we don't have a sync point for this, apply it anyway. It
998 // will be visually wrong, but it should keep us from getting
999 // into too much trouble.
1000 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -07001001 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001002 stateUpdateAvailable = true;
1003 continue;
1004 }
1005
Marissa Wallf58c14b2018-07-24 10:50:43 -07001006 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001007 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -08001008 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -08001009
1010 // Signal our end of the sync point and then dispose of it
1011 mRemoteSyncPoints.front()->setTransactionApplied();
1012 mRemoteSyncPoints.pop_front();
1013 continue;
1014 }
1015
Dan Stoza7dde5992015-05-22 09:51:44 -07001016 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
1017 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -07001018 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001019 stateUpdateAvailable = true;
1020
1021 // Signal our end of the sync point and then dispose of it
1022 mRemoteSyncPoints.front()->setTransactionApplied();
1023 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -08001024 } else {
1025 break;
Dan Stoza7dde5992015-05-22 09:51:44 -07001026 }
Dan Stoza7dde5992015-05-22 09:51:44 -07001027 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -07001028 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -07001029 stateUpdateAvailable = true;
1030 }
1031 }
1032
1033 // If we still have pending updates, wake SurfaceFlinger back up and point
1034 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001035 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001036 setTransactionFlags(eTransactionNeeded);
1037 mFlinger->setTransactionFlags(eTraversalNeeded);
1038 }
1039
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001040 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -07001041 return stateUpdateAvailable;
1042}
1043
Marissa Wall61c58622018-07-18 10:12:20 -07001044uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +00001045 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001046
Marissa Wall61c58622018-07-18 10:12:20 -07001047 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
1048 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -07001049
David Sodmaneb085e02017-10-05 18:49:04 -07001050 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001051 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +00001052 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -08001053 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
1054 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1055 " requested={ wh={%4u,%4u} }}\n"
1056 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1057 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -07001058 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -07001059 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
1060 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
1061 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
1062 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
1063 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -07001064 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
1065 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
1066 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001067 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001068
Robert Carre392b552017-09-19 12:16:05 -07001069 // Don't let Layer::doTransaction update the drawing state
1070 // if we have a pending resize, unless we are in fixed-size mode.
1071 // the drawing state will be updated only once we receive a buffer
1072 // with the correct size.
1073 //
1074 // In particular, we want to make sure the clip (which is part
1075 // of the geometry state) is latched together with the size but is
1076 // latched immediately when no resizing is involved.
1077 //
1078 // If a sideband stream is attached, however, we want to skip this
1079 // optimization so that transactions aren't missed when a buffer
1080 // never arrives
1081 //
1082 // In the case that we don't have a buffer we ignore other factors
1083 // and avoid entering the resizePending state. At a high level the
1084 // resizePending state is to avoid applying the state of the new buffer
1085 // to the old buffer. However in the state where we don't have an old buffer
1086 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -07001087 const bool resizePending =
1088 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
1089 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
David Sodman0cc69182017-11-17 12:12:07 -08001090 (getBE().compositionInfo.mBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001091 if (!isFixedSize()) {
David Sodman0cc69182017-11-17 12:12:07 -08001092 if (resizePending && getBE().compositionInfo.hwc.sidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001093 flags |= eDontUpdateGeometryState;
1094 }
1095 }
1096
Robert Carr7bf247e2017-05-18 14:02:49 -07001097 // Here we apply various requested geometry states, depending on our
1098 // latching configuration. See Layer.h for a detailed discussion of
1099 // how geometry latching is controlled.
1100 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +00001101 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001102
1103 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1104 // mode, which causes attributes which normally latch regardless of scaling mode,
1105 // to be delayed. We copy the requested state to the active state making sure
1106 // to respect these rules (again see Layer.h for a detailed discussion).
1107 //
1108 // There is an awkward asymmetry in the handling of the crop states in the position
1109 // states, as can be seen below. Largely this arises from position and transform
1110 // being stored in the same data structure while having different latching rules.
1111 // b/38182305
1112 //
Marissa Wall61c58622018-07-18 10:12:20 -07001113 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -07001114 // applyPendingStates in the presence of deferred transactions.
1115 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -07001116 float tx = stateToCommit->active_legacy.transform.tx();
1117 float ty = stateToCommit->active_legacy.transform.ty();
1118 stateToCommit->active_legacy = stateToCommit->requested_legacy;
1119 stateToCommit->active_legacy.transform.set(tx, ty);
1120 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001121 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001122 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -07001123 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001124 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001125 }
1126
Marissa Wall61c58622018-07-18 10:12:20 -07001127 return flags;
1128}
1129
1130uint32_t Layer::doTransaction(uint32_t flags) {
1131 ATRACE_CALL();
1132
chaviw5aedec92018-10-22 10:40:38 -07001133 if (mLayerDetached) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001134 return flags;
1135 }
1136
1137 if (mChildrenChanged) {
1138 flags |= eVisibleRegion;
1139 mChildrenChanged = false;
chaviw5aedec92018-10-22 10:40:38 -07001140 }
1141
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001142 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +00001143 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -07001144 if (!applyPendingStates(&c)) {
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001145 return flags;
Marissa Wall61c58622018-07-18 10:12:20 -07001146 }
1147
1148 flags = doTransactionResize(flags, &c);
1149
Alec Mourib416efd2018-09-06 21:01:59 +00001150 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -07001151
1152 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001153 // invalidate and recompute the visible regions if needed
1154 flags |= Layer::eVisibleRegion;
1155 }
1156
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001157 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001158 // invalidate and recompute the visible regions if needed
1159 flags |= eVisibleRegion;
1160 this->contentDirty = true;
1161
1162 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -07001163 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -07001164 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -08001165 }
1166
Dan Stozac8145172016-04-28 16:29:06 -07001167 // If the layer is hidden, signal and clear out all local sync points so
1168 // that transactions for layers depending on this layer's frames becoming
1169 // visible are not blocked
1170 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001171 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001172 }
1173
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001174 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -07001175 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001176 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -07001177 }
1178
Mathias Agopian13127d82013-03-05 17:47:11 -08001179 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001180 commitTransaction(c);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001181 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -08001182 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001183}
1184
Pablo Ceballos05289c22016-04-14 15:49:55 -07001185void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001186 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001187}
1188
Mathias Agopian13127d82013-03-05 17:47:11 -08001189uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001190 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -08001191}
1192
1193uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001194 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -08001195}
1196
Robert Carr82364e32016-05-15 11:27:47 -07001197bool Layer::setPosition(float x, float y, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001198 if (mCurrentState.requested_legacy.transform.tx() == x &&
1199 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001200 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001201 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001202
1203 // We update the requested and active position simultaneously because
1204 // we want to apply the position portion of the transform matrix immediately,
1205 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001206 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001207 if (immediate && !mFreezeGeometryUpdates) {
1208 // Here we directly update the active state
1209 // unlike other setters, because we store it within
1210 // the transform, but use different latching rules.
1211 // b/38182305
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001212 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001213 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001214 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001215
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001216 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001217 setTransactionFlags(eTransactionNeeded);
1218 return true;
1219}
Robert Carr82364e32016-05-15 11:27:47 -07001220
Robert Carr1f0a16a2016-10-24 16:27:39 -07001221bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1222 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1223 if (idx < 0) {
1224 return false;
1225 }
1226 if (childLayer->setLayer(z)) {
1227 mCurrentChildren.removeAt(idx);
1228 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001229 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001230 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001231 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001232}
1233
Robert Carr503c7042017-09-27 15:06:08 -07001234bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1235 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1236 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1237 if (idx < 0) {
1238 return false;
1239 }
1240 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1241 mCurrentChildren.removeAt(idx);
1242 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001243 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001244 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001245 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001246}
1247
Robert Carrae060832016-11-28 10:51:00 -08001248bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001249 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
1250 mCurrentState.sequence++;
1251 mCurrentState.z = z;
1252 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001253
1254 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001255 if (mCurrentState.zOrderRelativeOf != nullptr) {
1256 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -07001257 if (strongRelative != nullptr) {
1258 strongRelative->removeZOrderRelative(this);
1259 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001260 mCurrentState.zOrderRelativeOf = nullptr;
Robert Carrdb66e622017-04-10 16:55:57 -07001261 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001262 setTransactionFlags(eTransactionNeeded);
1263 return true;
1264}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001265
Robert Carrdb66e622017-04-10 16:55:57 -07001266void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001267 mCurrentState.zOrderRelatives.remove(relative);
1268 mCurrentState.sequence++;
1269 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001270 setTransactionFlags(eTransactionNeeded);
1271}
1272
1273void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001274 mCurrentState.zOrderRelatives.add(relative);
1275 mCurrentState.modified = true;
1276 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -07001277 setTransactionFlags(eTransactionNeeded);
1278}
1279
Robert Carr503d2bd2017-12-04 15:49:47 -08001280bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001281 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1282 if (handle == nullptr) {
1283 return false;
1284 }
1285 sp<Layer> relative = handle->owner.promote();
1286 if (relative == nullptr) {
1287 return false;
1288 }
1289
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001290 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1291 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001292 return false;
1293 }
1294
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001295 mCurrentState.sequence++;
1296 mCurrentState.modified = true;
1297 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001298
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001299 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -07001300 if (oldZOrderRelativeOf != nullptr) {
1301 oldZOrderRelativeOf->removeZOrderRelative(this);
1302 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001303 mCurrentState.zOrderRelativeOf = relative;
Robert Carrdb66e622017-04-10 16:55:57 -07001304 relative->addZOrderRelative(this);
1305
1306 setTransactionFlags(eTransactionNeeded);
1307
1308 return true;
1309}
1310
Mathias Agopian13127d82013-03-05 17:47:11 -08001311bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001312 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001313 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001314 mCurrentState.requested_legacy.w = w;
1315 mCurrentState.requested_legacy.h = h;
1316 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001317 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001318
1319 // record the new size, from this point on, when the client request
1320 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001321 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001322 return true;
1323}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001324bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001325 if (mCurrentState.color.a == alpha) return false;
1326 mCurrentState.sequence++;
1327 mCurrentState.color.a = alpha;
1328 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001329 setTransactionFlags(eTransactionNeeded);
1330 return true;
1331}
chaviw13fdc492017-06-27 12:40:18 -07001332
Valerie Haudd0b7572019-01-29 14:59:27 -08001333bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1334 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001335 return false;
Valerie Hauaa194562019-02-05 16:21:38 -08001336 }
1337 mCurrentState.sequence++;
1338 mCurrentState.modified = true;
1339 setTransactionFlags(eTransactionNeeded);
1340
1341 if (!mCurrentState.bgColorLayer && alpha != 0) {
Valerie Haudd0b7572019-01-29 14:59:27 -08001342 // create background color layer if one does not yet exist
1343 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
1344 const String8& name = mName + "BackgroundColorLayer";
1345 mCurrentState.bgColorLayer =
1346 new ColorLayer(LayerCreationArgs(mFlinger.get(), nullptr, name, 0, 0, flags));
chaviw13fdc492017-06-27 12:40:18 -07001347
Valerie Haudd0b7572019-01-29 14:59:27 -08001348 // add to child list
1349 addChild(mCurrentState.bgColorLayer);
1350 mFlinger->mLayersAdded = true;
1351 // set up SF to handle added color layer
1352 if (isRemovedFromCurrentState()) {
1353 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1354 }
1355 mFlinger->setTransactionFlags(eTransactionNeeded);
1356 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1357 mCurrentState.bgColorLayer->reparent(nullptr);
1358 mCurrentState.bgColorLayer = nullptr;
1359 return true;
1360 }
1361
1362 mCurrentState.bgColorLayer->setColor(color);
1363 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1364 mCurrentState.bgColorLayer->setAlpha(alpha);
1365 mCurrentState.bgColorLayer->setDataspace(dataspace);
1366
chaviw13fdc492017-06-27 12:40:18 -07001367 return true;
1368}
1369
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001370bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001371 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001372
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001373 mCurrentState.sequence++;
1374 mCurrentState.cornerRadius = cornerRadius;
1375 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001376 setTransactionFlags(eTransactionNeeded);
1377 return true;
1378}
1379
Robert Carrd4ae7f32018-06-07 16:10:57 -07001380bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1381 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001382 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001383 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1384
1385 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1386 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1387 return false;
1388 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001389 mCurrentState.sequence++;
1390 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1391 matrix.dsdy);
1392 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001393 setTransactionFlags(eTransactionNeeded);
1394 return true;
1395}
Marissa Wall61c58622018-07-18 10:12:20 -07001396
Mathias Agopian13127d82013-03-05 17:47:11 -08001397bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001398 mCurrentState.requestedTransparentRegion_legacy = transparent;
1399 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001400 setTransactionFlags(eTransactionNeeded);
1401 return true;
1402}
1403bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001404 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1405 if (mCurrentState.flags == newFlags) return false;
1406 mCurrentState.sequence++;
1407 mCurrentState.flags = newFlags;
1408 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001409 setTransactionFlags(eTransactionNeeded);
1410 return true;
1411}
Robert Carr99e27f02016-06-16 15:18:02 -07001412
Marissa Wallf58c14b2018-07-24 10:50:43 -07001413bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001414 if (mCurrentState.requestedCrop_legacy == crop) return false;
1415 mCurrentState.sequence++;
1416 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001417 if (immediate && !mFreezeGeometryUpdates) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001418 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001419 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001420 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1421
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001422 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001423 setTransactionFlags(eTransactionNeeded);
1424 return true;
1425}
Robert Carr8d5227b2017-03-16 15:41:03 -07001426
Robert Carrc3574f72016-03-24 12:19:32 -07001427bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001428 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001429 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001430 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001431 return true;
1432}
1433
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001434bool Layer::setMetadata(LayerMetadata data) {
1435 bool changed = data.mMap != mCurrentState.metadata.mMap;
1436 if (!changed) return false;
1437 mCurrentState.metadata = std::move(data);
1438 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001439 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001440 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001441 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001442}
1443
Mathias Agopian13127d82013-03-05 17:47:11 -08001444bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001445 if (mCurrentState.layerStack == layerStack) return false;
1446 mCurrentState.sequence++;
1447 mCurrentState.layerStack = layerStack;
1448 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001449 setTransactionFlags(eTransactionNeeded);
1450 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001451}
1452
Robert Carr1f0a16a2016-10-24 16:27:39 -07001453uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001454 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001455 if (p == nullptr) {
1456 return getDrawingState().layerStack;
1457 }
1458 return p->getLayerStack();
1459}
1460
Marissa Wallf58c14b2018-07-24 10:50:43 -07001461void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001462 mCurrentState.barrierLayer_legacy = barrierLayer;
1463 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001464 // We don't set eTransactionNeeded, because just receiving a deferral
1465 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001466 mCurrentState.modified = true;
1467 pushPendingState();
1468 mCurrentState.barrierLayer_legacy = nullptr;
1469 mCurrentState.frameNumber_legacy = 0;
1470 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001471}
1472
Marissa Wallf58c14b2018-07-24 10:50:43 -07001473void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001474 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001475 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001476}
1477
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001478// ----------------------------------------------------------------------------
1479// pageflip handling...
1480// ----------------------------------------------------------------------------
1481
Robert Carr1f0a16a2016-10-24 16:27:39 -07001482bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001483 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001484 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001485 if (parent != nullptr && parent->isHiddenByPolicy()) {
1486 return true;
1487 }
1488 return s.flags & layer_state_t::eLayerHidden;
1489}
1490
David Sodman41fdfc92017-11-06 16:09:56 -08001491uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001492 // TODO: should we do something special if mSecure is set?
1493 if (mProtectedByApp) {
1494 // need a hardware-protected path to external video sink
1495 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001496 }
Riley Andrews03414a12014-07-01 14:22:59 -07001497 if (mPotentialCursor) {
1498 usage |= GraphicBuffer::USAGE_CURSOR;
1499 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001500 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001501 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001502}
1503
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001504void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001505 uint32_t orientation = 0;
Vishnu Nairf2deb822018-11-12 17:53:48 -08001506 // Disable setting transform hint if the debug flag is set or if the
1507 // getTransformToDisplayInverse flag is set and the client wants to submit buffers
1508 // in one orientation.
1509 if (!mFlinger->mDebugDisableTransformHint && !getTransformToDisplayInverse()) {
Mathias Agopian84300952012-11-21 16:02:13 -08001510 // The transform hint is used to improve performance, but we can
1511 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001512 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001513 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001514 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001515 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001516 orientation = 0;
1517 }
1518 }
David Sodmaneb085e02017-10-05 18:49:04 -07001519 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001520}
1521
Mathias Agopian13127d82013-03-05 17:47:11 -08001522// ----------------------------------------------------------------------------
1523// debugging
1524// ----------------------------------------------------------------------------
1525
Marissa Wall61c58622018-07-18 10:12:20 -07001526// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001527LayerDebugInfo Layer::getLayerDebugInfo() const {
1528 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001529 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001530 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001531 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001532 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
Yiwei Zhang5434a782018-12-05 18:06:32 -08001533 info.mType = std::string(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001534 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001535 info.mVisibleRegion = visibleRegion;
1536 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001537 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001538 info.mX = ds.active_legacy.transform.tx();
1539 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001540 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001541 info.mWidth = ds.active_legacy.w;
1542 info.mHeight = ds.active_legacy.h;
1543 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001544 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001545 info.mFlags = ds.flags;
1546 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001547 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001548 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1549 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1550 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1551 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001552 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001553 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001554 if (buffer != 0) {
1555 info.mActiveBufferWidth = buffer->getWidth();
1556 info.mActiveBufferHeight = buffer->getHeight();
1557 info.mActiveBufferStride = buffer->getStride();
1558 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001559 } else {
1560 info.mActiveBufferWidth = 0;
1561 info.mActiveBufferHeight = 0;
1562 info.mActiveBufferStride = 0;
1563 info.mActiveBufferFormat = 0;
1564 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001565 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001566 info.mNumQueuedFrames = getQueuedFrameCount();
1567 info.mRefreshPending = isBufferLatched();
1568 info.mIsOpaque = isOpaque(ds);
1569 info.mContentDirty = contentDirty;
1570 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001571}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001572
Yiwei Zhang5434a782018-12-05 18:06:32 -08001573void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001574 result.append("-------------------------------");
1575 result.append("-------------------------------");
1576 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001577 result.append(" Layer name\n");
1578 result.append(" Z | ");
1579 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001580 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001581 result.append(" Disp Frame (LTRB) | ");
1582 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001583 result.append("-------------------------------");
1584 result.append("-------------------------------");
1585 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001586}
1587
Yiwei Zhang5434a782018-12-05 18:06:32 -08001588void Layer::miniDump(std::string& result, DisplayId displayId) const {
Dominik Laskowski34157762018-10-31 13:07:19 -07001589 if (!hasHwcLayer(displayId)) {
Dan Stozae22aec72016-08-01 13:20:59 -07001590 return;
1591 }
1592
Yiwei Zhang5434a782018-12-05 18:06:32 -08001593 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001594 if (mName.length() > 77) {
1595 std::string shortened;
1596 shortened.append(mName.string(), 36);
1597 shortened.append("[...]");
1598 shortened.append(mName.string() + (mName.length() - 36), 36);
Yiwei Zhang5434a782018-12-05 18:06:32 -08001599 name = shortened;
Dan Stozae22aec72016-08-01 13:20:59 -07001600 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001601 name = std::string(mName.string(), mName.size());
Dan Stozae22aec72016-08-01 13:20:59 -07001602 }
1603
Yiwei Zhang5434a782018-12-05 18:06:32 -08001604 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001605
Alec Mourib416efd2018-09-06 21:01:59 +00001606 const State& layerState(getDrawingState());
Lloyd Pique074e8122018-07-26 12:57:23 -07001607 const LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers.at(displayId);
Chia-I Wu1e043612018-03-01 09:45:09 -08001608 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001609 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001610 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001611 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001612 }
Yiwei Zhang5434a782018-12-05 18:06:32 -08001613 StringAppendF(&result, "%10s | ", to_string(getCompositionType(displayId)).c_str());
1614 StringAppendF(&result, "%10s | ", to_string(hwcInfo.transform).c_str());
Lloyd Pique074e8122018-07-26 12:57:23 -07001615 const Rect& frame = hwcInfo.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001616 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique074e8122018-07-26 12:57:23 -07001617 const FloatRect& crop = hwcInfo.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001618 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1619 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001620
David Sodman7e4ae112018-02-09 15:02:28 -08001621 result.append("- - - - - - - - - - - - - - - -\n");
1622
1623 std::string compositionInfoStr;
1624 getBE().compositionInfo.dump(compositionInfoStr, "compositionInfo");
Yiwei Zhang5434a782018-12-05 18:06:32 -08001625 result.append(compositionInfoStr);
David Sodman7e4ae112018-02-09 15:02:28 -08001626
Yichi Chen6ca35192018-05-29 12:20:43 +08001627 result.append("- - - - - - - - - - - - - - - -");
1628 result.append("- - - - - - - - - - - - - - - -");
1629 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001630}
Dan Stozae22aec72016-08-01 13:20:59 -07001631
Yiwei Zhang5434a782018-12-05 18:06:32 -08001632void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001633 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001634}
1635
Svetoslavd85084b2014-03-20 10:28:31 -07001636void Layer::clearFrameStats() {
1637 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001638}
1639
Jamie Gennis6547ff42013-07-16 20:12:42 -07001640void Layer::logFrameStats() {
1641 mFrameTracker.logAndResetStats(mName);
1642}
1643
Svetoslavd85084b2014-03-20 10:28:31 -07001644void Layer::getFrameStats(FrameStats* outStats) const {
1645 mFrameTracker.getStats(outStats);
1646}
1647
Yiwei Zhang5434a782018-12-05 18:06:32 -08001648void Layer::dumpFrameEvents(std::string& result) {
1649 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001650 Mutex::Autolock lock(mFrameEventHistoryMutex);
1651 mFrameEventHistory.checkFencesForCompletion();
1652 mFrameEventHistory.dump(result);
1653}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001654
Brian Anderson5ea5e592016-12-01 16:54:33 -08001655void Layer::onDisconnect() {
1656 Mutex::Autolock lock(mFrameEventHistoryMutex);
1657 mFrameEventHistory.onDisconnect();
Yiwei Zhangaf8ee942018-11-22 00:15:23 -08001658 mFlinger->mTimeStats->onDestroy(getSequence());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001659}
1660
Brian Anderson3890c392016-07-25 12:48:08 -07001661void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001662 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001663 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001664 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1665 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001666 }
1667
Brian Andersond6927fb2016-07-23 23:37:30 -07001668 Mutex::Autolock lock(mFrameEventHistoryMutex);
1669 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001670 // If there are any unsignaled fences in the aquire timeline at this
1671 // point, the previously queued frame hasn't been latched yet. Go ahead
1672 // and try to get the signal time here so the syscall is taken out of
1673 // the main thread's critical path.
1674 mAcquireTimeline.updateSignalTimes();
1675 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001676 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001677 mFrameEventHistory.addQueue(*newTimestamps);
1678 }
1679
Brian Anderson3890c392016-07-25 12:48:08 -07001680 if (outDelta) {
1681 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001682 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001683}
Dan Stozae77c7662016-05-13 11:37:28 -07001684
Chia-I Wu98f1c102017-05-30 14:54:08 -07001685size_t Layer::getChildrenCount() const {
1686 size_t count = 0;
1687 for (const sp<Layer>& child : mCurrentChildren) {
1688 count += 1 + child->getChildrenCount();
1689 }
1690 return count;
1691}
1692
Robert Carr1f0a16a2016-10-24 16:27:39 -07001693void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001694 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001695 setTransactionFlags(eTransactionNeeded);
Robert Carr1323c952019-01-28 18:13:27 -08001696
Robert Carr1f0a16a2016-10-24 16:27:39 -07001697 mCurrentChildren.add(layer);
1698 layer->setParent(this);
1699}
1700
1701ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001702 mChildrenChanged = true;
Robert Carr7f2ed8b2019-02-07 14:45:11 -08001703 setTransactionFlags(eTransactionNeeded);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001704
Robert Carr1323c952019-01-28 18:13:27 -08001705 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001706 return mCurrentChildren.remove(layer);
1707}
1708
Robert Carr1db73f62016-12-21 12:58:51 -08001709bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1710 sp<Handle> handle = nullptr;
1711 sp<Layer> newParent = nullptr;
1712 if (newParentHandle == nullptr) {
1713 return false;
1714 }
1715 handle = static_cast<Handle*>(newParentHandle.get());
1716 newParent = handle->owner.promote();
1717 if (newParent == nullptr) {
1718 ALOGE("Unable to promote Layer handle");
1719 return false;
1720 }
1721
chaviw5aedec92018-10-22 10:40:38 -07001722 if (attachChildren()) {
1723 setTransactionFlags(eTransactionNeeded);
1724 }
Robert Carr1db73f62016-12-21 12:58:51 -08001725 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001726 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001727 }
1728 mCurrentChildren.clear();
1729
1730 return true;
1731}
1732
Robert Carr15eae092018-03-23 13:43:53 -07001733void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001734 for (const sp<Layer>& child : mDrawingChildren) {
1735 child->mDrawingParent = newParent;
Vishnu Nair4351ad52019-02-11 14:13:02 -08001736 child->computeBounds(newParent->mBounds, newParent->getTransformWithScale());
Robert Carr578038f2018-03-09 12:25:24 -08001737 }
1738}
1739
chaviwf1961f72017-09-18 16:41:07 -07001740bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001741 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001742
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001743 // While layers are detached, we allow most operations
1744 // and simply halt performing the actual transaction. However
1745 // for reparent != null we would enter the mRemovedFromCurrentState
1746 // state, regardless of whether doTransaction was called, and
1747 // so we need to prevent the update here.
1748 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001749 return false;
1750 }
1751
Robert Carr54cf5b12019-01-25 14:02:28 -08001752 sp<Layer> newParent;
1753 if (newParentHandle != nullptr) {
1754 auto handle = static_cast<Handle*>(newParentHandle.get());
1755 newParent = handle->owner.promote();
1756 if (newParent == nullptr) {
1757 ALOGE("Unable to promote Layer handle");
1758 return false;
1759 }
1760 if (newParent == this) {
1761 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1762 return false;
1763 }
1764 }
1765
chaviwf1961f72017-09-18 16:41:07 -07001766 sp<Layer> parent = getParent();
1767 if (parent != nullptr) {
1768 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001769 }
1770
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001771 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001772 newParent->addChild(this);
1773 if (!newParent->isRemovedFromCurrentState()) {
1774 addToCurrentState();
1775 } else {
1776 onRemovedFromCurrentState();
1777 }
1778
1779 if (mLayerDetached) {
1780 mLayerDetached = false;
1781 callSetTransactionFlags = true;
1782 }
1783 } else {
1784 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001785 }
1786
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001787 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001788 setTransactionFlags(eTransactionNeeded);
1789 }
chaviw06178942017-07-27 10:25:59 -07001790 return true;
1791}
1792
Robert Carr9524cb32017-02-13 11:32:32 -08001793bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001794 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001795 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001796 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001797 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001798 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001799 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001800 }
Robert Carr7f619b22017-11-06 12:56:35 -08001801 }
Robert Carr9524cb32017-02-13 11:32:32 -08001802
1803 return true;
1804}
1805
chaviw5aedec92018-10-22 10:40:38 -07001806bool Layer::attachChildren() {
1807 bool changed = false;
1808 for (const sp<Layer>& child : mCurrentChildren) {
1809 sp<Client> parentClient = mClientRef.promote();
1810 sp<Client> client(child->mClientRef.promote());
1811 if (client != nullptr && parentClient != client) {
1812 if (child->mLayerDetached) {
1813 child->mLayerDetached = false;
1814 changed = true;
1815 }
1816 changed |= child->attachChildren();
1817 }
1818 }
1819
1820 return changed;
1821}
1822
Peiyong Lind3788632018-09-18 16:01:31 -07001823bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001824 static const mat4 identityMatrix = mat4();
1825
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001826 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001827 return false;
1828 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001829 ++mCurrentState.sequence;
1830 mCurrentState.colorTransform = matrix;
1831 mCurrentState.hasColorTransform = matrix != identityMatrix;
1832 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001833 setTransactionFlags(eTransactionNeeded);
1834 return true;
1835}
1836
chaviwf66724d2018-11-28 16:35:21 -08001837mat4 Layer::getColorTransform() const {
1838 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1839 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1840 colorTransform = parent->getColorTransform() * colorTransform;
1841 }
1842 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001843}
1844
1845bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001846 bool hasColorTransform = getDrawingState().hasColorTransform;
1847 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1848 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1849 }
1850 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001851}
1852
Chia-I Wu11481472018-05-04 10:43:19 -07001853bool Layer::isLegacyDataSpace() const {
1854 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001855 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001856 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001857}
1858
Robert Carr1f0a16a2016-10-24 16:27:39 -07001859void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001860 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001861}
1862
1863void Layer::clearSyncPoints() {
1864 for (const auto& child : mCurrentChildren) {
1865 child->clearSyncPoints();
1866 }
1867
1868 Mutex::Autolock lock(mLocalSyncPointMutex);
1869 for (auto& point : mLocalSyncPoints) {
1870 point->setFrameAvailable();
1871 }
1872 mLocalSyncPoints.clear();
1873}
1874
1875int32_t Layer::getZ() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001876 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001877}
1878
Robert Carr29abff82017-12-04 13:51:20 -08001879bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1880 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001881 const State& state = useDrawing ? mDrawingState : mCurrentState;
Robert Carr29abff82017-12-04 13:51:20 -08001882 return state.zOrderRelativeOf != nullptr;
1883}
1884
David Sodman41fdfc92017-11-06 16:09:56 -08001885__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001886 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001887 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1888 "makeTraversalList received invalid stateSet");
1889 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1890 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001891 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001892
Robert Carr29abff82017-12-04 13:51:20 -08001893 if (state.zOrderRelatives.size() == 0) {
1894 *outSkipRelativeZUsers = true;
1895 return children;
1896 }
1897
chaviwfd462612018-05-31 16:11:27 -07001898 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001899 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001900 sp<Layer> strongRelative = weakRelative.promote();
1901 if (strongRelative != nullptr) {
1902 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001903 }
1904 }
1905
Dan Stoza412903f2017-04-27 13:42:17 -07001906 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001907 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
Robert Carr503c7042017-09-27 15:06:08 -07001908 if (childState.zOrderRelativeOf != nullptr) {
1909 continue;
1910 }
Robert Carrdb66e622017-04-10 16:55:57 -07001911 traverse.add(child);
1912 }
1913
1914 return traverse;
1915}
1916
Robert Carr1f0a16a2016-10-24 16:27:39 -07001917/**
Robert Carrdb66e622017-04-10 16:55:57 -07001918 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001919 */
Dan Stoza412903f2017-04-27 13:42:17 -07001920void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001921 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1922 // produce a new list for traversing, including our relatives, and not including our children
1923 // who are relatives of another surface. In the case that there are no relative Z,
1924 // makeTraversalList returns our children directly to avoid significant overhead.
1925 // However in this case we need to take the responsibility for filtering children which
1926 // are relatives of another surface here.
1927 bool skipRelativeZUsers = false;
1928 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001929
Robert Carr1f0a16a2016-10-24 16:27:39 -07001930 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001931 for (; i < list.size(); i++) {
1932 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001933 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1934 continue;
1935 }
1936
Robert Carrdb66e622017-04-10 16:55:57 -07001937 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001938 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001939 }
Dan Stoza412903f2017-04-27 13:42:17 -07001940 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001941 }
Robert Carr29abff82017-12-04 13:51:20 -08001942
Dan Stoza412903f2017-04-27 13:42:17 -07001943 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001944 for (; i < list.size(); i++) {
1945 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001946
Robert Carr29abff82017-12-04 13:51:20 -08001947 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1948 continue;
1949 }
Dan Stoza412903f2017-04-27 13:42:17 -07001950 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001951 }
1952}
1953
1954/**
Robert Carrdb66e622017-04-10 16:55:57 -07001955 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001956 */
Dan Stoza412903f2017-04-27 13:42:17 -07001957void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1958 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001959 // See traverseInZOrder for documentation.
1960 bool skipRelativeZUsers = false;
1961 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001962
Robert Carr1f0a16a2016-10-24 16:27:39 -07001963 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001964 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001965 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001966
1967 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1968 continue;
1969 }
1970
Robert Carrdb66e622017-04-10 16:55:57 -07001971 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001972 break;
1973 }
Dan Stoza412903f2017-04-27 13:42:17 -07001974 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001975 }
Dan Stoza412903f2017-04-27 13:42:17 -07001976 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001977 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001978 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001979
1980 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1981 continue;
1982 }
1983
Dan Stoza412903f2017-04-27 13:42:17 -07001984 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001985 }
1986}
1987
chaviw4b129c22018-04-09 16:19:43 -07001988LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1989 const std::vector<Layer*>& layersInTree) {
1990 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1991 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001992 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1993 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001994 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001995
chaviwfd462612018-05-31 16:11:27 -07001996 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001997 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1998 sp<Layer> strongRelative = weakRelative.promote();
1999 // Only add relative layers that are also descendents of the top most parent of the tree.
2000 // If a relative layer is not a descendent, then it should be ignored.
2001 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
2002 traverse.add(strongRelative);
2003 }
2004 }
2005
2006 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002007 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07002008 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
2009 // descendent of the top most parent of the tree. If it's not a descendent, then just add
2010 // the child here since it won't be added later as a relative.
2011 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
2012 childState.zOrderRelativeOf.promote().get())) {
2013 continue;
2014 }
2015 traverse.add(child);
2016 }
2017
2018 return traverse;
2019}
2020
2021void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
2022 LayerVector::StateSet stateSet,
2023 const LayerVector::Visitor& visitor) {
2024 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07002025
2026 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07002027 for (; i < list.size(); i++) {
2028 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07002029 if (relative->getZ() >= 0) {
2030 break;
2031 }
chaviw4b129c22018-04-09 16:19:43 -07002032 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07002033 }
chaviw4b129c22018-04-09 16:19:43 -07002034
chaviwa76b2712017-09-20 12:02:26 -07002035 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07002036 for (; i < list.size(); i++) {
2037 const auto& relative = list[i];
2038 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07002039 }
2040}
2041
chaviw4b129c22018-04-09 16:19:43 -07002042std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
2043 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2044 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
2045
2046 std::vector<Layer*> layersInTree = {this};
2047 for (size_t i = 0; i < children.size(); i++) {
2048 const auto& child = children[i];
2049 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
2050 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
2051 }
2052
2053 return layersInTree;
2054}
2055
2056void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
2057 const LayerVector::Visitor& visitor) {
2058 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
2059 std::sort(layersInTree.begin(), layersInTree.end());
2060 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
2061}
2062
Peiyong Linefefaac2018-08-17 12:27:51 -07002063ui::Transform Layer::getTransform() const {
2064 ui::Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07002065 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07002066 if (p != nullptr) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002067 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07002068
2069 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
2070 // it isFixedSize) then there may be additional scaling not accounted
2071 // for in the transform. We need to mirror this scaling in child surfaces
2072 // or we will break the contract where WM can treat child surfaces as
2073 // pixels in the parent surface.
David Sodman0cc69182017-11-17 12:12:07 -08002074 if (p->isFixedSize() && p->getBE().compositionInfo.mBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07002075 int bufferWidth;
2076 int bufferHeight;
2077 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
David Sodman0cc69182017-11-17 12:12:07 -08002078 bufferWidth = p->getBE().compositionInfo.mBuffer->getWidth();
2079 bufferHeight = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07002080 } else {
David Sodman0cc69182017-11-17 12:12:07 -08002081 bufferHeight = p->getBE().compositionInfo.mBuffer->getWidth();
2082 bufferWidth = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07002083 }
Marissa Wall61c58622018-07-18 10:12:20 -07002084 float sx = p->getActiveWidth(p->getDrawingState()) / static_cast<float>(bufferWidth);
2085 float sy = p->getActiveHeight(p->getDrawingState()) / static_cast<float>(bufferHeight);
Peiyong Linefefaac2018-08-17 12:27:51 -07002086 ui::Transform extraParentScaling;
Robert Carr9b429f42017-04-17 14:56:57 -07002087 extraParentScaling.set(sx, 0, 0, sy);
2088 t = t * extraParentScaling;
2089 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07002090 }
Marissa Wall61c58622018-07-18 10:12:20 -07002091 return t * getActiveTransform(getDrawingState());
Robert Carr1f0a16a2016-10-24 16:27:39 -07002092}
2093
chaviw13fdc492017-06-27 12:40:18 -07002094half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08002095 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002096
chaviw13fdc492017-06-27 12:40:18 -07002097 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
2098 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07002099}
Robert Carr6452f122017-03-21 10:41:29 -07002100
chaviw13fdc492017-06-27 12:40:18 -07002101half4 Layer::getColor() const {
2102 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002103 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07002104}
Robert Carr6452f122017-03-21 10:41:29 -07002105
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002106Layer::RoundedCornerState Layer::getRoundedCornerState() const {
2107 const auto& p = mDrawingParent.promote();
2108 if (p != nullptr) {
2109 RoundedCornerState parentState = p->getRoundedCornerState();
2110 if (parentState.radius > 0) {
2111 ui::Transform t = getActiveTransform(getDrawingState());
2112 t = t.inverse();
2113 parentState.cropRect = t.transform(parentState.cropRect);
2114 // The rounded corners shader only accepts 1 corner radius for performance reasons,
2115 // but a transform matrix can define horizontal and vertical scales.
2116 // Let's take the average between both of them and pass into the shader, practically we
2117 // never do this type of transformation on windows anyway.
2118 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
2119 return parentState;
2120 }
2121 }
2122 const float radius = getDrawingState().cornerRadius;
Vishnu Nair4351ad52019-02-11 14:13:02 -08002123 return radius > 0 ? RoundedCornerState(getBounds(), radius) : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002124}
2125
Robert Carr1f0a16a2016-10-24 16:27:39 -07002126void Layer::commitChildList() {
2127 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
2128 const auto& child = mCurrentChildren[i];
2129 child->commitChildList();
2130 }
2131 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07002132 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002133}
2134
Robert Carr720e5062018-07-30 17:45:14 -07002135void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002136 mCurrentState.inputInfo = info;
2137 mCurrentState.modified = true;
2138 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07002139 setTransactionFlags(eTransactionNeeded);
2140}
2141
chaviw1d044282017-09-27 12:19:28 -07002142void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
2143 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2144 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002145 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07002146
Peiyong Linefefaac2018-08-17 12:27:51 -07002147 ui::Transform requestedTransform = state.active_legacy.transform;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002148 ui::Transform transform = getTransform();
chaviw1d044282017-09-27 12:19:28 -07002149
2150 layerInfo->set_id(sequence);
2151 layerInfo->set_name(getName().c_str());
2152 layerInfo->set_type(String8(getTypeId()));
2153
2154 for (const auto& child : children) {
2155 layerInfo->add_children(child->sequence);
2156 }
2157
2158 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
2159 sp<Layer> strongRelative = weakRelative.promote();
2160 if (strongRelative != nullptr) {
2161 layerInfo->add_relatives(strongRelative->sequence);
2162 }
2163 }
2164
Marissa Wallf58c14b2018-07-24 10:50:43 -07002165 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
chaviw1d044282017-09-27 12:19:28 -07002166 layerInfo->mutable_transparent_region());
2167 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
2168 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
2169
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002170 layerInfo->set_layer_stack(getLayerStack());
chaviw1d044282017-09-27 12:19:28 -07002171 layerInfo->set_z(state.z);
2172
2173 PositionProto* position = layerInfo->mutable_position();
2174 position->set_x(transform.tx());
2175 position->set_y(transform.ty());
2176
2177 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
2178 requestedPosition->set_x(requestedTransform.tx());
2179 requestedPosition->set_y(requestedTransform.ty());
2180
2181 SizeProto* size = layerInfo->mutable_size();
Marissa Wallf58c14b2018-07-24 10:50:43 -07002182 size->set_w(state.active_legacy.w);
2183 size->set_h(state.active_legacy.h);
chaviw1d044282017-09-27 12:19:28 -07002184
Marissa Wallf58c14b2018-07-24 10:50:43 -07002185 LayerProtoHelper::writeToProto(state.crop_legacy, layerInfo->mutable_crop());
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002186 layerInfo->set_corner_radius(getRoundedCornerState().radius);
chaviw1d044282017-09-27 12:19:28 -07002187
2188 layerInfo->set_is_opaque(isOpaque(state));
2189 layerInfo->set_invalidate(contentDirty);
Chia-I Wu01591c92018-05-22 12:03:00 -07002190
2191 // XXX (b/79210409) mCurrentDataSpace is not protected
2192 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
2193
chaviw1d044282017-09-27 12:19:28 -07002194 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
2195 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
2196 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
2197 layerInfo->set_flags(state.flags);
2198
2199 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
2200 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
2201
Jorim Jaggi8e0af362017-11-14 16:28:28 +01002202 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07002203 if (parent != nullptr) {
2204 layerInfo->set_parent(parent->sequence);
2205 }
2206
2207 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2208 if (zOrderRelativeOf != nullptr) {
2209 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
2210 }
2211
Chia-I Wu01591c92018-05-22 12:03:00 -07002212 // XXX getBE().compositionInfo.mBuffer is not protected
David Sodman0cc69182017-11-17 12:12:07 -08002213 auto buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08002214 if (buffer != nullptr) {
2215 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
Peiyong Linefefaac2018-08-17 12:27:51 -07002216 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
Yichi Chen6ca35192018-05-29 12:20:43 +08002217 layerInfo->mutable_buffer_transform());
chaviw1d044282017-09-27 12:19:28 -07002218 }
2219
2220 layerInfo->set_queued_frames(getQueuedFrameCount());
2221 layerInfo->set_refresh_pending(isBufferLatched());
chaviwadc40c22018-07-10 16:57:27 -07002222 layerInfo->set_curr_frame(mCurrentFrameNumber);
Vishnu Nairf2deb822018-11-12 17:53:48 -08002223 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
chaviwadc40c22018-07-10 16:57:27 -07002224
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002225 for (const auto& pendingState : mPendingStates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07002226 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
chaviwadc40c22018-07-10 16:57:27 -07002227 if (barrierLayer != nullptr) {
2228 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
2229 barrierLayerProto->set_id(barrierLayer->sequence);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002230 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
chaviwadc40c22018-07-10 16:57:27 -07002231 }
2232 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002233
2234 auto protoMap = layerInfo->mutable_metadata();
2235 for (const auto& entry : state.metadata.mMap) {
2236 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
2237 }
Vishnu Nair4351ad52019-02-11 14:13:02 -08002238 LayerProtoHelper::writeToProto(mEffectiveTransform, layerInfo->mutable_effective_transform());
2239 LayerProtoHelper::writeToProto(mSourceBounds, layerInfo->mutable_source_bounds());
2240 LayerProtoHelper::writeToProto(mScreenBounds, layerInfo->mutable_screen_bounds());
2241 LayerProtoHelper::writeToProto(mBounds, layerInfo->mutable_bounds());
chaviw1d044282017-09-27 12:19:28 -07002242}
2243
Dominik Laskowski075d3172018-05-24 15:50:06 -07002244void Layer::writeToProto(LayerProto* layerInfo, DisplayId displayId) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07002245 if (!hasHwcLayer(displayId)) {
2246 return;
2247 }
2248
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002249 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
2250
Dominik Laskowski7e045462018-05-30 13:02:02 -07002251 const auto& hwcInfo = getBE().mHwcLayers.at(displayId);
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002252
2253 const Rect& frame = hwcInfo.displayFrame;
2254 LayerProtoHelper::writeToProto(frame, layerInfo->mutable_hwc_frame());
2255
2256 const FloatRect& crop = hwcInfo.sourceCrop;
2257 LayerProtoHelper::writeToProto(crop, layerInfo->mutable_hwc_crop());
2258
2259 const int32_t transform = static_cast<int32_t>(hwcInfo.transform);
2260 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002261
2262 const int32_t compositionType = static_cast<int32_t>(hwcInfo.compositionType);
2263 layerInfo->set_hwc_composition_type(compositionType);
2264
2265 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
2266 static_cast<BufferLayer*>(this)->isProtected()) {
2267 layerInfo->set_is_protected(true);
2268 } else {
2269 layerInfo->set_is_protected(false);
2270 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002271}
2272
Robert Carr2e102c92018-10-23 12:11:15 -07002273bool Layer::isRemovedFromCurrentState() const {
2274 return mRemovedFromCurrentState;
2275}
2276
Arthur Hungd20b2702019-01-14 18:16:16 +08002277InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002278 InputWindowInfo info = mDrawingState.inputInfo;
Robert Carr720e5062018-07-30 17:45:14 -07002279
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002280 if (info.displayId == ADISPLAY_ID_NONE) {
2281 info.displayId = mDrawingState.layerStack;
2282 }
2283
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002284 ui::Transform t = getTransform();
2285 const float xScale = t.sx();
2286 const float yScale = t.sy();
2287 if (xScale != 1.0f || yScale != 1.0f) {
2288 info.windowXScale *= 1.0f / xScale;
2289 info.windowYScale *= 1.0f / yScale;
2290 info.touchableRegion.scaleSelf(xScale, yScale);
Riddle Hsu39d4aa52018-11-30 20:46:53 +08002291 }
Robert Carre07e1032018-11-26 12:55:53 -08002292
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002293 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002294 // If this is a portal window, set the touchableRegion to the layerBounds.
2295 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
2296 ? getBufferSize(getDrawingState())
2297 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002298 if (!layerBounds.isValid()) {
2299 layerBounds = getCroppedBufferSize(getDrawingState());
2300 }
Vishnu Nair8033a492018-12-05 07:27:23 -08002301 layerBounds = t.transform(layerBounds);
2302 layerBounds.inset(info.surfaceInset, info.surfaceInset, info.surfaceInset, info.surfaceInset);
2303
Arthur Hungd20b2702019-01-14 18:16:16 +08002304 // Input coordinate should match the layer bounds.
2305 info.frameLeft = layerBounds.left;
2306 info.frameTop = layerBounds.top;
2307 info.frameRight = layerBounds.right;
2308 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08002309
2310 // Position the touchable region relative to frame screen location and restrict it to frame
2311 // bounds.
2312 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08002313 info.visible = canReceiveInput();
Robert Carr720e5062018-07-30 17:45:14 -07002314 return info;
2315}
2316
2317bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002318 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002319}
2320
Lloyd Piquefeb73d72018-12-04 17:23:44 -08002321std::shared_ptr<compositionengine::Layer> Layer::getCompositionLayer() const {
2322 return nullptr;
2323}
2324
chaviw3e727cd2019-01-31 13:41:05 -08002325bool Layer::canReceiveInput() const {
2326 return isVisible();
2327}
2328
Mathias Agopian13127d82013-03-05 17:47:11 -08002329// ---------------------------------------------------------------------------
2330
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002331}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002332
2333#if defined(__gl_h_)
2334#error "don't include gl/gl.h in this file"
2335#endif
2336
2337#if defined(__gl2_h_)
2338#error "don't include gl2/gl2.h in this file"
2339#endif