blob: 1dc3f4c69341c6642f1d35e766665c386eeb1e63 [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 mFiltering(false),
92 mNeedsFiltering(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080093 mProtectedByApp(false),
Riley Andrews03414a12014-07-01 14:22:59 -070094 mClientRef(client),
Dan Stozaa4650a52015-05-12 12:56:16 -070095 mPotentialCursor(false),
96 mQueueItemLock(),
97 mQueueItemCondition(),
98 mQueueItems(),
Dan Stoza65476f32015-05-14 09:27:25 -070099 mLastFrameNumberReceived(0),
Robert Carr82364e32016-05-15 11:27:47 -0700100 mAutoRefresh(false),
chaviw738df022018-05-31 16:11:27 -0700101 mFreezeGeometryUpdates(false),
102 mCurrentChildren(LayerVector::StateSet::Current),
103 mDrawingChildren(LayerVector::StateSet::Drawing) {
Mathias Agopiana67932f2011-04-20 14:20:59 -0700104 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700105
106 uint32_t layerFlags = 0;
David Sodman41fdfc92017-11-06 16:09:56 -0800107 if (flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
108 if (flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
109 if (flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700110
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700111 mName = name;
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700112 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700113
114 mCurrentState.active.w = w;
115 mCurrentState.active.h = h;
David Sodman0c69cad2017-08-21 12:12:51 -0700116 mCurrentState.flags = layerFlags;
Robert Carr3dcabfa2016-03-01 18:36:58 -0800117 mCurrentState.active.transform.set(0, 0);
Robert Carrb5d3d262016-03-25 15:08:13 -0700118 mCurrentState.crop.makeInvalid();
119 mCurrentState.finalCrop.makeInvalid();
Robert Carr7bf247e2017-05-18 14:02:49 -0700120 mCurrentState.requestedFinalCrop = mCurrentState.finalCrop;
121 mCurrentState.requestedCrop = mCurrentState.crop;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700122 mCurrentState.z = 0;
chaviw13fdc492017-06-27 12:40:18 -0700123 mCurrentState.color.a = 1.0f;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700124 mCurrentState.layerStack = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700125 mCurrentState.sequence = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700126 mCurrentState.requested = mCurrentState.active;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500127 mCurrentState.appId = 0;
128 mCurrentState.type = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700129
130 // drawing state & current state are identical
131 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700132
Dan Stoza9e56aa02015-11-02 13:00:03 -0800133 const auto& hwc = flinger->getHwComposer();
134 const auto& activeConfig = hwc.getActiveConfig(HWC_DISPLAY_PRIMARY);
135 nsecs_t displayPeriod = activeConfig->getVsyncPeriod();
Jamie Gennis6547ff42013-07-16 20:12:42 -0700136 mFrameTracker.setDisplayRefreshPeriod(displayPeriod);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800137
138 CompositorTiming compositorTiming;
139 flinger->getCompositorTiming(&compositorTiming);
140 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jamie Gennise8696a42012-01-15 18:54:57 -0800141}
142
David Sodman41fdfc92017-11-06 16:09:56 -0800143void Layer::onFirstRef() {}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700144
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700145Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800146 sp<Client> c(mClientRef.promote());
147 if (c != 0) {
148 c->detachLayer(this);
149 }
150
151 for (auto& point : mRemoteSyncPoints) {
152 point->setTransactionApplied();
153 }
154 for (auto& point : mLocalSyncPoints) {
155 point->setFrameAvailable();
156 }
Jamie Gennis6547ff42013-07-16 20:12:42 -0700157 mFrameTracker.logAndResetStats(mName);
Mathias Agopian96f08192010-06-02 23:28:45 -0700158}
159
Mathias Agopian13127d82013-03-05 17:47:11 -0800160// ---------------------------------------------------------------------------
161// callbacks
162// ---------------------------------------------------------------------------
163
David Sodmaneb085e02017-10-05 18:49:04 -0700164/*
165 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
166 * Layer. So, the implementation is done in BufferLayer. When called on a
167 * ColorLayer object, it's essentially a NOP.
168 */
David Sodmaneb085e02017-10-05 18:49:04 -0700169void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800170
Chia-I Wuc6657022017-08-15 11:18:17 -0700171void Layer::onRemovedFromCurrentState() {
172 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
173
chaviw8b3871a2017-11-01 17:41:01 -0700174 mPendingRemoval = true;
175
Robert Carr5edb1ad2017-04-25 10:54:24 -0700176 if (mCurrentState.zOrderRelativeOf != nullptr) {
177 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
178 if (strongRelative != nullptr) {
179 strongRelative->removeZOrderRelative(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700180 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700181 }
182 mCurrentState.zOrderRelativeOf = nullptr;
183 }
184
Chia-I Wuc6657022017-08-15 11:18:17 -0700185 for (const auto& child : mCurrentChildren) {
186 child->onRemovedFromCurrentState();
187 }
188}
Chia-I Wu38512252017-05-17 14:36:16 -0700189
Chia-I Wuc6657022017-08-15 11:18:17 -0700190void Layer::onRemoved() {
191 // the layer is removed from SF mLayersPendingRemoval
David Sodmaneb085e02017-10-05 18:49:04 -0700192 abandon();
Chia-I Wuc6657022017-08-15 11:18:17 -0700193
Steven Thomasb02664d2017-07-26 18:48:28 -0700194 destroyAllHwcLayers();
Chia-I Wu38512252017-05-17 14:36:16 -0700195
Robert Carr1f0a16a2016-10-24 16:27:39 -0700196 for (const auto& child : mCurrentChildren) {
197 child->onRemoved();
198 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700199}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700200
Mathias Agopian13127d82013-03-05 17:47:11 -0800201// ---------------------------------------------------------------------------
202// set-up
203// ---------------------------------------------------------------------------
204
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700205const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800206 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207}
208
chaviw13fdc492017-06-27 12:40:18 -0700209bool Layer::getPremultipledAlpha() const {
210 return mPremultipliedAlpha;
211}
212
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700213sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800214 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700215 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800216}
217
218// ---------------------------------------------------------------------------
219// h/w composer set-up
220// ---------------------------------------------------------------------------
221
Steven Thomasb02664d2017-07-26 18:48:28 -0700222bool Layer::createHwcLayer(HWComposer* hwc, int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700223 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.count(hwcId) != 0,
David Sodman9eeae692017-11-02 10:53:32 -0700224 "Already have a layer for hwcId %d", hwcId);
Steven Thomasb02664d2017-07-26 18:48:28 -0700225 HWC2::Layer* layer = hwc->createLayer(hwcId);
226 if (!layer) {
227 return false;
228 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700229 LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers[hwcId];
Steven Thomasb02664d2017-07-26 18:48:28 -0700230 hwcInfo.hwc = hwc;
231 hwcInfo.layer = layer;
232 layer->setLayerDestroyedListener(
David Sodman6f65f3e2017-11-03 14:28:09 -0700233 [this, hwcId](HWC2::Layer* /*layer*/) { getBE().mHwcLayers.erase(hwcId); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700234 return true;
235}
236
Chia-I Wu83806892017-11-16 10:50:20 -0800237bool Layer::destroyHwcLayer(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700238 if (getBE().mHwcLayers.count(hwcId) == 0) {
Chia-I Wu83806892017-11-16 10:50:20 -0800239 return false;
Steven Thomasb02664d2017-07-26 18:48:28 -0700240 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700241 auto& hwcInfo = getBE().mHwcLayers[hwcId];
David Sodman41fdfc92017-11-06 16:09:56 -0800242 LOG_ALWAYS_FATAL_IF(hwcInfo.layer == nullptr, "Attempt to destroy null layer");
Steven Thomasb02664d2017-07-26 18:48:28 -0700243 LOG_ALWAYS_FATAL_IF(hwcInfo.hwc == nullptr, "Missing HWComposer");
244 hwcInfo.hwc->destroyLayer(hwcId, hwcInfo.layer);
245 // The layer destroyed listener should have cleared the entry from
246 // mHwcLayers. Verify that.
David Sodman6f65f3e2017-11-03 14:28:09 -0700247 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.count(hwcId) != 0,
248 "Stale layer entry in getBE().mHwcLayers");
Chia-I Wu83806892017-11-16 10:50:20 -0800249 return true;
Steven Thomasb02664d2017-07-26 18:48:28 -0700250}
251
252void Layer::destroyAllHwcLayers() {
David Sodman6f65f3e2017-11-03 14:28:09 -0700253 size_t numLayers = getBE().mHwcLayers.size();
Steven Thomasb02664d2017-07-26 18:48:28 -0700254 for (size_t i = 0; i < numLayers; ++i) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700255 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.empty(), "destroyAllHwcLayers failed");
256 destroyHwcLayer(getBE().mHwcLayers.begin()->first);
Steven Thomasb02664d2017-07-26 18:48:28 -0700257 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700258 LOG_ALWAYS_FATAL_IF(!getBE().mHwcLayers.empty(),
David Sodman41fdfc92017-11-06 16:09:56 -0800259 "All hardware composer layers should have been destroyed");
Steven Thomasb02664d2017-07-26 18:48:28 -0700260}
Steven Thomasb02664d2017-07-26 18:48:28 -0700261
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800262Rect Layer::getContentCrop() const {
263 // this is the crop rectangle that applies to the buffer
264 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700265 Rect crop;
266 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800267 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700268 crop = mCurrentCrop;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800269 } else if (getBE().compositionInfo.mBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800270 // otherwise we use the whole buffer
David Sodman0cc69182017-11-17 12:12:07 -0800271 crop = getBE().compositionInfo.mBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700272 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800273 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700274 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700275 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700276 return crop;
277}
278
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700279static Rect reduce(const Rect& win, const Region& exclude) {
280 if (CC_LIKELY(exclude.isEmpty())) {
281 return win;
282 }
283 if (exclude.isRect()) {
284 return win.reduce(exclude.getBounds());
285 }
286 return Region(win).subtract(exclude).getBounds();
287}
288
Dan Stoza80d61162017-12-20 15:57:52 -0800289static FloatRect reduce(const FloatRect& win, const Region& exclude) {
290 if (CC_LIKELY(exclude.isEmpty())) {
291 return win;
292 }
293 // Convert through Rect (by rounding) for lack of FloatRegion
294 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
295}
296
Robert Carr1f0a16a2016-10-24 16:27:39 -0700297Rect Layer::computeScreenBounds(bool reduceTransparentRegion) const {
298 const Layer::State& s(getDrawingState());
299 Rect win(s.active.w, s.active.h);
300
301 if (!s.crop.isEmpty()) {
302 win.intersect(s.crop, &win);
303 }
304
305 Transform t = getTransform();
306 win = t.transform(win);
307
Robert Carr41b08b52017-06-01 16:11:34 -0700308 if (!s.finalCrop.isEmpty()) {
309 win.intersect(s.finalCrop, &win);
310 }
311
Chia-I Wue41dbe62017-06-13 14:10:56 -0700312 const sp<Layer>& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700313 // Now we need to calculate the parent bounds, so we can clip ourselves to those.
314 // When calculating the parent bounds for purposes of clipping,
315 // we don't need to constrain the parent to its transparent region.
316 // The transparent region is an optimization based on the
317 // buffer contents of the layer, but does not affect the space allocated to
318 // it by policy, and thus children should be allowed to extend into the
319 // parent's transparent region. In fact one of the main uses, is to reduce
320 // buffer allocation size in cases where a child window sits behind a main window
321 // (by marking the hole in the parent window as a transparent region)
322 if (p != nullptr) {
323 Rect bounds = p->computeScreenBounds(false);
324 bounds.intersect(win, &win);
325 }
326
327 if (reduceTransparentRegion) {
328 auto const screenTransparentRegion = t.transform(s.activeTransparentRegion);
329 win = reduce(win, screenTransparentRegion);
330 }
331
332 return win;
333}
334
Dan Stoza80d61162017-12-20 15:57:52 -0800335FloatRect Layer::computeBounds() const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700336 const Layer::State& s(getDrawingState());
Michael Lentine6c925ed2014-09-26 17:55:01 -0700337 return computeBounds(s.activeTransparentRegion);
338}
339
Dan Stoza80d61162017-12-20 15:57:52 -0800340FloatRect Layer::computeBounds(const Region& activeTransparentRegion) const {
Michael Lentine6c925ed2014-09-26 17:55:01 -0700341 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800342 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700343
344 if (!s.crop.isEmpty()) {
345 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800346 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700347
348 Rect bounds = win;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700349 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700350 if (p != nullptr) {
Robert Carrde9ec442017-02-08 17:43:36 -0800351 // Look in computeScreenBounds recursive call for explanation of
352 // why we pass false here.
353 bounds = p->computeScreenBounds(false /* reduceTransparentRegion */);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700354 }
355
356 Transform t = getTransform();
Dan Stoza80d61162017-12-20 15:57:52 -0800357
358 FloatRect floatWin = win.toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700359 if (p != nullptr) {
Dan Stoza80d61162017-12-20 15:57:52 -0800360 floatWin = t.transform(floatWin);
361 floatWin = floatWin.intersect(bounds.toFloatRect());
362 floatWin = t.inverse().transform(floatWin);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700363 }
364
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700365 // subtract the transparent region and snap to the bounds
Dan Stoza80d61162017-12-20 15:57:52 -0800366 return reduce(floatWin, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800367}
368
Robert Carr1f0a16a2016-10-24 16:27:39 -0700369Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& hw) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700370 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800371 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700372 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800373
374 // apply the projection's clipping to the window crop in
375 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700376 // if there are no window scaling involved, this operation will map to full
377 // pixels in the buffer.
378 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
379 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700380
381 Rect activeCrop(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700382 if (!s.crop.isEmpty()) {
Chia-I Wudf7867f2017-07-20 14:24:37 -0700383 activeCrop.intersect(s.crop, &activeCrop);
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700384 }
385
Robert Carr1f0a16a2016-10-24 16:27:39 -0700386 Transform t = getTransform();
387 activeCrop = t.transform(activeCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000388 if (!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
389 activeCrop.clear();
390 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700391 if (!s.finalCrop.isEmpty()) {
David Sodman41fdfc92017-11-06 16:09:56 -0800392 if (!activeCrop.intersect(s.finalCrop, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000393 activeCrop.clear();
394 }
395 }
chaviwb1154d12017-10-31 14:15:36 -0700396
397 const auto& p = mDrawingParent.promote();
398 if (p != nullptr) {
399 auto parentCrop = p->computeInitialCrop(hw);
400 activeCrop.intersect(parentCrop, &activeCrop);
401 }
402
Robert Carr1f0a16a2016-10-24 16:27:39 -0700403 return activeCrop;
404}
405
Dan Stoza5a423ea2017-02-16 14:10:39 -0800406FloatRect Layer::computeCrop(const sp<const DisplayDevice>& hw) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700407 // the content crop is the area of the content that gets scaled to the
408 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800409 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700410
411 // In addition there is a WM-specified crop we pull from our drawing state.
412 const State& s(getDrawingState());
413
414 // Screen space to make reduction to parent crop clearer.
415 Rect activeCrop = computeInitialCrop(hw);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700416 Transform t = getTransform();
417 // Back to layer space to work with the content crop.
418 activeCrop = t.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800419
Michael Lentine28ea2172014-11-19 18:32:37 -0800420 // This needs to be here as transform.transform(Rect) computes the
421 // transformed rect and then takes the bounding box of the result before
422 // returning. This means
423 // transform.inverse().transform(transform.transform(Rect)) != Rect
424 // in which case we need to make sure the final rect is clipped to the
425 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000426 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
427 activeCrop.clear();
428 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800429
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700430 // subtract the transparent region and snap to the bounds
431 activeCrop = reduce(activeCrop, s.activeTransparentRegion);
432
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000433 // Transform the window crop to match the buffer coordinate system,
434 // which means using the inverse of the current transform set on the
435 // SurfaceFlingerConsumer.
436 uint32_t invTransform = mCurrentTransform;
Robert Carrcae605c2017-03-29 12:10:31 -0700437 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000438 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700439 * the code below applies the primary display's inverse transform to the
440 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000441 */
David Sodman41fdfc92017-11-06 16:09:56 -0800442 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000443 // calculate the inverse transform
444 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800445 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800446 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000447 // and apply to the current transform
David Sodman41fdfc92017-11-06 16:09:56 -0800448 invTransform = (Transform(invTransformOrient) * Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800449 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000450
451 int winWidth = s.active.w;
452 int winHeight = s.active.h;
453 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
454 // If the activeCrop has been rotate the ends are rotated but not
455 // the space itself so when transforming ends back we can't rely on
456 // a modification of the axes of rotation. To account for this we
457 // need to reorient the inverse rotation in terms of the current
458 // axes of rotation.
459 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
460 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
461 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800462 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000463 }
464 winWidth = s.active.h;
465 winHeight = s.active.w;
466 }
David Sodman41fdfc92017-11-06 16:09:56 -0800467 const Rect winCrop = activeCrop.transform(invTransform, s.active.w, s.active.h);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000468
469 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800470 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000471 float yScale = crop.getHeight() / float(winHeight);
472
David Sodman41fdfc92017-11-06 16:09:56 -0800473 float insetL = winCrop.left * xScale;
474 float insetT = winCrop.top * yScale;
475 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000476 float insetB = (winHeight - winCrop.bottom) * yScale;
477
David Sodman41fdfc92017-11-06 16:09:56 -0800478 crop.left += insetL;
479 crop.top += insetT;
480 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000481 crop.bottom -= insetB;
482
Mathias Agopian13127d82013-03-05 17:47:11 -0800483 return crop;
484}
485
Robert Carrae060832016-11-28 10:51:00 -0800486void Layer::setGeometry(const sp<const DisplayDevice>& displayDevice, uint32_t z)
Mathias Agopiana350ff92010-08-10 17:14:02 -0700487{
Dan Stoza9e56aa02015-11-02 13:00:03 -0800488 const auto hwcId = displayDevice->getHwcDisplayId();
Peiyong Lin0874d2b2018-09-04 09:39:34 -0700489 if (!hasHwcLayer(hwcId)) {
490 return;
491 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700492 auto& hwcInfo = getBE().mHwcLayers[hwcId];
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700493
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700494 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800495 hwcInfo.forceClientComposition = false;
496
497 if (isSecure() && !displayDevice->isSecure()) {
498 hwcInfo.forceClientComposition = true;
499 }
500
501 auto& hwcLayer = hwcInfo.layer;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700502
Mathias Agopian13127d82013-03-05 17:47:11 -0800503 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700504 const State& s(getDrawingState());
David Revemanecf0fa52017-03-03 11:32:44 -0500505 auto blendMode = HWC2::BlendMode::None;
Robert Carr6452f122017-03-21 10:41:29 -0700506 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800507 blendMode =
508 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800509 }
David Revemanecf0fa52017-03-03 11:32:44 -0500510 auto error = hwcLayer->setBlendMode(blendMode);
David Sodman41fdfc92017-11-06 16:09:56 -0800511 ALOGE_IF(error != HWC2::Error::None,
512 "[%s] Failed to set blend mode %s:"
513 " %s (%d)",
514 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
515 static_cast<int32_t>(error));
Mathias Agopian13127d82013-03-05 17:47:11 -0800516
517 // apply the layer's transform, followed by the display's global transform
518 // here we're guaranteed that the layer's transform preserves rects
Michael Lentine6c925ed2014-09-26 17:55:01 -0700519 Region activeTransparentRegion(s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700520 Transform t = getTransform();
Robert Carrb5d3d262016-03-25 15:08:13 -0700521 if (!s.crop.isEmpty()) {
522 Rect activeCrop(s.crop);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700523 activeCrop = t.transform(activeCrop);
David Sodman41fdfc92017-11-06 16:09:56 -0800524 if (!activeCrop.intersect(displayDevice->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000525 activeCrop.clear();
526 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700527 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800528 // This needs to be here as transform.transform(Rect) computes the
529 // transformed rect and then takes the bounding box of the result before
530 // returning. This means
531 // transform.inverse().transform(transform.transform(Rect)) != Rect
532 // in which case we need to make sure the final rect is clipped to the
533 // display bounds.
David Sodman41fdfc92017-11-06 16:09:56 -0800534 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000535 activeCrop.clear();
536 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700537 // mark regions outside the crop as transparent
538 activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top));
David Sodman41fdfc92017-11-06 16:09:56 -0800539 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom, s.active.w, s.active.h));
540 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
541 activeTransparentRegion.orSelf(
542 Rect(activeCrop.right, activeCrop.top, s.active.w, activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700543 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700544
Dan Stoza80d61162017-12-20 15:57:52 -0800545 // computeBounds returns a FloatRect to provide more accuracy during the
546 // transformation. We then round upon constructing 'frame'.
547 Rect frame{t.transform(computeBounds(activeTransparentRegion))};
Robert Carrb5d3d262016-03-25 15:08:13 -0700548 if (!s.finalCrop.isEmpty()) {
David Sodman41fdfc92017-11-06 16:09:56 -0800549 if (!frame.intersect(s.finalCrop, &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000550 frame.clear();
551 }
552 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000553 if (!frame.intersect(displayDevice->getViewport(), &frame)) {
554 frame.clear();
555 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800556 const Transform& tr(displayDevice->getTransform());
557 Rect transformedFrame = tr.transform(frame);
David Revemanecf0fa52017-03-03 11:32:44 -0500558 error = hwcLayer->setDisplayFrame(transformedFrame);
Dan Stozae22aec72016-08-01 13:20:59 -0700559 if (error != HWC2::Error::None) {
David Sodman41fdfc92017-11-06 16:09:56 -0800560 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
561 transformedFrame.left, transformedFrame.top, transformedFrame.right,
562 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stozae22aec72016-08-01 13:20:59 -0700563 } else {
564 hwcInfo.displayFrame = transformedFrame;
565 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800566
Dan Stoza5a423ea2017-02-16 14:10:39 -0800567 FloatRect sourceCrop = computeCrop(displayDevice);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800568 error = hwcLayer->setSourceCrop(sourceCrop);
Dan Stozae22aec72016-08-01 13:20:59 -0700569 if (error != HWC2::Error::None) {
570 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
David Sodman41fdfc92017-11-06 16:09:56 -0800571 "%s (%d)",
572 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
573 to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stozae22aec72016-08-01 13:20:59 -0700574 } else {
575 hwcInfo.sourceCrop = sourceCrop;
576 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800577
chaviw13fdc492017-06-27 12:40:18 -0700578 float alpha = static_cast<float>(getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -0700579 error = hwcLayer->setPlaneAlpha(alpha);
David Sodman41fdfc92017-11-06 16:09:56 -0800580 ALOGE_IF(error != HWC2::Error::None,
581 "[%s] Failed to set plane alpha %.3f: "
582 "%s (%d)",
583 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800584
Robert Carrae060832016-11-28 10:51:00 -0800585 error = hwcLayer->setZOrder(z);
David Sodman41fdfc92017-11-06 16:09:56 -0800586 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
587 to_string(error).c_str(), static_cast<int32_t>(error));
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500588
Albert Chaulk2a589632017-05-04 16:59:44 -0400589 int type = s.type;
590 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700591 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400592 if (parent.get()) {
593 auto& parentState = parent->getDrawingState();
rongliuccd34842018-03-14 12:26:23 -0700594 if (parentState.type >= 0 || parentState.appId >= 0) {
595 type = parentState.type;
596 appId = parentState.appId;
597 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400598 }
599
600 error = hwcLayer->setInfo(type, appId);
David Sodman41fdfc92017-11-06 16:09:56 -0800601 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
602 static_cast<int32_t>(error));
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800603
Mathias Agopian29a367b2011-07-12 14:51:45 -0700604 /*
605 * Transformations are applied in this order:
606 * 1) buffer orientation/flip/mirror
607 * 2) state transformation (window manager)
608 * 3) layer orientation (screen orientation)
609 * (NOTE: the matrices are multiplied in reverse order)
610 */
611
612 const Transform bufferOrientation(mCurrentTransform);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700613 Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700614
Robert Carrcae605c2017-03-29 12:10:31 -0700615 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700616 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700617 * the code below applies the primary display's inverse transform to the
618 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700619 */
David Sodman41fdfc92017-11-06 16:09:56 -0800620 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700621 // calculate the inverse transform
622 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800623 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700624 }
Robert Carrcae605c2017-03-29 12:10:31 -0700625
626 /*
627 * Here we cancel out the orientation component of the WM transform.
628 * The scaling and translate components are already included in our bounds
629 * computation so it's enough to just omit it in the composition.
630 * See comment in onDraw with ref to b/36727915 for why.
631 */
632 transform = Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700633 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700634
635 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800636 const uint32_t orientation = transform.getOrientation();
Jorim Jaggif3bd94a2018-03-27 15:38:03 +0200637 if (orientation & Transform::ROT_INVALID) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800638 // we can only handle simple transformation
639 hwcInfo.forceClientComposition = true;
640 } else {
641 auto transform = static_cast<HWC2::Transform>(orientation);
Yiwei Zhang068e31b2018-02-21 13:02:45 -0800642 hwcInfo.transform = transform;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800643 auto error = hwcLayer->setTransform(transform);
David Sodman41fdfc92017-11-06 16:09:56 -0800644 ALOGE_IF(error != HWC2::Error::None,
645 "[%s] Failed to set transform %s: "
646 "%s (%d)",
647 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
648 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800649 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700650}
651
Dan Stoza9e56aa02015-11-02 13:00:03 -0800652void Layer::forceClientComposition(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700653 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800654 ALOGE("forceClientComposition: no HWC layer found (%d)", hwcId);
655 return;
656 }
657
David Sodman6f65f3e2017-11-03 14:28:09 -0700658 getBE().mHwcLayers[hwcId].forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800659}
Dan Stozaee44edd2015-03-23 15:50:23 -0700660
chaviwc9232ed2017-11-14 15:31:15 -0800661bool Layer::getForceClientComposition(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700662 if (getBE().mHwcLayers.count(hwcId) == 0) {
chaviwc9232ed2017-11-14 15:31:15 -0800663 ALOGE("getForceClientComposition: no HWC layer found (%d)", hwcId);
664 return false;
665 }
666
David Sodman6f65f3e2017-11-03 14:28:09 -0700667 return getBE().mHwcLayers[hwcId].forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800668}
669
Dan Stoza9e56aa02015-11-02 13:00:03 -0800670void Layer::updateCursorPosition(const sp<const DisplayDevice>& displayDevice) {
671 auto hwcId = displayDevice->getHwcDisplayId();
David Sodman6f65f3e2017-11-03 14:28:09 -0700672 if (getBE().mHwcLayers.count(hwcId) == 0 ||
David Sodman9eeae692017-11-02 10:53:32 -0700673 getCompositionType(hwcId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800674 return;
675 }
676
677 // This gives us only the "orientation" component of the transform
678 const State& s(getCurrentState());
679
680 // Apply the layer's transform, followed by the display's global transform
681 // Here we're guaranteed that the layer's transform preserves rects
682 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700683 if (!s.crop.isEmpty()) {
684 win.intersect(s.crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800685 }
686 // Subtract the transparent region and snap to the bounds
687 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700688 Rect frame(getTransform().transform(bounds));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800689 frame.intersect(displayDevice->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700690 if (!s.finalCrop.isEmpty()) {
691 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000692 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800693 auto& displayTransform(displayDevice->getTransform());
694 auto position = displayTransform.transform(frame);
695
David Sodman6f65f3e2017-11-03 14:28:09 -0700696 auto error = getBE().mHwcLayers[hwcId].layer->setCursorPosition(position.left,
David Sodman9eeae692017-11-02 10:53:32 -0700697 position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800698 ALOGE_IF(error != HWC2::Error::None,
699 "[%s] Failed to set cursor position "
700 "to (%d, %d): %s (%d)",
701 mName.string(), position.left, position.top, to_string(error).c_str(),
702 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800703}
Riley Andrews03414a12014-07-01 14:22:59 -0700704
Mathias Agopian13127d82013-03-05 17:47:11 -0800705// ---------------------------------------------------------------------------
706// drawing...
707// ---------------------------------------------------------------------------
708
chaviwa76b2712017-09-20 12:02:26 -0700709void Layer::draw(const RenderArea& renderArea, const Region& clip) const {
710 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800711}
712
chaviwa76b2712017-09-20 12:02:26 -0700713void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) const {
714 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800715}
716
chaviwa76b2712017-09-20 12:02:26 -0700717void Layer::draw(const RenderArea& renderArea) const {
718 onDraw(renderArea, Region(renderArea.getBounds()), false);
Dan Stozac7014012014-02-14 15:03:43 -0800719}
720
David Sodman41fdfc92017-11-06 16:09:56 -0800721void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
722 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800723 auto& engine(mFlinger->getRenderEngine());
David Sodman9eeae692017-11-02 10:53:32 -0700724 computeGeometry(renderArea, getBE().mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700725 engine.setupFillWithColor(red, green, blue, alpha);
David Sodman9eeae692017-11-02 10:53:32 -0700726 engine.drawMesh(getBE().mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800727}
728
chaviwa76b2712017-09-20 12:02:26 -0700729void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
David Sodman41fdfc92017-11-06 16:09:56 -0800730 clearWithOpenGL(renderArea, 0, 0, 0, 0);
Mathias Agopian13127d82013-03-05 17:47:11 -0800731}
732
David Sodman41fdfc92017-11-06 16:09:56 -0800733void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type, bool callIntoHwc) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700734 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800735 ALOGE("setCompositionType called without a valid HWC layer");
736 return;
737 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700738 auto& hwcInfo = getBE().mHwcLayers[hwcId];
Dan Stoza9e56aa02015-11-02 13:00:03 -0800739 auto& hwcLayer = hwcInfo.layer;
David Sodman41fdfc92017-11-06 16:09:56 -0800740 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(), to_string(type).c_str(),
741 static_cast<int>(callIntoHwc));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800742 if (hwcInfo.compositionType != type) {
743 ALOGV(" actually setting");
744 hwcInfo.compositionType = type;
745 if (callIntoHwc) {
746 auto error = hwcLayer->setCompositionType(type);
David Sodman41fdfc92017-11-06 16:09:56 -0800747 ALOGE_IF(error != HWC2::Error::None,
748 "[%s] Failed to set "
749 "composition type %s: %s (%d)",
750 mName.string(), to_string(type).c_str(), to_string(error).c_str(),
751 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800752 }
753 }
754}
755
756HWC2::Composition Layer::getCompositionType(int32_t hwcId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -0700757 if (hwcId == DisplayDevice::DISPLAY_ID_INVALID) {
758 // If we're querying the composition type for a display that does not
759 // have a HWC counterpart, then it will always be Client
760 return HWC2::Composition::Client;
761 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700762 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700763 ALOGE("getCompositionType called with an invalid HWC layer");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800764 return HWC2::Composition::Invalid;
765 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700766 return getBE().mHwcLayers.at(hwcId).compositionType;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800767}
768
769void Layer::setClearClientTarget(int32_t hwcId, bool clear) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700770 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800771 ALOGE("setClearClientTarget called without a valid HWC layer");
772 return;
773 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700774 getBE().mHwcLayers[hwcId].clearClientTarget = clear;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800775}
776
777bool Layer::getClearClientTarget(int32_t hwcId) const {
David Sodman6f65f3e2017-11-03 14:28:09 -0700778 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800779 ALOGE("getClearClientTarget called without a valid HWC layer");
780 return false;
781 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700782 return getBE().mHwcLayers.at(hwcId).clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800783}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800784
Dan Stozacac35382016-01-27 12:21:06 -0800785bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
786 if (point->getFrameNumber() <= mCurrentFrameNumber) {
787 // Don't bother with a SyncPoint, since we've already latched the
788 // relevant frame
789 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700790 }
791
Dan Stozacac35382016-01-27 12:21:06 -0800792 Mutex::Autolock lock(mLocalSyncPointMutex);
793 mLocalSyncPoints.push_back(point);
794 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700795}
796
Mathias Agopian13127d82013-03-05 17:47:11 -0800797void Layer::setFiltering(bool filtering) {
798 mFiltering = filtering;
799}
800
801bool Layer::getFiltering() const {
802 return mFiltering;
803}
804
Mathias Agopian13127d82013-03-05 17:47:11 -0800805// ----------------------------------------------------------------------------
806// local state
807// ----------------------------------------------------------------------------
808
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000809static void boundPoint(vec2* point, const Rect& crop) {
810 if (point->x < crop.left) {
811 point->x = crop.left;
812 }
813 if (point->x > crop.right) {
814 point->x = crop.right;
815 }
816 if (point->y < crop.top) {
817 point->y = crop.top;
818 }
819 if (point->y > crop.bottom) {
820 point->y = crop.bottom;
821 }
822}
823
chaviwa76b2712017-09-20 12:02:26 -0700824void Layer::computeGeometry(const RenderArea& renderArea, Mesh& mesh,
825 bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700826 const Layer::State& s(getDrawingState());
chaviwa76b2712017-09-20 12:02:26 -0700827 const Transform renderAreaTransform(renderArea.getTransform());
828 const uint32_t height = renderArea.getHeight();
Dan Stoza80d61162017-12-20 15:57:52 -0800829 FloatRect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700830
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000831 vec2 lt = vec2(win.left, win.top);
832 vec2 lb = vec2(win.left, win.bottom);
833 vec2 rb = vec2(win.right, win.bottom);
834 vec2 rt = vec2(win.right, win.top);
835
Robert Carr1f0a16a2016-10-24 16:27:39 -0700836 Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000837 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700838 lt = layerTransform.transform(lt);
839 lb = layerTransform.transform(lb);
840 rb = layerTransform.transform(rb);
841 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000842 }
843
Robert Carrb5d3d262016-03-25 15:08:13 -0700844 if (!s.finalCrop.isEmpty()) {
845 boundPoint(&lt, s.finalCrop);
846 boundPoint(&lb, s.finalCrop);
847 boundPoint(&rb, s.finalCrop);
848 boundPoint(&rt, s.finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000849 }
850
Mathias Agopianff2ed702013-09-01 21:36:12 -0700851 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700852 position[0] = renderAreaTransform.transform(lt);
853 position[1] = renderAreaTransform.transform(lb);
854 position[2] = renderAreaTransform.transform(rb);
855 position[3] = renderAreaTransform.transform(rt);
David Sodman41fdfc92017-11-06 16:09:56 -0800856 for (size_t i = 0; i < 4; i++) {
chaviwa76b2712017-09-20 12:02:26 -0700857 position[i].y = height - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -0800858 }
859}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800860
David Sodman41fdfc92017-11-06 16:09:56 -0800861bool Layer::isSecure() const {
Dan Stoza23116082015-06-18 14:58:39 -0700862 const Layer::State& s(mDrawingState);
863 return (s.flags & layer_state_t::eLayerSecure);
864}
865
Mathias Agopian13127d82013-03-05 17:47:11 -0800866void Layer::setVisibleRegion(const Region& visibleRegion) {
867 // always called from main thread
868 this->visibleRegion = visibleRegion;
869}
870
871void Layer::setCoveredRegion(const Region& coveredRegion) {
872 // always called from main thread
873 this->coveredRegion = coveredRegion;
874}
875
David Sodman41fdfc92017-11-06 16:09:56 -0800876void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800877 // always called from main thread
878 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
879}
880
Robert Carre5f4f692018-01-12 13:12:28 -0800881void Layer::clearVisibilityRegions() {
882 visibleRegion.clear();
883 visibleNonTransparentRegion.clear();
884 coveredRegion.clear();
885}
886
Mathias Agopian13127d82013-03-05 17:47:11 -0800887// ----------------------------------------------------------------------------
888// transaction
889// ----------------------------------------------------------------------------
890
Dan Stoza7dde5992015-05-22 09:51:44 -0700891void Layer::pushPendingState() {
892 if (!mCurrentState.modified) {
893 return;
894 }
895
Dan Stoza7dde5992015-05-22 09:51:44 -0700896 // If this transaction is waiting on the receipt of a frame, generate a sync
897 // point and send it to the remote layer.
Robert Carr0d480722017-01-10 16:42:54 -0800898 if (mCurrentState.barrierLayer != nullptr) {
899 sp<Layer> barrierLayer = mCurrentState.barrierLayer.promote();
900 if (barrierLayer == nullptr) {
901 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700902 // If we can't promote the layer we are intended to wait on,
903 // then it is expired or otherwise invalid. Allow this transaction
904 // to be applied as per normal (no synchronization).
Robert Carr0d480722017-01-10 16:42:54 -0800905 mCurrentState.barrierLayer = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800906 } else {
David Sodman41fdfc92017-11-06 16:09:56 -0800907 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber);
Robert Carr0d480722017-01-10 16:42:54 -0800908 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800909 mRemoteSyncPoints.push_back(std::move(syncPoint));
910 } else {
911 // We already missed the frame we're supposed to synchronize
912 // on, so go ahead and apply the state update
Robert Carr0d480722017-01-10 16:42:54 -0800913 mCurrentState.barrierLayer = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800914 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700915 }
916
Dan Stoza7dde5992015-05-22 09:51:44 -0700917 // Wake us up to check if the frame has been received
918 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700919 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700920 }
921 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700922 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700923}
924
Pablo Ceballos05289c22016-04-14 15:49:55 -0700925void Layer::popPendingState(State* stateToCommit) {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700926 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700927
928 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700929 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700930}
931
Pablo Ceballos05289c22016-04-14 15:49:55 -0700932bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700933 bool stateUpdateAvailable = false;
934 while (!mPendingStates.empty()) {
Robert Carr0d480722017-01-10 16:42:54 -0800935 if (mPendingStates[0].barrierLayer != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700936 if (mRemoteSyncPoints.empty()) {
937 // If we don't have a sync point for this, apply it anyway. It
938 // will be visually wrong, but it should keep us from getting
939 // into too much trouble.
940 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700941 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700942 stateUpdateAvailable = true;
943 continue;
944 }
945
David Sodman41fdfc92017-11-06 16:09:56 -0800946 if (mRemoteSyncPoints.front()->getFrameNumber() != mPendingStates[0].frameNumber) {
947 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800948
949 // Signal our end of the sync point and then dispose of it
950 mRemoteSyncPoints.front()->setTransactionApplied();
951 mRemoteSyncPoints.pop_front();
952 continue;
953 }
954
Dan Stoza7dde5992015-05-22 09:51:44 -0700955 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
956 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700957 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700958 stateUpdateAvailable = true;
959
960 // Signal our end of the sync point and then dispose of it
961 mRemoteSyncPoints.front()->setTransactionApplied();
962 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800963 } else {
964 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700965 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700966 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700967 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700968 stateUpdateAvailable = true;
969 }
970 }
971
972 // If we still have pending updates, wake SurfaceFlinger back up and point
973 // it at this layer so we can process them
974 if (!mPendingStates.empty()) {
975 setTransactionFlags(eTransactionNeeded);
976 mFlinger->setTransactionFlags(eTraversalNeeded);
977 }
978
979 mCurrentState.modified = false;
980 return stateUpdateAvailable;
981}
982
Mathias Agopian13127d82013-03-05 17:47:11 -0800983uint32_t Layer::doTransaction(uint32_t flags) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800984 ATRACE_CALL();
985
Dan Stoza7dde5992015-05-22 09:51:44 -0700986 pushPendingState();
Pablo Ceballos05289c22016-04-14 15:49:55 -0700987 Layer::State c = getCurrentState();
988 if (!applyPendingStates(&c)) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700989 return 0;
990 }
991
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700992 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800993
David Sodman41fdfc92017-11-06 16:09:56 -0800994 const bool sizeChanged = (c.requested.w != s.requested.w) || (c.requested.h != s.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700995
David Sodmaneb085e02017-10-05 18:49:04 -0700996 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700997 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000998 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800999 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
1000 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1001 " requested={ wh={%4u,%4u} }}\n"
1002 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1003 " requested={ wh={%4u,%4u} }}\n",
David Sodman9eeae692017-11-02 10:53:32 -07001004 this, getName().string(), mCurrentTransform,
1005 getEffectiveScalingMode(), c.active.w, c.active.h, c.crop.left, c.crop.top,
1006 c.crop.right, c.crop.bottom, c.crop.getWidth(), c.crop.getHeight(), c.requested.w,
1007 c.requested.h, s.active.w, s.active.h, s.crop.left, s.crop.top, s.crop.right,
1008 s.crop.bottom, s.crop.getWidth(), s.crop.getHeight(), s.requested.w,
1009 s.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001010
Jamie Gennis2a0d5b62011-09-26 16:54:44 -07001011 // record the new size, form this point on, when the client request
1012 // a buffer, it'll get the new size.
David Sodmaneb085e02017-10-05 18:49:04 -07001013 setDefaultBufferSize(c.requested.w, c.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001014 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001015
Robert Carre392b552017-09-19 12:16:05 -07001016 // Don't let Layer::doTransaction update the drawing state
1017 // if we have a pending resize, unless we are in fixed-size mode.
1018 // the drawing state will be updated only once we receive a buffer
1019 // with the correct size.
1020 //
1021 // In particular, we want to make sure the clip (which is part
1022 // of the geometry state) is latched together with the size but is
1023 // latched immediately when no resizing is involved.
1024 //
1025 // If a sideband stream is attached, however, we want to skip this
1026 // optimization so that transactions aren't missed when a buffer
1027 // never arrives
1028 //
1029 // In the case that we don't have a buffer we ignore other factors
1030 // and avoid entering the resizePending state. At a high level the
1031 // resizePending state is to avoid applying the state of the new buffer
1032 // to the old buffer. However in the state where we don't have an old buffer
1033 // there is no such concern but we may still be being used as a parent layer.
David Sodman41fdfc92017-11-06 16:09:56 -08001034 const bool resizePending = ((c.requested.w != c.active.w) || (c.requested.h != c.active.h)) &&
David Sodman0cc69182017-11-17 12:12:07 -08001035 (getBE().compositionInfo.mBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001036 if (!isFixedSize()) {
David Sodman0cc69182017-11-17 12:12:07 -08001037 if (resizePending && getBE().compositionInfo.hwc.sidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001038 flags |= eDontUpdateGeometryState;
1039 }
1040 }
1041
Robert Carr7bf247e2017-05-18 14:02:49 -07001042 // Here we apply various requested geometry states, depending on our
1043 // latching configuration. See Layer.h for a detailed discussion of
1044 // how geometry latching is controlled.
1045 if (!(flags & eDontUpdateGeometryState)) {
Pablo Ceballos7d052572016-06-02 17:46:05 -07001046 Layer::State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001047
1048 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1049 // mode, which causes attributes which normally latch regardless of scaling mode,
1050 // to be delayed. We copy the requested state to the active state making sure
1051 // to respect these rules (again see Layer.h for a detailed discussion).
1052 //
1053 // There is an awkward asymmetry in the handling of the crop states in the position
1054 // states, as can be seen below. Largely this arises from position and transform
1055 // being stored in the same data structure while having different latching rules.
1056 // b/38182305
1057 //
1058 // Careful that "c" and editCurrentState may not begin as equivalent due to
1059 // applyPendingStates in the presence of deferred transactions.
1060 if (mFreezeGeometryUpdates) {
Robert Carr82364e32016-05-15 11:27:47 -07001061 float tx = c.active.transform.tx();
1062 float ty = c.active.transform.ty();
1063 c.active = c.requested;
1064 c.active.transform.set(tx, ty);
1065 editCurrentState.active = c.active;
1066 } else {
1067 editCurrentState.active = editCurrentState.requested;
1068 c.active = c.requested;
1069 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001070 }
1071
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001072 if (s.active != c.active) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001073 // invalidate and recompute the visible regions if needed
1074 flags |= Layer::eVisibleRegion;
1075 }
1076
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001077 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001078 // invalidate and recompute the visible regions if needed
1079 flags |= eVisibleRegion;
1080 this->contentDirty = true;
1081
1082 // we may use linear filtering, if the matrix scales us
Robert Carr3dcabfa2016-03-01 18:36:58 -08001083 const uint8_t type = c.active.transform.getType();
David Sodman41fdfc92017-11-06 16:09:56 -08001084 mNeedsFiltering = (!c.active.transform.preserveRects() || (type >= Transform::SCALE));
Mathias Agopian13127d82013-03-05 17:47:11 -08001085 }
1086
Dan Stozac8145172016-04-28 16:29:06 -07001087 // If the layer is hidden, signal and clear out all local sync points so
1088 // that transactions for layers depending on this layer's frames becoming
1089 // visible are not blocked
1090 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001091 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001092 }
1093
Mathias Agopian13127d82013-03-05 17:47:11 -08001094 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001095 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001096 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001097}
1098
Pablo Ceballos05289c22016-04-14 15:49:55 -07001099void Layer::commitTransaction(const State& stateToCommit) {
1100 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001101}
1102
Mathias Agopian13127d82013-03-05 17:47:11 -08001103uint32_t Layer::getTransactionFlags(uint32_t flags) {
1104 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1105}
1106
1107uint32_t Layer::setTransactionFlags(uint32_t flags) {
1108 return android_atomic_or(flags, &mTransactionFlags);
1109}
1110
Robert Carr82364e32016-05-15 11:27:47 -07001111bool Layer::setPosition(float x, float y, bool immediate) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001112 if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001113 return false;
1114 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001115
1116 // We update the requested and active position simultaneously because
1117 // we want to apply the position portion of the transform matrix immediately,
1118 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Robert Carr3dcabfa2016-03-01 18:36:58 -08001119 mCurrentState.requested.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001120 if (immediate && !mFreezeGeometryUpdates) {
1121 // Here we directly update the active state
1122 // unlike other setters, because we store it within
1123 // the transform, but use different latching rules.
1124 // b/38182305
Robert Carr82364e32016-05-15 11:27:47 -07001125 mCurrentState.active.transform.set(x, y);
1126 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001127 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001128
Dan Stoza7dde5992015-05-22 09:51:44 -07001129 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001130 setTransactionFlags(eTransactionNeeded);
1131 return true;
1132}
Robert Carr82364e32016-05-15 11:27:47 -07001133
Robert Carr1f0a16a2016-10-24 16:27:39 -07001134bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1135 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1136 if (idx < 0) {
1137 return false;
1138 }
1139 if (childLayer->setLayer(z)) {
1140 mCurrentChildren.removeAt(idx);
1141 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001142 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001143 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001144 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001145}
1146
Robert Carr503c7042017-09-27 15:06:08 -07001147bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1148 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1149 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1150 if (idx < 0) {
1151 return false;
1152 }
1153 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1154 mCurrentChildren.removeAt(idx);
1155 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001156 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001157 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001158 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001159}
1160
Robert Carrae060832016-11-28 10:51:00 -08001161bool Layer::setLayer(int32_t z) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001162 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001163 mCurrentState.sequence++;
1164 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001165 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001166
1167 // Discard all relative layering.
1168 if (mCurrentState.zOrderRelativeOf != nullptr) {
1169 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1170 if (strongRelative != nullptr) {
1171 strongRelative->removeZOrderRelative(this);
1172 }
1173 mCurrentState.zOrderRelativeOf = nullptr;
1174 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001175 setTransactionFlags(eTransactionNeeded);
1176 return true;
1177}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001178
Robert Carrdb66e622017-04-10 16:55:57 -07001179void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1180 mCurrentState.zOrderRelatives.remove(relative);
1181 mCurrentState.sequence++;
1182 mCurrentState.modified = true;
1183 setTransactionFlags(eTransactionNeeded);
1184}
1185
1186void Layer::addZOrderRelative(const wp<Layer>& relative) {
1187 mCurrentState.zOrderRelatives.add(relative);
1188 mCurrentState.modified = true;
1189 mCurrentState.sequence++;
1190 setTransactionFlags(eTransactionNeeded);
1191}
1192
Robert Carr503d2bd2017-12-04 15:49:47 -08001193bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001194 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1195 if (handle == nullptr) {
1196 return false;
1197 }
1198 sp<Layer> relative = handle->owner.promote();
1199 if (relative == nullptr) {
1200 return false;
1201 }
1202
Robert Carr503d2bd2017-12-04 15:49:47 -08001203 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1204 mCurrentState.zOrderRelativeOf == relative) {
1205 return false;
1206 }
1207
Robert Carrdb66e622017-04-10 16:55:57 -07001208 mCurrentState.sequence++;
1209 mCurrentState.modified = true;
Robert Carr503d2bd2017-12-04 15:49:47 -08001210 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001211
chaviw9ab4bd12017-11-03 13:11:00 -07001212 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
1213 if (oldZOrderRelativeOf != nullptr) {
1214 oldZOrderRelativeOf->removeZOrderRelative(this);
1215 }
Robert Carrdb66e622017-04-10 16:55:57 -07001216 mCurrentState.zOrderRelativeOf = relative;
1217 relative->addZOrderRelative(this);
1218
1219 setTransactionFlags(eTransactionNeeded);
1220
1221 return true;
1222}
1223
Mathias Agopian13127d82013-03-05 17:47:11 -08001224bool Layer::setSize(uint32_t w, uint32_t h) {
David Sodman41fdfc92017-11-06 16:09:56 -08001225 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001226 mCurrentState.requested.w = w;
1227 mCurrentState.requested.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001228 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001229 setTransactionFlags(eTransactionNeeded);
1230 return true;
1231}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001232bool Layer::setAlpha(float alpha) {
David Sodman41fdfc92017-11-06 16:09:56 -08001233 if (mCurrentState.color.a == alpha) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001234 mCurrentState.sequence++;
chaviw13fdc492017-06-27 12:40:18 -07001235 mCurrentState.color.a = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001236 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001237 setTransactionFlags(eTransactionNeeded);
1238 return true;
1239}
chaviw13fdc492017-06-27 12:40:18 -07001240
1241bool Layer::setColor(const half3& color) {
David Sodman41fdfc92017-11-06 16:09:56 -08001242 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g &&
1243 color.b == mCurrentState.color.b)
chaviw13fdc492017-06-27 12:40:18 -07001244 return false;
1245
1246 mCurrentState.sequence++;
1247 mCurrentState.color.r = color.r;
1248 mCurrentState.color.g = color.g;
1249 mCurrentState.color.b = color.b;
1250 mCurrentState.modified = true;
1251 setTransactionFlags(eTransactionNeeded);
1252 return true;
1253}
1254
Mathias Agopian13127d82013-03-05 17:47:11 -08001255bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
1256 mCurrentState.sequence++;
David Sodman41fdfc92017-11-06 16:09:56 -08001257 mCurrentState.requested.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001258 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001259 setTransactionFlags(eTransactionNeeded);
1260 return true;
1261}
1262bool Layer::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001263 mCurrentState.requestedTransparentRegion = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001264 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001265 setTransactionFlags(eTransactionNeeded);
1266 return true;
1267}
1268bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1269 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
David Sodman41fdfc92017-11-06 16:09:56 -08001270 if (mCurrentState.flags == newFlags) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001271 mCurrentState.sequence++;
1272 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001273 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001274 setTransactionFlags(eTransactionNeeded);
1275 return true;
1276}
Robert Carr99e27f02016-06-16 15:18:02 -07001277
1278bool Layer::setCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001279 if (mCurrentState.requestedCrop == crop) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001280 mCurrentState.sequence++;
Robert Carr99e27f02016-06-16 15:18:02 -07001281 mCurrentState.requestedCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001282 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr99e27f02016-06-16 15:18:02 -07001283 mCurrentState.crop = crop;
1284 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001285 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1286
Dan Stoza7dde5992015-05-22 09:51:44 -07001287 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001288 setTransactionFlags(eTransactionNeeded);
1289 return true;
1290}
Robert Carr8d5227b2017-03-16 15:41:03 -07001291
1292bool Layer::setFinalCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001293 if (mCurrentState.requestedFinalCrop == crop) return false;
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001294 mCurrentState.sequence++;
Robert Carr8d5227b2017-03-16 15:41:03 -07001295 mCurrentState.requestedFinalCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001296 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr8d5227b2017-03-16 15:41:03 -07001297 mCurrentState.finalCrop = crop;
1298 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001299 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1300
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001301 mCurrentState.modified = true;
1302 setTransactionFlags(eTransactionNeeded);
1303 return true;
1304}
Mathias Agopian13127d82013-03-05 17:47:11 -08001305
Robert Carrc3574f72016-03-24 12:19:32 -07001306bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001307 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001308 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001309 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001310 return true;
1311}
1312
rongliuccd34842018-03-14 12:26:23 -07001313void Layer::setInfo(int32_t type, int32_t appId) {
David Sodman41fdfc92017-11-06 16:09:56 -08001314 mCurrentState.appId = appId;
1315 mCurrentState.type = type;
1316 mCurrentState.modified = true;
1317 setTransactionFlags(eTransactionNeeded);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001318}
1319
Mathias Agopian13127d82013-03-05 17:47:11 -08001320bool Layer::setLayerStack(uint32_t layerStack) {
David Sodman41fdfc92017-11-06 16:09:56 -08001321 if (mCurrentState.layerStack == layerStack) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001322 mCurrentState.sequence++;
1323 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001324 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001325 setTransactionFlags(eTransactionNeeded);
1326 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001327}
1328
Robert Carr1f0a16a2016-10-24 16:27:39 -07001329uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001330 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001331 if (p == nullptr) {
1332 return getDrawingState().layerStack;
1333 }
1334 return p->getLayerStack();
1335}
1336
David Sodman41fdfc92017-11-06 16:09:56 -08001337void Layer::deferTransactionUntil(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001338 mCurrentState.barrierLayer = barrierLayer;
Dan Stoza7dde5992015-05-22 09:51:44 -07001339 mCurrentState.frameNumber = frameNumber;
1340 // We don't set eTransactionNeeded, because just receiving a deferral
1341 // request without any other state updates shouldn't actually induce a delay
1342 mCurrentState.modified = true;
1343 pushPendingState();
Robert Carr0d480722017-01-10 16:42:54 -08001344 mCurrentState.barrierLayer = nullptr;
Dan Stoza792e5292016-02-11 11:43:58 -08001345 mCurrentState.frameNumber = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001346 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001347}
1348
David Sodman41fdfc92017-11-06 16:09:56 -08001349void Layer::deferTransactionUntil(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001350 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
1351 deferTransactionUntil(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001352}
1353
Dan Stozaee44edd2015-03-23 15:50:23 -07001354
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001355// ----------------------------------------------------------------------------
1356// pageflip handling...
1357// ----------------------------------------------------------------------------
1358
Robert Carr1f0a16a2016-10-24 16:27:39 -07001359bool Layer::isHiddenByPolicy() const {
1360 const Layer::State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001361 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001362 if (parent != nullptr && parent->isHiddenByPolicy()) {
1363 return true;
1364 }
1365 return s.flags & layer_state_t::eLayerHidden;
1366}
1367
David Sodman41fdfc92017-11-06 16:09:56 -08001368uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001369 // TODO: should we do something special if mSecure is set?
1370 if (mProtectedByApp) {
1371 // need a hardware-protected path to external video sink
1372 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001373 }
Riley Andrews03414a12014-07-01 14:22:59 -07001374 if (mPotentialCursor) {
1375 usage |= GraphicBuffer::USAGE_CURSOR;
1376 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001377 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001378 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001379}
1380
Mathias Agopian84300952012-11-21 16:02:13 -08001381void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001382 uint32_t orientation = 0;
1383 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001384 // The transform hint is used to improve performance, but we can
1385 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001386 // apply to all displays.
Mathias Agopian42977342012-08-05 00:40:46 -07001387 const Transform& planeTransform(hw->getTransform());
Mathias Agopian4fec8732012-06-29 14:12:52 -07001388 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07001389 if (orientation & Transform::ROT_INVALID) {
1390 orientation = 0;
1391 }
1392 }
David Sodmaneb085e02017-10-05 18:49:04 -07001393 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001394}
1395
Mathias Agopian13127d82013-03-05 17:47:11 -08001396// ----------------------------------------------------------------------------
1397// debugging
1398// ----------------------------------------------------------------------------
1399
Kalle Raitaa099a242017-01-11 11:17:29 -08001400LayerDebugInfo Layer::getLayerDebugInfo() const {
1401 LayerDebugInfo info;
1402 const Layer::State& ds = getDrawingState();
1403 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001404 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001405 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
1406 info.mType = String8(getTypeId());
1407 info.mTransparentRegion = ds.activeTransparentRegion;
1408 info.mVisibleRegion = visibleRegion;
1409 info.mSurfaceDamageRegion = surfaceDamageRegion;
1410 info.mLayerStack = getLayerStack();
1411 info.mX = ds.active.transform.tx();
1412 info.mY = ds.active.transform.ty();
1413 info.mZ = ds.z;
1414 info.mWidth = ds.active.w;
1415 info.mHeight = ds.active.h;
1416 info.mCrop = ds.crop;
1417 info.mFinalCrop = ds.finalCrop;
chaviw13fdc492017-06-27 12:40:18 -07001418 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001419 info.mFlags = ds.flags;
1420 info.mPixelFormat = getPixelFormat();
Chia-I Wu1b6bafc2018-05-22 12:03:00 -07001421 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Kalle Raitaa099a242017-01-11 11:17:29 -08001422 info.mMatrix[0][0] = ds.active.transform[0][0];
1423 info.mMatrix[0][1] = ds.active.transform[0][1];
1424 info.mMatrix[1][0] = ds.active.transform[1][0];
1425 info.mMatrix[1][1] = ds.active.transform[1][1];
1426 {
David Sodman0cc69182017-11-17 12:12:07 -08001427 sp<const GraphicBuffer> buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001428 if (buffer != 0) {
1429 info.mActiveBufferWidth = buffer->getWidth();
1430 info.mActiveBufferHeight = buffer->getHeight();
1431 info.mActiveBufferStride = buffer->getStride();
1432 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001433 } else {
1434 info.mActiveBufferWidth = 0;
1435 info.mActiveBufferHeight = 0;
1436 info.mActiveBufferStride = 0;
1437 info.mActiveBufferFormat = 0;
1438 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001439 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001440 info.mNumQueuedFrames = getQueuedFrameCount();
1441 info.mRefreshPending = isBufferLatched();
1442 info.mIsOpaque = isOpaque(ds);
1443 info.mContentDirty = contentDirty;
1444 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001445}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001446
Dan Stozae22aec72016-08-01 13:20:59 -07001447void Layer::miniDumpHeader(String8& result) {
1448 result.append("----------------------------------------");
1449 result.append("---------------------------------------\n");
1450 result.append(" Layer name\n");
1451 result.append(" Z | ");
1452 result.append(" Comp Type | ");
1453 result.append(" Disp Frame (LTRB) | ");
1454 result.append(" Source Crop (LTRB)\n");
1455 result.append("----------------------------------------");
1456 result.append("---------------------------------------\n");
1457}
1458
1459void Layer::miniDump(String8& result, int32_t hwcId) const {
David Sodman6f65f3e2017-11-03 14:28:09 -07001460 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stozae22aec72016-08-01 13:20:59 -07001461 return;
1462 }
1463
1464 String8 name;
1465 if (mName.length() > 77) {
1466 std::string shortened;
1467 shortened.append(mName.string(), 36);
1468 shortened.append("[...]");
1469 shortened.append(mName.string() + (mName.length() - 36), 36);
1470 name = shortened.c_str();
1471 } else {
1472 name = mName;
1473 }
1474
1475 result.appendFormat(" %s\n", name.string());
1476
1477 const Layer::State& layerState(getDrawingState());
David Sodman6f65f3e2017-11-03 14:28:09 -07001478 const LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers.at(hwcId);
Chia-I Wu1e043612018-03-01 09:45:09 -08001479 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
1480 result.appendFormat(" rel %6d | ", layerState.z);
1481 } else {
1482 result.appendFormat(" %10d | ", layerState.z);
1483 }
David Sodman41fdfc92017-11-06 16:09:56 -08001484 result.appendFormat("%10s | ", to_string(getCompositionType(hwcId)).c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001485 const Rect& frame = hwcInfo.displayFrame;
David Sodman41fdfc92017-11-06 16:09:56 -08001486 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Dan Stoza5a423ea2017-02-16 14:10:39 -08001487 const FloatRect& crop = hwcInfo.sourceCrop;
David Sodman41fdfc92017-11-06 16:09:56 -08001488 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 -07001489
1490 result.append("- - - - - - - - - - - - - - - - - - - - ");
1491 result.append("- - - - - - - - - - - - - - - - - - - -\n");
1492}
Dan Stozae22aec72016-08-01 13:20:59 -07001493
Svetoslavd85084b2014-03-20 10:28:31 -07001494void Layer::dumpFrameStats(String8& result) const {
1495 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001496}
1497
Svetoslavd85084b2014-03-20 10:28:31 -07001498void Layer::clearFrameStats() {
1499 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001500}
1501
Jamie Gennis6547ff42013-07-16 20:12:42 -07001502void Layer::logFrameStats() {
1503 mFrameTracker.logAndResetStats(mName);
1504}
1505
Svetoslavd85084b2014-03-20 10:28:31 -07001506void Layer::getFrameStats(FrameStats* outStats) const {
1507 mFrameTracker.getStats(outStats);
1508}
1509
Brian Andersond6927fb2016-07-23 23:37:30 -07001510void Layer::dumpFrameEvents(String8& result) {
David Sodman41fdfc92017-11-06 16:09:56 -08001511 result.appendFormat("- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001512 Mutex::Autolock lock(mFrameEventHistoryMutex);
1513 mFrameEventHistory.checkFencesForCompletion();
1514 mFrameEventHistory.dump(result);
1515}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001516
Brian Anderson5ea5e592016-12-01 16:54:33 -08001517void Layer::onDisconnect() {
1518 Mutex::Autolock lock(mFrameEventHistoryMutex);
1519 mFrameEventHistory.onDisconnect();
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001520 mTimeStats.onDisconnect(getName().c_str());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001521}
1522
Brian Anderson3890c392016-07-25 12:48:08 -07001523void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001524 FrameEventHistoryDelta* outDelta) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001525 if (newTimestamps) {
1526 mTimeStats.setPostTime(getName().c_str(), newTimestamps->frameNumber,
1527 newTimestamps->postedTime);
1528 }
1529
Brian Andersond6927fb2016-07-23 23:37:30 -07001530 Mutex::Autolock lock(mFrameEventHistoryMutex);
1531 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001532 // If there are any unsignaled fences in the aquire timeline at this
1533 // point, the previously queued frame hasn't been latched yet. Go ahead
1534 // and try to get the signal time here so the syscall is taken out of
1535 // the main thread's critical path.
1536 mAcquireTimeline.updateSignalTimes();
1537 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001538 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001539 mFrameEventHistory.addQueue(*newTimestamps);
1540 }
1541
Brian Anderson3890c392016-07-25 12:48:08 -07001542 if (outDelta) {
1543 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001544 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001545}
Dan Stozae77c7662016-05-13 11:37:28 -07001546
Chia-I Wu98f1c102017-05-30 14:54:08 -07001547size_t Layer::getChildrenCount() const {
1548 size_t count = 0;
1549 for (const sp<Layer>& child : mCurrentChildren) {
1550 count += 1 + child->getChildrenCount();
1551 }
1552 return count;
1553}
1554
Robert Carr1f0a16a2016-10-24 16:27:39 -07001555void Layer::addChild(const sp<Layer>& layer) {
1556 mCurrentChildren.add(layer);
1557 layer->setParent(this);
1558}
1559
1560ssize_t Layer::removeChild(const sp<Layer>& layer) {
1561 layer->setParent(nullptr);
1562 return mCurrentChildren.remove(layer);
1563}
1564
Robert Carr1db73f62016-12-21 12:58:51 -08001565bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1566 sp<Handle> handle = nullptr;
1567 sp<Layer> newParent = nullptr;
1568 if (newParentHandle == nullptr) {
1569 return false;
1570 }
1571 handle = static_cast<Handle*>(newParentHandle.get());
1572 newParent = handle->owner.promote();
1573 if (newParent == nullptr) {
1574 ALOGE("Unable to promote Layer handle");
1575 return false;
1576 }
1577
1578 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001579 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001580
1581 sp<Client> client(child->mClientRef.promote());
1582 if (client != nullptr) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001583 client->updateParent(newParent);
Robert Carr1db73f62016-12-21 12:58:51 -08001584 }
1585 }
1586 mCurrentChildren.clear();
1587
1588 return true;
1589}
1590
Robert Carr15eae092018-03-23 13:43:53 -07001591void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001592 for (const sp<Layer>& child : mDrawingChildren) {
1593 child->mDrawingParent = newParent;
1594 }
1595}
1596
chaviwf1961f72017-09-18 16:41:07 -07001597bool Layer::reparent(const sp<IBinder>& newParentHandle) {
1598 if (newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001599 return false;
1600 }
1601
1602 auto handle = static_cast<Handle*>(newParentHandle.get());
1603 sp<Layer> newParent = handle->owner.promote();
1604 if (newParent == nullptr) {
1605 ALOGE("Unable to promote Layer handle");
1606 return false;
1607 }
1608
chaviwf1961f72017-09-18 16:41:07 -07001609 sp<Layer> parent = getParent();
1610 if (parent != nullptr) {
1611 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001612 }
chaviwf1961f72017-09-18 16:41:07 -07001613 newParent->addChild(this);
chaviw06178942017-07-27 10:25:59 -07001614
chaviwf1961f72017-09-18 16:41:07 -07001615 sp<Client> client(mClientRef.promote());
chaviw06178942017-07-27 10:25:59 -07001616 sp<Client> newParentClient(newParent->mClientRef.promote());
1617
chaviwf1961f72017-09-18 16:41:07 -07001618 if (client != newParentClient) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001619 client->updateParent(newParent);
chaviw06178942017-07-27 10:25:59 -07001620 }
1621
chaviw06178942017-07-27 10:25:59 -07001622 return true;
1623}
1624
Robert Carr9524cb32017-02-13 11:32:32 -08001625bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001626 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001627 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001628 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001629 if (client != nullptr && parentClient != client) {
Robert Carr7f619b22017-11-06 12:56:35 -08001630 client->detachLayer(child.get());
1631 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001632 }
Robert Carr7f619b22017-11-06 12:56:35 -08001633 }
Robert Carr9524cb32017-02-13 11:32:32 -08001634
1635 return true;
1636}
1637
Chia-I Wu11481472018-05-04 10:43:19 -07001638bool Layer::isLegacyDataSpace() const {
1639 // return true when no higher bits are set
Chia-I Wu1b6bafc2018-05-22 12:03:00 -07001640 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001641 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001642}
1643
Robert Carr1f0a16a2016-10-24 16:27:39 -07001644void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001645 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001646}
1647
1648void Layer::clearSyncPoints() {
1649 for (const auto& child : mCurrentChildren) {
1650 child->clearSyncPoints();
1651 }
1652
1653 Mutex::Autolock lock(mLocalSyncPointMutex);
1654 for (auto& point : mLocalSyncPoints) {
1655 point->setFrameAvailable();
1656 }
1657 mLocalSyncPoints.clear();
1658}
1659
1660int32_t Layer::getZ() const {
1661 return mDrawingState.z;
1662}
1663
Robert Carr29abff82017-12-04 13:51:20 -08001664bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1665 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1666 const State& state = useDrawing ? mDrawingState : mCurrentState;
1667 return state.zOrderRelativeOf != nullptr;
1668}
1669
David Sodman41fdfc92017-11-06 16:09:56 -08001670__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001671 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001672 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1673 "makeTraversalList received invalid stateSet");
1674 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1675 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1676 const State& state = useDrawing ? mDrawingState : mCurrentState;
1677
Robert Carr29abff82017-12-04 13:51:20 -08001678 if (state.zOrderRelatives.size() == 0) {
1679 *outSkipRelativeZUsers = true;
1680 return children;
1681 }
1682
chaviw738df022018-05-31 16:11:27 -07001683 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001684 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001685 sp<Layer> strongRelative = weakRelative.promote();
1686 if (strongRelative != nullptr) {
1687 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001688 }
1689 }
1690
Dan Stoza412903f2017-04-27 13:42:17 -07001691 for (const sp<Layer>& child : children) {
Robert Carr503c7042017-09-27 15:06:08 -07001692 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1693 if (childState.zOrderRelativeOf != nullptr) {
1694 continue;
1695 }
Robert Carrdb66e622017-04-10 16:55:57 -07001696 traverse.add(child);
1697 }
1698
1699 return traverse;
1700}
1701
Robert Carr1f0a16a2016-10-24 16:27:39 -07001702/**
Robert Carrdb66e622017-04-10 16:55:57 -07001703 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001704 */
Dan Stoza412903f2017-04-27 13:42:17 -07001705void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001706 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1707 // produce a new list for traversing, including our relatives, and not including our children
1708 // who are relatives of another surface. In the case that there are no relative Z,
1709 // makeTraversalList returns our children directly to avoid significant overhead.
1710 // However in this case we need to take the responsibility for filtering children which
1711 // are relatives of another surface here.
1712 bool skipRelativeZUsers = false;
1713 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001714
Robert Carr1f0a16a2016-10-24 16:27:39 -07001715 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001716 for (; i < list.size(); i++) {
1717 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001718 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1719 continue;
1720 }
1721
Robert Carrdb66e622017-04-10 16:55:57 -07001722 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001723 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001724 }
Dan Stoza412903f2017-04-27 13:42:17 -07001725 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001726 }
Robert Carr29abff82017-12-04 13:51:20 -08001727
Dan Stoza412903f2017-04-27 13:42:17 -07001728 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001729 for (; i < list.size(); i++) {
1730 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001731
1732 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1733 continue;
1734 }
Dan Stoza412903f2017-04-27 13:42:17 -07001735 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001736 }
1737}
1738
1739/**
Robert Carrdb66e622017-04-10 16:55:57 -07001740 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001741 */
Dan Stoza412903f2017-04-27 13:42:17 -07001742void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1743 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001744 // See traverseInZOrder for documentation.
1745 bool skipRelativeZUsers = false;
1746 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001747
Robert Carr1f0a16a2016-10-24 16:27:39 -07001748 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001749 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001750 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001751
1752 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1753 continue;
1754 }
1755
Robert Carrdb66e622017-04-10 16:55:57 -07001756 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001757 break;
1758 }
Dan Stoza412903f2017-04-27 13:42:17 -07001759 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001760 }
Dan Stoza412903f2017-04-27 13:42:17 -07001761 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001762 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001763 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001764
1765 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1766 continue;
1767 }
1768
Dan Stoza412903f2017-04-27 13:42:17 -07001769 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001770 }
1771}
1772
chaviw4b129c22018-04-09 16:19:43 -07001773LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1774 const std::vector<Layer*>& layersInTree) {
1775 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1776 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001777 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1778 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
chaviw4b129c22018-04-09 16:19:43 -07001779 const State& state = useDrawing ? mDrawingState : mCurrentState;
1780
chaviw738df022018-05-31 16:11:27 -07001781 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001782 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1783 sp<Layer> strongRelative = weakRelative.promote();
1784 // Only add relative layers that are also descendents of the top most parent of the tree.
1785 // If a relative layer is not a descendent, then it should be ignored.
1786 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1787 traverse.add(strongRelative);
1788 }
1789 }
1790
1791 for (const sp<Layer>& child : children) {
1792 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1793 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1794 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1795 // the child here since it won't be added later as a relative.
1796 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1797 childState.zOrderRelativeOf.promote().get())) {
1798 continue;
1799 }
1800 traverse.add(child);
1801 }
1802
1803 return traverse;
1804}
1805
1806void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1807 LayerVector::StateSet stateSet,
1808 const LayerVector::Visitor& visitor) {
1809 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001810
1811 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001812 for (; i < list.size(); i++) {
1813 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001814 if (relative->getZ() >= 0) {
1815 break;
1816 }
chaviw4b129c22018-04-09 16:19:43 -07001817 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001818 }
chaviw4b129c22018-04-09 16:19:43 -07001819
chaviwa76b2712017-09-20 12:02:26 -07001820 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001821 for (; i < list.size(); i++) {
1822 const auto& relative = list[i];
1823 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001824 }
1825}
1826
chaviw4b129c22018-04-09 16:19:43 -07001827std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1828 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1829 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1830
1831 std::vector<Layer*> layersInTree = {this};
1832 for (size_t i = 0; i < children.size(); i++) {
1833 const auto& child = children[i];
1834 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1835 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1836 }
1837
1838 return layersInTree;
1839}
1840
1841void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1842 const LayerVector::Visitor& visitor) {
1843 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1844 std::sort(layersInTree.begin(), layersInTree.end());
1845 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1846}
1847
Robert Carr1f0a16a2016-10-24 16:27:39 -07001848Transform Layer::getTransform() const {
1849 Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001850 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001851 if (p != nullptr) {
1852 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07001853
1854 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
1855 // it isFixedSize) then there may be additional scaling not accounted
1856 // for in the transform. We need to mirror this scaling in child surfaces
1857 // or we will break the contract where WM can treat child surfaces as
1858 // pixels in the parent surface.
David Sodman0cc69182017-11-17 12:12:07 -08001859 if (p->isFixedSize() && p->getBE().compositionInfo.mBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07001860 int bufferWidth;
1861 int bufferHeight;
1862 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
David Sodman0cc69182017-11-17 12:12:07 -08001863 bufferWidth = p->getBE().compositionInfo.mBuffer->getWidth();
1864 bufferHeight = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001865 } else {
David Sodman0cc69182017-11-17 12:12:07 -08001866 bufferHeight = p->getBE().compositionInfo.mBuffer->getWidth();
1867 bufferWidth = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001868 }
David Sodman41fdfc92017-11-06 16:09:56 -08001869 float sx = p->getDrawingState().active.w / static_cast<float>(bufferWidth);
1870 float sy = p->getDrawingState().active.h / static_cast<float>(bufferHeight);
Robert Carr9b429f42017-04-17 14:56:57 -07001871 Transform extraParentScaling;
1872 extraParentScaling.set(sx, 0, 0, sy);
1873 t = t * extraParentScaling;
1874 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001875 }
1876 return t * getDrawingState().active.transform;
1877}
1878
chaviw13fdc492017-06-27 12:40:18 -07001879half Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001880 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07001881
chaviw13fdc492017-06-27 12:40:18 -07001882 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1883 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001884}
Robert Carr6452f122017-03-21 10:41:29 -07001885
chaviw13fdc492017-06-27 12:40:18 -07001886half4 Layer::getColor() const {
1887 const half4 color(getDrawingState().color);
1888 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001889}
Robert Carr6452f122017-03-21 10:41:29 -07001890
Robert Carr1f0a16a2016-10-24 16:27:39 -07001891void Layer::commitChildList() {
1892 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1893 const auto& child = mCurrentChildren[i];
1894 child->commitChildList();
1895 }
1896 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001897 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001898}
1899
chaviw1d044282017-09-27 12:19:28 -07001900void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
1901 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1902 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1903 const State& state = useDrawing ? mDrawingState : mCurrentState;
1904
1905 Transform requestedTransform = state.active.transform;
1906 Transform transform = getTransform();
1907
1908 layerInfo->set_id(sequence);
1909 layerInfo->set_name(getName().c_str());
1910 layerInfo->set_type(String8(getTypeId()));
1911
1912 for (const auto& child : children) {
1913 layerInfo->add_children(child->sequence);
1914 }
1915
1916 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1917 sp<Layer> strongRelative = weakRelative.promote();
1918 if (strongRelative != nullptr) {
1919 layerInfo->add_relatives(strongRelative->sequence);
1920 }
1921 }
1922
1923 LayerProtoHelper::writeToProto(state.activeTransparentRegion,
1924 layerInfo->mutable_transparent_region());
1925 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
1926 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
1927
1928 layerInfo->set_layer_stack(getLayerStack());
1929 layerInfo->set_z(state.z);
1930
1931 PositionProto* position = layerInfo->mutable_position();
1932 position->set_x(transform.tx());
1933 position->set_y(transform.ty());
1934
1935 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
1936 requestedPosition->set_x(requestedTransform.tx());
1937 requestedPosition->set_y(requestedTransform.ty());
1938
1939 SizeProto* size = layerInfo->mutable_size();
1940 size->set_w(state.active.w);
1941 size->set_h(state.active.h);
1942
1943 LayerProtoHelper::writeToProto(state.crop, layerInfo->mutable_crop());
1944 LayerProtoHelper::writeToProto(state.finalCrop, layerInfo->mutable_final_crop());
1945
1946 layerInfo->set_is_opaque(isOpaque(state));
1947 layerInfo->set_invalidate(contentDirty);
Chia-I Wu1b6bafc2018-05-22 12:03:00 -07001948
1949 // XXX (b/79210409) mCurrentDataSpace is not protected
1950 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
1951
chaviw1d044282017-09-27 12:19:28 -07001952 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1953 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
1954 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
1955 layerInfo->set_flags(state.flags);
1956
1957 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1958 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
1959
Jorim Jaggi8e0af362017-11-14 16:28:28 +01001960 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07001961 if (parent != nullptr) {
1962 layerInfo->set_parent(parent->sequence);
1963 }
1964
1965 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1966 if (zOrderRelativeOf != nullptr) {
1967 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1968 }
1969
Chia-I Wu1b6bafc2018-05-22 12:03:00 -07001970 // XXX getBE().compositionInfo.mBuffer is not protected
David Sodman0cc69182017-11-17 12:12:07 -08001971 auto buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001972 if (buffer != nullptr) {
1973 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
chaviw1d044282017-09-27 12:19:28 -07001974 }
1975
1976 layerInfo->set_queued_frames(getQueuedFrameCount());
1977 layerInfo->set_refresh_pending(isBufferLatched());
rongliuccd34842018-03-14 12:26:23 -07001978 layerInfo->set_window_type(state.type);
1979 layerInfo->set_app_id(state.appId);
chaviw1d044282017-09-27 12:19:28 -07001980}
1981
Yiwei Zhang068e31b2018-02-21 13:02:45 -08001982void Layer::writeToProto(LayerProto* layerInfo, int32_t hwcId) {
Peiyong Lin0874d2b2018-09-04 09:39:34 -07001983 if (!hasHwcLayer(hwcId)) {
1984 return;
1985 }
Yiwei Zhang068e31b2018-02-21 13:02:45 -08001986 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
1987
1988 const auto& hwcInfo = getBE().mHwcLayers.at(hwcId);
1989
1990 const Rect& frame = hwcInfo.displayFrame;
1991 LayerProtoHelper::writeToProto(frame, layerInfo->mutable_hwc_frame());
1992
1993 const FloatRect& crop = hwcInfo.sourceCrop;
1994 LayerProtoHelper::writeToProto(crop, layerInfo->mutable_hwc_crop());
1995
1996 const int32_t transform = static_cast<int32_t>(hwcInfo.transform);
1997 layerInfo->set_hwc_transform(transform);
Yiwei Zhang7c64f172018-03-07 14:52:28 -08001998
1999 const int32_t compositionType = static_cast<int32_t>(hwcInfo.compositionType);
2000 layerInfo->set_hwc_composition_type(compositionType);
2001
2002 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
2003 static_cast<BufferLayer*>(this)->isProtected()) {
2004 layerInfo->set_is_protected(true);
2005 } else {
2006 layerInfo->set_is_protected(false);
2007 }
Yiwei Zhang068e31b2018-02-21 13:02:45 -08002008}
2009
Mathias Agopian13127d82013-03-05 17:47:11 -08002010// ---------------------------------------------------------------------------
2011
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002012}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002013
2014#if defined(__gl_h_)
2015#error "don't include gl/gl.h in this file"
2016#endif
2017
2018#if defined(__gl2_h_)
2019#error "don't include gl2/gl2.h in this file"
2020#endif