blob: 03b63bda619b6854c3a6faa6febab0d6f3d4e696 [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
Mathias Agopiana67932f2011-04-20 14:20:59 -070028#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070029#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070030#include <cutils/properties.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080031
32#include <utils/Errors.h>
33#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080034#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080036#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037
Courtney Goeltzenleuchter36c44dc2017-04-14 09:33:16 -060038#include <ui/DebugUtils.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070039#include <ui/GraphicBuffer.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080040#include <ui/PixelFormat.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080041
Dan Stoza6b9454d2014-11-07 16:00:59 -080042#include <gui/BufferItem.h>
Kalle Raitaa099a242017-01-11 11:17:29 -080043#include <gui/LayerDebugInfo.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080044#include <gui/Surface.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045
Yiwei Zhang60d1a192018-03-07 14:52:28 -080046#include "BufferLayer.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020047#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070048#include "DisplayDevice.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include "Layer.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070050#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070051#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053
Mathias Agopian1b031492012-06-20 17:51:20 -070054#include "DisplayHardware/HWComposer.h"
55
Peiyong Lincbc184f2018-08-22 13:24:10 -070056#include <renderengine/RenderEngine.h>
Mathias Agopian875d8e12013-06-07 15:35:48 -070057
Dan Stozac5da2712016-07-20 15:38:12 -070058#include <mutex>
chaviw1d044282017-09-27 12:19:28 -070059#include "LayerProtoHelper.h"
Dan Stozac5da2712016-07-20 15:38:12 -070060
David Sodman41fdfc92017-11-06 16:09:56 -080061#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063namespace android {
64
Mathias Agopian13127d82013-03-05 17:47:11 -080065int32_t Layer::sSequence = 1;
66
David Sodman41fdfc92017-11-06 16:09:56 -080067Layer::Layer(SurfaceFlinger* flinger, const sp<Client>& client, const String8& name, uint32_t w,
68 uint32_t h, uint32_t flags)
David Sodman0c69cad2017-08-21 12:12:51 -070069 : contentDirty(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080070 sequence(uint32_t(android_atomic_inc(&sSequence))),
71 mFlinger(flinger),
Mathias Agopian13127d82013-03-05 17:47:11 -080072 mPremultipliedAlpha(true),
David Sodman0c69cad2017-08-21 12:12:51 -070073 mName(name),
Mathias Agopian13127d82013-03-05 17:47:11 -080074 mTransactionFlags(0),
Dan Stoza7dde5992015-05-22 09:51:44 -070075 mPendingStateMutex(),
76 mPendingStates(),
Mathias Agopiana67932f2011-04-20 14:20:59 -070077 mCurrentTransform(0),
Robert Carrc3574f72016-03-24 12:19:32 -070078 mOverrideScalingMode(-1),
Dan Stozacac35382016-01-27 12:21:06 -080079 mCurrentFrameNumber(0),
Mathias Agopian82d7ab62012-01-19 18:34:40 -080080 mFrameLatencyNeeded(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080081 mNeedsFiltering(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080082 mProtectedByApp(false),
Riley Andrews03414a12014-07-01 14:22:59 -070083 mClientRef(client),
Dan Stozaa4650a52015-05-12 12:56:16 -070084 mPotentialCursor(false),
David Sodmanb8af7922017-12-21 15:17:55 -080085 mFreezeGeometryUpdates(false),
chaviwfd462612018-05-31 16:11:27 -070086 mCurrentChildren(LayerVector::StateSet::Current),
87 mDrawingChildren(LayerVector::StateSet::Drawing),
David Sodman2b727ac2017-12-21 14:28:08 -080088 mBE{this, name.string()} {
Dan Stoza9e56aa02015-11-02 13:00:03 -080089
Mathias Agopiana67932f2011-04-20 14:20:59 -070090 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070091
92 uint32_t layerFlags = 0;
David Sodman41fdfc92017-11-06 16:09:56 -080093 if (flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
94 if (flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
95 if (flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070096
Mathias Agopian4d9b8222013-03-12 17:11:48 -070097 mName = name;
Dan Stozaf7ba41a2017-05-10 15:11:11 -070098 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070099
Marissa Wallf58c14b2018-07-24 10:50:43 -0700100 mCurrentState.active_legacy.w = w;
101 mCurrentState.active_legacy.h = h;
David Sodman0c69cad2017-08-21 12:12:51 -0700102 mCurrentState.flags = layerFlags;
Marissa Wallf58c14b2018-07-24 10:50:43 -0700103 mCurrentState.active_legacy.transform.set(0, 0);
104 mCurrentState.crop_legacy.makeInvalid();
Marissa Wallf58c14b2018-07-24 10:50:43 -0700105 mCurrentState.requestedCrop_legacy = mCurrentState.crop_legacy;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700106 mCurrentState.z = 0;
chaviw13fdc492017-06-27 12:40:18 -0700107 mCurrentState.color.a = 1.0f;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700108 mCurrentState.layerStack = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700109 mCurrentState.sequence = 0;
Marissa Wallf58c14b2018-07-24 10:50:43 -0700110 mCurrentState.requested_legacy = mCurrentState.active_legacy;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500111 mCurrentState.appId = 0;
112 mCurrentState.type = 0;
Marissa Wall61c58622018-07-18 10:12:20 -0700113 mCurrentState.active.w = 0;
114 mCurrentState.active.h = 0;
115 mCurrentState.active.transform.set(0, 0);
116 mCurrentState.transform = 0;
117 mCurrentState.transformToDisplayInverse = false;
118 mCurrentState.crop.makeInvalid();
119 mCurrentState.acquireFence = new Fence(-1);
120 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
121 mCurrentState.hdrMetadata.validTypes = 0;
122 mCurrentState.surfaceDamageRegion.clear();
123 mCurrentState.api = -1;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700124
125 // drawing state & current state are identical
126 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700127
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800128 CompositorTiming compositorTiming;
129 flinger->getCompositorTiming(&compositorTiming);
130 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200131 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Dan Stoza436ccf32018-06-21 12:10:12 -0700132}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700133
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700134Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800135 sp<Client> c(mClientRef.promote());
136 if (c != 0) {
137 c->detachLayer(this);
138 }
139
140 for (auto& point : mRemoteSyncPoints) {
141 point->setTransactionApplied();
142 }
143 for (auto& point : mLocalSyncPoints) {
144 point->setFrameAvailable();
145 }
Jamie Gennis6547ff42013-07-16 20:12:42 -0700146 mFrameTracker.logAndResetStats(mName);
Mathias Agopian96f08192010-06-02 23:28:45 -0700147}
148
Mathias Agopian13127d82013-03-05 17:47:11 -0800149// ---------------------------------------------------------------------------
150// callbacks
151// ---------------------------------------------------------------------------
152
David Sodmaneb085e02017-10-05 18:49:04 -0700153/*
154 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
155 * Layer. So, the implementation is done in BufferLayer. When called on a
156 * ColorLayer object, it's essentially a NOP.
157 */
David Sodmaneb085e02017-10-05 18:49:04 -0700158void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800159
Chia-I Wuc6657022017-08-15 11:18:17 -0700160void Layer::onRemovedFromCurrentState() {
161 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
162
chaviw8b3871a2017-11-01 17:41:01 -0700163 mPendingRemoval = true;
164
Robert Carr5edb1ad2017-04-25 10:54:24 -0700165 if (mCurrentState.zOrderRelativeOf != nullptr) {
166 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
167 if (strongRelative != nullptr) {
168 strongRelative->removeZOrderRelative(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700169 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700170 }
171 mCurrentState.zOrderRelativeOf = nullptr;
172 }
173
Chia-I Wuc6657022017-08-15 11:18:17 -0700174 for (const auto& child : mCurrentChildren) {
175 child->onRemovedFromCurrentState();
176 }
177}
Chia-I Wu38512252017-05-17 14:36:16 -0700178
Chia-I Wuc6657022017-08-15 11:18:17 -0700179void Layer::onRemoved() {
180 // the layer is removed from SF mLayersPendingRemoval
David Sodmaneb085e02017-10-05 18:49:04 -0700181 abandon();
Chia-I Wuc6657022017-08-15 11:18:17 -0700182
Steven Thomasb02664d2017-07-26 18:48:28 -0700183 destroyAllHwcLayers();
Chia-I Wu38512252017-05-17 14:36:16 -0700184
Robert Carr1f0a16a2016-10-24 16:27:39 -0700185 for (const auto& child : mCurrentChildren) {
186 child->onRemoved();
187 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700188}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700189
Mathias Agopian13127d82013-03-05 17:47:11 -0800190// ---------------------------------------------------------------------------
191// set-up
192// ---------------------------------------------------------------------------
193
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700194const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800195 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800196}
197
chaviw13fdc492017-06-27 12:40:18 -0700198bool Layer::getPremultipledAlpha() const {
199 return mPremultipliedAlpha;
200}
201
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700202sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800203 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700204 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800205}
206
207// ---------------------------------------------------------------------------
208// h/w composer set-up
209// ---------------------------------------------------------------------------
210
Dominik Laskowski7e045462018-05-30 13:02:02 -0700211bool Layer::createHwcLayer(HWComposer* hwc, int32_t displayId) {
212 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.count(displayId) != 0,
213 "Already have a layer for display %d", displayId);
David Sodmanb8aaea12017-12-14 15:54:51 -0800214 auto layer = std::shared_ptr<HWC2::Layer>(
215 hwc->createLayer(displayId),
216 [hwc, displayId](HWC2::Layer* layer) {
217 hwc->destroyLayer(displayId, layer); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700218 if (!layer) {
219 return false;
220 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700221 LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers[displayId];
Steven Thomasb02664d2017-07-26 18:48:28 -0700222 hwcInfo.hwc = hwc;
223 hwcInfo.layer = layer;
David Sodmanf6a38932018-05-25 15:27:50 -0700224 layer->setLayerDestroyedListener(
Dominik Laskowski7e045462018-05-30 13:02:02 -0700225 [this, displayId](HWC2::Layer* /*layer*/) { getBE().mHwcLayers.erase(displayId); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700226 return true;
227}
228
Dominik Laskowski7e045462018-05-30 13:02:02 -0700229bool Layer::destroyHwcLayer(int32_t displayId) {
230 if (getBE().mHwcLayers.count(displayId) == 0) {
Chia-I Wu83806892017-11-16 10:50:20 -0800231 return false;
Steven Thomasb02664d2017-07-26 18:48:28 -0700232 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700233 auto& hwcInfo = getBE().mHwcLayers[displayId];
David Sodman41fdfc92017-11-06 16:09:56 -0800234 LOG_ALWAYS_FATAL_IF(hwcInfo.layer == nullptr, "Attempt to destroy null layer");
Steven Thomasb02664d2017-07-26 18:48:28 -0700235 LOG_ALWAYS_FATAL_IF(hwcInfo.hwc == nullptr, "Missing HWComposer");
David Sodmanb8aaea12017-12-14 15:54:51 -0800236 hwcInfo.layer = nullptr;
237
Chia-I Wu83806892017-11-16 10:50:20 -0800238 return true;
Steven Thomasb02664d2017-07-26 18:48:28 -0700239}
240
241void Layer::destroyAllHwcLayers() {
David Sodman6f65f3e2017-11-03 14:28:09 -0700242 size_t numLayers = getBE().mHwcLayers.size();
Steven Thomasb02664d2017-07-26 18:48:28 -0700243 for (size_t i = 0; i < numLayers; ++i) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700244 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.empty(), "destroyAllHwcLayers failed");
245 destroyHwcLayer(getBE().mHwcLayers.begin()->first);
Steven Thomasb02664d2017-07-26 18:48:28 -0700246 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700247 LOG_ALWAYS_FATAL_IF(!getBE().mHwcLayers.empty(),
David Sodman41fdfc92017-11-06 16:09:56 -0800248 "All hardware composer layers should have been destroyed");
Steven Thomasb02664d2017-07-26 18:48:28 -0700249}
Steven Thomasb02664d2017-07-26 18:48:28 -0700250
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800251Rect Layer::getContentCrop() const {
252 // this is the crop rectangle that applies to the buffer
253 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700254 Rect crop;
255 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800256 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700257 crop = mCurrentCrop;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800258 } else if (getBE().compositionInfo.mBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800259 // otherwise we use the whole buffer
David Sodman0cc69182017-11-17 12:12:07 -0800260 crop = getBE().compositionInfo.mBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700261 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800262 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700263 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700264 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700265 return crop;
266}
267
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700268static Rect reduce(const Rect& win, const Region& exclude) {
269 if (CC_LIKELY(exclude.isEmpty())) {
270 return win;
271 }
272 if (exclude.isRect()) {
273 return win.reduce(exclude.getBounds());
274 }
275 return Region(win).subtract(exclude).getBounds();
276}
277
Dan Stoza80d61162017-12-20 15:57:52 -0800278static FloatRect reduce(const FloatRect& win, const Region& exclude) {
279 if (CC_LIKELY(exclude.isEmpty())) {
280 return win;
281 }
282 // Convert through Rect (by rounding) for lack of FloatRegion
283 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
284}
285
Robert Carr1f0a16a2016-10-24 16:27:39 -0700286Rect Layer::computeScreenBounds(bool reduceTransparentRegion) const {
Alec Mourib416efd2018-09-06 21:01:59 +0000287 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700288 Rect win(getActiveWidth(s), getActiveHeight(s));
Robert Carr1f0a16a2016-10-24 16:27:39 -0700289
Marissa Wall61c58622018-07-18 10:12:20 -0700290 Rect crop = getCrop(s);
291 if (!crop.isEmpty()) {
292 win.intersect(crop, &win);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700293 }
294
Peiyong Linefefaac2018-08-17 12:27:51 -0700295 ui::Transform t = getTransform();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700296 win = t.transform(win);
297
Chia-I Wue41dbe62017-06-13 14:10:56 -0700298 const sp<Layer>& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700299 // Now we need to calculate the parent bounds, so we can clip ourselves to those.
300 // When calculating the parent bounds for purposes of clipping,
301 // we don't need to constrain the parent to its transparent region.
302 // The transparent region is an optimization based on the
303 // buffer contents of the layer, but does not affect the space allocated to
304 // it by policy, and thus children should be allowed to extend into the
305 // parent's transparent region. In fact one of the main uses, is to reduce
306 // buffer allocation size in cases where a child window sits behind a main window
307 // (by marking the hole in the parent window as a transparent region)
308 if (p != nullptr) {
309 Rect bounds = p->computeScreenBounds(false);
310 bounds.intersect(win, &win);
311 }
312
313 if (reduceTransparentRegion) {
Marissa Wall61c58622018-07-18 10:12:20 -0700314 auto const screenTransparentRegion = t.transform(getActiveTransparentRegion(s));
Robert Carr1f0a16a2016-10-24 16:27:39 -0700315 win = reduce(win, screenTransparentRegion);
316 }
317
318 return win;
319}
320
Dan Stoza80d61162017-12-20 15:57:52 -0800321FloatRect Layer::computeBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000322 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700323 return computeBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700324}
325
Dan Stoza80d61162017-12-20 15:57:52 -0800326FloatRect Layer::computeBounds(const Region& activeTransparentRegion) const {
Alec Mourib416efd2018-09-06 21:01:59 +0000327 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700328 Rect win(getActiveWidth(s), getActiveHeight(s));
Robert Carrb5d3d262016-03-25 15:08:13 -0700329
Marissa Wall61c58622018-07-18 10:12:20 -0700330 Rect crop = getCrop(s);
331 if (!crop.isEmpty()) {
332 win.intersect(crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800333 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700334
Chia-I Wue41dbe62017-06-13 14:10:56 -0700335 const auto& p = mDrawingParent.promote();
Robert Carrd4ae7f32018-06-07 16:10:57 -0700336 FloatRect floatWin = win.toFloatRect();
337 FloatRect parentBounds = floatWin;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700338 if (p != nullptr) {
Robert Carrd4ae7f32018-06-07 16:10:57 -0700339 // We pass an empty Region here for reasons mirroring that of the case described in
340 // the computeScreenBounds reduceTransparentRegion=false case.
341 parentBounds = p->computeBounds(Region());
Robert Carr1f0a16a2016-10-24 16:27:39 -0700342 }
343
Peiyong Linefefaac2018-08-17 12:27:51 -0700344 ui::Transform t = s.active_legacy.transform;
Dan Stoza80d61162017-12-20 15:57:52 -0800345
Vishnu Nairdcce0e22018-08-23 08:35:19 -0700346 if (p != nullptr) {
Dan Stoza80d61162017-12-20 15:57:52 -0800347 floatWin = t.transform(floatWin);
Robert Carrd4ae7f32018-06-07 16:10:57 -0700348 floatWin = floatWin.intersect(parentBounds);
Dan Stoza80d61162017-12-20 15:57:52 -0800349 floatWin = t.inverse().transform(floatWin);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700350 }
351
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700352 // subtract the transparent region and snap to the bounds
Dan Stoza80d61162017-12-20 15:57:52 -0800353 return reduce(floatWin, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800354}
355
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700356Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& display) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700357 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800358 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700359 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800360
361 // apply the projection's clipping to the window crop in
362 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700363 // if there are no window scaling involved, this operation will map to full
364 // pixels in the buffer.
365 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
366 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700367
Marissa Wall61c58622018-07-18 10:12:20 -0700368 Rect activeCrop(getActiveWidth(s), getActiveHeight(s));
369 Rect crop = getCrop(s);
370 if (!crop.isEmpty()) {
371 activeCrop.intersect(crop, &activeCrop);
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700372 }
373
Peiyong Linefefaac2018-08-17 12:27:51 -0700374 ui::Transform t = getTransform();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700375 activeCrop = t.transform(activeCrop);
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700376 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000377 activeCrop.clear();
378 }
chaviwb1154d12017-10-31 14:15:36 -0700379
380 const auto& p = mDrawingParent.promote();
381 if (p != nullptr) {
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700382 auto parentCrop = p->computeInitialCrop(display);
chaviwb1154d12017-10-31 14:15:36 -0700383 activeCrop.intersect(parentCrop, &activeCrop);
384 }
385
Robert Carr1f0a16a2016-10-24 16:27:39 -0700386 return activeCrop;
387}
388
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700389FloatRect Layer::computeCrop(const sp<const DisplayDevice>& display) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700390 // the content crop is the area of the content that gets scaled to the
391 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800392 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700393
394 // In addition there is a WM-specified crop we pull from our drawing state.
395 const State& s(getDrawingState());
396
397 // Screen space to make reduction to parent crop clearer.
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700398 Rect activeCrop = computeInitialCrop(display);
Peiyong Linefefaac2018-08-17 12:27:51 -0700399 ui::Transform t = getTransform();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700400 // Back to layer space to work with the content crop.
401 activeCrop = t.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800402
Michael Lentine28ea2172014-11-19 18:32:37 -0800403 // This needs to be here as transform.transform(Rect) computes the
404 // transformed rect and then takes the bounding box of the result before
405 // returning. This means
406 // transform.inverse().transform(transform.transform(Rect)) != Rect
407 // in which case we need to make sure the final rect is clipped to the
408 // display bounds.
Marissa Wall61c58622018-07-18 10:12:20 -0700409 if (!activeCrop.intersect(Rect(getActiveWidth(s), getActiveHeight(s)), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000410 activeCrop.clear();
411 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800412
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700413 // subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700414 activeCrop = reduce(activeCrop, getActiveTransparentRegion(s));
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700415
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000416 // Transform the window crop to match the buffer coordinate system,
417 // which means using the inverse of the current transform set on the
418 // SurfaceFlingerConsumer.
419 uint32_t invTransform = mCurrentTransform;
Robert Carrcae605c2017-03-29 12:10:31 -0700420 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000421 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700422 * the code below applies the primary display's inverse transform to the
423 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000424 */
David Sodman41fdfc92017-11-06 16:09:56 -0800425 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000426 // calculate the inverse transform
427 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800428 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800429 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000430 // and apply to the current transform
Peiyong Linefefaac2018-08-17 12:27:51 -0700431 invTransform = (ui::Transform(invTransformOrient) *
432 ui::Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800433 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000434
Marissa Wall61c58622018-07-18 10:12:20 -0700435 int winWidth = getActiveWidth(s);
436 int winHeight = getActiveHeight(s);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000437 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
438 // If the activeCrop has been rotate the ends are rotated but not
439 // the space itself so when transforming ends back we can't rely on
440 // a modification of the axes of rotation. To account for this we
441 // need to reorient the inverse rotation in terms of the current
442 // axes of rotation.
443 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
444 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
445 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800446 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000447 }
Marissa Wall61c58622018-07-18 10:12:20 -0700448 winWidth = getActiveHeight(s);
449 winHeight = getActiveWidth(s);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000450 }
Marissa Wall61c58622018-07-18 10:12:20 -0700451 const Rect winCrop = activeCrop.transform(invTransform, getActiveWidth(s), getActiveHeight(s));
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000452
453 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800454 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000455 float yScale = crop.getHeight() / float(winHeight);
456
David Sodman41fdfc92017-11-06 16:09:56 -0800457 float insetL = winCrop.left * xScale;
458 float insetT = winCrop.top * yScale;
459 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000460 float insetB = (winHeight - winCrop.bottom) * yScale;
461
David Sodman41fdfc92017-11-06 16:09:56 -0800462 crop.left += insetL;
463 crop.top += insetT;
464 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000465 crop.bottom -= insetB;
466
Mathias Agopian13127d82013-03-05 17:47:11 -0800467 return crop;
468}
469
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700470void Layer::setGeometry(const sp<const DisplayDevice>& display, uint32_t z) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700471 const auto displayId = display->getId();
Peiyong Lin91b1df22018-06-18 18:00:16 -0700472 if (!hasHwcLayer(displayId)) {
473 ALOGE("[%s] failed to setGeometry: no HWC layer found (%d)",
474 mName.string(), displayId);
475 return;
476 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700477 auto& hwcInfo = getBE().mHwcLayers[displayId];
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700478
David Sodman10a41ff2018-08-05 12:14:17 -0700479 // Device or Cursor layers
480 if (mPotentialCursor) {
481 ALOGV("[%s] Requesting Cursor composition", mName.string());
482 setCompositionType(displayId, HWC2::Composition::Cursor);
483 } else {
484 ALOGV("[%s] Requesting Device composition", mName.string());
485 setCompositionType(displayId, HWC2::Composition::Device);
486 }
487
488 // Need to program geometry parts
489 getBE().compositionInfo.hwc.skipGeometry = false;
490
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700491 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800492 hwcInfo.forceClientComposition = false;
493
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700494 if (isSecure() && !display->isSecure()) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800495 hwcInfo.forceClientComposition = true;
496 }
497
Mathias Agopian13127d82013-03-05 17:47:11 -0800498 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700499 const State& s(getDrawingState());
David Revemanecf0fa52017-03-03 11:32:44 -0500500 auto blendMode = HWC2::BlendMode::None;
Robert Carr6452f122017-03-21 10:41:29 -0700501 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800502 blendMode =
503 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800504 }
David Sodmanba340492018-08-05 21:51:33 -0700505 getBE().compositionInfo.hwc.blendMode = blendMode;
Mathias Agopian13127d82013-03-05 17:47:11 -0800506
507 // apply the layer's transform, followed by the display's global transform
508 // here we're guaranteed that the layer's transform preserves rects
Marissa Wall61c58622018-07-18 10:12:20 -0700509 Region activeTransparentRegion(getActiveTransparentRegion(s));
Peiyong Linefefaac2018-08-17 12:27:51 -0700510 ui::Transform t = getTransform();
Marissa Wall61c58622018-07-18 10:12:20 -0700511 Rect activeCrop = getCrop(s);
512 if (!activeCrop.isEmpty()) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700513 activeCrop = t.transform(activeCrop);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700514 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000515 activeCrop.clear();
516 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700517 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800518 // This needs to be here as transform.transform(Rect) computes the
519 // transformed rect and then takes the bounding box of the result before
520 // returning. This means
521 // transform.inverse().transform(transform.transform(Rect)) != Rect
522 // in which case we need to make sure the final rect is clipped to the
523 // display bounds.
Marissa Wall61c58622018-07-18 10:12:20 -0700524 if (!activeCrop.intersect(Rect(getActiveWidth(s), getActiveHeight(s)), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000525 activeCrop.clear();
526 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700527 // mark regions outside the crop as transparent
Marissa Wall61c58622018-07-18 10:12:20 -0700528 activeTransparentRegion.orSelf(Rect(0, 0, getActiveWidth(s), activeCrop.top));
Marissa Wallf58c14b2018-07-24 10:50:43 -0700529 activeTransparentRegion.orSelf(
Marissa Wall61c58622018-07-18 10:12:20 -0700530 Rect(0, activeCrop.bottom, getActiveWidth(s), getActiveHeight(s)));
David Sodman41fdfc92017-11-06 16:09:56 -0800531 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
532 activeTransparentRegion.orSelf(
Marissa Wall61c58622018-07-18 10:12:20 -0700533 Rect(activeCrop.right, activeCrop.top, getActiveWidth(s), activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700534 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700535
Dan Stoza80d61162017-12-20 15:57:52 -0800536 // computeBounds returns a FloatRect to provide more accuracy during the
537 // transformation. We then round upon constructing 'frame'.
538 Rect frame{t.transform(computeBounds(activeTransparentRegion))};
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700539 if (!frame.intersect(display->getViewport(), &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000540 frame.clear();
541 }
Peiyong Linefefaac2018-08-17 12:27:51 -0700542 const ui::Transform& tr = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800543 Rect transformedFrame = tr.transform(frame);
David Sodmanba340492018-08-05 21:51:33 -0700544 getBE().compositionInfo.hwc.displayFrame = transformedFrame;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800545
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700546 FloatRect sourceCrop = computeCrop(display);
David Sodmanba340492018-08-05 21:51:33 -0700547 getBE().compositionInfo.hwc.sourceCrop = sourceCrop;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800548
chaviw13fdc492017-06-27 12:40:18 -0700549 float alpha = static_cast<float>(getAlpha());
David Sodmanba340492018-08-05 21:51:33 -0700550 getBE().compositionInfo.hwc.alpha = alpha;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800551
David Sodmanba340492018-08-05 21:51:33 -0700552 getBE().compositionInfo.hwc.z = z;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500553
Albert Chaulk2a589632017-05-04 16:59:44 -0400554 int type = s.type;
555 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700556 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400557 if (parent.get()) {
558 auto& parentState = parent->getDrawingState();
rongliucfb187b2018-03-14 12:26:23 -0700559 if (parentState.type >= 0 || parentState.appId >= 0) {
560 type = parentState.type;
561 appId = parentState.appId;
562 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400563 }
564
David Sodmanba340492018-08-05 21:51:33 -0700565 getBE().compositionInfo.hwc.type = type;
566 getBE().compositionInfo.hwc.appId = appId;
567
Mathias Agopian29a367b2011-07-12 14:51:45 -0700568 /*
569 * Transformations are applied in this order:
570 * 1) buffer orientation/flip/mirror
571 * 2) state transformation (window manager)
572 * 3) layer orientation (screen orientation)
573 * (NOTE: the matrices are multiplied in reverse order)
574 */
575
Peiyong Linefefaac2018-08-17 12:27:51 -0700576 const ui::Transform bufferOrientation(mCurrentTransform);
577 ui::Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700578
Robert Carrcae605c2017-03-29 12:10:31 -0700579 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700580 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700581 * the code below applies the primary display's inverse transform to the
582 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700583 */
David Sodman41fdfc92017-11-06 16:09:56 -0800584 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700585 // calculate the inverse transform
586 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800587 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700588 }
Robert Carrcae605c2017-03-29 12:10:31 -0700589
590 /*
591 * Here we cancel out the orientation component of the WM transform.
592 * The scaling and translate components are already included in our bounds
593 * computation so it's enough to just omit it in the composition.
594 * See comment in onDraw with ref to b/36727915 for why.
595 */
Peiyong Linefefaac2018-08-17 12:27:51 -0700596 transform = ui::Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700597 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700598
599 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800600 const uint32_t orientation = transform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -0700601 if (orientation & ui::Transform::ROT_INVALID) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800602 // we can only handle simple transformation
Lloyd Pique074e8122018-07-26 12:57:23 -0700603 hwcInfo.forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800604 } else {
605 auto transform = static_cast<HWC2::Transform>(orientation);
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800606 hwcInfo.transform = transform;
David Sodmanba340492018-08-05 21:51:33 -0700607 getBE().compositionInfo.hwc.transform = transform;
David Sodman4b7c4bc2017-11-17 12:13:59 -0800608 }
609}
610
Dominik Laskowski7e045462018-05-30 13:02:02 -0700611void Layer::forceClientComposition(int32_t displayId) {
612 if (getBE().mHwcLayers.count(displayId) == 0) {
613 ALOGE("forceClientComposition: no HWC layer found (%d)", displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800614 return;
615 }
616
Dominik Laskowski7e045462018-05-30 13:02:02 -0700617 getBE().mHwcLayers[displayId].forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800618}
Dan Stozaee44edd2015-03-23 15:50:23 -0700619
Dominik Laskowski7e045462018-05-30 13:02:02 -0700620bool Layer::getForceClientComposition(int32_t displayId) {
621 if (getBE().mHwcLayers.count(displayId) == 0) {
622 ALOGE("getForceClientComposition: no HWC layer found (%d)", displayId);
chaviwc9232ed2017-11-14 15:31:15 -0800623 return false;
624 }
625
Dominik Laskowski7e045462018-05-30 13:02:02 -0700626 return getBE().mHwcLayers[displayId].forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800627}
628
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700629void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700630 const auto displayId = display->getId();
631 if (getBE().mHwcLayers.count(displayId) == 0 ||
632 getCompositionType(displayId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800633 return;
634 }
635
636 // This gives us only the "orientation" component of the transform
637 const State& s(getCurrentState());
638
639 // Apply the layer's transform, followed by the display's global transform
640 // Here we're guaranteed that the layer's transform preserves rects
Marissa Wall61c58622018-07-18 10:12:20 -0700641 Rect win(getActiveWidth(s), getActiveHeight(s));
642 Rect crop = getCrop(s);
643 if (!crop.isEmpty()) {
644 win.intersect(crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800645 }
646 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700647 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Robert Carr1f0a16a2016-10-24 16:27:39 -0700648 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700649 frame.intersect(display->getViewport(), &frame);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700650 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800651 auto position = displayTransform.transform(frame);
652
Dominik Laskowski7e045462018-05-30 13:02:02 -0700653 auto error =
David Sodmanb8aaea12017-12-14 15:54:51 -0800654 (getBE().mHwcLayers[displayId].layer)->setCursorPosition(
655 position.left, position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800656 ALOGE_IF(error != HWC2::Error::None,
657 "[%s] Failed to set cursor position "
658 "to (%d, %d): %s (%d)",
659 mName.string(), position.left, position.top, to_string(error).c_str(),
660 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800661}
Riley Andrews03414a12014-07-01 14:22:59 -0700662
Mathias Agopian13127d82013-03-05 17:47:11 -0800663// ---------------------------------------------------------------------------
664// drawing...
665// ---------------------------------------------------------------------------
666
Marissa Wall61c58622018-07-18 10:12:20 -0700667void Layer::draw(const RenderArea& renderArea, const Region& clip) {
chaviwa76b2712017-09-20 12:02:26 -0700668 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800669}
670
Marissa Wall61c58622018-07-18 10:12:20 -0700671void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) {
chaviwa76b2712017-09-20 12:02:26 -0700672 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800673}
674
David Sodman41fdfc92017-11-06 16:09:56 -0800675void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
676 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800677 auto& engine(mFlinger->getRenderEngine());
David Sodman9eeae692017-11-02 10:53:32 -0700678 computeGeometry(renderArea, getBE().mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700679 engine.setupFillWithColor(red, green, blue, alpha);
David Sodman9eeae692017-11-02 10:53:32 -0700680 engine.drawMesh(getBE().mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800681}
682
David Sodmanc1498e62018-09-12 14:36:26 -0700683void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
684 clearWithOpenGL(renderArea, 0, 0, 0, 0);
685}
686
David Sodman10a41ff2018-08-05 12:14:17 -0700687void Layer::setCompositionType(int32_t displayId, HWC2::Composition type, bool /*callIntoHwc*/) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700688 if (getBE().mHwcLayers.count(displayId) == 0) {
Chia-I Wu30505fb2018-03-26 16:20:31 -0700689 ALOGE("setCompositionType called without a valid HWC layer");
690 return;
David Sodman10a41ff2018-08-05 12:14:17 -0700691 } else {
692 if (getBE().mHwcLayers[displayId].compositionType != type) {
693 ALOGV("setCompositionType: Changing compositionType from %s to %s",
694 to_string(getBE().mHwcLayers[displayId].compositionType).c_str(),
695 to_string(type).c_str());
696 getBE().mHwcLayers[displayId].compositionType = type;
Chia-I Wu30505fb2018-03-26 16:20:31 -0700697 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800698 }
699}
700
Dominik Laskowski7e045462018-05-30 13:02:02 -0700701HWC2::Composition Layer::getCompositionType(int32_t displayId) const {
David Sodman15fb96e2018-01-07 10:23:24 -0800702 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700703 // If we're querying the composition type for a display that does not
704 // have a HWC counterpart, then it will always be Client
705 return HWC2::Composition::Client;
706 }
David Sodman15fb96e2018-01-07 10:23:24 -0800707 return getBE().mHwcLayers[displayId].compositionType;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800708}
709
Dominik Laskowski7e045462018-05-30 13:02:02 -0700710void Layer::setClearClientTarget(int32_t displayId, bool clear) {
711 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800712 ALOGE("setClearClientTarget called without a valid HWC layer");
713 return;
714 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700715 getBE().mHwcLayers[displayId].clearClientTarget = clear;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800716}
717
Dominik Laskowski7e045462018-05-30 13:02:02 -0700718bool Layer::getClearClientTarget(int32_t displayId) const {
719 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800720 ALOGE("getClearClientTarget called without a valid HWC layer");
721 return false;
722 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700723 return getBE().mHwcLayers.at(displayId).clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800724}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800725
Dan Stozacac35382016-01-27 12:21:06 -0800726bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
727 if (point->getFrameNumber() <= mCurrentFrameNumber) {
728 // Don't bother with a SyncPoint, since we've already latched the
729 // relevant frame
730 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700731 }
732
Dan Stozacac35382016-01-27 12:21:06 -0800733 Mutex::Autolock lock(mLocalSyncPointMutex);
734 mLocalSyncPoints.push_back(point);
735 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700736}
737
Mathias Agopian13127d82013-03-05 17:47:11 -0800738// ----------------------------------------------------------------------------
739// local state
740// ----------------------------------------------------------------------------
741
Peiyong Lin833074a2018-08-28 11:53:54 -0700742void Layer::computeGeometry(const RenderArea& renderArea,
743 renderengine::Mesh& mesh,
chaviwa76b2712017-09-20 12:02:26 -0700744 bool useIdentityTransform) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700745 const ui::Transform renderAreaTransform(renderArea.getTransform());
Dan Stoza80d61162017-12-20 15:57:52 -0800746 FloatRect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700747
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000748 vec2 lt = vec2(win.left, win.top);
749 vec2 lb = vec2(win.left, win.bottom);
750 vec2 rb = vec2(win.right, win.bottom);
751 vec2 rt = vec2(win.right, win.top);
752
Peiyong Linefefaac2018-08-17 12:27:51 -0700753 ui::Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000754 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700755 lt = layerTransform.transform(lt);
756 lb = layerTransform.transform(lb);
757 rb = layerTransform.transform(rb);
758 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000759 }
760
Peiyong Lin833074a2018-08-28 11:53:54 -0700761 renderengine::Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700762 position[0] = renderAreaTransform.transform(lt);
763 position[1] = renderAreaTransform.transform(lb);
764 position[2] = renderAreaTransform.transform(rb);
765 position[3] = renderAreaTransform.transform(rt);
Mathias Agopian13127d82013-03-05 17:47:11 -0800766}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800767
David Sodman41fdfc92017-11-06 16:09:56 -0800768bool Layer::isSecure() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000769 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700770 return (s.flags & layer_state_t::eLayerSecure);
771}
772
Mathias Agopian13127d82013-03-05 17:47:11 -0800773void Layer::setVisibleRegion(const Region& visibleRegion) {
774 // always called from main thread
775 this->visibleRegion = visibleRegion;
776}
777
778void Layer::setCoveredRegion(const Region& coveredRegion) {
779 // always called from main thread
780 this->coveredRegion = coveredRegion;
781}
782
David Sodman41fdfc92017-11-06 16:09:56 -0800783void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800784 // always called from main thread
785 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
786}
787
Robert Carre5f4f692018-01-12 13:12:28 -0800788void Layer::clearVisibilityRegions() {
789 visibleRegion.clear();
790 visibleNonTransparentRegion.clear();
791 coveredRegion.clear();
792}
793
Mathias Agopian13127d82013-03-05 17:47:11 -0800794// ----------------------------------------------------------------------------
795// transaction
796// ----------------------------------------------------------------------------
797
Dan Stoza7dde5992015-05-22 09:51:44 -0700798void Layer::pushPendingState() {
799 if (!mCurrentState.modified) {
800 return;
801 }
802
Dan Stoza7dde5992015-05-22 09:51:44 -0700803 // If this transaction is waiting on the receipt of a frame, generate a sync
804 // point and send it to the remote layer.
Marissa Wallf58c14b2018-07-24 10:50:43 -0700805 if (mCurrentState.barrierLayer_legacy != nullptr) {
806 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800807 if (barrierLayer == nullptr) {
808 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700809 // If we can't promote the layer we are intended to wait on,
810 // then it is expired or otherwise invalid. Allow this transaction
811 // to be applied as per normal (no synchronization).
Marissa Wallf58c14b2018-07-24 10:50:43 -0700812 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800813 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700814 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy);
Robert Carr0d480722017-01-10 16:42:54 -0800815 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800816 mRemoteSyncPoints.push_back(std::move(syncPoint));
817 } else {
818 // We already missed the frame we're supposed to synchronize
819 // on, so go ahead and apply the state update
Marissa Wallf58c14b2018-07-24 10:50:43 -0700820 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800821 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700822 }
823
Dan Stoza7dde5992015-05-22 09:51:44 -0700824 // Wake us up to check if the frame has been received
825 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700826 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700827 }
828 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700829 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700830}
831
Pablo Ceballos05289c22016-04-14 15:49:55 -0700832void Layer::popPendingState(State* stateToCommit) {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700833 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700834
835 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700836 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700837}
838
Pablo Ceballos05289c22016-04-14 15:49:55 -0700839bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700840 bool stateUpdateAvailable = false;
841 while (!mPendingStates.empty()) {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700842 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700843 if (mRemoteSyncPoints.empty()) {
844 // If we don't have a sync point for this, apply it anyway. It
845 // will be visually wrong, but it should keep us from getting
846 // into too much trouble.
847 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700848 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700849 stateUpdateAvailable = true;
850 continue;
851 }
852
Marissa Wallf58c14b2018-07-24 10:50:43 -0700853 if (mRemoteSyncPoints.front()->getFrameNumber() !=
854 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800855 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800856
857 // Signal our end of the sync point and then dispose of it
858 mRemoteSyncPoints.front()->setTransactionApplied();
859 mRemoteSyncPoints.pop_front();
860 continue;
861 }
862
Dan Stoza7dde5992015-05-22 09:51:44 -0700863 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
864 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700865 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700866 stateUpdateAvailable = true;
867
868 // Signal our end of the sync point and then dispose of it
869 mRemoteSyncPoints.front()->setTransactionApplied();
870 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800871 } else {
872 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700873 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700874 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700875 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700876 stateUpdateAvailable = true;
877 }
878 }
879
880 // If we still have pending updates, wake SurfaceFlinger back up and point
881 // it at this layer so we can process them
882 if (!mPendingStates.empty()) {
883 setTransactionFlags(eTransactionNeeded);
884 mFlinger->setTransactionFlags(eTraversalNeeded);
885 }
886
887 mCurrentState.modified = false;
888 return stateUpdateAvailable;
889}
890
Marissa Wall61c58622018-07-18 10:12:20 -0700891uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000892 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800893
Marissa Wall61c58622018-07-18 10:12:20 -0700894 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
895 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700896
David Sodmaneb085e02017-10-05 18:49:04 -0700897 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700898 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000899 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800900 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
901 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
902 " requested={ wh={%4u,%4u} }}\n"
903 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
904 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700905 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700906 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
907 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
908 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
909 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
910 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700911 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
912 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
913 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800914 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700915
Robert Carre392b552017-09-19 12:16:05 -0700916 // Don't let Layer::doTransaction update the drawing state
917 // if we have a pending resize, unless we are in fixed-size mode.
918 // the drawing state will be updated only once we receive a buffer
919 // with the correct size.
920 //
921 // In particular, we want to make sure the clip (which is part
922 // of the geometry state) is latched together with the size but is
923 // latched immediately when no resizing is involved.
924 //
925 // If a sideband stream is attached, however, we want to skip this
926 // optimization so that transactions aren't missed when a buffer
927 // never arrives
928 //
929 // In the case that we don't have a buffer we ignore other factors
930 // and avoid entering the resizePending state. At a high level the
931 // resizePending state is to avoid applying the state of the new buffer
932 // to the old buffer. However in the state where we don't have an old buffer
933 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700934 const bool resizePending =
935 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
936 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
David Sodman0cc69182017-11-17 12:12:07 -0800937 (getBE().compositionInfo.mBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700938 if (!isFixedSize()) {
David Sodman0cc69182017-11-17 12:12:07 -0800939 if (resizePending && getBE().compositionInfo.hwc.sidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700940 flags |= eDontUpdateGeometryState;
941 }
942 }
943
Robert Carr7bf247e2017-05-18 14:02:49 -0700944 // Here we apply various requested geometry states, depending on our
945 // latching configuration. See Layer.h for a detailed discussion of
946 // how geometry latching is controlled.
947 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000948 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700949
950 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
951 // mode, which causes attributes which normally latch regardless of scaling mode,
952 // to be delayed. We copy the requested state to the active state making sure
953 // to respect these rules (again see Layer.h for a detailed discussion).
954 //
955 // There is an awkward asymmetry in the handling of the crop states in the position
956 // states, as can be seen below. Largely this arises from position and transform
957 // being stored in the same data structure while having different latching rules.
958 // b/38182305
959 //
Marissa Wall61c58622018-07-18 10:12:20 -0700960 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700961 // applyPendingStates in the presence of deferred transactions.
962 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -0700963 float tx = stateToCommit->active_legacy.transform.tx();
964 float ty = stateToCommit->active_legacy.transform.ty();
965 stateToCommit->active_legacy = stateToCommit->requested_legacy;
966 stateToCommit->active_legacy.transform.set(tx, ty);
967 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700968 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700969 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -0700970 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700971 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800972 }
973
Marissa Wall61c58622018-07-18 10:12:20 -0700974 return flags;
975}
976
977uint32_t Layer::doTransaction(uint32_t flags) {
978 ATRACE_CALL();
979
980 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +0000981 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -0700982 if (!applyPendingStates(&c)) {
983 return 0;
984 }
985
986 flags = doTransactionResize(flags, &c);
987
Alec Mourib416efd2018-09-06 21:01:59 +0000988 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700989
990 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800991 // invalidate and recompute the visible regions if needed
992 flags |= Layer::eVisibleRegion;
993 }
994
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700995 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800996 // invalidate and recompute the visible regions if needed
997 flags |= eVisibleRegion;
998 this->contentDirty = true;
999
1000 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -07001001 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -07001002 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -08001003 }
1004
Dan Stozac8145172016-04-28 16:29:06 -07001005 // If the layer is hidden, signal and clear out all local sync points so
1006 // that transactions for layers depending on this layer's frames becoming
1007 // visible are not blocked
1008 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001009 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001010 }
1011
Mathias Agopian13127d82013-03-05 17:47:11 -08001012 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001013 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001014 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001015}
1016
Pablo Ceballos05289c22016-04-14 15:49:55 -07001017void Layer::commitTransaction(const State& stateToCommit) {
1018 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001019}
1020
Mathias Agopian13127d82013-03-05 17:47:11 -08001021uint32_t Layer::getTransactionFlags(uint32_t flags) {
1022 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1023}
1024
1025uint32_t Layer::setTransactionFlags(uint32_t flags) {
1026 return android_atomic_or(flags, &mTransactionFlags);
1027}
1028
Robert Carr82364e32016-05-15 11:27:47 -07001029bool Layer::setPosition(float x, float y, bool immediate) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001030 if (mCurrentState.requested_legacy.transform.tx() == x &&
1031 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001032 return false;
1033 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001034
1035 // We update the requested and active position simultaneously because
1036 // we want to apply the position portion of the transform matrix immediately,
1037 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Marissa Wallf58c14b2018-07-24 10:50:43 -07001038 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001039 if (immediate && !mFreezeGeometryUpdates) {
1040 // Here we directly update the active state
1041 // unlike other setters, because we store it within
1042 // the transform, but use different latching rules.
1043 // b/38182305
Marissa Wallf58c14b2018-07-24 10:50:43 -07001044 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001045 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001046 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001047
Dan Stoza7dde5992015-05-22 09:51:44 -07001048 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001049 setTransactionFlags(eTransactionNeeded);
1050 return true;
1051}
Robert Carr82364e32016-05-15 11:27:47 -07001052
Robert Carr1f0a16a2016-10-24 16:27:39 -07001053bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1054 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1055 if (idx < 0) {
1056 return false;
1057 }
1058 if (childLayer->setLayer(z)) {
1059 mCurrentChildren.removeAt(idx);
1060 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001061 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001062 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001063 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001064}
1065
Robert Carr503c7042017-09-27 15:06:08 -07001066bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1067 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1068 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1069 if (idx < 0) {
1070 return false;
1071 }
1072 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1073 mCurrentChildren.removeAt(idx);
1074 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001075 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001076 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001077 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001078}
1079
Robert Carrae060832016-11-28 10:51:00 -08001080bool Layer::setLayer(int32_t z) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001081 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001082 mCurrentState.sequence++;
1083 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001084 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001085
1086 // Discard all relative layering.
1087 if (mCurrentState.zOrderRelativeOf != nullptr) {
1088 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1089 if (strongRelative != nullptr) {
1090 strongRelative->removeZOrderRelative(this);
1091 }
1092 mCurrentState.zOrderRelativeOf = nullptr;
1093 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001094 setTransactionFlags(eTransactionNeeded);
1095 return true;
1096}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001097
Robert Carrdb66e622017-04-10 16:55:57 -07001098void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1099 mCurrentState.zOrderRelatives.remove(relative);
1100 mCurrentState.sequence++;
1101 mCurrentState.modified = true;
1102 setTransactionFlags(eTransactionNeeded);
1103}
1104
1105void Layer::addZOrderRelative(const wp<Layer>& relative) {
1106 mCurrentState.zOrderRelatives.add(relative);
1107 mCurrentState.modified = true;
1108 mCurrentState.sequence++;
1109 setTransactionFlags(eTransactionNeeded);
1110}
1111
Robert Carr503d2bd2017-12-04 15:49:47 -08001112bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001113 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1114 if (handle == nullptr) {
1115 return false;
1116 }
1117 sp<Layer> relative = handle->owner.promote();
1118 if (relative == nullptr) {
1119 return false;
1120 }
1121
Robert Carr503d2bd2017-12-04 15:49:47 -08001122 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1123 mCurrentState.zOrderRelativeOf == relative) {
1124 return false;
1125 }
1126
Robert Carrdb66e622017-04-10 16:55:57 -07001127 mCurrentState.sequence++;
1128 mCurrentState.modified = true;
Robert Carr503d2bd2017-12-04 15:49:47 -08001129 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001130
chaviw9ab4bd12017-11-03 13:11:00 -07001131 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
1132 if (oldZOrderRelativeOf != nullptr) {
1133 oldZOrderRelativeOf->removeZOrderRelative(this);
1134 }
Robert Carrdb66e622017-04-10 16:55:57 -07001135 mCurrentState.zOrderRelativeOf = relative;
1136 relative->addZOrderRelative(this);
1137
1138 setTransactionFlags(eTransactionNeeded);
1139
1140 return true;
1141}
1142
Mathias Agopian13127d82013-03-05 17:47:11 -08001143bool Layer::setSize(uint32_t w, uint32_t h) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001144 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
1145 return false;
1146 mCurrentState.requested_legacy.w = w;
1147 mCurrentState.requested_legacy.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001148 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001149 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001150
1151 // record the new size, from this point on, when the client request
1152 // a buffer, it'll get the new size.
1153 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001154 return true;
1155}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001156bool Layer::setAlpha(float alpha) {
David Sodman41fdfc92017-11-06 16:09:56 -08001157 if (mCurrentState.color.a == alpha) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001158 mCurrentState.sequence++;
chaviw13fdc492017-06-27 12:40:18 -07001159 mCurrentState.color.a = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001160 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001161 setTransactionFlags(eTransactionNeeded);
1162 return true;
1163}
chaviw13fdc492017-06-27 12:40:18 -07001164
1165bool Layer::setColor(const half3& color) {
David Sodman41fdfc92017-11-06 16:09:56 -08001166 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g &&
1167 color.b == mCurrentState.color.b)
chaviw13fdc492017-06-27 12:40:18 -07001168 return false;
1169
1170 mCurrentState.sequence++;
1171 mCurrentState.color.r = color.r;
1172 mCurrentState.color.g = color.g;
1173 mCurrentState.color.b = color.b;
1174 mCurrentState.modified = true;
1175 setTransactionFlags(eTransactionNeeded);
1176 return true;
1177}
1178
Robert Carrd4ae7f32018-06-07 16:10:57 -07001179bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1180 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001181 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001182 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1183
1184 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1185 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1186 return false;
1187 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001188 mCurrentState.sequence++;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001189 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1190 matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001191 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001192 setTransactionFlags(eTransactionNeeded);
1193 return true;
1194}
Marissa Wall61c58622018-07-18 10:12:20 -07001195
Mathias Agopian13127d82013-03-05 17:47:11 -08001196bool Layer::setTransparentRegionHint(const Region& transparent) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001197 mCurrentState.requestedTransparentRegion_legacy = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001198 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001199 setTransactionFlags(eTransactionNeeded);
1200 return true;
1201}
1202bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1203 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
David Sodman41fdfc92017-11-06 16:09:56 -08001204 if (mCurrentState.flags == newFlags) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001205 mCurrentState.sequence++;
1206 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001207 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001208 setTransactionFlags(eTransactionNeeded);
1209 return true;
1210}
Robert Carr99e27f02016-06-16 15:18:02 -07001211
Marissa Wallf58c14b2018-07-24 10:50:43 -07001212bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
1213 if (mCurrentState.requestedCrop_legacy == crop) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001214 mCurrentState.sequence++;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001215 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001216 if (immediate && !mFreezeGeometryUpdates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001217 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001218 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001219 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1220
Dan Stoza7dde5992015-05-22 09:51:44 -07001221 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001222 setTransactionFlags(eTransactionNeeded);
1223 return true;
1224}
Robert Carr8d5227b2017-03-16 15:41:03 -07001225
Robert Carrc3574f72016-03-24 12:19:32 -07001226bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001227 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001228 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001229 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001230 return true;
1231}
1232
rongliucfb187b2018-03-14 12:26:23 -07001233void Layer::setInfo(int32_t type, int32_t appId) {
David Sodman41fdfc92017-11-06 16:09:56 -08001234 mCurrentState.appId = appId;
1235 mCurrentState.type = type;
1236 mCurrentState.modified = true;
1237 setTransactionFlags(eTransactionNeeded);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001238}
1239
Mathias Agopian13127d82013-03-05 17:47:11 -08001240bool Layer::setLayerStack(uint32_t layerStack) {
David Sodman41fdfc92017-11-06 16:09:56 -08001241 if (mCurrentState.layerStack == layerStack) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001242 mCurrentState.sequence++;
1243 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001244 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001245 setTransactionFlags(eTransactionNeeded);
1246 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001247}
1248
Robert Carr1f0a16a2016-10-24 16:27:39 -07001249uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001250 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001251 if (p == nullptr) {
1252 return getDrawingState().layerStack;
1253 }
1254 return p->getLayerStack();
1255}
1256
Marissa Wallf58c14b2018-07-24 10:50:43 -07001257void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
1258 mCurrentState.barrierLayer_legacy = barrierLayer;
1259 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001260 // We don't set eTransactionNeeded, because just receiving a deferral
1261 // request without any other state updates shouldn't actually induce a delay
1262 mCurrentState.modified = true;
1263 pushPendingState();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001264 mCurrentState.barrierLayer_legacy = nullptr;
1265 mCurrentState.frameNumber_legacy = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001266 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001267}
1268
Marissa Wallf58c14b2018-07-24 10:50:43 -07001269void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001270 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001271 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001272}
1273
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001274// ----------------------------------------------------------------------------
1275// pageflip handling...
1276// ----------------------------------------------------------------------------
1277
Robert Carr1f0a16a2016-10-24 16:27:39 -07001278bool Layer::isHiddenByPolicy() const {
Alec Mourib416efd2018-09-06 21:01:59 +00001279 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001280 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001281 if (parent != nullptr && parent->isHiddenByPolicy()) {
1282 return true;
1283 }
1284 return s.flags & layer_state_t::eLayerHidden;
1285}
1286
David Sodman41fdfc92017-11-06 16:09:56 -08001287uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001288 // TODO: should we do something special if mSecure is set?
1289 if (mProtectedByApp) {
1290 // need a hardware-protected path to external video sink
1291 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001292 }
Riley Andrews03414a12014-07-01 14:22:59 -07001293 if (mPotentialCursor) {
1294 usage |= GraphicBuffer::USAGE_CURSOR;
1295 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001296 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001297 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001298}
1299
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001300void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001301 uint32_t orientation = 0;
1302 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001303 // The transform hint is used to improve performance, but we can
1304 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001305 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001306 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001307 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001308 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001309 orientation = 0;
1310 }
1311 }
David Sodmaneb085e02017-10-05 18:49:04 -07001312 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001313}
1314
Mathias Agopian13127d82013-03-05 17:47:11 -08001315// ----------------------------------------------------------------------------
1316// debugging
1317// ----------------------------------------------------------------------------
1318
Marissa Wall61c58622018-07-18 10:12:20 -07001319// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001320LayerDebugInfo Layer::getLayerDebugInfo() const {
1321 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001322 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001323 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001324 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001325 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
1326 info.mType = String8(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001327 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001328 info.mVisibleRegion = visibleRegion;
1329 info.mSurfaceDamageRegion = surfaceDamageRegion;
1330 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001331 info.mX = ds.active_legacy.transform.tx();
1332 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001333 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001334 info.mWidth = ds.active_legacy.w;
1335 info.mHeight = ds.active_legacy.h;
1336 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001337 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001338 info.mFlags = ds.flags;
1339 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001340 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001341 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1342 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1343 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1344 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001345 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001346 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001347 if (buffer != 0) {
1348 info.mActiveBufferWidth = buffer->getWidth();
1349 info.mActiveBufferHeight = buffer->getHeight();
1350 info.mActiveBufferStride = buffer->getStride();
1351 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001352 } else {
1353 info.mActiveBufferWidth = 0;
1354 info.mActiveBufferHeight = 0;
1355 info.mActiveBufferStride = 0;
1356 info.mActiveBufferFormat = 0;
1357 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001358 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001359 info.mNumQueuedFrames = getQueuedFrameCount();
1360 info.mRefreshPending = isBufferLatched();
1361 info.mIsOpaque = isOpaque(ds);
1362 info.mContentDirty = contentDirty;
1363 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001364}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001365
Dan Stozae22aec72016-08-01 13:20:59 -07001366void Layer::miniDumpHeader(String8& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001367 result.append("-------------------------------");
1368 result.append("-------------------------------");
1369 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001370 result.append(" Layer name\n");
1371 result.append(" Z | ");
1372 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001373 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001374 result.append(" Disp Frame (LTRB) | ");
1375 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001376 result.append("-------------------------------");
1377 result.append("-------------------------------");
1378 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001379}
1380
Dominik Laskowski7e045462018-05-30 13:02:02 -07001381void Layer::miniDump(String8& result, int32_t displayId) const {
1382 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stozae22aec72016-08-01 13:20:59 -07001383 return;
1384 }
1385
1386 String8 name;
1387 if (mName.length() > 77) {
1388 std::string shortened;
1389 shortened.append(mName.string(), 36);
1390 shortened.append("[...]");
1391 shortened.append(mName.string() + (mName.length() - 36), 36);
1392 name = shortened.c_str();
1393 } else {
1394 name = mName;
1395 }
1396
1397 result.appendFormat(" %s\n", name.string());
1398
Alec Mourib416efd2018-09-06 21:01:59 +00001399 const State& layerState(getDrawingState());
Lloyd Pique074e8122018-07-26 12:57:23 -07001400 const LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers.at(displayId);
Chia-I Wu1e043612018-03-01 09:45:09 -08001401 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
1402 result.appendFormat(" rel %6d | ", layerState.z);
1403 } else {
1404 result.appendFormat(" %10d | ", layerState.z);
1405 }
Dominik Laskowski7e045462018-05-30 13:02:02 -07001406 result.appendFormat("%10s | ", to_string(getCompositionType(displayId)).c_str());
Lloyd Pique074e8122018-07-26 12:57:23 -07001407 result.appendFormat("%10s | ", to_string(hwcInfo.transform).c_str());
1408 const Rect& frame = hwcInfo.displayFrame;
David Sodman41fdfc92017-11-06 16:09:56 -08001409 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique074e8122018-07-26 12:57:23 -07001410 const FloatRect& crop = hwcInfo.sourceCrop;
David Sodman41fdfc92017-11-06 16:09:56 -08001411 result.appendFormat("%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right, crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001412
David Sodman7e4ae112018-02-09 15:02:28 -08001413 result.append("- - - - - - - - - - - - - - - -\n");
1414
1415 std::string compositionInfoStr;
1416 getBE().compositionInfo.dump(compositionInfoStr, "compositionInfo");
1417 result.append(compositionInfoStr.c_str());
1418
Yichi Chen6ca35192018-05-29 12:20:43 +08001419 result.append("- - - - - - - - - - - - - - - -");
1420 result.append("- - - - - - - - - - - - - - - -");
1421 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001422}
Dan Stozae22aec72016-08-01 13:20:59 -07001423
Svetoslavd85084b2014-03-20 10:28:31 -07001424void Layer::dumpFrameStats(String8& result) const {
1425 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001426}
1427
Svetoslavd85084b2014-03-20 10:28:31 -07001428void Layer::clearFrameStats() {
1429 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001430}
1431
Jamie Gennis6547ff42013-07-16 20:12:42 -07001432void Layer::logFrameStats() {
1433 mFrameTracker.logAndResetStats(mName);
1434}
1435
Svetoslavd85084b2014-03-20 10:28:31 -07001436void Layer::getFrameStats(FrameStats* outStats) const {
1437 mFrameTracker.getStats(outStats);
1438}
1439
Brian Andersond6927fb2016-07-23 23:37:30 -07001440void Layer::dumpFrameEvents(String8& result) {
David Sodman41fdfc92017-11-06 16:09:56 -08001441 result.appendFormat("- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001442 Mutex::Autolock lock(mFrameEventHistoryMutex);
1443 mFrameEventHistory.checkFencesForCompletion();
1444 mFrameEventHistory.dump(result);
1445}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001446
Brian Anderson5ea5e592016-12-01 16:54:33 -08001447void Layer::onDisconnect() {
1448 Mutex::Autolock lock(mFrameEventHistoryMutex);
1449 mFrameEventHistory.onDisconnect();
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001450 mTimeStats.onDisconnect(getName().c_str());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001451}
1452
Brian Anderson3890c392016-07-25 12:48:08 -07001453void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001454 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001455 if (newTimestamps) {
1456 mTimeStats.setPostTime(getName().c_str(), newTimestamps->frameNumber,
1457 newTimestamps->postedTime);
1458 }
1459
Brian Andersond6927fb2016-07-23 23:37:30 -07001460 Mutex::Autolock lock(mFrameEventHistoryMutex);
1461 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001462 // If there are any unsignaled fences in the aquire timeline at this
1463 // point, the previously queued frame hasn't been latched yet. Go ahead
1464 // and try to get the signal time here so the syscall is taken out of
1465 // the main thread's critical path.
1466 mAcquireTimeline.updateSignalTimes();
1467 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001468 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001469 mFrameEventHistory.addQueue(*newTimestamps);
1470 }
1471
Brian Anderson3890c392016-07-25 12:48:08 -07001472 if (outDelta) {
1473 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001474 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001475}
Dan Stozae77c7662016-05-13 11:37:28 -07001476
Chia-I Wu98f1c102017-05-30 14:54:08 -07001477size_t Layer::getChildrenCount() const {
1478 size_t count = 0;
1479 for (const sp<Layer>& child : mCurrentChildren) {
1480 count += 1 + child->getChildrenCount();
1481 }
1482 return count;
1483}
1484
Robert Carr1f0a16a2016-10-24 16:27:39 -07001485void Layer::addChild(const sp<Layer>& layer) {
1486 mCurrentChildren.add(layer);
1487 layer->setParent(this);
1488}
1489
1490ssize_t Layer::removeChild(const sp<Layer>& layer) {
1491 layer->setParent(nullptr);
1492 return mCurrentChildren.remove(layer);
1493}
1494
Robert Carr1db73f62016-12-21 12:58:51 -08001495bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1496 sp<Handle> handle = nullptr;
1497 sp<Layer> newParent = nullptr;
1498 if (newParentHandle == nullptr) {
1499 return false;
1500 }
1501 handle = static_cast<Handle*>(newParentHandle.get());
1502 newParent = handle->owner.promote();
1503 if (newParent == nullptr) {
1504 ALOGE("Unable to promote Layer handle");
1505 return false;
1506 }
1507
1508 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001509 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001510
1511 sp<Client> client(child->mClientRef.promote());
1512 if (client != nullptr) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001513 client->updateParent(newParent);
Robert Carr1db73f62016-12-21 12:58:51 -08001514 }
1515 }
1516 mCurrentChildren.clear();
1517
1518 return true;
1519}
1520
Robert Carr15eae092018-03-23 13:43:53 -07001521void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001522 for (const sp<Layer>& child : mDrawingChildren) {
1523 child->mDrawingParent = newParent;
1524 }
1525}
1526
chaviwf1961f72017-09-18 16:41:07 -07001527bool Layer::reparent(const sp<IBinder>& newParentHandle) {
1528 if (newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001529 return false;
1530 }
1531
1532 auto handle = static_cast<Handle*>(newParentHandle.get());
1533 sp<Layer> newParent = handle->owner.promote();
1534 if (newParent == nullptr) {
1535 ALOGE("Unable to promote Layer handle");
1536 return false;
1537 }
1538
chaviwf1961f72017-09-18 16:41:07 -07001539 sp<Layer> parent = getParent();
1540 if (parent != nullptr) {
1541 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001542 }
chaviwf1961f72017-09-18 16:41:07 -07001543 newParent->addChild(this);
chaviw06178942017-07-27 10:25:59 -07001544
chaviwf1961f72017-09-18 16:41:07 -07001545 sp<Client> client(mClientRef.promote());
chaviw06178942017-07-27 10:25:59 -07001546 sp<Client> newParentClient(newParent->mClientRef.promote());
1547
chaviwf1961f72017-09-18 16:41:07 -07001548 if (client != newParentClient) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001549 client->updateParent(newParent);
chaviw06178942017-07-27 10:25:59 -07001550 }
1551
chaviw06178942017-07-27 10:25:59 -07001552 return true;
1553}
1554
Robert Carr9524cb32017-02-13 11:32:32 -08001555bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001556 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001557 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001558 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001559 if (client != nullptr && parentClient != client) {
Robert Carr7f619b22017-11-06 12:56:35 -08001560 client->detachLayer(child.get());
1561 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001562 }
Robert Carr7f619b22017-11-06 12:56:35 -08001563 }
Robert Carr9524cb32017-02-13 11:32:32 -08001564
1565 return true;
1566}
1567
Chia-I Wu11481472018-05-04 10:43:19 -07001568bool Layer::isLegacyDataSpace() const {
1569 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001570 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001571 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001572}
1573
Robert Carr1f0a16a2016-10-24 16:27:39 -07001574void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001575 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001576}
1577
1578void Layer::clearSyncPoints() {
1579 for (const auto& child : mCurrentChildren) {
1580 child->clearSyncPoints();
1581 }
1582
1583 Mutex::Autolock lock(mLocalSyncPointMutex);
1584 for (auto& point : mLocalSyncPoints) {
1585 point->setFrameAvailable();
1586 }
1587 mLocalSyncPoints.clear();
1588}
1589
1590int32_t Layer::getZ() const {
1591 return mDrawingState.z;
1592}
1593
Robert Carr29abff82017-12-04 13:51:20 -08001594bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1595 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1596 const State& state = useDrawing ? mDrawingState : mCurrentState;
1597 return state.zOrderRelativeOf != nullptr;
1598}
1599
David Sodman41fdfc92017-11-06 16:09:56 -08001600__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001601 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001602 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1603 "makeTraversalList received invalid stateSet");
1604 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1605 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1606 const State& state = useDrawing ? mDrawingState : mCurrentState;
1607
Robert Carr29abff82017-12-04 13:51:20 -08001608 if (state.zOrderRelatives.size() == 0) {
1609 *outSkipRelativeZUsers = true;
1610 return children;
1611 }
1612
chaviwfd462612018-05-31 16:11:27 -07001613 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001614 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001615 sp<Layer> strongRelative = weakRelative.promote();
1616 if (strongRelative != nullptr) {
1617 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001618 }
1619 }
1620
Dan Stoza412903f2017-04-27 13:42:17 -07001621 for (const sp<Layer>& child : children) {
Robert Carr503c7042017-09-27 15:06:08 -07001622 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1623 if (childState.zOrderRelativeOf != nullptr) {
1624 continue;
1625 }
Robert Carrdb66e622017-04-10 16:55:57 -07001626 traverse.add(child);
1627 }
1628
1629 return traverse;
1630}
1631
Robert Carr1f0a16a2016-10-24 16:27:39 -07001632/**
Robert Carrdb66e622017-04-10 16:55:57 -07001633 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001634 */
Dan Stoza412903f2017-04-27 13:42:17 -07001635void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001636 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1637 // produce a new list for traversing, including our relatives, and not including our children
1638 // who are relatives of another surface. In the case that there are no relative Z,
1639 // makeTraversalList returns our children directly to avoid significant overhead.
1640 // However in this case we need to take the responsibility for filtering children which
1641 // are relatives of another surface here.
1642 bool skipRelativeZUsers = false;
1643 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001644
Robert Carr1f0a16a2016-10-24 16:27:39 -07001645 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001646 for (; i < list.size(); i++) {
1647 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001648 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1649 continue;
1650 }
1651
Robert Carrdb66e622017-04-10 16:55:57 -07001652 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001653 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001654 }
Dan Stoza412903f2017-04-27 13:42:17 -07001655 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001656 }
Robert Carr29abff82017-12-04 13:51:20 -08001657
Dan Stoza412903f2017-04-27 13:42:17 -07001658 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001659 for (; i < list.size(); i++) {
1660 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001661
1662 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1663 continue;
1664 }
Dan Stoza412903f2017-04-27 13:42:17 -07001665 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001666 }
1667}
1668
1669/**
Robert Carrdb66e622017-04-10 16:55:57 -07001670 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001671 */
Dan Stoza412903f2017-04-27 13:42:17 -07001672void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1673 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001674 // See traverseInZOrder for documentation.
1675 bool skipRelativeZUsers = false;
1676 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001677
Robert Carr1f0a16a2016-10-24 16:27:39 -07001678 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001679 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001680 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001681
1682 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1683 continue;
1684 }
1685
Robert Carrdb66e622017-04-10 16:55:57 -07001686 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001687 break;
1688 }
Dan Stoza412903f2017-04-27 13:42:17 -07001689 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001690 }
Dan Stoza412903f2017-04-27 13:42:17 -07001691 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001692 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001693 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001694
1695 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1696 continue;
1697 }
1698
Dan Stoza412903f2017-04-27 13:42:17 -07001699 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001700 }
1701}
1702
chaviw4b129c22018-04-09 16:19:43 -07001703LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1704 const std::vector<Layer*>& layersInTree) {
1705 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1706 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001707 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1708 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
chaviw4b129c22018-04-09 16:19:43 -07001709 const State& state = useDrawing ? mDrawingState : mCurrentState;
1710
chaviwfd462612018-05-31 16:11:27 -07001711 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001712 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1713 sp<Layer> strongRelative = weakRelative.promote();
1714 // Only add relative layers that are also descendents of the top most parent of the tree.
1715 // If a relative layer is not a descendent, then it should be ignored.
1716 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1717 traverse.add(strongRelative);
1718 }
1719 }
1720
1721 for (const sp<Layer>& child : children) {
1722 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1723 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1724 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1725 // the child here since it won't be added later as a relative.
1726 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1727 childState.zOrderRelativeOf.promote().get())) {
1728 continue;
1729 }
1730 traverse.add(child);
1731 }
1732
1733 return traverse;
1734}
1735
1736void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1737 LayerVector::StateSet stateSet,
1738 const LayerVector::Visitor& visitor) {
1739 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001740
1741 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001742 for (; i < list.size(); i++) {
1743 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001744 if (relative->getZ() >= 0) {
1745 break;
1746 }
chaviw4b129c22018-04-09 16:19:43 -07001747 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001748 }
chaviw4b129c22018-04-09 16:19:43 -07001749
chaviwa76b2712017-09-20 12:02:26 -07001750 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001751 for (; i < list.size(); i++) {
1752 const auto& relative = list[i];
1753 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001754 }
1755}
1756
chaviw4b129c22018-04-09 16:19:43 -07001757std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1758 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1759 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1760
1761 std::vector<Layer*> layersInTree = {this};
1762 for (size_t i = 0; i < children.size(); i++) {
1763 const auto& child = children[i];
1764 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1765 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1766 }
1767
1768 return layersInTree;
1769}
1770
1771void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1772 const LayerVector::Visitor& visitor) {
1773 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1774 std::sort(layersInTree.begin(), layersInTree.end());
1775 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1776}
1777
Peiyong Linefefaac2018-08-17 12:27:51 -07001778ui::Transform Layer::getTransform() const {
1779 ui::Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001780 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001781 if (p != nullptr) {
1782 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07001783
1784 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
1785 // it isFixedSize) then there may be additional scaling not accounted
1786 // for in the transform. We need to mirror this scaling in child surfaces
1787 // or we will break the contract where WM can treat child surfaces as
1788 // pixels in the parent surface.
David Sodman0cc69182017-11-17 12:12:07 -08001789 if (p->isFixedSize() && p->getBE().compositionInfo.mBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07001790 int bufferWidth;
1791 int bufferHeight;
1792 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
David Sodman0cc69182017-11-17 12:12:07 -08001793 bufferWidth = p->getBE().compositionInfo.mBuffer->getWidth();
1794 bufferHeight = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001795 } else {
David Sodman0cc69182017-11-17 12:12:07 -08001796 bufferHeight = p->getBE().compositionInfo.mBuffer->getWidth();
1797 bufferWidth = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001798 }
Marissa Wall61c58622018-07-18 10:12:20 -07001799 float sx = p->getActiveWidth(p->getDrawingState()) / static_cast<float>(bufferWidth);
1800 float sy = p->getActiveHeight(p->getDrawingState()) / static_cast<float>(bufferHeight);
Peiyong Linefefaac2018-08-17 12:27:51 -07001801 ui::Transform extraParentScaling;
Robert Carr9b429f42017-04-17 14:56:57 -07001802 extraParentScaling.set(sx, 0, 0, sy);
1803 t = t * extraParentScaling;
1804 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001805 }
Marissa Wall61c58622018-07-18 10:12:20 -07001806 return t * getActiveTransform(getDrawingState());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001807}
1808
chaviw13fdc492017-06-27 12:40:18 -07001809half Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001810 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07001811
chaviw13fdc492017-06-27 12:40:18 -07001812 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1813 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001814}
Robert Carr6452f122017-03-21 10:41:29 -07001815
chaviw13fdc492017-06-27 12:40:18 -07001816half4 Layer::getColor() const {
1817 const half4 color(getDrawingState().color);
1818 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001819}
Robert Carr6452f122017-03-21 10:41:29 -07001820
Robert Carr1f0a16a2016-10-24 16:27:39 -07001821void Layer::commitChildList() {
1822 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1823 const auto& child = mCurrentChildren[i];
1824 child->commitChildList();
1825 }
1826 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001827 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001828}
1829
chaviw1d044282017-09-27 12:19:28 -07001830void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
1831 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1832 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1833 const State& state = useDrawing ? mDrawingState : mCurrentState;
1834
Peiyong Linefefaac2018-08-17 12:27:51 -07001835 ui::Transform requestedTransform = state.active_legacy.transform;
1836 ui::Transform transform = getTransform();
chaviw1d044282017-09-27 12:19:28 -07001837
1838 layerInfo->set_id(sequence);
1839 layerInfo->set_name(getName().c_str());
1840 layerInfo->set_type(String8(getTypeId()));
1841
1842 for (const auto& child : children) {
1843 layerInfo->add_children(child->sequence);
1844 }
1845
1846 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1847 sp<Layer> strongRelative = weakRelative.promote();
1848 if (strongRelative != nullptr) {
1849 layerInfo->add_relatives(strongRelative->sequence);
1850 }
1851 }
1852
Marissa Wallf58c14b2018-07-24 10:50:43 -07001853 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
chaviw1d044282017-09-27 12:19:28 -07001854 layerInfo->mutable_transparent_region());
1855 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
1856 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
1857
1858 layerInfo->set_layer_stack(getLayerStack());
1859 layerInfo->set_z(state.z);
1860
1861 PositionProto* position = layerInfo->mutable_position();
1862 position->set_x(transform.tx());
1863 position->set_y(transform.ty());
1864
1865 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
1866 requestedPosition->set_x(requestedTransform.tx());
1867 requestedPosition->set_y(requestedTransform.ty());
1868
1869 SizeProto* size = layerInfo->mutable_size();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001870 size->set_w(state.active_legacy.w);
1871 size->set_h(state.active_legacy.h);
chaviw1d044282017-09-27 12:19:28 -07001872
Marissa Wallf58c14b2018-07-24 10:50:43 -07001873 LayerProtoHelper::writeToProto(state.crop_legacy, layerInfo->mutable_crop());
chaviw1d044282017-09-27 12:19:28 -07001874
1875 layerInfo->set_is_opaque(isOpaque(state));
1876 layerInfo->set_invalidate(contentDirty);
Chia-I Wu01591c92018-05-22 12:03:00 -07001877
1878 // XXX (b/79210409) mCurrentDataSpace is not protected
1879 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
1880
chaviw1d044282017-09-27 12:19:28 -07001881 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1882 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
1883 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
1884 layerInfo->set_flags(state.flags);
1885
1886 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1887 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
1888
Jorim Jaggi8e0af362017-11-14 16:28:28 +01001889 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07001890 if (parent != nullptr) {
1891 layerInfo->set_parent(parent->sequence);
1892 }
1893
1894 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1895 if (zOrderRelativeOf != nullptr) {
1896 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1897 }
1898
Chia-I Wu01591c92018-05-22 12:03:00 -07001899 // XXX getBE().compositionInfo.mBuffer is not protected
David Sodman0cc69182017-11-17 12:12:07 -08001900 auto buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001901 if (buffer != nullptr) {
1902 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
Peiyong Linefefaac2018-08-17 12:27:51 -07001903 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
Yichi Chen6ca35192018-05-29 12:20:43 +08001904 layerInfo->mutable_buffer_transform());
chaviw1d044282017-09-27 12:19:28 -07001905 }
1906
1907 layerInfo->set_queued_frames(getQueuedFrameCount());
1908 layerInfo->set_refresh_pending(isBufferLatched());
rongliucfb187b2018-03-14 12:26:23 -07001909 layerInfo->set_window_type(state.type);
1910 layerInfo->set_app_id(state.appId);
chaviwadc40c22018-07-10 16:57:27 -07001911 layerInfo->set_curr_frame(mCurrentFrameNumber);
1912
1913 for (const auto& pendingState : mPendingStates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001914 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
chaviwadc40c22018-07-10 16:57:27 -07001915 if (barrierLayer != nullptr) {
1916 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
1917 barrierLayerProto->set_id(barrierLayer->sequence);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001918 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
chaviwadc40c22018-07-10 16:57:27 -07001919 }
1920 }
chaviw1d044282017-09-27 12:19:28 -07001921}
1922
Dominik Laskowski7e045462018-05-30 13:02:02 -07001923void Layer::writeToProto(LayerProto* layerInfo, int32_t displayId) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07001924 if (!hasHwcLayer(displayId)) {
1925 return;
1926 }
1927
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001928 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
1929
Dominik Laskowski7e045462018-05-30 13:02:02 -07001930 const auto& hwcInfo = getBE().mHwcLayers.at(displayId);
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001931
1932 const Rect& frame = hwcInfo.displayFrame;
1933 LayerProtoHelper::writeToProto(frame, layerInfo->mutable_hwc_frame());
1934
1935 const FloatRect& crop = hwcInfo.sourceCrop;
1936 LayerProtoHelper::writeToProto(crop, layerInfo->mutable_hwc_crop());
1937
1938 const int32_t transform = static_cast<int32_t>(hwcInfo.transform);
1939 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08001940
1941 const int32_t compositionType = static_cast<int32_t>(hwcInfo.compositionType);
1942 layerInfo->set_hwc_composition_type(compositionType);
1943
1944 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
1945 static_cast<BufferLayer*>(this)->isProtected()) {
1946 layerInfo->set_is_protected(true);
1947 } else {
1948 layerInfo->set_is_protected(false);
1949 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001950}
1951
Mathias Agopian13127d82013-03-05 17:47:11 -08001952// ---------------------------------------------------------------------------
1953
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001954}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07001955
1956#if defined(__gl_h_)
1957#error "don't include gl/gl.h in this file"
1958#endif
1959
1960#if defined(__gl2_h_)
1961#error "don't include gl2/gl2.h in this file"
1962#endif