blob: 72f1fc4363dc1cc169fe034978bd7e5b7d1591a4 [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 Zhang7c64f172018-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
David Sodman9eeae692017-11-02 10:53:32 -070066LayerBE::LayerBE()
David Sodman0cc69182017-11-17 12:12:07 -080067 : mMesh(Mesh::TRIANGLE_FAN, 4, 2, 2) {
David Sodman9eeae692017-11-02 10:53:32 -070068}
69
70
Mathias Agopian13127d82013-03-05 17:47:11 -080071int32_t Layer::sSequence = 1;
72
David Sodman41fdfc92017-11-06 16:09:56 -080073Layer::Layer(SurfaceFlinger* flinger, const sp<Client>& client, const String8& name, uint32_t w,
74 uint32_t h, uint32_t flags)
David Sodman0c69cad2017-08-21 12:12:51 -070075 : contentDirty(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080076 sequence(uint32_t(android_atomic_inc(&sSequence))),
77 mFlinger(flinger),
Mathias Agopian13127d82013-03-05 17:47:11 -080078 mPremultipliedAlpha(true),
David Sodman0c69cad2017-08-21 12:12:51 -070079 mName(name),
Mathias Agopian13127d82013-03-05 17:47:11 -080080 mTransactionFlags(0),
Dan Stoza7dde5992015-05-22 09:51:44 -070081 mPendingStateMutex(),
82 mPendingStates(),
Mathias Agopiana67932f2011-04-20 14:20:59 -070083 mQueuedFrames(0),
Jesse Hall399184a2014-03-03 15:42:54 -080084 mSidebandStreamChanged(false),
Mathias Agopiana9347642017-02-13 16:42:28 -080085 mActiveBufferSlot(BufferQueue::INVALID_BUFFER_SLOT),
Mathias Agopiana67932f2011-04-20 14:20:59 -070086 mCurrentTransform(0),
Robert Carrc3574f72016-03-24 12:19:32 -070087 mOverrideScalingMode(-1),
Mathias Agopiana67932f2011-04-20 14:20:59 -070088 mCurrentOpacity(true),
Dan Stozacac35382016-01-27 12:21:06 -080089 mCurrentFrameNumber(0),
Mathias Agopian82d7ab62012-01-19 18:34:40 -080090 mFrameLatencyNeeded(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080091 mNeedsFiltering(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080092 mProtectedByApp(false),
Riley Andrews03414a12014-07-01 14:22:59 -070093 mClientRef(client),
Dan Stozaa4650a52015-05-12 12:56:16 -070094 mPotentialCursor(false),
95 mQueueItemLock(),
96 mQueueItemCondition(),
97 mQueueItems(),
Dan Stoza65476f32015-05-14 09:27:25 -070098 mLastFrameNumberReceived(0),
Robert Carr82364e32016-05-15 11:27:47 -070099 mAutoRefresh(false),
chaviw738df022018-05-31 16:11:27 -0700100 mFreezeGeometryUpdates(false),
101 mCurrentChildren(LayerVector::StateSet::Current),
102 mDrawingChildren(LayerVector::StateSet::Drawing) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700103 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700104
105 uint32_t layerFlags = 0;
David Sodman41fdfc92017-11-06 16:09:56 -0800106 if (flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
107 if (flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
108 if (flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700109
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700110 mName = name;
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700111 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700112
113 mCurrentState.active.w = w;
114 mCurrentState.active.h = h;
David Sodman0c69cad2017-08-21 12:12:51 -0700115 mCurrentState.flags = layerFlags;
Robert Carr3dcabfa2016-03-01 18:36:58 -0800116 mCurrentState.active.transform.set(0, 0);
Robert Carrb5d3d262016-03-25 15:08:13 -0700117 mCurrentState.crop.makeInvalid();
118 mCurrentState.finalCrop.makeInvalid();
Robert Carr7bf247e2017-05-18 14:02:49 -0700119 mCurrentState.requestedFinalCrop = mCurrentState.finalCrop;
120 mCurrentState.requestedCrop = mCurrentState.crop;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700121 mCurrentState.z = 0;
chaviw13fdc492017-06-27 12:40:18 -0700122 mCurrentState.color.a = 1.0f;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700123 mCurrentState.layerStack = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700124 mCurrentState.sequence = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700125 mCurrentState.requested = mCurrentState.active;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500126 mCurrentState.appId = 0;
127 mCurrentState.type = 0;
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 Jaggiedc1f1b2018-08-10 14:37:31 +0200135 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Dan Stoza9fdb7e02018-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
Steven Thomasb02664d2017-07-26 18:48:28 -0700215bool Layer::createHwcLayer(HWComposer* hwc, int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700216 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.count(hwcId) != 0,
David Sodman9eeae692017-11-02 10:53:32 -0700217 "Already have a layer for hwcId %d", hwcId);
Steven Thomasb02664d2017-07-26 18:48:28 -0700218 HWC2::Layer* layer = hwc->createLayer(hwcId);
219 if (!layer) {
220 return false;
221 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700222 LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers[hwcId];
Steven Thomasb02664d2017-07-26 18:48:28 -0700223 hwcInfo.hwc = hwc;
224 hwcInfo.layer = layer;
225 layer->setLayerDestroyedListener(
David Sodman6f65f3e2017-11-03 14:28:09 -0700226 [this, hwcId](HWC2::Layer* /*layer*/) { getBE().mHwcLayers.erase(hwcId); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700227 return true;
228}
229
Chia-I Wu83806892017-11-16 10:50:20 -0800230bool Layer::destroyHwcLayer(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700231 if (getBE().mHwcLayers.count(hwcId) == 0) {
Chia-I Wu83806892017-11-16 10:50:20 -0800232 return false;
Steven Thomasb02664d2017-07-26 18:48:28 -0700233 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700234 auto& hwcInfo = getBE().mHwcLayers[hwcId];
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");
237 hwcInfo.hwc->destroyLayer(hwcId, hwcInfo.layer);
238 // The layer destroyed listener should have cleared the entry from
239 // mHwcLayers. Verify that.
David Sodman6f65f3e2017-11-03 14:28:09 -0700240 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.count(hwcId) != 0,
241 "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());
292 Rect win(s.active.w, s.active.h);
293
294 if (!s.crop.isEmpty()) {
295 win.intersect(s.crop, &win);
296 }
297
298 Transform t = getTransform();
299 win = t.transform(win);
300
Robert Carr41b08b52017-06-01 16:11:34 -0700301 if (!s.finalCrop.isEmpty()) {
302 win.intersect(s.finalCrop, &win);
303 }
304
Chia-I Wue41dbe62017-06-13 14:10:56 -0700305 const sp<Layer>& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700306 // Now we need to calculate the parent bounds, so we can clip ourselves to those.
307 // When calculating the parent bounds for purposes of clipping,
308 // we don't need to constrain the parent to its transparent region.
309 // The transparent region is an optimization based on the
310 // buffer contents of the layer, but does not affect the space allocated to
311 // it by policy, and thus children should be allowed to extend into the
312 // parent's transparent region. In fact one of the main uses, is to reduce
313 // buffer allocation size in cases where a child window sits behind a main window
314 // (by marking the hole in the parent window as a transparent region)
315 if (p != nullptr) {
316 Rect bounds = p->computeScreenBounds(false);
317 bounds.intersect(win, &win);
318 }
319
320 if (reduceTransparentRegion) {
321 auto const screenTransparentRegion = t.transform(s.activeTransparentRegion);
322 win = reduce(win, screenTransparentRegion);
323 }
324
325 return win;
326}
327
Dan Stoza80d61162017-12-20 15:57:52 -0800328FloatRect Layer::computeBounds() const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700329 const Layer::State& s(getDrawingState());
Michael Lentine6c925ed2014-09-26 17:55:01 -0700330 return computeBounds(s.activeTransparentRegion);
331}
332
Dan Stoza80d61162017-12-20 15:57:52 -0800333FloatRect Layer::computeBounds(const Region& activeTransparentRegion) const {
Michael Lentine6c925ed2014-09-26 17:55:01 -0700334 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800335 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700336
337 if (!s.crop.isEmpty()) {
338 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800339 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700340
Chia-I Wue41dbe62017-06-13 14:10:56 -0700341 const auto& p = mDrawingParent.promote();
Robert Carrd4ae7f32018-06-07 16:10:57 -0700342 FloatRect floatWin = win.toFloatRect();
343 FloatRect parentBounds = floatWin;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700344 if (p != nullptr) {
Robert Carrd4ae7f32018-06-07 16:10:57 -0700345 // We pass an empty Region here for reasons mirroring that of the case described in
346 // the computeScreenBounds reduceTransparentRegion=false case.
347 parentBounds = p->computeBounds(Region());
Robert Carr1f0a16a2016-10-24 16:27:39 -0700348 }
349
Robert Carrd4ae7f32018-06-07 16:10:57 -0700350 Transform t = s.active.transform;
Dan Stoza80d61162017-12-20 15:57:52 -0800351
Robert Carrd4ae7f32018-06-07 16:10:57 -0700352
353 if (p != nullptr || !s.finalCrop.isEmpty()) {
Dan Stoza80d61162017-12-20 15:57:52 -0800354 floatWin = t.transform(floatWin);
Robert Carrd4ae7f32018-06-07 16:10:57 -0700355 floatWin = floatWin.intersect(parentBounds);
356
357 if (!s.finalCrop.isEmpty()) {
358 floatWin = floatWin.intersect(s.finalCrop.toFloatRect());
359 }
Dan Stoza80d61162017-12-20 15:57:52 -0800360 floatWin = t.inverse().transform(floatWin);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700361 }
362
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700363 // subtract the transparent region and snap to the bounds
Dan Stoza80d61162017-12-20 15:57:52 -0800364 return reduce(floatWin, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800365}
366
Robert Carr1f0a16a2016-10-24 16:27:39 -0700367Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& hw) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700368 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800369 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700370 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800371
372 // apply the projection's clipping to the window crop in
373 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700374 // if there are no window scaling involved, this operation will map to full
375 // pixels in the buffer.
376 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
377 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700378
379 Rect activeCrop(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700380 if (!s.crop.isEmpty()) {
Chia-I Wudf7867f2017-07-20 14:24:37 -0700381 activeCrop.intersect(s.crop, &activeCrop);
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700382 }
383
Robert Carr1f0a16a2016-10-24 16:27:39 -0700384 Transform t = getTransform();
385 activeCrop = t.transform(activeCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000386 if (!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
387 activeCrop.clear();
388 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700389 if (!s.finalCrop.isEmpty()) {
David Sodman41fdfc92017-11-06 16:09:56 -0800390 if (!activeCrop.intersect(s.finalCrop, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000391 activeCrop.clear();
392 }
393 }
chaviwb1154d12017-10-31 14:15:36 -0700394
395 const auto& p = mDrawingParent.promote();
396 if (p != nullptr) {
397 auto parentCrop = p->computeInitialCrop(hw);
398 activeCrop.intersect(parentCrop, &activeCrop);
399 }
400
Robert Carr1f0a16a2016-10-24 16:27:39 -0700401 return activeCrop;
402}
403
Dan Stoza5a423ea2017-02-16 14:10:39 -0800404FloatRect Layer::computeCrop(const sp<const DisplayDevice>& hw) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700405 // the content crop is the area of the content that gets scaled to the
406 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800407 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700408
409 // In addition there is a WM-specified crop we pull from our drawing state.
410 const State& s(getDrawingState());
411
412 // Screen space to make reduction to parent crop clearer.
413 Rect activeCrop = computeInitialCrop(hw);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700414 Transform t = getTransform();
415 // Back to layer space to work with the content crop.
416 activeCrop = t.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800417
Michael Lentine28ea2172014-11-19 18:32:37 -0800418 // This needs to be here as transform.transform(Rect) computes the
419 // transformed rect and then takes the bounding box of the result before
420 // returning. This means
421 // transform.inverse().transform(transform.transform(Rect)) != Rect
422 // in which case we need to make sure the final rect is clipped to the
423 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000424 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
425 activeCrop.clear();
426 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800427
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700428 // subtract the transparent region and snap to the bounds
429 activeCrop = reduce(activeCrop, s.activeTransparentRegion);
430
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000431 // Transform the window crop to match the buffer coordinate system,
432 // which means using the inverse of the current transform set on the
433 // SurfaceFlingerConsumer.
434 uint32_t invTransform = mCurrentTransform;
Robert Carrcae605c2017-03-29 12:10:31 -0700435 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000436 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700437 * the code below applies the primary display's inverse transform to the
438 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000439 */
David Sodman41fdfc92017-11-06 16:09:56 -0800440 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000441 // calculate the inverse transform
442 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800443 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800444 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000445 // and apply to the current transform
David Sodman41fdfc92017-11-06 16:09:56 -0800446 invTransform = (Transform(invTransformOrient) * Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800447 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000448
449 int winWidth = s.active.w;
450 int winHeight = s.active.h;
451 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
452 // If the activeCrop has been rotate the ends are rotated but not
453 // the space itself so when transforming ends back we can't rely on
454 // a modification of the axes of rotation. To account for this we
455 // need to reorient the inverse rotation in terms of the current
456 // axes of rotation.
457 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
458 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
459 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800460 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000461 }
462 winWidth = s.active.h;
463 winHeight = s.active.w;
464 }
David Sodman41fdfc92017-11-06 16:09:56 -0800465 const Rect winCrop = activeCrop.transform(invTransform, s.active.w, s.active.h);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000466
467 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800468 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000469 float yScale = crop.getHeight() / float(winHeight);
470
David Sodman41fdfc92017-11-06 16:09:56 -0800471 float insetL = winCrop.left * xScale;
472 float insetT = winCrop.top * yScale;
473 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000474 float insetB = (winHeight - winCrop.bottom) * yScale;
475
David Sodman41fdfc92017-11-06 16:09:56 -0800476 crop.left += insetL;
477 crop.top += insetT;
478 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000479 crop.bottom -= insetB;
480
Mathias Agopian13127d82013-03-05 17:47:11 -0800481 return crop;
482}
483
Robert Carrae060832016-11-28 10:51:00 -0800484void Layer::setGeometry(const sp<const DisplayDevice>& displayDevice, uint32_t z)
Mathias Agopiana350ff92010-08-10 17:14:02 -0700485{
Dan Stoza9e56aa02015-11-02 13:00:03 -0800486 const auto hwcId = displayDevice->getHwcDisplayId();
Peiyong Lin0874d2b2018-09-04 09:39:34 -0700487 if (!hasHwcLayer(hwcId)) {
488 return;
489 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700490 auto& hwcInfo = getBE().mHwcLayers[hwcId];
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700491
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700492 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800493 hwcInfo.forceClientComposition = false;
494
495 if (isSecure() && !displayDevice->isSecure()) {
496 hwcInfo.forceClientComposition = true;
497 }
498
499 auto& hwcLayer = hwcInfo.layer;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700500
Mathias Agopian13127d82013-03-05 17:47:11 -0800501 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700502 const State& s(getDrawingState());
David Revemanecf0fa52017-03-03 11:32:44 -0500503 auto blendMode = HWC2::BlendMode::None;
Robert Carr6452f122017-03-21 10:41:29 -0700504 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800505 blendMode =
506 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800507 }
David Revemanecf0fa52017-03-03 11:32:44 -0500508 auto error = hwcLayer->setBlendMode(blendMode);
David Sodman41fdfc92017-11-06 16:09:56 -0800509 ALOGE_IF(error != HWC2::Error::None,
510 "[%s] Failed to set blend mode %s:"
511 " %s (%d)",
512 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
513 static_cast<int32_t>(error));
Mathias Agopian13127d82013-03-05 17:47:11 -0800514
515 // apply the layer's transform, followed by the display's global transform
516 // here we're guaranteed that the layer's transform preserves rects
Michael Lentine6c925ed2014-09-26 17:55:01 -0700517 Region activeTransparentRegion(s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700518 Transform t = getTransform();
Robert Carrb5d3d262016-03-25 15:08:13 -0700519 if (!s.crop.isEmpty()) {
520 Rect activeCrop(s.crop);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700521 activeCrop = t.transform(activeCrop);
David Sodman41fdfc92017-11-06 16:09:56 -0800522 if (!activeCrop.intersect(displayDevice->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000523 activeCrop.clear();
524 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700525 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800526 // This needs to be here as transform.transform(Rect) computes the
527 // transformed rect and then takes the bounding box of the result before
528 // returning. This means
529 // transform.inverse().transform(transform.transform(Rect)) != Rect
530 // in which case we need to make sure the final rect is clipped to the
531 // display bounds.
David Sodman41fdfc92017-11-06 16:09:56 -0800532 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000533 activeCrop.clear();
534 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700535 // mark regions outside the crop as transparent
536 activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top));
David Sodman41fdfc92017-11-06 16:09:56 -0800537 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom, s.active.w, s.active.h));
538 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
539 activeTransparentRegion.orSelf(
540 Rect(activeCrop.right, activeCrop.top, s.active.w, activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700541 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700542
Dan Stoza80d61162017-12-20 15:57:52 -0800543 // computeBounds returns a FloatRect to provide more accuracy during the
544 // transformation. We then round upon constructing 'frame'.
545 Rect frame{t.transform(computeBounds(activeTransparentRegion))};
Robert Carrb5d3d262016-03-25 15:08:13 -0700546 if (!s.finalCrop.isEmpty()) {
David Sodman41fdfc92017-11-06 16:09:56 -0800547 if (!frame.intersect(s.finalCrop, &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000548 frame.clear();
549 }
550 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000551 if (!frame.intersect(displayDevice->getViewport(), &frame)) {
552 frame.clear();
553 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800554 const Transform& tr(displayDevice->getTransform());
555 Rect transformedFrame = tr.transform(frame);
David Revemanecf0fa52017-03-03 11:32:44 -0500556 error = hwcLayer->setDisplayFrame(transformedFrame);
Dan Stozae22aec72016-08-01 13:20:59 -0700557 if (error != HWC2::Error::None) {
David Sodman41fdfc92017-11-06 16:09:56 -0800558 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
559 transformedFrame.left, transformedFrame.top, transformedFrame.right,
560 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stozae22aec72016-08-01 13:20:59 -0700561 } else {
562 hwcInfo.displayFrame = transformedFrame;
563 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800564
Dan Stoza5a423ea2017-02-16 14:10:39 -0800565 FloatRect sourceCrop = computeCrop(displayDevice);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800566 error = hwcLayer->setSourceCrop(sourceCrop);
Dan Stozae22aec72016-08-01 13:20:59 -0700567 if (error != HWC2::Error::None) {
568 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
David Sodman41fdfc92017-11-06 16:09:56 -0800569 "%s (%d)",
570 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
571 to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stozae22aec72016-08-01 13:20:59 -0700572 } else {
573 hwcInfo.sourceCrop = sourceCrop;
574 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800575
chaviw13fdc492017-06-27 12:40:18 -0700576 float alpha = static_cast<float>(getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -0700577 error = hwcLayer->setPlaneAlpha(alpha);
David Sodman41fdfc92017-11-06 16:09:56 -0800578 ALOGE_IF(error != HWC2::Error::None,
579 "[%s] Failed to set plane alpha %.3f: "
580 "%s (%d)",
581 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800582
Robert Carrae060832016-11-28 10:51:00 -0800583 error = hwcLayer->setZOrder(z);
David Sodman41fdfc92017-11-06 16:09:56 -0800584 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
585 to_string(error).c_str(), static_cast<int32_t>(error));
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500586
Albert Chaulk2a589632017-05-04 16:59:44 -0400587 int type = s.type;
588 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700589 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400590 if (parent.get()) {
591 auto& parentState = parent->getDrawingState();
rongliuccd34842018-03-14 12:26:23 -0700592 if (parentState.type >= 0 || parentState.appId >= 0) {
593 type = parentState.type;
594 appId = parentState.appId;
595 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400596 }
597
598 error = hwcLayer->setInfo(type, appId);
David Sodman41fdfc92017-11-06 16:09:56 -0800599 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
600 static_cast<int32_t>(error));
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800601
Mathias Agopian29a367b2011-07-12 14:51:45 -0700602 /*
603 * Transformations are applied in this order:
604 * 1) buffer orientation/flip/mirror
605 * 2) state transformation (window manager)
606 * 3) layer orientation (screen orientation)
607 * (NOTE: the matrices are multiplied in reverse order)
608 */
609
610 const Transform bufferOrientation(mCurrentTransform);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700611 Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700612
Robert Carrcae605c2017-03-29 12:10:31 -0700613 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700614 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700615 * the code below applies the primary display's inverse transform to the
616 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700617 */
David Sodman41fdfc92017-11-06 16:09:56 -0800618 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700619 // calculate the inverse transform
620 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800621 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700622 }
Robert Carrcae605c2017-03-29 12:10:31 -0700623
624 /*
625 * Here we cancel out the orientation component of the WM transform.
626 * The scaling and translate components are already included in our bounds
627 * computation so it's enough to just omit it in the composition.
628 * See comment in onDraw with ref to b/36727915 for why.
629 */
630 transform = Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700631 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700632
633 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800634 const uint32_t orientation = transform.getOrientation();
Jorim Jaggif3bd94a2018-03-27 15:38:03 +0200635 if (orientation & Transform::ROT_INVALID) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800636 // we can only handle simple transformation
637 hwcInfo.forceClientComposition = true;
638 } else {
639 auto transform = static_cast<HWC2::Transform>(orientation);
Yiwei Zhang068e31b2018-02-21 13:02:45 -0800640 hwcInfo.transform = transform;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800641 auto error = hwcLayer->setTransform(transform);
David Sodman41fdfc92017-11-06 16:09:56 -0800642 ALOGE_IF(error != HWC2::Error::None,
643 "[%s] Failed to set transform %s: "
644 "%s (%d)",
645 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
646 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800647 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700648}
649
Dan Stoza9e56aa02015-11-02 13:00:03 -0800650void Layer::forceClientComposition(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700651 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800652 ALOGE("forceClientComposition: no HWC layer found (%d)", hwcId);
653 return;
654 }
655
David Sodman6f65f3e2017-11-03 14:28:09 -0700656 getBE().mHwcLayers[hwcId].forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800657}
Dan Stozaee44edd2015-03-23 15:50:23 -0700658
chaviwc9232ed2017-11-14 15:31:15 -0800659bool Layer::getForceClientComposition(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700660 if (getBE().mHwcLayers.count(hwcId) == 0) {
chaviwc9232ed2017-11-14 15:31:15 -0800661 ALOGE("getForceClientComposition: no HWC layer found (%d)", hwcId);
662 return false;
663 }
664
David Sodman6f65f3e2017-11-03 14:28:09 -0700665 return getBE().mHwcLayers[hwcId].forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800666}
667
Dan Stoza9e56aa02015-11-02 13:00:03 -0800668void Layer::updateCursorPosition(const sp<const DisplayDevice>& displayDevice) {
669 auto hwcId = displayDevice->getHwcDisplayId();
David Sodman6f65f3e2017-11-03 14:28:09 -0700670 if (getBE().mHwcLayers.count(hwcId) == 0 ||
David Sodman9eeae692017-11-02 10:53:32 -0700671 getCompositionType(hwcId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800672 return;
673 }
674
675 // This gives us only the "orientation" component of the transform
676 const State& s(getCurrentState());
677
678 // Apply the layer's transform, followed by the display's global transform
679 // Here we're guaranteed that the layer's transform preserves rects
680 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700681 if (!s.crop.isEmpty()) {
682 win.intersect(s.crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800683 }
684 // Subtract the transparent region and snap to the bounds
685 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700686 Rect frame(getTransform().transform(bounds));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800687 frame.intersect(displayDevice->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700688 if (!s.finalCrop.isEmpty()) {
689 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000690 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800691 auto& displayTransform(displayDevice->getTransform());
692 auto position = displayTransform.transform(frame);
693
David Sodman6f65f3e2017-11-03 14:28:09 -0700694 auto error = getBE().mHwcLayers[hwcId].layer->setCursorPosition(position.left,
David Sodman9eeae692017-11-02 10:53:32 -0700695 position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800696 ALOGE_IF(error != HWC2::Error::None,
697 "[%s] Failed to set cursor position "
698 "to (%d, %d): %s (%d)",
699 mName.string(), position.left, position.top, to_string(error).c_str(),
700 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800701}
Riley Andrews03414a12014-07-01 14:22:59 -0700702
Mathias Agopian13127d82013-03-05 17:47:11 -0800703// ---------------------------------------------------------------------------
704// drawing...
705// ---------------------------------------------------------------------------
706
chaviwa76b2712017-09-20 12:02:26 -0700707void Layer::draw(const RenderArea& renderArea, const Region& clip) const {
708 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800709}
710
chaviwa76b2712017-09-20 12:02:26 -0700711void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) const {
712 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800713}
714
chaviwa76b2712017-09-20 12:02:26 -0700715void Layer::draw(const RenderArea& renderArea) const {
716 onDraw(renderArea, Region(renderArea.getBounds()), false);
Dan Stozac7014012014-02-14 15:03:43 -0800717}
718
David Sodman41fdfc92017-11-06 16:09:56 -0800719void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
720 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800721 auto& engine(mFlinger->getRenderEngine());
David Sodman9eeae692017-11-02 10:53:32 -0700722 computeGeometry(renderArea, getBE().mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700723 engine.setupFillWithColor(red, green, blue, alpha);
David Sodman9eeae692017-11-02 10:53:32 -0700724 engine.drawMesh(getBE().mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800725}
726
chaviwa76b2712017-09-20 12:02:26 -0700727void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
David Sodman41fdfc92017-11-06 16:09:56 -0800728 clearWithOpenGL(renderArea, 0, 0, 0, 0);
Mathias Agopian13127d82013-03-05 17:47:11 -0800729}
730
David Sodman41fdfc92017-11-06 16:09:56 -0800731void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type, bool callIntoHwc) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700732 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800733 ALOGE("setCompositionType called without a valid HWC layer");
734 return;
735 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700736 auto& hwcInfo = getBE().mHwcLayers[hwcId];
Dan Stoza9e56aa02015-11-02 13:00:03 -0800737 auto& hwcLayer = hwcInfo.layer;
David Sodman41fdfc92017-11-06 16:09:56 -0800738 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(), to_string(type).c_str(),
739 static_cast<int>(callIntoHwc));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800740 if (hwcInfo.compositionType != type) {
741 ALOGV(" actually setting");
742 hwcInfo.compositionType = type;
743 if (callIntoHwc) {
744 auto error = hwcLayer->setCompositionType(type);
David Sodman41fdfc92017-11-06 16:09:56 -0800745 ALOGE_IF(error != HWC2::Error::None,
746 "[%s] Failed to set "
747 "composition type %s: %s (%d)",
748 mName.string(), to_string(type).c_str(), to_string(error).c_str(),
749 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800750 }
751 }
752}
753
754HWC2::Composition Layer::getCompositionType(int32_t hwcId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -0700755 if (hwcId == DisplayDevice::DISPLAY_ID_INVALID) {
756 // If we're querying the composition type for a display that does not
757 // have a HWC counterpart, then it will always be Client
758 return HWC2::Composition::Client;
759 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700760 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700761 ALOGE("getCompositionType called with an invalid HWC layer");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800762 return HWC2::Composition::Invalid;
763 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700764 return getBE().mHwcLayers.at(hwcId).compositionType;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800765}
766
767void Layer::setClearClientTarget(int32_t hwcId, bool clear) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700768 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800769 ALOGE("setClearClientTarget called without a valid HWC layer");
770 return;
771 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700772 getBE().mHwcLayers[hwcId].clearClientTarget = clear;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800773}
774
775bool Layer::getClearClientTarget(int32_t hwcId) const {
David Sodman6f65f3e2017-11-03 14:28:09 -0700776 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800777 ALOGE("getClearClientTarget called without a valid HWC layer");
778 return false;
779 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700780 return getBE().mHwcLayers.at(hwcId).clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800781}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800782
Dan Stozacac35382016-01-27 12:21:06 -0800783bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
784 if (point->getFrameNumber() <= mCurrentFrameNumber) {
785 // Don't bother with a SyncPoint, since we've already latched the
786 // relevant frame
787 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700788 }
789
Dan Stozacac35382016-01-27 12:21:06 -0800790 Mutex::Autolock lock(mLocalSyncPointMutex);
791 mLocalSyncPoints.push_back(point);
792 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700793}
794
Mathias Agopian13127d82013-03-05 17:47:11 -0800795// ----------------------------------------------------------------------------
796// local state
797// ----------------------------------------------------------------------------
798
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000799static void boundPoint(vec2* point, const Rect& crop) {
800 if (point->x < crop.left) {
801 point->x = crop.left;
802 }
803 if (point->x > crop.right) {
804 point->x = crop.right;
805 }
806 if (point->y < crop.top) {
807 point->y = crop.top;
808 }
809 if (point->y > crop.bottom) {
810 point->y = crop.bottom;
811 }
812}
813
chaviwa76b2712017-09-20 12:02:26 -0700814void Layer::computeGeometry(const RenderArea& renderArea, Mesh& mesh,
815 bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700816 const Layer::State& s(getDrawingState());
chaviwa76b2712017-09-20 12:02:26 -0700817 const Transform renderAreaTransform(renderArea.getTransform());
818 const uint32_t height = renderArea.getHeight();
Dan Stoza80d61162017-12-20 15:57:52 -0800819 FloatRect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700820
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000821 vec2 lt = vec2(win.left, win.top);
822 vec2 lb = vec2(win.left, win.bottom);
823 vec2 rb = vec2(win.right, win.bottom);
824 vec2 rt = vec2(win.right, win.top);
825
Robert Carr1f0a16a2016-10-24 16:27:39 -0700826 Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000827 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700828 lt = layerTransform.transform(lt);
829 lb = layerTransform.transform(lb);
830 rb = layerTransform.transform(rb);
831 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000832 }
833
Robert Carrb5d3d262016-03-25 15:08:13 -0700834 if (!s.finalCrop.isEmpty()) {
835 boundPoint(&lt, s.finalCrop);
836 boundPoint(&lb, s.finalCrop);
837 boundPoint(&rb, s.finalCrop);
838 boundPoint(&rt, s.finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000839 }
840
Mathias Agopianff2ed702013-09-01 21:36:12 -0700841 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700842 position[0] = renderAreaTransform.transform(lt);
843 position[1] = renderAreaTransform.transform(lb);
844 position[2] = renderAreaTransform.transform(rb);
845 position[3] = renderAreaTransform.transform(rt);
David Sodman41fdfc92017-11-06 16:09:56 -0800846 for (size_t i = 0; i < 4; i++) {
chaviwa76b2712017-09-20 12:02:26 -0700847 position[i].y = height - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -0800848 }
849}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800850
David Sodman41fdfc92017-11-06 16:09:56 -0800851bool Layer::isSecure() const {
Dan Stoza23116082015-06-18 14:58:39 -0700852 const Layer::State& s(mDrawingState);
853 return (s.flags & layer_state_t::eLayerSecure);
854}
855
Mathias Agopian13127d82013-03-05 17:47:11 -0800856void Layer::setVisibleRegion(const Region& visibleRegion) {
857 // always called from main thread
858 this->visibleRegion = visibleRegion;
859}
860
861void Layer::setCoveredRegion(const Region& coveredRegion) {
862 // always called from main thread
863 this->coveredRegion = coveredRegion;
864}
865
David Sodman41fdfc92017-11-06 16:09:56 -0800866void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800867 // always called from main thread
868 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
869}
870
Robert Carre5f4f692018-01-12 13:12:28 -0800871void Layer::clearVisibilityRegions() {
872 visibleRegion.clear();
873 visibleNonTransparentRegion.clear();
874 coveredRegion.clear();
875}
876
Mathias Agopian13127d82013-03-05 17:47:11 -0800877// ----------------------------------------------------------------------------
878// transaction
879// ----------------------------------------------------------------------------
880
Dan Stoza7dde5992015-05-22 09:51:44 -0700881void Layer::pushPendingState() {
882 if (!mCurrentState.modified) {
883 return;
884 }
885
Dan Stoza7dde5992015-05-22 09:51:44 -0700886 // If this transaction is waiting on the receipt of a frame, generate a sync
887 // point and send it to the remote layer.
Robert Carr0d480722017-01-10 16:42:54 -0800888 if (mCurrentState.barrierLayer != nullptr) {
889 sp<Layer> barrierLayer = mCurrentState.barrierLayer.promote();
890 if (barrierLayer == nullptr) {
891 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700892 // If we can't promote the layer we are intended to wait on,
893 // then it is expired or otherwise invalid. Allow this transaction
894 // to be applied as per normal (no synchronization).
Robert Carr0d480722017-01-10 16:42:54 -0800895 mCurrentState.barrierLayer = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800896 } else {
David Sodman41fdfc92017-11-06 16:09:56 -0800897 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber);
Robert Carr0d480722017-01-10 16:42:54 -0800898 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800899 mRemoteSyncPoints.push_back(std::move(syncPoint));
900 } else {
901 // We already missed the frame we're supposed to synchronize
902 // on, so go ahead and apply the state update
Robert Carr0d480722017-01-10 16:42:54 -0800903 mCurrentState.barrierLayer = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800904 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700905 }
906
Dan Stoza7dde5992015-05-22 09:51:44 -0700907 // Wake us up to check if the frame has been received
908 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700909 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700910 }
911 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700912 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700913}
914
Pablo Ceballos05289c22016-04-14 15:49:55 -0700915void Layer::popPendingState(State* stateToCommit) {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700916 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700917
918 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700919 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700920}
921
Pablo Ceballos05289c22016-04-14 15:49:55 -0700922bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700923 bool stateUpdateAvailable = false;
924 while (!mPendingStates.empty()) {
Robert Carr0d480722017-01-10 16:42:54 -0800925 if (mPendingStates[0].barrierLayer != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700926 if (mRemoteSyncPoints.empty()) {
927 // If we don't have a sync point for this, apply it anyway. It
928 // will be visually wrong, but it should keep us from getting
929 // into too much trouble.
930 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700931 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700932 stateUpdateAvailable = true;
933 continue;
934 }
935
David Sodman41fdfc92017-11-06 16:09:56 -0800936 if (mRemoteSyncPoints.front()->getFrameNumber() != mPendingStates[0].frameNumber) {
937 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800938
939 // Signal our end of the sync point and then dispose of it
940 mRemoteSyncPoints.front()->setTransactionApplied();
941 mRemoteSyncPoints.pop_front();
942 continue;
943 }
944
Dan Stoza7dde5992015-05-22 09:51:44 -0700945 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
946 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700947 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700948 stateUpdateAvailable = true;
949
950 // Signal our end of the sync point and then dispose of it
951 mRemoteSyncPoints.front()->setTransactionApplied();
952 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800953 } else {
954 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700955 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700956 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700957 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700958 stateUpdateAvailable = true;
959 }
960 }
961
962 // If we still have pending updates, wake SurfaceFlinger back up and point
963 // it at this layer so we can process them
964 if (!mPendingStates.empty()) {
965 setTransactionFlags(eTransactionNeeded);
966 mFlinger->setTransactionFlags(eTraversalNeeded);
967 }
968
969 mCurrentState.modified = false;
970 return stateUpdateAvailable;
971}
972
Mathias Agopian13127d82013-03-05 17:47:11 -0800973uint32_t Layer::doTransaction(uint32_t flags) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800974 ATRACE_CALL();
975
Dan Stoza7dde5992015-05-22 09:51:44 -0700976 pushPendingState();
Pablo Ceballos05289c22016-04-14 15:49:55 -0700977 Layer::State c = getCurrentState();
978 if (!applyPendingStates(&c)) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700979 return 0;
980 }
981
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700982 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800983
David Sodman41fdfc92017-11-06 16:09:56 -0800984 const bool sizeChanged = (c.requested.w != s.requested.w) || (c.requested.h != s.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700985
David Sodmaneb085e02017-10-05 18:49:04 -0700986 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700987 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000988 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800989 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
990 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
991 " requested={ wh={%4u,%4u} }}\n"
992 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
993 " requested={ wh={%4u,%4u} }}\n",
David Sodman9eeae692017-11-02 10:53:32 -0700994 this, getName().string(), mCurrentTransform,
995 getEffectiveScalingMode(), c.active.w, c.active.h, c.crop.left, c.crop.top,
996 c.crop.right, c.crop.bottom, c.crop.getWidth(), c.crop.getHeight(), c.requested.w,
997 c.requested.h, s.active.w, s.active.h, s.crop.left, s.crop.top, s.crop.right,
998 s.crop.bottom, s.crop.getWidth(), s.crop.getHeight(), s.requested.w,
999 s.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001000
Jamie Gennis2a0d5b62011-09-26 16:54:44 -07001001 // record the new size, form this point on, when the client request
1002 // a buffer, it'll get the new size.
David Sodmaneb085e02017-10-05 18:49:04 -07001003 setDefaultBufferSize(c.requested.w, c.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001004 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001005
Robert Carre392b552017-09-19 12:16:05 -07001006 // Don't let Layer::doTransaction update the drawing state
1007 // if we have a pending resize, unless we are in fixed-size mode.
1008 // the drawing state will be updated only once we receive a buffer
1009 // with the correct size.
1010 //
1011 // In particular, we want to make sure the clip (which is part
1012 // of the geometry state) is latched together with the size but is
1013 // latched immediately when no resizing is involved.
1014 //
1015 // If a sideband stream is attached, however, we want to skip this
1016 // optimization so that transactions aren't missed when a buffer
1017 // never arrives
1018 //
1019 // In the case that we don't have a buffer we ignore other factors
1020 // and avoid entering the resizePending state. At a high level the
1021 // resizePending state is to avoid applying the state of the new buffer
1022 // to the old buffer. However in the state where we don't have an old buffer
1023 // there is no such concern but we may still be being used as a parent layer.
David Sodman41fdfc92017-11-06 16:09:56 -08001024 const bool resizePending = ((c.requested.w != c.active.w) || (c.requested.h != c.active.h)) &&
David Sodman0cc69182017-11-17 12:12:07 -08001025 (getBE().compositionInfo.mBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001026 if (!isFixedSize()) {
David Sodman0cc69182017-11-17 12:12:07 -08001027 if (resizePending && getBE().compositionInfo.hwc.sidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001028 flags |= eDontUpdateGeometryState;
1029 }
1030 }
1031
Robert Carr7bf247e2017-05-18 14:02:49 -07001032 // Here we apply various requested geometry states, depending on our
1033 // latching configuration. See Layer.h for a detailed discussion of
1034 // how geometry latching is controlled.
1035 if (!(flags & eDontUpdateGeometryState)) {
Pablo Ceballos7d052572016-06-02 17:46:05 -07001036 Layer::State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001037
1038 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1039 // mode, which causes attributes which normally latch regardless of scaling mode,
1040 // to be delayed. We copy the requested state to the active state making sure
1041 // to respect these rules (again see Layer.h for a detailed discussion).
1042 //
1043 // There is an awkward asymmetry in the handling of the crop states in the position
1044 // states, as can be seen below. Largely this arises from position and transform
1045 // being stored in the same data structure while having different latching rules.
1046 // b/38182305
1047 //
1048 // Careful that "c" and editCurrentState may not begin as equivalent due to
1049 // applyPendingStates in the presence of deferred transactions.
1050 if (mFreezeGeometryUpdates) {
Robert Carr82364e32016-05-15 11:27:47 -07001051 float tx = c.active.transform.tx();
1052 float ty = c.active.transform.ty();
1053 c.active = c.requested;
1054 c.active.transform.set(tx, ty);
1055 editCurrentState.active = c.active;
1056 } else {
1057 editCurrentState.active = editCurrentState.requested;
1058 c.active = c.requested;
1059 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001060 }
1061
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001062 if (s.active != c.active) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001063 // invalidate and recompute the visible regions if needed
1064 flags |= Layer::eVisibleRegion;
1065 }
1066
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001067 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001068 // invalidate and recompute the visible regions if needed
1069 flags |= eVisibleRegion;
1070 this->contentDirty = true;
1071
1072 // we may use linear filtering, if the matrix scales us
Robert Carr3dcabfa2016-03-01 18:36:58 -08001073 const uint8_t type = c.active.transform.getType();
David Sodman41fdfc92017-11-06 16:09:56 -08001074 mNeedsFiltering = (!c.active.transform.preserveRects() || (type >= Transform::SCALE));
Mathias Agopian13127d82013-03-05 17:47:11 -08001075 }
1076
Dan Stozac8145172016-04-28 16:29:06 -07001077 // If the layer is hidden, signal and clear out all local sync points so
1078 // that transactions for layers depending on this layer's frames becoming
1079 // visible are not blocked
1080 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001081 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001082 }
1083
Mathias Agopian13127d82013-03-05 17:47:11 -08001084 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001085 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001086 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001087}
1088
Pablo Ceballos05289c22016-04-14 15:49:55 -07001089void Layer::commitTransaction(const State& stateToCommit) {
1090 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001091}
1092
Mathias Agopian13127d82013-03-05 17:47:11 -08001093uint32_t Layer::getTransactionFlags(uint32_t flags) {
1094 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1095}
1096
1097uint32_t Layer::setTransactionFlags(uint32_t flags) {
1098 return android_atomic_or(flags, &mTransactionFlags);
1099}
1100
Robert Carr82364e32016-05-15 11:27:47 -07001101bool Layer::setPosition(float x, float y, bool immediate) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001102 if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001103 return false;
1104 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001105
1106 // We update the requested and active position simultaneously because
1107 // we want to apply the position portion of the transform matrix immediately,
1108 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Robert Carr3dcabfa2016-03-01 18:36:58 -08001109 mCurrentState.requested.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001110 if (immediate && !mFreezeGeometryUpdates) {
1111 // Here we directly update the active state
1112 // unlike other setters, because we store it within
1113 // the transform, but use different latching rules.
1114 // b/38182305
Robert Carr82364e32016-05-15 11:27:47 -07001115 mCurrentState.active.transform.set(x, y);
1116 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001117 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001118
Dan Stoza7dde5992015-05-22 09:51:44 -07001119 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001120 setTransactionFlags(eTransactionNeeded);
1121 return true;
1122}
Robert Carr82364e32016-05-15 11:27:47 -07001123
Robert Carr1f0a16a2016-10-24 16:27:39 -07001124bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1125 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1126 if (idx < 0) {
1127 return false;
1128 }
1129 if (childLayer->setLayer(z)) {
1130 mCurrentChildren.removeAt(idx);
1131 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001132 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001133 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001134 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001135}
1136
Robert Carr503c7042017-09-27 15:06:08 -07001137bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1138 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1139 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1140 if (idx < 0) {
1141 return false;
1142 }
1143 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1144 mCurrentChildren.removeAt(idx);
1145 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001146 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001147 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001148 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001149}
1150
Robert Carrae060832016-11-28 10:51:00 -08001151bool Layer::setLayer(int32_t z) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001152 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001153 mCurrentState.sequence++;
1154 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001155 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001156
1157 // Discard all relative layering.
1158 if (mCurrentState.zOrderRelativeOf != nullptr) {
1159 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1160 if (strongRelative != nullptr) {
1161 strongRelative->removeZOrderRelative(this);
1162 }
1163 mCurrentState.zOrderRelativeOf = nullptr;
1164 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001165 setTransactionFlags(eTransactionNeeded);
1166 return true;
1167}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001168
Robert Carrdb66e622017-04-10 16:55:57 -07001169void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1170 mCurrentState.zOrderRelatives.remove(relative);
1171 mCurrentState.sequence++;
1172 mCurrentState.modified = true;
1173 setTransactionFlags(eTransactionNeeded);
1174}
1175
1176void Layer::addZOrderRelative(const wp<Layer>& relative) {
1177 mCurrentState.zOrderRelatives.add(relative);
1178 mCurrentState.modified = true;
1179 mCurrentState.sequence++;
1180 setTransactionFlags(eTransactionNeeded);
1181}
1182
Robert Carr503d2bd2017-12-04 15:49:47 -08001183bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001184 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1185 if (handle == nullptr) {
1186 return false;
1187 }
1188 sp<Layer> relative = handle->owner.promote();
1189 if (relative == nullptr) {
1190 return false;
1191 }
1192
Robert Carr503d2bd2017-12-04 15:49:47 -08001193 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1194 mCurrentState.zOrderRelativeOf == relative) {
1195 return false;
1196 }
1197
Robert Carrdb66e622017-04-10 16:55:57 -07001198 mCurrentState.sequence++;
1199 mCurrentState.modified = true;
Robert Carr503d2bd2017-12-04 15:49:47 -08001200 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001201
chaviw9ab4bd12017-11-03 13:11:00 -07001202 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
1203 if (oldZOrderRelativeOf != nullptr) {
1204 oldZOrderRelativeOf->removeZOrderRelative(this);
1205 }
Robert Carrdb66e622017-04-10 16:55:57 -07001206 mCurrentState.zOrderRelativeOf = relative;
1207 relative->addZOrderRelative(this);
1208
1209 setTransactionFlags(eTransactionNeeded);
1210
1211 return true;
1212}
1213
Mathias Agopian13127d82013-03-05 17:47:11 -08001214bool Layer::setSize(uint32_t w, uint32_t h) {
David Sodman41fdfc92017-11-06 16:09:56 -08001215 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001216 mCurrentState.requested.w = w;
1217 mCurrentState.requested.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001218 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001219 setTransactionFlags(eTransactionNeeded);
1220 return true;
1221}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001222bool Layer::setAlpha(float alpha) {
David Sodman41fdfc92017-11-06 16:09:56 -08001223 if (mCurrentState.color.a == alpha) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001224 mCurrentState.sequence++;
chaviw13fdc492017-06-27 12:40:18 -07001225 mCurrentState.color.a = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001226 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001227 setTransactionFlags(eTransactionNeeded);
1228 return true;
1229}
chaviw13fdc492017-06-27 12:40:18 -07001230
1231bool Layer::setColor(const half3& color) {
David Sodman41fdfc92017-11-06 16:09:56 -08001232 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g &&
1233 color.b == mCurrentState.color.b)
chaviw13fdc492017-06-27 12:40:18 -07001234 return false;
1235
1236 mCurrentState.sequence++;
1237 mCurrentState.color.r = color.r;
1238 mCurrentState.color.g = color.g;
1239 mCurrentState.color.b = color.b;
1240 mCurrentState.modified = true;
1241 setTransactionFlags(eTransactionNeeded);
1242 return true;
1243}
1244
Robert Carrd4ae7f32018-06-07 16:10:57 -07001245bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1246 bool allowNonRectPreservingTransforms) {
1247 Transform t;
1248 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1249
1250 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1251 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1252 return false;
1253 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001254 mCurrentState.sequence++;
David Sodman41fdfc92017-11-06 16:09:56 -08001255 mCurrentState.requested.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001256 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001257 setTransactionFlags(eTransactionNeeded);
1258 return true;
1259}
1260bool Layer::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001261 mCurrentState.requestedTransparentRegion = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001262 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001263 setTransactionFlags(eTransactionNeeded);
1264 return true;
1265}
1266bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1267 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
David Sodman41fdfc92017-11-06 16:09:56 -08001268 if (mCurrentState.flags == newFlags) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001269 mCurrentState.sequence++;
1270 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001271 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001272 setTransactionFlags(eTransactionNeeded);
1273 return true;
1274}
Robert Carr99e27f02016-06-16 15:18:02 -07001275
1276bool Layer::setCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001277 if (mCurrentState.requestedCrop == crop) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001278 mCurrentState.sequence++;
Robert Carr99e27f02016-06-16 15:18:02 -07001279 mCurrentState.requestedCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001280 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr99e27f02016-06-16 15:18:02 -07001281 mCurrentState.crop = crop;
1282 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001283 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1284
Dan Stoza7dde5992015-05-22 09:51:44 -07001285 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001286 setTransactionFlags(eTransactionNeeded);
1287 return true;
1288}
Robert Carr8d5227b2017-03-16 15:41:03 -07001289
1290bool Layer::setFinalCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001291 if (mCurrentState.requestedFinalCrop == crop) return false;
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001292 mCurrentState.sequence++;
Robert Carr8d5227b2017-03-16 15:41:03 -07001293 mCurrentState.requestedFinalCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001294 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr8d5227b2017-03-16 15:41:03 -07001295 mCurrentState.finalCrop = crop;
1296 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001297 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1298
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001299 mCurrentState.modified = true;
1300 setTransactionFlags(eTransactionNeeded);
1301 return true;
1302}
Mathias Agopian13127d82013-03-05 17:47:11 -08001303
Robert Carrc3574f72016-03-24 12:19:32 -07001304bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001305 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001306 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001307 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001308 return true;
1309}
1310
rongliuccd34842018-03-14 12:26:23 -07001311void Layer::setInfo(int32_t type, int32_t appId) {
David Sodman41fdfc92017-11-06 16:09:56 -08001312 mCurrentState.appId = appId;
1313 mCurrentState.type = type;
1314 mCurrentState.modified = true;
1315 setTransactionFlags(eTransactionNeeded);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001316}
1317
Mathias Agopian13127d82013-03-05 17:47:11 -08001318bool Layer::setLayerStack(uint32_t layerStack) {
David Sodman41fdfc92017-11-06 16:09:56 -08001319 if (mCurrentState.layerStack == layerStack) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001320 mCurrentState.sequence++;
1321 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001322 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001323 setTransactionFlags(eTransactionNeeded);
1324 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001325}
1326
Robert Carr1f0a16a2016-10-24 16:27:39 -07001327uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001328 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001329 if (p == nullptr) {
1330 return getDrawingState().layerStack;
1331 }
1332 return p->getLayerStack();
1333}
1334
David Sodman41fdfc92017-11-06 16:09:56 -08001335void Layer::deferTransactionUntil(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001336 mCurrentState.barrierLayer = barrierLayer;
Dan Stoza7dde5992015-05-22 09:51:44 -07001337 mCurrentState.frameNumber = frameNumber;
1338 // We don't set eTransactionNeeded, because just receiving a deferral
1339 // request without any other state updates shouldn't actually induce a delay
1340 mCurrentState.modified = true;
1341 pushPendingState();
Robert Carr0d480722017-01-10 16:42:54 -08001342 mCurrentState.barrierLayer = nullptr;
Dan Stoza792e5292016-02-11 11:43:58 -08001343 mCurrentState.frameNumber = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001344 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001345}
1346
David Sodman41fdfc92017-11-06 16:09:56 -08001347void Layer::deferTransactionUntil(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001348 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
1349 deferTransactionUntil(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001350}
1351
Dan Stozaee44edd2015-03-23 15:50:23 -07001352
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001353// ----------------------------------------------------------------------------
1354// pageflip handling...
1355// ----------------------------------------------------------------------------
1356
Robert Carr1f0a16a2016-10-24 16:27:39 -07001357bool Layer::isHiddenByPolicy() const {
1358 const Layer::State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001359 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001360 if (parent != nullptr && parent->isHiddenByPolicy()) {
1361 return true;
1362 }
1363 return s.flags & layer_state_t::eLayerHidden;
1364}
1365
David Sodman41fdfc92017-11-06 16:09:56 -08001366uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001367 // TODO: should we do something special if mSecure is set?
1368 if (mProtectedByApp) {
1369 // need a hardware-protected path to external video sink
1370 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001371 }
Riley Andrews03414a12014-07-01 14:22:59 -07001372 if (mPotentialCursor) {
1373 usage |= GraphicBuffer::USAGE_CURSOR;
1374 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001375 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001376 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001377}
1378
Mathias Agopian84300952012-11-21 16:02:13 -08001379void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001380 uint32_t orientation = 0;
1381 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001382 // The transform hint is used to improve performance, but we can
1383 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001384 // apply to all displays.
Mathias Agopian42977342012-08-05 00:40:46 -07001385 const Transform& planeTransform(hw->getTransform());
Mathias Agopian4fec8732012-06-29 14:12:52 -07001386 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07001387 if (orientation & Transform::ROT_INVALID) {
1388 orientation = 0;
1389 }
1390 }
David Sodmaneb085e02017-10-05 18:49:04 -07001391 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001392}
1393
Mathias Agopian13127d82013-03-05 17:47:11 -08001394// ----------------------------------------------------------------------------
1395// debugging
1396// ----------------------------------------------------------------------------
1397
Kalle Raitaa099a242017-01-11 11:17:29 -08001398LayerDebugInfo Layer::getLayerDebugInfo() const {
1399 LayerDebugInfo info;
1400 const Layer::State& ds = getDrawingState();
1401 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001402 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001403 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
1404 info.mType = String8(getTypeId());
1405 info.mTransparentRegion = ds.activeTransparentRegion;
1406 info.mVisibleRegion = visibleRegion;
1407 info.mSurfaceDamageRegion = surfaceDamageRegion;
1408 info.mLayerStack = getLayerStack();
1409 info.mX = ds.active.transform.tx();
1410 info.mY = ds.active.transform.ty();
1411 info.mZ = ds.z;
1412 info.mWidth = ds.active.w;
1413 info.mHeight = ds.active.h;
1414 info.mCrop = ds.crop;
1415 info.mFinalCrop = ds.finalCrop;
chaviw13fdc492017-06-27 12:40:18 -07001416 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001417 info.mFlags = ds.flags;
1418 info.mPixelFormat = getPixelFormat();
Chia-I Wu1b6bafc2018-05-22 12:03:00 -07001419 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Kalle Raitaa099a242017-01-11 11:17:29 -08001420 info.mMatrix[0][0] = ds.active.transform[0][0];
1421 info.mMatrix[0][1] = ds.active.transform[0][1];
1422 info.mMatrix[1][0] = ds.active.transform[1][0];
1423 info.mMatrix[1][1] = ds.active.transform[1][1];
1424 {
David Sodman0cc69182017-11-17 12:12:07 -08001425 sp<const GraphicBuffer> buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001426 if (buffer != 0) {
1427 info.mActiveBufferWidth = buffer->getWidth();
1428 info.mActiveBufferHeight = buffer->getHeight();
1429 info.mActiveBufferStride = buffer->getStride();
1430 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001431 } else {
1432 info.mActiveBufferWidth = 0;
1433 info.mActiveBufferHeight = 0;
1434 info.mActiveBufferStride = 0;
1435 info.mActiveBufferFormat = 0;
1436 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001437 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001438 info.mNumQueuedFrames = getQueuedFrameCount();
1439 info.mRefreshPending = isBufferLatched();
1440 info.mIsOpaque = isOpaque(ds);
1441 info.mContentDirty = contentDirty;
1442 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001443}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001444
Dan Stozae22aec72016-08-01 13:20:59 -07001445void Layer::miniDumpHeader(String8& result) {
1446 result.append("----------------------------------------");
1447 result.append("---------------------------------------\n");
1448 result.append(" Layer name\n");
1449 result.append(" Z | ");
1450 result.append(" Comp Type | ");
1451 result.append(" Disp Frame (LTRB) | ");
1452 result.append(" Source Crop (LTRB)\n");
1453 result.append("----------------------------------------");
1454 result.append("---------------------------------------\n");
1455}
1456
1457void Layer::miniDump(String8& result, int32_t hwcId) const {
David Sodman6f65f3e2017-11-03 14:28:09 -07001458 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stozae22aec72016-08-01 13:20:59 -07001459 return;
1460 }
1461
1462 String8 name;
1463 if (mName.length() > 77) {
1464 std::string shortened;
1465 shortened.append(mName.string(), 36);
1466 shortened.append("[...]");
1467 shortened.append(mName.string() + (mName.length() - 36), 36);
1468 name = shortened.c_str();
1469 } else {
1470 name = mName;
1471 }
1472
1473 result.appendFormat(" %s\n", name.string());
1474
1475 const Layer::State& layerState(getDrawingState());
David Sodman6f65f3e2017-11-03 14:28:09 -07001476 const LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers.at(hwcId);
Chia-I Wu1e043612018-03-01 09:45:09 -08001477 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
1478 result.appendFormat(" rel %6d | ", layerState.z);
1479 } else {
1480 result.appendFormat(" %10d | ", layerState.z);
1481 }
David Sodman41fdfc92017-11-06 16:09:56 -08001482 result.appendFormat("%10s | ", to_string(getCompositionType(hwcId)).c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001483 const Rect& frame = hwcInfo.displayFrame;
David Sodman41fdfc92017-11-06 16:09:56 -08001484 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Dan Stoza5a423ea2017-02-16 14:10:39 -08001485 const FloatRect& crop = hwcInfo.sourceCrop;
David Sodman41fdfc92017-11-06 16:09:56 -08001486 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 -07001487
1488 result.append("- - - - - - - - - - - - - - - - - - - - ");
1489 result.append("- - - - - - - - - - - - - - - - - - - -\n");
1490}
Dan Stozae22aec72016-08-01 13:20:59 -07001491
Svetoslavd85084b2014-03-20 10:28:31 -07001492void Layer::dumpFrameStats(String8& result) const {
1493 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001494}
1495
Svetoslavd85084b2014-03-20 10:28:31 -07001496void Layer::clearFrameStats() {
1497 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001498}
1499
Jamie Gennis6547ff42013-07-16 20:12:42 -07001500void Layer::logFrameStats() {
1501 mFrameTracker.logAndResetStats(mName);
1502}
1503
Svetoslavd85084b2014-03-20 10:28:31 -07001504void Layer::getFrameStats(FrameStats* outStats) const {
1505 mFrameTracker.getStats(outStats);
1506}
1507
Brian Andersond6927fb2016-07-23 23:37:30 -07001508void Layer::dumpFrameEvents(String8& result) {
David Sodman41fdfc92017-11-06 16:09:56 -08001509 result.appendFormat("- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001510 Mutex::Autolock lock(mFrameEventHistoryMutex);
1511 mFrameEventHistory.checkFencesForCompletion();
1512 mFrameEventHistory.dump(result);
1513}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001514
Brian Anderson5ea5e592016-12-01 16:54:33 -08001515void Layer::onDisconnect() {
1516 Mutex::Autolock lock(mFrameEventHistoryMutex);
1517 mFrameEventHistory.onDisconnect();
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001518 mTimeStats.onDisconnect(getName().c_str());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001519}
1520
Brian Anderson3890c392016-07-25 12:48:08 -07001521void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001522 FrameEventHistoryDelta* outDelta) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001523 if (newTimestamps) {
1524 mTimeStats.setPostTime(getName().c_str(), newTimestamps->frameNumber,
1525 newTimestamps->postedTime);
1526 }
1527
Brian Andersond6927fb2016-07-23 23:37:30 -07001528 Mutex::Autolock lock(mFrameEventHistoryMutex);
1529 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001530 // If there are any unsignaled fences in the aquire timeline at this
1531 // point, the previously queued frame hasn't been latched yet. Go ahead
1532 // and try to get the signal time here so the syscall is taken out of
1533 // the main thread's critical path.
1534 mAcquireTimeline.updateSignalTimes();
1535 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001536 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001537 mFrameEventHistory.addQueue(*newTimestamps);
1538 }
1539
Brian Anderson3890c392016-07-25 12:48:08 -07001540 if (outDelta) {
1541 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001542 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001543}
Dan Stozae77c7662016-05-13 11:37:28 -07001544
Chia-I Wu98f1c102017-05-30 14:54:08 -07001545size_t Layer::getChildrenCount() const {
1546 size_t count = 0;
1547 for (const sp<Layer>& child : mCurrentChildren) {
1548 count += 1 + child->getChildrenCount();
1549 }
1550 return count;
1551}
1552
Robert Carr1f0a16a2016-10-24 16:27:39 -07001553void Layer::addChild(const sp<Layer>& layer) {
1554 mCurrentChildren.add(layer);
1555 layer->setParent(this);
1556}
1557
1558ssize_t Layer::removeChild(const sp<Layer>& layer) {
1559 layer->setParent(nullptr);
1560 return mCurrentChildren.remove(layer);
1561}
1562
Robert Carr1db73f62016-12-21 12:58:51 -08001563bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1564 sp<Handle> handle = nullptr;
1565 sp<Layer> newParent = nullptr;
1566 if (newParentHandle == nullptr) {
1567 return false;
1568 }
1569 handle = static_cast<Handle*>(newParentHandle.get());
1570 newParent = handle->owner.promote();
1571 if (newParent == nullptr) {
1572 ALOGE("Unable to promote Layer handle");
1573 return false;
1574 }
1575
1576 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001577 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001578
1579 sp<Client> client(child->mClientRef.promote());
1580 if (client != nullptr) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001581 client->updateParent(newParent);
Robert Carr1db73f62016-12-21 12:58:51 -08001582 }
1583 }
1584 mCurrentChildren.clear();
1585
1586 return true;
1587}
1588
Robert Carr15eae092018-03-23 13:43:53 -07001589void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001590 for (const sp<Layer>& child : mDrawingChildren) {
1591 child->mDrawingParent = newParent;
1592 }
1593}
1594
chaviwf1961f72017-09-18 16:41:07 -07001595bool Layer::reparent(const sp<IBinder>& newParentHandle) {
1596 if (newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001597 return false;
1598 }
1599
1600 auto handle = static_cast<Handle*>(newParentHandle.get());
1601 sp<Layer> newParent = handle->owner.promote();
1602 if (newParent == nullptr) {
1603 ALOGE("Unable to promote Layer handle");
1604 return false;
1605 }
1606
chaviwf1961f72017-09-18 16:41:07 -07001607 sp<Layer> parent = getParent();
1608 if (parent != nullptr) {
1609 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001610 }
chaviwf1961f72017-09-18 16:41:07 -07001611 newParent->addChild(this);
chaviw06178942017-07-27 10:25:59 -07001612
chaviwf1961f72017-09-18 16:41:07 -07001613 sp<Client> client(mClientRef.promote());
chaviw06178942017-07-27 10:25:59 -07001614 sp<Client> newParentClient(newParent->mClientRef.promote());
1615
chaviwf1961f72017-09-18 16:41:07 -07001616 if (client != newParentClient) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001617 client->updateParent(newParent);
chaviw06178942017-07-27 10:25:59 -07001618 }
1619
chaviw06178942017-07-27 10:25:59 -07001620 return true;
1621}
1622
Robert Carr9524cb32017-02-13 11:32:32 -08001623bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001624 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001625 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001626 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001627 if (client != nullptr && parentClient != client) {
Robert Carr7f619b22017-11-06 12:56:35 -08001628 client->detachLayer(child.get());
1629 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001630 }
Robert Carr7f619b22017-11-06 12:56:35 -08001631 }
Robert Carr9524cb32017-02-13 11:32:32 -08001632
1633 return true;
1634}
1635
Chia-I Wu11481472018-05-04 10:43:19 -07001636bool Layer::isLegacyDataSpace() const {
1637 // return true when no higher bits are set
Chia-I Wu1b6bafc2018-05-22 12:03:00 -07001638 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001639 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001640}
1641
Robert Carr1f0a16a2016-10-24 16:27:39 -07001642void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001643 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001644}
1645
1646void Layer::clearSyncPoints() {
1647 for (const auto& child : mCurrentChildren) {
1648 child->clearSyncPoints();
1649 }
1650
1651 Mutex::Autolock lock(mLocalSyncPointMutex);
1652 for (auto& point : mLocalSyncPoints) {
1653 point->setFrameAvailable();
1654 }
1655 mLocalSyncPoints.clear();
1656}
1657
1658int32_t Layer::getZ() const {
1659 return mDrawingState.z;
1660}
1661
Robert Carr29abff82017-12-04 13:51:20 -08001662bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1663 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1664 const State& state = useDrawing ? mDrawingState : mCurrentState;
1665 return state.zOrderRelativeOf != nullptr;
1666}
1667
David Sodman41fdfc92017-11-06 16:09:56 -08001668__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001669 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001670 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1671 "makeTraversalList received invalid stateSet");
1672 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1673 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1674 const State& state = useDrawing ? mDrawingState : mCurrentState;
1675
Robert Carr29abff82017-12-04 13:51:20 -08001676 if (state.zOrderRelatives.size() == 0) {
1677 *outSkipRelativeZUsers = true;
1678 return children;
1679 }
1680
chaviw738df022018-05-31 16:11:27 -07001681 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001682 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001683 sp<Layer> strongRelative = weakRelative.promote();
1684 if (strongRelative != nullptr) {
1685 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001686 }
1687 }
1688
Dan Stoza412903f2017-04-27 13:42:17 -07001689 for (const sp<Layer>& child : children) {
Robert Carr503c7042017-09-27 15:06:08 -07001690 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1691 if (childState.zOrderRelativeOf != nullptr) {
1692 continue;
1693 }
Robert Carrdb66e622017-04-10 16:55:57 -07001694 traverse.add(child);
1695 }
1696
1697 return traverse;
1698}
1699
Robert Carr1f0a16a2016-10-24 16:27:39 -07001700/**
Robert Carrdb66e622017-04-10 16:55:57 -07001701 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001702 */
Dan Stoza412903f2017-04-27 13:42:17 -07001703void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001704 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1705 // produce a new list for traversing, including our relatives, and not including our children
1706 // who are relatives of another surface. In the case that there are no relative Z,
1707 // makeTraversalList returns our children directly to avoid significant overhead.
1708 // However in this case we need to take the responsibility for filtering children which
1709 // are relatives of another surface here.
1710 bool skipRelativeZUsers = false;
1711 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001712
Robert Carr1f0a16a2016-10-24 16:27:39 -07001713 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001714 for (; i < list.size(); i++) {
1715 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001716 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1717 continue;
1718 }
1719
Robert Carrdb66e622017-04-10 16:55:57 -07001720 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001721 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001722 }
Dan Stoza412903f2017-04-27 13:42:17 -07001723 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001724 }
Robert Carr29abff82017-12-04 13:51:20 -08001725
Dan Stoza412903f2017-04-27 13:42:17 -07001726 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001727 for (; i < list.size(); i++) {
1728 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001729
1730 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1731 continue;
1732 }
Dan Stoza412903f2017-04-27 13:42:17 -07001733 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001734 }
1735}
1736
1737/**
Robert Carrdb66e622017-04-10 16:55:57 -07001738 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001739 */
Dan Stoza412903f2017-04-27 13:42:17 -07001740void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1741 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001742 // See traverseInZOrder for documentation.
1743 bool skipRelativeZUsers = false;
1744 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001745
Robert Carr1f0a16a2016-10-24 16:27:39 -07001746 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001747 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001748 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001749
1750 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1751 continue;
1752 }
1753
Robert Carrdb66e622017-04-10 16:55:57 -07001754 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001755 break;
1756 }
Dan Stoza412903f2017-04-27 13:42:17 -07001757 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001758 }
Dan Stoza412903f2017-04-27 13:42:17 -07001759 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001760 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001761 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001762
1763 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1764 continue;
1765 }
1766
Dan Stoza412903f2017-04-27 13:42:17 -07001767 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001768 }
1769}
1770
chaviw4b129c22018-04-09 16:19:43 -07001771LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1772 const std::vector<Layer*>& layersInTree) {
1773 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1774 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001775 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1776 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
chaviw4b129c22018-04-09 16:19:43 -07001777 const State& state = useDrawing ? mDrawingState : mCurrentState;
1778
chaviw738df022018-05-31 16:11:27 -07001779 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001780 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1781 sp<Layer> strongRelative = weakRelative.promote();
1782 // Only add relative layers that are also descendents of the top most parent of the tree.
1783 // If a relative layer is not a descendent, then it should be ignored.
1784 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1785 traverse.add(strongRelative);
1786 }
1787 }
1788
1789 for (const sp<Layer>& child : children) {
1790 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1791 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1792 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1793 // the child here since it won't be added later as a relative.
1794 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1795 childState.zOrderRelativeOf.promote().get())) {
1796 continue;
1797 }
1798 traverse.add(child);
1799 }
1800
1801 return traverse;
1802}
1803
1804void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1805 LayerVector::StateSet stateSet,
1806 const LayerVector::Visitor& visitor) {
1807 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001808
1809 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001810 for (; i < list.size(); i++) {
1811 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001812 if (relative->getZ() >= 0) {
1813 break;
1814 }
chaviw4b129c22018-04-09 16:19:43 -07001815 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001816 }
chaviw4b129c22018-04-09 16:19:43 -07001817
chaviwa76b2712017-09-20 12:02:26 -07001818 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001819 for (; i < list.size(); i++) {
1820 const auto& relative = list[i];
1821 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001822 }
1823}
1824
chaviw4b129c22018-04-09 16:19:43 -07001825std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1826 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1827 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1828
1829 std::vector<Layer*> layersInTree = {this};
1830 for (size_t i = 0; i < children.size(); i++) {
1831 const auto& child = children[i];
1832 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1833 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1834 }
1835
1836 return layersInTree;
1837}
1838
1839void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1840 const LayerVector::Visitor& visitor) {
1841 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1842 std::sort(layersInTree.begin(), layersInTree.end());
1843 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1844}
1845
Robert Carr1f0a16a2016-10-24 16:27:39 -07001846Transform Layer::getTransform() const {
1847 Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001848 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001849 if (p != nullptr) {
1850 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07001851
1852 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
1853 // it isFixedSize) then there may be additional scaling not accounted
1854 // for in the transform. We need to mirror this scaling in child surfaces
1855 // or we will break the contract where WM can treat child surfaces as
1856 // pixels in the parent surface.
David Sodman0cc69182017-11-17 12:12:07 -08001857 if (p->isFixedSize() && p->getBE().compositionInfo.mBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07001858 int bufferWidth;
1859 int bufferHeight;
1860 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
David Sodman0cc69182017-11-17 12:12:07 -08001861 bufferWidth = p->getBE().compositionInfo.mBuffer->getWidth();
1862 bufferHeight = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001863 } else {
David Sodman0cc69182017-11-17 12:12:07 -08001864 bufferHeight = p->getBE().compositionInfo.mBuffer->getWidth();
1865 bufferWidth = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001866 }
David Sodman41fdfc92017-11-06 16:09:56 -08001867 float sx = p->getDrawingState().active.w / static_cast<float>(bufferWidth);
1868 float sy = p->getDrawingState().active.h / static_cast<float>(bufferHeight);
Robert Carr9b429f42017-04-17 14:56:57 -07001869 Transform extraParentScaling;
1870 extraParentScaling.set(sx, 0, 0, sy);
1871 t = t * extraParentScaling;
1872 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001873 }
1874 return t * getDrawingState().active.transform;
1875}
1876
chaviw13fdc492017-06-27 12:40:18 -07001877half Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001878 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07001879
chaviw13fdc492017-06-27 12:40:18 -07001880 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1881 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001882}
Robert Carr6452f122017-03-21 10:41:29 -07001883
chaviw13fdc492017-06-27 12:40:18 -07001884half4 Layer::getColor() const {
1885 const half4 color(getDrawingState().color);
1886 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001887}
Robert Carr6452f122017-03-21 10:41:29 -07001888
Robert Carr1f0a16a2016-10-24 16:27:39 -07001889void Layer::commitChildList() {
1890 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1891 const auto& child = mCurrentChildren[i];
1892 child->commitChildList();
1893 }
1894 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001895 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001896}
1897
chaviw1d044282017-09-27 12:19:28 -07001898void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
1899 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1900 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1901 const State& state = useDrawing ? mDrawingState : mCurrentState;
1902
1903 Transform requestedTransform = state.active.transform;
1904 Transform transform = getTransform();
1905
1906 layerInfo->set_id(sequence);
1907 layerInfo->set_name(getName().c_str());
1908 layerInfo->set_type(String8(getTypeId()));
1909
1910 for (const auto& child : children) {
1911 layerInfo->add_children(child->sequence);
1912 }
1913
1914 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1915 sp<Layer> strongRelative = weakRelative.promote();
1916 if (strongRelative != nullptr) {
1917 layerInfo->add_relatives(strongRelative->sequence);
1918 }
1919 }
1920
1921 LayerProtoHelper::writeToProto(state.activeTransparentRegion,
1922 layerInfo->mutable_transparent_region());
1923 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
1924 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
1925
1926 layerInfo->set_layer_stack(getLayerStack());
1927 layerInfo->set_z(state.z);
1928
1929 PositionProto* position = layerInfo->mutable_position();
1930 position->set_x(transform.tx());
1931 position->set_y(transform.ty());
1932
1933 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
1934 requestedPosition->set_x(requestedTransform.tx());
1935 requestedPosition->set_y(requestedTransform.ty());
1936
1937 SizeProto* size = layerInfo->mutable_size();
1938 size->set_w(state.active.w);
1939 size->set_h(state.active.h);
1940
1941 LayerProtoHelper::writeToProto(state.crop, layerInfo->mutable_crop());
1942 LayerProtoHelper::writeToProto(state.finalCrop, layerInfo->mutable_final_crop());
1943
1944 layerInfo->set_is_opaque(isOpaque(state));
1945 layerInfo->set_invalidate(contentDirty);
Chia-I Wu1b6bafc2018-05-22 12:03:00 -07001946
1947 // XXX (b/79210409) mCurrentDataSpace is not protected
1948 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
1949
chaviw1d044282017-09-27 12:19:28 -07001950 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1951 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
1952 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
1953 layerInfo->set_flags(state.flags);
1954
1955 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1956 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
1957
Jorim Jaggi8e0af362017-11-14 16:28:28 +01001958 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07001959 if (parent != nullptr) {
1960 layerInfo->set_parent(parent->sequence);
1961 }
1962
1963 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1964 if (zOrderRelativeOf != nullptr) {
1965 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1966 }
1967
Chia-I Wu1b6bafc2018-05-22 12:03:00 -07001968 // XXX getBE().compositionInfo.mBuffer is not protected
David Sodman0cc69182017-11-17 12:12:07 -08001969 auto buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001970 if (buffer != nullptr) {
1971 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
chaviw1d044282017-09-27 12:19:28 -07001972 }
1973
1974 layerInfo->set_queued_frames(getQueuedFrameCount());
1975 layerInfo->set_refresh_pending(isBufferLatched());
rongliuccd34842018-03-14 12:26:23 -07001976 layerInfo->set_window_type(state.type);
1977 layerInfo->set_app_id(state.appId);
chaviw9885bd42018-07-10 16:57:27 -07001978 layerInfo->set_curr_frame(mCurrentFrameNumber);
1979
1980 for (const auto& pendingState : mPendingStates) {
1981 auto barrierLayer = pendingState.barrierLayer.promote();
1982 if (barrierLayer != nullptr) {
1983 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
1984 barrierLayerProto->set_id(barrierLayer->sequence);
1985 barrierLayerProto->set_frame_number(pendingState.frameNumber);
1986 }
1987 }
chaviw1d044282017-09-27 12:19:28 -07001988}
1989
Yiwei Zhang068e31b2018-02-21 13:02:45 -08001990void Layer::writeToProto(LayerProto* layerInfo, int32_t hwcId) {
Peiyong Lin0874d2b2018-09-04 09:39:34 -07001991 if (!hasHwcLayer(hwcId)) {
1992 return;
1993 }
Yiwei Zhang068e31b2018-02-21 13:02:45 -08001994 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
1995
1996 const auto& hwcInfo = getBE().mHwcLayers.at(hwcId);
1997
1998 const Rect& frame = hwcInfo.displayFrame;
1999 LayerProtoHelper::writeToProto(frame, layerInfo->mutable_hwc_frame());
2000
2001 const FloatRect& crop = hwcInfo.sourceCrop;
2002 LayerProtoHelper::writeToProto(crop, layerInfo->mutable_hwc_crop());
2003
2004 const int32_t transform = static_cast<int32_t>(hwcInfo.transform);
2005 layerInfo->set_hwc_transform(transform);
Yiwei Zhang7c64f172018-03-07 14:52:28 -08002006
2007 const int32_t compositionType = static_cast<int32_t>(hwcInfo.compositionType);
2008 layerInfo->set_hwc_composition_type(compositionType);
2009
2010 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
2011 static_cast<BufferLayer*>(this)->isProtected()) {
2012 layerInfo->set_is_protected(true);
2013 } else {
2014 layerInfo->set_is_protected(false);
2015 }
Yiwei Zhang068e31b2018-02-21 13:02:45 -08002016}
2017
Mathias Agopian13127d82013-03-05 17:47:11 -08002018// ---------------------------------------------------------------------------
2019
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002020}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002021
2022#if defined(__gl_h_)
2023#error "don't include gl/gl.h in this file"
2024#endif
2025
2026#if defined(__gl2_h_)
2027#error "don't include gl2/gl2.h in this file"
2028#endif