blob: 90e0e9e0d79625186e39ef18d0a34849f93baf51 [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
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800133 CompositorTiming compositorTiming;
134 flinger->getCompositorTiming(&compositorTiming);
135 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jamie Gennise8696a42012-01-15 18:54:57 -0800136}
137
Dan Stoza9fdb7e02018-06-21 12:10:12 -0700138void Layer::onFirstRef() NO_THREAD_SAFETY_ANALYSIS {
139 if (!isCreatedFromMainThread()) {
140 // Grab the SF state lock during this since it's the only way to safely access HWC
141 mFlinger->mStateLock.lock();
142 }
143
144 const auto& hwc = mFlinger->getHwComposer();
145 const auto& activeConfig = hwc.getActiveConfig(HWC_DISPLAY_PRIMARY);
146 nsecs_t displayPeriod = activeConfig->getVsyncPeriod();
147 mFrameTracker.setDisplayRefreshPeriod(displayPeriod);
148
149 if (!isCreatedFromMainThread()) {
150 mFlinger->mStateLock.unlock();
151 }
152}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700153
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700154Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800155 sp<Client> c(mClientRef.promote());
156 if (c != 0) {
157 c->detachLayer(this);
158 }
159
160 for (auto& point : mRemoteSyncPoints) {
161 point->setTransactionApplied();
162 }
163 for (auto& point : mLocalSyncPoints) {
164 point->setFrameAvailable();
165 }
Jamie Gennis6547ff42013-07-16 20:12:42 -0700166 mFrameTracker.logAndResetStats(mName);
Mathias Agopian96f08192010-06-02 23:28:45 -0700167}
168
Mathias Agopian13127d82013-03-05 17:47:11 -0800169// ---------------------------------------------------------------------------
170// callbacks
171// ---------------------------------------------------------------------------
172
David Sodmaneb085e02017-10-05 18:49:04 -0700173/*
174 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
175 * Layer. So, the implementation is done in BufferLayer. When called on a
176 * ColorLayer object, it's essentially a NOP.
177 */
David Sodmaneb085e02017-10-05 18:49:04 -0700178void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800179
Chia-I Wuc6657022017-08-15 11:18:17 -0700180void Layer::onRemovedFromCurrentState() {
181 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
182
chaviw8b3871a2017-11-01 17:41:01 -0700183 mPendingRemoval = true;
184
Robert Carr5edb1ad2017-04-25 10:54:24 -0700185 if (mCurrentState.zOrderRelativeOf != nullptr) {
186 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
187 if (strongRelative != nullptr) {
188 strongRelative->removeZOrderRelative(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700189 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700190 }
191 mCurrentState.zOrderRelativeOf = nullptr;
192 }
193
Chia-I Wuc6657022017-08-15 11:18:17 -0700194 for (const auto& child : mCurrentChildren) {
195 child->onRemovedFromCurrentState();
196 }
197}
Chia-I Wu38512252017-05-17 14:36:16 -0700198
Chia-I Wuc6657022017-08-15 11:18:17 -0700199void Layer::onRemoved() {
200 // the layer is removed from SF mLayersPendingRemoval
David Sodmaneb085e02017-10-05 18:49:04 -0700201 abandon();
Chia-I Wuc6657022017-08-15 11:18:17 -0700202
Steven Thomasb02664d2017-07-26 18:48:28 -0700203 destroyAllHwcLayers();
Chia-I Wu38512252017-05-17 14:36:16 -0700204
Robert Carr1f0a16a2016-10-24 16:27:39 -0700205 for (const auto& child : mCurrentChildren) {
206 child->onRemoved();
207 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700208}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700209
Mathias Agopian13127d82013-03-05 17:47:11 -0800210// ---------------------------------------------------------------------------
211// set-up
212// ---------------------------------------------------------------------------
213
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700214const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800215 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800216}
217
chaviw13fdc492017-06-27 12:40:18 -0700218bool Layer::getPremultipledAlpha() const {
219 return mPremultipliedAlpha;
220}
221
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700222sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800223 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700224 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800225}
226
227// ---------------------------------------------------------------------------
228// h/w composer set-up
229// ---------------------------------------------------------------------------
230
Steven Thomasb02664d2017-07-26 18:48:28 -0700231bool Layer::createHwcLayer(HWComposer* hwc, int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700232 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.count(hwcId) != 0,
David Sodman9eeae692017-11-02 10:53:32 -0700233 "Already have a layer for hwcId %d", hwcId);
Steven Thomasb02664d2017-07-26 18:48:28 -0700234 HWC2::Layer* layer = hwc->createLayer(hwcId);
235 if (!layer) {
236 return false;
237 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700238 LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers[hwcId];
Steven Thomasb02664d2017-07-26 18:48:28 -0700239 hwcInfo.hwc = hwc;
240 hwcInfo.layer = layer;
241 layer->setLayerDestroyedListener(
David Sodman6f65f3e2017-11-03 14:28:09 -0700242 [this, hwcId](HWC2::Layer* /*layer*/) { getBE().mHwcLayers.erase(hwcId); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700243 return true;
244}
245
Chia-I Wu83806892017-11-16 10:50:20 -0800246bool Layer::destroyHwcLayer(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700247 if (getBE().mHwcLayers.count(hwcId) == 0) {
Chia-I Wu83806892017-11-16 10:50:20 -0800248 return false;
Steven Thomasb02664d2017-07-26 18:48:28 -0700249 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700250 auto& hwcInfo = getBE().mHwcLayers[hwcId];
David Sodman41fdfc92017-11-06 16:09:56 -0800251 LOG_ALWAYS_FATAL_IF(hwcInfo.layer == nullptr, "Attempt to destroy null layer");
Steven Thomasb02664d2017-07-26 18:48:28 -0700252 LOG_ALWAYS_FATAL_IF(hwcInfo.hwc == nullptr, "Missing HWComposer");
253 hwcInfo.hwc->destroyLayer(hwcId, hwcInfo.layer);
254 // The layer destroyed listener should have cleared the entry from
255 // mHwcLayers. Verify that.
David Sodman6f65f3e2017-11-03 14:28:09 -0700256 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.count(hwcId) != 0,
257 "Stale layer entry in getBE().mHwcLayers");
Chia-I Wu83806892017-11-16 10:50:20 -0800258 return true;
Steven Thomasb02664d2017-07-26 18:48:28 -0700259}
260
261void Layer::destroyAllHwcLayers() {
David Sodman6f65f3e2017-11-03 14:28:09 -0700262 size_t numLayers = getBE().mHwcLayers.size();
Steven Thomasb02664d2017-07-26 18:48:28 -0700263 for (size_t i = 0; i < numLayers; ++i) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700264 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.empty(), "destroyAllHwcLayers failed");
265 destroyHwcLayer(getBE().mHwcLayers.begin()->first);
Steven Thomasb02664d2017-07-26 18:48:28 -0700266 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700267 LOG_ALWAYS_FATAL_IF(!getBE().mHwcLayers.empty(),
David Sodman41fdfc92017-11-06 16:09:56 -0800268 "All hardware composer layers should have been destroyed");
Steven Thomasb02664d2017-07-26 18:48:28 -0700269}
Steven Thomasb02664d2017-07-26 18:48:28 -0700270
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800271Rect Layer::getContentCrop() const {
272 // this is the crop rectangle that applies to the buffer
273 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700274 Rect crop;
275 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800276 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700277 crop = mCurrentCrop;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800278 } else if (getBE().compositionInfo.mBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800279 // otherwise we use the whole buffer
David Sodman0cc69182017-11-17 12:12:07 -0800280 crop = getBE().compositionInfo.mBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700281 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800282 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700283 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700284 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700285 return crop;
286}
287
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700288static Rect reduce(const Rect& win, const Region& exclude) {
289 if (CC_LIKELY(exclude.isEmpty())) {
290 return win;
291 }
292 if (exclude.isRect()) {
293 return win.reduce(exclude.getBounds());
294 }
295 return Region(win).subtract(exclude).getBounds();
296}
297
Dan Stoza80d61162017-12-20 15:57:52 -0800298static FloatRect reduce(const FloatRect& win, const Region& exclude) {
299 if (CC_LIKELY(exclude.isEmpty())) {
300 return win;
301 }
302 // Convert through Rect (by rounding) for lack of FloatRegion
303 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
304}
305
Robert Carr1f0a16a2016-10-24 16:27:39 -0700306Rect Layer::computeScreenBounds(bool reduceTransparentRegion) const {
307 const Layer::State& s(getDrawingState());
308 Rect win(s.active.w, s.active.h);
309
310 if (!s.crop.isEmpty()) {
311 win.intersect(s.crop, &win);
312 }
313
314 Transform t = getTransform();
315 win = t.transform(win);
316
Robert Carr41b08b52017-06-01 16:11:34 -0700317 if (!s.finalCrop.isEmpty()) {
318 win.intersect(s.finalCrop, &win);
319 }
320
Chia-I Wue41dbe62017-06-13 14:10:56 -0700321 const sp<Layer>& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700322 // Now we need to calculate the parent bounds, so we can clip ourselves to those.
323 // When calculating the parent bounds for purposes of clipping,
324 // we don't need to constrain the parent to its transparent region.
325 // The transparent region is an optimization based on the
326 // buffer contents of the layer, but does not affect the space allocated to
327 // it by policy, and thus children should be allowed to extend into the
328 // parent's transparent region. In fact one of the main uses, is to reduce
329 // buffer allocation size in cases where a child window sits behind a main window
330 // (by marking the hole in the parent window as a transparent region)
331 if (p != nullptr) {
332 Rect bounds = p->computeScreenBounds(false);
333 bounds.intersect(win, &win);
334 }
335
336 if (reduceTransparentRegion) {
337 auto const screenTransparentRegion = t.transform(s.activeTransparentRegion);
338 win = reduce(win, screenTransparentRegion);
339 }
340
341 return win;
342}
343
Dan Stoza80d61162017-12-20 15:57:52 -0800344FloatRect Layer::computeBounds() const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700345 const Layer::State& s(getDrawingState());
Michael Lentine6c925ed2014-09-26 17:55:01 -0700346 return computeBounds(s.activeTransparentRegion);
347}
348
Dan Stoza80d61162017-12-20 15:57:52 -0800349FloatRect Layer::computeBounds(const Region& activeTransparentRegion) const {
Michael Lentine6c925ed2014-09-26 17:55:01 -0700350 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800351 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700352
353 if (!s.crop.isEmpty()) {
354 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800355 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700356
Chia-I Wue41dbe62017-06-13 14:10:56 -0700357 const auto& p = mDrawingParent.promote();
Robert Carrd4ae7f32018-06-07 16:10:57 -0700358 FloatRect floatWin = win.toFloatRect();
359 FloatRect parentBounds = floatWin;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700360 if (p != nullptr) {
Robert Carrd4ae7f32018-06-07 16:10:57 -0700361 // We pass an empty Region here for reasons mirroring that of the case described in
362 // the computeScreenBounds reduceTransparentRegion=false case.
363 parentBounds = p->computeBounds(Region());
Robert Carr1f0a16a2016-10-24 16:27:39 -0700364 }
365
Robert Carrd4ae7f32018-06-07 16:10:57 -0700366 Transform t = s.active.transform;
Dan Stoza80d61162017-12-20 15:57:52 -0800367
Robert Carrd4ae7f32018-06-07 16:10:57 -0700368
369 if (p != nullptr || !s.finalCrop.isEmpty()) {
Dan Stoza80d61162017-12-20 15:57:52 -0800370 floatWin = t.transform(floatWin);
Robert Carrd4ae7f32018-06-07 16:10:57 -0700371 floatWin = floatWin.intersect(parentBounds);
372
373 if (!s.finalCrop.isEmpty()) {
374 floatWin = floatWin.intersect(s.finalCrop.toFloatRect());
375 }
Dan Stoza80d61162017-12-20 15:57:52 -0800376 floatWin = t.inverse().transform(floatWin);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700377 }
378
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700379 // subtract the transparent region and snap to the bounds
Dan Stoza80d61162017-12-20 15:57:52 -0800380 return reduce(floatWin, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800381}
382
Robert Carr1f0a16a2016-10-24 16:27:39 -0700383Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& hw) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700384 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800385 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700386 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800387
388 // apply the projection's clipping to the window crop in
389 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700390 // if there are no window scaling involved, this operation will map to full
391 // pixels in the buffer.
392 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
393 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700394
395 Rect activeCrop(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700396 if (!s.crop.isEmpty()) {
Chia-I Wudf7867f2017-07-20 14:24:37 -0700397 activeCrop.intersect(s.crop, &activeCrop);
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700398 }
399
Robert Carr1f0a16a2016-10-24 16:27:39 -0700400 Transform t = getTransform();
401 activeCrop = t.transform(activeCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000402 if (!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
403 activeCrop.clear();
404 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700405 if (!s.finalCrop.isEmpty()) {
David Sodman41fdfc92017-11-06 16:09:56 -0800406 if (!activeCrop.intersect(s.finalCrop, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000407 activeCrop.clear();
408 }
409 }
chaviwb1154d12017-10-31 14:15:36 -0700410
411 const auto& p = mDrawingParent.promote();
412 if (p != nullptr) {
413 auto parentCrop = p->computeInitialCrop(hw);
414 activeCrop.intersect(parentCrop, &activeCrop);
415 }
416
Robert Carr1f0a16a2016-10-24 16:27:39 -0700417 return activeCrop;
418}
419
Dan Stoza5a423ea2017-02-16 14:10:39 -0800420FloatRect Layer::computeCrop(const sp<const DisplayDevice>& hw) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700421 // the content crop is the area of the content that gets scaled to the
422 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800423 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700424
425 // In addition there is a WM-specified crop we pull from our drawing state.
426 const State& s(getDrawingState());
427
428 // Screen space to make reduction to parent crop clearer.
429 Rect activeCrop = computeInitialCrop(hw);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700430 Transform t = getTransform();
431 // Back to layer space to work with the content crop.
432 activeCrop = t.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800433
Michael Lentine28ea2172014-11-19 18:32:37 -0800434 // This needs to be here as transform.transform(Rect) computes the
435 // transformed rect and then takes the bounding box of the result before
436 // returning. This means
437 // transform.inverse().transform(transform.transform(Rect)) != Rect
438 // in which case we need to make sure the final rect is clipped to the
439 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000440 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
441 activeCrop.clear();
442 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800443
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700444 // subtract the transparent region and snap to the bounds
445 activeCrop = reduce(activeCrop, s.activeTransparentRegion);
446
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000447 // Transform the window crop to match the buffer coordinate system,
448 // which means using the inverse of the current transform set on the
449 // SurfaceFlingerConsumer.
450 uint32_t invTransform = mCurrentTransform;
Robert Carrcae605c2017-03-29 12:10:31 -0700451 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000452 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700453 * the code below applies the primary display's inverse transform to the
454 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000455 */
David Sodman41fdfc92017-11-06 16:09:56 -0800456 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000457 // calculate the inverse transform
458 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800459 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800460 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000461 // and apply to the current transform
David Sodman41fdfc92017-11-06 16:09:56 -0800462 invTransform = (Transform(invTransformOrient) * Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800463 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000464
465 int winWidth = s.active.w;
466 int winHeight = s.active.h;
467 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
468 // If the activeCrop has been rotate the ends are rotated but not
469 // the space itself so when transforming ends back we can't rely on
470 // a modification of the axes of rotation. To account for this we
471 // need to reorient the inverse rotation in terms of the current
472 // axes of rotation.
473 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
474 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
475 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800476 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000477 }
478 winWidth = s.active.h;
479 winHeight = s.active.w;
480 }
David Sodman41fdfc92017-11-06 16:09:56 -0800481 const Rect winCrop = activeCrop.transform(invTransform, s.active.w, s.active.h);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000482
483 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800484 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000485 float yScale = crop.getHeight() / float(winHeight);
486
David Sodman41fdfc92017-11-06 16:09:56 -0800487 float insetL = winCrop.left * xScale;
488 float insetT = winCrop.top * yScale;
489 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000490 float insetB = (winHeight - winCrop.bottom) * yScale;
491
David Sodman41fdfc92017-11-06 16:09:56 -0800492 crop.left += insetL;
493 crop.top += insetT;
494 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000495 crop.bottom -= insetB;
496
Mathias Agopian13127d82013-03-05 17:47:11 -0800497 return crop;
498}
499
Robert Carrae060832016-11-28 10:51:00 -0800500void Layer::setGeometry(const sp<const DisplayDevice>& displayDevice, uint32_t z)
Mathias Agopiana350ff92010-08-10 17:14:02 -0700501{
Dan Stoza9e56aa02015-11-02 13:00:03 -0800502 const auto hwcId = displayDevice->getHwcDisplayId();
Peiyong Lin0874d2b2018-09-04 09:39:34 -0700503 if (!hasHwcLayer(hwcId)) {
504 return;
505 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700506 auto& hwcInfo = getBE().mHwcLayers[hwcId];
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700507
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700508 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800509 hwcInfo.forceClientComposition = false;
510
511 if (isSecure() && !displayDevice->isSecure()) {
512 hwcInfo.forceClientComposition = true;
513 }
514
515 auto& hwcLayer = hwcInfo.layer;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700516
Mathias Agopian13127d82013-03-05 17:47:11 -0800517 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700518 const State& s(getDrawingState());
David Revemanecf0fa52017-03-03 11:32:44 -0500519 auto blendMode = HWC2::BlendMode::None;
Robert Carr6452f122017-03-21 10:41:29 -0700520 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800521 blendMode =
522 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800523 }
David Revemanecf0fa52017-03-03 11:32:44 -0500524 auto error = hwcLayer->setBlendMode(blendMode);
David Sodman41fdfc92017-11-06 16:09:56 -0800525 ALOGE_IF(error != HWC2::Error::None,
526 "[%s] Failed to set blend mode %s:"
527 " %s (%d)",
528 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
529 static_cast<int32_t>(error));
Mathias Agopian13127d82013-03-05 17:47:11 -0800530
531 // apply the layer's transform, followed by the display's global transform
532 // here we're guaranteed that the layer's transform preserves rects
Michael Lentine6c925ed2014-09-26 17:55:01 -0700533 Region activeTransparentRegion(s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700534 Transform t = getTransform();
Robert Carrb5d3d262016-03-25 15:08:13 -0700535 if (!s.crop.isEmpty()) {
536 Rect activeCrop(s.crop);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700537 activeCrop = t.transform(activeCrop);
David Sodman41fdfc92017-11-06 16:09:56 -0800538 if (!activeCrop.intersect(displayDevice->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000539 activeCrop.clear();
540 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700541 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800542 // This needs to be here as transform.transform(Rect) computes the
543 // transformed rect and then takes the bounding box of the result before
544 // returning. This means
545 // transform.inverse().transform(transform.transform(Rect)) != Rect
546 // in which case we need to make sure the final rect is clipped to the
547 // display bounds.
David Sodman41fdfc92017-11-06 16:09:56 -0800548 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000549 activeCrop.clear();
550 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700551 // mark regions outside the crop as transparent
552 activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top));
David Sodman41fdfc92017-11-06 16:09:56 -0800553 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom, s.active.w, s.active.h));
554 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
555 activeTransparentRegion.orSelf(
556 Rect(activeCrop.right, activeCrop.top, s.active.w, activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700557 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700558
Dan Stoza80d61162017-12-20 15:57:52 -0800559 // computeBounds returns a FloatRect to provide more accuracy during the
560 // transformation. We then round upon constructing 'frame'.
561 Rect frame{t.transform(computeBounds(activeTransparentRegion))};
Robert Carrb5d3d262016-03-25 15:08:13 -0700562 if (!s.finalCrop.isEmpty()) {
David Sodman41fdfc92017-11-06 16:09:56 -0800563 if (!frame.intersect(s.finalCrop, &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000564 frame.clear();
565 }
566 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000567 if (!frame.intersect(displayDevice->getViewport(), &frame)) {
568 frame.clear();
569 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800570 const Transform& tr(displayDevice->getTransform());
571 Rect transformedFrame = tr.transform(frame);
David Revemanecf0fa52017-03-03 11:32:44 -0500572 error = hwcLayer->setDisplayFrame(transformedFrame);
Dan Stozae22aec72016-08-01 13:20:59 -0700573 if (error != HWC2::Error::None) {
David Sodman41fdfc92017-11-06 16:09:56 -0800574 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
575 transformedFrame.left, transformedFrame.top, transformedFrame.right,
576 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stozae22aec72016-08-01 13:20:59 -0700577 } else {
578 hwcInfo.displayFrame = transformedFrame;
579 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800580
Dan Stoza5a423ea2017-02-16 14:10:39 -0800581 FloatRect sourceCrop = computeCrop(displayDevice);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800582 error = hwcLayer->setSourceCrop(sourceCrop);
Dan Stozae22aec72016-08-01 13:20:59 -0700583 if (error != HWC2::Error::None) {
584 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
David Sodman41fdfc92017-11-06 16:09:56 -0800585 "%s (%d)",
586 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
587 to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stozae22aec72016-08-01 13:20:59 -0700588 } else {
589 hwcInfo.sourceCrop = sourceCrop;
590 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800591
chaviw13fdc492017-06-27 12:40:18 -0700592 float alpha = static_cast<float>(getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -0700593 error = hwcLayer->setPlaneAlpha(alpha);
David Sodman41fdfc92017-11-06 16:09:56 -0800594 ALOGE_IF(error != HWC2::Error::None,
595 "[%s] Failed to set plane alpha %.3f: "
596 "%s (%d)",
597 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800598
Robert Carrae060832016-11-28 10:51:00 -0800599 error = hwcLayer->setZOrder(z);
David Sodman41fdfc92017-11-06 16:09:56 -0800600 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
601 to_string(error).c_str(), static_cast<int32_t>(error));
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500602
Albert Chaulk2a589632017-05-04 16:59:44 -0400603 int type = s.type;
604 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700605 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400606 if (parent.get()) {
607 auto& parentState = parent->getDrawingState();
rongliuccd34842018-03-14 12:26:23 -0700608 if (parentState.type >= 0 || parentState.appId >= 0) {
609 type = parentState.type;
610 appId = parentState.appId;
611 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400612 }
613
614 error = hwcLayer->setInfo(type, appId);
David Sodman41fdfc92017-11-06 16:09:56 -0800615 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
616 static_cast<int32_t>(error));
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800617
Mathias Agopian29a367b2011-07-12 14:51:45 -0700618 /*
619 * Transformations are applied in this order:
620 * 1) buffer orientation/flip/mirror
621 * 2) state transformation (window manager)
622 * 3) layer orientation (screen orientation)
623 * (NOTE: the matrices are multiplied in reverse order)
624 */
625
626 const Transform bufferOrientation(mCurrentTransform);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700627 Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700628
Robert Carrcae605c2017-03-29 12:10:31 -0700629 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700630 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700631 * the code below applies the primary display's inverse transform to the
632 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700633 */
David Sodman41fdfc92017-11-06 16:09:56 -0800634 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700635 // calculate the inverse transform
636 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800637 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700638 }
Robert Carrcae605c2017-03-29 12:10:31 -0700639
640 /*
641 * Here we cancel out the orientation component of the WM transform.
642 * The scaling and translate components are already included in our bounds
643 * computation so it's enough to just omit it in the composition.
644 * See comment in onDraw with ref to b/36727915 for why.
645 */
646 transform = Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700647 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700648
649 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800650 const uint32_t orientation = transform.getOrientation();
Jorim Jaggif3bd94a2018-03-27 15:38:03 +0200651 if (orientation & Transform::ROT_INVALID) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800652 // we can only handle simple transformation
653 hwcInfo.forceClientComposition = true;
654 } else {
655 auto transform = static_cast<HWC2::Transform>(orientation);
Yiwei Zhang068e31b2018-02-21 13:02:45 -0800656 hwcInfo.transform = transform;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800657 auto error = hwcLayer->setTransform(transform);
David Sodman41fdfc92017-11-06 16:09:56 -0800658 ALOGE_IF(error != HWC2::Error::None,
659 "[%s] Failed to set transform %s: "
660 "%s (%d)",
661 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
662 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800663 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700664}
665
Dan Stoza9e56aa02015-11-02 13:00:03 -0800666void Layer::forceClientComposition(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700667 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800668 ALOGE("forceClientComposition: no HWC layer found (%d)", hwcId);
669 return;
670 }
671
David Sodman6f65f3e2017-11-03 14:28:09 -0700672 getBE().mHwcLayers[hwcId].forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800673}
Dan Stozaee44edd2015-03-23 15:50:23 -0700674
chaviwc9232ed2017-11-14 15:31:15 -0800675bool Layer::getForceClientComposition(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700676 if (getBE().mHwcLayers.count(hwcId) == 0) {
chaviwc9232ed2017-11-14 15:31:15 -0800677 ALOGE("getForceClientComposition: no HWC layer found (%d)", hwcId);
678 return false;
679 }
680
David Sodman6f65f3e2017-11-03 14:28:09 -0700681 return getBE().mHwcLayers[hwcId].forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800682}
683
Dan Stoza9e56aa02015-11-02 13:00:03 -0800684void Layer::updateCursorPosition(const sp<const DisplayDevice>& displayDevice) {
685 auto hwcId = displayDevice->getHwcDisplayId();
David Sodman6f65f3e2017-11-03 14:28:09 -0700686 if (getBE().mHwcLayers.count(hwcId) == 0 ||
David Sodman9eeae692017-11-02 10:53:32 -0700687 getCompositionType(hwcId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800688 return;
689 }
690
691 // This gives us only the "orientation" component of the transform
692 const State& s(getCurrentState());
693
694 // Apply the layer's transform, followed by the display's global transform
695 // Here we're guaranteed that the layer's transform preserves rects
696 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700697 if (!s.crop.isEmpty()) {
698 win.intersect(s.crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800699 }
700 // Subtract the transparent region and snap to the bounds
701 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700702 Rect frame(getTransform().transform(bounds));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800703 frame.intersect(displayDevice->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700704 if (!s.finalCrop.isEmpty()) {
705 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000706 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800707 auto& displayTransform(displayDevice->getTransform());
708 auto position = displayTransform.transform(frame);
709
David Sodman6f65f3e2017-11-03 14:28:09 -0700710 auto error = getBE().mHwcLayers[hwcId].layer->setCursorPosition(position.left,
David Sodman9eeae692017-11-02 10:53:32 -0700711 position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800712 ALOGE_IF(error != HWC2::Error::None,
713 "[%s] Failed to set cursor position "
714 "to (%d, %d): %s (%d)",
715 mName.string(), position.left, position.top, to_string(error).c_str(),
716 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800717}
Riley Andrews03414a12014-07-01 14:22:59 -0700718
Mathias Agopian13127d82013-03-05 17:47:11 -0800719// ---------------------------------------------------------------------------
720// drawing...
721// ---------------------------------------------------------------------------
722
chaviwa76b2712017-09-20 12:02:26 -0700723void Layer::draw(const RenderArea& renderArea, const Region& clip) const {
724 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800725}
726
chaviwa76b2712017-09-20 12:02:26 -0700727void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) const {
728 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800729}
730
chaviwa76b2712017-09-20 12:02:26 -0700731void Layer::draw(const RenderArea& renderArea) const {
732 onDraw(renderArea, Region(renderArea.getBounds()), false);
Dan Stozac7014012014-02-14 15:03:43 -0800733}
734
David Sodman41fdfc92017-11-06 16:09:56 -0800735void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
736 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800737 auto& engine(mFlinger->getRenderEngine());
David Sodman9eeae692017-11-02 10:53:32 -0700738 computeGeometry(renderArea, getBE().mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700739 engine.setupFillWithColor(red, green, blue, alpha);
David Sodman9eeae692017-11-02 10:53:32 -0700740 engine.drawMesh(getBE().mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800741}
742
chaviwa76b2712017-09-20 12:02:26 -0700743void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
David Sodman41fdfc92017-11-06 16:09:56 -0800744 clearWithOpenGL(renderArea, 0, 0, 0, 0);
Mathias Agopian13127d82013-03-05 17:47:11 -0800745}
746
David Sodman41fdfc92017-11-06 16:09:56 -0800747void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type, bool callIntoHwc) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700748 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800749 ALOGE("setCompositionType called without a valid HWC layer");
750 return;
751 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700752 auto& hwcInfo = getBE().mHwcLayers[hwcId];
Dan Stoza9e56aa02015-11-02 13:00:03 -0800753 auto& hwcLayer = hwcInfo.layer;
David Sodman41fdfc92017-11-06 16:09:56 -0800754 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(), to_string(type).c_str(),
755 static_cast<int>(callIntoHwc));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800756 if (hwcInfo.compositionType != type) {
757 ALOGV(" actually setting");
758 hwcInfo.compositionType = type;
759 if (callIntoHwc) {
760 auto error = hwcLayer->setCompositionType(type);
David Sodman41fdfc92017-11-06 16:09:56 -0800761 ALOGE_IF(error != HWC2::Error::None,
762 "[%s] Failed to set "
763 "composition type %s: %s (%d)",
764 mName.string(), to_string(type).c_str(), to_string(error).c_str(),
765 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800766 }
767 }
768}
769
770HWC2::Composition Layer::getCompositionType(int32_t hwcId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -0700771 if (hwcId == DisplayDevice::DISPLAY_ID_INVALID) {
772 // If we're querying the composition type for a display that does not
773 // have a HWC counterpart, then it will always be Client
774 return HWC2::Composition::Client;
775 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700776 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700777 ALOGE("getCompositionType called with an invalid HWC layer");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800778 return HWC2::Composition::Invalid;
779 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700780 return getBE().mHwcLayers.at(hwcId).compositionType;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800781}
782
783void Layer::setClearClientTarget(int32_t hwcId, bool clear) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700784 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800785 ALOGE("setClearClientTarget called without a valid HWC layer");
786 return;
787 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700788 getBE().mHwcLayers[hwcId].clearClientTarget = clear;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800789}
790
791bool Layer::getClearClientTarget(int32_t hwcId) const {
David Sodman6f65f3e2017-11-03 14:28:09 -0700792 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800793 ALOGE("getClearClientTarget called without a valid HWC layer");
794 return false;
795 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700796 return getBE().mHwcLayers.at(hwcId).clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800797}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800798
Dan Stozacac35382016-01-27 12:21:06 -0800799bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
800 if (point->getFrameNumber() <= mCurrentFrameNumber) {
801 // Don't bother with a SyncPoint, since we've already latched the
802 // relevant frame
803 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700804 }
805
Dan Stozacac35382016-01-27 12:21:06 -0800806 Mutex::Autolock lock(mLocalSyncPointMutex);
807 mLocalSyncPoints.push_back(point);
808 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700809}
810
Mathias Agopian13127d82013-03-05 17:47:11 -0800811void Layer::setFiltering(bool filtering) {
812 mFiltering = filtering;
813}
814
815bool Layer::getFiltering() const {
816 return mFiltering;
817}
818
Mathias Agopian13127d82013-03-05 17:47:11 -0800819// ----------------------------------------------------------------------------
820// local state
821// ----------------------------------------------------------------------------
822
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000823static void boundPoint(vec2* point, const Rect& crop) {
824 if (point->x < crop.left) {
825 point->x = crop.left;
826 }
827 if (point->x > crop.right) {
828 point->x = crop.right;
829 }
830 if (point->y < crop.top) {
831 point->y = crop.top;
832 }
833 if (point->y > crop.bottom) {
834 point->y = crop.bottom;
835 }
836}
837
chaviwa76b2712017-09-20 12:02:26 -0700838void Layer::computeGeometry(const RenderArea& renderArea, Mesh& mesh,
839 bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700840 const Layer::State& s(getDrawingState());
chaviwa76b2712017-09-20 12:02:26 -0700841 const Transform renderAreaTransform(renderArea.getTransform());
842 const uint32_t height = renderArea.getHeight();
Dan Stoza80d61162017-12-20 15:57:52 -0800843 FloatRect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700844
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000845 vec2 lt = vec2(win.left, win.top);
846 vec2 lb = vec2(win.left, win.bottom);
847 vec2 rb = vec2(win.right, win.bottom);
848 vec2 rt = vec2(win.right, win.top);
849
Robert Carr1f0a16a2016-10-24 16:27:39 -0700850 Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000851 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700852 lt = layerTransform.transform(lt);
853 lb = layerTransform.transform(lb);
854 rb = layerTransform.transform(rb);
855 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000856 }
857
Robert Carrb5d3d262016-03-25 15:08:13 -0700858 if (!s.finalCrop.isEmpty()) {
859 boundPoint(&lt, s.finalCrop);
860 boundPoint(&lb, s.finalCrop);
861 boundPoint(&rb, s.finalCrop);
862 boundPoint(&rt, s.finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000863 }
864
Mathias Agopianff2ed702013-09-01 21:36:12 -0700865 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700866 position[0] = renderAreaTransform.transform(lt);
867 position[1] = renderAreaTransform.transform(lb);
868 position[2] = renderAreaTransform.transform(rb);
869 position[3] = renderAreaTransform.transform(rt);
David Sodman41fdfc92017-11-06 16:09:56 -0800870 for (size_t i = 0; i < 4; i++) {
chaviwa76b2712017-09-20 12:02:26 -0700871 position[i].y = height - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -0800872 }
873}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800874
David Sodman41fdfc92017-11-06 16:09:56 -0800875bool Layer::isSecure() const {
Dan Stoza23116082015-06-18 14:58:39 -0700876 const Layer::State& s(mDrawingState);
877 return (s.flags & layer_state_t::eLayerSecure);
878}
879
Mathias Agopian13127d82013-03-05 17:47:11 -0800880void Layer::setVisibleRegion(const Region& visibleRegion) {
881 // always called from main thread
882 this->visibleRegion = visibleRegion;
883}
884
885void Layer::setCoveredRegion(const Region& coveredRegion) {
886 // always called from main thread
887 this->coveredRegion = coveredRegion;
888}
889
David Sodman41fdfc92017-11-06 16:09:56 -0800890void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800891 // always called from main thread
892 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
893}
894
Robert Carre5f4f692018-01-12 13:12:28 -0800895void Layer::clearVisibilityRegions() {
896 visibleRegion.clear();
897 visibleNonTransparentRegion.clear();
898 coveredRegion.clear();
899}
900
Mathias Agopian13127d82013-03-05 17:47:11 -0800901// ----------------------------------------------------------------------------
902// transaction
903// ----------------------------------------------------------------------------
904
Dan Stoza7dde5992015-05-22 09:51:44 -0700905void Layer::pushPendingState() {
906 if (!mCurrentState.modified) {
907 return;
908 }
909
Dan Stoza7dde5992015-05-22 09:51:44 -0700910 // If this transaction is waiting on the receipt of a frame, generate a sync
911 // point and send it to the remote layer.
Robert Carr0d480722017-01-10 16:42:54 -0800912 if (mCurrentState.barrierLayer != nullptr) {
913 sp<Layer> barrierLayer = mCurrentState.barrierLayer.promote();
914 if (barrierLayer == nullptr) {
915 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700916 // If we can't promote the layer we are intended to wait on,
917 // then it is expired or otherwise invalid. Allow this transaction
918 // to be applied as per normal (no synchronization).
Robert Carr0d480722017-01-10 16:42:54 -0800919 mCurrentState.barrierLayer = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800920 } else {
David Sodman41fdfc92017-11-06 16:09:56 -0800921 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber);
Robert Carr0d480722017-01-10 16:42:54 -0800922 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800923 mRemoteSyncPoints.push_back(std::move(syncPoint));
924 } else {
925 // We already missed the frame we're supposed to synchronize
926 // on, so go ahead and apply the state update
Robert Carr0d480722017-01-10 16:42:54 -0800927 mCurrentState.barrierLayer = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800928 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700929 }
930
Dan Stoza7dde5992015-05-22 09:51:44 -0700931 // Wake us up to check if the frame has been received
932 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700933 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700934 }
935 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700936 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700937}
938
Pablo Ceballos05289c22016-04-14 15:49:55 -0700939void Layer::popPendingState(State* stateToCommit) {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700940 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700941
942 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700943 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700944}
945
Pablo Ceballos05289c22016-04-14 15:49:55 -0700946bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700947 bool stateUpdateAvailable = false;
948 while (!mPendingStates.empty()) {
Robert Carr0d480722017-01-10 16:42:54 -0800949 if (mPendingStates[0].barrierLayer != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700950 if (mRemoteSyncPoints.empty()) {
951 // If we don't have a sync point for this, apply it anyway. It
952 // will be visually wrong, but it should keep us from getting
953 // into too much trouble.
954 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700955 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700956 stateUpdateAvailable = true;
957 continue;
958 }
959
David Sodman41fdfc92017-11-06 16:09:56 -0800960 if (mRemoteSyncPoints.front()->getFrameNumber() != mPendingStates[0].frameNumber) {
961 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800962
963 // Signal our end of the sync point and then dispose of it
964 mRemoteSyncPoints.front()->setTransactionApplied();
965 mRemoteSyncPoints.pop_front();
966 continue;
967 }
968
Dan Stoza7dde5992015-05-22 09:51:44 -0700969 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
970 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700971 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700972 stateUpdateAvailable = true;
973
974 // Signal our end of the sync point and then dispose of it
975 mRemoteSyncPoints.front()->setTransactionApplied();
976 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800977 } else {
978 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700979 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700980 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700981 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700982 stateUpdateAvailable = true;
983 }
984 }
985
986 // If we still have pending updates, wake SurfaceFlinger back up and point
987 // it at this layer so we can process them
988 if (!mPendingStates.empty()) {
989 setTransactionFlags(eTransactionNeeded);
990 mFlinger->setTransactionFlags(eTraversalNeeded);
991 }
992
993 mCurrentState.modified = false;
994 return stateUpdateAvailable;
995}
996
Mathias Agopian13127d82013-03-05 17:47:11 -0800997uint32_t Layer::doTransaction(uint32_t flags) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800998 ATRACE_CALL();
999
Dan Stoza7dde5992015-05-22 09:51:44 -07001000 pushPendingState();
Pablo Ceballos05289c22016-04-14 15:49:55 -07001001 Layer::State c = getCurrentState();
1002 if (!applyPendingStates(&c)) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001003 return 0;
1004 }
1005
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001006 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001007
David Sodman41fdfc92017-11-06 16:09:56 -08001008 const bool sizeChanged = (c.requested.w != s.requested.w) || (c.requested.h != s.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -07001009
David Sodmaneb085e02017-10-05 18:49:04 -07001010 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001011 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +00001012 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -08001013 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
1014 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1015 " requested={ wh={%4u,%4u} }}\n"
1016 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1017 " requested={ wh={%4u,%4u} }}\n",
David Sodman9eeae692017-11-02 10:53:32 -07001018 this, getName().string(), mCurrentTransform,
1019 getEffectiveScalingMode(), c.active.w, c.active.h, c.crop.left, c.crop.top,
1020 c.crop.right, c.crop.bottom, c.crop.getWidth(), c.crop.getHeight(), c.requested.w,
1021 c.requested.h, s.active.w, s.active.h, s.crop.left, s.crop.top, s.crop.right,
1022 s.crop.bottom, s.crop.getWidth(), s.crop.getHeight(), s.requested.w,
1023 s.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001024
Jamie Gennis2a0d5b62011-09-26 16:54:44 -07001025 // record the new size, form this point on, when the client request
1026 // a buffer, it'll get the new size.
David Sodmaneb085e02017-10-05 18:49:04 -07001027 setDefaultBufferSize(c.requested.w, c.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001028 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001029
Robert Carre392b552017-09-19 12:16:05 -07001030 // Don't let Layer::doTransaction update the drawing state
1031 // if we have a pending resize, unless we are in fixed-size mode.
1032 // the drawing state will be updated only once we receive a buffer
1033 // with the correct size.
1034 //
1035 // In particular, we want to make sure the clip (which is part
1036 // of the geometry state) is latched together with the size but is
1037 // latched immediately when no resizing is involved.
1038 //
1039 // If a sideband stream is attached, however, we want to skip this
1040 // optimization so that transactions aren't missed when a buffer
1041 // never arrives
1042 //
1043 // In the case that we don't have a buffer we ignore other factors
1044 // and avoid entering the resizePending state. At a high level the
1045 // resizePending state is to avoid applying the state of the new buffer
1046 // to the old buffer. However in the state where we don't have an old buffer
1047 // there is no such concern but we may still be being used as a parent layer.
David Sodman41fdfc92017-11-06 16:09:56 -08001048 const bool resizePending = ((c.requested.w != c.active.w) || (c.requested.h != c.active.h)) &&
David Sodman0cc69182017-11-17 12:12:07 -08001049 (getBE().compositionInfo.mBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001050 if (!isFixedSize()) {
David Sodman0cc69182017-11-17 12:12:07 -08001051 if (resizePending && getBE().compositionInfo.hwc.sidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001052 flags |= eDontUpdateGeometryState;
1053 }
1054 }
1055
Robert Carr7bf247e2017-05-18 14:02:49 -07001056 // Here we apply various requested geometry states, depending on our
1057 // latching configuration. See Layer.h for a detailed discussion of
1058 // how geometry latching is controlled.
1059 if (!(flags & eDontUpdateGeometryState)) {
Pablo Ceballos7d052572016-06-02 17:46:05 -07001060 Layer::State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001061
1062 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1063 // mode, which causes attributes which normally latch regardless of scaling mode,
1064 // to be delayed. We copy the requested state to the active state making sure
1065 // to respect these rules (again see Layer.h for a detailed discussion).
1066 //
1067 // There is an awkward asymmetry in the handling of the crop states in the position
1068 // states, as can be seen below. Largely this arises from position and transform
1069 // being stored in the same data structure while having different latching rules.
1070 // b/38182305
1071 //
1072 // Careful that "c" and editCurrentState may not begin as equivalent due to
1073 // applyPendingStates in the presence of deferred transactions.
1074 if (mFreezeGeometryUpdates) {
Robert Carr82364e32016-05-15 11:27:47 -07001075 float tx = c.active.transform.tx();
1076 float ty = c.active.transform.ty();
1077 c.active = c.requested;
1078 c.active.transform.set(tx, ty);
1079 editCurrentState.active = c.active;
1080 } else {
1081 editCurrentState.active = editCurrentState.requested;
1082 c.active = c.requested;
1083 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001084 }
1085
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001086 if (s.active != c.active) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001087 // invalidate and recompute the visible regions if needed
1088 flags |= Layer::eVisibleRegion;
1089 }
1090
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001091 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001092 // invalidate and recompute the visible regions if needed
1093 flags |= eVisibleRegion;
1094 this->contentDirty = true;
1095
1096 // we may use linear filtering, if the matrix scales us
Robert Carr3dcabfa2016-03-01 18:36:58 -08001097 const uint8_t type = c.active.transform.getType();
David Sodman41fdfc92017-11-06 16:09:56 -08001098 mNeedsFiltering = (!c.active.transform.preserveRects() || (type >= Transform::SCALE));
Mathias Agopian13127d82013-03-05 17:47:11 -08001099 }
1100
Dan Stozac8145172016-04-28 16:29:06 -07001101 // If the layer is hidden, signal and clear out all local sync points so
1102 // that transactions for layers depending on this layer's frames becoming
1103 // visible are not blocked
1104 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001105 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001106 }
1107
Mathias Agopian13127d82013-03-05 17:47:11 -08001108 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001109 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001110 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001111}
1112
Pablo Ceballos05289c22016-04-14 15:49:55 -07001113void Layer::commitTransaction(const State& stateToCommit) {
1114 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001115}
1116
Mathias Agopian13127d82013-03-05 17:47:11 -08001117uint32_t Layer::getTransactionFlags(uint32_t flags) {
1118 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1119}
1120
1121uint32_t Layer::setTransactionFlags(uint32_t flags) {
1122 return android_atomic_or(flags, &mTransactionFlags);
1123}
1124
Robert Carr82364e32016-05-15 11:27:47 -07001125bool Layer::setPosition(float x, float y, bool immediate) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001126 if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001127 return false;
1128 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001129
1130 // We update the requested and active position simultaneously because
1131 // we want to apply the position portion of the transform matrix immediately,
1132 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Robert Carr3dcabfa2016-03-01 18:36:58 -08001133 mCurrentState.requested.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001134 if (immediate && !mFreezeGeometryUpdates) {
1135 // Here we directly update the active state
1136 // unlike other setters, because we store it within
1137 // the transform, but use different latching rules.
1138 // b/38182305
Robert Carr82364e32016-05-15 11:27:47 -07001139 mCurrentState.active.transform.set(x, y);
1140 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001141 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001142
Dan Stoza7dde5992015-05-22 09:51:44 -07001143 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001144 setTransactionFlags(eTransactionNeeded);
1145 return true;
1146}
Robert Carr82364e32016-05-15 11:27:47 -07001147
Robert Carr1f0a16a2016-10-24 16:27:39 -07001148bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1149 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1150 if (idx < 0) {
1151 return false;
1152 }
1153 if (childLayer->setLayer(z)) {
1154 mCurrentChildren.removeAt(idx);
1155 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001156 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001157 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001158 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001159}
1160
Robert Carr503c7042017-09-27 15:06:08 -07001161bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1162 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1163 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1164 if (idx < 0) {
1165 return false;
1166 }
1167 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1168 mCurrentChildren.removeAt(idx);
1169 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001170 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001171 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001172 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001173}
1174
Robert Carrae060832016-11-28 10:51:00 -08001175bool Layer::setLayer(int32_t z) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001176 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001177 mCurrentState.sequence++;
1178 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001179 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001180
1181 // Discard all relative layering.
1182 if (mCurrentState.zOrderRelativeOf != nullptr) {
1183 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1184 if (strongRelative != nullptr) {
1185 strongRelative->removeZOrderRelative(this);
1186 }
1187 mCurrentState.zOrderRelativeOf = nullptr;
1188 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001189 setTransactionFlags(eTransactionNeeded);
1190 return true;
1191}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001192
Robert Carrdb66e622017-04-10 16:55:57 -07001193void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1194 mCurrentState.zOrderRelatives.remove(relative);
1195 mCurrentState.sequence++;
1196 mCurrentState.modified = true;
1197 setTransactionFlags(eTransactionNeeded);
1198}
1199
1200void Layer::addZOrderRelative(const wp<Layer>& relative) {
1201 mCurrentState.zOrderRelatives.add(relative);
1202 mCurrentState.modified = true;
1203 mCurrentState.sequence++;
1204 setTransactionFlags(eTransactionNeeded);
1205}
1206
Robert Carr503d2bd2017-12-04 15:49:47 -08001207bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001208 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1209 if (handle == nullptr) {
1210 return false;
1211 }
1212 sp<Layer> relative = handle->owner.promote();
1213 if (relative == nullptr) {
1214 return false;
1215 }
1216
Robert Carr503d2bd2017-12-04 15:49:47 -08001217 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1218 mCurrentState.zOrderRelativeOf == relative) {
1219 return false;
1220 }
1221
Robert Carrdb66e622017-04-10 16:55:57 -07001222 mCurrentState.sequence++;
1223 mCurrentState.modified = true;
Robert Carr503d2bd2017-12-04 15:49:47 -08001224 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001225
chaviw9ab4bd12017-11-03 13:11:00 -07001226 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
1227 if (oldZOrderRelativeOf != nullptr) {
1228 oldZOrderRelativeOf->removeZOrderRelative(this);
1229 }
Robert Carrdb66e622017-04-10 16:55:57 -07001230 mCurrentState.zOrderRelativeOf = relative;
1231 relative->addZOrderRelative(this);
1232
1233 setTransactionFlags(eTransactionNeeded);
1234
1235 return true;
1236}
1237
Mathias Agopian13127d82013-03-05 17:47:11 -08001238bool Layer::setSize(uint32_t w, uint32_t h) {
David Sodman41fdfc92017-11-06 16:09:56 -08001239 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001240 mCurrentState.requested.w = w;
1241 mCurrentState.requested.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001242 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001243 setTransactionFlags(eTransactionNeeded);
1244 return true;
1245}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001246bool Layer::setAlpha(float alpha) {
David Sodman41fdfc92017-11-06 16:09:56 -08001247 if (mCurrentState.color.a == alpha) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001248 mCurrentState.sequence++;
chaviw13fdc492017-06-27 12:40:18 -07001249 mCurrentState.color.a = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001250 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001251 setTransactionFlags(eTransactionNeeded);
1252 return true;
1253}
chaviw13fdc492017-06-27 12:40:18 -07001254
1255bool Layer::setColor(const half3& color) {
David Sodman41fdfc92017-11-06 16:09:56 -08001256 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g &&
1257 color.b == mCurrentState.color.b)
chaviw13fdc492017-06-27 12:40:18 -07001258 return false;
1259
1260 mCurrentState.sequence++;
1261 mCurrentState.color.r = color.r;
1262 mCurrentState.color.g = color.g;
1263 mCurrentState.color.b = color.b;
1264 mCurrentState.modified = true;
1265 setTransactionFlags(eTransactionNeeded);
1266 return true;
1267}
1268
Robert Carrd4ae7f32018-06-07 16:10:57 -07001269bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1270 bool allowNonRectPreservingTransforms) {
1271 Transform t;
1272 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1273
1274 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1275 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1276 return false;
1277 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001278 mCurrentState.sequence++;
David Sodman41fdfc92017-11-06 16:09:56 -08001279 mCurrentState.requested.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001280 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001281 setTransactionFlags(eTransactionNeeded);
1282 return true;
1283}
1284bool Layer::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001285 mCurrentState.requestedTransparentRegion = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001286 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001287 setTransactionFlags(eTransactionNeeded);
1288 return true;
1289}
1290bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1291 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
David Sodman41fdfc92017-11-06 16:09:56 -08001292 if (mCurrentState.flags == newFlags) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001293 mCurrentState.sequence++;
1294 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001295 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001296 setTransactionFlags(eTransactionNeeded);
1297 return true;
1298}
Robert Carr99e27f02016-06-16 15:18:02 -07001299
1300bool Layer::setCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001301 if (mCurrentState.requestedCrop == crop) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001302 mCurrentState.sequence++;
Robert Carr99e27f02016-06-16 15:18:02 -07001303 mCurrentState.requestedCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001304 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr99e27f02016-06-16 15:18:02 -07001305 mCurrentState.crop = crop;
1306 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001307 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1308
Dan Stoza7dde5992015-05-22 09:51:44 -07001309 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001310 setTransactionFlags(eTransactionNeeded);
1311 return true;
1312}
Robert Carr8d5227b2017-03-16 15:41:03 -07001313
1314bool Layer::setFinalCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001315 if (mCurrentState.requestedFinalCrop == crop) return false;
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001316 mCurrentState.sequence++;
Robert Carr8d5227b2017-03-16 15:41:03 -07001317 mCurrentState.requestedFinalCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001318 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr8d5227b2017-03-16 15:41:03 -07001319 mCurrentState.finalCrop = crop;
1320 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001321 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1322
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001323 mCurrentState.modified = true;
1324 setTransactionFlags(eTransactionNeeded);
1325 return true;
1326}
Mathias Agopian13127d82013-03-05 17:47:11 -08001327
Robert Carrc3574f72016-03-24 12:19:32 -07001328bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001329 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001330 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001331 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001332 return true;
1333}
1334
rongliuccd34842018-03-14 12:26:23 -07001335void Layer::setInfo(int32_t type, int32_t appId) {
David Sodman41fdfc92017-11-06 16:09:56 -08001336 mCurrentState.appId = appId;
1337 mCurrentState.type = type;
1338 mCurrentState.modified = true;
1339 setTransactionFlags(eTransactionNeeded);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001340}
1341
Mathias Agopian13127d82013-03-05 17:47:11 -08001342bool Layer::setLayerStack(uint32_t layerStack) {
David Sodman41fdfc92017-11-06 16:09:56 -08001343 if (mCurrentState.layerStack == layerStack) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001344 mCurrentState.sequence++;
1345 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001346 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001347 setTransactionFlags(eTransactionNeeded);
1348 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001349}
1350
Robert Carr1f0a16a2016-10-24 16:27:39 -07001351uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001352 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001353 if (p == nullptr) {
1354 return getDrawingState().layerStack;
1355 }
1356 return p->getLayerStack();
1357}
1358
David Sodman41fdfc92017-11-06 16:09:56 -08001359void Layer::deferTransactionUntil(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001360 mCurrentState.barrierLayer = barrierLayer;
Dan Stoza7dde5992015-05-22 09:51:44 -07001361 mCurrentState.frameNumber = frameNumber;
1362 // We don't set eTransactionNeeded, because just receiving a deferral
1363 // request without any other state updates shouldn't actually induce a delay
1364 mCurrentState.modified = true;
1365 pushPendingState();
Robert Carr0d480722017-01-10 16:42:54 -08001366 mCurrentState.barrierLayer = nullptr;
Dan Stoza792e5292016-02-11 11:43:58 -08001367 mCurrentState.frameNumber = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001368 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001369}
1370
David Sodman41fdfc92017-11-06 16:09:56 -08001371void Layer::deferTransactionUntil(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001372 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
1373 deferTransactionUntil(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001374}
1375
Dan Stozaee44edd2015-03-23 15:50:23 -07001376
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001377// ----------------------------------------------------------------------------
1378// pageflip handling...
1379// ----------------------------------------------------------------------------
1380
Robert Carr1f0a16a2016-10-24 16:27:39 -07001381bool Layer::isHiddenByPolicy() const {
1382 const Layer::State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001383 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001384 if (parent != nullptr && parent->isHiddenByPolicy()) {
1385 return true;
1386 }
1387 return s.flags & layer_state_t::eLayerHidden;
1388}
1389
David Sodman41fdfc92017-11-06 16:09:56 -08001390uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001391 // TODO: should we do something special if mSecure is set?
1392 if (mProtectedByApp) {
1393 // need a hardware-protected path to external video sink
1394 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001395 }
Riley Andrews03414a12014-07-01 14:22:59 -07001396 if (mPotentialCursor) {
1397 usage |= GraphicBuffer::USAGE_CURSOR;
1398 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001399 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001400 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001401}
1402
Mathias Agopian84300952012-11-21 16:02:13 -08001403void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001404 uint32_t orientation = 0;
1405 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001406 // The transform hint is used to improve performance, but we can
1407 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001408 // apply to all displays.
Mathias Agopian42977342012-08-05 00:40:46 -07001409 const Transform& planeTransform(hw->getTransform());
Mathias Agopian4fec8732012-06-29 14:12:52 -07001410 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07001411 if (orientation & Transform::ROT_INVALID) {
1412 orientation = 0;
1413 }
1414 }
David Sodmaneb085e02017-10-05 18:49:04 -07001415 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001416}
1417
Mathias Agopian13127d82013-03-05 17:47:11 -08001418// ----------------------------------------------------------------------------
1419// debugging
1420// ----------------------------------------------------------------------------
1421
Kalle Raitaa099a242017-01-11 11:17:29 -08001422LayerDebugInfo Layer::getLayerDebugInfo() const {
1423 LayerDebugInfo info;
1424 const Layer::State& ds = getDrawingState();
1425 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001426 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001427 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
1428 info.mType = String8(getTypeId());
1429 info.mTransparentRegion = ds.activeTransparentRegion;
1430 info.mVisibleRegion = visibleRegion;
1431 info.mSurfaceDamageRegion = surfaceDamageRegion;
1432 info.mLayerStack = getLayerStack();
1433 info.mX = ds.active.transform.tx();
1434 info.mY = ds.active.transform.ty();
1435 info.mZ = ds.z;
1436 info.mWidth = ds.active.w;
1437 info.mHeight = ds.active.h;
1438 info.mCrop = ds.crop;
1439 info.mFinalCrop = ds.finalCrop;
chaviw13fdc492017-06-27 12:40:18 -07001440 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001441 info.mFlags = ds.flags;
1442 info.mPixelFormat = getPixelFormat();
Chia-I Wu1b6bafc2018-05-22 12:03:00 -07001443 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Kalle Raitaa099a242017-01-11 11:17:29 -08001444 info.mMatrix[0][0] = ds.active.transform[0][0];
1445 info.mMatrix[0][1] = ds.active.transform[0][1];
1446 info.mMatrix[1][0] = ds.active.transform[1][0];
1447 info.mMatrix[1][1] = ds.active.transform[1][1];
1448 {
David Sodman0cc69182017-11-17 12:12:07 -08001449 sp<const GraphicBuffer> buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001450 if (buffer != 0) {
1451 info.mActiveBufferWidth = buffer->getWidth();
1452 info.mActiveBufferHeight = buffer->getHeight();
1453 info.mActiveBufferStride = buffer->getStride();
1454 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001455 } else {
1456 info.mActiveBufferWidth = 0;
1457 info.mActiveBufferHeight = 0;
1458 info.mActiveBufferStride = 0;
1459 info.mActiveBufferFormat = 0;
1460 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001461 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001462 info.mNumQueuedFrames = getQueuedFrameCount();
1463 info.mRefreshPending = isBufferLatched();
1464 info.mIsOpaque = isOpaque(ds);
1465 info.mContentDirty = contentDirty;
1466 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001467}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001468
Dan Stozae22aec72016-08-01 13:20:59 -07001469void Layer::miniDumpHeader(String8& result) {
1470 result.append("----------------------------------------");
1471 result.append("---------------------------------------\n");
1472 result.append(" Layer name\n");
1473 result.append(" Z | ");
1474 result.append(" Comp Type | ");
1475 result.append(" Disp Frame (LTRB) | ");
1476 result.append(" Source Crop (LTRB)\n");
1477 result.append("----------------------------------------");
1478 result.append("---------------------------------------\n");
1479}
1480
1481void Layer::miniDump(String8& result, int32_t hwcId) const {
David Sodman6f65f3e2017-11-03 14:28:09 -07001482 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stozae22aec72016-08-01 13:20:59 -07001483 return;
1484 }
1485
1486 String8 name;
1487 if (mName.length() > 77) {
1488 std::string shortened;
1489 shortened.append(mName.string(), 36);
1490 shortened.append("[...]");
1491 shortened.append(mName.string() + (mName.length() - 36), 36);
1492 name = shortened.c_str();
1493 } else {
1494 name = mName;
1495 }
1496
1497 result.appendFormat(" %s\n", name.string());
1498
1499 const Layer::State& layerState(getDrawingState());
David Sodman6f65f3e2017-11-03 14:28:09 -07001500 const LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers.at(hwcId);
Chia-I Wu1e043612018-03-01 09:45:09 -08001501 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
1502 result.appendFormat(" rel %6d | ", layerState.z);
1503 } else {
1504 result.appendFormat(" %10d | ", layerState.z);
1505 }
David Sodman41fdfc92017-11-06 16:09:56 -08001506 result.appendFormat("%10s | ", to_string(getCompositionType(hwcId)).c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001507 const Rect& frame = hwcInfo.displayFrame;
David Sodman41fdfc92017-11-06 16:09:56 -08001508 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Dan Stoza5a423ea2017-02-16 14:10:39 -08001509 const FloatRect& crop = hwcInfo.sourceCrop;
David Sodman41fdfc92017-11-06 16:09:56 -08001510 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 -07001511
1512 result.append("- - - - - - - - - - - - - - - - - - - - ");
1513 result.append("- - - - - - - - - - - - - - - - - - - -\n");
1514}
Dan Stozae22aec72016-08-01 13:20:59 -07001515
Svetoslavd85084b2014-03-20 10:28:31 -07001516void Layer::dumpFrameStats(String8& result) const {
1517 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001518}
1519
Svetoslavd85084b2014-03-20 10:28:31 -07001520void Layer::clearFrameStats() {
1521 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001522}
1523
Jamie Gennis6547ff42013-07-16 20:12:42 -07001524void Layer::logFrameStats() {
1525 mFrameTracker.logAndResetStats(mName);
1526}
1527
Svetoslavd85084b2014-03-20 10:28:31 -07001528void Layer::getFrameStats(FrameStats* outStats) const {
1529 mFrameTracker.getStats(outStats);
1530}
1531
Brian Andersond6927fb2016-07-23 23:37:30 -07001532void Layer::dumpFrameEvents(String8& result) {
David Sodman41fdfc92017-11-06 16:09:56 -08001533 result.appendFormat("- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001534 Mutex::Autolock lock(mFrameEventHistoryMutex);
1535 mFrameEventHistory.checkFencesForCompletion();
1536 mFrameEventHistory.dump(result);
1537}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001538
Brian Anderson5ea5e592016-12-01 16:54:33 -08001539void Layer::onDisconnect() {
1540 Mutex::Autolock lock(mFrameEventHistoryMutex);
1541 mFrameEventHistory.onDisconnect();
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001542 mTimeStats.onDisconnect(getName().c_str());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001543}
1544
Brian Anderson3890c392016-07-25 12:48:08 -07001545void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001546 FrameEventHistoryDelta* outDelta) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001547 if (newTimestamps) {
1548 mTimeStats.setPostTime(getName().c_str(), newTimestamps->frameNumber,
1549 newTimestamps->postedTime);
1550 }
1551
Brian Andersond6927fb2016-07-23 23:37:30 -07001552 Mutex::Autolock lock(mFrameEventHistoryMutex);
1553 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001554 // If there are any unsignaled fences in the aquire timeline at this
1555 // point, the previously queued frame hasn't been latched yet. Go ahead
1556 // and try to get the signal time here so the syscall is taken out of
1557 // the main thread's critical path.
1558 mAcquireTimeline.updateSignalTimes();
1559 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001560 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001561 mFrameEventHistory.addQueue(*newTimestamps);
1562 }
1563
Brian Anderson3890c392016-07-25 12:48:08 -07001564 if (outDelta) {
1565 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001566 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001567}
Dan Stozae77c7662016-05-13 11:37:28 -07001568
Chia-I Wu98f1c102017-05-30 14:54:08 -07001569size_t Layer::getChildrenCount() const {
1570 size_t count = 0;
1571 for (const sp<Layer>& child : mCurrentChildren) {
1572 count += 1 + child->getChildrenCount();
1573 }
1574 return count;
1575}
1576
Robert Carr1f0a16a2016-10-24 16:27:39 -07001577void Layer::addChild(const sp<Layer>& layer) {
1578 mCurrentChildren.add(layer);
1579 layer->setParent(this);
1580}
1581
1582ssize_t Layer::removeChild(const sp<Layer>& layer) {
1583 layer->setParent(nullptr);
1584 return mCurrentChildren.remove(layer);
1585}
1586
Robert Carr1db73f62016-12-21 12:58:51 -08001587bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1588 sp<Handle> handle = nullptr;
1589 sp<Layer> newParent = nullptr;
1590 if (newParentHandle == nullptr) {
1591 return false;
1592 }
1593 handle = static_cast<Handle*>(newParentHandle.get());
1594 newParent = handle->owner.promote();
1595 if (newParent == nullptr) {
1596 ALOGE("Unable to promote Layer handle");
1597 return false;
1598 }
1599
1600 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001601 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001602
1603 sp<Client> client(child->mClientRef.promote());
1604 if (client != nullptr) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001605 client->updateParent(newParent);
Robert Carr1db73f62016-12-21 12:58:51 -08001606 }
1607 }
1608 mCurrentChildren.clear();
1609
1610 return true;
1611}
1612
Robert Carr15eae092018-03-23 13:43:53 -07001613void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001614 for (const sp<Layer>& child : mDrawingChildren) {
1615 child->mDrawingParent = newParent;
1616 }
1617}
1618
chaviwf1961f72017-09-18 16:41:07 -07001619bool Layer::reparent(const sp<IBinder>& newParentHandle) {
1620 if (newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001621 return false;
1622 }
1623
1624 auto handle = static_cast<Handle*>(newParentHandle.get());
1625 sp<Layer> newParent = handle->owner.promote();
1626 if (newParent == nullptr) {
1627 ALOGE("Unable to promote Layer handle");
1628 return false;
1629 }
1630
chaviwf1961f72017-09-18 16:41:07 -07001631 sp<Layer> parent = getParent();
1632 if (parent != nullptr) {
1633 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001634 }
chaviwf1961f72017-09-18 16:41:07 -07001635 newParent->addChild(this);
chaviw06178942017-07-27 10:25:59 -07001636
chaviwf1961f72017-09-18 16:41:07 -07001637 sp<Client> client(mClientRef.promote());
chaviw06178942017-07-27 10:25:59 -07001638 sp<Client> newParentClient(newParent->mClientRef.promote());
1639
chaviwf1961f72017-09-18 16:41:07 -07001640 if (client != newParentClient) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001641 client->updateParent(newParent);
chaviw06178942017-07-27 10:25:59 -07001642 }
1643
chaviw06178942017-07-27 10:25:59 -07001644 return true;
1645}
1646
Robert Carr9524cb32017-02-13 11:32:32 -08001647bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001648 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001649 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001650 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001651 if (client != nullptr && parentClient != client) {
Robert Carr7f619b22017-11-06 12:56:35 -08001652 client->detachLayer(child.get());
1653 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001654 }
Robert Carr7f619b22017-11-06 12:56:35 -08001655 }
Robert Carr9524cb32017-02-13 11:32:32 -08001656
1657 return true;
1658}
1659
Chia-I Wu11481472018-05-04 10:43:19 -07001660bool Layer::isLegacyDataSpace() const {
1661 // return true when no higher bits are set
Chia-I Wu1b6bafc2018-05-22 12:03:00 -07001662 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001663 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001664}
1665
Robert Carr1f0a16a2016-10-24 16:27:39 -07001666void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001667 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001668}
1669
1670void Layer::clearSyncPoints() {
1671 for (const auto& child : mCurrentChildren) {
1672 child->clearSyncPoints();
1673 }
1674
1675 Mutex::Autolock lock(mLocalSyncPointMutex);
1676 for (auto& point : mLocalSyncPoints) {
1677 point->setFrameAvailable();
1678 }
1679 mLocalSyncPoints.clear();
1680}
1681
1682int32_t Layer::getZ() const {
1683 return mDrawingState.z;
1684}
1685
Robert Carr29abff82017-12-04 13:51:20 -08001686bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1687 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1688 const State& state = useDrawing ? mDrawingState : mCurrentState;
1689 return state.zOrderRelativeOf != nullptr;
1690}
1691
David Sodman41fdfc92017-11-06 16:09:56 -08001692__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001693 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001694 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1695 "makeTraversalList received invalid stateSet");
1696 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1697 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1698 const State& state = useDrawing ? mDrawingState : mCurrentState;
1699
Robert Carr29abff82017-12-04 13:51:20 -08001700 if (state.zOrderRelatives.size() == 0) {
1701 *outSkipRelativeZUsers = true;
1702 return children;
1703 }
1704
chaviw738df022018-05-31 16:11:27 -07001705 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001706 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001707 sp<Layer> strongRelative = weakRelative.promote();
1708 if (strongRelative != nullptr) {
1709 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001710 }
1711 }
1712
Dan Stoza412903f2017-04-27 13:42:17 -07001713 for (const sp<Layer>& child : children) {
Robert Carr503c7042017-09-27 15:06:08 -07001714 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1715 if (childState.zOrderRelativeOf != nullptr) {
1716 continue;
1717 }
Robert Carrdb66e622017-04-10 16:55:57 -07001718 traverse.add(child);
1719 }
1720
1721 return traverse;
1722}
1723
Robert Carr1f0a16a2016-10-24 16:27:39 -07001724/**
Robert Carrdb66e622017-04-10 16:55:57 -07001725 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001726 */
Dan Stoza412903f2017-04-27 13:42:17 -07001727void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001728 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1729 // produce a new list for traversing, including our relatives, and not including our children
1730 // who are relatives of another surface. In the case that there are no relative Z,
1731 // makeTraversalList returns our children directly to avoid significant overhead.
1732 // However in this case we need to take the responsibility for filtering children which
1733 // are relatives of another surface here.
1734 bool skipRelativeZUsers = false;
1735 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001736
Robert Carr1f0a16a2016-10-24 16:27:39 -07001737 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001738 for (; i < list.size(); i++) {
1739 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001740 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1741 continue;
1742 }
1743
Robert Carrdb66e622017-04-10 16:55:57 -07001744 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001745 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001746 }
Dan Stoza412903f2017-04-27 13:42:17 -07001747 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001748 }
Robert Carr29abff82017-12-04 13:51:20 -08001749
Dan Stoza412903f2017-04-27 13:42:17 -07001750 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001751 for (; i < list.size(); i++) {
1752 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001753
1754 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1755 continue;
1756 }
Dan Stoza412903f2017-04-27 13:42:17 -07001757 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001758 }
1759}
1760
1761/**
Robert Carrdb66e622017-04-10 16:55:57 -07001762 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001763 */
Dan Stoza412903f2017-04-27 13:42:17 -07001764void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1765 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001766 // See traverseInZOrder for documentation.
1767 bool skipRelativeZUsers = false;
1768 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001769
Robert Carr1f0a16a2016-10-24 16:27:39 -07001770 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001771 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001772 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001773
1774 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1775 continue;
1776 }
1777
Robert Carrdb66e622017-04-10 16:55:57 -07001778 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001779 break;
1780 }
Dan Stoza412903f2017-04-27 13:42:17 -07001781 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001782 }
Dan Stoza412903f2017-04-27 13:42:17 -07001783 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001784 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001785 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001786
1787 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1788 continue;
1789 }
1790
Dan Stoza412903f2017-04-27 13:42:17 -07001791 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001792 }
1793}
1794
chaviw4b129c22018-04-09 16:19:43 -07001795LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1796 const std::vector<Layer*>& layersInTree) {
1797 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1798 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001799 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1800 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
chaviw4b129c22018-04-09 16:19:43 -07001801 const State& state = useDrawing ? mDrawingState : mCurrentState;
1802
chaviw738df022018-05-31 16:11:27 -07001803 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001804 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1805 sp<Layer> strongRelative = weakRelative.promote();
1806 // Only add relative layers that are also descendents of the top most parent of the tree.
1807 // If a relative layer is not a descendent, then it should be ignored.
1808 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1809 traverse.add(strongRelative);
1810 }
1811 }
1812
1813 for (const sp<Layer>& child : children) {
1814 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1815 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1816 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1817 // the child here since it won't be added later as a relative.
1818 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1819 childState.zOrderRelativeOf.promote().get())) {
1820 continue;
1821 }
1822 traverse.add(child);
1823 }
1824
1825 return traverse;
1826}
1827
1828void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1829 LayerVector::StateSet stateSet,
1830 const LayerVector::Visitor& visitor) {
1831 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001832
1833 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001834 for (; i < list.size(); i++) {
1835 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001836 if (relative->getZ() >= 0) {
1837 break;
1838 }
chaviw4b129c22018-04-09 16:19:43 -07001839 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001840 }
chaviw4b129c22018-04-09 16:19:43 -07001841
chaviwa76b2712017-09-20 12:02:26 -07001842 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001843 for (; i < list.size(); i++) {
1844 const auto& relative = list[i];
1845 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001846 }
1847}
1848
chaviw4b129c22018-04-09 16:19:43 -07001849std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1850 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1851 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1852
1853 std::vector<Layer*> layersInTree = {this};
1854 for (size_t i = 0; i < children.size(); i++) {
1855 const auto& child = children[i];
1856 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1857 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1858 }
1859
1860 return layersInTree;
1861}
1862
1863void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1864 const LayerVector::Visitor& visitor) {
1865 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1866 std::sort(layersInTree.begin(), layersInTree.end());
1867 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1868}
1869
Robert Carr1f0a16a2016-10-24 16:27:39 -07001870Transform Layer::getTransform() const {
1871 Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001872 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001873 if (p != nullptr) {
1874 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07001875
1876 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
1877 // it isFixedSize) then there may be additional scaling not accounted
1878 // for in the transform. We need to mirror this scaling in child surfaces
1879 // or we will break the contract where WM can treat child surfaces as
1880 // pixels in the parent surface.
David Sodman0cc69182017-11-17 12:12:07 -08001881 if (p->isFixedSize() && p->getBE().compositionInfo.mBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07001882 int bufferWidth;
1883 int bufferHeight;
1884 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
David Sodman0cc69182017-11-17 12:12:07 -08001885 bufferWidth = p->getBE().compositionInfo.mBuffer->getWidth();
1886 bufferHeight = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001887 } else {
David Sodman0cc69182017-11-17 12:12:07 -08001888 bufferHeight = p->getBE().compositionInfo.mBuffer->getWidth();
1889 bufferWidth = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001890 }
David Sodman41fdfc92017-11-06 16:09:56 -08001891 float sx = p->getDrawingState().active.w / static_cast<float>(bufferWidth);
1892 float sy = p->getDrawingState().active.h / static_cast<float>(bufferHeight);
Robert Carr9b429f42017-04-17 14:56:57 -07001893 Transform extraParentScaling;
1894 extraParentScaling.set(sx, 0, 0, sy);
1895 t = t * extraParentScaling;
1896 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001897 }
1898 return t * getDrawingState().active.transform;
1899}
1900
chaviw13fdc492017-06-27 12:40:18 -07001901half Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001902 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07001903
chaviw13fdc492017-06-27 12:40:18 -07001904 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1905 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001906}
Robert Carr6452f122017-03-21 10:41:29 -07001907
chaviw13fdc492017-06-27 12:40:18 -07001908half4 Layer::getColor() const {
1909 const half4 color(getDrawingState().color);
1910 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001911}
Robert Carr6452f122017-03-21 10:41:29 -07001912
Robert Carr1f0a16a2016-10-24 16:27:39 -07001913void Layer::commitChildList() {
1914 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1915 const auto& child = mCurrentChildren[i];
1916 child->commitChildList();
1917 }
1918 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001919 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001920}
1921
chaviw1d044282017-09-27 12:19:28 -07001922void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
1923 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1924 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1925 const State& state = useDrawing ? mDrawingState : mCurrentState;
1926
1927 Transform requestedTransform = state.active.transform;
1928 Transform transform = getTransform();
1929
1930 layerInfo->set_id(sequence);
1931 layerInfo->set_name(getName().c_str());
1932 layerInfo->set_type(String8(getTypeId()));
1933
1934 for (const auto& child : children) {
1935 layerInfo->add_children(child->sequence);
1936 }
1937
1938 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1939 sp<Layer> strongRelative = weakRelative.promote();
1940 if (strongRelative != nullptr) {
1941 layerInfo->add_relatives(strongRelative->sequence);
1942 }
1943 }
1944
1945 LayerProtoHelper::writeToProto(state.activeTransparentRegion,
1946 layerInfo->mutable_transparent_region());
1947 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
1948 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
1949
1950 layerInfo->set_layer_stack(getLayerStack());
1951 layerInfo->set_z(state.z);
1952
1953 PositionProto* position = layerInfo->mutable_position();
1954 position->set_x(transform.tx());
1955 position->set_y(transform.ty());
1956
1957 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
1958 requestedPosition->set_x(requestedTransform.tx());
1959 requestedPosition->set_y(requestedTransform.ty());
1960
1961 SizeProto* size = layerInfo->mutable_size();
1962 size->set_w(state.active.w);
1963 size->set_h(state.active.h);
1964
1965 LayerProtoHelper::writeToProto(state.crop, layerInfo->mutable_crop());
1966 LayerProtoHelper::writeToProto(state.finalCrop, layerInfo->mutable_final_crop());
1967
1968 layerInfo->set_is_opaque(isOpaque(state));
1969 layerInfo->set_invalidate(contentDirty);
Chia-I Wu1b6bafc2018-05-22 12:03:00 -07001970
1971 // XXX (b/79210409) mCurrentDataSpace is not protected
1972 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
1973
chaviw1d044282017-09-27 12:19:28 -07001974 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1975 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
1976 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
1977 layerInfo->set_flags(state.flags);
1978
1979 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1980 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
1981
Jorim Jaggi8e0af362017-11-14 16:28:28 +01001982 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07001983 if (parent != nullptr) {
1984 layerInfo->set_parent(parent->sequence);
1985 }
1986
1987 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1988 if (zOrderRelativeOf != nullptr) {
1989 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1990 }
1991
Chia-I Wu1b6bafc2018-05-22 12:03:00 -07001992 // XXX getBE().compositionInfo.mBuffer is not protected
David Sodman0cc69182017-11-17 12:12:07 -08001993 auto buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001994 if (buffer != nullptr) {
1995 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
chaviw1d044282017-09-27 12:19:28 -07001996 }
1997
1998 layerInfo->set_queued_frames(getQueuedFrameCount());
1999 layerInfo->set_refresh_pending(isBufferLatched());
rongliuccd34842018-03-14 12:26:23 -07002000 layerInfo->set_window_type(state.type);
2001 layerInfo->set_app_id(state.appId);
chaviw9885bd42018-07-10 16:57:27 -07002002 layerInfo->set_curr_frame(mCurrentFrameNumber);
2003
2004 for (const auto& pendingState : mPendingStates) {
2005 auto barrierLayer = pendingState.barrierLayer.promote();
2006 if (barrierLayer != nullptr) {
2007 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
2008 barrierLayerProto->set_id(barrierLayer->sequence);
2009 barrierLayerProto->set_frame_number(pendingState.frameNumber);
2010 }
2011 }
chaviw1d044282017-09-27 12:19:28 -07002012}
2013
Yiwei Zhang068e31b2018-02-21 13:02:45 -08002014void Layer::writeToProto(LayerProto* layerInfo, int32_t hwcId) {
Peiyong Lin0874d2b2018-09-04 09:39:34 -07002015 if (!hasHwcLayer(hwcId)) {
2016 return;
2017 }
Yiwei Zhang068e31b2018-02-21 13:02:45 -08002018 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
2019
2020 const auto& hwcInfo = getBE().mHwcLayers.at(hwcId);
2021
2022 const Rect& frame = hwcInfo.displayFrame;
2023 LayerProtoHelper::writeToProto(frame, layerInfo->mutable_hwc_frame());
2024
2025 const FloatRect& crop = hwcInfo.sourceCrop;
2026 LayerProtoHelper::writeToProto(crop, layerInfo->mutable_hwc_crop());
2027
2028 const int32_t transform = static_cast<int32_t>(hwcInfo.transform);
2029 layerInfo->set_hwc_transform(transform);
Yiwei Zhang7c64f172018-03-07 14:52:28 -08002030
2031 const int32_t compositionType = static_cast<int32_t>(hwcInfo.compositionType);
2032 layerInfo->set_hwc_composition_type(compositionType);
2033
2034 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
2035 static_cast<BufferLayer*>(this)->isProtected()) {
2036 layerInfo->set_is_protected(true);
2037 } else {
2038 layerInfo->set_is_protected(false);
2039 }
Yiwei Zhang068e31b2018-02-21 13:02:45 -08002040}
2041
Mathias Agopian13127d82013-03-05 17:47:11 -08002042// ---------------------------------------------------------------------------
2043
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002044}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002045
2046#if defined(__gl_h_)
2047#error "don't include gl/gl.h in this file"
2048#endif
2049
2050#if defined(__gl2_h_)
2051#error "don't include gl2/gl2.h in this file"
2052#endif