blob: a14bb98d8daa5cc5d67d95533f018c1198148d8e [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();
David Sodman6f65f3e2017-11-03 14:28:09 -0700503 auto& hwcInfo = getBE().mHwcLayers[hwcId];
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700504
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700505 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800506 hwcInfo.forceClientComposition = false;
507
508 if (isSecure() && !displayDevice->isSecure()) {
509 hwcInfo.forceClientComposition = true;
510 }
511
512 auto& hwcLayer = hwcInfo.layer;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700513
Mathias Agopian13127d82013-03-05 17:47:11 -0800514 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700515 const State& s(getDrawingState());
David Revemanecf0fa52017-03-03 11:32:44 -0500516 auto blendMode = HWC2::BlendMode::None;
Robert Carr6452f122017-03-21 10:41:29 -0700517 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800518 blendMode =
519 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800520 }
David Revemanecf0fa52017-03-03 11:32:44 -0500521 auto error = hwcLayer->setBlendMode(blendMode);
David Sodman41fdfc92017-11-06 16:09:56 -0800522 ALOGE_IF(error != HWC2::Error::None,
523 "[%s] Failed to set blend mode %s:"
524 " %s (%d)",
525 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
526 static_cast<int32_t>(error));
Mathias Agopian13127d82013-03-05 17:47:11 -0800527
528 // apply the layer's transform, followed by the display's global transform
529 // here we're guaranteed that the layer's transform preserves rects
Michael Lentine6c925ed2014-09-26 17:55:01 -0700530 Region activeTransparentRegion(s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700531 Transform t = getTransform();
Robert Carrb5d3d262016-03-25 15:08:13 -0700532 if (!s.crop.isEmpty()) {
533 Rect activeCrop(s.crop);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700534 activeCrop = t.transform(activeCrop);
David Sodman41fdfc92017-11-06 16:09:56 -0800535 if (!activeCrop.intersect(displayDevice->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000536 activeCrop.clear();
537 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700538 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800539 // This needs to be here as transform.transform(Rect) computes the
540 // transformed rect and then takes the bounding box of the result before
541 // returning. This means
542 // transform.inverse().transform(transform.transform(Rect)) != Rect
543 // in which case we need to make sure the final rect is clipped to the
544 // display bounds.
David Sodman41fdfc92017-11-06 16:09:56 -0800545 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000546 activeCrop.clear();
547 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700548 // mark regions outside the crop as transparent
549 activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top));
David Sodman41fdfc92017-11-06 16:09:56 -0800550 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom, s.active.w, s.active.h));
551 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
552 activeTransparentRegion.orSelf(
553 Rect(activeCrop.right, activeCrop.top, s.active.w, activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700554 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700555
Dan Stoza80d61162017-12-20 15:57:52 -0800556 // computeBounds returns a FloatRect to provide more accuracy during the
557 // transformation. We then round upon constructing 'frame'.
558 Rect frame{t.transform(computeBounds(activeTransparentRegion))};
Robert Carrb5d3d262016-03-25 15:08:13 -0700559 if (!s.finalCrop.isEmpty()) {
David Sodman41fdfc92017-11-06 16:09:56 -0800560 if (!frame.intersect(s.finalCrop, &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000561 frame.clear();
562 }
563 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000564 if (!frame.intersect(displayDevice->getViewport(), &frame)) {
565 frame.clear();
566 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800567 const Transform& tr(displayDevice->getTransform());
568 Rect transformedFrame = tr.transform(frame);
David Revemanecf0fa52017-03-03 11:32:44 -0500569 error = hwcLayer->setDisplayFrame(transformedFrame);
Dan Stozae22aec72016-08-01 13:20:59 -0700570 if (error != HWC2::Error::None) {
David Sodman41fdfc92017-11-06 16:09:56 -0800571 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
572 transformedFrame.left, transformedFrame.top, transformedFrame.right,
573 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stozae22aec72016-08-01 13:20:59 -0700574 } else {
575 hwcInfo.displayFrame = transformedFrame;
576 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800577
Dan Stoza5a423ea2017-02-16 14:10:39 -0800578 FloatRect sourceCrop = computeCrop(displayDevice);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800579 error = hwcLayer->setSourceCrop(sourceCrop);
Dan Stozae22aec72016-08-01 13:20:59 -0700580 if (error != HWC2::Error::None) {
581 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
David Sodman41fdfc92017-11-06 16:09:56 -0800582 "%s (%d)",
583 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
584 to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stozae22aec72016-08-01 13:20:59 -0700585 } else {
586 hwcInfo.sourceCrop = sourceCrop;
587 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800588
chaviw13fdc492017-06-27 12:40:18 -0700589 float alpha = static_cast<float>(getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -0700590 error = hwcLayer->setPlaneAlpha(alpha);
David Sodman41fdfc92017-11-06 16:09:56 -0800591 ALOGE_IF(error != HWC2::Error::None,
592 "[%s] Failed to set plane alpha %.3f: "
593 "%s (%d)",
594 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800595
Robert Carrae060832016-11-28 10:51:00 -0800596 error = hwcLayer->setZOrder(z);
David Sodman41fdfc92017-11-06 16:09:56 -0800597 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
598 to_string(error).c_str(), static_cast<int32_t>(error));
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500599
Albert Chaulk2a589632017-05-04 16:59:44 -0400600 int type = s.type;
601 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700602 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400603 if (parent.get()) {
604 auto& parentState = parent->getDrawingState();
rongliuccd34842018-03-14 12:26:23 -0700605 if (parentState.type >= 0 || parentState.appId >= 0) {
606 type = parentState.type;
607 appId = parentState.appId;
608 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400609 }
610
611 error = hwcLayer->setInfo(type, appId);
David Sodman41fdfc92017-11-06 16:09:56 -0800612 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
613 static_cast<int32_t>(error));
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800614
Mathias Agopian29a367b2011-07-12 14:51:45 -0700615 /*
616 * Transformations are applied in this order:
617 * 1) buffer orientation/flip/mirror
618 * 2) state transformation (window manager)
619 * 3) layer orientation (screen orientation)
620 * (NOTE: the matrices are multiplied in reverse order)
621 */
622
623 const Transform bufferOrientation(mCurrentTransform);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700624 Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700625
Robert Carrcae605c2017-03-29 12:10:31 -0700626 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700627 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700628 * the code below applies the primary display's inverse transform to the
629 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700630 */
David Sodman41fdfc92017-11-06 16:09:56 -0800631 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700632 // calculate the inverse transform
633 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800634 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700635 }
Robert Carrcae605c2017-03-29 12:10:31 -0700636
637 /*
638 * Here we cancel out the orientation component of the WM transform.
639 * The scaling and translate components are already included in our bounds
640 * computation so it's enough to just omit it in the composition.
641 * See comment in onDraw with ref to b/36727915 for why.
642 */
643 transform = Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700644 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700645
646 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800647 const uint32_t orientation = transform.getOrientation();
Jorim Jaggif3bd94a2018-03-27 15:38:03 +0200648 if (orientation & Transform::ROT_INVALID) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800649 // we can only handle simple transformation
650 hwcInfo.forceClientComposition = true;
651 } else {
652 auto transform = static_cast<HWC2::Transform>(orientation);
Yiwei Zhang068e31b2018-02-21 13:02:45 -0800653 hwcInfo.transform = transform;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800654 auto error = hwcLayer->setTransform(transform);
David Sodman41fdfc92017-11-06 16:09:56 -0800655 ALOGE_IF(error != HWC2::Error::None,
656 "[%s] Failed to set transform %s: "
657 "%s (%d)",
658 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
659 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800660 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700661}
662
Dan Stoza9e56aa02015-11-02 13:00:03 -0800663void Layer::forceClientComposition(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700664 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800665 ALOGE("forceClientComposition: no HWC layer found (%d)", hwcId);
666 return;
667 }
668
David Sodman6f65f3e2017-11-03 14:28:09 -0700669 getBE().mHwcLayers[hwcId].forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800670}
Dan Stozaee44edd2015-03-23 15:50:23 -0700671
chaviwc9232ed2017-11-14 15:31:15 -0800672bool Layer::getForceClientComposition(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700673 if (getBE().mHwcLayers.count(hwcId) == 0) {
chaviwc9232ed2017-11-14 15:31:15 -0800674 ALOGE("getForceClientComposition: no HWC layer found (%d)", hwcId);
675 return false;
676 }
677
David Sodman6f65f3e2017-11-03 14:28:09 -0700678 return getBE().mHwcLayers[hwcId].forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800679}
680
Dan Stoza9e56aa02015-11-02 13:00:03 -0800681void Layer::updateCursorPosition(const sp<const DisplayDevice>& displayDevice) {
682 auto hwcId = displayDevice->getHwcDisplayId();
David Sodman6f65f3e2017-11-03 14:28:09 -0700683 if (getBE().mHwcLayers.count(hwcId) == 0 ||
David Sodman9eeae692017-11-02 10:53:32 -0700684 getCompositionType(hwcId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800685 return;
686 }
687
688 // This gives us only the "orientation" component of the transform
689 const State& s(getCurrentState());
690
691 // Apply the layer's transform, followed by the display's global transform
692 // Here we're guaranteed that the layer's transform preserves rects
693 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700694 if (!s.crop.isEmpty()) {
695 win.intersect(s.crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800696 }
697 // Subtract the transparent region and snap to the bounds
698 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700699 Rect frame(getTransform().transform(bounds));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800700 frame.intersect(displayDevice->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700701 if (!s.finalCrop.isEmpty()) {
702 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000703 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800704 auto& displayTransform(displayDevice->getTransform());
705 auto position = displayTransform.transform(frame);
706
David Sodman6f65f3e2017-11-03 14:28:09 -0700707 auto error = getBE().mHwcLayers[hwcId].layer->setCursorPosition(position.left,
David Sodman9eeae692017-11-02 10:53:32 -0700708 position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800709 ALOGE_IF(error != HWC2::Error::None,
710 "[%s] Failed to set cursor position "
711 "to (%d, %d): %s (%d)",
712 mName.string(), position.left, position.top, to_string(error).c_str(),
713 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800714}
Riley Andrews03414a12014-07-01 14:22:59 -0700715
Mathias Agopian13127d82013-03-05 17:47:11 -0800716// ---------------------------------------------------------------------------
717// drawing...
718// ---------------------------------------------------------------------------
719
chaviwa76b2712017-09-20 12:02:26 -0700720void Layer::draw(const RenderArea& renderArea, const Region& clip) const {
721 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800722}
723
chaviwa76b2712017-09-20 12:02:26 -0700724void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) const {
725 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800726}
727
chaviwa76b2712017-09-20 12:02:26 -0700728void Layer::draw(const RenderArea& renderArea) const {
729 onDraw(renderArea, Region(renderArea.getBounds()), false);
Dan Stozac7014012014-02-14 15:03:43 -0800730}
731
David Sodman41fdfc92017-11-06 16:09:56 -0800732void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
733 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800734 auto& engine(mFlinger->getRenderEngine());
David Sodman9eeae692017-11-02 10:53:32 -0700735 computeGeometry(renderArea, getBE().mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700736 engine.setupFillWithColor(red, green, blue, alpha);
David Sodman9eeae692017-11-02 10:53:32 -0700737 engine.drawMesh(getBE().mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800738}
739
chaviwa76b2712017-09-20 12:02:26 -0700740void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
David Sodman41fdfc92017-11-06 16:09:56 -0800741 clearWithOpenGL(renderArea, 0, 0, 0, 0);
Mathias Agopian13127d82013-03-05 17:47:11 -0800742}
743
David Sodman41fdfc92017-11-06 16:09:56 -0800744void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type, bool callIntoHwc) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700745 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800746 ALOGE("setCompositionType called without a valid HWC layer");
747 return;
748 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700749 auto& hwcInfo = getBE().mHwcLayers[hwcId];
Dan Stoza9e56aa02015-11-02 13:00:03 -0800750 auto& hwcLayer = hwcInfo.layer;
David Sodman41fdfc92017-11-06 16:09:56 -0800751 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(), to_string(type).c_str(),
752 static_cast<int>(callIntoHwc));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800753 if (hwcInfo.compositionType != type) {
754 ALOGV(" actually setting");
755 hwcInfo.compositionType = type;
756 if (callIntoHwc) {
757 auto error = hwcLayer->setCompositionType(type);
David Sodman41fdfc92017-11-06 16:09:56 -0800758 ALOGE_IF(error != HWC2::Error::None,
759 "[%s] Failed to set "
760 "composition type %s: %s (%d)",
761 mName.string(), to_string(type).c_str(), to_string(error).c_str(),
762 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800763 }
764 }
765}
766
767HWC2::Composition Layer::getCompositionType(int32_t hwcId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -0700768 if (hwcId == DisplayDevice::DISPLAY_ID_INVALID) {
769 // If we're querying the composition type for a display that does not
770 // have a HWC counterpart, then it will always be Client
771 return HWC2::Composition::Client;
772 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700773 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700774 ALOGE("getCompositionType called with an invalid HWC layer");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800775 return HWC2::Composition::Invalid;
776 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700777 return getBE().mHwcLayers.at(hwcId).compositionType;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800778}
779
780void Layer::setClearClientTarget(int32_t hwcId, bool clear) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700781 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800782 ALOGE("setClearClientTarget called without a valid HWC layer");
783 return;
784 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700785 getBE().mHwcLayers[hwcId].clearClientTarget = clear;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800786}
787
788bool Layer::getClearClientTarget(int32_t hwcId) const {
David Sodman6f65f3e2017-11-03 14:28:09 -0700789 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800790 ALOGE("getClearClientTarget called without a valid HWC layer");
791 return false;
792 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700793 return getBE().mHwcLayers.at(hwcId).clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800794}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800795
Dan Stozacac35382016-01-27 12:21:06 -0800796bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
797 if (point->getFrameNumber() <= mCurrentFrameNumber) {
798 // Don't bother with a SyncPoint, since we've already latched the
799 // relevant frame
800 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700801 }
802
Dan Stozacac35382016-01-27 12:21:06 -0800803 Mutex::Autolock lock(mLocalSyncPointMutex);
804 mLocalSyncPoints.push_back(point);
805 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700806}
807
Mathias Agopian13127d82013-03-05 17:47:11 -0800808void Layer::setFiltering(bool filtering) {
809 mFiltering = filtering;
810}
811
812bool Layer::getFiltering() const {
813 return mFiltering;
814}
815
Mathias Agopian13127d82013-03-05 17:47:11 -0800816// ----------------------------------------------------------------------------
817// local state
818// ----------------------------------------------------------------------------
819
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000820static void boundPoint(vec2* point, const Rect& crop) {
821 if (point->x < crop.left) {
822 point->x = crop.left;
823 }
824 if (point->x > crop.right) {
825 point->x = crop.right;
826 }
827 if (point->y < crop.top) {
828 point->y = crop.top;
829 }
830 if (point->y > crop.bottom) {
831 point->y = crop.bottom;
832 }
833}
834
chaviwa76b2712017-09-20 12:02:26 -0700835void Layer::computeGeometry(const RenderArea& renderArea, Mesh& mesh,
836 bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700837 const Layer::State& s(getDrawingState());
chaviwa76b2712017-09-20 12:02:26 -0700838 const Transform renderAreaTransform(renderArea.getTransform());
839 const uint32_t height = renderArea.getHeight();
Dan Stoza80d61162017-12-20 15:57:52 -0800840 FloatRect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700841
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000842 vec2 lt = vec2(win.left, win.top);
843 vec2 lb = vec2(win.left, win.bottom);
844 vec2 rb = vec2(win.right, win.bottom);
845 vec2 rt = vec2(win.right, win.top);
846
Robert Carr1f0a16a2016-10-24 16:27:39 -0700847 Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000848 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700849 lt = layerTransform.transform(lt);
850 lb = layerTransform.transform(lb);
851 rb = layerTransform.transform(rb);
852 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000853 }
854
Robert Carrb5d3d262016-03-25 15:08:13 -0700855 if (!s.finalCrop.isEmpty()) {
856 boundPoint(&lt, s.finalCrop);
857 boundPoint(&lb, s.finalCrop);
858 boundPoint(&rb, s.finalCrop);
859 boundPoint(&rt, s.finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000860 }
861
Mathias Agopianff2ed702013-09-01 21:36:12 -0700862 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700863 position[0] = renderAreaTransform.transform(lt);
864 position[1] = renderAreaTransform.transform(lb);
865 position[2] = renderAreaTransform.transform(rb);
866 position[3] = renderAreaTransform.transform(rt);
David Sodman41fdfc92017-11-06 16:09:56 -0800867 for (size_t i = 0; i < 4; i++) {
chaviwa76b2712017-09-20 12:02:26 -0700868 position[i].y = height - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -0800869 }
870}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800871
David Sodman41fdfc92017-11-06 16:09:56 -0800872bool Layer::isSecure() const {
Dan Stoza23116082015-06-18 14:58:39 -0700873 const Layer::State& s(mDrawingState);
874 return (s.flags & layer_state_t::eLayerSecure);
875}
876
Mathias Agopian13127d82013-03-05 17:47:11 -0800877void Layer::setVisibleRegion(const Region& visibleRegion) {
878 // always called from main thread
879 this->visibleRegion = visibleRegion;
880}
881
882void Layer::setCoveredRegion(const Region& coveredRegion) {
883 // always called from main thread
884 this->coveredRegion = coveredRegion;
885}
886
David Sodman41fdfc92017-11-06 16:09:56 -0800887void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800888 // always called from main thread
889 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
890}
891
Robert Carre5f4f692018-01-12 13:12:28 -0800892void Layer::clearVisibilityRegions() {
893 visibleRegion.clear();
894 visibleNonTransparentRegion.clear();
895 coveredRegion.clear();
896}
897
Mathias Agopian13127d82013-03-05 17:47:11 -0800898// ----------------------------------------------------------------------------
899// transaction
900// ----------------------------------------------------------------------------
901
Dan Stoza7dde5992015-05-22 09:51:44 -0700902void Layer::pushPendingState() {
903 if (!mCurrentState.modified) {
904 return;
905 }
906
Dan Stoza7dde5992015-05-22 09:51:44 -0700907 // If this transaction is waiting on the receipt of a frame, generate a sync
908 // point and send it to the remote layer.
Robert Carr0d480722017-01-10 16:42:54 -0800909 if (mCurrentState.barrierLayer != nullptr) {
910 sp<Layer> barrierLayer = mCurrentState.barrierLayer.promote();
911 if (barrierLayer == nullptr) {
912 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700913 // If we can't promote the layer we are intended to wait on,
914 // then it is expired or otherwise invalid. Allow this transaction
915 // to be applied as per normal (no synchronization).
Robert Carr0d480722017-01-10 16:42:54 -0800916 mCurrentState.barrierLayer = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800917 } else {
David Sodman41fdfc92017-11-06 16:09:56 -0800918 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber);
Robert Carr0d480722017-01-10 16:42:54 -0800919 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800920 mRemoteSyncPoints.push_back(std::move(syncPoint));
921 } else {
922 // We already missed the frame we're supposed to synchronize
923 // on, so go ahead and apply the state update
Robert Carr0d480722017-01-10 16:42:54 -0800924 mCurrentState.barrierLayer = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800925 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700926 }
927
Dan Stoza7dde5992015-05-22 09:51:44 -0700928 // Wake us up to check if the frame has been received
929 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700930 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700931 }
932 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700933 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700934}
935
Pablo Ceballos05289c22016-04-14 15:49:55 -0700936void Layer::popPendingState(State* stateToCommit) {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700937 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700938
939 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700940 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700941}
942
Pablo Ceballos05289c22016-04-14 15:49:55 -0700943bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700944 bool stateUpdateAvailable = false;
945 while (!mPendingStates.empty()) {
Robert Carr0d480722017-01-10 16:42:54 -0800946 if (mPendingStates[0].barrierLayer != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700947 if (mRemoteSyncPoints.empty()) {
948 // If we don't have a sync point for this, apply it anyway. It
949 // will be visually wrong, but it should keep us from getting
950 // into too much trouble.
951 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700952 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700953 stateUpdateAvailable = true;
954 continue;
955 }
956
David Sodman41fdfc92017-11-06 16:09:56 -0800957 if (mRemoteSyncPoints.front()->getFrameNumber() != mPendingStates[0].frameNumber) {
958 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800959
960 // Signal our end of the sync point and then dispose of it
961 mRemoteSyncPoints.front()->setTransactionApplied();
962 mRemoteSyncPoints.pop_front();
963 continue;
964 }
965
Dan Stoza7dde5992015-05-22 09:51:44 -0700966 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
967 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700968 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700969 stateUpdateAvailable = true;
970
971 // Signal our end of the sync point and then dispose of it
972 mRemoteSyncPoints.front()->setTransactionApplied();
973 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800974 } else {
975 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700976 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700977 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700978 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700979 stateUpdateAvailable = true;
980 }
981 }
982
983 // If we still have pending updates, wake SurfaceFlinger back up and point
984 // it at this layer so we can process them
985 if (!mPendingStates.empty()) {
986 setTransactionFlags(eTransactionNeeded);
987 mFlinger->setTransactionFlags(eTraversalNeeded);
988 }
989
990 mCurrentState.modified = false;
991 return stateUpdateAvailable;
992}
993
Mathias Agopian13127d82013-03-05 17:47:11 -0800994uint32_t Layer::doTransaction(uint32_t flags) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800995 ATRACE_CALL();
996
Dan Stoza7dde5992015-05-22 09:51:44 -0700997 pushPendingState();
Pablo Ceballos05289c22016-04-14 15:49:55 -0700998 Layer::State c = getCurrentState();
999 if (!applyPendingStates(&c)) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001000 return 0;
1001 }
1002
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001003 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001004
David Sodman41fdfc92017-11-06 16:09:56 -08001005 const bool sizeChanged = (c.requested.w != s.requested.w) || (c.requested.h != s.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -07001006
David Sodmaneb085e02017-10-05 18:49:04 -07001007 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001008 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +00001009 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -08001010 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
1011 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1012 " requested={ wh={%4u,%4u} }}\n"
1013 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1014 " requested={ wh={%4u,%4u} }}\n",
David Sodman9eeae692017-11-02 10:53:32 -07001015 this, getName().string(), mCurrentTransform,
1016 getEffectiveScalingMode(), c.active.w, c.active.h, c.crop.left, c.crop.top,
1017 c.crop.right, c.crop.bottom, c.crop.getWidth(), c.crop.getHeight(), c.requested.w,
1018 c.requested.h, s.active.w, s.active.h, s.crop.left, s.crop.top, s.crop.right,
1019 s.crop.bottom, s.crop.getWidth(), s.crop.getHeight(), s.requested.w,
1020 s.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001021
Jamie Gennis2a0d5b62011-09-26 16:54:44 -07001022 // record the new size, form this point on, when the client request
1023 // a buffer, it'll get the new size.
David Sodmaneb085e02017-10-05 18:49:04 -07001024 setDefaultBufferSize(c.requested.w, c.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001025 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001026
Robert Carre392b552017-09-19 12:16:05 -07001027 // Don't let Layer::doTransaction update the drawing state
1028 // if we have a pending resize, unless we are in fixed-size mode.
1029 // the drawing state will be updated only once we receive a buffer
1030 // with the correct size.
1031 //
1032 // In particular, we want to make sure the clip (which is part
1033 // of the geometry state) is latched together with the size but is
1034 // latched immediately when no resizing is involved.
1035 //
1036 // If a sideband stream is attached, however, we want to skip this
1037 // optimization so that transactions aren't missed when a buffer
1038 // never arrives
1039 //
1040 // In the case that we don't have a buffer we ignore other factors
1041 // and avoid entering the resizePending state. At a high level the
1042 // resizePending state is to avoid applying the state of the new buffer
1043 // to the old buffer. However in the state where we don't have an old buffer
1044 // there is no such concern but we may still be being used as a parent layer.
David Sodman41fdfc92017-11-06 16:09:56 -08001045 const bool resizePending = ((c.requested.w != c.active.w) || (c.requested.h != c.active.h)) &&
David Sodman0cc69182017-11-17 12:12:07 -08001046 (getBE().compositionInfo.mBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001047 if (!isFixedSize()) {
David Sodman0cc69182017-11-17 12:12:07 -08001048 if (resizePending && getBE().compositionInfo.hwc.sidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001049 flags |= eDontUpdateGeometryState;
1050 }
1051 }
1052
Robert Carr7bf247e2017-05-18 14:02:49 -07001053 // Here we apply various requested geometry states, depending on our
1054 // latching configuration. See Layer.h for a detailed discussion of
1055 // how geometry latching is controlled.
1056 if (!(flags & eDontUpdateGeometryState)) {
Pablo Ceballos7d052572016-06-02 17:46:05 -07001057 Layer::State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001058
1059 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1060 // mode, which causes attributes which normally latch regardless of scaling mode,
1061 // to be delayed. We copy the requested state to the active state making sure
1062 // to respect these rules (again see Layer.h for a detailed discussion).
1063 //
1064 // There is an awkward asymmetry in the handling of the crop states in the position
1065 // states, as can be seen below. Largely this arises from position and transform
1066 // being stored in the same data structure while having different latching rules.
1067 // b/38182305
1068 //
1069 // Careful that "c" and editCurrentState may not begin as equivalent due to
1070 // applyPendingStates in the presence of deferred transactions.
1071 if (mFreezeGeometryUpdates) {
Robert Carr82364e32016-05-15 11:27:47 -07001072 float tx = c.active.transform.tx();
1073 float ty = c.active.transform.ty();
1074 c.active = c.requested;
1075 c.active.transform.set(tx, ty);
1076 editCurrentState.active = c.active;
1077 } else {
1078 editCurrentState.active = editCurrentState.requested;
1079 c.active = c.requested;
1080 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001081 }
1082
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001083 if (s.active != c.active) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001084 // invalidate and recompute the visible regions if needed
1085 flags |= Layer::eVisibleRegion;
1086 }
1087
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001088 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001089 // invalidate and recompute the visible regions if needed
1090 flags |= eVisibleRegion;
1091 this->contentDirty = true;
1092
1093 // we may use linear filtering, if the matrix scales us
Robert Carr3dcabfa2016-03-01 18:36:58 -08001094 const uint8_t type = c.active.transform.getType();
David Sodman41fdfc92017-11-06 16:09:56 -08001095 mNeedsFiltering = (!c.active.transform.preserveRects() || (type >= Transform::SCALE));
Mathias Agopian13127d82013-03-05 17:47:11 -08001096 }
1097
Dan Stozac8145172016-04-28 16:29:06 -07001098 // If the layer is hidden, signal and clear out all local sync points so
1099 // that transactions for layers depending on this layer's frames becoming
1100 // visible are not blocked
1101 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001102 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001103 }
1104
Mathias Agopian13127d82013-03-05 17:47:11 -08001105 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001106 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001107 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001108}
1109
Pablo Ceballos05289c22016-04-14 15:49:55 -07001110void Layer::commitTransaction(const State& stateToCommit) {
1111 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001112}
1113
Mathias Agopian13127d82013-03-05 17:47:11 -08001114uint32_t Layer::getTransactionFlags(uint32_t flags) {
1115 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1116}
1117
1118uint32_t Layer::setTransactionFlags(uint32_t flags) {
1119 return android_atomic_or(flags, &mTransactionFlags);
1120}
1121
Robert Carr82364e32016-05-15 11:27:47 -07001122bool Layer::setPosition(float x, float y, bool immediate) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001123 if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001124 return false;
1125 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001126
1127 // We update the requested and active position simultaneously because
1128 // we want to apply the position portion of the transform matrix immediately,
1129 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Robert Carr3dcabfa2016-03-01 18:36:58 -08001130 mCurrentState.requested.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001131 if (immediate && !mFreezeGeometryUpdates) {
1132 // Here we directly update the active state
1133 // unlike other setters, because we store it within
1134 // the transform, but use different latching rules.
1135 // b/38182305
Robert Carr82364e32016-05-15 11:27:47 -07001136 mCurrentState.active.transform.set(x, y);
1137 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001138 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001139
Dan Stoza7dde5992015-05-22 09:51:44 -07001140 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001141 setTransactionFlags(eTransactionNeeded);
1142 return true;
1143}
Robert Carr82364e32016-05-15 11:27:47 -07001144
Robert Carr1f0a16a2016-10-24 16:27:39 -07001145bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1146 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1147 if (idx < 0) {
1148 return false;
1149 }
1150 if (childLayer->setLayer(z)) {
1151 mCurrentChildren.removeAt(idx);
1152 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001153 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001154 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001155 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001156}
1157
Robert Carr503c7042017-09-27 15:06:08 -07001158bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1159 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1160 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1161 if (idx < 0) {
1162 return false;
1163 }
1164 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1165 mCurrentChildren.removeAt(idx);
1166 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001167 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001168 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001169 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001170}
1171
Robert Carrae060832016-11-28 10:51:00 -08001172bool Layer::setLayer(int32_t z) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001173 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001174 mCurrentState.sequence++;
1175 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001176 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001177
1178 // Discard all relative layering.
1179 if (mCurrentState.zOrderRelativeOf != nullptr) {
1180 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1181 if (strongRelative != nullptr) {
1182 strongRelative->removeZOrderRelative(this);
1183 }
1184 mCurrentState.zOrderRelativeOf = nullptr;
1185 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001186 setTransactionFlags(eTransactionNeeded);
1187 return true;
1188}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001189
Robert Carrdb66e622017-04-10 16:55:57 -07001190void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1191 mCurrentState.zOrderRelatives.remove(relative);
1192 mCurrentState.sequence++;
1193 mCurrentState.modified = true;
1194 setTransactionFlags(eTransactionNeeded);
1195}
1196
1197void Layer::addZOrderRelative(const wp<Layer>& relative) {
1198 mCurrentState.zOrderRelatives.add(relative);
1199 mCurrentState.modified = true;
1200 mCurrentState.sequence++;
1201 setTransactionFlags(eTransactionNeeded);
1202}
1203
Robert Carr503d2bd2017-12-04 15:49:47 -08001204bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001205 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1206 if (handle == nullptr) {
1207 return false;
1208 }
1209 sp<Layer> relative = handle->owner.promote();
1210 if (relative == nullptr) {
1211 return false;
1212 }
1213
Robert Carr503d2bd2017-12-04 15:49:47 -08001214 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1215 mCurrentState.zOrderRelativeOf == relative) {
1216 return false;
1217 }
1218
Robert Carrdb66e622017-04-10 16:55:57 -07001219 mCurrentState.sequence++;
1220 mCurrentState.modified = true;
Robert Carr503d2bd2017-12-04 15:49:47 -08001221 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001222
chaviw9ab4bd12017-11-03 13:11:00 -07001223 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
1224 if (oldZOrderRelativeOf != nullptr) {
1225 oldZOrderRelativeOf->removeZOrderRelative(this);
1226 }
Robert Carrdb66e622017-04-10 16:55:57 -07001227 mCurrentState.zOrderRelativeOf = relative;
1228 relative->addZOrderRelative(this);
1229
1230 setTransactionFlags(eTransactionNeeded);
1231
1232 return true;
1233}
1234
Mathias Agopian13127d82013-03-05 17:47:11 -08001235bool Layer::setSize(uint32_t w, uint32_t h) {
David Sodman41fdfc92017-11-06 16:09:56 -08001236 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001237 mCurrentState.requested.w = w;
1238 mCurrentState.requested.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001239 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001240 setTransactionFlags(eTransactionNeeded);
1241 return true;
1242}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001243bool Layer::setAlpha(float alpha) {
David Sodman41fdfc92017-11-06 16:09:56 -08001244 if (mCurrentState.color.a == alpha) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001245 mCurrentState.sequence++;
chaviw13fdc492017-06-27 12:40:18 -07001246 mCurrentState.color.a = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001247 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001248 setTransactionFlags(eTransactionNeeded);
1249 return true;
1250}
chaviw13fdc492017-06-27 12:40:18 -07001251
1252bool Layer::setColor(const half3& color) {
David Sodman41fdfc92017-11-06 16:09:56 -08001253 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g &&
1254 color.b == mCurrentState.color.b)
chaviw13fdc492017-06-27 12:40:18 -07001255 return false;
1256
1257 mCurrentState.sequence++;
1258 mCurrentState.color.r = color.r;
1259 mCurrentState.color.g = color.g;
1260 mCurrentState.color.b = color.b;
1261 mCurrentState.modified = true;
1262 setTransactionFlags(eTransactionNeeded);
1263 return true;
1264}
1265
Robert Carrd4ae7f32018-06-07 16:10:57 -07001266bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1267 bool allowNonRectPreservingTransforms) {
1268 Transform t;
1269 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1270
1271 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1272 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1273 return false;
1274 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001275 mCurrentState.sequence++;
David Sodman41fdfc92017-11-06 16:09:56 -08001276 mCurrentState.requested.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001277 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001278 setTransactionFlags(eTransactionNeeded);
1279 return true;
1280}
1281bool Layer::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001282 mCurrentState.requestedTransparentRegion = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001283 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001284 setTransactionFlags(eTransactionNeeded);
1285 return true;
1286}
1287bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1288 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
David Sodman41fdfc92017-11-06 16:09:56 -08001289 if (mCurrentState.flags == newFlags) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001290 mCurrentState.sequence++;
1291 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001292 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001293 setTransactionFlags(eTransactionNeeded);
1294 return true;
1295}
Robert Carr99e27f02016-06-16 15:18:02 -07001296
1297bool Layer::setCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001298 if (mCurrentState.requestedCrop == crop) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001299 mCurrentState.sequence++;
Robert Carr99e27f02016-06-16 15:18:02 -07001300 mCurrentState.requestedCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001301 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr99e27f02016-06-16 15:18:02 -07001302 mCurrentState.crop = crop;
1303 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001304 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1305
Dan Stoza7dde5992015-05-22 09:51:44 -07001306 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001307 setTransactionFlags(eTransactionNeeded);
1308 return true;
1309}
Robert Carr8d5227b2017-03-16 15:41:03 -07001310
1311bool Layer::setFinalCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001312 if (mCurrentState.requestedFinalCrop == crop) return false;
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001313 mCurrentState.sequence++;
Robert Carr8d5227b2017-03-16 15:41:03 -07001314 mCurrentState.requestedFinalCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001315 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr8d5227b2017-03-16 15:41:03 -07001316 mCurrentState.finalCrop = crop;
1317 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001318 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1319
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001320 mCurrentState.modified = true;
1321 setTransactionFlags(eTransactionNeeded);
1322 return true;
1323}
Mathias Agopian13127d82013-03-05 17:47:11 -08001324
Robert Carrc3574f72016-03-24 12:19:32 -07001325bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001326 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001327 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001328 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001329 return true;
1330}
1331
rongliuccd34842018-03-14 12:26:23 -07001332void Layer::setInfo(int32_t type, int32_t appId) {
David Sodman41fdfc92017-11-06 16:09:56 -08001333 mCurrentState.appId = appId;
1334 mCurrentState.type = type;
1335 mCurrentState.modified = true;
1336 setTransactionFlags(eTransactionNeeded);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001337}
1338
Mathias Agopian13127d82013-03-05 17:47:11 -08001339bool Layer::setLayerStack(uint32_t layerStack) {
David Sodman41fdfc92017-11-06 16:09:56 -08001340 if (mCurrentState.layerStack == layerStack) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001341 mCurrentState.sequence++;
1342 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001343 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001344 setTransactionFlags(eTransactionNeeded);
1345 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001346}
1347
Robert Carr1f0a16a2016-10-24 16:27:39 -07001348uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001349 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001350 if (p == nullptr) {
1351 return getDrawingState().layerStack;
1352 }
1353 return p->getLayerStack();
1354}
1355
David Sodman41fdfc92017-11-06 16:09:56 -08001356void Layer::deferTransactionUntil(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001357 mCurrentState.barrierLayer = barrierLayer;
Dan Stoza7dde5992015-05-22 09:51:44 -07001358 mCurrentState.frameNumber = frameNumber;
1359 // We don't set eTransactionNeeded, because just receiving a deferral
1360 // request without any other state updates shouldn't actually induce a delay
1361 mCurrentState.modified = true;
1362 pushPendingState();
Robert Carr0d480722017-01-10 16:42:54 -08001363 mCurrentState.barrierLayer = nullptr;
Dan Stoza792e5292016-02-11 11:43:58 -08001364 mCurrentState.frameNumber = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001365 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001366}
1367
David Sodman41fdfc92017-11-06 16:09:56 -08001368void Layer::deferTransactionUntil(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001369 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
1370 deferTransactionUntil(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001371}
1372
Dan Stozaee44edd2015-03-23 15:50:23 -07001373
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001374// ----------------------------------------------------------------------------
1375// pageflip handling...
1376// ----------------------------------------------------------------------------
1377
Robert Carr1f0a16a2016-10-24 16:27:39 -07001378bool Layer::isHiddenByPolicy() const {
1379 const Layer::State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001380 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001381 if (parent != nullptr && parent->isHiddenByPolicy()) {
1382 return true;
1383 }
1384 return s.flags & layer_state_t::eLayerHidden;
1385}
1386
David Sodman41fdfc92017-11-06 16:09:56 -08001387uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001388 // TODO: should we do something special if mSecure is set?
1389 if (mProtectedByApp) {
1390 // need a hardware-protected path to external video sink
1391 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001392 }
Riley Andrews03414a12014-07-01 14:22:59 -07001393 if (mPotentialCursor) {
1394 usage |= GraphicBuffer::USAGE_CURSOR;
1395 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001396 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001397 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001398}
1399
Mathias Agopian84300952012-11-21 16:02:13 -08001400void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001401 uint32_t orientation = 0;
1402 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001403 // The transform hint is used to improve performance, but we can
1404 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001405 // apply to all displays.
Mathias Agopian42977342012-08-05 00:40:46 -07001406 const Transform& planeTransform(hw->getTransform());
Mathias Agopian4fec8732012-06-29 14:12:52 -07001407 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07001408 if (orientation & Transform::ROT_INVALID) {
1409 orientation = 0;
1410 }
1411 }
David Sodmaneb085e02017-10-05 18:49:04 -07001412 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001413}
1414
Mathias Agopian13127d82013-03-05 17:47:11 -08001415// ----------------------------------------------------------------------------
1416// debugging
1417// ----------------------------------------------------------------------------
1418
Kalle Raitaa099a242017-01-11 11:17:29 -08001419LayerDebugInfo Layer::getLayerDebugInfo() const {
1420 LayerDebugInfo info;
1421 const Layer::State& ds = getDrawingState();
1422 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001423 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001424 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
1425 info.mType = String8(getTypeId());
1426 info.mTransparentRegion = ds.activeTransparentRegion;
1427 info.mVisibleRegion = visibleRegion;
1428 info.mSurfaceDamageRegion = surfaceDamageRegion;
1429 info.mLayerStack = getLayerStack();
1430 info.mX = ds.active.transform.tx();
1431 info.mY = ds.active.transform.ty();
1432 info.mZ = ds.z;
1433 info.mWidth = ds.active.w;
1434 info.mHeight = ds.active.h;
1435 info.mCrop = ds.crop;
1436 info.mFinalCrop = ds.finalCrop;
chaviw13fdc492017-06-27 12:40:18 -07001437 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001438 info.mFlags = ds.flags;
1439 info.mPixelFormat = getPixelFormat();
Chia-I Wu1b6bafc2018-05-22 12:03:00 -07001440 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Kalle Raitaa099a242017-01-11 11:17:29 -08001441 info.mMatrix[0][0] = ds.active.transform[0][0];
1442 info.mMatrix[0][1] = ds.active.transform[0][1];
1443 info.mMatrix[1][0] = ds.active.transform[1][0];
1444 info.mMatrix[1][1] = ds.active.transform[1][1];
1445 {
David Sodman0cc69182017-11-17 12:12:07 -08001446 sp<const GraphicBuffer> buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001447 if (buffer != 0) {
1448 info.mActiveBufferWidth = buffer->getWidth();
1449 info.mActiveBufferHeight = buffer->getHeight();
1450 info.mActiveBufferStride = buffer->getStride();
1451 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001452 } else {
1453 info.mActiveBufferWidth = 0;
1454 info.mActiveBufferHeight = 0;
1455 info.mActiveBufferStride = 0;
1456 info.mActiveBufferFormat = 0;
1457 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001458 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001459 info.mNumQueuedFrames = getQueuedFrameCount();
1460 info.mRefreshPending = isBufferLatched();
1461 info.mIsOpaque = isOpaque(ds);
1462 info.mContentDirty = contentDirty;
1463 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001464}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001465
Dan Stozae22aec72016-08-01 13:20:59 -07001466void Layer::miniDumpHeader(String8& result) {
1467 result.append("----------------------------------------");
1468 result.append("---------------------------------------\n");
1469 result.append(" Layer name\n");
1470 result.append(" Z | ");
1471 result.append(" Comp Type | ");
1472 result.append(" Disp Frame (LTRB) | ");
1473 result.append(" Source Crop (LTRB)\n");
1474 result.append("----------------------------------------");
1475 result.append("---------------------------------------\n");
1476}
1477
1478void Layer::miniDump(String8& result, int32_t hwcId) const {
David Sodman6f65f3e2017-11-03 14:28:09 -07001479 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stozae22aec72016-08-01 13:20:59 -07001480 return;
1481 }
1482
1483 String8 name;
1484 if (mName.length() > 77) {
1485 std::string shortened;
1486 shortened.append(mName.string(), 36);
1487 shortened.append("[...]");
1488 shortened.append(mName.string() + (mName.length() - 36), 36);
1489 name = shortened.c_str();
1490 } else {
1491 name = mName;
1492 }
1493
1494 result.appendFormat(" %s\n", name.string());
1495
1496 const Layer::State& layerState(getDrawingState());
David Sodman6f65f3e2017-11-03 14:28:09 -07001497 const LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers.at(hwcId);
Chia-I Wu1e043612018-03-01 09:45:09 -08001498 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
1499 result.appendFormat(" rel %6d | ", layerState.z);
1500 } else {
1501 result.appendFormat(" %10d | ", layerState.z);
1502 }
David Sodman41fdfc92017-11-06 16:09:56 -08001503 result.appendFormat("%10s | ", to_string(getCompositionType(hwcId)).c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001504 const Rect& frame = hwcInfo.displayFrame;
David Sodman41fdfc92017-11-06 16:09:56 -08001505 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Dan Stoza5a423ea2017-02-16 14:10:39 -08001506 const FloatRect& crop = hwcInfo.sourceCrop;
David Sodman41fdfc92017-11-06 16:09:56 -08001507 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 -07001508
1509 result.append("- - - - - - - - - - - - - - - - - - - - ");
1510 result.append("- - - - - - - - - - - - - - - - - - - -\n");
1511}
Dan Stozae22aec72016-08-01 13:20:59 -07001512
Svetoslavd85084b2014-03-20 10:28:31 -07001513void Layer::dumpFrameStats(String8& result) const {
1514 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001515}
1516
Svetoslavd85084b2014-03-20 10:28:31 -07001517void Layer::clearFrameStats() {
1518 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001519}
1520
Jamie Gennis6547ff42013-07-16 20:12:42 -07001521void Layer::logFrameStats() {
1522 mFrameTracker.logAndResetStats(mName);
1523}
1524
Svetoslavd85084b2014-03-20 10:28:31 -07001525void Layer::getFrameStats(FrameStats* outStats) const {
1526 mFrameTracker.getStats(outStats);
1527}
1528
Brian Andersond6927fb2016-07-23 23:37:30 -07001529void Layer::dumpFrameEvents(String8& result) {
David Sodman41fdfc92017-11-06 16:09:56 -08001530 result.appendFormat("- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001531 Mutex::Autolock lock(mFrameEventHistoryMutex);
1532 mFrameEventHistory.checkFencesForCompletion();
1533 mFrameEventHistory.dump(result);
1534}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001535
Brian Anderson5ea5e592016-12-01 16:54:33 -08001536void Layer::onDisconnect() {
1537 Mutex::Autolock lock(mFrameEventHistoryMutex);
1538 mFrameEventHistory.onDisconnect();
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001539 mTimeStats.onDisconnect(getName().c_str());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001540}
1541
Brian Anderson3890c392016-07-25 12:48:08 -07001542void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001543 FrameEventHistoryDelta* outDelta) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001544 if (newTimestamps) {
1545 mTimeStats.setPostTime(getName().c_str(), newTimestamps->frameNumber,
1546 newTimestamps->postedTime);
1547 }
1548
Brian Andersond6927fb2016-07-23 23:37:30 -07001549 Mutex::Autolock lock(mFrameEventHistoryMutex);
1550 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001551 // If there are any unsignaled fences in the aquire timeline at this
1552 // point, the previously queued frame hasn't been latched yet. Go ahead
1553 // and try to get the signal time here so the syscall is taken out of
1554 // the main thread's critical path.
1555 mAcquireTimeline.updateSignalTimes();
1556 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001557 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001558 mFrameEventHistory.addQueue(*newTimestamps);
1559 }
1560
Brian Anderson3890c392016-07-25 12:48:08 -07001561 if (outDelta) {
1562 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001563 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001564}
Dan Stozae77c7662016-05-13 11:37:28 -07001565
Chia-I Wu98f1c102017-05-30 14:54:08 -07001566size_t Layer::getChildrenCount() const {
1567 size_t count = 0;
1568 for (const sp<Layer>& child : mCurrentChildren) {
1569 count += 1 + child->getChildrenCount();
1570 }
1571 return count;
1572}
1573
Robert Carr1f0a16a2016-10-24 16:27:39 -07001574void Layer::addChild(const sp<Layer>& layer) {
1575 mCurrentChildren.add(layer);
1576 layer->setParent(this);
1577}
1578
1579ssize_t Layer::removeChild(const sp<Layer>& layer) {
1580 layer->setParent(nullptr);
1581 return mCurrentChildren.remove(layer);
1582}
1583
Robert Carr1db73f62016-12-21 12:58:51 -08001584bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1585 sp<Handle> handle = nullptr;
1586 sp<Layer> newParent = nullptr;
1587 if (newParentHandle == nullptr) {
1588 return false;
1589 }
1590 handle = static_cast<Handle*>(newParentHandle.get());
1591 newParent = handle->owner.promote();
1592 if (newParent == nullptr) {
1593 ALOGE("Unable to promote Layer handle");
1594 return false;
1595 }
1596
1597 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001598 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001599
1600 sp<Client> client(child->mClientRef.promote());
1601 if (client != nullptr) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001602 client->updateParent(newParent);
Robert Carr1db73f62016-12-21 12:58:51 -08001603 }
1604 }
1605 mCurrentChildren.clear();
1606
1607 return true;
1608}
1609
Robert Carr15eae092018-03-23 13:43:53 -07001610void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001611 for (const sp<Layer>& child : mDrawingChildren) {
1612 child->mDrawingParent = newParent;
1613 }
1614}
1615
chaviwf1961f72017-09-18 16:41:07 -07001616bool Layer::reparent(const sp<IBinder>& newParentHandle) {
1617 if (newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001618 return false;
1619 }
1620
1621 auto handle = static_cast<Handle*>(newParentHandle.get());
1622 sp<Layer> newParent = handle->owner.promote();
1623 if (newParent == nullptr) {
1624 ALOGE("Unable to promote Layer handle");
1625 return false;
1626 }
1627
chaviwf1961f72017-09-18 16:41:07 -07001628 sp<Layer> parent = getParent();
1629 if (parent != nullptr) {
1630 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001631 }
chaviwf1961f72017-09-18 16:41:07 -07001632 newParent->addChild(this);
chaviw06178942017-07-27 10:25:59 -07001633
chaviwf1961f72017-09-18 16:41:07 -07001634 sp<Client> client(mClientRef.promote());
chaviw06178942017-07-27 10:25:59 -07001635 sp<Client> newParentClient(newParent->mClientRef.promote());
1636
chaviwf1961f72017-09-18 16:41:07 -07001637 if (client != newParentClient) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001638 client->updateParent(newParent);
chaviw06178942017-07-27 10:25:59 -07001639 }
1640
chaviw06178942017-07-27 10:25:59 -07001641 return true;
1642}
1643
Robert Carr9524cb32017-02-13 11:32:32 -08001644bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001645 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001646 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001647 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001648 if (client != nullptr && parentClient != client) {
Robert Carr7f619b22017-11-06 12:56:35 -08001649 client->detachLayer(child.get());
1650 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001651 }
Robert Carr7f619b22017-11-06 12:56:35 -08001652 }
Robert Carr9524cb32017-02-13 11:32:32 -08001653
1654 return true;
1655}
1656
Chia-I Wu11481472018-05-04 10:43:19 -07001657bool Layer::isLegacyDataSpace() const {
1658 // return true when no higher bits are set
Chia-I Wu1b6bafc2018-05-22 12:03:00 -07001659 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001660 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001661}
1662
Robert Carr1f0a16a2016-10-24 16:27:39 -07001663void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001664 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001665}
1666
1667void Layer::clearSyncPoints() {
1668 for (const auto& child : mCurrentChildren) {
1669 child->clearSyncPoints();
1670 }
1671
1672 Mutex::Autolock lock(mLocalSyncPointMutex);
1673 for (auto& point : mLocalSyncPoints) {
1674 point->setFrameAvailable();
1675 }
1676 mLocalSyncPoints.clear();
1677}
1678
1679int32_t Layer::getZ() const {
1680 return mDrawingState.z;
1681}
1682
Robert Carr29abff82017-12-04 13:51:20 -08001683bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1684 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1685 const State& state = useDrawing ? mDrawingState : mCurrentState;
1686 return state.zOrderRelativeOf != nullptr;
1687}
1688
David Sodman41fdfc92017-11-06 16:09:56 -08001689__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001690 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001691 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1692 "makeTraversalList received invalid stateSet");
1693 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1694 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1695 const State& state = useDrawing ? mDrawingState : mCurrentState;
1696
Robert Carr29abff82017-12-04 13:51:20 -08001697 if (state.zOrderRelatives.size() == 0) {
1698 *outSkipRelativeZUsers = true;
1699 return children;
1700 }
1701
chaviw738df022018-05-31 16:11:27 -07001702 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001703 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001704 sp<Layer> strongRelative = weakRelative.promote();
1705 if (strongRelative != nullptr) {
1706 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001707 }
1708 }
1709
Dan Stoza412903f2017-04-27 13:42:17 -07001710 for (const sp<Layer>& child : children) {
Robert Carr503c7042017-09-27 15:06:08 -07001711 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1712 if (childState.zOrderRelativeOf != nullptr) {
1713 continue;
1714 }
Robert Carrdb66e622017-04-10 16:55:57 -07001715 traverse.add(child);
1716 }
1717
1718 return traverse;
1719}
1720
Robert Carr1f0a16a2016-10-24 16:27:39 -07001721/**
Robert Carrdb66e622017-04-10 16:55:57 -07001722 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001723 */
Dan Stoza412903f2017-04-27 13:42:17 -07001724void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001725 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1726 // produce a new list for traversing, including our relatives, and not including our children
1727 // who are relatives of another surface. In the case that there are no relative Z,
1728 // makeTraversalList returns our children directly to avoid significant overhead.
1729 // However in this case we need to take the responsibility for filtering children which
1730 // are relatives of another surface here.
1731 bool skipRelativeZUsers = false;
1732 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001733
Robert Carr1f0a16a2016-10-24 16:27:39 -07001734 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001735 for (; i < list.size(); i++) {
1736 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001737 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1738 continue;
1739 }
1740
Robert Carrdb66e622017-04-10 16:55:57 -07001741 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001742 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001743 }
Dan Stoza412903f2017-04-27 13:42:17 -07001744 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001745 }
Robert Carr29abff82017-12-04 13:51:20 -08001746
Dan Stoza412903f2017-04-27 13:42:17 -07001747 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001748 for (; i < list.size(); i++) {
1749 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001750
1751 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1752 continue;
1753 }
Dan Stoza412903f2017-04-27 13:42:17 -07001754 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001755 }
1756}
1757
1758/**
Robert Carrdb66e622017-04-10 16:55:57 -07001759 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001760 */
Dan Stoza412903f2017-04-27 13:42:17 -07001761void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1762 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001763 // See traverseInZOrder for documentation.
1764 bool skipRelativeZUsers = false;
1765 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001766
Robert Carr1f0a16a2016-10-24 16:27:39 -07001767 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001768 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001769 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001770
1771 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1772 continue;
1773 }
1774
Robert Carrdb66e622017-04-10 16:55:57 -07001775 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001776 break;
1777 }
Dan Stoza412903f2017-04-27 13:42:17 -07001778 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001779 }
Dan Stoza412903f2017-04-27 13:42:17 -07001780 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001781 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001782 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001783
1784 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1785 continue;
1786 }
1787
Dan Stoza412903f2017-04-27 13:42:17 -07001788 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001789 }
1790}
1791
chaviw4b129c22018-04-09 16:19:43 -07001792LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1793 const std::vector<Layer*>& layersInTree) {
1794 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1795 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001796 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1797 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
chaviw4b129c22018-04-09 16:19:43 -07001798 const State& state = useDrawing ? mDrawingState : mCurrentState;
1799
chaviw738df022018-05-31 16:11:27 -07001800 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001801 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1802 sp<Layer> strongRelative = weakRelative.promote();
1803 // Only add relative layers that are also descendents of the top most parent of the tree.
1804 // If a relative layer is not a descendent, then it should be ignored.
1805 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1806 traverse.add(strongRelative);
1807 }
1808 }
1809
1810 for (const sp<Layer>& child : children) {
1811 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1812 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1813 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1814 // the child here since it won't be added later as a relative.
1815 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1816 childState.zOrderRelativeOf.promote().get())) {
1817 continue;
1818 }
1819 traverse.add(child);
1820 }
1821
1822 return traverse;
1823}
1824
1825void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1826 LayerVector::StateSet stateSet,
1827 const LayerVector::Visitor& visitor) {
1828 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001829
1830 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001831 for (; i < list.size(); i++) {
1832 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001833 if (relative->getZ() >= 0) {
1834 break;
1835 }
chaviw4b129c22018-04-09 16:19:43 -07001836 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001837 }
chaviw4b129c22018-04-09 16:19:43 -07001838
chaviwa76b2712017-09-20 12:02:26 -07001839 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001840 for (; i < list.size(); i++) {
1841 const auto& relative = list[i];
1842 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001843 }
1844}
1845
chaviw4b129c22018-04-09 16:19:43 -07001846std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1847 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1848 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1849
1850 std::vector<Layer*> layersInTree = {this};
1851 for (size_t i = 0; i < children.size(); i++) {
1852 const auto& child = children[i];
1853 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1854 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1855 }
1856
1857 return layersInTree;
1858}
1859
1860void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1861 const LayerVector::Visitor& visitor) {
1862 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1863 std::sort(layersInTree.begin(), layersInTree.end());
1864 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1865}
1866
Robert Carr1f0a16a2016-10-24 16:27:39 -07001867Transform Layer::getTransform() const {
1868 Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001869 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001870 if (p != nullptr) {
1871 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07001872
1873 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
1874 // it isFixedSize) then there may be additional scaling not accounted
1875 // for in the transform. We need to mirror this scaling in child surfaces
1876 // or we will break the contract where WM can treat child surfaces as
1877 // pixels in the parent surface.
David Sodman0cc69182017-11-17 12:12:07 -08001878 if (p->isFixedSize() && p->getBE().compositionInfo.mBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07001879 int bufferWidth;
1880 int bufferHeight;
1881 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
David Sodman0cc69182017-11-17 12:12:07 -08001882 bufferWidth = p->getBE().compositionInfo.mBuffer->getWidth();
1883 bufferHeight = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001884 } else {
David Sodman0cc69182017-11-17 12:12:07 -08001885 bufferHeight = p->getBE().compositionInfo.mBuffer->getWidth();
1886 bufferWidth = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001887 }
David Sodman41fdfc92017-11-06 16:09:56 -08001888 float sx = p->getDrawingState().active.w / static_cast<float>(bufferWidth);
1889 float sy = p->getDrawingState().active.h / static_cast<float>(bufferHeight);
Robert Carr9b429f42017-04-17 14:56:57 -07001890 Transform extraParentScaling;
1891 extraParentScaling.set(sx, 0, 0, sy);
1892 t = t * extraParentScaling;
1893 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001894 }
1895 return t * getDrawingState().active.transform;
1896}
1897
chaviw13fdc492017-06-27 12:40:18 -07001898half Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001899 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07001900
chaviw13fdc492017-06-27 12:40:18 -07001901 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1902 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001903}
Robert Carr6452f122017-03-21 10:41:29 -07001904
chaviw13fdc492017-06-27 12:40:18 -07001905half4 Layer::getColor() const {
1906 const half4 color(getDrawingState().color);
1907 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001908}
Robert Carr6452f122017-03-21 10:41:29 -07001909
Robert Carr1f0a16a2016-10-24 16:27:39 -07001910void Layer::commitChildList() {
1911 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1912 const auto& child = mCurrentChildren[i];
1913 child->commitChildList();
1914 }
1915 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001916 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001917}
1918
chaviw1d044282017-09-27 12:19:28 -07001919void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
1920 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1921 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1922 const State& state = useDrawing ? mDrawingState : mCurrentState;
1923
1924 Transform requestedTransform = state.active.transform;
1925 Transform transform = getTransform();
1926
1927 layerInfo->set_id(sequence);
1928 layerInfo->set_name(getName().c_str());
1929 layerInfo->set_type(String8(getTypeId()));
1930
1931 for (const auto& child : children) {
1932 layerInfo->add_children(child->sequence);
1933 }
1934
1935 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1936 sp<Layer> strongRelative = weakRelative.promote();
1937 if (strongRelative != nullptr) {
1938 layerInfo->add_relatives(strongRelative->sequence);
1939 }
1940 }
1941
1942 LayerProtoHelper::writeToProto(state.activeTransparentRegion,
1943 layerInfo->mutable_transparent_region());
1944 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
1945 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
1946
1947 layerInfo->set_layer_stack(getLayerStack());
1948 layerInfo->set_z(state.z);
1949
1950 PositionProto* position = layerInfo->mutable_position();
1951 position->set_x(transform.tx());
1952 position->set_y(transform.ty());
1953
1954 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
1955 requestedPosition->set_x(requestedTransform.tx());
1956 requestedPosition->set_y(requestedTransform.ty());
1957
1958 SizeProto* size = layerInfo->mutable_size();
1959 size->set_w(state.active.w);
1960 size->set_h(state.active.h);
1961
1962 LayerProtoHelper::writeToProto(state.crop, layerInfo->mutable_crop());
1963 LayerProtoHelper::writeToProto(state.finalCrop, layerInfo->mutable_final_crop());
1964
1965 layerInfo->set_is_opaque(isOpaque(state));
1966 layerInfo->set_invalidate(contentDirty);
Chia-I Wu1b6bafc2018-05-22 12:03:00 -07001967
1968 // XXX (b/79210409) mCurrentDataSpace is not protected
1969 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
1970
chaviw1d044282017-09-27 12:19:28 -07001971 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1972 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
1973 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
1974 layerInfo->set_flags(state.flags);
1975
1976 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1977 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
1978
Jorim Jaggi8e0af362017-11-14 16:28:28 +01001979 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07001980 if (parent != nullptr) {
1981 layerInfo->set_parent(parent->sequence);
1982 }
1983
1984 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1985 if (zOrderRelativeOf != nullptr) {
1986 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1987 }
1988
Chia-I Wu1b6bafc2018-05-22 12:03:00 -07001989 // XXX getBE().compositionInfo.mBuffer is not protected
David Sodman0cc69182017-11-17 12:12:07 -08001990 auto buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001991 if (buffer != nullptr) {
1992 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
chaviw1d044282017-09-27 12:19:28 -07001993 }
1994
1995 layerInfo->set_queued_frames(getQueuedFrameCount());
1996 layerInfo->set_refresh_pending(isBufferLatched());
rongliuccd34842018-03-14 12:26:23 -07001997 layerInfo->set_window_type(state.type);
1998 layerInfo->set_app_id(state.appId);
chaviw9885bd42018-07-10 16:57:27 -07001999 layerInfo->set_curr_frame(mCurrentFrameNumber);
2000
2001 for (const auto& pendingState : mPendingStates) {
2002 auto barrierLayer = pendingState.barrierLayer.promote();
2003 if (barrierLayer != nullptr) {
2004 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
2005 barrierLayerProto->set_id(barrierLayer->sequence);
2006 barrierLayerProto->set_frame_number(pendingState.frameNumber);
2007 }
2008 }
chaviw1d044282017-09-27 12:19:28 -07002009}
2010
Yiwei Zhang068e31b2018-02-21 13:02:45 -08002011void Layer::writeToProto(LayerProto* layerInfo, int32_t hwcId) {
2012 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
2013
2014 const auto& hwcInfo = getBE().mHwcLayers.at(hwcId);
2015
2016 const Rect& frame = hwcInfo.displayFrame;
2017 LayerProtoHelper::writeToProto(frame, layerInfo->mutable_hwc_frame());
2018
2019 const FloatRect& crop = hwcInfo.sourceCrop;
2020 LayerProtoHelper::writeToProto(crop, layerInfo->mutable_hwc_crop());
2021
2022 const int32_t transform = static_cast<int32_t>(hwcInfo.transform);
2023 layerInfo->set_hwc_transform(transform);
Yiwei Zhang7c64f172018-03-07 14:52:28 -08002024
2025 const int32_t compositionType = static_cast<int32_t>(hwcInfo.compositionType);
2026 layerInfo->set_hwc_composition_type(compositionType);
2027
2028 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
2029 static_cast<BufferLayer*>(this)->isProtected()) {
2030 layerInfo->set_is_protected(true);
2031 } else {
2032 layerInfo->set_is_protected(false);
2033 }
Yiwei Zhang068e31b2018-02-21 13:02:45 -08002034}
2035
Mathias Agopian13127d82013-03-05 17:47:11 -08002036// ---------------------------------------------------------------------------
2037
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002038}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002039
2040#if defined(__gl_h_)
2041#error "don't include gl/gl.h in this file"
2042#endif
2043
2044#if defined(__gl2_h_)
2045#error "don't include gl2/gl2.h in this file"
2046#endif