blob: 6d5a598e892f53375d49963886f9e5de4e699267 [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
Chia-I Wue41dbe62017-06-13 14:10:56 -0700348 const auto& p = mDrawingParent.promote();
Robert Carrd4ae7f32018-06-07 16:10:57 -0700349 FloatRect floatWin = win.toFloatRect();
350 FloatRect parentBounds = floatWin;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700351 if (p != nullptr) {
Robert Carrd4ae7f32018-06-07 16:10:57 -0700352 // We pass an empty Region here for reasons mirroring that of the case described in
353 // the computeScreenBounds reduceTransparentRegion=false case.
354 parentBounds = p->computeBounds(Region());
Robert Carr1f0a16a2016-10-24 16:27:39 -0700355 }
356
Robert Carrd4ae7f32018-06-07 16:10:57 -0700357 Transform t = s.active.transform;
Dan Stoza80d61162017-12-20 15:57:52 -0800358
Robert Carrd4ae7f32018-06-07 16:10:57 -0700359
360 if (p != nullptr || !s.finalCrop.isEmpty()) {
Dan Stoza80d61162017-12-20 15:57:52 -0800361 floatWin = t.transform(floatWin);
Robert Carrd4ae7f32018-06-07 16:10:57 -0700362 floatWin = floatWin.intersect(parentBounds);
363
364 if (!s.finalCrop.isEmpty()) {
365 floatWin = floatWin.intersect(s.finalCrop.toFloatRect());
366 }
Dan Stoza80d61162017-12-20 15:57:52 -0800367 floatWin = t.inverse().transform(floatWin);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700368 }
369
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700370 // subtract the transparent region and snap to the bounds
Dan Stoza80d61162017-12-20 15:57:52 -0800371 return reduce(floatWin, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800372}
373
Robert Carr1f0a16a2016-10-24 16:27:39 -0700374Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& hw) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700375 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800376 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700377 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800378
379 // apply the projection's clipping to the window crop in
380 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700381 // if there are no window scaling involved, this operation will map to full
382 // pixels in the buffer.
383 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
384 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700385
386 Rect activeCrop(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700387 if (!s.crop.isEmpty()) {
Chia-I Wudf7867f2017-07-20 14:24:37 -0700388 activeCrop.intersect(s.crop, &activeCrop);
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700389 }
390
Robert Carr1f0a16a2016-10-24 16:27:39 -0700391 Transform t = getTransform();
392 activeCrop = t.transform(activeCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000393 if (!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
394 activeCrop.clear();
395 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700396 if (!s.finalCrop.isEmpty()) {
David Sodman41fdfc92017-11-06 16:09:56 -0800397 if (!activeCrop.intersect(s.finalCrop, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000398 activeCrop.clear();
399 }
400 }
chaviwb1154d12017-10-31 14:15:36 -0700401
402 const auto& p = mDrawingParent.promote();
403 if (p != nullptr) {
404 auto parentCrop = p->computeInitialCrop(hw);
405 activeCrop.intersect(parentCrop, &activeCrop);
406 }
407
Robert Carr1f0a16a2016-10-24 16:27:39 -0700408 return activeCrop;
409}
410
Dan Stoza5a423ea2017-02-16 14:10:39 -0800411FloatRect Layer::computeCrop(const sp<const DisplayDevice>& hw) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700412 // the content crop is the area of the content that gets scaled to the
413 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800414 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700415
416 // In addition there is a WM-specified crop we pull from our drawing state.
417 const State& s(getDrawingState());
418
419 // Screen space to make reduction to parent crop clearer.
420 Rect activeCrop = computeInitialCrop(hw);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700421 Transform t = getTransform();
422 // Back to layer space to work with the content crop.
423 activeCrop = t.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800424
Michael Lentine28ea2172014-11-19 18:32:37 -0800425 // This needs to be here as transform.transform(Rect) computes the
426 // transformed rect and then takes the bounding box of the result before
427 // returning. This means
428 // transform.inverse().transform(transform.transform(Rect)) != Rect
429 // in which case we need to make sure the final rect is clipped to the
430 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000431 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
432 activeCrop.clear();
433 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800434
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700435 // subtract the transparent region and snap to the bounds
436 activeCrop = reduce(activeCrop, s.activeTransparentRegion);
437
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000438 // Transform the window crop to match the buffer coordinate system,
439 // which means using the inverse of the current transform set on the
440 // SurfaceFlingerConsumer.
441 uint32_t invTransform = mCurrentTransform;
Robert Carrcae605c2017-03-29 12:10:31 -0700442 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000443 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700444 * the code below applies the primary display's inverse transform to the
445 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000446 */
David Sodman41fdfc92017-11-06 16:09:56 -0800447 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000448 // calculate the inverse transform
449 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800450 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800451 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000452 // and apply to the current transform
David Sodman41fdfc92017-11-06 16:09:56 -0800453 invTransform = (Transform(invTransformOrient) * Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800454 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000455
456 int winWidth = s.active.w;
457 int winHeight = s.active.h;
458 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
459 // If the activeCrop has been rotate the ends are rotated but not
460 // the space itself so when transforming ends back we can't rely on
461 // a modification of the axes of rotation. To account for this we
462 // need to reorient the inverse rotation in terms of the current
463 // axes of rotation.
464 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
465 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
466 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800467 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000468 }
469 winWidth = s.active.h;
470 winHeight = s.active.w;
471 }
David Sodman41fdfc92017-11-06 16:09:56 -0800472 const Rect winCrop = activeCrop.transform(invTransform, s.active.w, s.active.h);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000473
474 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800475 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000476 float yScale = crop.getHeight() / float(winHeight);
477
David Sodman41fdfc92017-11-06 16:09:56 -0800478 float insetL = winCrop.left * xScale;
479 float insetT = winCrop.top * yScale;
480 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000481 float insetB = (winHeight - winCrop.bottom) * yScale;
482
David Sodman41fdfc92017-11-06 16:09:56 -0800483 crop.left += insetL;
484 crop.top += insetT;
485 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000486 crop.bottom -= insetB;
487
Mathias Agopian13127d82013-03-05 17:47:11 -0800488 return crop;
489}
490
Robert Carrae060832016-11-28 10:51:00 -0800491void Layer::setGeometry(const sp<const DisplayDevice>& displayDevice, uint32_t z)
Mathias Agopiana350ff92010-08-10 17:14:02 -0700492{
Dan Stoza9e56aa02015-11-02 13:00:03 -0800493 const auto hwcId = displayDevice->getHwcDisplayId();
David Sodman6f65f3e2017-11-03 14:28:09 -0700494 auto& hwcInfo = getBE().mHwcLayers[hwcId];
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700495
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700496 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800497 hwcInfo.forceClientComposition = false;
498
499 if (isSecure() && !displayDevice->isSecure()) {
500 hwcInfo.forceClientComposition = true;
501 }
502
503 auto& hwcLayer = hwcInfo.layer;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700504
Mathias Agopian13127d82013-03-05 17:47:11 -0800505 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700506 const State& s(getDrawingState());
David Revemanecf0fa52017-03-03 11:32:44 -0500507 auto blendMode = HWC2::BlendMode::None;
Robert Carr6452f122017-03-21 10:41:29 -0700508 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800509 blendMode =
510 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800511 }
David Revemanecf0fa52017-03-03 11:32:44 -0500512 auto error = hwcLayer->setBlendMode(blendMode);
David Sodman41fdfc92017-11-06 16:09:56 -0800513 ALOGE_IF(error != HWC2::Error::None,
514 "[%s] Failed to set blend mode %s:"
515 " %s (%d)",
516 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
517 static_cast<int32_t>(error));
Mathias Agopian13127d82013-03-05 17:47:11 -0800518
519 // apply the layer's transform, followed by the display's global transform
520 // here we're guaranteed that the layer's transform preserves rects
Michael Lentine6c925ed2014-09-26 17:55:01 -0700521 Region activeTransparentRegion(s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700522 Transform t = getTransform();
Robert Carrb5d3d262016-03-25 15:08:13 -0700523 if (!s.crop.isEmpty()) {
524 Rect activeCrop(s.crop);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700525 activeCrop = t.transform(activeCrop);
David Sodman41fdfc92017-11-06 16:09:56 -0800526 if (!activeCrop.intersect(displayDevice->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000527 activeCrop.clear();
528 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700529 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800530 // This needs to be here as transform.transform(Rect) computes the
531 // transformed rect and then takes the bounding box of the result before
532 // returning. This means
533 // transform.inverse().transform(transform.transform(Rect)) != Rect
534 // in which case we need to make sure the final rect is clipped to the
535 // display bounds.
David Sodman41fdfc92017-11-06 16:09:56 -0800536 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000537 activeCrop.clear();
538 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700539 // mark regions outside the crop as transparent
540 activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top));
David Sodman41fdfc92017-11-06 16:09:56 -0800541 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom, s.active.w, s.active.h));
542 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
543 activeTransparentRegion.orSelf(
544 Rect(activeCrop.right, activeCrop.top, s.active.w, activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700545 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700546
Dan Stoza80d61162017-12-20 15:57:52 -0800547 // computeBounds returns a FloatRect to provide more accuracy during the
548 // transformation. We then round upon constructing 'frame'.
549 Rect frame{t.transform(computeBounds(activeTransparentRegion))};
Robert Carrb5d3d262016-03-25 15:08:13 -0700550 if (!s.finalCrop.isEmpty()) {
David Sodman41fdfc92017-11-06 16:09:56 -0800551 if (!frame.intersect(s.finalCrop, &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000552 frame.clear();
553 }
554 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000555 if (!frame.intersect(displayDevice->getViewport(), &frame)) {
556 frame.clear();
557 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800558 const Transform& tr(displayDevice->getTransform());
559 Rect transformedFrame = tr.transform(frame);
David Revemanecf0fa52017-03-03 11:32:44 -0500560 error = hwcLayer->setDisplayFrame(transformedFrame);
Dan Stozae22aec72016-08-01 13:20:59 -0700561 if (error != HWC2::Error::None) {
David Sodman41fdfc92017-11-06 16:09:56 -0800562 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
563 transformedFrame.left, transformedFrame.top, transformedFrame.right,
564 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stozae22aec72016-08-01 13:20:59 -0700565 } else {
566 hwcInfo.displayFrame = transformedFrame;
567 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800568
Dan Stoza5a423ea2017-02-16 14:10:39 -0800569 FloatRect sourceCrop = computeCrop(displayDevice);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800570 error = hwcLayer->setSourceCrop(sourceCrop);
Dan Stozae22aec72016-08-01 13:20:59 -0700571 if (error != HWC2::Error::None) {
572 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
David Sodman41fdfc92017-11-06 16:09:56 -0800573 "%s (%d)",
574 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
575 to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stozae22aec72016-08-01 13:20:59 -0700576 } else {
577 hwcInfo.sourceCrop = sourceCrop;
578 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800579
chaviw13fdc492017-06-27 12:40:18 -0700580 float alpha = static_cast<float>(getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -0700581 error = hwcLayer->setPlaneAlpha(alpha);
David Sodman41fdfc92017-11-06 16:09:56 -0800582 ALOGE_IF(error != HWC2::Error::None,
583 "[%s] Failed to set plane alpha %.3f: "
584 "%s (%d)",
585 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800586
Robert Carrae060832016-11-28 10:51:00 -0800587 error = hwcLayer->setZOrder(z);
David Sodman41fdfc92017-11-06 16:09:56 -0800588 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
589 to_string(error).c_str(), static_cast<int32_t>(error));
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500590
Albert Chaulk2a589632017-05-04 16:59:44 -0400591 int type = s.type;
592 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700593 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400594 if (parent.get()) {
595 auto& parentState = parent->getDrawingState();
rongliuccd34842018-03-14 12:26:23 -0700596 if (parentState.type >= 0 || parentState.appId >= 0) {
597 type = parentState.type;
598 appId = parentState.appId;
599 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400600 }
601
602 error = hwcLayer->setInfo(type, appId);
David Sodman41fdfc92017-11-06 16:09:56 -0800603 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
604 static_cast<int32_t>(error));
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800605
Mathias Agopian29a367b2011-07-12 14:51:45 -0700606 /*
607 * Transformations are applied in this order:
608 * 1) buffer orientation/flip/mirror
609 * 2) state transformation (window manager)
610 * 3) layer orientation (screen orientation)
611 * (NOTE: the matrices are multiplied in reverse order)
612 */
613
614 const Transform bufferOrientation(mCurrentTransform);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700615 Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700616
Robert Carrcae605c2017-03-29 12:10:31 -0700617 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700618 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700619 * the code below applies the primary display's inverse transform to the
620 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700621 */
David Sodman41fdfc92017-11-06 16:09:56 -0800622 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700623 // calculate the inverse transform
624 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800625 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700626 }
Robert Carrcae605c2017-03-29 12:10:31 -0700627
628 /*
629 * Here we cancel out the orientation component of the WM transform.
630 * The scaling and translate components are already included in our bounds
631 * computation so it's enough to just omit it in the composition.
632 * See comment in onDraw with ref to b/36727915 for why.
633 */
634 transform = Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700635 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700636
637 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800638 const uint32_t orientation = transform.getOrientation();
Jorim Jaggif3bd94a2018-03-27 15:38:03 +0200639 if (orientation & Transform::ROT_INVALID) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800640 // we can only handle simple transformation
641 hwcInfo.forceClientComposition = true;
642 } else {
643 auto transform = static_cast<HWC2::Transform>(orientation);
Yiwei Zhang068e31b2018-02-21 13:02:45 -0800644 hwcInfo.transform = transform;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800645 auto error = hwcLayer->setTransform(transform);
David Sodman41fdfc92017-11-06 16:09:56 -0800646 ALOGE_IF(error != HWC2::Error::None,
647 "[%s] Failed to set transform %s: "
648 "%s (%d)",
649 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
650 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800651 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700652}
653
Dan Stoza9e56aa02015-11-02 13:00:03 -0800654void Layer::forceClientComposition(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700655 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800656 ALOGE("forceClientComposition: no HWC layer found (%d)", hwcId);
657 return;
658 }
659
David Sodman6f65f3e2017-11-03 14:28:09 -0700660 getBE().mHwcLayers[hwcId].forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800661}
Dan Stozaee44edd2015-03-23 15:50:23 -0700662
chaviwc9232ed2017-11-14 15:31:15 -0800663bool Layer::getForceClientComposition(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700664 if (getBE().mHwcLayers.count(hwcId) == 0) {
chaviwc9232ed2017-11-14 15:31:15 -0800665 ALOGE("getForceClientComposition: no HWC layer found (%d)", hwcId);
666 return false;
667 }
668
David Sodman6f65f3e2017-11-03 14:28:09 -0700669 return getBE().mHwcLayers[hwcId].forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800670}
671
Dan Stoza9e56aa02015-11-02 13:00:03 -0800672void Layer::updateCursorPosition(const sp<const DisplayDevice>& displayDevice) {
673 auto hwcId = displayDevice->getHwcDisplayId();
David Sodman6f65f3e2017-11-03 14:28:09 -0700674 if (getBE().mHwcLayers.count(hwcId) == 0 ||
David Sodman9eeae692017-11-02 10:53:32 -0700675 getCompositionType(hwcId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800676 return;
677 }
678
679 // This gives us only the "orientation" component of the transform
680 const State& s(getCurrentState());
681
682 // Apply the layer's transform, followed by the display's global transform
683 // Here we're guaranteed that the layer's transform preserves rects
684 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700685 if (!s.crop.isEmpty()) {
686 win.intersect(s.crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800687 }
688 // Subtract the transparent region and snap to the bounds
689 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700690 Rect frame(getTransform().transform(bounds));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800691 frame.intersect(displayDevice->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700692 if (!s.finalCrop.isEmpty()) {
693 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000694 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800695 auto& displayTransform(displayDevice->getTransform());
696 auto position = displayTransform.transform(frame);
697
David Sodman6f65f3e2017-11-03 14:28:09 -0700698 auto error = getBE().mHwcLayers[hwcId].layer->setCursorPosition(position.left,
David Sodman9eeae692017-11-02 10:53:32 -0700699 position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800700 ALOGE_IF(error != HWC2::Error::None,
701 "[%s] Failed to set cursor position "
702 "to (%d, %d): %s (%d)",
703 mName.string(), position.left, position.top, to_string(error).c_str(),
704 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800705}
Riley Andrews03414a12014-07-01 14:22:59 -0700706
Mathias Agopian13127d82013-03-05 17:47:11 -0800707// ---------------------------------------------------------------------------
708// drawing...
709// ---------------------------------------------------------------------------
710
chaviwa76b2712017-09-20 12:02:26 -0700711void Layer::draw(const RenderArea& renderArea, const Region& clip) const {
712 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800713}
714
chaviwa76b2712017-09-20 12:02:26 -0700715void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) const {
716 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800717}
718
chaviwa76b2712017-09-20 12:02:26 -0700719void Layer::draw(const RenderArea& renderArea) const {
720 onDraw(renderArea, Region(renderArea.getBounds()), false);
Dan Stozac7014012014-02-14 15:03:43 -0800721}
722
David Sodman41fdfc92017-11-06 16:09:56 -0800723void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
724 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800725 auto& engine(mFlinger->getRenderEngine());
David Sodman9eeae692017-11-02 10:53:32 -0700726 computeGeometry(renderArea, getBE().mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700727 engine.setupFillWithColor(red, green, blue, alpha);
David Sodman9eeae692017-11-02 10:53:32 -0700728 engine.drawMesh(getBE().mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800729}
730
chaviwa76b2712017-09-20 12:02:26 -0700731void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
David Sodman41fdfc92017-11-06 16:09:56 -0800732 clearWithOpenGL(renderArea, 0, 0, 0, 0);
Mathias Agopian13127d82013-03-05 17:47:11 -0800733}
734
David Sodman41fdfc92017-11-06 16:09:56 -0800735void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type, bool callIntoHwc) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700736 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800737 ALOGE("setCompositionType called without a valid HWC layer");
738 return;
739 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700740 auto& hwcInfo = getBE().mHwcLayers[hwcId];
Dan Stoza9e56aa02015-11-02 13:00:03 -0800741 auto& hwcLayer = hwcInfo.layer;
David Sodman41fdfc92017-11-06 16:09:56 -0800742 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(), to_string(type).c_str(),
743 static_cast<int>(callIntoHwc));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800744 if (hwcInfo.compositionType != type) {
745 ALOGV(" actually setting");
746 hwcInfo.compositionType = type;
747 if (callIntoHwc) {
748 auto error = hwcLayer->setCompositionType(type);
David Sodman41fdfc92017-11-06 16:09:56 -0800749 ALOGE_IF(error != HWC2::Error::None,
750 "[%s] Failed to set "
751 "composition type %s: %s (%d)",
752 mName.string(), to_string(type).c_str(), to_string(error).c_str(),
753 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800754 }
755 }
756}
757
758HWC2::Composition Layer::getCompositionType(int32_t hwcId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -0700759 if (hwcId == DisplayDevice::DISPLAY_ID_INVALID) {
760 // If we're querying the composition type for a display that does not
761 // have a HWC counterpart, then it will always be Client
762 return HWC2::Composition::Client;
763 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700764 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700765 ALOGE("getCompositionType called with an invalid HWC layer");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800766 return HWC2::Composition::Invalid;
767 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700768 return getBE().mHwcLayers.at(hwcId).compositionType;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800769}
770
771void Layer::setClearClientTarget(int32_t hwcId, bool clear) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700772 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800773 ALOGE("setClearClientTarget called without a valid HWC layer");
774 return;
775 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700776 getBE().mHwcLayers[hwcId].clearClientTarget = clear;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800777}
778
779bool Layer::getClearClientTarget(int32_t hwcId) const {
David Sodman6f65f3e2017-11-03 14:28:09 -0700780 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800781 ALOGE("getClearClientTarget called without a valid HWC layer");
782 return false;
783 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700784 return getBE().mHwcLayers.at(hwcId).clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800785}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800786
Dan Stozacac35382016-01-27 12:21:06 -0800787bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
788 if (point->getFrameNumber() <= mCurrentFrameNumber) {
789 // Don't bother with a SyncPoint, since we've already latched the
790 // relevant frame
791 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700792 }
793
Dan Stozacac35382016-01-27 12:21:06 -0800794 Mutex::Autolock lock(mLocalSyncPointMutex);
795 mLocalSyncPoints.push_back(point);
796 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700797}
798
Mathias Agopian13127d82013-03-05 17:47:11 -0800799void Layer::setFiltering(bool filtering) {
800 mFiltering = filtering;
801}
802
803bool Layer::getFiltering() const {
804 return mFiltering;
805}
806
Mathias Agopian13127d82013-03-05 17:47:11 -0800807// ----------------------------------------------------------------------------
808// local state
809// ----------------------------------------------------------------------------
810
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000811static void boundPoint(vec2* point, const Rect& crop) {
812 if (point->x < crop.left) {
813 point->x = crop.left;
814 }
815 if (point->x > crop.right) {
816 point->x = crop.right;
817 }
818 if (point->y < crop.top) {
819 point->y = crop.top;
820 }
821 if (point->y > crop.bottom) {
822 point->y = crop.bottom;
823 }
824}
825
chaviwa76b2712017-09-20 12:02:26 -0700826void Layer::computeGeometry(const RenderArea& renderArea, Mesh& mesh,
827 bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700828 const Layer::State& s(getDrawingState());
chaviwa76b2712017-09-20 12:02:26 -0700829 const Transform renderAreaTransform(renderArea.getTransform());
830 const uint32_t height = renderArea.getHeight();
Dan Stoza80d61162017-12-20 15:57:52 -0800831 FloatRect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700832
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000833 vec2 lt = vec2(win.left, win.top);
834 vec2 lb = vec2(win.left, win.bottom);
835 vec2 rb = vec2(win.right, win.bottom);
836 vec2 rt = vec2(win.right, win.top);
837
Robert Carr1f0a16a2016-10-24 16:27:39 -0700838 Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000839 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700840 lt = layerTransform.transform(lt);
841 lb = layerTransform.transform(lb);
842 rb = layerTransform.transform(rb);
843 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000844 }
845
Robert Carrb5d3d262016-03-25 15:08:13 -0700846 if (!s.finalCrop.isEmpty()) {
847 boundPoint(&lt, s.finalCrop);
848 boundPoint(&lb, s.finalCrop);
849 boundPoint(&rb, s.finalCrop);
850 boundPoint(&rt, s.finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000851 }
852
Mathias Agopianff2ed702013-09-01 21:36:12 -0700853 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700854 position[0] = renderAreaTransform.transform(lt);
855 position[1] = renderAreaTransform.transform(lb);
856 position[2] = renderAreaTransform.transform(rb);
857 position[3] = renderAreaTransform.transform(rt);
David Sodman41fdfc92017-11-06 16:09:56 -0800858 for (size_t i = 0; i < 4; i++) {
chaviwa76b2712017-09-20 12:02:26 -0700859 position[i].y = height - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -0800860 }
861}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800862
David Sodman41fdfc92017-11-06 16:09:56 -0800863bool Layer::isSecure() const {
Dan Stoza23116082015-06-18 14:58:39 -0700864 const Layer::State& s(mDrawingState);
865 return (s.flags & layer_state_t::eLayerSecure);
866}
867
Mathias Agopian13127d82013-03-05 17:47:11 -0800868void Layer::setVisibleRegion(const Region& visibleRegion) {
869 // always called from main thread
870 this->visibleRegion = visibleRegion;
871}
872
873void Layer::setCoveredRegion(const Region& coveredRegion) {
874 // always called from main thread
875 this->coveredRegion = coveredRegion;
876}
877
David Sodman41fdfc92017-11-06 16:09:56 -0800878void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800879 // always called from main thread
880 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
881}
882
Robert Carre5f4f692018-01-12 13:12:28 -0800883void Layer::clearVisibilityRegions() {
884 visibleRegion.clear();
885 visibleNonTransparentRegion.clear();
886 coveredRegion.clear();
887}
888
Mathias Agopian13127d82013-03-05 17:47:11 -0800889// ----------------------------------------------------------------------------
890// transaction
891// ----------------------------------------------------------------------------
892
Dan Stoza7dde5992015-05-22 09:51:44 -0700893void Layer::pushPendingState() {
894 if (!mCurrentState.modified) {
895 return;
896 }
897
Dan Stoza7dde5992015-05-22 09:51:44 -0700898 // If this transaction is waiting on the receipt of a frame, generate a sync
899 // point and send it to the remote layer.
Robert Carr0d480722017-01-10 16:42:54 -0800900 if (mCurrentState.barrierLayer != nullptr) {
901 sp<Layer> barrierLayer = mCurrentState.barrierLayer.promote();
902 if (barrierLayer == nullptr) {
903 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700904 // If we can't promote the layer we are intended to wait on,
905 // then it is expired or otherwise invalid. Allow this transaction
906 // to be applied as per normal (no synchronization).
Robert Carr0d480722017-01-10 16:42:54 -0800907 mCurrentState.barrierLayer = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800908 } else {
David Sodman41fdfc92017-11-06 16:09:56 -0800909 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber);
Robert Carr0d480722017-01-10 16:42:54 -0800910 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800911 mRemoteSyncPoints.push_back(std::move(syncPoint));
912 } else {
913 // We already missed the frame we're supposed to synchronize
914 // on, so go ahead and apply the state update
Robert Carr0d480722017-01-10 16:42:54 -0800915 mCurrentState.barrierLayer = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800916 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700917 }
918
Dan Stoza7dde5992015-05-22 09:51:44 -0700919 // Wake us up to check if the frame has been received
920 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700921 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700922 }
923 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700924 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700925}
926
Pablo Ceballos05289c22016-04-14 15:49:55 -0700927void Layer::popPendingState(State* stateToCommit) {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700928 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700929
930 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700931 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700932}
933
Pablo Ceballos05289c22016-04-14 15:49:55 -0700934bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700935 bool stateUpdateAvailable = false;
936 while (!mPendingStates.empty()) {
Robert Carr0d480722017-01-10 16:42:54 -0800937 if (mPendingStates[0].barrierLayer != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700938 if (mRemoteSyncPoints.empty()) {
939 // If we don't have a sync point for this, apply it anyway. It
940 // will be visually wrong, but it should keep us from getting
941 // into too much trouble.
942 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700943 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700944 stateUpdateAvailable = true;
945 continue;
946 }
947
David Sodman41fdfc92017-11-06 16:09:56 -0800948 if (mRemoteSyncPoints.front()->getFrameNumber() != mPendingStates[0].frameNumber) {
949 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800950
951 // Signal our end of the sync point and then dispose of it
952 mRemoteSyncPoints.front()->setTransactionApplied();
953 mRemoteSyncPoints.pop_front();
954 continue;
955 }
956
Dan Stoza7dde5992015-05-22 09:51:44 -0700957 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
958 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700959 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700960 stateUpdateAvailable = true;
961
962 // Signal our end of the sync point and then dispose of it
963 mRemoteSyncPoints.front()->setTransactionApplied();
964 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800965 } else {
966 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700967 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700968 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700969 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700970 stateUpdateAvailable = true;
971 }
972 }
973
974 // If we still have pending updates, wake SurfaceFlinger back up and point
975 // it at this layer so we can process them
976 if (!mPendingStates.empty()) {
977 setTransactionFlags(eTransactionNeeded);
978 mFlinger->setTransactionFlags(eTraversalNeeded);
979 }
980
981 mCurrentState.modified = false;
982 return stateUpdateAvailable;
983}
984
Mathias Agopian13127d82013-03-05 17:47:11 -0800985uint32_t Layer::doTransaction(uint32_t flags) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800986 ATRACE_CALL();
987
Dan Stoza7dde5992015-05-22 09:51:44 -0700988 pushPendingState();
Pablo Ceballos05289c22016-04-14 15:49:55 -0700989 Layer::State c = getCurrentState();
990 if (!applyPendingStates(&c)) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700991 return 0;
992 }
993
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700994 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800995
David Sodman41fdfc92017-11-06 16:09:56 -0800996 const bool sizeChanged = (c.requested.w != s.requested.w) || (c.requested.h != s.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700997
David Sodmaneb085e02017-10-05 18:49:04 -0700998 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700999 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +00001000 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -08001001 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
1002 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1003 " requested={ wh={%4u,%4u} }}\n"
1004 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1005 " requested={ wh={%4u,%4u} }}\n",
David Sodman9eeae692017-11-02 10:53:32 -07001006 this, getName().string(), mCurrentTransform,
1007 getEffectiveScalingMode(), c.active.w, c.active.h, c.crop.left, c.crop.top,
1008 c.crop.right, c.crop.bottom, c.crop.getWidth(), c.crop.getHeight(), c.requested.w,
1009 c.requested.h, s.active.w, s.active.h, s.crop.left, s.crop.top, s.crop.right,
1010 s.crop.bottom, s.crop.getWidth(), s.crop.getHeight(), s.requested.w,
1011 s.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001012
Jamie Gennis2a0d5b62011-09-26 16:54:44 -07001013 // record the new size, form this point on, when the client request
1014 // a buffer, it'll get the new size.
David Sodmaneb085e02017-10-05 18:49:04 -07001015 setDefaultBufferSize(c.requested.w, c.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001016 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001017
Robert Carre392b552017-09-19 12:16:05 -07001018 // Don't let Layer::doTransaction update the drawing state
1019 // if we have a pending resize, unless we are in fixed-size mode.
1020 // the drawing state will be updated only once we receive a buffer
1021 // with the correct size.
1022 //
1023 // In particular, we want to make sure the clip (which is part
1024 // of the geometry state) is latched together with the size but is
1025 // latched immediately when no resizing is involved.
1026 //
1027 // If a sideband stream is attached, however, we want to skip this
1028 // optimization so that transactions aren't missed when a buffer
1029 // never arrives
1030 //
1031 // In the case that we don't have a buffer we ignore other factors
1032 // and avoid entering the resizePending state. At a high level the
1033 // resizePending state is to avoid applying the state of the new buffer
1034 // to the old buffer. However in the state where we don't have an old buffer
1035 // there is no such concern but we may still be being used as a parent layer.
David Sodman41fdfc92017-11-06 16:09:56 -08001036 const bool resizePending = ((c.requested.w != c.active.w) || (c.requested.h != c.active.h)) &&
David Sodman0cc69182017-11-17 12:12:07 -08001037 (getBE().compositionInfo.mBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001038 if (!isFixedSize()) {
David Sodman0cc69182017-11-17 12:12:07 -08001039 if (resizePending && getBE().compositionInfo.hwc.sidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001040 flags |= eDontUpdateGeometryState;
1041 }
1042 }
1043
Robert Carr7bf247e2017-05-18 14:02:49 -07001044 // Here we apply various requested geometry states, depending on our
1045 // latching configuration. See Layer.h for a detailed discussion of
1046 // how geometry latching is controlled.
1047 if (!(flags & eDontUpdateGeometryState)) {
Pablo Ceballos7d052572016-06-02 17:46:05 -07001048 Layer::State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001049
1050 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1051 // mode, which causes attributes which normally latch regardless of scaling mode,
1052 // to be delayed. We copy the requested state to the active state making sure
1053 // to respect these rules (again see Layer.h for a detailed discussion).
1054 //
1055 // There is an awkward asymmetry in the handling of the crop states in the position
1056 // states, as can be seen below. Largely this arises from position and transform
1057 // being stored in the same data structure while having different latching rules.
1058 // b/38182305
1059 //
1060 // Careful that "c" and editCurrentState may not begin as equivalent due to
1061 // applyPendingStates in the presence of deferred transactions.
1062 if (mFreezeGeometryUpdates) {
Robert Carr82364e32016-05-15 11:27:47 -07001063 float tx = c.active.transform.tx();
1064 float ty = c.active.transform.ty();
1065 c.active = c.requested;
1066 c.active.transform.set(tx, ty);
1067 editCurrentState.active = c.active;
1068 } else {
1069 editCurrentState.active = editCurrentState.requested;
1070 c.active = c.requested;
1071 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001072 }
1073
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001074 if (s.active != c.active) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001075 // invalidate and recompute the visible regions if needed
1076 flags |= Layer::eVisibleRegion;
1077 }
1078
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001079 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001080 // invalidate and recompute the visible regions if needed
1081 flags |= eVisibleRegion;
1082 this->contentDirty = true;
1083
1084 // we may use linear filtering, if the matrix scales us
Robert Carr3dcabfa2016-03-01 18:36:58 -08001085 const uint8_t type = c.active.transform.getType();
David Sodman41fdfc92017-11-06 16:09:56 -08001086 mNeedsFiltering = (!c.active.transform.preserveRects() || (type >= Transform::SCALE));
Mathias Agopian13127d82013-03-05 17:47:11 -08001087 }
1088
Dan Stozac8145172016-04-28 16:29:06 -07001089 // If the layer is hidden, signal and clear out all local sync points so
1090 // that transactions for layers depending on this layer's frames becoming
1091 // visible are not blocked
1092 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001093 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001094 }
1095
Mathias Agopian13127d82013-03-05 17:47:11 -08001096 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001097 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001098 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001099}
1100
Pablo Ceballos05289c22016-04-14 15:49:55 -07001101void Layer::commitTransaction(const State& stateToCommit) {
1102 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001103}
1104
Mathias Agopian13127d82013-03-05 17:47:11 -08001105uint32_t Layer::getTransactionFlags(uint32_t flags) {
1106 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1107}
1108
1109uint32_t Layer::setTransactionFlags(uint32_t flags) {
1110 return android_atomic_or(flags, &mTransactionFlags);
1111}
1112
Robert Carr82364e32016-05-15 11:27:47 -07001113bool Layer::setPosition(float x, float y, bool immediate) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001114 if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001115 return false;
1116 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001117
1118 // We update the requested and active position simultaneously because
1119 // we want to apply the position portion of the transform matrix immediately,
1120 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Robert Carr3dcabfa2016-03-01 18:36:58 -08001121 mCurrentState.requested.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001122 if (immediate && !mFreezeGeometryUpdates) {
1123 // Here we directly update the active state
1124 // unlike other setters, because we store it within
1125 // the transform, but use different latching rules.
1126 // b/38182305
Robert Carr82364e32016-05-15 11:27:47 -07001127 mCurrentState.active.transform.set(x, y);
1128 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001129 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001130
Dan Stoza7dde5992015-05-22 09:51:44 -07001131 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001132 setTransactionFlags(eTransactionNeeded);
1133 return true;
1134}
Robert Carr82364e32016-05-15 11:27:47 -07001135
Robert Carr1f0a16a2016-10-24 16:27:39 -07001136bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1137 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1138 if (idx < 0) {
1139 return false;
1140 }
1141 if (childLayer->setLayer(z)) {
1142 mCurrentChildren.removeAt(idx);
1143 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001144 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001145 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001146 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001147}
1148
Robert Carr503c7042017-09-27 15:06:08 -07001149bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1150 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1151 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1152 if (idx < 0) {
1153 return false;
1154 }
1155 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1156 mCurrentChildren.removeAt(idx);
1157 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001158 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001159 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001160 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001161}
1162
Robert Carrae060832016-11-28 10:51:00 -08001163bool Layer::setLayer(int32_t z) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001164 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001165 mCurrentState.sequence++;
1166 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001167 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001168
1169 // Discard all relative layering.
1170 if (mCurrentState.zOrderRelativeOf != nullptr) {
1171 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1172 if (strongRelative != nullptr) {
1173 strongRelative->removeZOrderRelative(this);
1174 }
1175 mCurrentState.zOrderRelativeOf = nullptr;
1176 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001177 setTransactionFlags(eTransactionNeeded);
1178 return true;
1179}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001180
Robert Carrdb66e622017-04-10 16:55:57 -07001181void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1182 mCurrentState.zOrderRelatives.remove(relative);
1183 mCurrentState.sequence++;
1184 mCurrentState.modified = true;
1185 setTransactionFlags(eTransactionNeeded);
1186}
1187
1188void Layer::addZOrderRelative(const wp<Layer>& relative) {
1189 mCurrentState.zOrderRelatives.add(relative);
1190 mCurrentState.modified = true;
1191 mCurrentState.sequence++;
1192 setTransactionFlags(eTransactionNeeded);
1193}
1194
Robert Carr503d2bd2017-12-04 15:49:47 -08001195bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001196 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1197 if (handle == nullptr) {
1198 return false;
1199 }
1200 sp<Layer> relative = handle->owner.promote();
1201 if (relative == nullptr) {
1202 return false;
1203 }
1204
Robert Carr503d2bd2017-12-04 15:49:47 -08001205 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1206 mCurrentState.zOrderRelativeOf == relative) {
1207 return false;
1208 }
1209
Robert Carrdb66e622017-04-10 16:55:57 -07001210 mCurrentState.sequence++;
1211 mCurrentState.modified = true;
Robert Carr503d2bd2017-12-04 15:49:47 -08001212 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001213
chaviw9ab4bd12017-11-03 13:11:00 -07001214 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
1215 if (oldZOrderRelativeOf != nullptr) {
1216 oldZOrderRelativeOf->removeZOrderRelative(this);
1217 }
Robert Carrdb66e622017-04-10 16:55:57 -07001218 mCurrentState.zOrderRelativeOf = relative;
1219 relative->addZOrderRelative(this);
1220
1221 setTransactionFlags(eTransactionNeeded);
1222
1223 return true;
1224}
1225
Mathias Agopian13127d82013-03-05 17:47:11 -08001226bool Layer::setSize(uint32_t w, uint32_t h) {
David Sodman41fdfc92017-11-06 16:09:56 -08001227 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001228 mCurrentState.requested.w = w;
1229 mCurrentState.requested.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001230 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001231 setTransactionFlags(eTransactionNeeded);
1232 return true;
1233}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001234bool Layer::setAlpha(float alpha) {
David Sodman41fdfc92017-11-06 16:09:56 -08001235 if (mCurrentState.color.a == alpha) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001236 mCurrentState.sequence++;
chaviw13fdc492017-06-27 12:40:18 -07001237 mCurrentState.color.a = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001238 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001239 setTransactionFlags(eTransactionNeeded);
1240 return true;
1241}
chaviw13fdc492017-06-27 12:40:18 -07001242
1243bool Layer::setColor(const half3& color) {
David Sodman41fdfc92017-11-06 16:09:56 -08001244 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g &&
1245 color.b == mCurrentState.color.b)
chaviw13fdc492017-06-27 12:40:18 -07001246 return false;
1247
1248 mCurrentState.sequence++;
1249 mCurrentState.color.r = color.r;
1250 mCurrentState.color.g = color.g;
1251 mCurrentState.color.b = color.b;
1252 mCurrentState.modified = true;
1253 setTransactionFlags(eTransactionNeeded);
1254 return true;
1255}
1256
Robert Carrd4ae7f32018-06-07 16:10:57 -07001257bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1258 bool allowNonRectPreservingTransforms) {
1259 Transform t;
1260 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1261
1262 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1263 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1264 return false;
1265 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001266 mCurrentState.sequence++;
David Sodman41fdfc92017-11-06 16:09:56 -08001267 mCurrentState.requested.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001268 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001269 setTransactionFlags(eTransactionNeeded);
1270 return true;
1271}
1272bool Layer::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001273 mCurrentState.requestedTransparentRegion = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001274 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001275 setTransactionFlags(eTransactionNeeded);
1276 return true;
1277}
1278bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1279 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
David Sodman41fdfc92017-11-06 16:09:56 -08001280 if (mCurrentState.flags == newFlags) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001281 mCurrentState.sequence++;
1282 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001283 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001284 setTransactionFlags(eTransactionNeeded);
1285 return true;
1286}
Robert Carr99e27f02016-06-16 15:18:02 -07001287
1288bool Layer::setCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001289 if (mCurrentState.requestedCrop == crop) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001290 mCurrentState.sequence++;
Robert Carr99e27f02016-06-16 15:18:02 -07001291 mCurrentState.requestedCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001292 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr99e27f02016-06-16 15:18:02 -07001293 mCurrentState.crop = crop;
1294 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001295 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1296
Dan Stoza7dde5992015-05-22 09:51:44 -07001297 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001298 setTransactionFlags(eTransactionNeeded);
1299 return true;
1300}
Robert Carr8d5227b2017-03-16 15:41:03 -07001301
1302bool Layer::setFinalCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001303 if (mCurrentState.requestedFinalCrop == crop) return false;
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001304 mCurrentState.sequence++;
Robert Carr8d5227b2017-03-16 15:41:03 -07001305 mCurrentState.requestedFinalCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001306 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr8d5227b2017-03-16 15:41:03 -07001307 mCurrentState.finalCrop = crop;
1308 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001309 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1310
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001311 mCurrentState.modified = true;
1312 setTransactionFlags(eTransactionNeeded);
1313 return true;
1314}
Mathias Agopian13127d82013-03-05 17:47:11 -08001315
Robert Carrc3574f72016-03-24 12:19:32 -07001316bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001317 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001318 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001319 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001320 return true;
1321}
1322
rongliuccd34842018-03-14 12:26:23 -07001323void Layer::setInfo(int32_t type, int32_t appId) {
David Sodman41fdfc92017-11-06 16:09:56 -08001324 mCurrentState.appId = appId;
1325 mCurrentState.type = type;
1326 mCurrentState.modified = true;
1327 setTransactionFlags(eTransactionNeeded);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001328}
1329
Mathias Agopian13127d82013-03-05 17:47:11 -08001330bool Layer::setLayerStack(uint32_t layerStack) {
David Sodman41fdfc92017-11-06 16:09:56 -08001331 if (mCurrentState.layerStack == layerStack) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001332 mCurrentState.sequence++;
1333 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001334 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001335 setTransactionFlags(eTransactionNeeded);
1336 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001337}
1338
Robert Carr1f0a16a2016-10-24 16:27:39 -07001339uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001340 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001341 if (p == nullptr) {
1342 return getDrawingState().layerStack;
1343 }
1344 return p->getLayerStack();
1345}
1346
David Sodman41fdfc92017-11-06 16:09:56 -08001347void Layer::deferTransactionUntil(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001348 mCurrentState.barrierLayer = barrierLayer;
Dan Stoza7dde5992015-05-22 09:51:44 -07001349 mCurrentState.frameNumber = frameNumber;
1350 // We don't set eTransactionNeeded, because just receiving a deferral
1351 // request without any other state updates shouldn't actually induce a delay
1352 mCurrentState.modified = true;
1353 pushPendingState();
Robert Carr0d480722017-01-10 16:42:54 -08001354 mCurrentState.barrierLayer = nullptr;
Dan Stoza792e5292016-02-11 11:43:58 -08001355 mCurrentState.frameNumber = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001356 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001357}
1358
David Sodman41fdfc92017-11-06 16:09:56 -08001359void Layer::deferTransactionUntil(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001360 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
1361 deferTransactionUntil(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001362}
1363
Dan Stozaee44edd2015-03-23 15:50:23 -07001364
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001365// ----------------------------------------------------------------------------
1366// pageflip handling...
1367// ----------------------------------------------------------------------------
1368
Robert Carr1f0a16a2016-10-24 16:27:39 -07001369bool Layer::isHiddenByPolicy() const {
1370 const Layer::State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001371 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001372 if (parent != nullptr && parent->isHiddenByPolicy()) {
1373 return true;
1374 }
1375 return s.flags & layer_state_t::eLayerHidden;
1376}
1377
David Sodman41fdfc92017-11-06 16:09:56 -08001378uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001379 // TODO: should we do something special if mSecure is set?
1380 if (mProtectedByApp) {
1381 // need a hardware-protected path to external video sink
1382 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001383 }
Riley Andrews03414a12014-07-01 14:22:59 -07001384 if (mPotentialCursor) {
1385 usage |= GraphicBuffer::USAGE_CURSOR;
1386 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001387 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001388 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001389}
1390
Mathias Agopian84300952012-11-21 16:02:13 -08001391void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001392 uint32_t orientation = 0;
1393 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001394 // The transform hint is used to improve performance, but we can
1395 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001396 // apply to all displays.
Mathias Agopian42977342012-08-05 00:40:46 -07001397 const Transform& planeTransform(hw->getTransform());
Mathias Agopian4fec8732012-06-29 14:12:52 -07001398 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07001399 if (orientation & Transform::ROT_INVALID) {
1400 orientation = 0;
1401 }
1402 }
David Sodmaneb085e02017-10-05 18:49:04 -07001403 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001404}
1405
Mathias Agopian13127d82013-03-05 17:47:11 -08001406// ----------------------------------------------------------------------------
1407// debugging
1408// ----------------------------------------------------------------------------
1409
Kalle Raitaa099a242017-01-11 11:17:29 -08001410LayerDebugInfo Layer::getLayerDebugInfo() const {
1411 LayerDebugInfo info;
1412 const Layer::State& ds = getDrawingState();
1413 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001414 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001415 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
1416 info.mType = String8(getTypeId());
1417 info.mTransparentRegion = ds.activeTransparentRegion;
1418 info.mVisibleRegion = visibleRegion;
1419 info.mSurfaceDamageRegion = surfaceDamageRegion;
1420 info.mLayerStack = getLayerStack();
1421 info.mX = ds.active.transform.tx();
1422 info.mY = ds.active.transform.ty();
1423 info.mZ = ds.z;
1424 info.mWidth = ds.active.w;
1425 info.mHeight = ds.active.h;
1426 info.mCrop = ds.crop;
1427 info.mFinalCrop = ds.finalCrop;
chaviw13fdc492017-06-27 12:40:18 -07001428 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001429 info.mFlags = ds.flags;
1430 info.mPixelFormat = getPixelFormat();
Chia-I Wu1b6bafc2018-05-22 12:03:00 -07001431 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Kalle Raitaa099a242017-01-11 11:17:29 -08001432 info.mMatrix[0][0] = ds.active.transform[0][0];
1433 info.mMatrix[0][1] = ds.active.transform[0][1];
1434 info.mMatrix[1][0] = ds.active.transform[1][0];
1435 info.mMatrix[1][1] = ds.active.transform[1][1];
1436 {
David Sodman0cc69182017-11-17 12:12:07 -08001437 sp<const GraphicBuffer> buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001438 if (buffer != 0) {
1439 info.mActiveBufferWidth = buffer->getWidth();
1440 info.mActiveBufferHeight = buffer->getHeight();
1441 info.mActiveBufferStride = buffer->getStride();
1442 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001443 } else {
1444 info.mActiveBufferWidth = 0;
1445 info.mActiveBufferHeight = 0;
1446 info.mActiveBufferStride = 0;
1447 info.mActiveBufferFormat = 0;
1448 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001449 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001450 info.mNumQueuedFrames = getQueuedFrameCount();
1451 info.mRefreshPending = isBufferLatched();
1452 info.mIsOpaque = isOpaque(ds);
1453 info.mContentDirty = contentDirty;
1454 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001455}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001456
Dan Stozae22aec72016-08-01 13:20:59 -07001457void Layer::miniDumpHeader(String8& result) {
1458 result.append("----------------------------------------");
1459 result.append("---------------------------------------\n");
1460 result.append(" Layer name\n");
1461 result.append(" Z | ");
1462 result.append(" Comp Type | ");
1463 result.append(" Disp Frame (LTRB) | ");
1464 result.append(" Source Crop (LTRB)\n");
1465 result.append("----------------------------------------");
1466 result.append("---------------------------------------\n");
1467}
1468
1469void Layer::miniDump(String8& result, int32_t hwcId) const {
David Sodman6f65f3e2017-11-03 14:28:09 -07001470 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stozae22aec72016-08-01 13:20:59 -07001471 return;
1472 }
1473
1474 String8 name;
1475 if (mName.length() > 77) {
1476 std::string shortened;
1477 shortened.append(mName.string(), 36);
1478 shortened.append("[...]");
1479 shortened.append(mName.string() + (mName.length() - 36), 36);
1480 name = shortened.c_str();
1481 } else {
1482 name = mName;
1483 }
1484
1485 result.appendFormat(" %s\n", name.string());
1486
1487 const Layer::State& layerState(getDrawingState());
David Sodman6f65f3e2017-11-03 14:28:09 -07001488 const LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers.at(hwcId);
Chia-I Wu1e043612018-03-01 09:45:09 -08001489 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
1490 result.appendFormat(" rel %6d | ", layerState.z);
1491 } else {
1492 result.appendFormat(" %10d | ", layerState.z);
1493 }
David Sodman41fdfc92017-11-06 16:09:56 -08001494 result.appendFormat("%10s | ", to_string(getCompositionType(hwcId)).c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001495 const Rect& frame = hwcInfo.displayFrame;
David Sodman41fdfc92017-11-06 16:09:56 -08001496 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Dan Stoza5a423ea2017-02-16 14:10:39 -08001497 const FloatRect& crop = hwcInfo.sourceCrop;
David Sodman41fdfc92017-11-06 16:09:56 -08001498 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 -07001499
1500 result.append("- - - - - - - - - - - - - - - - - - - - ");
1501 result.append("- - - - - - - - - - - - - - - - - - - -\n");
1502}
Dan Stozae22aec72016-08-01 13:20:59 -07001503
Svetoslavd85084b2014-03-20 10:28:31 -07001504void Layer::dumpFrameStats(String8& result) const {
1505 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001506}
1507
Svetoslavd85084b2014-03-20 10:28:31 -07001508void Layer::clearFrameStats() {
1509 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001510}
1511
Jamie Gennis6547ff42013-07-16 20:12:42 -07001512void Layer::logFrameStats() {
1513 mFrameTracker.logAndResetStats(mName);
1514}
1515
Svetoslavd85084b2014-03-20 10:28:31 -07001516void Layer::getFrameStats(FrameStats* outStats) const {
1517 mFrameTracker.getStats(outStats);
1518}
1519
Brian Andersond6927fb2016-07-23 23:37:30 -07001520void Layer::dumpFrameEvents(String8& result) {
David Sodman41fdfc92017-11-06 16:09:56 -08001521 result.appendFormat("- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001522 Mutex::Autolock lock(mFrameEventHistoryMutex);
1523 mFrameEventHistory.checkFencesForCompletion();
1524 mFrameEventHistory.dump(result);
1525}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001526
Brian Anderson5ea5e592016-12-01 16:54:33 -08001527void Layer::onDisconnect() {
1528 Mutex::Autolock lock(mFrameEventHistoryMutex);
1529 mFrameEventHistory.onDisconnect();
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001530 mTimeStats.onDisconnect(getName().c_str());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001531}
1532
Brian Anderson3890c392016-07-25 12:48:08 -07001533void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001534 FrameEventHistoryDelta* outDelta) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001535 if (newTimestamps) {
1536 mTimeStats.setPostTime(getName().c_str(), newTimestamps->frameNumber,
1537 newTimestamps->postedTime);
1538 }
1539
Brian Andersond6927fb2016-07-23 23:37:30 -07001540 Mutex::Autolock lock(mFrameEventHistoryMutex);
1541 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001542 // If there are any unsignaled fences in the aquire timeline at this
1543 // point, the previously queued frame hasn't been latched yet. Go ahead
1544 // and try to get the signal time here so the syscall is taken out of
1545 // the main thread's critical path.
1546 mAcquireTimeline.updateSignalTimes();
1547 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001548 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001549 mFrameEventHistory.addQueue(*newTimestamps);
1550 }
1551
Brian Anderson3890c392016-07-25 12:48:08 -07001552 if (outDelta) {
1553 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001554 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001555}
Dan Stozae77c7662016-05-13 11:37:28 -07001556
Chia-I Wu98f1c102017-05-30 14:54:08 -07001557size_t Layer::getChildrenCount() const {
1558 size_t count = 0;
1559 for (const sp<Layer>& child : mCurrentChildren) {
1560 count += 1 + child->getChildrenCount();
1561 }
1562 return count;
1563}
1564
Robert Carr1f0a16a2016-10-24 16:27:39 -07001565void Layer::addChild(const sp<Layer>& layer) {
1566 mCurrentChildren.add(layer);
1567 layer->setParent(this);
1568}
1569
1570ssize_t Layer::removeChild(const sp<Layer>& layer) {
1571 layer->setParent(nullptr);
1572 return mCurrentChildren.remove(layer);
1573}
1574
Robert Carr1db73f62016-12-21 12:58:51 -08001575bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1576 sp<Handle> handle = nullptr;
1577 sp<Layer> newParent = nullptr;
1578 if (newParentHandle == nullptr) {
1579 return false;
1580 }
1581 handle = static_cast<Handle*>(newParentHandle.get());
1582 newParent = handle->owner.promote();
1583 if (newParent == nullptr) {
1584 ALOGE("Unable to promote Layer handle");
1585 return false;
1586 }
1587
1588 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001589 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001590
1591 sp<Client> client(child->mClientRef.promote());
1592 if (client != nullptr) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001593 client->updateParent(newParent);
Robert Carr1db73f62016-12-21 12:58:51 -08001594 }
1595 }
1596 mCurrentChildren.clear();
1597
1598 return true;
1599}
1600
Robert Carr15eae092018-03-23 13:43:53 -07001601void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001602 for (const sp<Layer>& child : mDrawingChildren) {
1603 child->mDrawingParent = newParent;
1604 }
1605}
1606
chaviwf1961f72017-09-18 16:41:07 -07001607bool Layer::reparent(const sp<IBinder>& newParentHandle) {
1608 if (newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001609 return false;
1610 }
1611
1612 auto handle = static_cast<Handle*>(newParentHandle.get());
1613 sp<Layer> newParent = handle->owner.promote();
1614 if (newParent == nullptr) {
1615 ALOGE("Unable to promote Layer handle");
1616 return false;
1617 }
1618
chaviwf1961f72017-09-18 16:41:07 -07001619 sp<Layer> parent = getParent();
1620 if (parent != nullptr) {
1621 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001622 }
chaviwf1961f72017-09-18 16:41:07 -07001623 newParent->addChild(this);
chaviw06178942017-07-27 10:25:59 -07001624
chaviwf1961f72017-09-18 16:41:07 -07001625 sp<Client> client(mClientRef.promote());
chaviw06178942017-07-27 10:25:59 -07001626 sp<Client> newParentClient(newParent->mClientRef.promote());
1627
chaviwf1961f72017-09-18 16:41:07 -07001628 if (client != newParentClient) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001629 client->updateParent(newParent);
chaviw06178942017-07-27 10:25:59 -07001630 }
1631
chaviw06178942017-07-27 10:25:59 -07001632 return true;
1633}
1634
Robert Carr9524cb32017-02-13 11:32:32 -08001635bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001636 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001637 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001638 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001639 if (client != nullptr && parentClient != client) {
Robert Carr7f619b22017-11-06 12:56:35 -08001640 client->detachLayer(child.get());
1641 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001642 }
Robert Carr7f619b22017-11-06 12:56:35 -08001643 }
Robert Carr9524cb32017-02-13 11:32:32 -08001644
1645 return true;
1646}
1647
Chia-I Wu11481472018-05-04 10:43:19 -07001648bool Layer::isLegacyDataSpace() const {
1649 // return true when no higher bits are set
Chia-I Wu1b6bafc2018-05-22 12:03:00 -07001650 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001651 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001652}
1653
Robert Carr1f0a16a2016-10-24 16:27:39 -07001654void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001655 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001656}
1657
1658void Layer::clearSyncPoints() {
1659 for (const auto& child : mCurrentChildren) {
1660 child->clearSyncPoints();
1661 }
1662
1663 Mutex::Autolock lock(mLocalSyncPointMutex);
1664 for (auto& point : mLocalSyncPoints) {
1665 point->setFrameAvailable();
1666 }
1667 mLocalSyncPoints.clear();
1668}
1669
1670int32_t Layer::getZ() const {
1671 return mDrawingState.z;
1672}
1673
Robert Carr29abff82017-12-04 13:51:20 -08001674bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1675 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1676 const State& state = useDrawing ? mDrawingState : mCurrentState;
1677 return state.zOrderRelativeOf != nullptr;
1678}
1679
David Sodman41fdfc92017-11-06 16:09:56 -08001680__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001681 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001682 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1683 "makeTraversalList received invalid stateSet");
1684 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1685 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1686 const State& state = useDrawing ? mDrawingState : mCurrentState;
1687
Robert Carr29abff82017-12-04 13:51:20 -08001688 if (state.zOrderRelatives.size() == 0) {
1689 *outSkipRelativeZUsers = true;
1690 return children;
1691 }
1692
chaviw738df022018-05-31 16:11:27 -07001693 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001694 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001695 sp<Layer> strongRelative = weakRelative.promote();
1696 if (strongRelative != nullptr) {
1697 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001698 }
1699 }
1700
Dan Stoza412903f2017-04-27 13:42:17 -07001701 for (const sp<Layer>& child : children) {
Robert Carr503c7042017-09-27 15:06:08 -07001702 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1703 if (childState.zOrderRelativeOf != nullptr) {
1704 continue;
1705 }
Robert Carrdb66e622017-04-10 16:55:57 -07001706 traverse.add(child);
1707 }
1708
1709 return traverse;
1710}
1711
Robert Carr1f0a16a2016-10-24 16:27:39 -07001712/**
Robert Carrdb66e622017-04-10 16:55:57 -07001713 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001714 */
Dan Stoza412903f2017-04-27 13:42:17 -07001715void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001716 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1717 // produce a new list for traversing, including our relatives, and not including our children
1718 // who are relatives of another surface. In the case that there are no relative Z,
1719 // makeTraversalList returns our children directly to avoid significant overhead.
1720 // However in this case we need to take the responsibility for filtering children which
1721 // are relatives of another surface here.
1722 bool skipRelativeZUsers = false;
1723 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001724
Robert Carr1f0a16a2016-10-24 16:27:39 -07001725 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001726 for (; i < list.size(); i++) {
1727 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001728 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1729 continue;
1730 }
1731
Robert Carrdb66e622017-04-10 16:55:57 -07001732 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001733 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001734 }
Dan Stoza412903f2017-04-27 13:42:17 -07001735 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001736 }
Robert Carr29abff82017-12-04 13:51:20 -08001737
Dan Stoza412903f2017-04-27 13:42:17 -07001738 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001739 for (; i < list.size(); i++) {
1740 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001741
1742 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1743 continue;
1744 }
Dan Stoza412903f2017-04-27 13:42:17 -07001745 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001746 }
1747}
1748
1749/**
Robert Carrdb66e622017-04-10 16:55:57 -07001750 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001751 */
Dan Stoza412903f2017-04-27 13:42:17 -07001752void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1753 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001754 // See traverseInZOrder for documentation.
1755 bool skipRelativeZUsers = false;
1756 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001757
Robert Carr1f0a16a2016-10-24 16:27:39 -07001758 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001759 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001760 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001761
1762 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1763 continue;
1764 }
1765
Robert Carrdb66e622017-04-10 16:55:57 -07001766 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001767 break;
1768 }
Dan Stoza412903f2017-04-27 13:42:17 -07001769 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001770 }
Dan Stoza412903f2017-04-27 13:42:17 -07001771 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001772 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001773 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001774
1775 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1776 continue;
1777 }
1778
Dan Stoza412903f2017-04-27 13:42:17 -07001779 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001780 }
1781}
1782
chaviw4b129c22018-04-09 16:19:43 -07001783LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1784 const std::vector<Layer*>& layersInTree) {
1785 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1786 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001787 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1788 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
chaviw4b129c22018-04-09 16:19:43 -07001789 const State& state = useDrawing ? mDrawingState : mCurrentState;
1790
chaviw738df022018-05-31 16:11:27 -07001791 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001792 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1793 sp<Layer> strongRelative = weakRelative.promote();
1794 // Only add relative layers that are also descendents of the top most parent of the tree.
1795 // If a relative layer is not a descendent, then it should be ignored.
1796 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1797 traverse.add(strongRelative);
1798 }
1799 }
1800
1801 for (const sp<Layer>& child : children) {
1802 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1803 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1804 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1805 // the child here since it won't be added later as a relative.
1806 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1807 childState.zOrderRelativeOf.promote().get())) {
1808 continue;
1809 }
1810 traverse.add(child);
1811 }
1812
1813 return traverse;
1814}
1815
1816void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1817 LayerVector::StateSet stateSet,
1818 const LayerVector::Visitor& visitor) {
1819 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001820
1821 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001822 for (; i < list.size(); i++) {
1823 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001824 if (relative->getZ() >= 0) {
1825 break;
1826 }
chaviw4b129c22018-04-09 16:19:43 -07001827 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001828 }
chaviw4b129c22018-04-09 16:19:43 -07001829
chaviwa76b2712017-09-20 12:02:26 -07001830 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001831 for (; i < list.size(); i++) {
1832 const auto& relative = list[i];
1833 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001834 }
1835}
1836
chaviw4b129c22018-04-09 16:19:43 -07001837std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1838 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1839 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1840
1841 std::vector<Layer*> layersInTree = {this};
1842 for (size_t i = 0; i < children.size(); i++) {
1843 const auto& child = children[i];
1844 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1845 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1846 }
1847
1848 return layersInTree;
1849}
1850
1851void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1852 const LayerVector::Visitor& visitor) {
1853 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1854 std::sort(layersInTree.begin(), layersInTree.end());
1855 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1856}
1857
Robert Carr1f0a16a2016-10-24 16:27:39 -07001858Transform Layer::getTransform() const {
1859 Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001860 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001861 if (p != nullptr) {
1862 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07001863
1864 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
1865 // it isFixedSize) then there may be additional scaling not accounted
1866 // for in the transform. We need to mirror this scaling in child surfaces
1867 // or we will break the contract where WM can treat child surfaces as
1868 // pixels in the parent surface.
David Sodman0cc69182017-11-17 12:12:07 -08001869 if (p->isFixedSize() && p->getBE().compositionInfo.mBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07001870 int bufferWidth;
1871 int bufferHeight;
1872 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
David Sodman0cc69182017-11-17 12:12:07 -08001873 bufferWidth = p->getBE().compositionInfo.mBuffer->getWidth();
1874 bufferHeight = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001875 } else {
David Sodman0cc69182017-11-17 12:12:07 -08001876 bufferHeight = p->getBE().compositionInfo.mBuffer->getWidth();
1877 bufferWidth = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001878 }
David Sodman41fdfc92017-11-06 16:09:56 -08001879 float sx = p->getDrawingState().active.w / static_cast<float>(bufferWidth);
1880 float sy = p->getDrawingState().active.h / static_cast<float>(bufferHeight);
Robert Carr9b429f42017-04-17 14:56:57 -07001881 Transform extraParentScaling;
1882 extraParentScaling.set(sx, 0, 0, sy);
1883 t = t * extraParentScaling;
1884 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001885 }
1886 return t * getDrawingState().active.transform;
1887}
1888
chaviw13fdc492017-06-27 12:40:18 -07001889half Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001890 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07001891
chaviw13fdc492017-06-27 12:40:18 -07001892 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1893 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001894}
Robert Carr6452f122017-03-21 10:41:29 -07001895
chaviw13fdc492017-06-27 12:40:18 -07001896half4 Layer::getColor() const {
1897 const half4 color(getDrawingState().color);
1898 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001899}
Robert Carr6452f122017-03-21 10:41:29 -07001900
Robert Carr1f0a16a2016-10-24 16:27:39 -07001901void Layer::commitChildList() {
1902 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1903 const auto& child = mCurrentChildren[i];
1904 child->commitChildList();
1905 }
1906 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001907 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001908}
1909
chaviw1d044282017-09-27 12:19:28 -07001910void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
1911 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1912 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1913 const State& state = useDrawing ? mDrawingState : mCurrentState;
1914
1915 Transform requestedTransform = state.active.transform;
1916 Transform transform = getTransform();
1917
1918 layerInfo->set_id(sequence);
1919 layerInfo->set_name(getName().c_str());
1920 layerInfo->set_type(String8(getTypeId()));
1921
1922 for (const auto& child : children) {
1923 layerInfo->add_children(child->sequence);
1924 }
1925
1926 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1927 sp<Layer> strongRelative = weakRelative.promote();
1928 if (strongRelative != nullptr) {
1929 layerInfo->add_relatives(strongRelative->sequence);
1930 }
1931 }
1932
1933 LayerProtoHelper::writeToProto(state.activeTransparentRegion,
1934 layerInfo->mutable_transparent_region());
1935 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
1936 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
1937
1938 layerInfo->set_layer_stack(getLayerStack());
1939 layerInfo->set_z(state.z);
1940
1941 PositionProto* position = layerInfo->mutable_position();
1942 position->set_x(transform.tx());
1943 position->set_y(transform.ty());
1944
1945 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
1946 requestedPosition->set_x(requestedTransform.tx());
1947 requestedPosition->set_y(requestedTransform.ty());
1948
1949 SizeProto* size = layerInfo->mutable_size();
1950 size->set_w(state.active.w);
1951 size->set_h(state.active.h);
1952
1953 LayerProtoHelper::writeToProto(state.crop, layerInfo->mutable_crop());
1954 LayerProtoHelper::writeToProto(state.finalCrop, layerInfo->mutable_final_crop());
1955
1956 layerInfo->set_is_opaque(isOpaque(state));
1957 layerInfo->set_invalidate(contentDirty);
Chia-I Wu1b6bafc2018-05-22 12:03:00 -07001958
1959 // XXX (b/79210409) mCurrentDataSpace is not protected
1960 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
1961
chaviw1d044282017-09-27 12:19:28 -07001962 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1963 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
1964 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
1965 layerInfo->set_flags(state.flags);
1966
1967 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1968 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
1969
Jorim Jaggi8e0af362017-11-14 16:28:28 +01001970 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07001971 if (parent != nullptr) {
1972 layerInfo->set_parent(parent->sequence);
1973 }
1974
1975 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1976 if (zOrderRelativeOf != nullptr) {
1977 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1978 }
1979
Chia-I Wu1b6bafc2018-05-22 12:03:00 -07001980 // XXX getBE().compositionInfo.mBuffer is not protected
David Sodman0cc69182017-11-17 12:12:07 -08001981 auto buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001982 if (buffer != nullptr) {
1983 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
chaviw1d044282017-09-27 12:19:28 -07001984 }
1985
1986 layerInfo->set_queued_frames(getQueuedFrameCount());
1987 layerInfo->set_refresh_pending(isBufferLatched());
rongliuccd34842018-03-14 12:26:23 -07001988 layerInfo->set_window_type(state.type);
1989 layerInfo->set_app_id(state.appId);
chaviw1d044282017-09-27 12:19:28 -07001990}
1991
Yiwei Zhang068e31b2018-02-21 13:02:45 -08001992void Layer::writeToProto(LayerProto* layerInfo, int32_t hwcId) {
1993 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
1994
1995 const auto& hwcInfo = getBE().mHwcLayers.at(hwcId);
1996
1997 const Rect& frame = hwcInfo.displayFrame;
1998 LayerProtoHelper::writeToProto(frame, layerInfo->mutable_hwc_frame());
1999
2000 const FloatRect& crop = hwcInfo.sourceCrop;
2001 LayerProtoHelper::writeToProto(crop, layerInfo->mutable_hwc_crop());
2002
2003 const int32_t transform = static_cast<int32_t>(hwcInfo.transform);
2004 layerInfo->set_hwc_transform(transform);
Yiwei Zhang7c64f172018-03-07 14:52:28 -08002005
2006 const int32_t compositionType = static_cast<int32_t>(hwcInfo.compositionType);
2007 layerInfo->set_hwc_composition_type(compositionType);
2008
2009 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
2010 static_cast<BufferLayer*>(this)->isProtected()) {
2011 layerInfo->set_is_protected(true);
2012 } else {
2013 layerInfo->set_is_protected(false);
2014 }
Yiwei Zhang068e31b2018-02-21 13:02:45 -08002015}
2016
Mathias Agopian13127d82013-03-05 17:47:11 -08002017// ---------------------------------------------------------------------------
2018
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002019}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002020
2021#if defined(__gl_h_)
2022#error "don't include gl/gl.h in this file"
2023#endif
2024
2025#if defined(__gl2_h_)
2026#error "don't include gl2/gl2.h in this file"
2027#endif