blob: 0caac9b16dfbf37815a38402f52153b7a6dd6551 [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);
Jorim Jaggiedc1f1b2018-08-10 14:37:31 +0200136 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Dan Stoza9fdb7e02018-06-21 12:10:12 -0700137}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700138
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700139Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800140 sp<Client> c(mClientRef.promote());
141 if (c != 0) {
142 c->detachLayer(this);
143 }
144
145 for (auto& point : mRemoteSyncPoints) {
146 point->setTransactionApplied();
147 }
148 for (auto& point : mLocalSyncPoints) {
149 point->setFrameAvailable();
150 }
Jamie Gennis6547ff42013-07-16 20:12:42 -0700151 mFrameTracker.logAndResetStats(mName);
Mathias Agopian96f08192010-06-02 23:28:45 -0700152}
153
Mathias Agopian13127d82013-03-05 17:47:11 -0800154// ---------------------------------------------------------------------------
155// callbacks
156// ---------------------------------------------------------------------------
157
David Sodmaneb085e02017-10-05 18:49:04 -0700158/*
159 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
160 * Layer. So, the implementation is done in BufferLayer. When called on a
161 * ColorLayer object, it's essentially a NOP.
162 */
David Sodmaneb085e02017-10-05 18:49:04 -0700163void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800164
Chia-I Wuc6657022017-08-15 11:18:17 -0700165void Layer::onRemovedFromCurrentState() {
166 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
167
chaviw8b3871a2017-11-01 17:41:01 -0700168 mPendingRemoval = true;
169
Robert Carr5edb1ad2017-04-25 10:54:24 -0700170 if (mCurrentState.zOrderRelativeOf != nullptr) {
171 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
172 if (strongRelative != nullptr) {
173 strongRelative->removeZOrderRelative(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700174 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700175 }
176 mCurrentState.zOrderRelativeOf = nullptr;
177 }
178
Chia-I Wuc6657022017-08-15 11:18:17 -0700179 for (const auto& child : mCurrentChildren) {
180 child->onRemovedFromCurrentState();
181 }
182}
Chia-I Wu38512252017-05-17 14:36:16 -0700183
Chia-I Wuc6657022017-08-15 11:18:17 -0700184void Layer::onRemoved() {
185 // the layer is removed from SF mLayersPendingRemoval
David Sodmaneb085e02017-10-05 18:49:04 -0700186 abandon();
Chia-I Wuc6657022017-08-15 11:18:17 -0700187
Steven Thomasb02664d2017-07-26 18:48:28 -0700188 destroyAllHwcLayers();
Chia-I Wu38512252017-05-17 14:36:16 -0700189
Robert Carr1f0a16a2016-10-24 16:27:39 -0700190 for (const auto& child : mCurrentChildren) {
191 child->onRemoved();
192 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700193}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700194
Mathias Agopian13127d82013-03-05 17:47:11 -0800195// ---------------------------------------------------------------------------
196// set-up
197// ---------------------------------------------------------------------------
198
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700199const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800200 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800201}
202
chaviw13fdc492017-06-27 12:40:18 -0700203bool Layer::getPremultipledAlpha() const {
204 return mPremultipliedAlpha;
205}
206
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700207sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800208 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700209 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800210}
211
212// ---------------------------------------------------------------------------
213// h/w composer set-up
214// ---------------------------------------------------------------------------
215
Steven Thomasb02664d2017-07-26 18:48:28 -0700216bool Layer::createHwcLayer(HWComposer* hwc, int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700217 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.count(hwcId) != 0,
David Sodman9eeae692017-11-02 10:53:32 -0700218 "Already have a layer for hwcId %d", hwcId);
Steven Thomasb02664d2017-07-26 18:48:28 -0700219 HWC2::Layer* layer = hwc->createLayer(hwcId);
220 if (!layer) {
221 return false;
222 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700223 LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers[hwcId];
Steven Thomasb02664d2017-07-26 18:48:28 -0700224 hwcInfo.hwc = hwc;
225 hwcInfo.layer = layer;
226 layer->setLayerDestroyedListener(
David Sodman6f65f3e2017-11-03 14:28:09 -0700227 [this, hwcId](HWC2::Layer* /*layer*/) { getBE().mHwcLayers.erase(hwcId); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700228 return true;
229}
230
Chia-I Wu83806892017-11-16 10:50:20 -0800231bool Layer::destroyHwcLayer(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700232 if (getBE().mHwcLayers.count(hwcId) == 0) {
Chia-I Wu83806892017-11-16 10:50:20 -0800233 return false;
Steven Thomasb02664d2017-07-26 18:48:28 -0700234 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700235 auto& hwcInfo = getBE().mHwcLayers[hwcId];
David Sodman41fdfc92017-11-06 16:09:56 -0800236 LOG_ALWAYS_FATAL_IF(hwcInfo.layer == nullptr, "Attempt to destroy null layer");
Steven Thomasb02664d2017-07-26 18:48:28 -0700237 LOG_ALWAYS_FATAL_IF(hwcInfo.hwc == nullptr, "Missing HWComposer");
238 hwcInfo.hwc->destroyLayer(hwcId, hwcInfo.layer);
239 // The layer destroyed listener should have cleared the entry from
240 // mHwcLayers. Verify that.
David Sodman6f65f3e2017-11-03 14:28:09 -0700241 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.count(hwcId) != 0,
242 "Stale layer entry in getBE().mHwcLayers");
Chia-I Wu83806892017-11-16 10:50:20 -0800243 return true;
Steven Thomasb02664d2017-07-26 18:48:28 -0700244}
245
246void Layer::destroyAllHwcLayers() {
David Sodman6f65f3e2017-11-03 14:28:09 -0700247 size_t numLayers = getBE().mHwcLayers.size();
Steven Thomasb02664d2017-07-26 18:48:28 -0700248 for (size_t i = 0; i < numLayers; ++i) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700249 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.empty(), "destroyAllHwcLayers failed");
250 destroyHwcLayer(getBE().mHwcLayers.begin()->first);
Steven Thomasb02664d2017-07-26 18:48:28 -0700251 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700252 LOG_ALWAYS_FATAL_IF(!getBE().mHwcLayers.empty(),
David Sodman41fdfc92017-11-06 16:09:56 -0800253 "All hardware composer layers should have been destroyed");
Steven Thomasb02664d2017-07-26 18:48:28 -0700254}
Steven Thomasb02664d2017-07-26 18:48:28 -0700255
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800256Rect Layer::getContentCrop() const {
257 // this is the crop rectangle that applies to the buffer
258 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700259 Rect crop;
260 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800261 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700262 crop = mCurrentCrop;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800263 } else if (getBE().compositionInfo.mBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800264 // otherwise we use the whole buffer
David Sodman0cc69182017-11-17 12:12:07 -0800265 crop = getBE().compositionInfo.mBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700266 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800267 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700268 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700269 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700270 return crop;
271}
272
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700273static Rect reduce(const Rect& win, const Region& exclude) {
274 if (CC_LIKELY(exclude.isEmpty())) {
275 return win;
276 }
277 if (exclude.isRect()) {
278 return win.reduce(exclude.getBounds());
279 }
280 return Region(win).subtract(exclude).getBounds();
281}
282
Dan Stoza80d61162017-12-20 15:57:52 -0800283static FloatRect reduce(const FloatRect& win, const Region& exclude) {
284 if (CC_LIKELY(exclude.isEmpty())) {
285 return win;
286 }
287 // Convert through Rect (by rounding) for lack of FloatRegion
288 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
289}
290
Robert Carr1f0a16a2016-10-24 16:27:39 -0700291Rect Layer::computeScreenBounds(bool reduceTransparentRegion) const {
292 const Layer::State& s(getDrawingState());
293 Rect win(s.active.w, s.active.h);
294
295 if (!s.crop.isEmpty()) {
296 win.intersect(s.crop, &win);
297 }
298
299 Transform t = getTransform();
300 win = t.transform(win);
301
Robert Carr41b08b52017-06-01 16:11:34 -0700302 if (!s.finalCrop.isEmpty()) {
303 win.intersect(s.finalCrop, &win);
304 }
305
Chia-I Wue41dbe62017-06-13 14:10:56 -0700306 const sp<Layer>& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700307 // Now we need to calculate the parent bounds, so we can clip ourselves to those.
308 // When calculating the parent bounds for purposes of clipping,
309 // we don't need to constrain the parent to its transparent region.
310 // The transparent region is an optimization based on the
311 // buffer contents of the layer, but does not affect the space allocated to
312 // it by policy, and thus children should be allowed to extend into the
313 // parent's transparent region. In fact one of the main uses, is to reduce
314 // buffer allocation size in cases where a child window sits behind a main window
315 // (by marking the hole in the parent window as a transparent region)
316 if (p != nullptr) {
317 Rect bounds = p->computeScreenBounds(false);
318 bounds.intersect(win, &win);
319 }
320
321 if (reduceTransparentRegion) {
322 auto const screenTransparentRegion = t.transform(s.activeTransparentRegion);
323 win = reduce(win, screenTransparentRegion);
324 }
325
326 return win;
327}
328
Dan Stoza80d61162017-12-20 15:57:52 -0800329FloatRect Layer::computeBounds() const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700330 const Layer::State& s(getDrawingState());
Michael Lentine6c925ed2014-09-26 17:55:01 -0700331 return computeBounds(s.activeTransparentRegion);
332}
333
Dan Stoza80d61162017-12-20 15:57:52 -0800334FloatRect Layer::computeBounds(const Region& activeTransparentRegion) const {
Michael Lentine6c925ed2014-09-26 17:55:01 -0700335 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800336 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700337
338 if (!s.crop.isEmpty()) {
339 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800340 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700341
Chia-I Wue41dbe62017-06-13 14:10:56 -0700342 const auto& p = mDrawingParent.promote();
Robert Carrd4ae7f32018-06-07 16:10:57 -0700343 FloatRect floatWin = win.toFloatRect();
344 FloatRect parentBounds = floatWin;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700345 if (p != nullptr) {
Robert Carrd4ae7f32018-06-07 16:10:57 -0700346 // We pass an empty Region here for reasons mirroring that of the case described in
347 // the computeScreenBounds reduceTransparentRegion=false case.
348 parentBounds = p->computeBounds(Region());
Robert Carr1f0a16a2016-10-24 16:27:39 -0700349 }
350
Robert Carrd4ae7f32018-06-07 16:10:57 -0700351 Transform t = s.active.transform;
Dan Stoza80d61162017-12-20 15:57:52 -0800352
Robert Carrd4ae7f32018-06-07 16:10:57 -0700353
354 if (p != nullptr || !s.finalCrop.isEmpty()) {
Dan Stoza80d61162017-12-20 15:57:52 -0800355 floatWin = t.transform(floatWin);
Robert Carrd4ae7f32018-06-07 16:10:57 -0700356 floatWin = floatWin.intersect(parentBounds);
357
358 if (!s.finalCrop.isEmpty()) {
359 floatWin = floatWin.intersect(s.finalCrop.toFloatRect());
360 }
Dan Stoza80d61162017-12-20 15:57:52 -0800361 floatWin = t.inverse().transform(floatWin);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700362 }
363
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700364 // subtract the transparent region and snap to the bounds
Dan Stoza80d61162017-12-20 15:57:52 -0800365 return reduce(floatWin, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800366}
367
Robert Carr1f0a16a2016-10-24 16:27:39 -0700368Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& hw) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700369 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800370 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700371 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800372
373 // apply the projection's clipping to the window crop in
374 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700375 // if there are no window scaling involved, this operation will map to full
376 // pixels in the buffer.
377 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
378 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700379
380 Rect activeCrop(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700381 if (!s.crop.isEmpty()) {
Chia-I Wudf7867f2017-07-20 14:24:37 -0700382 activeCrop.intersect(s.crop, &activeCrop);
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700383 }
384
Robert Carr1f0a16a2016-10-24 16:27:39 -0700385 Transform t = getTransform();
386 activeCrop = t.transform(activeCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000387 if (!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
388 activeCrop.clear();
389 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700390 if (!s.finalCrop.isEmpty()) {
David Sodman41fdfc92017-11-06 16:09:56 -0800391 if (!activeCrop.intersect(s.finalCrop, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000392 activeCrop.clear();
393 }
394 }
chaviwb1154d12017-10-31 14:15:36 -0700395
396 const auto& p = mDrawingParent.promote();
397 if (p != nullptr) {
398 auto parentCrop = p->computeInitialCrop(hw);
399 activeCrop.intersect(parentCrop, &activeCrop);
400 }
401
Robert Carr1f0a16a2016-10-24 16:27:39 -0700402 return activeCrop;
403}
404
Dan Stoza5a423ea2017-02-16 14:10:39 -0800405FloatRect Layer::computeCrop(const sp<const DisplayDevice>& hw) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700406 // the content crop is the area of the content that gets scaled to the
407 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800408 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700409
410 // In addition there is a WM-specified crop we pull from our drawing state.
411 const State& s(getDrawingState());
412
413 // Screen space to make reduction to parent crop clearer.
414 Rect activeCrop = computeInitialCrop(hw);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700415 Transform t = getTransform();
416 // Back to layer space to work with the content crop.
417 activeCrop = t.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800418
Michael Lentine28ea2172014-11-19 18:32:37 -0800419 // This needs to be here as transform.transform(Rect) computes the
420 // transformed rect and then takes the bounding box of the result before
421 // returning. This means
422 // transform.inverse().transform(transform.transform(Rect)) != Rect
423 // in which case we need to make sure the final rect is clipped to the
424 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000425 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
426 activeCrop.clear();
427 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800428
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700429 // subtract the transparent region and snap to the bounds
430 activeCrop = reduce(activeCrop, s.activeTransparentRegion);
431
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000432 // Transform the window crop to match the buffer coordinate system,
433 // which means using the inverse of the current transform set on the
434 // SurfaceFlingerConsumer.
435 uint32_t invTransform = mCurrentTransform;
Robert Carrcae605c2017-03-29 12:10:31 -0700436 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000437 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700438 * the code below applies the primary display's inverse transform to the
439 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000440 */
David Sodman41fdfc92017-11-06 16:09:56 -0800441 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000442 // calculate the inverse transform
443 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800444 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800445 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000446 // and apply to the current transform
David Sodman41fdfc92017-11-06 16:09:56 -0800447 invTransform = (Transform(invTransformOrient) * Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800448 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000449
450 int winWidth = s.active.w;
451 int winHeight = s.active.h;
452 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
453 // If the activeCrop has been rotate the ends are rotated but not
454 // the space itself so when transforming ends back we can't rely on
455 // a modification of the axes of rotation. To account for this we
456 // need to reorient the inverse rotation in terms of the current
457 // axes of rotation.
458 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
459 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
460 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800461 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000462 }
463 winWidth = s.active.h;
464 winHeight = s.active.w;
465 }
David Sodman41fdfc92017-11-06 16:09:56 -0800466 const Rect winCrop = activeCrop.transform(invTransform, s.active.w, s.active.h);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000467
468 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800469 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000470 float yScale = crop.getHeight() / float(winHeight);
471
David Sodman41fdfc92017-11-06 16:09:56 -0800472 float insetL = winCrop.left * xScale;
473 float insetT = winCrop.top * yScale;
474 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000475 float insetB = (winHeight - winCrop.bottom) * yScale;
476
David Sodman41fdfc92017-11-06 16:09:56 -0800477 crop.left += insetL;
478 crop.top += insetT;
479 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000480 crop.bottom -= insetB;
481
Mathias Agopian13127d82013-03-05 17:47:11 -0800482 return crop;
483}
484
Robert Carrae060832016-11-28 10:51:00 -0800485void Layer::setGeometry(const sp<const DisplayDevice>& displayDevice, uint32_t z)
Mathias Agopiana350ff92010-08-10 17:14:02 -0700486{
Dan Stoza9e56aa02015-11-02 13:00:03 -0800487 const auto hwcId = displayDevice->getHwcDisplayId();
Peiyong Lin0874d2b2018-09-04 09:39:34 -0700488 if (!hasHwcLayer(hwcId)) {
489 return;
490 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700491 auto& hwcInfo = getBE().mHwcLayers[hwcId];
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700492
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700493 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800494 hwcInfo.forceClientComposition = false;
495
496 if (isSecure() && !displayDevice->isSecure()) {
497 hwcInfo.forceClientComposition = true;
498 }
499
500 auto& hwcLayer = hwcInfo.layer;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700501
Mathias Agopian13127d82013-03-05 17:47:11 -0800502 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700503 const State& s(getDrawingState());
David Revemanecf0fa52017-03-03 11:32:44 -0500504 auto blendMode = HWC2::BlendMode::None;
Robert Carr6452f122017-03-21 10:41:29 -0700505 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800506 blendMode =
507 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800508 }
David Revemanecf0fa52017-03-03 11:32:44 -0500509 auto error = hwcLayer->setBlendMode(blendMode);
David Sodman41fdfc92017-11-06 16:09:56 -0800510 ALOGE_IF(error != HWC2::Error::None,
511 "[%s] Failed to set blend mode %s:"
512 " %s (%d)",
513 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
514 static_cast<int32_t>(error));
Mathias Agopian13127d82013-03-05 17:47:11 -0800515
516 // apply the layer's transform, followed by the display's global transform
517 // here we're guaranteed that the layer's transform preserves rects
Michael Lentine6c925ed2014-09-26 17:55:01 -0700518 Region activeTransparentRegion(s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700519 Transform t = getTransform();
Robert Carrb5d3d262016-03-25 15:08:13 -0700520 if (!s.crop.isEmpty()) {
521 Rect activeCrop(s.crop);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700522 activeCrop = t.transform(activeCrop);
David Sodman41fdfc92017-11-06 16:09:56 -0800523 if (!activeCrop.intersect(displayDevice->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000524 activeCrop.clear();
525 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700526 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800527 // This needs to be here as transform.transform(Rect) computes the
528 // transformed rect and then takes the bounding box of the result before
529 // returning. This means
530 // transform.inverse().transform(transform.transform(Rect)) != Rect
531 // in which case we need to make sure the final rect is clipped to the
532 // display bounds.
David Sodman41fdfc92017-11-06 16:09:56 -0800533 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000534 activeCrop.clear();
535 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700536 // mark regions outside the crop as transparent
537 activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top));
David Sodman41fdfc92017-11-06 16:09:56 -0800538 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom, s.active.w, s.active.h));
539 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
540 activeTransparentRegion.orSelf(
541 Rect(activeCrop.right, activeCrop.top, s.active.w, activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700542 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700543
Dan Stoza80d61162017-12-20 15:57:52 -0800544 // computeBounds returns a FloatRect to provide more accuracy during the
545 // transformation. We then round upon constructing 'frame'.
546 Rect frame{t.transform(computeBounds(activeTransparentRegion))};
Robert Carrb5d3d262016-03-25 15:08:13 -0700547 if (!s.finalCrop.isEmpty()) {
David Sodman41fdfc92017-11-06 16:09:56 -0800548 if (!frame.intersect(s.finalCrop, &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000549 frame.clear();
550 }
551 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000552 if (!frame.intersect(displayDevice->getViewport(), &frame)) {
553 frame.clear();
554 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800555 const Transform& tr(displayDevice->getTransform());
556 Rect transformedFrame = tr.transform(frame);
David Revemanecf0fa52017-03-03 11:32:44 -0500557 error = hwcLayer->setDisplayFrame(transformedFrame);
Dan Stozae22aec72016-08-01 13:20:59 -0700558 if (error != HWC2::Error::None) {
David Sodman41fdfc92017-11-06 16:09:56 -0800559 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
560 transformedFrame.left, transformedFrame.top, transformedFrame.right,
561 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stozae22aec72016-08-01 13:20:59 -0700562 } else {
563 hwcInfo.displayFrame = transformedFrame;
564 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800565
Dan Stoza5a423ea2017-02-16 14:10:39 -0800566 FloatRect sourceCrop = computeCrop(displayDevice);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800567 error = hwcLayer->setSourceCrop(sourceCrop);
Dan Stozae22aec72016-08-01 13:20:59 -0700568 if (error != HWC2::Error::None) {
569 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
David Sodman41fdfc92017-11-06 16:09:56 -0800570 "%s (%d)",
571 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
572 to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stozae22aec72016-08-01 13:20:59 -0700573 } else {
574 hwcInfo.sourceCrop = sourceCrop;
575 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800576
chaviw13fdc492017-06-27 12:40:18 -0700577 float alpha = static_cast<float>(getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -0700578 error = hwcLayer->setPlaneAlpha(alpha);
David Sodman41fdfc92017-11-06 16:09:56 -0800579 ALOGE_IF(error != HWC2::Error::None,
580 "[%s] Failed to set plane alpha %.3f: "
581 "%s (%d)",
582 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800583
Robert Carrae060832016-11-28 10:51:00 -0800584 error = hwcLayer->setZOrder(z);
David Sodman41fdfc92017-11-06 16:09:56 -0800585 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
586 to_string(error).c_str(), static_cast<int32_t>(error));
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500587
Albert Chaulk2a589632017-05-04 16:59:44 -0400588 int type = s.type;
589 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700590 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400591 if (parent.get()) {
592 auto& parentState = parent->getDrawingState();
rongliuccd34842018-03-14 12:26:23 -0700593 if (parentState.type >= 0 || parentState.appId >= 0) {
594 type = parentState.type;
595 appId = parentState.appId;
596 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400597 }
598
599 error = hwcLayer->setInfo(type, appId);
David Sodman41fdfc92017-11-06 16:09:56 -0800600 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
601 static_cast<int32_t>(error));
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800602
Mathias Agopian29a367b2011-07-12 14:51:45 -0700603 /*
604 * Transformations are applied in this order:
605 * 1) buffer orientation/flip/mirror
606 * 2) state transformation (window manager)
607 * 3) layer orientation (screen orientation)
608 * (NOTE: the matrices are multiplied in reverse order)
609 */
610
611 const Transform bufferOrientation(mCurrentTransform);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700612 Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700613
Robert Carrcae605c2017-03-29 12:10:31 -0700614 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700615 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700616 * the code below applies the primary display's inverse transform to the
617 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700618 */
David Sodman41fdfc92017-11-06 16:09:56 -0800619 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700620 // calculate the inverse transform
621 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800622 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700623 }
Robert Carrcae605c2017-03-29 12:10:31 -0700624
625 /*
626 * Here we cancel out the orientation component of the WM transform.
627 * The scaling and translate components are already included in our bounds
628 * computation so it's enough to just omit it in the composition.
629 * See comment in onDraw with ref to b/36727915 for why.
630 */
631 transform = Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700632 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700633
634 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800635 const uint32_t orientation = transform.getOrientation();
Jorim Jaggif3bd94a2018-03-27 15:38:03 +0200636 if (orientation & Transform::ROT_INVALID) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800637 // we can only handle simple transformation
638 hwcInfo.forceClientComposition = true;
639 } else {
640 auto transform = static_cast<HWC2::Transform>(orientation);
Yiwei Zhang068e31b2018-02-21 13:02:45 -0800641 hwcInfo.transform = transform;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800642 auto error = hwcLayer->setTransform(transform);
David Sodman41fdfc92017-11-06 16:09:56 -0800643 ALOGE_IF(error != HWC2::Error::None,
644 "[%s] Failed to set transform %s: "
645 "%s (%d)",
646 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
647 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800648 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700649}
650
Dan Stoza9e56aa02015-11-02 13:00:03 -0800651void Layer::forceClientComposition(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700652 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800653 ALOGE("forceClientComposition: no HWC layer found (%d)", hwcId);
654 return;
655 }
656
David Sodman6f65f3e2017-11-03 14:28:09 -0700657 getBE().mHwcLayers[hwcId].forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800658}
Dan Stozaee44edd2015-03-23 15:50:23 -0700659
chaviwc9232ed2017-11-14 15:31:15 -0800660bool Layer::getForceClientComposition(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700661 if (getBE().mHwcLayers.count(hwcId) == 0) {
chaviwc9232ed2017-11-14 15:31:15 -0800662 ALOGE("getForceClientComposition: no HWC layer found (%d)", hwcId);
663 return false;
664 }
665
David Sodman6f65f3e2017-11-03 14:28:09 -0700666 return getBE().mHwcLayers[hwcId].forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800667}
668
Dan Stoza9e56aa02015-11-02 13:00:03 -0800669void Layer::updateCursorPosition(const sp<const DisplayDevice>& displayDevice) {
670 auto hwcId = displayDevice->getHwcDisplayId();
David Sodman6f65f3e2017-11-03 14:28:09 -0700671 if (getBE().mHwcLayers.count(hwcId) == 0 ||
David Sodman9eeae692017-11-02 10:53:32 -0700672 getCompositionType(hwcId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800673 return;
674 }
675
676 // This gives us only the "orientation" component of the transform
677 const State& s(getCurrentState());
678
679 // Apply the layer's transform, followed by the display's global transform
680 // Here we're guaranteed that the layer's transform preserves rects
681 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700682 if (!s.crop.isEmpty()) {
683 win.intersect(s.crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800684 }
685 // Subtract the transparent region and snap to the bounds
686 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700687 Rect frame(getTransform().transform(bounds));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800688 frame.intersect(displayDevice->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700689 if (!s.finalCrop.isEmpty()) {
690 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000691 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800692 auto& displayTransform(displayDevice->getTransform());
693 auto position = displayTransform.transform(frame);
694
David Sodman6f65f3e2017-11-03 14:28:09 -0700695 auto error = getBE().mHwcLayers[hwcId].layer->setCursorPosition(position.left,
David Sodman9eeae692017-11-02 10:53:32 -0700696 position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800697 ALOGE_IF(error != HWC2::Error::None,
698 "[%s] Failed to set cursor position "
699 "to (%d, %d): %s (%d)",
700 mName.string(), position.left, position.top, to_string(error).c_str(),
701 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800702}
Riley Andrews03414a12014-07-01 14:22:59 -0700703
Mathias Agopian13127d82013-03-05 17:47:11 -0800704// ---------------------------------------------------------------------------
705// drawing...
706// ---------------------------------------------------------------------------
707
chaviwa76b2712017-09-20 12:02:26 -0700708void Layer::draw(const RenderArea& renderArea, const Region& clip) const {
709 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800710}
711
chaviwa76b2712017-09-20 12:02:26 -0700712void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) const {
713 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800714}
715
chaviwa76b2712017-09-20 12:02:26 -0700716void Layer::draw(const RenderArea& renderArea) const {
717 onDraw(renderArea, Region(renderArea.getBounds()), false);
Dan Stozac7014012014-02-14 15:03:43 -0800718}
719
David Sodman41fdfc92017-11-06 16:09:56 -0800720void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
721 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800722 auto& engine(mFlinger->getRenderEngine());
David Sodman9eeae692017-11-02 10:53:32 -0700723 computeGeometry(renderArea, getBE().mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700724 engine.setupFillWithColor(red, green, blue, alpha);
David Sodman9eeae692017-11-02 10:53:32 -0700725 engine.drawMesh(getBE().mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800726}
727
chaviwa76b2712017-09-20 12:02:26 -0700728void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
David Sodman41fdfc92017-11-06 16:09:56 -0800729 clearWithOpenGL(renderArea, 0, 0, 0, 0);
Mathias Agopian13127d82013-03-05 17:47:11 -0800730}
731
David Sodman41fdfc92017-11-06 16:09:56 -0800732void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type, bool callIntoHwc) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700733 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800734 ALOGE("setCompositionType called without a valid HWC layer");
735 return;
736 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700737 auto& hwcInfo = getBE().mHwcLayers[hwcId];
Dan Stoza9e56aa02015-11-02 13:00:03 -0800738 auto& hwcLayer = hwcInfo.layer;
David Sodman41fdfc92017-11-06 16:09:56 -0800739 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(), to_string(type).c_str(),
740 static_cast<int>(callIntoHwc));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800741 if (hwcInfo.compositionType != type) {
742 ALOGV(" actually setting");
743 hwcInfo.compositionType = type;
744 if (callIntoHwc) {
745 auto error = hwcLayer->setCompositionType(type);
David Sodman41fdfc92017-11-06 16:09:56 -0800746 ALOGE_IF(error != HWC2::Error::None,
747 "[%s] Failed to set "
748 "composition type %s: %s (%d)",
749 mName.string(), to_string(type).c_str(), to_string(error).c_str(),
750 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800751 }
752 }
753}
754
755HWC2::Composition Layer::getCompositionType(int32_t hwcId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -0700756 if (hwcId == DisplayDevice::DISPLAY_ID_INVALID) {
757 // If we're querying the composition type for a display that does not
758 // have a HWC counterpart, then it will always be Client
759 return HWC2::Composition::Client;
760 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700761 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700762 ALOGE("getCompositionType called with an invalid HWC layer");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800763 return HWC2::Composition::Invalid;
764 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700765 return getBE().mHwcLayers.at(hwcId).compositionType;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800766}
767
768void Layer::setClearClientTarget(int32_t hwcId, bool clear) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700769 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800770 ALOGE("setClearClientTarget called without a valid HWC layer");
771 return;
772 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700773 getBE().mHwcLayers[hwcId].clearClientTarget = clear;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800774}
775
776bool Layer::getClearClientTarget(int32_t hwcId) const {
David Sodman6f65f3e2017-11-03 14:28:09 -0700777 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800778 ALOGE("getClearClientTarget called without a valid HWC layer");
779 return false;
780 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700781 return getBE().mHwcLayers.at(hwcId).clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800782}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800783
Dan Stozacac35382016-01-27 12:21:06 -0800784bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
785 if (point->getFrameNumber() <= mCurrentFrameNumber) {
786 // Don't bother with a SyncPoint, since we've already latched the
787 // relevant frame
788 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700789 }
790
Dan Stozacac35382016-01-27 12:21:06 -0800791 Mutex::Autolock lock(mLocalSyncPointMutex);
792 mLocalSyncPoints.push_back(point);
793 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700794}
795
Mathias Agopian13127d82013-03-05 17:47:11 -0800796void Layer::setFiltering(bool filtering) {
797 mFiltering = filtering;
798}
799
800bool Layer::getFiltering() const {
801 return mFiltering;
802}
803
Mathias Agopian13127d82013-03-05 17:47:11 -0800804// ----------------------------------------------------------------------------
805// local state
806// ----------------------------------------------------------------------------
807
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000808static void boundPoint(vec2* point, const Rect& crop) {
809 if (point->x < crop.left) {
810 point->x = crop.left;
811 }
812 if (point->x > crop.right) {
813 point->x = crop.right;
814 }
815 if (point->y < crop.top) {
816 point->y = crop.top;
817 }
818 if (point->y > crop.bottom) {
819 point->y = crop.bottom;
820 }
821}
822
chaviwa76b2712017-09-20 12:02:26 -0700823void Layer::computeGeometry(const RenderArea& renderArea, Mesh& mesh,
824 bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700825 const Layer::State& s(getDrawingState());
chaviwa76b2712017-09-20 12:02:26 -0700826 const Transform renderAreaTransform(renderArea.getTransform());
827 const uint32_t height = renderArea.getHeight();
Dan Stoza80d61162017-12-20 15:57:52 -0800828 FloatRect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700829
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000830 vec2 lt = vec2(win.left, win.top);
831 vec2 lb = vec2(win.left, win.bottom);
832 vec2 rb = vec2(win.right, win.bottom);
833 vec2 rt = vec2(win.right, win.top);
834
Robert Carr1f0a16a2016-10-24 16:27:39 -0700835 Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000836 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700837 lt = layerTransform.transform(lt);
838 lb = layerTransform.transform(lb);
839 rb = layerTransform.transform(rb);
840 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000841 }
842
Robert Carrb5d3d262016-03-25 15:08:13 -0700843 if (!s.finalCrop.isEmpty()) {
844 boundPoint(&lt, s.finalCrop);
845 boundPoint(&lb, s.finalCrop);
846 boundPoint(&rb, s.finalCrop);
847 boundPoint(&rt, s.finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000848 }
849
Mathias Agopianff2ed702013-09-01 21:36:12 -0700850 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700851 position[0] = renderAreaTransform.transform(lt);
852 position[1] = renderAreaTransform.transform(lb);
853 position[2] = renderAreaTransform.transform(rb);
854 position[3] = renderAreaTransform.transform(rt);
David Sodman41fdfc92017-11-06 16:09:56 -0800855 for (size_t i = 0; i < 4; i++) {
chaviwa76b2712017-09-20 12:02:26 -0700856 position[i].y = height - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -0800857 }
858}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800859
David Sodman41fdfc92017-11-06 16:09:56 -0800860bool Layer::isSecure() const {
Dan Stoza23116082015-06-18 14:58:39 -0700861 const Layer::State& s(mDrawingState);
862 return (s.flags & layer_state_t::eLayerSecure);
863}
864
Mathias Agopian13127d82013-03-05 17:47:11 -0800865void Layer::setVisibleRegion(const Region& visibleRegion) {
866 // always called from main thread
867 this->visibleRegion = visibleRegion;
868}
869
870void Layer::setCoveredRegion(const Region& coveredRegion) {
871 // always called from main thread
872 this->coveredRegion = coveredRegion;
873}
874
David Sodman41fdfc92017-11-06 16:09:56 -0800875void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800876 // always called from main thread
877 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
878}
879
Robert Carre5f4f692018-01-12 13:12:28 -0800880void Layer::clearVisibilityRegions() {
881 visibleRegion.clear();
882 visibleNonTransparentRegion.clear();
883 coveredRegion.clear();
884}
885
Mathias Agopian13127d82013-03-05 17:47:11 -0800886// ----------------------------------------------------------------------------
887// transaction
888// ----------------------------------------------------------------------------
889
Dan Stoza7dde5992015-05-22 09:51:44 -0700890void Layer::pushPendingState() {
891 if (!mCurrentState.modified) {
892 return;
893 }
894
Dan Stoza7dde5992015-05-22 09:51:44 -0700895 // If this transaction is waiting on the receipt of a frame, generate a sync
896 // point and send it to the remote layer.
Robert Carr0d480722017-01-10 16:42:54 -0800897 if (mCurrentState.barrierLayer != nullptr) {
898 sp<Layer> barrierLayer = mCurrentState.barrierLayer.promote();
899 if (barrierLayer == nullptr) {
900 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700901 // If we can't promote the layer we are intended to wait on,
902 // then it is expired or otherwise invalid. Allow this transaction
903 // to be applied as per normal (no synchronization).
Robert Carr0d480722017-01-10 16:42:54 -0800904 mCurrentState.barrierLayer = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800905 } else {
David Sodman41fdfc92017-11-06 16:09:56 -0800906 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber);
Robert Carr0d480722017-01-10 16:42:54 -0800907 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800908 mRemoteSyncPoints.push_back(std::move(syncPoint));
909 } else {
910 // We already missed the frame we're supposed to synchronize
911 // on, so go ahead and apply the state update
Robert Carr0d480722017-01-10 16:42:54 -0800912 mCurrentState.barrierLayer = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800913 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700914 }
915
Dan Stoza7dde5992015-05-22 09:51:44 -0700916 // Wake us up to check if the frame has been received
917 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700918 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700919 }
920 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700921 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700922}
923
Pablo Ceballos05289c22016-04-14 15:49:55 -0700924void Layer::popPendingState(State* stateToCommit) {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700925 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700926
927 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700928 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700929}
930
Pablo Ceballos05289c22016-04-14 15:49:55 -0700931bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700932 bool stateUpdateAvailable = false;
933 while (!mPendingStates.empty()) {
Robert Carr0d480722017-01-10 16:42:54 -0800934 if (mPendingStates[0].barrierLayer != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700935 if (mRemoteSyncPoints.empty()) {
936 // If we don't have a sync point for this, apply it anyway. It
937 // will be visually wrong, but it should keep us from getting
938 // into too much trouble.
939 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700940 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700941 stateUpdateAvailable = true;
942 continue;
943 }
944
David Sodman41fdfc92017-11-06 16:09:56 -0800945 if (mRemoteSyncPoints.front()->getFrameNumber() != mPendingStates[0].frameNumber) {
946 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800947
948 // Signal our end of the sync point and then dispose of it
949 mRemoteSyncPoints.front()->setTransactionApplied();
950 mRemoteSyncPoints.pop_front();
951 continue;
952 }
953
Dan Stoza7dde5992015-05-22 09:51:44 -0700954 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
955 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700956 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700957 stateUpdateAvailable = true;
958
959 // Signal our end of the sync point and then dispose of it
960 mRemoteSyncPoints.front()->setTransactionApplied();
961 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800962 } else {
963 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700964 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700965 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700966 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700967 stateUpdateAvailable = true;
968 }
969 }
970
971 // If we still have pending updates, wake SurfaceFlinger back up and point
972 // it at this layer so we can process them
973 if (!mPendingStates.empty()) {
974 setTransactionFlags(eTransactionNeeded);
975 mFlinger->setTransactionFlags(eTraversalNeeded);
976 }
977
978 mCurrentState.modified = false;
979 return stateUpdateAvailable;
980}
981
Mathias Agopian13127d82013-03-05 17:47:11 -0800982uint32_t Layer::doTransaction(uint32_t flags) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800983 ATRACE_CALL();
984
Dan Stoza7dde5992015-05-22 09:51:44 -0700985 pushPendingState();
Pablo Ceballos05289c22016-04-14 15:49:55 -0700986 Layer::State c = getCurrentState();
987 if (!applyPendingStates(&c)) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700988 return 0;
989 }
990
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700991 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800992
David Sodman41fdfc92017-11-06 16:09:56 -0800993 const bool sizeChanged = (c.requested.w != s.requested.w) || (c.requested.h != s.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700994
David Sodmaneb085e02017-10-05 18:49:04 -0700995 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700996 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000997 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800998 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
999 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1000 " requested={ wh={%4u,%4u} }}\n"
1001 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1002 " requested={ wh={%4u,%4u} }}\n",
David Sodman9eeae692017-11-02 10:53:32 -07001003 this, getName().string(), mCurrentTransform,
1004 getEffectiveScalingMode(), c.active.w, c.active.h, c.crop.left, c.crop.top,
1005 c.crop.right, c.crop.bottom, c.crop.getWidth(), c.crop.getHeight(), c.requested.w,
1006 c.requested.h, s.active.w, s.active.h, s.crop.left, s.crop.top, s.crop.right,
1007 s.crop.bottom, s.crop.getWidth(), s.crop.getHeight(), s.requested.w,
1008 s.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001009
Jamie Gennis2a0d5b62011-09-26 16:54:44 -07001010 // record the new size, form this point on, when the client request
1011 // a buffer, it'll get the new size.
David Sodmaneb085e02017-10-05 18:49:04 -07001012 setDefaultBufferSize(c.requested.w, c.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001013 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001014
Robert Carre392b552017-09-19 12:16:05 -07001015 // Don't let Layer::doTransaction update the drawing state
1016 // if we have a pending resize, unless we are in fixed-size mode.
1017 // the drawing state will be updated only once we receive a buffer
1018 // with the correct size.
1019 //
1020 // In particular, we want to make sure the clip (which is part
1021 // of the geometry state) is latched together with the size but is
1022 // latched immediately when no resizing is involved.
1023 //
1024 // If a sideband stream is attached, however, we want to skip this
1025 // optimization so that transactions aren't missed when a buffer
1026 // never arrives
1027 //
1028 // In the case that we don't have a buffer we ignore other factors
1029 // and avoid entering the resizePending state. At a high level the
1030 // resizePending state is to avoid applying the state of the new buffer
1031 // to the old buffer. However in the state where we don't have an old buffer
1032 // there is no such concern but we may still be being used as a parent layer.
David Sodman41fdfc92017-11-06 16:09:56 -08001033 const bool resizePending = ((c.requested.w != c.active.w) || (c.requested.h != c.active.h)) &&
David Sodman0cc69182017-11-17 12:12:07 -08001034 (getBE().compositionInfo.mBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001035 if (!isFixedSize()) {
David Sodman0cc69182017-11-17 12:12:07 -08001036 if (resizePending && getBE().compositionInfo.hwc.sidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001037 flags |= eDontUpdateGeometryState;
1038 }
1039 }
1040
Robert Carr7bf247e2017-05-18 14:02:49 -07001041 // Here we apply various requested geometry states, depending on our
1042 // latching configuration. See Layer.h for a detailed discussion of
1043 // how geometry latching is controlled.
1044 if (!(flags & eDontUpdateGeometryState)) {
Pablo Ceballos7d052572016-06-02 17:46:05 -07001045 Layer::State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001046
1047 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1048 // mode, which causes attributes which normally latch regardless of scaling mode,
1049 // to be delayed. We copy the requested state to the active state making sure
1050 // to respect these rules (again see Layer.h for a detailed discussion).
1051 //
1052 // There is an awkward asymmetry in the handling of the crop states in the position
1053 // states, as can be seen below. Largely this arises from position and transform
1054 // being stored in the same data structure while having different latching rules.
1055 // b/38182305
1056 //
1057 // Careful that "c" and editCurrentState may not begin as equivalent due to
1058 // applyPendingStates in the presence of deferred transactions.
1059 if (mFreezeGeometryUpdates) {
Robert Carr82364e32016-05-15 11:27:47 -07001060 float tx = c.active.transform.tx();
1061 float ty = c.active.transform.ty();
1062 c.active = c.requested;
1063 c.active.transform.set(tx, ty);
1064 editCurrentState.active = c.active;
1065 } else {
1066 editCurrentState.active = editCurrentState.requested;
1067 c.active = c.requested;
1068 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001069 }
1070
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001071 if (s.active != c.active) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001072 // invalidate and recompute the visible regions if needed
1073 flags |= Layer::eVisibleRegion;
1074 }
1075
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001076 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001077 // invalidate and recompute the visible regions if needed
1078 flags |= eVisibleRegion;
1079 this->contentDirty = true;
1080
1081 // we may use linear filtering, if the matrix scales us
Robert Carr3dcabfa2016-03-01 18:36:58 -08001082 const uint8_t type = c.active.transform.getType();
David Sodman41fdfc92017-11-06 16:09:56 -08001083 mNeedsFiltering = (!c.active.transform.preserveRects() || (type >= Transform::SCALE));
Mathias Agopian13127d82013-03-05 17:47:11 -08001084 }
1085
Dan Stozac8145172016-04-28 16:29:06 -07001086 // If the layer is hidden, signal and clear out all local sync points so
1087 // that transactions for layers depending on this layer's frames becoming
1088 // visible are not blocked
1089 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001090 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001091 }
1092
Mathias Agopian13127d82013-03-05 17:47:11 -08001093 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001094 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001095 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001096}
1097
Pablo Ceballos05289c22016-04-14 15:49:55 -07001098void Layer::commitTransaction(const State& stateToCommit) {
1099 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001100}
1101
Mathias Agopian13127d82013-03-05 17:47:11 -08001102uint32_t Layer::getTransactionFlags(uint32_t flags) {
1103 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1104}
1105
1106uint32_t Layer::setTransactionFlags(uint32_t flags) {
1107 return android_atomic_or(flags, &mTransactionFlags);
1108}
1109
Robert Carr82364e32016-05-15 11:27:47 -07001110bool Layer::setPosition(float x, float y, bool immediate) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001111 if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001112 return false;
1113 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001114
1115 // We update the requested and active position simultaneously because
1116 // we want to apply the position portion of the transform matrix immediately,
1117 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Robert Carr3dcabfa2016-03-01 18:36:58 -08001118 mCurrentState.requested.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001119 if (immediate && !mFreezeGeometryUpdates) {
1120 // Here we directly update the active state
1121 // unlike other setters, because we store it within
1122 // the transform, but use different latching rules.
1123 // b/38182305
Robert Carr82364e32016-05-15 11:27:47 -07001124 mCurrentState.active.transform.set(x, y);
1125 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001126 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001127
Dan Stoza7dde5992015-05-22 09:51:44 -07001128 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001129 setTransactionFlags(eTransactionNeeded);
1130 return true;
1131}
Robert Carr82364e32016-05-15 11:27:47 -07001132
Robert Carr1f0a16a2016-10-24 16:27:39 -07001133bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1134 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1135 if (idx < 0) {
1136 return false;
1137 }
1138 if (childLayer->setLayer(z)) {
1139 mCurrentChildren.removeAt(idx);
1140 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001141 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001142 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001143 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001144}
1145
Robert Carr503c7042017-09-27 15:06:08 -07001146bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1147 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1148 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1149 if (idx < 0) {
1150 return false;
1151 }
1152 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1153 mCurrentChildren.removeAt(idx);
1154 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001155 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001156 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001157 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001158}
1159
Robert Carrae060832016-11-28 10:51:00 -08001160bool Layer::setLayer(int32_t z) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001161 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001162 mCurrentState.sequence++;
1163 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001164 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001165
1166 // Discard all relative layering.
1167 if (mCurrentState.zOrderRelativeOf != nullptr) {
1168 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1169 if (strongRelative != nullptr) {
1170 strongRelative->removeZOrderRelative(this);
1171 }
1172 mCurrentState.zOrderRelativeOf = nullptr;
1173 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001174 setTransactionFlags(eTransactionNeeded);
1175 return true;
1176}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001177
Robert Carrdb66e622017-04-10 16:55:57 -07001178void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1179 mCurrentState.zOrderRelatives.remove(relative);
1180 mCurrentState.sequence++;
1181 mCurrentState.modified = true;
1182 setTransactionFlags(eTransactionNeeded);
1183}
1184
1185void Layer::addZOrderRelative(const wp<Layer>& relative) {
1186 mCurrentState.zOrderRelatives.add(relative);
1187 mCurrentState.modified = true;
1188 mCurrentState.sequence++;
1189 setTransactionFlags(eTransactionNeeded);
1190}
1191
Robert Carr503d2bd2017-12-04 15:49:47 -08001192bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001193 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1194 if (handle == nullptr) {
1195 return false;
1196 }
1197 sp<Layer> relative = handle->owner.promote();
1198 if (relative == nullptr) {
1199 return false;
1200 }
1201
Robert Carr503d2bd2017-12-04 15:49:47 -08001202 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1203 mCurrentState.zOrderRelativeOf == relative) {
1204 return false;
1205 }
1206
Robert Carrdb66e622017-04-10 16:55:57 -07001207 mCurrentState.sequence++;
1208 mCurrentState.modified = true;
Robert Carr503d2bd2017-12-04 15:49:47 -08001209 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001210
chaviw9ab4bd12017-11-03 13:11:00 -07001211 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
1212 if (oldZOrderRelativeOf != nullptr) {
1213 oldZOrderRelativeOf->removeZOrderRelative(this);
1214 }
Robert Carrdb66e622017-04-10 16:55:57 -07001215 mCurrentState.zOrderRelativeOf = relative;
1216 relative->addZOrderRelative(this);
1217
1218 setTransactionFlags(eTransactionNeeded);
1219
1220 return true;
1221}
1222
Mathias Agopian13127d82013-03-05 17:47:11 -08001223bool Layer::setSize(uint32_t w, uint32_t h) {
David Sodman41fdfc92017-11-06 16:09:56 -08001224 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001225 mCurrentState.requested.w = w;
1226 mCurrentState.requested.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001227 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001228 setTransactionFlags(eTransactionNeeded);
1229 return true;
1230}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001231bool Layer::setAlpha(float alpha) {
David Sodman41fdfc92017-11-06 16:09:56 -08001232 if (mCurrentState.color.a == alpha) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001233 mCurrentState.sequence++;
chaviw13fdc492017-06-27 12:40:18 -07001234 mCurrentState.color.a = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001235 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001236 setTransactionFlags(eTransactionNeeded);
1237 return true;
1238}
chaviw13fdc492017-06-27 12:40:18 -07001239
1240bool Layer::setColor(const half3& color) {
David Sodman41fdfc92017-11-06 16:09:56 -08001241 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g &&
1242 color.b == mCurrentState.color.b)
chaviw13fdc492017-06-27 12:40:18 -07001243 return false;
1244
1245 mCurrentState.sequence++;
1246 mCurrentState.color.r = color.r;
1247 mCurrentState.color.g = color.g;
1248 mCurrentState.color.b = color.b;
1249 mCurrentState.modified = true;
1250 setTransactionFlags(eTransactionNeeded);
1251 return true;
1252}
1253
Robert Carrd4ae7f32018-06-07 16:10:57 -07001254bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1255 bool allowNonRectPreservingTransforms) {
1256 Transform t;
1257 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1258
1259 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1260 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1261 return false;
1262 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001263 mCurrentState.sequence++;
David Sodman41fdfc92017-11-06 16:09:56 -08001264 mCurrentState.requested.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001265 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001266 setTransactionFlags(eTransactionNeeded);
1267 return true;
1268}
1269bool Layer::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001270 mCurrentState.requestedTransparentRegion = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001271 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001272 setTransactionFlags(eTransactionNeeded);
1273 return true;
1274}
1275bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1276 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
David Sodman41fdfc92017-11-06 16:09:56 -08001277 if (mCurrentState.flags == newFlags) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001278 mCurrentState.sequence++;
1279 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001280 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001281 setTransactionFlags(eTransactionNeeded);
1282 return true;
1283}
Robert Carr99e27f02016-06-16 15:18:02 -07001284
1285bool Layer::setCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001286 if (mCurrentState.requestedCrop == crop) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001287 mCurrentState.sequence++;
Robert Carr99e27f02016-06-16 15:18:02 -07001288 mCurrentState.requestedCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001289 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr99e27f02016-06-16 15:18:02 -07001290 mCurrentState.crop = crop;
1291 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001292 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1293
Dan Stoza7dde5992015-05-22 09:51:44 -07001294 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001295 setTransactionFlags(eTransactionNeeded);
1296 return true;
1297}
Robert Carr8d5227b2017-03-16 15:41:03 -07001298
1299bool Layer::setFinalCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001300 if (mCurrentState.requestedFinalCrop == crop) return false;
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001301 mCurrentState.sequence++;
Robert Carr8d5227b2017-03-16 15:41:03 -07001302 mCurrentState.requestedFinalCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001303 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr8d5227b2017-03-16 15:41:03 -07001304 mCurrentState.finalCrop = crop;
1305 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001306 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1307
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001308 mCurrentState.modified = true;
1309 setTransactionFlags(eTransactionNeeded);
1310 return true;
1311}
Mathias Agopian13127d82013-03-05 17:47:11 -08001312
Robert Carrc3574f72016-03-24 12:19:32 -07001313bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001314 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001315 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001316 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001317 return true;
1318}
1319
rongliuccd34842018-03-14 12:26:23 -07001320void Layer::setInfo(int32_t type, int32_t appId) {
David Sodman41fdfc92017-11-06 16:09:56 -08001321 mCurrentState.appId = appId;
1322 mCurrentState.type = type;
1323 mCurrentState.modified = true;
1324 setTransactionFlags(eTransactionNeeded);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001325}
1326
Mathias Agopian13127d82013-03-05 17:47:11 -08001327bool Layer::setLayerStack(uint32_t layerStack) {
David Sodman41fdfc92017-11-06 16:09:56 -08001328 if (mCurrentState.layerStack == layerStack) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001329 mCurrentState.sequence++;
1330 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001331 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001332 setTransactionFlags(eTransactionNeeded);
1333 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001334}
1335
Robert Carr1f0a16a2016-10-24 16:27:39 -07001336uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001337 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001338 if (p == nullptr) {
1339 return getDrawingState().layerStack;
1340 }
1341 return p->getLayerStack();
1342}
1343
David Sodman41fdfc92017-11-06 16:09:56 -08001344void Layer::deferTransactionUntil(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001345 mCurrentState.barrierLayer = barrierLayer;
Dan Stoza7dde5992015-05-22 09:51:44 -07001346 mCurrentState.frameNumber = frameNumber;
1347 // We don't set eTransactionNeeded, because just receiving a deferral
1348 // request without any other state updates shouldn't actually induce a delay
1349 mCurrentState.modified = true;
1350 pushPendingState();
Robert Carr0d480722017-01-10 16:42:54 -08001351 mCurrentState.barrierLayer = nullptr;
Dan Stoza792e5292016-02-11 11:43:58 -08001352 mCurrentState.frameNumber = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001353 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001354}
1355
David Sodman41fdfc92017-11-06 16:09:56 -08001356void Layer::deferTransactionUntil(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001357 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
1358 deferTransactionUntil(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001359}
1360
Dan Stozaee44edd2015-03-23 15:50:23 -07001361
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001362// ----------------------------------------------------------------------------
1363// pageflip handling...
1364// ----------------------------------------------------------------------------
1365
Robert Carr1f0a16a2016-10-24 16:27:39 -07001366bool Layer::isHiddenByPolicy() const {
1367 const Layer::State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001368 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001369 if (parent != nullptr && parent->isHiddenByPolicy()) {
1370 return true;
1371 }
1372 return s.flags & layer_state_t::eLayerHidden;
1373}
1374
David Sodman41fdfc92017-11-06 16:09:56 -08001375uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001376 // TODO: should we do something special if mSecure is set?
1377 if (mProtectedByApp) {
1378 // need a hardware-protected path to external video sink
1379 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001380 }
Riley Andrews03414a12014-07-01 14:22:59 -07001381 if (mPotentialCursor) {
1382 usage |= GraphicBuffer::USAGE_CURSOR;
1383 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001384 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001385 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001386}
1387
Mathias Agopian84300952012-11-21 16:02:13 -08001388void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001389 uint32_t orientation = 0;
1390 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001391 // The transform hint is used to improve performance, but we can
1392 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001393 // apply to all displays.
Mathias Agopian42977342012-08-05 00:40:46 -07001394 const Transform& planeTransform(hw->getTransform());
Mathias Agopian4fec8732012-06-29 14:12:52 -07001395 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07001396 if (orientation & Transform::ROT_INVALID) {
1397 orientation = 0;
1398 }
1399 }
David Sodmaneb085e02017-10-05 18:49:04 -07001400 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001401}
1402
Mathias Agopian13127d82013-03-05 17:47:11 -08001403// ----------------------------------------------------------------------------
1404// debugging
1405// ----------------------------------------------------------------------------
1406
Kalle Raitaa099a242017-01-11 11:17:29 -08001407LayerDebugInfo Layer::getLayerDebugInfo() const {
1408 LayerDebugInfo info;
1409 const Layer::State& ds = getDrawingState();
1410 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001411 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001412 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
1413 info.mType = String8(getTypeId());
1414 info.mTransparentRegion = ds.activeTransparentRegion;
1415 info.mVisibleRegion = visibleRegion;
1416 info.mSurfaceDamageRegion = surfaceDamageRegion;
1417 info.mLayerStack = getLayerStack();
1418 info.mX = ds.active.transform.tx();
1419 info.mY = ds.active.transform.ty();
1420 info.mZ = ds.z;
1421 info.mWidth = ds.active.w;
1422 info.mHeight = ds.active.h;
1423 info.mCrop = ds.crop;
1424 info.mFinalCrop = ds.finalCrop;
chaviw13fdc492017-06-27 12:40:18 -07001425 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001426 info.mFlags = ds.flags;
1427 info.mPixelFormat = getPixelFormat();
Chia-I Wu1b6bafc2018-05-22 12:03:00 -07001428 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Kalle Raitaa099a242017-01-11 11:17:29 -08001429 info.mMatrix[0][0] = ds.active.transform[0][0];
1430 info.mMatrix[0][1] = ds.active.transform[0][1];
1431 info.mMatrix[1][0] = ds.active.transform[1][0];
1432 info.mMatrix[1][1] = ds.active.transform[1][1];
1433 {
David Sodman0cc69182017-11-17 12:12:07 -08001434 sp<const GraphicBuffer> buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001435 if (buffer != 0) {
1436 info.mActiveBufferWidth = buffer->getWidth();
1437 info.mActiveBufferHeight = buffer->getHeight();
1438 info.mActiveBufferStride = buffer->getStride();
1439 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001440 } else {
1441 info.mActiveBufferWidth = 0;
1442 info.mActiveBufferHeight = 0;
1443 info.mActiveBufferStride = 0;
1444 info.mActiveBufferFormat = 0;
1445 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001446 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001447 info.mNumQueuedFrames = getQueuedFrameCount();
1448 info.mRefreshPending = isBufferLatched();
1449 info.mIsOpaque = isOpaque(ds);
1450 info.mContentDirty = contentDirty;
1451 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001452}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001453
Dan Stozae22aec72016-08-01 13:20:59 -07001454void Layer::miniDumpHeader(String8& result) {
1455 result.append("----------------------------------------");
1456 result.append("---------------------------------------\n");
1457 result.append(" Layer name\n");
1458 result.append(" Z | ");
1459 result.append(" Comp Type | ");
1460 result.append(" Disp Frame (LTRB) | ");
1461 result.append(" Source Crop (LTRB)\n");
1462 result.append("----------------------------------------");
1463 result.append("---------------------------------------\n");
1464}
1465
1466void Layer::miniDump(String8& result, int32_t hwcId) const {
David Sodman6f65f3e2017-11-03 14:28:09 -07001467 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stozae22aec72016-08-01 13:20:59 -07001468 return;
1469 }
1470
1471 String8 name;
1472 if (mName.length() > 77) {
1473 std::string shortened;
1474 shortened.append(mName.string(), 36);
1475 shortened.append("[...]");
1476 shortened.append(mName.string() + (mName.length() - 36), 36);
1477 name = shortened.c_str();
1478 } else {
1479 name = mName;
1480 }
1481
1482 result.appendFormat(" %s\n", name.string());
1483
1484 const Layer::State& layerState(getDrawingState());
David Sodman6f65f3e2017-11-03 14:28:09 -07001485 const LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers.at(hwcId);
Chia-I Wu1e043612018-03-01 09:45:09 -08001486 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
1487 result.appendFormat(" rel %6d | ", layerState.z);
1488 } else {
1489 result.appendFormat(" %10d | ", layerState.z);
1490 }
David Sodman41fdfc92017-11-06 16:09:56 -08001491 result.appendFormat("%10s | ", to_string(getCompositionType(hwcId)).c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001492 const Rect& frame = hwcInfo.displayFrame;
David Sodman41fdfc92017-11-06 16:09:56 -08001493 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Dan Stoza5a423ea2017-02-16 14:10:39 -08001494 const FloatRect& crop = hwcInfo.sourceCrop;
David Sodman41fdfc92017-11-06 16:09:56 -08001495 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 -07001496
1497 result.append("- - - - - - - - - - - - - - - - - - - - ");
1498 result.append("- - - - - - - - - - - - - - - - - - - -\n");
1499}
Dan Stozae22aec72016-08-01 13:20:59 -07001500
Svetoslavd85084b2014-03-20 10:28:31 -07001501void Layer::dumpFrameStats(String8& result) const {
1502 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001503}
1504
Svetoslavd85084b2014-03-20 10:28:31 -07001505void Layer::clearFrameStats() {
1506 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001507}
1508
Jamie Gennis6547ff42013-07-16 20:12:42 -07001509void Layer::logFrameStats() {
1510 mFrameTracker.logAndResetStats(mName);
1511}
1512
Svetoslavd85084b2014-03-20 10:28:31 -07001513void Layer::getFrameStats(FrameStats* outStats) const {
1514 mFrameTracker.getStats(outStats);
1515}
1516
Brian Andersond6927fb2016-07-23 23:37:30 -07001517void Layer::dumpFrameEvents(String8& result) {
David Sodman41fdfc92017-11-06 16:09:56 -08001518 result.appendFormat("- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001519 Mutex::Autolock lock(mFrameEventHistoryMutex);
1520 mFrameEventHistory.checkFencesForCompletion();
1521 mFrameEventHistory.dump(result);
1522}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001523
Brian Anderson5ea5e592016-12-01 16:54:33 -08001524void Layer::onDisconnect() {
1525 Mutex::Autolock lock(mFrameEventHistoryMutex);
1526 mFrameEventHistory.onDisconnect();
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001527 mTimeStats.onDisconnect(getName().c_str());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001528}
1529
Brian Anderson3890c392016-07-25 12:48:08 -07001530void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001531 FrameEventHistoryDelta* outDelta) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001532 if (newTimestamps) {
1533 mTimeStats.setPostTime(getName().c_str(), newTimestamps->frameNumber,
1534 newTimestamps->postedTime);
1535 }
1536
Brian Andersond6927fb2016-07-23 23:37:30 -07001537 Mutex::Autolock lock(mFrameEventHistoryMutex);
1538 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001539 // If there are any unsignaled fences in the aquire timeline at this
1540 // point, the previously queued frame hasn't been latched yet. Go ahead
1541 // and try to get the signal time here so the syscall is taken out of
1542 // the main thread's critical path.
1543 mAcquireTimeline.updateSignalTimes();
1544 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001545 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001546 mFrameEventHistory.addQueue(*newTimestamps);
1547 }
1548
Brian Anderson3890c392016-07-25 12:48:08 -07001549 if (outDelta) {
1550 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001551 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001552}
Dan Stozae77c7662016-05-13 11:37:28 -07001553
Chia-I Wu98f1c102017-05-30 14:54:08 -07001554size_t Layer::getChildrenCount() const {
1555 size_t count = 0;
1556 for (const sp<Layer>& child : mCurrentChildren) {
1557 count += 1 + child->getChildrenCount();
1558 }
1559 return count;
1560}
1561
Robert Carr1f0a16a2016-10-24 16:27:39 -07001562void Layer::addChild(const sp<Layer>& layer) {
1563 mCurrentChildren.add(layer);
1564 layer->setParent(this);
1565}
1566
1567ssize_t Layer::removeChild(const sp<Layer>& layer) {
1568 layer->setParent(nullptr);
1569 return mCurrentChildren.remove(layer);
1570}
1571
Robert Carr1db73f62016-12-21 12:58:51 -08001572bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1573 sp<Handle> handle = nullptr;
1574 sp<Layer> newParent = nullptr;
1575 if (newParentHandle == nullptr) {
1576 return false;
1577 }
1578 handle = static_cast<Handle*>(newParentHandle.get());
1579 newParent = handle->owner.promote();
1580 if (newParent == nullptr) {
1581 ALOGE("Unable to promote Layer handle");
1582 return false;
1583 }
1584
1585 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001586 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001587
1588 sp<Client> client(child->mClientRef.promote());
1589 if (client != nullptr) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001590 client->updateParent(newParent);
Robert Carr1db73f62016-12-21 12:58:51 -08001591 }
1592 }
1593 mCurrentChildren.clear();
1594
1595 return true;
1596}
1597
Robert Carr15eae092018-03-23 13:43:53 -07001598void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001599 for (const sp<Layer>& child : mDrawingChildren) {
1600 child->mDrawingParent = newParent;
1601 }
1602}
1603
chaviwf1961f72017-09-18 16:41:07 -07001604bool Layer::reparent(const sp<IBinder>& newParentHandle) {
1605 if (newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001606 return false;
1607 }
1608
1609 auto handle = static_cast<Handle*>(newParentHandle.get());
1610 sp<Layer> newParent = handle->owner.promote();
1611 if (newParent == nullptr) {
1612 ALOGE("Unable to promote Layer handle");
1613 return false;
1614 }
1615
chaviwf1961f72017-09-18 16:41:07 -07001616 sp<Layer> parent = getParent();
1617 if (parent != nullptr) {
1618 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001619 }
chaviwf1961f72017-09-18 16:41:07 -07001620 newParent->addChild(this);
chaviw06178942017-07-27 10:25:59 -07001621
chaviwf1961f72017-09-18 16:41:07 -07001622 sp<Client> client(mClientRef.promote());
chaviw06178942017-07-27 10:25:59 -07001623 sp<Client> newParentClient(newParent->mClientRef.promote());
1624
chaviwf1961f72017-09-18 16:41:07 -07001625 if (client != newParentClient) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001626 client->updateParent(newParent);
chaviw06178942017-07-27 10:25:59 -07001627 }
1628
chaviw06178942017-07-27 10:25:59 -07001629 return true;
1630}
1631
Robert Carr9524cb32017-02-13 11:32:32 -08001632bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001633 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001634 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001635 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001636 if (client != nullptr && parentClient != client) {
Robert Carr7f619b22017-11-06 12:56:35 -08001637 client->detachLayer(child.get());
1638 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001639 }
Robert Carr7f619b22017-11-06 12:56:35 -08001640 }
Robert Carr9524cb32017-02-13 11:32:32 -08001641
1642 return true;
1643}
1644
Chia-I Wu11481472018-05-04 10:43:19 -07001645bool Layer::isLegacyDataSpace() const {
1646 // return true when no higher bits are set
Chia-I Wu1b6bafc2018-05-22 12:03:00 -07001647 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001648 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001649}
1650
Robert Carr1f0a16a2016-10-24 16:27:39 -07001651void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001652 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001653}
1654
1655void Layer::clearSyncPoints() {
1656 for (const auto& child : mCurrentChildren) {
1657 child->clearSyncPoints();
1658 }
1659
1660 Mutex::Autolock lock(mLocalSyncPointMutex);
1661 for (auto& point : mLocalSyncPoints) {
1662 point->setFrameAvailable();
1663 }
1664 mLocalSyncPoints.clear();
1665}
1666
1667int32_t Layer::getZ() const {
1668 return mDrawingState.z;
1669}
1670
Robert Carr29abff82017-12-04 13:51:20 -08001671bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1672 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1673 const State& state = useDrawing ? mDrawingState : mCurrentState;
1674 return state.zOrderRelativeOf != nullptr;
1675}
1676
David Sodman41fdfc92017-11-06 16:09:56 -08001677__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001678 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001679 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1680 "makeTraversalList received invalid stateSet");
1681 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1682 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1683 const State& state = useDrawing ? mDrawingState : mCurrentState;
1684
Robert Carr29abff82017-12-04 13:51:20 -08001685 if (state.zOrderRelatives.size() == 0) {
1686 *outSkipRelativeZUsers = true;
1687 return children;
1688 }
1689
chaviw738df022018-05-31 16:11:27 -07001690 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001691 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001692 sp<Layer> strongRelative = weakRelative.promote();
1693 if (strongRelative != nullptr) {
1694 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001695 }
1696 }
1697
Dan Stoza412903f2017-04-27 13:42:17 -07001698 for (const sp<Layer>& child : children) {
Robert Carr503c7042017-09-27 15:06:08 -07001699 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1700 if (childState.zOrderRelativeOf != nullptr) {
1701 continue;
1702 }
Robert Carrdb66e622017-04-10 16:55:57 -07001703 traverse.add(child);
1704 }
1705
1706 return traverse;
1707}
1708
Robert Carr1f0a16a2016-10-24 16:27:39 -07001709/**
Robert Carrdb66e622017-04-10 16:55:57 -07001710 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001711 */
Dan Stoza412903f2017-04-27 13:42:17 -07001712void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001713 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1714 // produce a new list for traversing, including our relatives, and not including our children
1715 // who are relatives of another surface. In the case that there are no relative Z,
1716 // makeTraversalList returns our children directly to avoid significant overhead.
1717 // However in this case we need to take the responsibility for filtering children which
1718 // are relatives of another surface here.
1719 bool skipRelativeZUsers = false;
1720 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001721
Robert Carr1f0a16a2016-10-24 16:27:39 -07001722 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001723 for (; i < list.size(); i++) {
1724 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001725 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1726 continue;
1727 }
1728
Robert Carrdb66e622017-04-10 16:55:57 -07001729 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001730 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001731 }
Dan Stoza412903f2017-04-27 13:42:17 -07001732 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001733 }
Robert Carr29abff82017-12-04 13:51:20 -08001734
Dan Stoza412903f2017-04-27 13:42:17 -07001735 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001736 for (; i < list.size(); i++) {
1737 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001738
1739 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1740 continue;
1741 }
Dan Stoza412903f2017-04-27 13:42:17 -07001742 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001743 }
1744}
1745
1746/**
Robert Carrdb66e622017-04-10 16:55:57 -07001747 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001748 */
Dan Stoza412903f2017-04-27 13:42:17 -07001749void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1750 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001751 // See traverseInZOrder for documentation.
1752 bool skipRelativeZUsers = false;
1753 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001754
Robert Carr1f0a16a2016-10-24 16:27:39 -07001755 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001756 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001757 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001758
1759 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1760 continue;
1761 }
1762
Robert Carrdb66e622017-04-10 16:55:57 -07001763 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001764 break;
1765 }
Dan Stoza412903f2017-04-27 13:42:17 -07001766 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001767 }
Dan Stoza412903f2017-04-27 13:42:17 -07001768 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001769 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001770 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001771
1772 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1773 continue;
1774 }
1775
Dan Stoza412903f2017-04-27 13:42:17 -07001776 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001777 }
1778}
1779
chaviw4b129c22018-04-09 16:19:43 -07001780LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1781 const std::vector<Layer*>& layersInTree) {
1782 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1783 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001784 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1785 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
chaviw4b129c22018-04-09 16:19:43 -07001786 const State& state = useDrawing ? mDrawingState : mCurrentState;
1787
chaviw738df022018-05-31 16:11:27 -07001788 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001789 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1790 sp<Layer> strongRelative = weakRelative.promote();
1791 // Only add relative layers that are also descendents of the top most parent of the tree.
1792 // If a relative layer is not a descendent, then it should be ignored.
1793 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1794 traverse.add(strongRelative);
1795 }
1796 }
1797
1798 for (const sp<Layer>& child : children) {
1799 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1800 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1801 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1802 // the child here since it won't be added later as a relative.
1803 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1804 childState.zOrderRelativeOf.promote().get())) {
1805 continue;
1806 }
1807 traverse.add(child);
1808 }
1809
1810 return traverse;
1811}
1812
1813void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1814 LayerVector::StateSet stateSet,
1815 const LayerVector::Visitor& visitor) {
1816 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001817
1818 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001819 for (; i < list.size(); i++) {
1820 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001821 if (relative->getZ() >= 0) {
1822 break;
1823 }
chaviw4b129c22018-04-09 16:19:43 -07001824 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001825 }
chaviw4b129c22018-04-09 16:19:43 -07001826
chaviwa76b2712017-09-20 12:02:26 -07001827 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001828 for (; i < list.size(); i++) {
1829 const auto& relative = list[i];
1830 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001831 }
1832}
1833
chaviw4b129c22018-04-09 16:19:43 -07001834std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1835 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1836 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1837
1838 std::vector<Layer*> layersInTree = {this};
1839 for (size_t i = 0; i < children.size(); i++) {
1840 const auto& child = children[i];
1841 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1842 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1843 }
1844
1845 return layersInTree;
1846}
1847
1848void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1849 const LayerVector::Visitor& visitor) {
1850 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1851 std::sort(layersInTree.begin(), layersInTree.end());
1852 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1853}
1854
Robert Carr1f0a16a2016-10-24 16:27:39 -07001855Transform Layer::getTransform() const {
1856 Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001857 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001858 if (p != nullptr) {
1859 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07001860
1861 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
1862 // it isFixedSize) then there may be additional scaling not accounted
1863 // for in the transform. We need to mirror this scaling in child surfaces
1864 // or we will break the contract where WM can treat child surfaces as
1865 // pixels in the parent surface.
David Sodman0cc69182017-11-17 12:12:07 -08001866 if (p->isFixedSize() && p->getBE().compositionInfo.mBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07001867 int bufferWidth;
1868 int bufferHeight;
1869 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
David Sodman0cc69182017-11-17 12:12:07 -08001870 bufferWidth = p->getBE().compositionInfo.mBuffer->getWidth();
1871 bufferHeight = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001872 } else {
David Sodman0cc69182017-11-17 12:12:07 -08001873 bufferHeight = p->getBE().compositionInfo.mBuffer->getWidth();
1874 bufferWidth = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001875 }
David Sodman41fdfc92017-11-06 16:09:56 -08001876 float sx = p->getDrawingState().active.w / static_cast<float>(bufferWidth);
1877 float sy = p->getDrawingState().active.h / static_cast<float>(bufferHeight);
Robert Carr9b429f42017-04-17 14:56:57 -07001878 Transform extraParentScaling;
1879 extraParentScaling.set(sx, 0, 0, sy);
1880 t = t * extraParentScaling;
1881 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001882 }
1883 return t * getDrawingState().active.transform;
1884}
1885
chaviw13fdc492017-06-27 12:40:18 -07001886half Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001887 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07001888
chaviw13fdc492017-06-27 12:40:18 -07001889 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1890 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001891}
Robert Carr6452f122017-03-21 10:41:29 -07001892
chaviw13fdc492017-06-27 12:40:18 -07001893half4 Layer::getColor() const {
1894 const half4 color(getDrawingState().color);
1895 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001896}
Robert Carr6452f122017-03-21 10:41:29 -07001897
Robert Carr1f0a16a2016-10-24 16:27:39 -07001898void Layer::commitChildList() {
1899 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1900 const auto& child = mCurrentChildren[i];
1901 child->commitChildList();
1902 }
1903 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001904 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001905}
1906
chaviw1d044282017-09-27 12:19:28 -07001907void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
1908 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1909 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1910 const State& state = useDrawing ? mDrawingState : mCurrentState;
1911
1912 Transform requestedTransform = state.active.transform;
1913 Transform transform = getTransform();
1914
1915 layerInfo->set_id(sequence);
1916 layerInfo->set_name(getName().c_str());
1917 layerInfo->set_type(String8(getTypeId()));
1918
1919 for (const auto& child : children) {
1920 layerInfo->add_children(child->sequence);
1921 }
1922
1923 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1924 sp<Layer> strongRelative = weakRelative.promote();
1925 if (strongRelative != nullptr) {
1926 layerInfo->add_relatives(strongRelative->sequence);
1927 }
1928 }
1929
1930 LayerProtoHelper::writeToProto(state.activeTransparentRegion,
1931 layerInfo->mutable_transparent_region());
1932 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
1933 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
1934
1935 layerInfo->set_layer_stack(getLayerStack());
1936 layerInfo->set_z(state.z);
1937
1938 PositionProto* position = layerInfo->mutable_position();
1939 position->set_x(transform.tx());
1940 position->set_y(transform.ty());
1941
1942 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
1943 requestedPosition->set_x(requestedTransform.tx());
1944 requestedPosition->set_y(requestedTransform.ty());
1945
1946 SizeProto* size = layerInfo->mutable_size();
1947 size->set_w(state.active.w);
1948 size->set_h(state.active.h);
1949
1950 LayerProtoHelper::writeToProto(state.crop, layerInfo->mutable_crop());
1951 LayerProtoHelper::writeToProto(state.finalCrop, layerInfo->mutable_final_crop());
1952
1953 layerInfo->set_is_opaque(isOpaque(state));
1954 layerInfo->set_invalidate(contentDirty);
Chia-I Wu1b6bafc2018-05-22 12:03:00 -07001955
1956 // XXX (b/79210409) mCurrentDataSpace is not protected
1957 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
1958
chaviw1d044282017-09-27 12:19:28 -07001959 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1960 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
1961 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
1962 layerInfo->set_flags(state.flags);
1963
1964 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1965 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
1966
Jorim Jaggi8e0af362017-11-14 16:28:28 +01001967 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07001968 if (parent != nullptr) {
1969 layerInfo->set_parent(parent->sequence);
1970 }
1971
1972 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1973 if (zOrderRelativeOf != nullptr) {
1974 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1975 }
1976
Chia-I Wu1b6bafc2018-05-22 12:03:00 -07001977 // XXX getBE().compositionInfo.mBuffer is not protected
David Sodman0cc69182017-11-17 12:12:07 -08001978 auto buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001979 if (buffer != nullptr) {
1980 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
chaviw1d044282017-09-27 12:19:28 -07001981 }
1982
1983 layerInfo->set_queued_frames(getQueuedFrameCount());
1984 layerInfo->set_refresh_pending(isBufferLatched());
rongliuccd34842018-03-14 12:26:23 -07001985 layerInfo->set_window_type(state.type);
1986 layerInfo->set_app_id(state.appId);
chaviw9885bd42018-07-10 16:57:27 -07001987 layerInfo->set_curr_frame(mCurrentFrameNumber);
1988
1989 for (const auto& pendingState : mPendingStates) {
1990 auto barrierLayer = pendingState.barrierLayer.promote();
1991 if (barrierLayer != nullptr) {
1992 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
1993 barrierLayerProto->set_id(barrierLayer->sequence);
1994 barrierLayerProto->set_frame_number(pendingState.frameNumber);
1995 }
1996 }
chaviw1d044282017-09-27 12:19:28 -07001997}
1998
Yiwei Zhang068e31b2018-02-21 13:02:45 -08001999void Layer::writeToProto(LayerProto* layerInfo, int32_t hwcId) {
Peiyong Lin0874d2b2018-09-04 09:39:34 -07002000 if (!hasHwcLayer(hwcId)) {
2001 return;
2002 }
Yiwei Zhang068e31b2018-02-21 13:02:45 -08002003 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
2004
2005 const auto& hwcInfo = getBE().mHwcLayers.at(hwcId);
2006
2007 const Rect& frame = hwcInfo.displayFrame;
2008 LayerProtoHelper::writeToProto(frame, layerInfo->mutable_hwc_frame());
2009
2010 const FloatRect& crop = hwcInfo.sourceCrop;
2011 LayerProtoHelper::writeToProto(crop, layerInfo->mutable_hwc_crop());
2012
2013 const int32_t transform = static_cast<int32_t>(hwcInfo.transform);
2014 layerInfo->set_hwc_transform(transform);
Yiwei Zhang7c64f172018-03-07 14:52:28 -08002015
2016 const int32_t compositionType = static_cast<int32_t>(hwcInfo.compositionType);
2017 layerInfo->set_hwc_composition_type(compositionType);
2018
2019 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
2020 static_cast<BufferLayer*>(this)->isProtected()) {
2021 layerInfo->set_is_protected(true);
2022 } else {
2023 layerInfo->set_is_protected(false);
2024 }
Yiwei Zhang068e31b2018-02-21 13:02:45 -08002025}
2026
Mathias Agopian13127d82013-03-05 17:47:11 -08002027// ---------------------------------------------------------------------------
2028
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002029}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002030
2031#if defined(__gl_h_)
2032#error "don't include gl/gl.h in this file"
2033#endif
2034
2035#if defined(__gl2_h_)
2036#error "don't include gl2/gl2.h in this file"
2037#endif