blob: edd5cd1716e965764ee397530691efb53e98d650 [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 mFiltering(false),
82 mNeedsFiltering(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080083 mProtectedByApp(false),
Riley Andrews03414a12014-07-01 14:22:59 -070084 mClientRef(client),
Dan Stozaa4650a52015-05-12 12:56:16 -070085 mPotentialCursor(false),
David Sodmanb8af7922017-12-21 15:17:55 -080086 mFreezeGeometryUpdates(false),
chaviwfd462612018-05-31 16:11:27 -070087 mCurrentChildren(LayerVector::StateSet::Current),
88 mDrawingChildren(LayerVector::StateSet::Drawing),
David Sodman2b727ac2017-12-21 14:28:08 -080089 mBE{this, name.string()} {
Dan Stoza9e56aa02015-11-02 13:00:03 -080090
Mathias Agopiana67932f2011-04-20 14:20:59 -070091 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070092
93 uint32_t layerFlags = 0;
David Sodman41fdfc92017-11-06 16:09:56 -080094 if (flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
95 if (flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
96 if (flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070097
Mathias Agopian4d9b8222013-03-12 17:11:48 -070098 mName = name;
Dan Stozaf7ba41a2017-05-10 15:11:11 -070099 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700100
Marissa Wallf58c14b2018-07-24 10:50:43 -0700101 mCurrentState.active_legacy.w = w;
102 mCurrentState.active_legacy.h = h;
David Sodman0c69cad2017-08-21 12:12:51 -0700103 mCurrentState.flags = layerFlags;
Marissa Wallf58c14b2018-07-24 10:50:43 -0700104 mCurrentState.active_legacy.transform.set(0, 0);
105 mCurrentState.crop_legacy.makeInvalid();
Marissa Wallf58c14b2018-07-24 10:50:43 -0700106 mCurrentState.requestedCrop_legacy = mCurrentState.crop_legacy;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700107 mCurrentState.z = 0;
chaviw13fdc492017-06-27 12:40:18 -0700108 mCurrentState.color.a = 1.0f;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700109 mCurrentState.layerStack = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700110 mCurrentState.sequence = 0;
Marissa Wallf58c14b2018-07-24 10:50:43 -0700111 mCurrentState.requested_legacy = mCurrentState.active_legacy;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500112 mCurrentState.appId = 0;
113 mCurrentState.type = 0;
Marissa Wall61c58622018-07-18 10:12:20 -0700114 mCurrentState.active.w = 0;
115 mCurrentState.active.h = 0;
116 mCurrentState.active.transform.set(0, 0);
117 mCurrentState.transform = 0;
118 mCurrentState.transformToDisplayInverse = false;
119 mCurrentState.crop.makeInvalid();
120 mCurrentState.acquireFence = new Fence(-1);
121 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
122 mCurrentState.hdrMetadata.validTypes = 0;
123 mCurrentState.surfaceDamageRegion.clear();
124 mCurrentState.api = -1;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700125
126 // drawing state & current state are identical
127 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700128
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800129 CompositorTiming compositorTiming;
130 flinger->getCompositorTiming(&compositorTiming);
131 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200132 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Dan Stoza436ccf32018-06-21 12:10:12 -0700133}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700134
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700135Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800136 sp<Client> c(mClientRef.promote());
137 if (c != 0) {
138 c->detachLayer(this);
139 }
140
141 for (auto& point : mRemoteSyncPoints) {
142 point->setTransactionApplied();
143 }
144 for (auto& point : mLocalSyncPoints) {
145 point->setFrameAvailable();
146 }
Jamie Gennis6547ff42013-07-16 20:12:42 -0700147 mFrameTracker.logAndResetStats(mName);
Mathias Agopian96f08192010-06-02 23:28:45 -0700148}
149
Mathias Agopian13127d82013-03-05 17:47:11 -0800150// ---------------------------------------------------------------------------
151// callbacks
152// ---------------------------------------------------------------------------
153
David Sodmaneb085e02017-10-05 18:49:04 -0700154/*
155 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
156 * Layer. So, the implementation is done in BufferLayer. When called on a
157 * ColorLayer object, it's essentially a NOP.
158 */
David Sodmaneb085e02017-10-05 18:49:04 -0700159void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800160
Chia-I Wuc6657022017-08-15 11:18:17 -0700161void Layer::onRemovedFromCurrentState() {
162 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
163
chaviw8b3871a2017-11-01 17:41:01 -0700164 mPendingRemoval = true;
165
Robert Carr5edb1ad2017-04-25 10:54:24 -0700166 if (mCurrentState.zOrderRelativeOf != nullptr) {
167 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
168 if (strongRelative != nullptr) {
169 strongRelative->removeZOrderRelative(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700170 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700171 }
172 mCurrentState.zOrderRelativeOf = nullptr;
173 }
174
Chia-I Wuc6657022017-08-15 11:18:17 -0700175 for (const auto& child : mCurrentChildren) {
176 child->onRemovedFromCurrentState();
177 }
178}
Chia-I Wu38512252017-05-17 14:36:16 -0700179
Chia-I Wuc6657022017-08-15 11:18:17 -0700180void Layer::onRemoved() {
181 // the layer is removed from SF mLayersPendingRemoval
David Sodmaneb085e02017-10-05 18:49:04 -0700182 abandon();
Chia-I Wuc6657022017-08-15 11:18:17 -0700183
Steven Thomasb02664d2017-07-26 18:48:28 -0700184 destroyAllHwcLayers();
Chia-I Wu38512252017-05-17 14:36:16 -0700185
Robert Carr1f0a16a2016-10-24 16:27:39 -0700186 for (const auto& child : mCurrentChildren) {
187 child->onRemoved();
188 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700189}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700190
Mathias Agopian13127d82013-03-05 17:47:11 -0800191// ---------------------------------------------------------------------------
192// set-up
193// ---------------------------------------------------------------------------
194
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700195const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800196 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800197}
198
chaviw13fdc492017-06-27 12:40:18 -0700199bool Layer::getPremultipledAlpha() const {
200 return mPremultipliedAlpha;
201}
202
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700203sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800204 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700205 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800206}
207
208// ---------------------------------------------------------------------------
209// h/w composer set-up
210// ---------------------------------------------------------------------------
211
Dominik Laskowski7e045462018-05-30 13:02:02 -0700212bool Layer::createHwcLayer(HWComposer* hwc, int32_t displayId) {
213 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.count(displayId) != 0,
214 "Already have a layer for display %d", displayId);
David Sodmanb8aaea12017-12-14 15:54:51 -0800215 auto layer = std::shared_ptr<HWC2::Layer>(
216 hwc->createLayer(displayId),
217 [hwc, displayId](HWC2::Layer* layer) {
218 hwc->destroyLayer(displayId, layer); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700219 if (!layer) {
220 return false;
221 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700222 LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers[displayId];
Steven Thomasb02664d2017-07-26 18:48:28 -0700223 hwcInfo.hwc = hwc;
224 hwcInfo.layer = layer;
David Sodmanf6a38932018-05-25 15:27:50 -0700225 layer->setLayerDestroyedListener(
Dominik Laskowski7e045462018-05-30 13:02:02 -0700226 [this, displayId](HWC2::Layer* /*layer*/) { getBE().mHwcLayers.erase(displayId); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700227 return true;
228}
229
Dominik Laskowski7e045462018-05-30 13:02:02 -0700230bool Layer::destroyHwcLayer(int32_t displayId) {
231 if (getBE().mHwcLayers.count(displayId) == 0) {
Chia-I Wu83806892017-11-16 10:50:20 -0800232 return false;
Steven Thomasb02664d2017-07-26 18:48:28 -0700233 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700234 auto& hwcInfo = getBE().mHwcLayers[displayId];
David Sodman41fdfc92017-11-06 16:09:56 -0800235 LOG_ALWAYS_FATAL_IF(hwcInfo.layer == nullptr, "Attempt to destroy null layer");
Steven Thomasb02664d2017-07-26 18:48:28 -0700236 LOG_ALWAYS_FATAL_IF(hwcInfo.hwc == nullptr, "Missing HWComposer");
David Sodmanb8aaea12017-12-14 15:54:51 -0800237 hwcInfo.layer = nullptr;
238
Chia-I Wu83806892017-11-16 10:50:20 -0800239 return true;
Steven Thomasb02664d2017-07-26 18:48:28 -0700240}
241
242void Layer::destroyAllHwcLayers() {
David Sodman6f65f3e2017-11-03 14:28:09 -0700243 size_t numLayers = getBE().mHwcLayers.size();
Steven Thomasb02664d2017-07-26 18:48:28 -0700244 for (size_t i = 0; i < numLayers; ++i) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700245 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.empty(), "destroyAllHwcLayers failed");
246 destroyHwcLayer(getBE().mHwcLayers.begin()->first);
Steven Thomasb02664d2017-07-26 18:48:28 -0700247 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700248 LOG_ALWAYS_FATAL_IF(!getBE().mHwcLayers.empty(),
David Sodman41fdfc92017-11-06 16:09:56 -0800249 "All hardware composer layers should have been destroyed");
Steven Thomasb02664d2017-07-26 18:48:28 -0700250}
Steven Thomasb02664d2017-07-26 18:48:28 -0700251
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800252Rect Layer::getContentCrop() const {
253 // this is the crop rectangle that applies to the buffer
254 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700255 Rect crop;
256 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800257 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700258 crop = mCurrentCrop;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800259 } else if (getBE().compositionInfo.mBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800260 // otherwise we use the whole buffer
David Sodman0cc69182017-11-17 12:12:07 -0800261 crop = getBE().compositionInfo.mBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700262 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800263 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700264 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700265 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700266 return crop;
267}
268
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700269static Rect reduce(const Rect& win, const Region& exclude) {
270 if (CC_LIKELY(exclude.isEmpty())) {
271 return win;
272 }
273 if (exclude.isRect()) {
274 return win.reduce(exclude.getBounds());
275 }
276 return Region(win).subtract(exclude).getBounds();
277}
278
Dan Stoza80d61162017-12-20 15:57:52 -0800279static FloatRect reduce(const FloatRect& win, const Region& exclude) {
280 if (CC_LIKELY(exclude.isEmpty())) {
281 return win;
282 }
283 // Convert through Rect (by rounding) for lack of FloatRegion
284 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
285}
286
Robert Carr1f0a16a2016-10-24 16:27:39 -0700287Rect Layer::computeScreenBounds(bool reduceTransparentRegion) const {
288 const Layer::State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700289 Rect win(getActiveWidth(s), getActiveHeight(s));
Robert Carr1f0a16a2016-10-24 16:27:39 -0700290
Marissa Wall61c58622018-07-18 10:12:20 -0700291 Rect crop = getCrop(s);
292 if (!crop.isEmpty()) {
293 win.intersect(crop, &win);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700294 }
295
Peiyong Linefefaac2018-08-17 12:27:51 -0700296 ui::Transform t = getTransform();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700297 win = t.transform(win);
298
Chia-I Wue41dbe62017-06-13 14:10:56 -0700299 const sp<Layer>& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700300 // Now we need to calculate the parent bounds, so we can clip ourselves to those.
301 // When calculating the parent bounds for purposes of clipping,
302 // we don't need to constrain the parent to its transparent region.
303 // The transparent region is an optimization based on the
304 // buffer contents of the layer, but does not affect the space allocated to
305 // it by policy, and thus children should be allowed to extend into the
306 // parent's transparent region. In fact one of the main uses, is to reduce
307 // buffer allocation size in cases where a child window sits behind a main window
308 // (by marking the hole in the parent window as a transparent region)
309 if (p != nullptr) {
310 Rect bounds = p->computeScreenBounds(false);
311 bounds.intersect(win, &win);
312 }
313
314 if (reduceTransparentRegion) {
Marissa Wall61c58622018-07-18 10:12:20 -0700315 auto const screenTransparentRegion = t.transform(getActiveTransparentRegion(s));
Robert Carr1f0a16a2016-10-24 16:27:39 -0700316 win = reduce(win, screenTransparentRegion);
317 }
318
319 return win;
320}
321
Dan Stoza80d61162017-12-20 15:57:52 -0800322FloatRect Layer::computeBounds() const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700323 const Layer::State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700324 return computeBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700325}
326
Dan Stoza80d61162017-12-20 15:57:52 -0800327FloatRect Layer::computeBounds(const Region& activeTransparentRegion) const {
Michael Lentine6c925ed2014-09-26 17:55:01 -0700328 const Layer::State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700329 Rect win(getActiveWidth(s), getActiveHeight(s));
Robert Carrb5d3d262016-03-25 15:08:13 -0700330
Marissa Wall61c58622018-07-18 10:12:20 -0700331 Rect crop = getCrop(s);
332 if (!crop.isEmpty()) {
333 win.intersect(crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800334 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700335
Chia-I Wue41dbe62017-06-13 14:10:56 -0700336 const auto& p = mDrawingParent.promote();
Robert Carrd4ae7f32018-06-07 16:10:57 -0700337 FloatRect floatWin = win.toFloatRect();
338 FloatRect parentBounds = floatWin;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700339 if (p != nullptr) {
Robert Carrd4ae7f32018-06-07 16:10:57 -0700340 // We pass an empty Region here for reasons mirroring that of the case described in
341 // the computeScreenBounds reduceTransparentRegion=false case.
342 parentBounds = p->computeBounds(Region());
Robert Carr1f0a16a2016-10-24 16:27:39 -0700343 }
344
Peiyong Linefefaac2018-08-17 12:27:51 -0700345 ui::Transform t = s.active_legacy.transform;
Dan Stoza80d61162017-12-20 15:57:52 -0800346
Vishnu Nairdcce0e22018-08-23 08:35:19 -0700347 if (p != nullptr) {
Dan Stoza80d61162017-12-20 15:57:52 -0800348 floatWin = t.transform(floatWin);
Robert Carrd4ae7f32018-06-07 16:10:57 -0700349 floatWin = floatWin.intersect(parentBounds);
Dan Stoza80d61162017-12-20 15:57:52 -0800350 floatWin = t.inverse().transform(floatWin);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700351 }
352
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700353 // subtract the transparent region and snap to the bounds
Dan Stoza80d61162017-12-20 15:57:52 -0800354 return reduce(floatWin, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800355}
356
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700357Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& display) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700358 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800359 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700360 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800361
362 // apply the projection's clipping to the window crop in
363 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700364 // if there are no window scaling involved, this operation will map to full
365 // pixels in the buffer.
366 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
367 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700368
Marissa Wall61c58622018-07-18 10:12:20 -0700369 Rect activeCrop(getActiveWidth(s), getActiveHeight(s));
370 Rect crop = getCrop(s);
371 if (!crop.isEmpty()) {
372 activeCrop.intersect(crop, &activeCrop);
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700373 }
374
Peiyong Linefefaac2018-08-17 12:27:51 -0700375 ui::Transform t = getTransform();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700376 activeCrop = t.transform(activeCrop);
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700377 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000378 activeCrop.clear();
379 }
chaviwb1154d12017-10-31 14:15:36 -0700380
381 const auto& p = mDrawingParent.promote();
382 if (p != nullptr) {
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700383 auto parentCrop = p->computeInitialCrop(display);
chaviwb1154d12017-10-31 14:15:36 -0700384 activeCrop.intersect(parentCrop, &activeCrop);
385 }
386
Robert Carr1f0a16a2016-10-24 16:27:39 -0700387 return activeCrop;
388}
389
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700390FloatRect Layer::computeCrop(const sp<const DisplayDevice>& display) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700391 // the content crop is the area of the content that gets scaled to the
392 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800393 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700394
395 // In addition there is a WM-specified crop we pull from our drawing state.
396 const State& s(getDrawingState());
397
398 // Screen space to make reduction to parent crop clearer.
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700399 Rect activeCrop = computeInitialCrop(display);
Peiyong Linefefaac2018-08-17 12:27:51 -0700400 ui::Transform t = getTransform();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700401 // Back to layer space to work with the content crop.
402 activeCrop = t.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800403
Michael Lentine28ea2172014-11-19 18:32:37 -0800404 // This needs to be here as transform.transform(Rect) computes the
405 // transformed rect and then takes the bounding box of the result before
406 // returning. This means
407 // transform.inverse().transform(transform.transform(Rect)) != Rect
408 // in which case we need to make sure the final rect is clipped to the
409 // display bounds.
Marissa Wall61c58622018-07-18 10:12:20 -0700410 if (!activeCrop.intersect(Rect(getActiveWidth(s), getActiveHeight(s)), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000411 activeCrop.clear();
412 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800413
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700414 // subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700415 activeCrop = reduce(activeCrop, getActiveTransparentRegion(s));
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700416
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000417 // Transform the window crop to match the buffer coordinate system,
418 // which means using the inverse of the current transform set on the
419 // SurfaceFlingerConsumer.
420 uint32_t invTransform = mCurrentTransform;
Robert Carrcae605c2017-03-29 12:10:31 -0700421 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000422 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700423 * the code below applies the primary display's inverse transform to the
424 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000425 */
David Sodman41fdfc92017-11-06 16:09:56 -0800426 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000427 // calculate the inverse transform
428 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800429 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800430 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000431 // and apply to the current transform
Peiyong Linefefaac2018-08-17 12:27:51 -0700432 invTransform = (ui::Transform(invTransformOrient) *
433 ui::Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800434 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000435
Marissa Wall61c58622018-07-18 10:12:20 -0700436 int winWidth = getActiveWidth(s);
437 int winHeight = getActiveHeight(s);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000438 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
439 // If the activeCrop has been rotate the ends are rotated but not
440 // the space itself so when transforming ends back we can't rely on
441 // a modification of the axes of rotation. To account for this we
442 // need to reorient the inverse rotation in terms of the current
443 // axes of rotation.
444 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
445 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
446 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800447 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000448 }
Marissa Wall61c58622018-07-18 10:12:20 -0700449 winWidth = getActiveHeight(s);
450 winHeight = getActiveWidth(s);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000451 }
Marissa Wall61c58622018-07-18 10:12:20 -0700452 const Rect winCrop = activeCrop.transform(invTransform, getActiveWidth(s), getActiveHeight(s));
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000453
454 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800455 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000456 float yScale = crop.getHeight() / float(winHeight);
457
David Sodman41fdfc92017-11-06 16:09:56 -0800458 float insetL = winCrop.left * xScale;
459 float insetT = winCrop.top * yScale;
460 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000461 float insetB = (winHeight - winCrop.bottom) * yScale;
462
David Sodman41fdfc92017-11-06 16:09:56 -0800463 crop.left += insetL;
464 crop.top += insetT;
465 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000466 crop.bottom -= insetB;
467
Mathias Agopian13127d82013-03-05 17:47:11 -0800468 return crop;
469}
470
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700471void Layer::setGeometry(const sp<const DisplayDevice>& display, uint32_t z) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700472 const auto displayId = display->getId();
Peiyong Lin91b1df22018-06-18 18:00:16 -0700473 if (!hasHwcLayer(displayId)) {
474 ALOGE("[%s] failed to setGeometry: no HWC layer found (%d)",
475 mName.string(), displayId);
476 return;
477 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700478 auto& hwcInfo = getBE().mHwcLayers[displayId];
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700479
David Sodman10a41ff2018-08-05 12:14:17 -0700480 // Device or Cursor layers
481 if (mPotentialCursor) {
482 ALOGV("[%s] Requesting Cursor composition", mName.string());
483 setCompositionType(displayId, HWC2::Composition::Cursor);
484 } else {
485 ALOGV("[%s] Requesting Device composition", mName.string());
486 setCompositionType(displayId, HWC2::Composition::Device);
487 }
488
489 // Need to program geometry parts
490 getBE().compositionInfo.hwc.skipGeometry = false;
491
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700492 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800493 hwcInfo.forceClientComposition = false;
494
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700495 if (isSecure() && !display->isSecure()) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800496 hwcInfo.forceClientComposition = true;
497 }
498
Mathias Agopian13127d82013-03-05 17:47:11 -0800499 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700500 const State& s(getDrawingState());
David Revemanecf0fa52017-03-03 11:32:44 -0500501 auto blendMode = HWC2::BlendMode::None;
Robert Carr6452f122017-03-21 10:41:29 -0700502 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800503 blendMode =
504 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800505 }
David Sodmanba340492018-08-05 21:51:33 -0700506 getBE().compositionInfo.hwc.blendMode = blendMode;
Mathias Agopian13127d82013-03-05 17:47:11 -0800507
508 // apply the layer's transform, followed by the display's global transform
509 // here we're guaranteed that the layer's transform preserves rects
Marissa Wall61c58622018-07-18 10:12:20 -0700510 Region activeTransparentRegion(getActiveTransparentRegion(s));
Peiyong Linefefaac2018-08-17 12:27:51 -0700511 ui::Transform t = getTransform();
Marissa Wall61c58622018-07-18 10:12:20 -0700512 Rect activeCrop = getCrop(s);
513 if (!activeCrop.isEmpty()) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700514 activeCrop = t.transform(activeCrop);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700515 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000516 activeCrop.clear();
517 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700518 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800519 // This needs to be here as transform.transform(Rect) computes the
520 // transformed rect and then takes the bounding box of the result before
521 // returning. This means
522 // transform.inverse().transform(transform.transform(Rect)) != Rect
523 // in which case we need to make sure the final rect is clipped to the
524 // display bounds.
Marissa Wall61c58622018-07-18 10:12:20 -0700525 if (!activeCrop.intersect(Rect(getActiveWidth(s), getActiveHeight(s)), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000526 activeCrop.clear();
527 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700528 // mark regions outside the crop as transparent
Marissa Wall61c58622018-07-18 10:12:20 -0700529 activeTransparentRegion.orSelf(Rect(0, 0, getActiveWidth(s), activeCrop.top));
Marissa Wallf58c14b2018-07-24 10:50:43 -0700530 activeTransparentRegion.orSelf(
Marissa Wall61c58622018-07-18 10:12:20 -0700531 Rect(0, activeCrop.bottom, getActiveWidth(s), getActiveHeight(s)));
David Sodman41fdfc92017-11-06 16:09:56 -0800532 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
533 activeTransparentRegion.orSelf(
Marissa Wall61c58622018-07-18 10:12:20 -0700534 Rect(activeCrop.right, activeCrop.top, getActiveWidth(s), activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700535 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700536
Dan Stoza80d61162017-12-20 15:57:52 -0800537 // computeBounds returns a FloatRect to provide more accuracy during the
538 // transformation. We then round upon constructing 'frame'.
539 Rect frame{t.transform(computeBounds(activeTransparentRegion))};
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700540 if (!frame.intersect(display->getViewport(), &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000541 frame.clear();
542 }
Peiyong Linefefaac2018-08-17 12:27:51 -0700543 const ui::Transform& tr = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800544 Rect transformedFrame = tr.transform(frame);
David Sodmanba340492018-08-05 21:51:33 -0700545 getBE().compositionInfo.hwc.displayFrame = transformedFrame;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800546
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700547 FloatRect sourceCrop = computeCrop(display);
David Sodmanba340492018-08-05 21:51:33 -0700548 getBE().compositionInfo.hwc.sourceCrop = sourceCrop;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800549
chaviw13fdc492017-06-27 12:40:18 -0700550 float alpha = static_cast<float>(getAlpha());
David Sodmanba340492018-08-05 21:51:33 -0700551 getBE().compositionInfo.hwc.alpha = alpha;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800552
David Sodmanba340492018-08-05 21:51:33 -0700553 getBE().compositionInfo.hwc.z = z;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500554
Albert Chaulk2a589632017-05-04 16:59:44 -0400555 int type = s.type;
556 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700557 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400558 if (parent.get()) {
559 auto& parentState = parent->getDrawingState();
rongliucfb187b2018-03-14 12:26:23 -0700560 if (parentState.type >= 0 || parentState.appId >= 0) {
561 type = parentState.type;
562 appId = parentState.appId;
563 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400564 }
565
David Sodmanba340492018-08-05 21:51:33 -0700566 getBE().compositionInfo.hwc.type = type;
567 getBE().compositionInfo.hwc.appId = appId;
568
Mathias Agopian29a367b2011-07-12 14:51:45 -0700569 /*
570 * Transformations are applied in this order:
571 * 1) buffer orientation/flip/mirror
572 * 2) state transformation (window manager)
573 * 3) layer orientation (screen orientation)
574 * (NOTE: the matrices are multiplied in reverse order)
575 */
576
Peiyong Linefefaac2018-08-17 12:27:51 -0700577 const ui::Transform bufferOrientation(mCurrentTransform);
578 ui::Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700579
Robert Carrcae605c2017-03-29 12:10:31 -0700580 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700581 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700582 * the code below applies the primary display's inverse transform to the
583 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700584 */
David Sodman41fdfc92017-11-06 16:09:56 -0800585 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700586 // calculate the inverse transform
587 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800588 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700589 }
Robert Carrcae605c2017-03-29 12:10:31 -0700590
591 /*
592 * Here we cancel out the orientation component of the WM transform.
593 * The scaling and translate components are already included in our bounds
594 * computation so it's enough to just omit it in the composition.
595 * See comment in onDraw with ref to b/36727915 for why.
596 */
Peiyong Linefefaac2018-08-17 12:27:51 -0700597 transform = ui::Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700598 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700599
600 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800601 const uint32_t orientation = transform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -0700602 if (orientation & ui::Transform::ROT_INVALID) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800603 // we can only handle simple transformation
Lloyd Pique074e8122018-07-26 12:57:23 -0700604 hwcInfo.forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800605 } else {
606 auto transform = static_cast<HWC2::Transform>(orientation);
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800607 hwcInfo.transform = transform;
David Sodmanba340492018-08-05 21:51:33 -0700608 getBE().compositionInfo.hwc.transform = transform;
David Sodman4b7c4bc2017-11-17 12:13:59 -0800609 }
610}
611
Dominik Laskowski7e045462018-05-30 13:02:02 -0700612void Layer::forceClientComposition(int32_t displayId) {
613 if (getBE().mHwcLayers.count(displayId) == 0) {
614 ALOGE("forceClientComposition: no HWC layer found (%d)", displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800615 return;
616 }
617
Dominik Laskowski7e045462018-05-30 13:02:02 -0700618 getBE().mHwcLayers[displayId].forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800619}
Dan Stozaee44edd2015-03-23 15:50:23 -0700620
Dominik Laskowski7e045462018-05-30 13:02:02 -0700621bool Layer::getForceClientComposition(int32_t displayId) {
622 if (getBE().mHwcLayers.count(displayId) == 0) {
623 ALOGE("getForceClientComposition: no HWC layer found (%d)", displayId);
chaviwc9232ed2017-11-14 15:31:15 -0800624 return false;
625 }
626
Dominik Laskowski7e045462018-05-30 13:02:02 -0700627 return getBE().mHwcLayers[displayId].forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800628}
629
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700630void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700631 const auto displayId = display->getId();
632 if (getBE().mHwcLayers.count(displayId) == 0 ||
633 getCompositionType(displayId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800634 return;
635 }
636
637 // This gives us only the "orientation" component of the transform
638 const State& s(getCurrentState());
639
640 // Apply the layer's transform, followed by the display's global transform
641 // Here we're guaranteed that the layer's transform preserves rects
Marissa Wall61c58622018-07-18 10:12:20 -0700642 Rect win(getActiveWidth(s), getActiveHeight(s));
643 Rect crop = getCrop(s);
644 if (!crop.isEmpty()) {
645 win.intersect(crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800646 }
647 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700648 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Robert Carr1f0a16a2016-10-24 16:27:39 -0700649 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700650 frame.intersect(display->getViewport(), &frame);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700651 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800652 auto position = displayTransform.transform(frame);
653
Dominik Laskowski7e045462018-05-30 13:02:02 -0700654 auto error =
David Sodmanb8aaea12017-12-14 15:54:51 -0800655 (getBE().mHwcLayers[displayId].layer)->setCursorPosition(
656 position.left, position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800657 ALOGE_IF(error != HWC2::Error::None,
658 "[%s] Failed to set cursor position "
659 "to (%d, %d): %s (%d)",
660 mName.string(), position.left, position.top, to_string(error).c_str(),
661 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800662}
Riley Andrews03414a12014-07-01 14:22:59 -0700663
Mathias Agopian13127d82013-03-05 17:47:11 -0800664// ---------------------------------------------------------------------------
665// drawing...
666// ---------------------------------------------------------------------------
667
Marissa Wall61c58622018-07-18 10:12:20 -0700668void Layer::draw(const RenderArea& renderArea, const Region& clip) {
chaviwa76b2712017-09-20 12:02:26 -0700669 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800670}
671
Marissa Wall61c58622018-07-18 10:12:20 -0700672void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) {
chaviwa76b2712017-09-20 12:02:26 -0700673 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800674}
675
Marissa Wall61c58622018-07-18 10:12:20 -0700676void Layer::draw(const RenderArea& renderArea) {
chaviwa76b2712017-09-20 12:02:26 -0700677 onDraw(renderArea, Region(renderArea.getBounds()), false);
Dan Stozac7014012014-02-14 15:03:43 -0800678}
679
David Sodman41fdfc92017-11-06 16:09:56 -0800680void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
681 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800682 auto& engine(mFlinger->getRenderEngine());
David Sodman9eeae692017-11-02 10:53:32 -0700683 computeGeometry(renderArea, getBE().mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700684 engine.setupFillWithColor(red, green, blue, alpha);
David Sodman9eeae692017-11-02 10:53:32 -0700685 engine.drawMesh(getBE().mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800686}
687
chaviwa76b2712017-09-20 12:02:26 -0700688void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
David Sodman10a41ff2018-08-05 12:14:17 -0700689 getBE().compositionInfo.firstClear = true;
690 computeGeometry(renderArea, getBE().mMesh, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800691}
692
David Sodman10a41ff2018-08-05 12:14:17 -0700693void Layer::setCompositionType(int32_t displayId, HWC2::Composition type, bool /*callIntoHwc*/) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700694 if (getBE().mHwcLayers.count(displayId) == 0) {
Chia-I Wu30505fb2018-03-26 16:20:31 -0700695 ALOGE("setCompositionType called without a valid HWC layer");
696 return;
David Sodman10a41ff2018-08-05 12:14:17 -0700697 } else {
698 if (getBE().mHwcLayers[displayId].compositionType != type) {
699 ALOGV("setCompositionType: Changing compositionType from %s to %s",
700 to_string(getBE().mHwcLayers[displayId].compositionType).c_str(),
701 to_string(type).c_str());
702 getBE().mHwcLayers[displayId].compositionType = type;
Chia-I Wu30505fb2018-03-26 16:20:31 -0700703 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800704 }
705}
706
Dominik Laskowski7e045462018-05-30 13:02:02 -0700707HWC2::Composition Layer::getCompositionType(int32_t displayId) const {
David Sodman15fb96e2018-01-07 10:23:24 -0800708 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700709 // If we're querying the composition type for a display that does not
710 // have a HWC counterpart, then it will always be Client
711 return HWC2::Composition::Client;
712 }
David Sodman15fb96e2018-01-07 10:23:24 -0800713 return getBE().mHwcLayers[displayId].compositionType;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800714}
715
Dominik Laskowski7e045462018-05-30 13:02:02 -0700716void Layer::setClearClientTarget(int32_t displayId, bool clear) {
717 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800718 ALOGE("setClearClientTarget called without a valid HWC layer");
719 return;
720 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700721 getBE().mHwcLayers[displayId].clearClientTarget = clear;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800722}
723
Dominik Laskowski7e045462018-05-30 13:02:02 -0700724bool Layer::getClearClientTarget(int32_t displayId) const {
725 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800726 ALOGE("getClearClientTarget called without a valid HWC layer");
727 return false;
728 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700729 return getBE().mHwcLayers.at(displayId).clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800730}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800731
Dan Stozacac35382016-01-27 12:21:06 -0800732bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
733 if (point->getFrameNumber() <= mCurrentFrameNumber) {
734 // Don't bother with a SyncPoint, since we've already latched the
735 // relevant frame
736 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700737 }
738
Dan Stozacac35382016-01-27 12:21:06 -0800739 Mutex::Autolock lock(mLocalSyncPointMutex);
740 mLocalSyncPoints.push_back(point);
741 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700742}
743
Mathias Agopian13127d82013-03-05 17:47:11 -0800744void Layer::setFiltering(bool filtering) {
745 mFiltering = filtering;
746}
747
748bool Layer::getFiltering() const {
749 return mFiltering;
750}
751
Mathias Agopian13127d82013-03-05 17:47:11 -0800752// ----------------------------------------------------------------------------
753// local state
754// ----------------------------------------------------------------------------
755
Peiyong Lin833074a2018-08-28 11:53:54 -0700756void Layer::computeGeometry(const RenderArea& renderArea,
757 renderengine::Mesh& mesh,
chaviwa76b2712017-09-20 12:02:26 -0700758 bool useIdentityTransform) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700759 const ui::Transform renderAreaTransform(renderArea.getTransform());
chaviwa76b2712017-09-20 12:02:26 -0700760 const uint32_t height = renderArea.getHeight();
Dan Stoza80d61162017-12-20 15:57:52 -0800761 FloatRect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700762
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000763 vec2 lt = vec2(win.left, win.top);
764 vec2 lb = vec2(win.left, win.bottom);
765 vec2 rb = vec2(win.right, win.bottom);
766 vec2 rt = vec2(win.right, win.top);
767
Peiyong Linefefaac2018-08-17 12:27:51 -0700768 ui::Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000769 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700770 lt = layerTransform.transform(lt);
771 lb = layerTransform.transform(lb);
772 rb = layerTransform.transform(rb);
773 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000774 }
775
Peiyong Lin833074a2018-08-28 11:53:54 -0700776 renderengine::Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700777 position[0] = renderAreaTransform.transform(lt);
778 position[1] = renderAreaTransform.transform(lb);
779 position[2] = renderAreaTransform.transform(rb);
780 position[3] = renderAreaTransform.transform(rt);
David Sodman41fdfc92017-11-06 16:09:56 -0800781 for (size_t i = 0; i < 4; i++) {
chaviwa76b2712017-09-20 12:02:26 -0700782 position[i].y = height - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -0800783 }
784}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800785
David Sodman41fdfc92017-11-06 16:09:56 -0800786bool Layer::isSecure() const {
Dan Stoza23116082015-06-18 14:58:39 -0700787 const Layer::State& s(mDrawingState);
788 return (s.flags & layer_state_t::eLayerSecure);
789}
790
Mathias Agopian13127d82013-03-05 17:47:11 -0800791void Layer::setVisibleRegion(const Region& visibleRegion) {
792 // always called from main thread
793 this->visibleRegion = visibleRegion;
794}
795
796void Layer::setCoveredRegion(const Region& coveredRegion) {
797 // always called from main thread
798 this->coveredRegion = coveredRegion;
799}
800
David Sodman41fdfc92017-11-06 16:09:56 -0800801void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800802 // always called from main thread
803 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
804}
805
Robert Carre5f4f692018-01-12 13:12:28 -0800806void Layer::clearVisibilityRegions() {
807 visibleRegion.clear();
808 visibleNonTransparentRegion.clear();
809 coveredRegion.clear();
810}
811
Mathias Agopian13127d82013-03-05 17:47:11 -0800812// ----------------------------------------------------------------------------
813// transaction
814// ----------------------------------------------------------------------------
815
Dan Stoza7dde5992015-05-22 09:51:44 -0700816void Layer::pushPendingState() {
817 if (!mCurrentState.modified) {
818 return;
819 }
820
Dan Stoza7dde5992015-05-22 09:51:44 -0700821 // If this transaction is waiting on the receipt of a frame, generate a sync
822 // point and send it to the remote layer.
Marissa Wallf58c14b2018-07-24 10:50:43 -0700823 if (mCurrentState.barrierLayer_legacy != nullptr) {
824 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800825 if (barrierLayer == nullptr) {
826 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700827 // If we can't promote the layer we are intended to wait on,
828 // then it is expired or otherwise invalid. Allow this transaction
829 // to be applied as per normal (no synchronization).
Marissa Wallf58c14b2018-07-24 10:50:43 -0700830 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800831 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700832 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy);
Robert Carr0d480722017-01-10 16:42:54 -0800833 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800834 mRemoteSyncPoints.push_back(std::move(syncPoint));
835 } else {
836 // We already missed the frame we're supposed to synchronize
837 // on, so go ahead and apply the state update
Marissa Wallf58c14b2018-07-24 10:50:43 -0700838 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800839 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700840 }
841
Dan Stoza7dde5992015-05-22 09:51:44 -0700842 // Wake us up to check if the frame has been received
843 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700844 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700845 }
846 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700847 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700848}
849
Pablo Ceballos05289c22016-04-14 15:49:55 -0700850void Layer::popPendingState(State* stateToCommit) {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700851 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700852
853 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700854 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700855}
856
Pablo Ceballos05289c22016-04-14 15:49:55 -0700857bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700858 bool stateUpdateAvailable = false;
859 while (!mPendingStates.empty()) {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700860 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700861 if (mRemoteSyncPoints.empty()) {
862 // If we don't have a sync point for this, apply it anyway. It
863 // will be visually wrong, but it should keep us from getting
864 // into too much trouble.
865 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700866 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700867 stateUpdateAvailable = true;
868 continue;
869 }
870
Marissa Wallf58c14b2018-07-24 10:50:43 -0700871 if (mRemoteSyncPoints.front()->getFrameNumber() !=
872 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800873 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800874
875 // Signal our end of the sync point and then dispose of it
876 mRemoteSyncPoints.front()->setTransactionApplied();
877 mRemoteSyncPoints.pop_front();
878 continue;
879 }
880
Dan Stoza7dde5992015-05-22 09:51:44 -0700881 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
882 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700883 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700884 stateUpdateAvailable = true;
885
886 // Signal our end of the sync point and then dispose of it
887 mRemoteSyncPoints.front()->setTransactionApplied();
888 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800889 } else {
890 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700891 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700892 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700893 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700894 stateUpdateAvailable = true;
895 }
896 }
897
898 // If we still have pending updates, wake SurfaceFlinger back up and point
899 // it at this layer so we can process them
900 if (!mPendingStates.empty()) {
901 setTransactionFlags(eTransactionNeeded);
902 mFlinger->setTransactionFlags(eTraversalNeeded);
903 }
904
905 mCurrentState.modified = false;
906 return stateUpdateAvailable;
907}
908
Marissa Wall61c58622018-07-18 10:12:20 -0700909uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700910 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800911
Marissa Wall61c58622018-07-18 10:12:20 -0700912 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
913 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700914
David Sodmaneb085e02017-10-05 18:49:04 -0700915 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700916 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000917 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800918 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
919 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
920 " requested={ wh={%4u,%4u} }}\n"
921 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
922 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700923 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700924 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
925 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
926 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
927 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
928 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700929 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
930 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
931 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800932 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700933
Robert Carre392b552017-09-19 12:16:05 -0700934 // Don't let Layer::doTransaction update the drawing state
935 // if we have a pending resize, unless we are in fixed-size mode.
936 // the drawing state will be updated only once we receive a buffer
937 // with the correct size.
938 //
939 // In particular, we want to make sure the clip (which is part
940 // of the geometry state) is latched together with the size but is
941 // latched immediately when no resizing is involved.
942 //
943 // If a sideband stream is attached, however, we want to skip this
944 // optimization so that transactions aren't missed when a buffer
945 // never arrives
946 //
947 // In the case that we don't have a buffer we ignore other factors
948 // and avoid entering the resizePending state. At a high level the
949 // resizePending state is to avoid applying the state of the new buffer
950 // to the old buffer. However in the state where we don't have an old buffer
951 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700952 const bool resizePending =
953 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
954 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
David Sodman0cc69182017-11-17 12:12:07 -0800955 (getBE().compositionInfo.mBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700956 if (!isFixedSize()) {
David Sodman0cc69182017-11-17 12:12:07 -0800957 if (resizePending && getBE().compositionInfo.hwc.sidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700958 flags |= eDontUpdateGeometryState;
959 }
960 }
961
Robert Carr7bf247e2017-05-18 14:02:49 -0700962 // Here we apply various requested geometry states, depending on our
963 // latching configuration. See Layer.h for a detailed discussion of
964 // how geometry latching is controlled.
965 if (!(flags & eDontUpdateGeometryState)) {
Pablo Ceballos7d052572016-06-02 17:46:05 -0700966 Layer::State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700967
968 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
969 // mode, which causes attributes which normally latch regardless of scaling mode,
970 // to be delayed. We copy the requested state to the active state making sure
971 // to respect these rules (again see Layer.h for a detailed discussion).
972 //
973 // There is an awkward asymmetry in the handling of the crop states in the position
974 // states, as can be seen below. Largely this arises from position and transform
975 // being stored in the same data structure while having different latching rules.
976 // b/38182305
977 //
Marissa Wall61c58622018-07-18 10:12:20 -0700978 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700979 // applyPendingStates in the presence of deferred transactions.
980 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -0700981 float tx = stateToCommit->active_legacy.transform.tx();
982 float ty = stateToCommit->active_legacy.transform.ty();
983 stateToCommit->active_legacy = stateToCommit->requested_legacy;
984 stateToCommit->active_legacy.transform.set(tx, ty);
985 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700986 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700987 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -0700988 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700989 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800990 }
991
Marissa Wall61c58622018-07-18 10:12:20 -0700992 return flags;
993}
994
995uint32_t Layer::doTransaction(uint32_t flags) {
996 ATRACE_CALL();
997
998 pushPendingState();
999 Layer::State c = getCurrentState();
1000 if (!applyPendingStates(&c)) {
1001 return 0;
1002 }
1003
1004 flags = doTransactionResize(flags, &c);
1005
1006 const Layer::State& s(getDrawingState());
1007
1008 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001009 // invalidate and recompute the visible regions if needed
1010 flags |= Layer::eVisibleRegion;
1011 }
1012
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001013 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001014 // invalidate and recompute the visible regions if needed
1015 flags |= eVisibleRegion;
1016 this->contentDirty = true;
1017
1018 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -07001019 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -07001020 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -08001021 }
1022
Dan Stozac8145172016-04-28 16:29:06 -07001023 // If the layer is hidden, signal and clear out all local sync points so
1024 // that transactions for layers depending on this layer's frames becoming
1025 // visible are not blocked
1026 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001027 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001028 }
1029
Mathias Agopian13127d82013-03-05 17:47:11 -08001030 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001031 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001032 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001033}
1034
Pablo Ceballos05289c22016-04-14 15:49:55 -07001035void Layer::commitTransaction(const State& stateToCommit) {
1036 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001037}
1038
Mathias Agopian13127d82013-03-05 17:47:11 -08001039uint32_t Layer::getTransactionFlags(uint32_t flags) {
1040 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1041}
1042
1043uint32_t Layer::setTransactionFlags(uint32_t flags) {
1044 return android_atomic_or(flags, &mTransactionFlags);
1045}
1046
Robert Carr82364e32016-05-15 11:27:47 -07001047bool Layer::setPosition(float x, float y, bool immediate) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001048 if (mCurrentState.requested_legacy.transform.tx() == x &&
1049 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001050 return false;
1051 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001052
1053 // We update the requested and active position simultaneously because
1054 // we want to apply the position portion of the transform matrix immediately,
1055 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Marissa Wallf58c14b2018-07-24 10:50:43 -07001056 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001057 if (immediate && !mFreezeGeometryUpdates) {
1058 // Here we directly update the active state
1059 // unlike other setters, because we store it within
1060 // the transform, but use different latching rules.
1061 // b/38182305
Marissa Wallf58c14b2018-07-24 10:50:43 -07001062 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001063 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001064 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001065
Dan Stoza7dde5992015-05-22 09:51:44 -07001066 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001067 setTransactionFlags(eTransactionNeeded);
1068 return true;
1069}
Robert Carr82364e32016-05-15 11:27:47 -07001070
Robert Carr1f0a16a2016-10-24 16:27:39 -07001071bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1072 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1073 if (idx < 0) {
1074 return false;
1075 }
1076 if (childLayer->setLayer(z)) {
1077 mCurrentChildren.removeAt(idx);
1078 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001079 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001080 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001081 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001082}
1083
Robert Carr503c7042017-09-27 15:06:08 -07001084bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1085 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1086 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1087 if (idx < 0) {
1088 return false;
1089 }
1090 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1091 mCurrentChildren.removeAt(idx);
1092 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001093 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001094 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001095 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001096}
1097
Robert Carrae060832016-11-28 10:51:00 -08001098bool Layer::setLayer(int32_t z) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001099 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001100 mCurrentState.sequence++;
1101 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001102 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001103
1104 // Discard all relative layering.
1105 if (mCurrentState.zOrderRelativeOf != nullptr) {
1106 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1107 if (strongRelative != nullptr) {
1108 strongRelative->removeZOrderRelative(this);
1109 }
1110 mCurrentState.zOrderRelativeOf = nullptr;
1111 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001112 setTransactionFlags(eTransactionNeeded);
1113 return true;
1114}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001115
Robert Carrdb66e622017-04-10 16:55:57 -07001116void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1117 mCurrentState.zOrderRelatives.remove(relative);
1118 mCurrentState.sequence++;
1119 mCurrentState.modified = true;
1120 setTransactionFlags(eTransactionNeeded);
1121}
1122
1123void Layer::addZOrderRelative(const wp<Layer>& relative) {
1124 mCurrentState.zOrderRelatives.add(relative);
1125 mCurrentState.modified = true;
1126 mCurrentState.sequence++;
1127 setTransactionFlags(eTransactionNeeded);
1128}
1129
Robert Carr503d2bd2017-12-04 15:49:47 -08001130bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001131 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1132 if (handle == nullptr) {
1133 return false;
1134 }
1135 sp<Layer> relative = handle->owner.promote();
1136 if (relative == nullptr) {
1137 return false;
1138 }
1139
Robert Carr503d2bd2017-12-04 15:49:47 -08001140 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1141 mCurrentState.zOrderRelativeOf == relative) {
1142 return false;
1143 }
1144
Robert Carrdb66e622017-04-10 16:55:57 -07001145 mCurrentState.sequence++;
1146 mCurrentState.modified = true;
Robert Carr503d2bd2017-12-04 15:49:47 -08001147 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001148
chaviw9ab4bd12017-11-03 13:11:00 -07001149 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
1150 if (oldZOrderRelativeOf != nullptr) {
1151 oldZOrderRelativeOf->removeZOrderRelative(this);
1152 }
Robert Carrdb66e622017-04-10 16:55:57 -07001153 mCurrentState.zOrderRelativeOf = relative;
1154 relative->addZOrderRelative(this);
1155
1156 setTransactionFlags(eTransactionNeeded);
1157
1158 return true;
1159}
1160
Mathias Agopian13127d82013-03-05 17:47:11 -08001161bool Layer::setSize(uint32_t w, uint32_t h) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001162 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
1163 return false;
1164 mCurrentState.requested_legacy.w = w;
1165 mCurrentState.requested_legacy.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001166 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001167 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001168
1169 // record the new size, from this point on, when the client request
1170 // a buffer, it'll get the new size.
1171 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001172 return true;
1173}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001174bool Layer::setAlpha(float alpha) {
David Sodman41fdfc92017-11-06 16:09:56 -08001175 if (mCurrentState.color.a == alpha) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001176 mCurrentState.sequence++;
chaviw13fdc492017-06-27 12:40:18 -07001177 mCurrentState.color.a = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001178 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001179 setTransactionFlags(eTransactionNeeded);
1180 return true;
1181}
chaviw13fdc492017-06-27 12:40:18 -07001182
1183bool Layer::setColor(const half3& color) {
David Sodman41fdfc92017-11-06 16:09:56 -08001184 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g &&
1185 color.b == mCurrentState.color.b)
chaviw13fdc492017-06-27 12:40:18 -07001186 return false;
1187
1188 mCurrentState.sequence++;
1189 mCurrentState.color.r = color.r;
1190 mCurrentState.color.g = color.g;
1191 mCurrentState.color.b = color.b;
1192 mCurrentState.modified = true;
1193 setTransactionFlags(eTransactionNeeded);
1194 return true;
1195}
1196
Robert Carrd4ae7f32018-06-07 16:10:57 -07001197bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1198 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001199 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001200 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1201
1202 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1203 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1204 return false;
1205 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001206 mCurrentState.sequence++;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001207 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1208 matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001209 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001210 setTransactionFlags(eTransactionNeeded);
1211 return true;
1212}
Marissa Wall61c58622018-07-18 10:12:20 -07001213
Mathias Agopian13127d82013-03-05 17:47:11 -08001214bool Layer::setTransparentRegionHint(const Region& transparent) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001215 mCurrentState.requestedTransparentRegion_legacy = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001216 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001217 setTransactionFlags(eTransactionNeeded);
1218 return true;
1219}
1220bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1221 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
David Sodman41fdfc92017-11-06 16:09:56 -08001222 if (mCurrentState.flags == newFlags) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001223 mCurrentState.sequence++;
1224 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001225 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001226 setTransactionFlags(eTransactionNeeded);
1227 return true;
1228}
Robert Carr99e27f02016-06-16 15:18:02 -07001229
Marissa Wallf58c14b2018-07-24 10:50:43 -07001230bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
1231 if (mCurrentState.requestedCrop_legacy == crop) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001232 mCurrentState.sequence++;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001233 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001234 if (immediate && !mFreezeGeometryUpdates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001235 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001236 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001237 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1238
Dan Stoza7dde5992015-05-22 09:51:44 -07001239 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001240 setTransactionFlags(eTransactionNeeded);
1241 return true;
1242}
Robert Carr8d5227b2017-03-16 15:41:03 -07001243
Robert Carrc3574f72016-03-24 12:19:32 -07001244bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001245 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001246 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001247 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001248 return true;
1249}
1250
rongliucfb187b2018-03-14 12:26:23 -07001251void Layer::setInfo(int32_t type, int32_t appId) {
David Sodman41fdfc92017-11-06 16:09:56 -08001252 mCurrentState.appId = appId;
1253 mCurrentState.type = type;
1254 mCurrentState.modified = true;
1255 setTransactionFlags(eTransactionNeeded);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001256}
1257
Mathias Agopian13127d82013-03-05 17:47:11 -08001258bool Layer::setLayerStack(uint32_t layerStack) {
David Sodman41fdfc92017-11-06 16:09:56 -08001259 if (mCurrentState.layerStack == layerStack) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001260 mCurrentState.sequence++;
1261 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001262 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001263 setTransactionFlags(eTransactionNeeded);
1264 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001265}
1266
Robert Carr1f0a16a2016-10-24 16:27:39 -07001267uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001268 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001269 if (p == nullptr) {
1270 return getDrawingState().layerStack;
1271 }
1272 return p->getLayerStack();
1273}
1274
Marissa Wallf58c14b2018-07-24 10:50:43 -07001275void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
1276 mCurrentState.barrierLayer_legacy = barrierLayer;
1277 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001278 // We don't set eTransactionNeeded, because just receiving a deferral
1279 // request without any other state updates shouldn't actually induce a delay
1280 mCurrentState.modified = true;
1281 pushPendingState();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001282 mCurrentState.barrierLayer_legacy = nullptr;
1283 mCurrentState.frameNumber_legacy = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001284 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001285}
1286
Marissa Wallf58c14b2018-07-24 10:50:43 -07001287void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001288 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001289 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001290}
1291
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001292// ----------------------------------------------------------------------------
1293// pageflip handling...
1294// ----------------------------------------------------------------------------
1295
Robert Carr1f0a16a2016-10-24 16:27:39 -07001296bool Layer::isHiddenByPolicy() const {
1297 const Layer::State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001298 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001299 if (parent != nullptr && parent->isHiddenByPolicy()) {
1300 return true;
1301 }
1302 return s.flags & layer_state_t::eLayerHidden;
1303}
1304
David Sodman41fdfc92017-11-06 16:09:56 -08001305uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001306 // TODO: should we do something special if mSecure is set?
1307 if (mProtectedByApp) {
1308 // need a hardware-protected path to external video sink
1309 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001310 }
Riley Andrews03414a12014-07-01 14:22:59 -07001311 if (mPotentialCursor) {
1312 usage |= GraphicBuffer::USAGE_CURSOR;
1313 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001314 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001315 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001316}
1317
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001318void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001319 uint32_t orientation = 0;
1320 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001321 // The transform hint is used to improve performance, but we can
1322 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001323 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001324 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001325 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001326 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001327 orientation = 0;
1328 }
1329 }
David Sodmaneb085e02017-10-05 18:49:04 -07001330 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001331}
1332
Mathias Agopian13127d82013-03-05 17:47:11 -08001333// ----------------------------------------------------------------------------
1334// debugging
1335// ----------------------------------------------------------------------------
1336
Marissa Wall61c58622018-07-18 10:12:20 -07001337// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001338LayerDebugInfo Layer::getLayerDebugInfo() const {
1339 LayerDebugInfo info;
1340 const Layer::State& ds = getDrawingState();
1341 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001342 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001343 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
1344 info.mType = String8(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001345 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001346 info.mVisibleRegion = visibleRegion;
1347 info.mSurfaceDamageRegion = surfaceDamageRegion;
1348 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001349 info.mX = ds.active_legacy.transform.tx();
1350 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001351 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001352 info.mWidth = ds.active_legacy.w;
1353 info.mHeight = ds.active_legacy.h;
1354 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001355 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001356 info.mFlags = ds.flags;
1357 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001358 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001359 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1360 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1361 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1362 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001363 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001364 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001365 if (buffer != 0) {
1366 info.mActiveBufferWidth = buffer->getWidth();
1367 info.mActiveBufferHeight = buffer->getHeight();
1368 info.mActiveBufferStride = buffer->getStride();
1369 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001370 } else {
1371 info.mActiveBufferWidth = 0;
1372 info.mActiveBufferHeight = 0;
1373 info.mActiveBufferStride = 0;
1374 info.mActiveBufferFormat = 0;
1375 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001376 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001377 info.mNumQueuedFrames = getQueuedFrameCount();
1378 info.mRefreshPending = isBufferLatched();
1379 info.mIsOpaque = isOpaque(ds);
1380 info.mContentDirty = contentDirty;
1381 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001382}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001383
Dan Stozae22aec72016-08-01 13:20:59 -07001384void Layer::miniDumpHeader(String8& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001385 result.append("-------------------------------");
1386 result.append("-------------------------------");
1387 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001388 result.append(" Layer name\n");
1389 result.append(" Z | ");
1390 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001391 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001392 result.append(" Disp Frame (LTRB) | ");
1393 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001394 result.append("-------------------------------");
1395 result.append("-------------------------------");
1396 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001397}
1398
Dominik Laskowski7e045462018-05-30 13:02:02 -07001399void Layer::miniDump(String8& result, int32_t displayId) const {
1400 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stozae22aec72016-08-01 13:20:59 -07001401 return;
1402 }
1403
1404 String8 name;
1405 if (mName.length() > 77) {
1406 std::string shortened;
1407 shortened.append(mName.string(), 36);
1408 shortened.append("[...]");
1409 shortened.append(mName.string() + (mName.length() - 36), 36);
1410 name = shortened.c_str();
1411 } else {
1412 name = mName;
1413 }
1414
1415 result.appendFormat(" %s\n", name.string());
1416
1417 const Layer::State& layerState(getDrawingState());
Lloyd Pique074e8122018-07-26 12:57:23 -07001418 const LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers.at(displayId);
Chia-I Wu1e043612018-03-01 09:45:09 -08001419 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
1420 result.appendFormat(" rel %6d | ", layerState.z);
1421 } else {
1422 result.appendFormat(" %10d | ", layerState.z);
1423 }
Dominik Laskowski7e045462018-05-30 13:02:02 -07001424 result.appendFormat("%10s | ", to_string(getCompositionType(displayId)).c_str());
Lloyd Pique074e8122018-07-26 12:57:23 -07001425 result.appendFormat("%10s | ", to_string(hwcInfo.transform).c_str());
1426 const Rect& frame = hwcInfo.displayFrame;
David Sodman41fdfc92017-11-06 16:09:56 -08001427 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique074e8122018-07-26 12:57:23 -07001428 const FloatRect& crop = hwcInfo.sourceCrop;
David Sodman41fdfc92017-11-06 16:09:56 -08001429 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 -07001430
David Sodman7e4ae112018-02-09 15:02:28 -08001431 result.append("- - - - - - - - - - - - - - - -\n");
1432
1433 std::string compositionInfoStr;
1434 getBE().compositionInfo.dump(compositionInfoStr, "compositionInfo");
1435 result.append(compositionInfoStr.c_str());
1436
Yichi Chen6ca35192018-05-29 12:20:43 +08001437 result.append("- - - - - - - - - - - - - - - -");
1438 result.append("- - - - - - - - - - - - - - - -");
1439 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001440}
Dan Stozae22aec72016-08-01 13:20:59 -07001441
Svetoslavd85084b2014-03-20 10:28:31 -07001442void Layer::dumpFrameStats(String8& result) const {
1443 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001444}
1445
Svetoslavd85084b2014-03-20 10:28:31 -07001446void Layer::clearFrameStats() {
1447 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001448}
1449
Jamie Gennis6547ff42013-07-16 20:12:42 -07001450void Layer::logFrameStats() {
1451 mFrameTracker.logAndResetStats(mName);
1452}
1453
Svetoslavd85084b2014-03-20 10:28:31 -07001454void Layer::getFrameStats(FrameStats* outStats) const {
1455 mFrameTracker.getStats(outStats);
1456}
1457
Brian Andersond6927fb2016-07-23 23:37:30 -07001458void Layer::dumpFrameEvents(String8& result) {
David Sodman41fdfc92017-11-06 16:09:56 -08001459 result.appendFormat("- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001460 Mutex::Autolock lock(mFrameEventHistoryMutex);
1461 mFrameEventHistory.checkFencesForCompletion();
1462 mFrameEventHistory.dump(result);
1463}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001464
Brian Anderson5ea5e592016-12-01 16:54:33 -08001465void Layer::onDisconnect() {
1466 Mutex::Autolock lock(mFrameEventHistoryMutex);
1467 mFrameEventHistory.onDisconnect();
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001468 mTimeStats.onDisconnect(getName().c_str());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001469}
1470
Brian Anderson3890c392016-07-25 12:48:08 -07001471void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001472 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001473 if (newTimestamps) {
1474 mTimeStats.setPostTime(getName().c_str(), newTimestamps->frameNumber,
1475 newTimestamps->postedTime);
1476 }
1477
Brian Andersond6927fb2016-07-23 23:37:30 -07001478 Mutex::Autolock lock(mFrameEventHistoryMutex);
1479 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001480 // If there are any unsignaled fences in the aquire timeline at this
1481 // point, the previously queued frame hasn't been latched yet. Go ahead
1482 // and try to get the signal time here so the syscall is taken out of
1483 // the main thread's critical path.
1484 mAcquireTimeline.updateSignalTimes();
1485 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001486 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001487 mFrameEventHistory.addQueue(*newTimestamps);
1488 }
1489
Brian Anderson3890c392016-07-25 12:48:08 -07001490 if (outDelta) {
1491 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001492 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001493}
Dan Stozae77c7662016-05-13 11:37:28 -07001494
Chia-I Wu98f1c102017-05-30 14:54:08 -07001495size_t Layer::getChildrenCount() const {
1496 size_t count = 0;
1497 for (const sp<Layer>& child : mCurrentChildren) {
1498 count += 1 + child->getChildrenCount();
1499 }
1500 return count;
1501}
1502
Robert Carr1f0a16a2016-10-24 16:27:39 -07001503void Layer::addChild(const sp<Layer>& layer) {
1504 mCurrentChildren.add(layer);
1505 layer->setParent(this);
1506}
1507
1508ssize_t Layer::removeChild(const sp<Layer>& layer) {
1509 layer->setParent(nullptr);
1510 return mCurrentChildren.remove(layer);
1511}
1512
Robert Carr1db73f62016-12-21 12:58:51 -08001513bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1514 sp<Handle> handle = nullptr;
1515 sp<Layer> newParent = nullptr;
1516 if (newParentHandle == nullptr) {
1517 return false;
1518 }
1519 handle = static_cast<Handle*>(newParentHandle.get());
1520 newParent = handle->owner.promote();
1521 if (newParent == nullptr) {
1522 ALOGE("Unable to promote Layer handle");
1523 return false;
1524 }
1525
1526 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001527 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001528
1529 sp<Client> client(child->mClientRef.promote());
1530 if (client != nullptr) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001531 client->updateParent(newParent);
Robert Carr1db73f62016-12-21 12:58:51 -08001532 }
1533 }
1534 mCurrentChildren.clear();
1535
1536 return true;
1537}
1538
Robert Carr15eae092018-03-23 13:43:53 -07001539void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001540 for (const sp<Layer>& child : mDrawingChildren) {
1541 child->mDrawingParent = newParent;
1542 }
1543}
1544
chaviwf1961f72017-09-18 16:41:07 -07001545bool Layer::reparent(const sp<IBinder>& newParentHandle) {
1546 if (newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001547 return false;
1548 }
1549
1550 auto handle = static_cast<Handle*>(newParentHandle.get());
1551 sp<Layer> newParent = handle->owner.promote();
1552 if (newParent == nullptr) {
1553 ALOGE("Unable to promote Layer handle");
1554 return false;
1555 }
1556
chaviwf1961f72017-09-18 16:41:07 -07001557 sp<Layer> parent = getParent();
1558 if (parent != nullptr) {
1559 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001560 }
chaviwf1961f72017-09-18 16:41:07 -07001561 newParent->addChild(this);
chaviw06178942017-07-27 10:25:59 -07001562
chaviwf1961f72017-09-18 16:41:07 -07001563 sp<Client> client(mClientRef.promote());
chaviw06178942017-07-27 10:25:59 -07001564 sp<Client> newParentClient(newParent->mClientRef.promote());
1565
chaviwf1961f72017-09-18 16:41:07 -07001566 if (client != newParentClient) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001567 client->updateParent(newParent);
chaviw06178942017-07-27 10:25:59 -07001568 }
1569
chaviw06178942017-07-27 10:25:59 -07001570 return true;
1571}
1572
Robert Carr9524cb32017-02-13 11:32:32 -08001573bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001574 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001575 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001576 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001577 if (client != nullptr && parentClient != client) {
Robert Carr7f619b22017-11-06 12:56:35 -08001578 client->detachLayer(child.get());
1579 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001580 }
Robert Carr7f619b22017-11-06 12:56:35 -08001581 }
Robert Carr9524cb32017-02-13 11:32:32 -08001582
1583 return true;
1584}
1585
Chia-I Wu11481472018-05-04 10:43:19 -07001586bool Layer::isLegacyDataSpace() const {
1587 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001588 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001589 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001590}
1591
Robert Carr1f0a16a2016-10-24 16:27:39 -07001592void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001593 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001594}
1595
1596void Layer::clearSyncPoints() {
1597 for (const auto& child : mCurrentChildren) {
1598 child->clearSyncPoints();
1599 }
1600
1601 Mutex::Autolock lock(mLocalSyncPointMutex);
1602 for (auto& point : mLocalSyncPoints) {
1603 point->setFrameAvailable();
1604 }
1605 mLocalSyncPoints.clear();
1606}
1607
1608int32_t Layer::getZ() const {
1609 return mDrawingState.z;
1610}
1611
Robert Carr29abff82017-12-04 13:51:20 -08001612bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1613 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1614 const State& state = useDrawing ? mDrawingState : mCurrentState;
1615 return state.zOrderRelativeOf != nullptr;
1616}
1617
David Sodman41fdfc92017-11-06 16:09:56 -08001618__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001619 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001620 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1621 "makeTraversalList received invalid stateSet");
1622 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1623 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1624 const State& state = useDrawing ? mDrawingState : mCurrentState;
1625
Robert Carr29abff82017-12-04 13:51:20 -08001626 if (state.zOrderRelatives.size() == 0) {
1627 *outSkipRelativeZUsers = true;
1628 return children;
1629 }
1630
chaviwfd462612018-05-31 16:11:27 -07001631 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001632 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001633 sp<Layer> strongRelative = weakRelative.promote();
1634 if (strongRelative != nullptr) {
1635 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001636 }
1637 }
1638
Dan Stoza412903f2017-04-27 13:42:17 -07001639 for (const sp<Layer>& child : children) {
Robert Carr503c7042017-09-27 15:06:08 -07001640 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1641 if (childState.zOrderRelativeOf != nullptr) {
1642 continue;
1643 }
Robert Carrdb66e622017-04-10 16:55:57 -07001644 traverse.add(child);
1645 }
1646
1647 return traverse;
1648}
1649
Robert Carr1f0a16a2016-10-24 16:27:39 -07001650/**
Robert Carrdb66e622017-04-10 16:55:57 -07001651 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001652 */
Dan Stoza412903f2017-04-27 13:42:17 -07001653void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001654 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1655 // produce a new list for traversing, including our relatives, and not including our children
1656 // who are relatives of another surface. In the case that there are no relative Z,
1657 // makeTraversalList returns our children directly to avoid significant overhead.
1658 // However in this case we need to take the responsibility for filtering children which
1659 // are relatives of another surface here.
1660 bool skipRelativeZUsers = false;
1661 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001662
Robert Carr1f0a16a2016-10-24 16:27:39 -07001663 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001664 for (; i < list.size(); i++) {
1665 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001666 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1667 continue;
1668 }
1669
Robert Carrdb66e622017-04-10 16:55:57 -07001670 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001671 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001672 }
Dan Stoza412903f2017-04-27 13:42:17 -07001673 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001674 }
Robert Carr29abff82017-12-04 13:51:20 -08001675
Dan Stoza412903f2017-04-27 13:42:17 -07001676 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001677 for (; i < list.size(); i++) {
1678 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001679
1680 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1681 continue;
1682 }
Dan Stoza412903f2017-04-27 13:42:17 -07001683 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001684 }
1685}
1686
1687/**
Robert Carrdb66e622017-04-10 16:55:57 -07001688 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001689 */
Dan Stoza412903f2017-04-27 13:42:17 -07001690void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1691 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001692 // See traverseInZOrder for documentation.
1693 bool skipRelativeZUsers = false;
1694 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001695
Robert Carr1f0a16a2016-10-24 16:27:39 -07001696 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001697 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001698 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001699
1700 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1701 continue;
1702 }
1703
Robert Carrdb66e622017-04-10 16:55:57 -07001704 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001705 break;
1706 }
Dan Stoza412903f2017-04-27 13:42:17 -07001707 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001708 }
Dan Stoza412903f2017-04-27 13:42:17 -07001709 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001710 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001711 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001712
1713 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1714 continue;
1715 }
1716
Dan Stoza412903f2017-04-27 13:42:17 -07001717 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001718 }
1719}
1720
chaviw4b129c22018-04-09 16:19:43 -07001721LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1722 const std::vector<Layer*>& layersInTree) {
1723 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1724 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001725 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1726 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
chaviw4b129c22018-04-09 16:19:43 -07001727 const State& state = useDrawing ? mDrawingState : mCurrentState;
1728
chaviwfd462612018-05-31 16:11:27 -07001729 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001730 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1731 sp<Layer> strongRelative = weakRelative.promote();
1732 // Only add relative layers that are also descendents of the top most parent of the tree.
1733 // If a relative layer is not a descendent, then it should be ignored.
1734 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1735 traverse.add(strongRelative);
1736 }
1737 }
1738
1739 for (const sp<Layer>& child : children) {
1740 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1741 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1742 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1743 // the child here since it won't be added later as a relative.
1744 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1745 childState.zOrderRelativeOf.promote().get())) {
1746 continue;
1747 }
1748 traverse.add(child);
1749 }
1750
1751 return traverse;
1752}
1753
1754void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1755 LayerVector::StateSet stateSet,
1756 const LayerVector::Visitor& visitor) {
1757 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001758
1759 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001760 for (; i < list.size(); i++) {
1761 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001762 if (relative->getZ() >= 0) {
1763 break;
1764 }
chaviw4b129c22018-04-09 16:19:43 -07001765 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001766 }
chaviw4b129c22018-04-09 16:19:43 -07001767
chaviwa76b2712017-09-20 12:02:26 -07001768 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001769 for (; i < list.size(); i++) {
1770 const auto& relative = list[i];
1771 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001772 }
1773}
1774
chaviw4b129c22018-04-09 16:19:43 -07001775std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1776 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1777 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1778
1779 std::vector<Layer*> layersInTree = {this};
1780 for (size_t i = 0; i < children.size(); i++) {
1781 const auto& child = children[i];
1782 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1783 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1784 }
1785
1786 return layersInTree;
1787}
1788
1789void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1790 const LayerVector::Visitor& visitor) {
1791 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1792 std::sort(layersInTree.begin(), layersInTree.end());
1793 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1794}
1795
Peiyong Linefefaac2018-08-17 12:27:51 -07001796ui::Transform Layer::getTransform() const {
1797 ui::Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001798 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001799 if (p != nullptr) {
1800 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07001801
1802 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
1803 // it isFixedSize) then there may be additional scaling not accounted
1804 // for in the transform. We need to mirror this scaling in child surfaces
1805 // or we will break the contract where WM can treat child surfaces as
1806 // pixels in the parent surface.
David Sodman0cc69182017-11-17 12:12:07 -08001807 if (p->isFixedSize() && p->getBE().compositionInfo.mBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07001808 int bufferWidth;
1809 int bufferHeight;
1810 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
David Sodman0cc69182017-11-17 12:12:07 -08001811 bufferWidth = p->getBE().compositionInfo.mBuffer->getWidth();
1812 bufferHeight = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001813 } else {
David Sodman0cc69182017-11-17 12:12:07 -08001814 bufferHeight = p->getBE().compositionInfo.mBuffer->getWidth();
1815 bufferWidth = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001816 }
Marissa Wall61c58622018-07-18 10:12:20 -07001817 float sx = p->getActiveWidth(p->getDrawingState()) / static_cast<float>(bufferWidth);
1818 float sy = p->getActiveHeight(p->getDrawingState()) / static_cast<float>(bufferHeight);
Peiyong Linefefaac2018-08-17 12:27:51 -07001819 ui::Transform extraParentScaling;
Robert Carr9b429f42017-04-17 14:56:57 -07001820 extraParentScaling.set(sx, 0, 0, sy);
1821 t = t * extraParentScaling;
1822 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001823 }
Marissa Wall61c58622018-07-18 10:12:20 -07001824 return t * getActiveTransform(getDrawingState());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001825}
1826
chaviw13fdc492017-06-27 12:40:18 -07001827half Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001828 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07001829
chaviw13fdc492017-06-27 12:40:18 -07001830 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1831 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001832}
Robert Carr6452f122017-03-21 10:41:29 -07001833
chaviw13fdc492017-06-27 12:40:18 -07001834half4 Layer::getColor() const {
1835 const half4 color(getDrawingState().color);
1836 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001837}
Robert Carr6452f122017-03-21 10:41:29 -07001838
Robert Carr1f0a16a2016-10-24 16:27:39 -07001839void Layer::commitChildList() {
1840 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1841 const auto& child = mCurrentChildren[i];
1842 child->commitChildList();
1843 }
1844 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001845 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001846}
1847
chaviw1d044282017-09-27 12:19:28 -07001848void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
1849 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1850 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1851 const State& state = useDrawing ? mDrawingState : mCurrentState;
1852
Peiyong Linefefaac2018-08-17 12:27:51 -07001853 ui::Transform requestedTransform = state.active_legacy.transform;
1854 ui::Transform transform = getTransform();
chaviw1d044282017-09-27 12:19:28 -07001855
1856 layerInfo->set_id(sequence);
1857 layerInfo->set_name(getName().c_str());
1858 layerInfo->set_type(String8(getTypeId()));
1859
1860 for (const auto& child : children) {
1861 layerInfo->add_children(child->sequence);
1862 }
1863
1864 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1865 sp<Layer> strongRelative = weakRelative.promote();
1866 if (strongRelative != nullptr) {
1867 layerInfo->add_relatives(strongRelative->sequence);
1868 }
1869 }
1870
Marissa Wallf58c14b2018-07-24 10:50:43 -07001871 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
chaviw1d044282017-09-27 12:19:28 -07001872 layerInfo->mutable_transparent_region());
1873 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
1874 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
1875
1876 layerInfo->set_layer_stack(getLayerStack());
1877 layerInfo->set_z(state.z);
1878
1879 PositionProto* position = layerInfo->mutable_position();
1880 position->set_x(transform.tx());
1881 position->set_y(transform.ty());
1882
1883 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
1884 requestedPosition->set_x(requestedTransform.tx());
1885 requestedPosition->set_y(requestedTransform.ty());
1886
1887 SizeProto* size = layerInfo->mutable_size();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001888 size->set_w(state.active_legacy.w);
1889 size->set_h(state.active_legacy.h);
chaviw1d044282017-09-27 12:19:28 -07001890
Marissa Wallf58c14b2018-07-24 10:50:43 -07001891 LayerProtoHelper::writeToProto(state.crop_legacy, layerInfo->mutable_crop());
chaviw1d044282017-09-27 12:19:28 -07001892
1893 layerInfo->set_is_opaque(isOpaque(state));
1894 layerInfo->set_invalidate(contentDirty);
Chia-I Wu01591c92018-05-22 12:03:00 -07001895
1896 // XXX (b/79210409) mCurrentDataSpace is not protected
1897 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
1898
chaviw1d044282017-09-27 12:19:28 -07001899 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1900 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
1901 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
1902 layerInfo->set_flags(state.flags);
1903
1904 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1905 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
1906
Jorim Jaggi8e0af362017-11-14 16:28:28 +01001907 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07001908 if (parent != nullptr) {
1909 layerInfo->set_parent(parent->sequence);
1910 }
1911
1912 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1913 if (zOrderRelativeOf != nullptr) {
1914 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1915 }
1916
Chia-I Wu01591c92018-05-22 12:03:00 -07001917 // XXX getBE().compositionInfo.mBuffer is not protected
David Sodman0cc69182017-11-17 12:12:07 -08001918 auto buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001919 if (buffer != nullptr) {
1920 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
Peiyong Linefefaac2018-08-17 12:27:51 -07001921 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
Yichi Chen6ca35192018-05-29 12:20:43 +08001922 layerInfo->mutable_buffer_transform());
chaviw1d044282017-09-27 12:19:28 -07001923 }
1924
1925 layerInfo->set_queued_frames(getQueuedFrameCount());
1926 layerInfo->set_refresh_pending(isBufferLatched());
rongliucfb187b2018-03-14 12:26:23 -07001927 layerInfo->set_window_type(state.type);
1928 layerInfo->set_app_id(state.appId);
chaviwadc40c22018-07-10 16:57:27 -07001929 layerInfo->set_curr_frame(mCurrentFrameNumber);
1930
1931 for (const auto& pendingState : mPendingStates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001932 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
chaviwadc40c22018-07-10 16:57:27 -07001933 if (barrierLayer != nullptr) {
1934 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
1935 barrierLayerProto->set_id(barrierLayer->sequence);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001936 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
chaviwadc40c22018-07-10 16:57:27 -07001937 }
1938 }
chaviw1d044282017-09-27 12:19:28 -07001939}
1940
Dominik Laskowski7e045462018-05-30 13:02:02 -07001941void Layer::writeToProto(LayerProto* layerInfo, int32_t displayId) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07001942 if (!hasHwcLayer(displayId)) {
1943 return;
1944 }
1945
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001946 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
1947
Dominik Laskowski7e045462018-05-30 13:02:02 -07001948 const auto& hwcInfo = getBE().mHwcLayers.at(displayId);
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001949
1950 const Rect& frame = hwcInfo.displayFrame;
1951 LayerProtoHelper::writeToProto(frame, layerInfo->mutable_hwc_frame());
1952
1953 const FloatRect& crop = hwcInfo.sourceCrop;
1954 LayerProtoHelper::writeToProto(crop, layerInfo->mutable_hwc_crop());
1955
1956 const int32_t transform = static_cast<int32_t>(hwcInfo.transform);
1957 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08001958
1959 const int32_t compositionType = static_cast<int32_t>(hwcInfo.compositionType);
1960 layerInfo->set_hwc_composition_type(compositionType);
1961
1962 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
1963 static_cast<BufferLayer*>(this)->isProtected()) {
1964 layerInfo->set_is_protected(true);
1965 } else {
1966 layerInfo->set_is_protected(false);
1967 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001968}
1969
Mathias Agopian13127d82013-03-05 17:47:11 -08001970// ---------------------------------------------------------------------------
1971
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001972}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07001973
1974#if defined(__gl_h_)
1975#error "don't include gl/gl.h in this file"
1976#endif
1977
1978#if defined(__gl2_h_)
1979#error "don't include gl2/gl2.h in this file"
1980#endif