blob: d9978b86590a3e7f9c88fe1ba9144c541f17643e [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"
David Sodman41fdfc92017-11-06 16:09:56 -080053#include "clz.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054
Mathias Agopian1b031492012-06-20 17:51:20 -070055#include "DisplayHardware/HWComposer.h"
56
Mathias Agopian875d8e12013-06-07 15:35:48 -070057#include "RenderEngine/RenderEngine.h"
58
Dan Stozac5da2712016-07-20 15:38:12 -070059#include <mutex>
chaviw1d044282017-09-27 12:19:28 -070060#include "LayerProtoHelper.h"
Dan Stozac5da2712016-07-20 15:38:12 -070061
David Sodman41fdfc92017-11-06 16:09:56 -080062#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064namespace android {
65
Mathias Agopian13127d82013-03-05 17:47:11 -080066int32_t Layer::sSequence = 1;
67
David Sodman41fdfc92017-11-06 16:09:56 -080068Layer::Layer(SurfaceFlinger* flinger, const sp<Client>& client, const String8& name, uint32_t w,
69 uint32_t h, uint32_t flags)
David Sodman0c69cad2017-08-21 12:12:51 -070070 : contentDirty(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080071 sequence(uint32_t(android_atomic_inc(&sSequence))),
72 mFlinger(flinger),
Mathias Agopian13127d82013-03-05 17:47:11 -080073 mPremultipliedAlpha(true),
David Sodman0c69cad2017-08-21 12:12:51 -070074 mName(name),
Mathias Agopian13127d82013-03-05 17:47:11 -080075 mTransactionFlags(0),
Dan Stoza7dde5992015-05-22 09:51:44 -070076 mPendingStateMutex(),
77 mPendingStates(),
Mathias Agopiana67932f2011-04-20 14:20:59 -070078 mCurrentTransform(0),
Robert Carrc3574f72016-03-24 12:19:32 -070079 mOverrideScalingMode(-1),
Dan Stozacac35382016-01-27 12:21:06 -080080 mCurrentFrameNumber(0),
Mathias Agopian82d7ab62012-01-19 18:34:40 -080081 mFrameLatencyNeeded(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080082 mFiltering(false),
83 mNeedsFiltering(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080084 mProtectedByApp(false),
Riley Andrews03414a12014-07-01 14:22:59 -070085 mClientRef(client),
Dan Stozaa4650a52015-05-12 12:56:16 -070086 mPotentialCursor(false),
David Sodmanb8af7922017-12-21 15:17:55 -080087 mFreezeGeometryUpdates(false),
chaviwfd462612018-05-31 16:11:27 -070088 mCurrentChildren(LayerVector::StateSet::Current),
89 mDrawingChildren(LayerVector::StateSet::Drawing),
David Sodman2b727ac2017-12-21 14:28:08 -080090 mBE{this, name.string()} {
Dan Stoza9e56aa02015-11-02 13:00:03 -080091
Mathias Agopiana67932f2011-04-20 14:20:59 -070092 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070093
94 uint32_t layerFlags = 0;
David Sodman41fdfc92017-11-06 16:09:56 -080095 if (flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
96 if (flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
97 if (flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070098
Mathias Agopian4d9b8222013-03-12 17:11:48 -070099 mName = name;
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700100 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700101
Marissa Wallf58c14b2018-07-24 10:50:43 -0700102 mCurrentState.active_legacy.w = w;
103 mCurrentState.active_legacy.h = h;
David Sodman0c69cad2017-08-21 12:12:51 -0700104 mCurrentState.flags = layerFlags;
Marissa Wallf58c14b2018-07-24 10:50:43 -0700105 mCurrentState.active_legacy.transform.set(0, 0);
106 mCurrentState.crop_legacy.makeInvalid();
107 mCurrentState.finalCrop_legacy.makeInvalid();
108 mCurrentState.requestedFinalCrop_legacy = mCurrentState.finalCrop_legacy;
109 mCurrentState.requestedCrop_legacy = mCurrentState.crop_legacy;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700110 mCurrentState.z = 0;
chaviw13fdc492017-06-27 12:40:18 -0700111 mCurrentState.color.a = 1.0f;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700112 mCurrentState.layerStack = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700113 mCurrentState.sequence = 0;
Marissa Wallf58c14b2018-07-24 10:50:43 -0700114 mCurrentState.requested_legacy = mCurrentState.active_legacy;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500115 mCurrentState.appId = 0;
116 mCurrentState.type = 0;
Marissa Wall61c58622018-07-18 10:12:20 -0700117 mCurrentState.active.w = 0;
118 mCurrentState.active.h = 0;
119 mCurrentState.active.transform.set(0, 0);
120 mCurrentState.transform = 0;
121 mCurrentState.transformToDisplayInverse = false;
122 mCurrentState.crop.makeInvalid();
123 mCurrentState.acquireFence = new Fence(-1);
124 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
125 mCurrentState.hdrMetadata.validTypes = 0;
126 mCurrentState.surfaceDamageRegion.clear();
127 mCurrentState.api = -1;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700128
129 // drawing state & current state are identical
130 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700131
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800132 CompositorTiming compositorTiming;
133 flinger->getCompositorTiming(&compositorTiming);
134 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200135 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Dan Stoza436ccf32018-06-21 12:10:12 -0700136}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700137
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700138Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800139 sp<Client> c(mClientRef.promote());
140 if (c != 0) {
141 c->detachLayer(this);
142 }
143
144 for (auto& point : mRemoteSyncPoints) {
145 point->setTransactionApplied();
146 }
147 for (auto& point : mLocalSyncPoints) {
148 point->setFrameAvailable();
149 }
Jamie Gennis6547ff42013-07-16 20:12:42 -0700150 mFrameTracker.logAndResetStats(mName);
Mathias Agopian96f08192010-06-02 23:28:45 -0700151}
152
Mathias Agopian13127d82013-03-05 17:47:11 -0800153// ---------------------------------------------------------------------------
154// callbacks
155// ---------------------------------------------------------------------------
156
David Sodmaneb085e02017-10-05 18:49:04 -0700157/*
158 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
159 * Layer. So, the implementation is done in BufferLayer. When called on a
160 * ColorLayer object, it's essentially a NOP.
161 */
David Sodmaneb085e02017-10-05 18:49:04 -0700162void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800163
Chia-I Wuc6657022017-08-15 11:18:17 -0700164void Layer::onRemovedFromCurrentState() {
165 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
166
chaviw8b3871a2017-11-01 17:41:01 -0700167 mPendingRemoval = true;
168
Robert Carr5edb1ad2017-04-25 10:54:24 -0700169 if (mCurrentState.zOrderRelativeOf != nullptr) {
170 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
171 if (strongRelative != nullptr) {
172 strongRelative->removeZOrderRelative(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700173 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700174 }
175 mCurrentState.zOrderRelativeOf = nullptr;
176 }
177
Chia-I Wuc6657022017-08-15 11:18:17 -0700178 for (const auto& child : mCurrentChildren) {
179 child->onRemovedFromCurrentState();
180 }
181}
Chia-I Wu38512252017-05-17 14:36:16 -0700182
Chia-I Wuc6657022017-08-15 11:18:17 -0700183void Layer::onRemoved() {
184 // the layer is removed from SF mLayersPendingRemoval
David Sodmaneb085e02017-10-05 18:49:04 -0700185 abandon();
Chia-I Wuc6657022017-08-15 11:18:17 -0700186
Steven Thomasb02664d2017-07-26 18:48:28 -0700187 destroyAllHwcLayers();
Chia-I Wu38512252017-05-17 14:36:16 -0700188
Robert Carr1f0a16a2016-10-24 16:27:39 -0700189 for (const auto& child : mCurrentChildren) {
190 child->onRemoved();
191 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700192}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700193
Mathias Agopian13127d82013-03-05 17:47:11 -0800194// ---------------------------------------------------------------------------
195// set-up
196// ---------------------------------------------------------------------------
197
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700198const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800199 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800200}
201
chaviw13fdc492017-06-27 12:40:18 -0700202bool Layer::getPremultipledAlpha() const {
203 return mPremultipliedAlpha;
204}
205
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700206sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800207 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700208 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800209}
210
211// ---------------------------------------------------------------------------
212// h/w composer set-up
213// ---------------------------------------------------------------------------
214
Dominik Laskowski7e045462018-05-30 13:02:02 -0700215bool Layer::createHwcLayer(HWComposer* hwc, int32_t displayId) {
216 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.count(displayId) != 0,
217 "Already have a layer for display %d", displayId);
218 HWC2::Layer* layer = hwc->createLayer(displayId);
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");
Dominik Laskowski7e045462018-05-30 13:02:02 -0700237 hwcInfo.hwc->destroyLayer(displayId, hwcInfo.layer);
David Sodmanf6a38932018-05-25 15:27:50 -0700238 // The layer destroyed listener should have cleared the entry from
239 // mHwcLayers. Verify that.
Dominik Laskowski7e045462018-05-30 13:02:02 -0700240 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.count(displayId) != 0,
David Sodmanf6a38932018-05-25 15:27:50 -0700241 "Stale layer entry in getBE().mHwcLayers");
Chia-I Wu83806892017-11-16 10:50:20 -0800242 return true;
Steven Thomasb02664d2017-07-26 18:48:28 -0700243}
244
245void Layer::destroyAllHwcLayers() {
David Sodman6f65f3e2017-11-03 14:28:09 -0700246 size_t numLayers = getBE().mHwcLayers.size();
Steven Thomasb02664d2017-07-26 18:48:28 -0700247 for (size_t i = 0; i < numLayers; ++i) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700248 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.empty(), "destroyAllHwcLayers failed");
249 destroyHwcLayer(getBE().mHwcLayers.begin()->first);
Steven Thomasb02664d2017-07-26 18:48:28 -0700250 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700251 LOG_ALWAYS_FATAL_IF(!getBE().mHwcLayers.empty(),
David Sodman41fdfc92017-11-06 16:09:56 -0800252 "All hardware composer layers should have been destroyed");
Steven Thomasb02664d2017-07-26 18:48:28 -0700253}
Steven Thomasb02664d2017-07-26 18:48:28 -0700254
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800255Rect Layer::getContentCrop() const {
256 // this is the crop rectangle that applies to the buffer
257 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700258 Rect crop;
259 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800260 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700261 crop = mCurrentCrop;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800262 } else if (getBE().compositionInfo.mBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800263 // otherwise we use the whole buffer
David Sodman0cc69182017-11-17 12:12:07 -0800264 crop = getBE().compositionInfo.mBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700265 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800266 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700267 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700268 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700269 return crop;
270}
271
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700272static Rect reduce(const Rect& win, const Region& exclude) {
273 if (CC_LIKELY(exclude.isEmpty())) {
274 return win;
275 }
276 if (exclude.isRect()) {
277 return win.reduce(exclude.getBounds());
278 }
279 return Region(win).subtract(exclude).getBounds();
280}
281
Dan Stoza80d61162017-12-20 15:57:52 -0800282static FloatRect reduce(const FloatRect& win, const Region& exclude) {
283 if (CC_LIKELY(exclude.isEmpty())) {
284 return win;
285 }
286 // Convert through Rect (by rounding) for lack of FloatRegion
287 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
288}
289
Robert Carr1f0a16a2016-10-24 16:27:39 -0700290Rect Layer::computeScreenBounds(bool reduceTransparentRegion) const {
291 const Layer::State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700292 Rect win(getActiveWidth(s), getActiveHeight(s));
Robert Carr1f0a16a2016-10-24 16:27:39 -0700293
Marissa Wall61c58622018-07-18 10:12:20 -0700294 Rect crop = getCrop(s);
295 if (!crop.isEmpty()) {
296 win.intersect(crop, &win);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700297 }
298
299 Transform t = getTransform();
300 win = t.transform(win);
301
Marissa Wall61c58622018-07-18 10:12:20 -0700302 Rect finalCrop = getFinalCrop(s);
303 if (!finalCrop.isEmpty()) {
304 win.intersect(finalCrop, &win);
Robert Carr41b08b52017-06-01 16:11:34 -0700305 }
306
Chia-I Wue41dbe62017-06-13 14:10:56 -0700307 const sp<Layer>& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700308 // Now we need to calculate the parent bounds, so we can clip ourselves to those.
309 // When calculating the parent bounds for purposes of clipping,
310 // we don't need to constrain the parent to its transparent region.
311 // The transparent region is an optimization based on the
312 // buffer contents of the layer, but does not affect the space allocated to
313 // it by policy, and thus children should be allowed to extend into the
314 // parent's transparent region. In fact one of the main uses, is to reduce
315 // buffer allocation size in cases where a child window sits behind a main window
316 // (by marking the hole in the parent window as a transparent region)
317 if (p != nullptr) {
318 Rect bounds = p->computeScreenBounds(false);
319 bounds.intersect(win, &win);
320 }
321
322 if (reduceTransparentRegion) {
Marissa Wall61c58622018-07-18 10:12:20 -0700323 auto const screenTransparentRegion = t.transform(getActiveTransparentRegion(s));
Robert Carr1f0a16a2016-10-24 16:27:39 -0700324 win = reduce(win, screenTransparentRegion);
325 }
326
327 return win;
328}
329
Dan Stoza80d61162017-12-20 15:57:52 -0800330FloatRect Layer::computeBounds() const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700331 const Layer::State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700332 return computeBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700333}
334
Dan Stoza80d61162017-12-20 15:57:52 -0800335FloatRect Layer::computeBounds(const Region& activeTransparentRegion) const {
Michael Lentine6c925ed2014-09-26 17:55:01 -0700336 const Layer::State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700337 Rect win(getActiveWidth(s), getActiveHeight(s));
Robert Carrb5d3d262016-03-25 15:08:13 -0700338
Marissa Wall61c58622018-07-18 10:12:20 -0700339 Rect crop = getCrop(s);
340 if (!crop.isEmpty()) {
341 win.intersect(crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800342 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700343
Chia-I Wue41dbe62017-06-13 14:10:56 -0700344 const auto& p = mDrawingParent.promote();
Robert Carrd4ae7f32018-06-07 16:10:57 -0700345 FloatRect floatWin = win.toFloatRect();
346 FloatRect parentBounds = floatWin;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700347 if (p != nullptr) {
Robert Carrd4ae7f32018-06-07 16:10:57 -0700348 // We pass an empty Region here for reasons mirroring that of the case described in
349 // the computeScreenBounds reduceTransparentRegion=false case.
350 parentBounds = p->computeBounds(Region());
Robert Carr1f0a16a2016-10-24 16:27:39 -0700351 }
352
Marissa Wallf58c14b2018-07-24 10:50:43 -0700353 Transform t = s.active_legacy.transform;
Dan Stoza80d61162017-12-20 15:57:52 -0800354
Marissa Wallf58c14b2018-07-24 10:50:43 -0700355 if (p != nullptr || !s.finalCrop_legacy.isEmpty()) {
Dan Stoza80d61162017-12-20 15:57:52 -0800356 floatWin = t.transform(floatWin);
Robert Carrd4ae7f32018-06-07 16:10:57 -0700357 floatWin = floatWin.intersect(parentBounds);
358
Marissa Wallf58c14b2018-07-24 10:50:43 -0700359 if (!s.finalCrop_legacy.isEmpty()) {
360 floatWin = floatWin.intersect(s.finalCrop_legacy.toFloatRect());
Robert Carrd4ae7f32018-06-07 16:10:57 -0700361 }
Dan Stoza80d61162017-12-20 15:57:52 -0800362 floatWin = t.inverse().transform(floatWin);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700363 }
364
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700365 // subtract the transparent region and snap to the bounds
Dan Stoza80d61162017-12-20 15:57:52 -0800366 return reduce(floatWin, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800367}
368
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700369Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& display) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700370 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800371 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700372 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800373
374 // apply the projection's clipping to the window crop in
375 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700376 // if there are no window scaling involved, this operation will map to full
377 // pixels in the buffer.
378 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
379 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700380
Marissa Wall61c58622018-07-18 10:12:20 -0700381 Rect activeCrop(getActiveWidth(s), getActiveHeight(s));
382 Rect crop = getCrop(s);
383 if (!crop.isEmpty()) {
384 activeCrop.intersect(crop, &activeCrop);
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700385 }
386
Robert Carr1f0a16a2016-10-24 16:27:39 -0700387 Transform t = getTransform();
388 activeCrop = t.transform(activeCrop);
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700389 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000390 activeCrop.clear();
391 }
Marissa Wall61c58622018-07-18 10:12:20 -0700392 Rect finalCrop = getFinalCrop(s);
393 if (!finalCrop.isEmpty()) {
394 if (!activeCrop.intersect(finalCrop, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000395 activeCrop.clear();
396 }
397 }
chaviwb1154d12017-10-31 14:15:36 -0700398
399 const auto& p = mDrawingParent.promote();
400 if (p != nullptr) {
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700401 auto parentCrop = p->computeInitialCrop(display);
chaviwb1154d12017-10-31 14:15:36 -0700402 activeCrop.intersect(parentCrop, &activeCrop);
403 }
404
Robert Carr1f0a16a2016-10-24 16:27:39 -0700405 return activeCrop;
406}
407
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700408FloatRect Layer::computeCrop(const sp<const DisplayDevice>& display) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700409 // the content crop is the area of the content that gets scaled to the
410 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800411 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700412
413 // In addition there is a WM-specified crop we pull from our drawing state.
414 const State& s(getDrawingState());
415
416 // Screen space to make reduction to parent crop clearer.
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700417 Rect activeCrop = computeInitialCrop(display);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700418 Transform t = getTransform();
419 // Back to layer space to work with the content crop.
420 activeCrop = t.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800421
Michael Lentine28ea2172014-11-19 18:32:37 -0800422 // This needs to be here as transform.transform(Rect) computes the
423 // transformed rect and then takes the bounding box of the result before
424 // returning. This means
425 // transform.inverse().transform(transform.transform(Rect)) != Rect
426 // in which case we need to make sure the final rect is clipped to the
427 // display bounds.
Marissa Wall61c58622018-07-18 10:12:20 -0700428 if (!activeCrop.intersect(Rect(getActiveWidth(s), getActiveHeight(s)), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000429 activeCrop.clear();
430 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800431
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700432 // subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700433 activeCrop = reduce(activeCrop, getActiveTransparentRegion(s));
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700434
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000435 // Transform the window crop to match the buffer coordinate system,
436 // which means using the inverse of the current transform set on the
437 // SurfaceFlingerConsumer.
438 uint32_t invTransform = mCurrentTransform;
Robert Carrcae605c2017-03-29 12:10:31 -0700439 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000440 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700441 * the code below applies the primary display's inverse transform to the
442 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000443 */
David Sodman41fdfc92017-11-06 16:09:56 -0800444 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000445 // calculate the inverse transform
446 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800447 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800448 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000449 // and apply to the current transform
David Sodman41fdfc92017-11-06 16:09:56 -0800450 invTransform = (Transform(invTransformOrient) * Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800451 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000452
Marissa Wall61c58622018-07-18 10:12:20 -0700453 int winWidth = getActiveWidth(s);
454 int winHeight = getActiveHeight(s);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000455 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
456 // If the activeCrop has been rotate the ends are rotated but not
457 // the space itself so when transforming ends back we can't rely on
458 // a modification of the axes of rotation. To account for this we
459 // need to reorient the inverse rotation in terms of the current
460 // axes of rotation.
461 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
462 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
463 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800464 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000465 }
Marissa Wall61c58622018-07-18 10:12:20 -0700466 winWidth = getActiveHeight(s);
467 winHeight = getActiveWidth(s);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000468 }
Marissa Wall61c58622018-07-18 10:12:20 -0700469 const Rect winCrop = activeCrop.transform(invTransform, getActiveWidth(s), getActiveHeight(s));
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000470
471 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800472 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000473 float yScale = crop.getHeight() / float(winHeight);
474
David Sodman41fdfc92017-11-06 16:09:56 -0800475 float insetL = winCrop.left * xScale;
476 float insetT = winCrop.top * yScale;
477 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000478 float insetB = (winHeight - winCrop.bottom) * yScale;
479
David Sodman41fdfc92017-11-06 16:09:56 -0800480 crop.left += insetL;
481 crop.top += insetT;
482 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000483 crop.bottom -= insetB;
484
Mathias Agopian13127d82013-03-05 17:47:11 -0800485 return crop;
486}
487
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700488void Layer::setGeometry(const sp<const DisplayDevice>& display, uint32_t z) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700489 const auto displayId = display->getId();
Peiyong Lin91b1df22018-06-18 18:00:16 -0700490 if (!hasHwcLayer(displayId)) {
491 ALOGE("[%s] failed to setGeometry: no HWC layer found (%d)",
492 mName.string(), displayId);
493 return;
494 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700495 auto& hwcInfo = getBE().mHwcLayers[displayId];
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700496
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700497 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800498 hwcInfo.forceClientComposition = false;
499
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700500 if (isSecure() && !display->isSecure()) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800501 hwcInfo.forceClientComposition = true;
502 }
503
Lloyd Pique074e8122018-07-26 12:57:23 -0700504 auto& hwcLayer = hwcInfo.layer;
505
Mathias Agopian13127d82013-03-05 17:47:11 -0800506 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700507 const State& s(getDrawingState());
David Revemanecf0fa52017-03-03 11:32:44 -0500508 auto blendMode = HWC2::BlendMode::None;
Robert Carr6452f122017-03-21 10:41:29 -0700509 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800510 blendMode =
511 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800512 }
Lloyd Pique074e8122018-07-26 12:57:23 -0700513 auto error = hwcLayer->setBlendMode(blendMode);
514 ALOGE_IF(error != HWC2::Error::None,
515 "[%s] Failed to set blend mode %s:"
516 " %s (%d)",
517 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
518 static_cast<int32_t>(error));
Mathias Agopian13127d82013-03-05 17:47:11 -0800519
520 // apply the layer's transform, followed by the display's global transform
521 // here we're guaranteed that the layer's transform preserves rects
Marissa Wall61c58622018-07-18 10:12:20 -0700522 Region activeTransparentRegion(getActiveTransparentRegion(s));
Robert Carr1f0a16a2016-10-24 16:27:39 -0700523 Transform t = getTransform();
Marissa Wall61c58622018-07-18 10:12:20 -0700524 Rect activeCrop = getCrop(s);
525 if (!activeCrop.isEmpty()) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700526 activeCrop = t.transform(activeCrop);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700527 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000528 activeCrop.clear();
529 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700530 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800531 // This needs to be here as transform.transform(Rect) computes the
532 // transformed rect and then takes the bounding box of the result before
533 // returning. This means
534 // transform.inverse().transform(transform.transform(Rect)) != Rect
535 // in which case we need to make sure the final rect is clipped to the
536 // display bounds.
Marissa Wall61c58622018-07-18 10:12:20 -0700537 if (!activeCrop.intersect(Rect(getActiveWidth(s), getActiveHeight(s)), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000538 activeCrop.clear();
539 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700540 // mark regions outside the crop as transparent
Marissa Wall61c58622018-07-18 10:12:20 -0700541 activeTransparentRegion.orSelf(Rect(0, 0, getActiveWidth(s), activeCrop.top));
Marissa Wallf58c14b2018-07-24 10:50:43 -0700542 activeTransparentRegion.orSelf(
Marissa Wall61c58622018-07-18 10:12:20 -0700543 Rect(0, activeCrop.bottom, getActiveWidth(s), getActiveHeight(s)));
David Sodman41fdfc92017-11-06 16:09:56 -0800544 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
545 activeTransparentRegion.orSelf(
Marissa Wall61c58622018-07-18 10:12:20 -0700546 Rect(activeCrop.right, activeCrop.top, getActiveWidth(s), activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700547 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700548
Dan Stoza80d61162017-12-20 15:57:52 -0800549 // computeBounds returns a FloatRect to provide more accuracy during the
550 // transformation. We then round upon constructing 'frame'.
551 Rect frame{t.transform(computeBounds(activeTransparentRegion))};
Marissa Wall61c58622018-07-18 10:12:20 -0700552 Rect finalCrop = getFinalCrop(s);
553 if (!finalCrop.isEmpty()) {
554 if (!frame.intersect(finalCrop, &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000555 frame.clear();
556 }
557 }
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700558 if (!frame.intersect(display->getViewport(), &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000559 frame.clear();
560 }
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700561 const Transform& tr = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800562 Rect transformedFrame = tr.transform(frame);
Lloyd Pique074e8122018-07-26 12:57:23 -0700563 error = hwcLayer->setDisplayFrame(transformedFrame);
564 if (error != HWC2::Error::None) {
565 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
566 transformedFrame.left, transformedFrame.top, transformedFrame.right,
567 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
568 } else {
569 hwcInfo.displayFrame = transformedFrame;
570 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800571
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700572 FloatRect sourceCrop = computeCrop(display);
Lloyd Pique074e8122018-07-26 12:57:23 -0700573 error = hwcLayer->setSourceCrop(sourceCrop);
574 if (error != HWC2::Error::None) {
575 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
576 "%s (%d)",
577 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
578 to_string(error).c_str(), static_cast<int32_t>(error));
579 } else {
580 hwcInfo.sourceCrop = sourceCrop;
581 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800582
chaviw13fdc492017-06-27 12:40:18 -0700583 float alpha = static_cast<float>(getAlpha());
Lloyd Pique074e8122018-07-26 12:57:23 -0700584 error = hwcLayer->setPlaneAlpha(alpha);
585 ALOGE_IF(error != HWC2::Error::None,
586 "[%s] Failed to set plane alpha %.3f: "
587 "%s (%d)",
588 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800589
Lloyd Pique074e8122018-07-26 12:57:23 -0700590 error = hwcLayer->setZOrder(z);
591 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
592 to_string(error).c_str(), static_cast<int32_t>(error));
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500593
Albert Chaulk2a589632017-05-04 16:59:44 -0400594 int type = s.type;
595 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700596 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400597 if (parent.get()) {
598 auto& parentState = parent->getDrawingState();
rongliucfb187b2018-03-14 12:26:23 -0700599 if (parentState.type >= 0 || parentState.appId >= 0) {
600 type = parentState.type;
601 appId = parentState.appId;
602 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400603 }
604
Lloyd Pique074e8122018-07-26 12:57:23 -0700605 error = hwcLayer->setInfo(type, appId);
606 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
607 static_cast<int32_t>(error));
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800608
Mathias Agopian29a367b2011-07-12 14:51:45 -0700609 /*
610 * Transformations are applied in this order:
611 * 1) buffer orientation/flip/mirror
612 * 2) state transformation (window manager)
613 * 3) layer orientation (screen orientation)
614 * (NOTE: the matrices are multiplied in reverse order)
615 */
616
617 const Transform bufferOrientation(mCurrentTransform);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700618 Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700619
Robert Carrcae605c2017-03-29 12:10:31 -0700620 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700621 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700622 * the code below applies the primary display's inverse transform to the
623 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700624 */
David Sodman41fdfc92017-11-06 16:09:56 -0800625 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700626 // calculate the inverse transform
627 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800628 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700629 }
Robert Carrcae605c2017-03-29 12:10:31 -0700630
631 /*
632 * Here we cancel out the orientation component of the WM transform.
633 * The scaling and translate components are already included in our bounds
634 * computation so it's enough to just omit it in the composition.
635 * See comment in onDraw with ref to b/36727915 for why.
636 */
637 transform = Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700638 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700639
640 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800641 const uint32_t orientation = transform.getOrientation();
Jorim Jaggif3bd94a2018-03-27 15:38:03 +0200642 if (orientation & Transform::ROT_INVALID) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800643 // we can only handle simple transformation
Lloyd Pique074e8122018-07-26 12:57:23 -0700644 hwcInfo.forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800645 } else {
646 auto transform = static_cast<HWC2::Transform>(orientation);
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800647 hwcInfo.transform = transform;
Lloyd Pique074e8122018-07-26 12:57:23 -0700648 auto error = hwcLayer->setTransform(transform);
649 ALOGE_IF(error != HWC2::Error::None,
650 "[%s] Failed to set transform %s: "
651 "%s (%d)",
652 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
653 static_cast<int32_t>(error));
David Sodman4b7c4bc2017-11-17 12:13:59 -0800654 }
655}
656
Dominik Laskowski7e045462018-05-30 13:02:02 -0700657void Layer::forceClientComposition(int32_t displayId) {
658 if (getBE().mHwcLayers.count(displayId) == 0) {
659 ALOGE("forceClientComposition: no HWC layer found (%d)", displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800660 return;
661 }
662
Dominik Laskowski7e045462018-05-30 13:02:02 -0700663 getBE().mHwcLayers[displayId].forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800664}
Dan Stozaee44edd2015-03-23 15:50:23 -0700665
Dominik Laskowski7e045462018-05-30 13:02:02 -0700666bool Layer::getForceClientComposition(int32_t displayId) {
667 if (getBE().mHwcLayers.count(displayId) == 0) {
668 ALOGE("getForceClientComposition: no HWC layer found (%d)", displayId);
chaviwc9232ed2017-11-14 15:31:15 -0800669 return false;
670 }
671
Dominik Laskowski7e045462018-05-30 13:02:02 -0700672 return getBE().mHwcLayers[displayId].forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800673}
674
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700675void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700676 const auto displayId = display->getId();
677 if (getBE().mHwcLayers.count(displayId) == 0 ||
678 getCompositionType(displayId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800679 return;
680 }
681
682 // This gives us only the "orientation" component of the transform
683 const State& s(getCurrentState());
684
685 // Apply the layer's transform, followed by the display's global transform
686 // Here we're guaranteed that the layer's transform preserves rects
Marissa Wall61c58622018-07-18 10:12:20 -0700687 Rect win(getActiveWidth(s), getActiveHeight(s));
688 Rect crop = getCrop(s);
689 if (!crop.isEmpty()) {
690 win.intersect(crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800691 }
692 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700693 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Robert Carr1f0a16a2016-10-24 16:27:39 -0700694 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700695 frame.intersect(display->getViewport(), &frame);
Marissa Wall61c58622018-07-18 10:12:20 -0700696 Rect finalCrop = getFinalCrop(s);
697 if (!finalCrop.isEmpty()) {
698 frame.intersect(finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000699 }
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700700 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800701 auto position = displayTransform.transform(frame);
702
Dominik Laskowski7e045462018-05-30 13:02:02 -0700703 auto error =
704 getBE().mHwcLayers[displayId].layer->setCursorPosition(position.left, position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800705 ALOGE_IF(error != HWC2::Error::None,
706 "[%s] Failed to set cursor position "
707 "to (%d, %d): %s (%d)",
708 mName.string(), position.left, position.top, to_string(error).c_str(),
709 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800710}
Riley Andrews03414a12014-07-01 14:22:59 -0700711
Mathias Agopian13127d82013-03-05 17:47:11 -0800712// ---------------------------------------------------------------------------
713// drawing...
714// ---------------------------------------------------------------------------
715
Marissa Wall61c58622018-07-18 10:12:20 -0700716void Layer::draw(const RenderArea& renderArea, const Region& clip) {
chaviwa76b2712017-09-20 12:02:26 -0700717 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800718}
719
Marissa Wall61c58622018-07-18 10:12:20 -0700720void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) {
chaviwa76b2712017-09-20 12:02:26 -0700721 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800722}
723
Marissa Wall61c58622018-07-18 10:12:20 -0700724void Layer::draw(const RenderArea& renderArea) {
chaviwa76b2712017-09-20 12:02:26 -0700725 onDraw(renderArea, Region(renderArea.getBounds()), false);
Dan Stozac7014012014-02-14 15:03:43 -0800726}
727
David Sodman41fdfc92017-11-06 16:09:56 -0800728void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
729 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800730 auto& engine(mFlinger->getRenderEngine());
David Sodman9eeae692017-11-02 10:53:32 -0700731 computeGeometry(renderArea, getBE().mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700732 engine.setupFillWithColor(red, green, blue, alpha);
David Sodman9eeae692017-11-02 10:53:32 -0700733 engine.drawMesh(getBE().mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800734}
735
chaviwa76b2712017-09-20 12:02:26 -0700736void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
David Sodman41fdfc92017-11-06 16:09:56 -0800737 clearWithOpenGL(renderArea, 0, 0, 0, 0);
Mathias Agopian13127d82013-03-05 17:47:11 -0800738}
739
Dominik Laskowski7e045462018-05-30 13:02:02 -0700740void Layer::setCompositionType(int32_t displayId, HWC2::Composition type, bool callIntoHwc) {
741 if (getBE().mHwcLayers.count(displayId) == 0) {
Chia-I Wu30505fb2018-03-26 16:20:31 -0700742 ALOGE("setCompositionType called without a valid HWC layer");
743 return;
744 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700745 auto& hwcInfo = getBE().mHwcLayers[displayId];
Chia-I Wu30505fb2018-03-26 16:20:31 -0700746 auto& hwcLayer = hwcInfo.layer;
David Sodmanf6a38932018-05-25 15:27:50 -0700747 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(), to_string(type).c_str(),
Chia-I Wu30505fb2018-03-26 16:20:31 -0700748 static_cast<int>(callIntoHwc));
749 if (hwcInfo.compositionType != type) {
750 ALOGV(" actually setting");
751 hwcInfo.compositionType = type;
752 if (callIntoHwc) {
David Sodmanf6a38932018-05-25 15:27:50 -0700753 auto error = hwcLayer->setCompositionType(type);
Chia-I Wu30505fb2018-03-26 16:20:31 -0700754 ALOGE_IF(error != HWC2::Error::None,
755 "[%s] Failed to set "
756 "composition type %s: %s (%d)",
757 mName.string(), to_string(type).c_str(), to_string(error).c_str(),
758 static_cast<int32_t>(error));
759 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800760 }
761}
762
Dominik Laskowski7e045462018-05-30 13:02:02 -0700763HWC2::Composition Layer::getCompositionType(int32_t displayId) const {
764 if (displayId == DisplayDevice::DISPLAY_ID_INVALID) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700765 // If we're querying the composition type for a display that does not
766 // have a HWC counterpart, then it will always be Client
767 return HWC2::Composition::Client;
768 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700769 if (getBE().mHwcLayers.count(displayId) == 0) {
Chia-I Wu30505fb2018-03-26 16:20:31 -0700770 ALOGE("getCompositionType called with an invalid HWC layer");
771 return HWC2::Composition::Invalid;
772 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700773 return getBE().mHwcLayers.at(displayId).compositionType;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800774}
775
Dominik Laskowski7e045462018-05-30 13:02:02 -0700776void Layer::setClearClientTarget(int32_t displayId, bool clear) {
777 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800778 ALOGE("setClearClientTarget called without a valid HWC layer");
779 return;
780 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700781 getBE().mHwcLayers[displayId].clearClientTarget = clear;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800782}
783
Dominik Laskowski7e045462018-05-30 13:02:02 -0700784bool Layer::getClearClientTarget(int32_t displayId) const {
785 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800786 ALOGE("getClearClientTarget called without a valid HWC layer");
787 return false;
788 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700789 return getBE().mHwcLayers.at(displayId).clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800790}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800791
Dan Stozacac35382016-01-27 12:21:06 -0800792bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
793 if (point->getFrameNumber() <= mCurrentFrameNumber) {
794 // Don't bother with a SyncPoint, since we've already latched the
795 // relevant frame
796 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700797 }
798
Dan Stozacac35382016-01-27 12:21:06 -0800799 Mutex::Autolock lock(mLocalSyncPointMutex);
800 mLocalSyncPoints.push_back(point);
801 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700802}
803
Mathias Agopian13127d82013-03-05 17:47:11 -0800804void Layer::setFiltering(bool filtering) {
805 mFiltering = filtering;
806}
807
808bool Layer::getFiltering() const {
809 return mFiltering;
810}
811
Mathias Agopian13127d82013-03-05 17:47:11 -0800812// ----------------------------------------------------------------------------
813// local state
814// ----------------------------------------------------------------------------
815
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000816static void boundPoint(vec2* point, const Rect& crop) {
817 if (point->x < crop.left) {
818 point->x = crop.left;
819 }
820 if (point->x > crop.right) {
821 point->x = crop.right;
822 }
823 if (point->y < crop.top) {
824 point->y = crop.top;
825 }
826 if (point->y > crop.bottom) {
827 point->y = crop.bottom;
828 }
829}
830
chaviwa76b2712017-09-20 12:02:26 -0700831void Layer::computeGeometry(const RenderArea& renderArea, Mesh& mesh,
832 bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700833 const Layer::State& s(getDrawingState());
chaviwa76b2712017-09-20 12:02:26 -0700834 const Transform renderAreaTransform(renderArea.getTransform());
835 const uint32_t height = renderArea.getHeight();
Dan Stoza80d61162017-12-20 15:57:52 -0800836 FloatRect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700837
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000838 vec2 lt = vec2(win.left, win.top);
839 vec2 lb = vec2(win.left, win.bottom);
840 vec2 rb = vec2(win.right, win.bottom);
841 vec2 rt = vec2(win.right, win.top);
842
Robert Carr1f0a16a2016-10-24 16:27:39 -0700843 Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000844 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700845 lt = layerTransform.transform(lt);
846 lb = layerTransform.transform(lb);
847 rb = layerTransform.transform(rb);
848 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000849 }
850
Marissa Wall61c58622018-07-18 10:12:20 -0700851 Rect finalCrop = getFinalCrop(s);
852 if (!finalCrop.isEmpty()) {
853 boundPoint(&lt, finalCrop);
854 boundPoint(&lb, finalCrop);
855 boundPoint(&rb, finalCrop);
856 boundPoint(&rt, finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000857 }
858
Mathias Agopianff2ed702013-09-01 21:36:12 -0700859 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700860 position[0] = renderAreaTransform.transform(lt);
861 position[1] = renderAreaTransform.transform(lb);
862 position[2] = renderAreaTransform.transform(rb);
863 position[3] = renderAreaTransform.transform(rt);
David Sodman41fdfc92017-11-06 16:09:56 -0800864 for (size_t i = 0; i < 4; i++) {
chaviwa76b2712017-09-20 12:02:26 -0700865 position[i].y = height - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -0800866 }
867}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800868
David Sodman41fdfc92017-11-06 16:09:56 -0800869bool Layer::isSecure() const {
Dan Stoza23116082015-06-18 14:58:39 -0700870 const Layer::State& s(mDrawingState);
871 return (s.flags & layer_state_t::eLayerSecure);
872}
873
Mathias Agopian13127d82013-03-05 17:47:11 -0800874void Layer::setVisibleRegion(const Region& visibleRegion) {
875 // always called from main thread
876 this->visibleRegion = visibleRegion;
877}
878
879void Layer::setCoveredRegion(const Region& coveredRegion) {
880 // always called from main thread
881 this->coveredRegion = coveredRegion;
882}
883
David Sodman41fdfc92017-11-06 16:09:56 -0800884void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800885 // always called from main thread
886 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
887}
888
Robert Carre5f4f692018-01-12 13:12:28 -0800889void Layer::clearVisibilityRegions() {
890 visibleRegion.clear();
891 visibleNonTransparentRegion.clear();
892 coveredRegion.clear();
893}
894
Mathias Agopian13127d82013-03-05 17:47:11 -0800895// ----------------------------------------------------------------------------
896// transaction
897// ----------------------------------------------------------------------------
898
Dan Stoza7dde5992015-05-22 09:51:44 -0700899void Layer::pushPendingState() {
900 if (!mCurrentState.modified) {
901 return;
902 }
903
Dan Stoza7dde5992015-05-22 09:51:44 -0700904 // If this transaction is waiting on the receipt of a frame, generate a sync
905 // point and send it to the remote layer.
Marissa Wallf58c14b2018-07-24 10:50:43 -0700906 if (mCurrentState.barrierLayer_legacy != nullptr) {
907 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800908 if (barrierLayer == nullptr) {
909 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700910 // If we can't promote the layer we are intended to wait on,
911 // then it is expired or otherwise invalid. Allow this transaction
912 // to be applied as per normal (no synchronization).
Marissa Wallf58c14b2018-07-24 10:50:43 -0700913 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800914 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700915 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy);
Robert Carr0d480722017-01-10 16:42:54 -0800916 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800917 mRemoteSyncPoints.push_back(std::move(syncPoint));
918 } else {
919 // We already missed the frame we're supposed to synchronize
920 // on, so go ahead and apply the state update
Marissa Wallf58c14b2018-07-24 10:50:43 -0700921 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800922 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700923 }
924
Dan Stoza7dde5992015-05-22 09:51:44 -0700925 // Wake us up to check if the frame has been received
926 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700927 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700928 }
929 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700930 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700931}
932
Pablo Ceballos05289c22016-04-14 15:49:55 -0700933void Layer::popPendingState(State* stateToCommit) {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700934 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700935
936 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700937 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700938}
939
Pablo Ceballos05289c22016-04-14 15:49:55 -0700940bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700941 bool stateUpdateAvailable = false;
942 while (!mPendingStates.empty()) {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700943 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700944 if (mRemoteSyncPoints.empty()) {
945 // If we don't have a sync point for this, apply it anyway. It
946 // will be visually wrong, but it should keep us from getting
947 // into too much trouble.
948 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700949 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700950 stateUpdateAvailable = true;
951 continue;
952 }
953
Marissa Wallf58c14b2018-07-24 10:50:43 -0700954 if (mRemoteSyncPoints.front()->getFrameNumber() !=
955 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800956 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800957
958 // Signal our end of the sync point and then dispose of it
959 mRemoteSyncPoints.front()->setTransactionApplied();
960 mRemoteSyncPoints.pop_front();
961 continue;
962 }
963
Dan Stoza7dde5992015-05-22 09:51:44 -0700964 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
965 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700966 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700967 stateUpdateAvailable = true;
968
969 // Signal our end of the sync point and then dispose of it
970 mRemoteSyncPoints.front()->setTransactionApplied();
971 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800972 } else {
973 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700974 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700975 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700976 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700977 stateUpdateAvailable = true;
978 }
979 }
980
981 // If we still have pending updates, wake SurfaceFlinger back up and point
982 // it at this layer so we can process them
983 if (!mPendingStates.empty()) {
984 setTransactionFlags(eTransactionNeeded);
985 mFlinger->setTransactionFlags(eTraversalNeeded);
986 }
987
988 mCurrentState.modified = false;
989 return stateUpdateAvailable;
990}
991
Marissa Wall61c58622018-07-18 10:12:20 -0700992uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700993 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800994
Marissa Wall61c58622018-07-18 10:12:20 -0700995 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
996 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700997
David Sodmaneb085e02017-10-05 18:49:04 -0700998 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700999 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +00001000 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -08001001 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
1002 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1003 " requested={ wh={%4u,%4u} }}\n"
1004 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1005 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -07001006 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -07001007 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
1008 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
1009 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
1010 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
1011 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -07001012 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
1013 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
1014 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001015
Jamie Gennis2a0d5b62011-09-26 16:54:44 -07001016 // record the new size, form this point on, when the client request
1017 // a buffer, it'll get the new size.
Marissa Wall61c58622018-07-18 10:12:20 -07001018 setDefaultBufferSize(stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001019 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001020
Robert Carre392b552017-09-19 12:16:05 -07001021 // Don't let Layer::doTransaction update the drawing state
1022 // if we have a pending resize, unless we are in fixed-size mode.
1023 // the drawing state will be updated only once we receive a buffer
1024 // with the correct size.
1025 //
1026 // In particular, we want to make sure the clip (which is part
1027 // of the geometry state) is latched together with the size but is
1028 // latched immediately when no resizing is involved.
1029 //
1030 // If a sideband stream is attached, however, we want to skip this
1031 // optimization so that transactions aren't missed when a buffer
1032 // never arrives
1033 //
1034 // In the case that we don't have a buffer we ignore other factors
1035 // and avoid entering the resizePending state. At a high level the
1036 // resizePending state is to avoid applying the state of the new buffer
1037 // to the old buffer. However in the state where we don't have an old buffer
1038 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -07001039 const bool resizePending =
1040 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
1041 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
David Sodman0cc69182017-11-17 12:12:07 -08001042 (getBE().compositionInfo.mBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001043 if (!isFixedSize()) {
David Sodman0cc69182017-11-17 12:12:07 -08001044 if (resizePending && getBE().compositionInfo.hwc.sidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001045 flags |= eDontUpdateGeometryState;
1046 }
1047 }
1048
Robert Carr7bf247e2017-05-18 14:02:49 -07001049 // Here we apply various requested geometry states, depending on our
1050 // latching configuration. See Layer.h for a detailed discussion of
1051 // how geometry latching is controlled.
1052 if (!(flags & eDontUpdateGeometryState)) {
Pablo Ceballos7d052572016-06-02 17:46:05 -07001053 Layer::State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001054
1055 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1056 // mode, which causes attributes which normally latch regardless of scaling mode,
1057 // to be delayed. We copy the requested state to the active state making sure
1058 // to respect these rules (again see Layer.h for a detailed discussion).
1059 //
1060 // There is an awkward asymmetry in the handling of the crop states in the position
1061 // states, as can be seen below. Largely this arises from position and transform
1062 // being stored in the same data structure while having different latching rules.
1063 // b/38182305
1064 //
Marissa Wall61c58622018-07-18 10:12:20 -07001065 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -07001066 // applyPendingStates in the presence of deferred transactions.
1067 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -07001068 float tx = stateToCommit->active_legacy.transform.tx();
1069 float ty = stateToCommit->active_legacy.transform.ty();
1070 stateToCommit->active_legacy = stateToCommit->requested_legacy;
1071 stateToCommit->active_legacy.transform.set(tx, ty);
1072 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001073 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001074 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -07001075 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001076 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001077 }
1078
Marissa Wall61c58622018-07-18 10:12:20 -07001079 return flags;
1080}
1081
1082uint32_t Layer::doTransaction(uint32_t flags) {
1083 ATRACE_CALL();
1084
1085 pushPendingState();
1086 Layer::State c = getCurrentState();
1087 if (!applyPendingStates(&c)) {
1088 return 0;
1089 }
1090
1091 flags = doTransactionResize(flags, &c);
1092
1093 const Layer::State& s(getDrawingState());
1094
1095 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001096 // invalidate and recompute the visible regions if needed
1097 flags |= Layer::eVisibleRegion;
1098 }
1099
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001100 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001101 // invalidate and recompute the visible regions if needed
1102 flags |= eVisibleRegion;
1103 this->contentDirty = true;
1104
1105 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -07001106 const uint8_t type = getActiveTransform(c).getType();
1107 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || (type >= Transform::SCALE));
Mathias Agopian13127d82013-03-05 17:47:11 -08001108 }
1109
Dan Stozac8145172016-04-28 16:29:06 -07001110 // If the layer is hidden, signal and clear out all local sync points so
1111 // that transactions for layers depending on this layer's frames becoming
1112 // visible are not blocked
1113 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001114 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001115 }
1116
Mathias Agopian13127d82013-03-05 17:47:11 -08001117 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001118 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001119 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001120}
1121
Pablo Ceballos05289c22016-04-14 15:49:55 -07001122void Layer::commitTransaction(const State& stateToCommit) {
1123 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001124}
1125
Mathias Agopian13127d82013-03-05 17:47:11 -08001126uint32_t Layer::getTransactionFlags(uint32_t flags) {
1127 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1128}
1129
1130uint32_t Layer::setTransactionFlags(uint32_t flags) {
1131 return android_atomic_or(flags, &mTransactionFlags);
1132}
1133
Robert Carr82364e32016-05-15 11:27:47 -07001134bool Layer::setPosition(float x, float y, bool immediate) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001135 if (mCurrentState.requested_legacy.transform.tx() == x &&
1136 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001137 return false;
1138 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001139
1140 // We update the requested and active position simultaneously because
1141 // we want to apply the position portion of the transform matrix immediately,
1142 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Marissa Wallf58c14b2018-07-24 10:50:43 -07001143 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001144 if (immediate && !mFreezeGeometryUpdates) {
1145 // Here we directly update the active state
1146 // unlike other setters, because we store it within
1147 // the transform, but use different latching rules.
1148 // b/38182305
Marissa Wallf58c14b2018-07-24 10:50:43 -07001149 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001150 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001151 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001152
Dan Stoza7dde5992015-05-22 09:51:44 -07001153 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001154 setTransactionFlags(eTransactionNeeded);
1155 return true;
1156}
Robert Carr82364e32016-05-15 11:27:47 -07001157
Robert Carr1f0a16a2016-10-24 16:27:39 -07001158bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1159 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1160 if (idx < 0) {
1161 return false;
1162 }
1163 if (childLayer->setLayer(z)) {
1164 mCurrentChildren.removeAt(idx);
1165 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001166 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001167 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001168 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001169}
1170
Robert Carr503c7042017-09-27 15:06:08 -07001171bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1172 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1173 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1174 if (idx < 0) {
1175 return false;
1176 }
1177 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1178 mCurrentChildren.removeAt(idx);
1179 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001180 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001181 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001182 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001183}
1184
Robert Carrae060832016-11-28 10:51:00 -08001185bool Layer::setLayer(int32_t z) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001186 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001187 mCurrentState.sequence++;
1188 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001189 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001190
1191 // Discard all relative layering.
1192 if (mCurrentState.zOrderRelativeOf != nullptr) {
1193 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1194 if (strongRelative != nullptr) {
1195 strongRelative->removeZOrderRelative(this);
1196 }
1197 mCurrentState.zOrderRelativeOf = nullptr;
1198 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001199 setTransactionFlags(eTransactionNeeded);
1200 return true;
1201}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001202
Robert Carrdb66e622017-04-10 16:55:57 -07001203void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1204 mCurrentState.zOrderRelatives.remove(relative);
1205 mCurrentState.sequence++;
1206 mCurrentState.modified = true;
1207 setTransactionFlags(eTransactionNeeded);
1208}
1209
1210void Layer::addZOrderRelative(const wp<Layer>& relative) {
1211 mCurrentState.zOrderRelatives.add(relative);
1212 mCurrentState.modified = true;
1213 mCurrentState.sequence++;
1214 setTransactionFlags(eTransactionNeeded);
1215}
1216
Robert Carr503d2bd2017-12-04 15:49:47 -08001217bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001218 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1219 if (handle == nullptr) {
1220 return false;
1221 }
1222 sp<Layer> relative = handle->owner.promote();
1223 if (relative == nullptr) {
1224 return false;
1225 }
1226
Robert Carr503d2bd2017-12-04 15:49:47 -08001227 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1228 mCurrentState.zOrderRelativeOf == relative) {
1229 return false;
1230 }
1231
Robert Carrdb66e622017-04-10 16:55:57 -07001232 mCurrentState.sequence++;
1233 mCurrentState.modified = true;
Robert Carr503d2bd2017-12-04 15:49:47 -08001234 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001235
chaviw9ab4bd12017-11-03 13:11:00 -07001236 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
1237 if (oldZOrderRelativeOf != nullptr) {
1238 oldZOrderRelativeOf->removeZOrderRelative(this);
1239 }
Robert Carrdb66e622017-04-10 16:55:57 -07001240 mCurrentState.zOrderRelativeOf = relative;
1241 relative->addZOrderRelative(this);
1242
1243 setTransactionFlags(eTransactionNeeded);
1244
1245 return true;
1246}
1247
Mathias Agopian13127d82013-03-05 17:47:11 -08001248bool Layer::setSize(uint32_t w, uint32_t h) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001249 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
1250 return false;
1251 mCurrentState.requested_legacy.w = w;
1252 mCurrentState.requested_legacy.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001253 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001254 setTransactionFlags(eTransactionNeeded);
1255 return true;
1256}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001257bool Layer::setAlpha(float alpha) {
David Sodman41fdfc92017-11-06 16:09:56 -08001258 if (mCurrentState.color.a == alpha) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001259 mCurrentState.sequence++;
chaviw13fdc492017-06-27 12:40:18 -07001260 mCurrentState.color.a = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001261 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001262 setTransactionFlags(eTransactionNeeded);
1263 return true;
1264}
chaviw13fdc492017-06-27 12:40:18 -07001265
1266bool Layer::setColor(const half3& color) {
David Sodman41fdfc92017-11-06 16:09:56 -08001267 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g &&
1268 color.b == mCurrentState.color.b)
chaviw13fdc492017-06-27 12:40:18 -07001269 return false;
1270
1271 mCurrentState.sequence++;
1272 mCurrentState.color.r = color.r;
1273 mCurrentState.color.g = color.g;
1274 mCurrentState.color.b = color.b;
1275 mCurrentState.modified = true;
1276 setTransactionFlags(eTransactionNeeded);
1277 return true;
1278}
1279
Robert Carrd4ae7f32018-06-07 16:10:57 -07001280bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1281 bool allowNonRectPreservingTransforms) {
1282 Transform t;
1283 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1284
1285 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1286 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1287 return false;
1288 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001289 mCurrentState.sequence++;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001290 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1291 matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001292 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001293 setTransactionFlags(eTransactionNeeded);
1294 return true;
1295}
Marissa Wall61c58622018-07-18 10:12:20 -07001296
Mathias Agopian13127d82013-03-05 17:47:11 -08001297bool Layer::setTransparentRegionHint(const Region& transparent) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001298 mCurrentState.requestedTransparentRegion_legacy = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001299 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001300 setTransactionFlags(eTransactionNeeded);
1301 return true;
1302}
1303bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1304 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
David Sodman41fdfc92017-11-06 16:09:56 -08001305 if (mCurrentState.flags == newFlags) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001306 mCurrentState.sequence++;
1307 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001308 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001309 setTransactionFlags(eTransactionNeeded);
1310 return true;
1311}
Robert Carr99e27f02016-06-16 15:18:02 -07001312
Marissa Wallf58c14b2018-07-24 10:50:43 -07001313bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
1314 if (mCurrentState.requestedCrop_legacy == crop) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001315 mCurrentState.sequence++;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001316 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001317 if (immediate && !mFreezeGeometryUpdates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001318 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001319 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001320 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1321
Dan Stoza7dde5992015-05-22 09:51:44 -07001322 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001323 setTransactionFlags(eTransactionNeeded);
1324 return true;
1325}
Robert Carr8d5227b2017-03-16 15:41:03 -07001326
Marissa Wallf58c14b2018-07-24 10:50:43 -07001327bool Layer::setFinalCrop_legacy(const Rect& crop, bool immediate) {
1328 if (mCurrentState.requestedFinalCrop_legacy == crop) return false;
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001329 mCurrentState.sequence++;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001330 mCurrentState.requestedFinalCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001331 if (immediate && !mFreezeGeometryUpdates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001332 mCurrentState.finalCrop_legacy = crop;
Robert Carr8d5227b2017-03-16 15:41:03 -07001333 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001334 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1335
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001336 mCurrentState.modified = true;
1337 setTransactionFlags(eTransactionNeeded);
1338 return true;
1339}
Mathias Agopian13127d82013-03-05 17:47:11 -08001340
Robert Carrc3574f72016-03-24 12:19:32 -07001341bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001342 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001343 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001344 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001345 return true;
1346}
1347
rongliucfb187b2018-03-14 12:26:23 -07001348void Layer::setInfo(int32_t type, int32_t appId) {
David Sodman41fdfc92017-11-06 16:09:56 -08001349 mCurrentState.appId = appId;
1350 mCurrentState.type = type;
1351 mCurrentState.modified = true;
1352 setTransactionFlags(eTransactionNeeded);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001353}
1354
Mathias Agopian13127d82013-03-05 17:47:11 -08001355bool Layer::setLayerStack(uint32_t layerStack) {
David Sodman41fdfc92017-11-06 16:09:56 -08001356 if (mCurrentState.layerStack == layerStack) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001357 mCurrentState.sequence++;
1358 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001359 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001360 setTransactionFlags(eTransactionNeeded);
1361 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001362}
1363
Robert Carr1f0a16a2016-10-24 16:27:39 -07001364uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001365 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001366 if (p == nullptr) {
1367 return getDrawingState().layerStack;
1368 }
1369 return p->getLayerStack();
1370}
1371
Marissa Wallf58c14b2018-07-24 10:50:43 -07001372void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
1373 mCurrentState.barrierLayer_legacy = barrierLayer;
1374 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001375 // We don't set eTransactionNeeded, because just receiving a deferral
1376 // request without any other state updates shouldn't actually induce a delay
1377 mCurrentState.modified = true;
1378 pushPendingState();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001379 mCurrentState.barrierLayer_legacy = nullptr;
1380 mCurrentState.frameNumber_legacy = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001381 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001382}
1383
Marissa Wallf58c14b2018-07-24 10:50:43 -07001384void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001385 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001386 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001387}
1388
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001389// ----------------------------------------------------------------------------
1390// pageflip handling...
1391// ----------------------------------------------------------------------------
1392
Robert Carr1f0a16a2016-10-24 16:27:39 -07001393bool Layer::isHiddenByPolicy() const {
1394 const Layer::State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001395 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001396 if (parent != nullptr && parent->isHiddenByPolicy()) {
1397 return true;
1398 }
1399 return s.flags & layer_state_t::eLayerHidden;
1400}
1401
David Sodman41fdfc92017-11-06 16:09:56 -08001402uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001403 // TODO: should we do something special if mSecure is set?
1404 if (mProtectedByApp) {
1405 // need a hardware-protected path to external video sink
1406 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001407 }
Riley Andrews03414a12014-07-01 14:22:59 -07001408 if (mPotentialCursor) {
1409 usage |= GraphicBuffer::USAGE_CURSOR;
1410 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001411 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001412 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001413}
1414
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001415void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001416 uint32_t orientation = 0;
1417 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001418 // The transform hint is used to improve performance, but we can
1419 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001420 // apply to all displays.
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001421 const Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001422 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07001423 if (orientation & Transform::ROT_INVALID) {
1424 orientation = 0;
1425 }
1426 }
David Sodmaneb085e02017-10-05 18:49:04 -07001427 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001428}
1429
Mathias Agopian13127d82013-03-05 17:47:11 -08001430// ----------------------------------------------------------------------------
1431// debugging
1432// ----------------------------------------------------------------------------
1433
Marissa Wall61c58622018-07-18 10:12:20 -07001434// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001435LayerDebugInfo Layer::getLayerDebugInfo() const {
1436 LayerDebugInfo info;
1437 const Layer::State& ds = getDrawingState();
1438 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001439 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001440 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
1441 info.mType = String8(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001442 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001443 info.mVisibleRegion = visibleRegion;
1444 info.mSurfaceDamageRegion = surfaceDamageRegion;
1445 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001446 info.mX = ds.active_legacy.transform.tx();
1447 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001448 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001449 info.mWidth = ds.active_legacy.w;
1450 info.mHeight = ds.active_legacy.h;
1451 info.mCrop = ds.crop_legacy;
1452 info.mFinalCrop = ds.finalCrop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001453 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001454 info.mFlags = ds.flags;
1455 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001456 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001457 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1458 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1459 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1460 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001461 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001462 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001463 if (buffer != 0) {
1464 info.mActiveBufferWidth = buffer->getWidth();
1465 info.mActiveBufferHeight = buffer->getHeight();
1466 info.mActiveBufferStride = buffer->getStride();
1467 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001468 } else {
1469 info.mActiveBufferWidth = 0;
1470 info.mActiveBufferHeight = 0;
1471 info.mActiveBufferStride = 0;
1472 info.mActiveBufferFormat = 0;
1473 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001474 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001475 info.mNumQueuedFrames = getQueuedFrameCount();
1476 info.mRefreshPending = isBufferLatched();
1477 info.mIsOpaque = isOpaque(ds);
1478 info.mContentDirty = contentDirty;
1479 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001480}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001481
Dan Stozae22aec72016-08-01 13:20:59 -07001482void Layer::miniDumpHeader(String8& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001483 result.append("-------------------------------");
1484 result.append("-------------------------------");
1485 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001486 result.append(" Layer name\n");
1487 result.append(" Z | ");
1488 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001489 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001490 result.append(" Disp Frame (LTRB) | ");
1491 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001492 result.append("-------------------------------");
1493 result.append("-------------------------------");
1494 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001495}
1496
Dominik Laskowski7e045462018-05-30 13:02:02 -07001497void Layer::miniDump(String8& result, int32_t displayId) const {
1498 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stozae22aec72016-08-01 13:20:59 -07001499 return;
1500 }
1501
1502 String8 name;
1503 if (mName.length() > 77) {
1504 std::string shortened;
1505 shortened.append(mName.string(), 36);
1506 shortened.append("[...]");
1507 shortened.append(mName.string() + (mName.length() - 36), 36);
1508 name = shortened.c_str();
1509 } else {
1510 name = mName;
1511 }
1512
1513 result.appendFormat(" %s\n", name.string());
1514
1515 const Layer::State& layerState(getDrawingState());
Lloyd Pique074e8122018-07-26 12:57:23 -07001516 const LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers.at(displayId);
Chia-I Wu1e043612018-03-01 09:45:09 -08001517 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
1518 result.appendFormat(" rel %6d | ", layerState.z);
1519 } else {
1520 result.appendFormat(" %10d | ", layerState.z);
1521 }
Dominik Laskowski7e045462018-05-30 13:02:02 -07001522 result.appendFormat("%10s | ", to_string(getCompositionType(displayId)).c_str());
Lloyd Pique074e8122018-07-26 12:57:23 -07001523 result.appendFormat("%10s | ", to_string(hwcInfo.transform).c_str());
1524 const Rect& frame = hwcInfo.displayFrame;
David Sodman41fdfc92017-11-06 16:09:56 -08001525 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique074e8122018-07-26 12:57:23 -07001526 const FloatRect& crop = hwcInfo.sourceCrop;
David Sodman41fdfc92017-11-06 16:09:56 -08001527 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 -07001528
Yichi Chen6ca35192018-05-29 12:20:43 +08001529 result.append("- - - - - - - - - - - - - - - -");
1530 result.append("- - - - - - - - - - - - - - - -");
1531 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001532}
Dan Stozae22aec72016-08-01 13:20:59 -07001533
Svetoslavd85084b2014-03-20 10:28:31 -07001534void Layer::dumpFrameStats(String8& result) const {
1535 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001536}
1537
Svetoslavd85084b2014-03-20 10:28:31 -07001538void Layer::clearFrameStats() {
1539 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001540}
1541
Jamie Gennis6547ff42013-07-16 20:12:42 -07001542void Layer::logFrameStats() {
1543 mFrameTracker.logAndResetStats(mName);
1544}
1545
Svetoslavd85084b2014-03-20 10:28:31 -07001546void Layer::getFrameStats(FrameStats* outStats) const {
1547 mFrameTracker.getStats(outStats);
1548}
1549
Brian Andersond6927fb2016-07-23 23:37:30 -07001550void Layer::dumpFrameEvents(String8& result) {
David Sodman41fdfc92017-11-06 16:09:56 -08001551 result.appendFormat("- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001552 Mutex::Autolock lock(mFrameEventHistoryMutex);
1553 mFrameEventHistory.checkFencesForCompletion();
1554 mFrameEventHistory.dump(result);
1555}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001556
Brian Anderson5ea5e592016-12-01 16:54:33 -08001557void Layer::onDisconnect() {
1558 Mutex::Autolock lock(mFrameEventHistoryMutex);
1559 mFrameEventHistory.onDisconnect();
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001560 mTimeStats.onDisconnect(getName().c_str());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001561}
1562
Brian Anderson3890c392016-07-25 12:48:08 -07001563void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001564 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001565 if (newTimestamps) {
1566 mTimeStats.setPostTime(getName().c_str(), newTimestamps->frameNumber,
1567 newTimestamps->postedTime);
1568 }
1569
Brian Andersond6927fb2016-07-23 23:37:30 -07001570 Mutex::Autolock lock(mFrameEventHistoryMutex);
1571 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001572 // If there are any unsignaled fences in the aquire timeline at this
1573 // point, the previously queued frame hasn't been latched yet. Go ahead
1574 // and try to get the signal time here so the syscall is taken out of
1575 // the main thread's critical path.
1576 mAcquireTimeline.updateSignalTimes();
1577 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001578 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001579 mFrameEventHistory.addQueue(*newTimestamps);
1580 }
1581
Brian Anderson3890c392016-07-25 12:48:08 -07001582 if (outDelta) {
1583 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001584 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001585}
Dan Stozae77c7662016-05-13 11:37:28 -07001586
Chia-I Wu98f1c102017-05-30 14:54:08 -07001587size_t Layer::getChildrenCount() const {
1588 size_t count = 0;
1589 for (const sp<Layer>& child : mCurrentChildren) {
1590 count += 1 + child->getChildrenCount();
1591 }
1592 return count;
1593}
1594
Robert Carr1f0a16a2016-10-24 16:27:39 -07001595void Layer::addChild(const sp<Layer>& layer) {
1596 mCurrentChildren.add(layer);
1597 layer->setParent(this);
1598}
1599
1600ssize_t Layer::removeChild(const sp<Layer>& layer) {
1601 layer->setParent(nullptr);
1602 return mCurrentChildren.remove(layer);
1603}
1604
Robert Carr1db73f62016-12-21 12:58:51 -08001605bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1606 sp<Handle> handle = nullptr;
1607 sp<Layer> newParent = nullptr;
1608 if (newParentHandle == nullptr) {
1609 return false;
1610 }
1611 handle = static_cast<Handle*>(newParentHandle.get());
1612 newParent = handle->owner.promote();
1613 if (newParent == nullptr) {
1614 ALOGE("Unable to promote Layer handle");
1615 return false;
1616 }
1617
1618 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001619 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001620
1621 sp<Client> client(child->mClientRef.promote());
1622 if (client != nullptr) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001623 client->updateParent(newParent);
Robert Carr1db73f62016-12-21 12:58:51 -08001624 }
1625 }
1626 mCurrentChildren.clear();
1627
1628 return true;
1629}
1630
Robert Carr15eae092018-03-23 13:43:53 -07001631void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001632 for (const sp<Layer>& child : mDrawingChildren) {
1633 child->mDrawingParent = newParent;
1634 }
1635}
1636
chaviwf1961f72017-09-18 16:41:07 -07001637bool Layer::reparent(const sp<IBinder>& newParentHandle) {
1638 if (newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001639 return false;
1640 }
1641
1642 auto handle = static_cast<Handle*>(newParentHandle.get());
1643 sp<Layer> newParent = handle->owner.promote();
1644 if (newParent == nullptr) {
1645 ALOGE("Unable to promote Layer handle");
1646 return false;
1647 }
1648
chaviwf1961f72017-09-18 16:41:07 -07001649 sp<Layer> parent = getParent();
1650 if (parent != nullptr) {
1651 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001652 }
chaviwf1961f72017-09-18 16:41:07 -07001653 newParent->addChild(this);
chaviw06178942017-07-27 10:25:59 -07001654
chaviwf1961f72017-09-18 16:41:07 -07001655 sp<Client> client(mClientRef.promote());
chaviw06178942017-07-27 10:25:59 -07001656 sp<Client> newParentClient(newParent->mClientRef.promote());
1657
chaviwf1961f72017-09-18 16:41:07 -07001658 if (client != newParentClient) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001659 client->updateParent(newParent);
chaviw06178942017-07-27 10:25:59 -07001660 }
1661
chaviw06178942017-07-27 10:25:59 -07001662 return true;
1663}
1664
Robert Carr9524cb32017-02-13 11:32:32 -08001665bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001666 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001667 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001668 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001669 if (client != nullptr && parentClient != client) {
Robert Carr7f619b22017-11-06 12:56:35 -08001670 client->detachLayer(child.get());
1671 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001672 }
Robert Carr7f619b22017-11-06 12:56:35 -08001673 }
Robert Carr9524cb32017-02-13 11:32:32 -08001674
1675 return true;
1676}
1677
Chia-I Wu11481472018-05-04 10:43:19 -07001678bool Layer::isLegacyDataSpace() const {
1679 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001680 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001681 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001682}
1683
Robert Carr1f0a16a2016-10-24 16:27:39 -07001684void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001685 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001686}
1687
1688void Layer::clearSyncPoints() {
1689 for (const auto& child : mCurrentChildren) {
1690 child->clearSyncPoints();
1691 }
1692
1693 Mutex::Autolock lock(mLocalSyncPointMutex);
1694 for (auto& point : mLocalSyncPoints) {
1695 point->setFrameAvailable();
1696 }
1697 mLocalSyncPoints.clear();
1698}
1699
1700int32_t Layer::getZ() const {
1701 return mDrawingState.z;
1702}
1703
Robert Carr29abff82017-12-04 13:51:20 -08001704bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1705 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1706 const State& state = useDrawing ? mDrawingState : mCurrentState;
1707 return state.zOrderRelativeOf != nullptr;
1708}
1709
David Sodman41fdfc92017-11-06 16:09:56 -08001710__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001711 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001712 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1713 "makeTraversalList received invalid stateSet");
1714 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1715 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1716 const State& state = useDrawing ? mDrawingState : mCurrentState;
1717
Robert Carr29abff82017-12-04 13:51:20 -08001718 if (state.zOrderRelatives.size() == 0) {
1719 *outSkipRelativeZUsers = true;
1720 return children;
1721 }
1722
chaviwfd462612018-05-31 16:11:27 -07001723 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001724 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001725 sp<Layer> strongRelative = weakRelative.promote();
1726 if (strongRelative != nullptr) {
1727 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001728 }
1729 }
1730
Dan Stoza412903f2017-04-27 13:42:17 -07001731 for (const sp<Layer>& child : children) {
Robert Carr503c7042017-09-27 15:06:08 -07001732 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1733 if (childState.zOrderRelativeOf != nullptr) {
1734 continue;
1735 }
Robert Carrdb66e622017-04-10 16:55:57 -07001736 traverse.add(child);
1737 }
1738
1739 return traverse;
1740}
1741
Robert Carr1f0a16a2016-10-24 16:27:39 -07001742/**
Robert Carrdb66e622017-04-10 16:55:57 -07001743 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001744 */
Dan Stoza412903f2017-04-27 13:42:17 -07001745void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001746 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1747 // produce a new list for traversing, including our relatives, and not including our children
1748 // who are relatives of another surface. In the case that there are no relative Z,
1749 // makeTraversalList returns our children directly to avoid significant overhead.
1750 // However in this case we need to take the responsibility for filtering children which
1751 // are relatives of another surface here.
1752 bool skipRelativeZUsers = false;
1753 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001754
Robert Carr1f0a16a2016-10-24 16:27:39 -07001755 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001756 for (; i < list.size(); i++) {
1757 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001758 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1759 continue;
1760 }
1761
Robert Carrdb66e622017-04-10 16:55:57 -07001762 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001763 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001764 }
Dan Stoza412903f2017-04-27 13:42:17 -07001765 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001766 }
Robert Carr29abff82017-12-04 13:51:20 -08001767
Dan Stoza412903f2017-04-27 13:42:17 -07001768 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001769 for (; i < list.size(); i++) {
1770 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001771
1772 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1773 continue;
1774 }
Dan Stoza412903f2017-04-27 13:42:17 -07001775 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001776 }
1777}
1778
1779/**
Robert Carrdb66e622017-04-10 16:55:57 -07001780 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001781 */
Dan Stoza412903f2017-04-27 13:42:17 -07001782void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1783 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001784 // See traverseInZOrder for documentation.
1785 bool skipRelativeZUsers = false;
1786 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001787
Robert Carr1f0a16a2016-10-24 16:27:39 -07001788 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001789 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001790 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001791
1792 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1793 continue;
1794 }
1795
Robert Carrdb66e622017-04-10 16:55:57 -07001796 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001797 break;
1798 }
Dan Stoza412903f2017-04-27 13:42:17 -07001799 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001800 }
Dan Stoza412903f2017-04-27 13:42:17 -07001801 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001802 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001803 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001804
1805 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1806 continue;
1807 }
1808
Dan Stoza412903f2017-04-27 13:42:17 -07001809 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001810 }
1811}
1812
chaviw4b129c22018-04-09 16:19:43 -07001813LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1814 const std::vector<Layer*>& layersInTree) {
1815 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1816 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001817 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1818 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
chaviw4b129c22018-04-09 16:19:43 -07001819 const State& state = useDrawing ? mDrawingState : mCurrentState;
1820
chaviwfd462612018-05-31 16:11:27 -07001821 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001822 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1823 sp<Layer> strongRelative = weakRelative.promote();
1824 // Only add relative layers that are also descendents of the top most parent of the tree.
1825 // If a relative layer is not a descendent, then it should be ignored.
1826 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1827 traverse.add(strongRelative);
1828 }
1829 }
1830
1831 for (const sp<Layer>& child : children) {
1832 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1833 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1834 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1835 // the child here since it won't be added later as a relative.
1836 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1837 childState.zOrderRelativeOf.promote().get())) {
1838 continue;
1839 }
1840 traverse.add(child);
1841 }
1842
1843 return traverse;
1844}
1845
1846void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1847 LayerVector::StateSet stateSet,
1848 const LayerVector::Visitor& visitor) {
1849 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001850
1851 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001852 for (; i < list.size(); i++) {
1853 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001854 if (relative->getZ() >= 0) {
1855 break;
1856 }
chaviw4b129c22018-04-09 16:19:43 -07001857 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001858 }
chaviw4b129c22018-04-09 16:19:43 -07001859
chaviwa76b2712017-09-20 12:02:26 -07001860 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001861 for (; i < list.size(); i++) {
1862 const auto& relative = list[i];
1863 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001864 }
1865}
1866
chaviw4b129c22018-04-09 16:19:43 -07001867std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1868 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1869 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1870
1871 std::vector<Layer*> layersInTree = {this};
1872 for (size_t i = 0; i < children.size(); i++) {
1873 const auto& child = children[i];
1874 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1875 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1876 }
1877
1878 return layersInTree;
1879}
1880
1881void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1882 const LayerVector::Visitor& visitor) {
1883 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1884 std::sort(layersInTree.begin(), layersInTree.end());
1885 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1886}
1887
Robert Carr1f0a16a2016-10-24 16:27:39 -07001888Transform Layer::getTransform() const {
1889 Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001890 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001891 if (p != nullptr) {
1892 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07001893
1894 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
1895 // it isFixedSize) then there may be additional scaling not accounted
1896 // for in the transform. We need to mirror this scaling in child surfaces
1897 // or we will break the contract where WM can treat child surfaces as
1898 // pixels in the parent surface.
David Sodman0cc69182017-11-17 12:12:07 -08001899 if (p->isFixedSize() && p->getBE().compositionInfo.mBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07001900 int bufferWidth;
1901 int bufferHeight;
1902 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
David Sodman0cc69182017-11-17 12:12:07 -08001903 bufferWidth = p->getBE().compositionInfo.mBuffer->getWidth();
1904 bufferHeight = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001905 } else {
David Sodman0cc69182017-11-17 12:12:07 -08001906 bufferHeight = p->getBE().compositionInfo.mBuffer->getWidth();
1907 bufferWidth = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001908 }
Marissa Wall61c58622018-07-18 10:12:20 -07001909 float sx = p->getActiveWidth(p->getDrawingState()) / static_cast<float>(bufferWidth);
1910 float sy = p->getActiveHeight(p->getDrawingState()) / static_cast<float>(bufferHeight);
Robert Carr9b429f42017-04-17 14:56:57 -07001911 Transform extraParentScaling;
1912 extraParentScaling.set(sx, 0, 0, sy);
1913 t = t * extraParentScaling;
1914 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001915 }
Marissa Wall61c58622018-07-18 10:12:20 -07001916 return t * getActiveTransform(getDrawingState());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001917}
1918
chaviw13fdc492017-06-27 12:40:18 -07001919half Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001920 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07001921
chaviw13fdc492017-06-27 12:40:18 -07001922 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1923 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001924}
Robert Carr6452f122017-03-21 10:41:29 -07001925
chaviw13fdc492017-06-27 12:40:18 -07001926half4 Layer::getColor() const {
1927 const half4 color(getDrawingState().color);
1928 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001929}
Robert Carr6452f122017-03-21 10:41:29 -07001930
Robert Carr1f0a16a2016-10-24 16:27:39 -07001931void Layer::commitChildList() {
1932 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1933 const auto& child = mCurrentChildren[i];
1934 child->commitChildList();
1935 }
1936 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001937 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001938}
1939
chaviw1d044282017-09-27 12:19:28 -07001940void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
1941 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1942 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1943 const State& state = useDrawing ? mDrawingState : mCurrentState;
1944
Marissa Wallf58c14b2018-07-24 10:50:43 -07001945 Transform requestedTransform = state.active_legacy.transform;
chaviw1d044282017-09-27 12:19:28 -07001946 Transform transform = getTransform();
1947
1948 layerInfo->set_id(sequence);
1949 layerInfo->set_name(getName().c_str());
1950 layerInfo->set_type(String8(getTypeId()));
1951
1952 for (const auto& child : children) {
1953 layerInfo->add_children(child->sequence);
1954 }
1955
1956 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1957 sp<Layer> strongRelative = weakRelative.promote();
1958 if (strongRelative != nullptr) {
1959 layerInfo->add_relatives(strongRelative->sequence);
1960 }
1961 }
1962
Marissa Wallf58c14b2018-07-24 10:50:43 -07001963 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
chaviw1d044282017-09-27 12:19:28 -07001964 layerInfo->mutable_transparent_region());
1965 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
1966 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
1967
1968 layerInfo->set_layer_stack(getLayerStack());
1969 layerInfo->set_z(state.z);
1970
1971 PositionProto* position = layerInfo->mutable_position();
1972 position->set_x(transform.tx());
1973 position->set_y(transform.ty());
1974
1975 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
1976 requestedPosition->set_x(requestedTransform.tx());
1977 requestedPosition->set_y(requestedTransform.ty());
1978
1979 SizeProto* size = layerInfo->mutable_size();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001980 size->set_w(state.active_legacy.w);
1981 size->set_h(state.active_legacy.h);
chaviw1d044282017-09-27 12:19:28 -07001982
Marissa Wallf58c14b2018-07-24 10:50:43 -07001983 LayerProtoHelper::writeToProto(state.crop_legacy, layerInfo->mutable_crop());
1984 LayerProtoHelper::writeToProto(state.finalCrop_legacy, layerInfo->mutable_final_crop());
chaviw1d044282017-09-27 12:19:28 -07001985
1986 layerInfo->set_is_opaque(isOpaque(state));
1987 layerInfo->set_invalidate(contentDirty);
Chia-I Wu01591c92018-05-22 12:03:00 -07001988
1989 // XXX (b/79210409) mCurrentDataSpace is not protected
1990 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
1991
chaviw1d044282017-09-27 12:19:28 -07001992 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1993 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
1994 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
1995 layerInfo->set_flags(state.flags);
1996
1997 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1998 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
1999
Jorim Jaggi8e0af362017-11-14 16:28:28 +01002000 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07002001 if (parent != nullptr) {
2002 layerInfo->set_parent(parent->sequence);
2003 }
2004
2005 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2006 if (zOrderRelativeOf != nullptr) {
2007 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
2008 }
2009
Chia-I Wu01591c92018-05-22 12:03:00 -07002010 // XXX getBE().compositionInfo.mBuffer is not protected
David Sodman0cc69182017-11-17 12:12:07 -08002011 auto buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08002012 if (buffer != nullptr) {
2013 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
Yichi Chen6ca35192018-05-29 12:20:43 +08002014 LayerProtoHelper::writeToProto(Transform(mCurrentTransform),
2015 layerInfo->mutable_buffer_transform());
chaviw1d044282017-09-27 12:19:28 -07002016 }
2017
2018 layerInfo->set_queued_frames(getQueuedFrameCount());
2019 layerInfo->set_refresh_pending(isBufferLatched());
rongliucfb187b2018-03-14 12:26:23 -07002020 layerInfo->set_window_type(state.type);
2021 layerInfo->set_app_id(state.appId);
chaviwadc40c22018-07-10 16:57:27 -07002022 layerInfo->set_curr_frame(mCurrentFrameNumber);
2023
2024 for (const auto& pendingState : mPendingStates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07002025 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
chaviwadc40c22018-07-10 16:57:27 -07002026 if (barrierLayer != nullptr) {
2027 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
2028 barrierLayerProto->set_id(barrierLayer->sequence);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002029 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
chaviwadc40c22018-07-10 16:57:27 -07002030 }
2031 }
chaviw1d044282017-09-27 12:19:28 -07002032}
2033
Dominik Laskowski7e045462018-05-30 13:02:02 -07002034void Layer::writeToProto(LayerProto* layerInfo, int32_t displayId) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07002035 if (!hasHwcLayer(displayId)) {
2036 return;
2037 }
2038
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002039 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
2040
Dominik Laskowski7e045462018-05-30 13:02:02 -07002041 const auto& hwcInfo = getBE().mHwcLayers.at(displayId);
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002042
2043 const Rect& frame = hwcInfo.displayFrame;
2044 LayerProtoHelper::writeToProto(frame, layerInfo->mutable_hwc_frame());
2045
2046 const FloatRect& crop = hwcInfo.sourceCrop;
2047 LayerProtoHelper::writeToProto(crop, layerInfo->mutable_hwc_crop());
2048
2049 const int32_t transform = static_cast<int32_t>(hwcInfo.transform);
2050 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002051
2052 const int32_t compositionType = static_cast<int32_t>(hwcInfo.compositionType);
2053 layerInfo->set_hwc_composition_type(compositionType);
2054
2055 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
2056 static_cast<BufferLayer*>(this)->isProtected()) {
2057 layerInfo->set_is_protected(true);
2058 } else {
2059 layerInfo->set_is_protected(false);
2060 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002061}
2062
Mathias Agopian13127d82013-03-05 17:47:11 -08002063// ---------------------------------------------------------------------------
2064
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002065}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002066
2067#if defined(__gl_h_)
2068#error "don't include gl/gl.h in this file"
2069#endif
2070
2071#if defined(__gl2_h_)
2072#error "don't include gl2/gl2.h in this file"
2073#endif