blob: ee2a8fff0f488b83999085b726b095eb5069e3b6 [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),
David Sodman41fdfc92017-11-06 16:09:56 -0800101 mFreezeGeometryUpdates(false) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800102
Mathias Agopiana67932f2011-04-20 14:20:59 -0700103 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700104
105 uint32_t layerFlags = 0;
David Sodman41fdfc92017-11-06 16:09:56 -0800106 if (flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
107 if (flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
108 if (flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700109
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700110 mName = name;
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700111 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700112
113 mCurrentState.active.w = w;
114 mCurrentState.active.h = h;
David Sodman0c69cad2017-08-21 12:12:51 -0700115 mCurrentState.flags = layerFlags;
Robert Carr3dcabfa2016-03-01 18:36:58 -0800116 mCurrentState.active.transform.set(0, 0);
Robert Carrb5d3d262016-03-25 15:08:13 -0700117 mCurrentState.crop.makeInvalid();
118 mCurrentState.finalCrop.makeInvalid();
Robert Carr7bf247e2017-05-18 14:02:49 -0700119 mCurrentState.requestedFinalCrop = mCurrentState.finalCrop;
120 mCurrentState.requestedCrop = mCurrentState.crop;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700121 mCurrentState.z = 0;
chaviw13fdc492017-06-27 12:40:18 -0700122 mCurrentState.color.a = 1.0f;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700123 mCurrentState.layerStack = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700124 mCurrentState.sequence = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700125 mCurrentState.requested = mCurrentState.active;
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700126 mCurrentState.dataSpace = ui::Dataspace::UNKNOWN;
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);
David Sodman9eeae692017-11-02 10:53:32 -0700136
Jamie Gennise8696a42012-01-15 18:54:57 -0800137}
138
Dan Stoza9fdb7e02018-06-21 12:10:12 -0700139void Layer::onFirstRef() NO_THREAD_SAFETY_ANALYSIS {
140 if (!isCreatedFromMainThread()) {
141 // Grab the SF state lock during this since it's the only way to safely access HWC
142 mFlinger->mStateLock.lock();
143 }
144
145 const auto& hwc = mFlinger->getHwComposer();
146 const auto& activeConfig = hwc.getActiveConfig(HWC_DISPLAY_PRIMARY);
147 nsecs_t displayPeriod = activeConfig->getVsyncPeriod();
148 mFrameTracker.setDisplayRefreshPeriod(displayPeriod);
149
150 if (!isCreatedFromMainThread()) {
151 mFlinger->mStateLock.unlock();
152 }
153}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700154
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700155Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800156 sp<Client> c(mClientRef.promote());
157 if (c != 0) {
158 c->detachLayer(this);
159 }
160
161 for (auto& point : mRemoteSyncPoints) {
162 point->setTransactionApplied();
163 }
164 for (auto& point : mLocalSyncPoints) {
165 point->setFrameAvailable();
166 }
Jamie Gennis6547ff42013-07-16 20:12:42 -0700167 mFrameTracker.logAndResetStats(mName);
Mathias Agopian96f08192010-06-02 23:28:45 -0700168}
169
Mathias Agopian13127d82013-03-05 17:47:11 -0800170// ---------------------------------------------------------------------------
171// callbacks
172// ---------------------------------------------------------------------------
173
David Sodmaneb085e02017-10-05 18:49:04 -0700174/*
175 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
176 * Layer. So, the implementation is done in BufferLayer. When called on a
177 * ColorLayer object, it's essentially a NOP.
178 */
David Sodmaneb085e02017-10-05 18:49:04 -0700179void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800180
Chia-I Wuc6657022017-08-15 11:18:17 -0700181void Layer::onRemovedFromCurrentState() {
182 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
183
chaviw8b3871a2017-11-01 17:41:01 -0700184 mPendingRemoval = true;
185
Robert Carr5edb1ad2017-04-25 10:54:24 -0700186 if (mCurrentState.zOrderRelativeOf != nullptr) {
187 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
188 if (strongRelative != nullptr) {
189 strongRelative->removeZOrderRelative(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700190 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700191 }
192 mCurrentState.zOrderRelativeOf = nullptr;
193 }
194
Chia-I Wuc6657022017-08-15 11:18:17 -0700195 for (const auto& child : mCurrentChildren) {
196 child->onRemovedFromCurrentState();
197 }
198}
Chia-I Wu38512252017-05-17 14:36:16 -0700199
Chia-I Wuc6657022017-08-15 11:18:17 -0700200void Layer::onRemoved() {
201 // the layer is removed from SF mLayersPendingRemoval
David Sodmaneb085e02017-10-05 18:49:04 -0700202 abandon();
Chia-I Wuc6657022017-08-15 11:18:17 -0700203
Steven Thomasb02664d2017-07-26 18:48:28 -0700204 destroyAllHwcLayers();
Chia-I Wu38512252017-05-17 14:36:16 -0700205
Robert Carr1f0a16a2016-10-24 16:27:39 -0700206 for (const auto& child : mCurrentChildren) {
207 child->onRemoved();
208 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700209}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700210
Mathias Agopian13127d82013-03-05 17:47:11 -0800211// ---------------------------------------------------------------------------
212// set-up
213// ---------------------------------------------------------------------------
214
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700215const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800216 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800217}
218
chaviw13fdc492017-06-27 12:40:18 -0700219bool Layer::getPremultipledAlpha() const {
220 return mPremultipliedAlpha;
221}
222
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700223sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800224 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700225 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800226}
227
228// ---------------------------------------------------------------------------
229// h/w composer set-up
230// ---------------------------------------------------------------------------
231
Steven Thomasb02664d2017-07-26 18:48:28 -0700232bool Layer::createHwcLayer(HWComposer* hwc, int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700233 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.count(hwcId) != 0,
David Sodman9eeae692017-11-02 10:53:32 -0700234 "Already have a layer for hwcId %d", hwcId);
Steven Thomasb02664d2017-07-26 18:48:28 -0700235 HWC2::Layer* layer = hwc->createLayer(hwcId);
236 if (!layer) {
237 return false;
238 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700239 LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers[hwcId];
Steven Thomasb02664d2017-07-26 18:48:28 -0700240 hwcInfo.hwc = hwc;
241 hwcInfo.layer = layer;
242 layer->setLayerDestroyedListener(
David Sodman6f65f3e2017-11-03 14:28:09 -0700243 [this, hwcId](HWC2::Layer* /*layer*/) { getBE().mHwcLayers.erase(hwcId); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700244 return true;
245}
246
Chia-I Wu83806892017-11-16 10:50:20 -0800247bool Layer::destroyHwcLayer(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700248 if (getBE().mHwcLayers.count(hwcId) == 0) {
Chia-I Wu83806892017-11-16 10:50:20 -0800249 return false;
Steven Thomasb02664d2017-07-26 18:48:28 -0700250 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700251 auto& hwcInfo = getBE().mHwcLayers[hwcId];
David Sodman41fdfc92017-11-06 16:09:56 -0800252 LOG_ALWAYS_FATAL_IF(hwcInfo.layer == nullptr, "Attempt to destroy null layer");
Steven Thomasb02664d2017-07-26 18:48:28 -0700253 LOG_ALWAYS_FATAL_IF(hwcInfo.hwc == nullptr, "Missing HWComposer");
254 hwcInfo.hwc->destroyLayer(hwcId, hwcInfo.layer);
255 // The layer destroyed listener should have cleared the entry from
256 // mHwcLayers. Verify that.
David Sodman6f65f3e2017-11-03 14:28:09 -0700257 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.count(hwcId) != 0,
258 "Stale layer entry in getBE().mHwcLayers");
Chia-I Wu83806892017-11-16 10:50:20 -0800259 return true;
Steven Thomasb02664d2017-07-26 18:48:28 -0700260}
261
262void Layer::destroyAllHwcLayers() {
David Sodman6f65f3e2017-11-03 14:28:09 -0700263 size_t numLayers = getBE().mHwcLayers.size();
Steven Thomasb02664d2017-07-26 18:48:28 -0700264 for (size_t i = 0; i < numLayers; ++i) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700265 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.empty(), "destroyAllHwcLayers failed");
266 destroyHwcLayer(getBE().mHwcLayers.begin()->first);
Steven Thomasb02664d2017-07-26 18:48:28 -0700267 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700268 LOG_ALWAYS_FATAL_IF(!getBE().mHwcLayers.empty(),
David Sodman41fdfc92017-11-06 16:09:56 -0800269 "All hardware composer layers should have been destroyed");
Steven Thomasb02664d2017-07-26 18:48:28 -0700270}
Steven Thomasb02664d2017-07-26 18:48:28 -0700271
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800272Rect Layer::getContentCrop() const {
273 // this is the crop rectangle that applies to the buffer
274 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700275 Rect crop;
276 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800277 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700278 crop = mCurrentCrop;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800279 } else if (getBE().compositionInfo.mBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800280 // otherwise we use the whole buffer
David Sodman0cc69182017-11-17 12:12:07 -0800281 crop = getBE().compositionInfo.mBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700282 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800283 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700284 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700285 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700286 return crop;
287}
288
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700289static Rect reduce(const Rect& win, const Region& exclude) {
290 if (CC_LIKELY(exclude.isEmpty())) {
291 return win;
292 }
293 if (exclude.isRect()) {
294 return win.reduce(exclude.getBounds());
295 }
296 return Region(win).subtract(exclude).getBounds();
297}
298
Dan Stoza80d61162017-12-20 15:57:52 -0800299static FloatRect reduce(const FloatRect& win, const Region& exclude) {
300 if (CC_LIKELY(exclude.isEmpty())) {
301 return win;
302 }
303 // Convert through Rect (by rounding) for lack of FloatRegion
304 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
305}
306
Robert Carr1f0a16a2016-10-24 16:27:39 -0700307Rect Layer::computeScreenBounds(bool reduceTransparentRegion) const {
308 const Layer::State& s(getDrawingState());
309 Rect win(s.active.w, s.active.h);
310
311 if (!s.crop.isEmpty()) {
312 win.intersect(s.crop, &win);
313 }
314
315 Transform t = getTransform();
316 win = t.transform(win);
317
Robert Carr41b08b52017-06-01 16:11:34 -0700318 if (!s.finalCrop.isEmpty()) {
319 win.intersect(s.finalCrop, &win);
320 }
321
Chia-I Wue41dbe62017-06-13 14:10:56 -0700322 const sp<Layer>& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700323 // Now we need to calculate the parent bounds, so we can clip ourselves to those.
324 // When calculating the parent bounds for purposes of clipping,
325 // we don't need to constrain the parent to its transparent region.
326 // The transparent region is an optimization based on the
327 // buffer contents of the layer, but does not affect the space allocated to
328 // it by policy, and thus children should be allowed to extend into the
329 // parent's transparent region. In fact one of the main uses, is to reduce
330 // buffer allocation size in cases where a child window sits behind a main window
331 // (by marking the hole in the parent window as a transparent region)
332 if (p != nullptr) {
333 Rect bounds = p->computeScreenBounds(false);
334 bounds.intersect(win, &win);
335 }
336
337 if (reduceTransparentRegion) {
338 auto const screenTransparentRegion = t.transform(s.activeTransparentRegion);
339 win = reduce(win, screenTransparentRegion);
340 }
341
342 return win;
343}
344
Dan Stoza80d61162017-12-20 15:57:52 -0800345FloatRect Layer::computeBounds() const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700346 const Layer::State& s(getDrawingState());
Michael Lentine6c925ed2014-09-26 17:55:01 -0700347 return computeBounds(s.activeTransparentRegion);
348}
349
Dan Stoza80d61162017-12-20 15:57:52 -0800350FloatRect Layer::computeBounds(const Region& activeTransparentRegion) const {
Michael Lentine6c925ed2014-09-26 17:55:01 -0700351 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800352 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700353
354 if (!s.crop.isEmpty()) {
355 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800356 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700357
358 Rect bounds = win;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700359 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700360 if (p != nullptr) {
Robert Carrde9ec442017-02-08 17:43:36 -0800361 // Look in computeScreenBounds recursive call for explanation of
362 // why we pass false here.
363 bounds = p->computeScreenBounds(false /* reduceTransparentRegion */);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700364 }
365
366 Transform t = getTransform();
Dan Stoza80d61162017-12-20 15:57:52 -0800367
368 FloatRect floatWin = win.toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700369 if (p != nullptr) {
Dan Stoza80d61162017-12-20 15:57:52 -0800370 floatWin = t.transform(floatWin);
371 floatWin = floatWin.intersect(bounds.toFloatRect());
372 floatWin = t.inverse().transform(floatWin);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700373 }
374
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700375 // subtract the transparent region and snap to the bounds
Dan Stoza80d61162017-12-20 15:57:52 -0800376 return reduce(floatWin, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800377}
378
Robert Carr1f0a16a2016-10-24 16:27:39 -0700379Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& hw) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700380 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800381 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700382 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800383
384 // apply the projection's clipping to the window crop in
385 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700386 // if there are no window scaling involved, this operation will map to full
387 // pixels in the buffer.
388 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
389 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700390
391 Rect activeCrop(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700392 if (!s.crop.isEmpty()) {
Chia-I Wudf7867f2017-07-20 14:24:37 -0700393 activeCrop.intersect(s.crop, &activeCrop);
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700394 }
395
Robert Carr1f0a16a2016-10-24 16:27:39 -0700396 Transform t = getTransform();
397 activeCrop = t.transform(activeCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000398 if (!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
399 activeCrop.clear();
400 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700401 if (!s.finalCrop.isEmpty()) {
David Sodman41fdfc92017-11-06 16:09:56 -0800402 if (!activeCrop.intersect(s.finalCrop, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000403 activeCrop.clear();
404 }
405 }
chaviwb1154d12017-10-31 14:15:36 -0700406
407 const auto& p = mDrawingParent.promote();
408 if (p != nullptr) {
409 auto parentCrop = p->computeInitialCrop(hw);
410 activeCrop.intersect(parentCrop, &activeCrop);
411 }
412
Robert Carr1f0a16a2016-10-24 16:27:39 -0700413 return activeCrop;
414}
415
Dan Stoza5a423ea2017-02-16 14:10:39 -0800416FloatRect Layer::computeCrop(const sp<const DisplayDevice>& hw) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700417 // the content crop is the area of the content that gets scaled to the
418 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800419 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700420
421 // In addition there is a WM-specified crop we pull from our drawing state.
422 const State& s(getDrawingState());
423
424 // Screen space to make reduction to parent crop clearer.
425 Rect activeCrop = computeInitialCrop(hw);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700426 Transform t = getTransform();
427 // Back to layer space to work with the content crop.
428 activeCrop = t.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800429
Michael Lentine28ea2172014-11-19 18:32:37 -0800430 // This needs to be here as transform.transform(Rect) computes the
431 // transformed rect and then takes the bounding box of the result before
432 // returning. This means
433 // transform.inverse().transform(transform.transform(Rect)) != Rect
434 // in which case we need to make sure the final rect is clipped to the
435 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000436 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
437 activeCrop.clear();
438 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800439
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700440 // subtract the transparent region and snap to the bounds
441 activeCrop = reduce(activeCrop, s.activeTransparentRegion);
442
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000443 // Transform the window crop to match the buffer coordinate system,
444 // which means using the inverse of the current transform set on the
445 // SurfaceFlingerConsumer.
446 uint32_t invTransform = mCurrentTransform;
Robert Carrcae605c2017-03-29 12:10:31 -0700447 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000448 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700449 * the code below applies the primary display's inverse transform to the
450 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000451 */
David Sodman41fdfc92017-11-06 16:09:56 -0800452 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000453 // calculate the inverse transform
454 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800455 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800456 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000457 // and apply to the current transform
David Sodman41fdfc92017-11-06 16:09:56 -0800458 invTransform = (Transform(invTransformOrient) * Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800459 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000460
461 int winWidth = s.active.w;
462 int winHeight = s.active.h;
463 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
464 // If the activeCrop has been rotate the ends are rotated but not
465 // the space itself so when transforming ends back we can't rely on
466 // a modification of the axes of rotation. To account for this we
467 // need to reorient the inverse rotation in terms of the current
468 // axes of rotation.
469 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
470 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
471 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800472 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000473 }
474 winWidth = s.active.h;
475 winHeight = s.active.w;
476 }
David Sodman41fdfc92017-11-06 16:09:56 -0800477 const Rect winCrop = activeCrop.transform(invTransform, s.active.w, s.active.h);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000478
479 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800480 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000481 float yScale = crop.getHeight() / float(winHeight);
482
David Sodman41fdfc92017-11-06 16:09:56 -0800483 float insetL = winCrop.left * xScale;
484 float insetT = winCrop.top * yScale;
485 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000486 float insetB = (winHeight - winCrop.bottom) * yScale;
487
David Sodman41fdfc92017-11-06 16:09:56 -0800488 crop.left += insetL;
489 crop.top += insetT;
490 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000491 crop.bottom -= insetB;
492
Mathias Agopian13127d82013-03-05 17:47:11 -0800493 return crop;
494}
495
Robert Carrae060832016-11-28 10:51:00 -0800496void Layer::setGeometry(const sp<const DisplayDevice>& displayDevice, uint32_t z)
Mathias Agopiana350ff92010-08-10 17:14:02 -0700497{
Dan Stoza9e56aa02015-11-02 13:00:03 -0800498 const auto hwcId = displayDevice->getHwcDisplayId();
David Sodman6f65f3e2017-11-03 14:28:09 -0700499 auto& hwcInfo = getBE().mHwcLayers[hwcId];
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700500
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700501 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800502 hwcInfo.forceClientComposition = false;
503
504 if (isSecure() && !displayDevice->isSecure()) {
505 hwcInfo.forceClientComposition = true;
506 }
507
508 auto& hwcLayer = hwcInfo.layer;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700509
Mathias Agopian13127d82013-03-05 17:47:11 -0800510 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700511 const State& s(getDrawingState());
David Revemanecf0fa52017-03-03 11:32:44 -0500512 auto blendMode = HWC2::BlendMode::None;
Robert Carr6452f122017-03-21 10:41:29 -0700513 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800514 blendMode =
515 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800516 }
David Revemanecf0fa52017-03-03 11:32:44 -0500517 auto error = hwcLayer->setBlendMode(blendMode);
David Sodman41fdfc92017-11-06 16:09:56 -0800518 ALOGE_IF(error != HWC2::Error::None,
519 "[%s] Failed to set blend mode %s:"
520 " %s (%d)",
521 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
522 static_cast<int32_t>(error));
Mathias Agopian13127d82013-03-05 17:47:11 -0800523
524 // apply the layer's transform, followed by the display's global transform
525 // here we're guaranteed that the layer's transform preserves rects
Michael Lentine6c925ed2014-09-26 17:55:01 -0700526 Region activeTransparentRegion(s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700527 Transform t = getTransform();
Robert Carrb5d3d262016-03-25 15:08:13 -0700528 if (!s.crop.isEmpty()) {
529 Rect activeCrop(s.crop);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700530 activeCrop = t.transform(activeCrop);
David Sodman41fdfc92017-11-06 16:09:56 -0800531 if (!activeCrop.intersect(displayDevice->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000532 activeCrop.clear();
533 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700534 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800535 // This needs to be here as transform.transform(Rect) computes the
536 // transformed rect and then takes the bounding box of the result before
537 // returning. This means
538 // transform.inverse().transform(transform.transform(Rect)) != Rect
539 // in which case we need to make sure the final rect is clipped to the
540 // display bounds.
David Sodman41fdfc92017-11-06 16:09:56 -0800541 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000542 activeCrop.clear();
543 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700544 // mark regions outside the crop as transparent
545 activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top));
David Sodman41fdfc92017-11-06 16:09:56 -0800546 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom, s.active.w, s.active.h));
547 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
548 activeTransparentRegion.orSelf(
549 Rect(activeCrop.right, activeCrop.top, s.active.w, activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700550 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700551
Dan Stoza80d61162017-12-20 15:57:52 -0800552 // computeBounds returns a FloatRect to provide more accuracy during the
553 // transformation. We then round upon constructing 'frame'.
554 Rect frame{t.transform(computeBounds(activeTransparentRegion))};
Robert Carrb5d3d262016-03-25 15:08:13 -0700555 if (!s.finalCrop.isEmpty()) {
David Sodman41fdfc92017-11-06 16:09:56 -0800556 if (!frame.intersect(s.finalCrop, &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000557 frame.clear();
558 }
559 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000560 if (!frame.intersect(displayDevice->getViewport(), &frame)) {
561 frame.clear();
562 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800563 const Transform& tr(displayDevice->getTransform());
564 Rect transformedFrame = tr.transform(frame);
David Revemanecf0fa52017-03-03 11:32:44 -0500565 error = hwcLayer->setDisplayFrame(transformedFrame);
Dan Stozae22aec72016-08-01 13:20:59 -0700566 if (error != HWC2::Error::None) {
David Sodman41fdfc92017-11-06 16:09:56 -0800567 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
568 transformedFrame.left, transformedFrame.top, transformedFrame.right,
569 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stozae22aec72016-08-01 13:20:59 -0700570 } else {
571 hwcInfo.displayFrame = transformedFrame;
572 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800573
Dan Stoza5a423ea2017-02-16 14:10:39 -0800574 FloatRect sourceCrop = computeCrop(displayDevice);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800575 error = hwcLayer->setSourceCrop(sourceCrop);
Dan Stozae22aec72016-08-01 13:20:59 -0700576 if (error != HWC2::Error::None) {
577 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
David Sodman41fdfc92017-11-06 16:09:56 -0800578 "%s (%d)",
579 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
580 to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stozae22aec72016-08-01 13:20:59 -0700581 } else {
582 hwcInfo.sourceCrop = sourceCrop;
583 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800584
chaviw13fdc492017-06-27 12:40:18 -0700585 float alpha = static_cast<float>(getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -0700586 error = hwcLayer->setPlaneAlpha(alpha);
David Sodman41fdfc92017-11-06 16:09:56 -0800587 ALOGE_IF(error != HWC2::Error::None,
588 "[%s] Failed to set plane alpha %.3f: "
589 "%s (%d)",
590 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800591
Robert Carrae060832016-11-28 10:51:00 -0800592 error = hwcLayer->setZOrder(z);
David Sodman41fdfc92017-11-06 16:09:56 -0800593 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
594 to_string(error).c_str(), static_cast<int32_t>(error));
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500595
Albert Chaulk2a589632017-05-04 16:59:44 -0400596 int type = s.type;
597 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700598 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400599 if (parent.get()) {
600 auto& parentState = parent->getDrawingState();
rongliuccd34842018-03-14 12:26:23 -0700601 if (parentState.type >= 0 || parentState.appId >= 0) {
602 type = parentState.type;
603 appId = parentState.appId;
604 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400605 }
606
607 error = hwcLayer->setInfo(type, appId);
David Sodman41fdfc92017-11-06 16:09:56 -0800608 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
609 static_cast<int32_t>(error));
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800610
Mathias Agopian29a367b2011-07-12 14:51:45 -0700611 /*
612 * Transformations are applied in this order:
613 * 1) buffer orientation/flip/mirror
614 * 2) state transformation (window manager)
615 * 3) layer orientation (screen orientation)
616 * (NOTE: the matrices are multiplied in reverse order)
617 */
618
619 const Transform bufferOrientation(mCurrentTransform);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700620 Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700621
Robert Carrcae605c2017-03-29 12:10:31 -0700622 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700623 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700624 * the code below applies the primary display's inverse transform to the
625 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700626 */
David Sodman41fdfc92017-11-06 16:09:56 -0800627 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700628 // calculate the inverse transform
629 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800630 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700631 }
Robert Carrcae605c2017-03-29 12:10:31 -0700632
633 /*
634 * Here we cancel out the orientation component of the WM transform.
635 * The scaling and translate components are already included in our bounds
636 * computation so it's enough to just omit it in the composition.
637 * See comment in onDraw with ref to b/36727915 for why.
638 */
639 transform = Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700640 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700641
642 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800643 const uint32_t orientation = transform.getOrientation();
Jorim Jaggif3bd94a2018-03-27 15:38:03 +0200644 if (orientation & Transform::ROT_INVALID) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800645 // we can only handle simple transformation
646 hwcInfo.forceClientComposition = true;
647 } else {
648 auto transform = static_cast<HWC2::Transform>(orientation);
Yiwei Zhang068e31b2018-02-21 13:02:45 -0800649 hwcInfo.transform = transform;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800650 auto error = hwcLayer->setTransform(transform);
David Sodman41fdfc92017-11-06 16:09:56 -0800651 ALOGE_IF(error != HWC2::Error::None,
652 "[%s] Failed to set transform %s: "
653 "%s (%d)",
654 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
655 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800656 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700657}
658
Dan Stoza9e56aa02015-11-02 13:00:03 -0800659void Layer::forceClientComposition(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700660 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800661 ALOGE("forceClientComposition: no HWC layer found (%d)", hwcId);
662 return;
663 }
664
David Sodman6f65f3e2017-11-03 14:28:09 -0700665 getBE().mHwcLayers[hwcId].forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800666}
Dan Stozaee44edd2015-03-23 15:50:23 -0700667
chaviwc9232ed2017-11-14 15:31:15 -0800668bool Layer::getForceClientComposition(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700669 if (getBE().mHwcLayers.count(hwcId) == 0) {
chaviwc9232ed2017-11-14 15:31:15 -0800670 ALOGE("getForceClientComposition: no HWC layer found (%d)", hwcId);
671 return false;
672 }
673
David Sodman6f65f3e2017-11-03 14:28:09 -0700674 return getBE().mHwcLayers[hwcId].forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800675}
676
Dan Stoza9e56aa02015-11-02 13:00:03 -0800677void Layer::updateCursorPosition(const sp<const DisplayDevice>& displayDevice) {
678 auto hwcId = displayDevice->getHwcDisplayId();
David Sodman6f65f3e2017-11-03 14:28:09 -0700679 if (getBE().mHwcLayers.count(hwcId) == 0 ||
David Sodman9eeae692017-11-02 10:53:32 -0700680 getCompositionType(hwcId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800681 return;
682 }
683
684 // This gives us only the "orientation" component of the transform
685 const State& s(getCurrentState());
686
687 // Apply the layer's transform, followed by the display's global transform
688 // Here we're guaranteed that the layer's transform preserves rects
689 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700690 if (!s.crop.isEmpty()) {
691 win.intersect(s.crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800692 }
693 // Subtract the transparent region and snap to the bounds
694 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700695 Rect frame(getTransform().transform(bounds));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800696 frame.intersect(displayDevice->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700697 if (!s.finalCrop.isEmpty()) {
698 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000699 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800700 auto& displayTransform(displayDevice->getTransform());
701 auto position = displayTransform.transform(frame);
702
David Sodman6f65f3e2017-11-03 14:28:09 -0700703 auto error = getBE().mHwcLayers[hwcId].layer->setCursorPosition(position.left,
David Sodman9eeae692017-11-02 10:53:32 -0700704 position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800705 ALOGE_IF(error != HWC2::Error::None,
706 "[%s] Failed to set cursor position "
707 "to (%d, %d): %s (%d)",
708 mName.string(), position.left, position.top, to_string(error).c_str(),
709 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800710}
Riley Andrews03414a12014-07-01 14:22:59 -0700711
Mathias Agopian13127d82013-03-05 17:47:11 -0800712// ---------------------------------------------------------------------------
713// drawing...
714// ---------------------------------------------------------------------------
715
chaviwa76b2712017-09-20 12:02:26 -0700716void Layer::draw(const RenderArea& renderArea, const Region& clip) const {
717 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800718}
719
chaviwa76b2712017-09-20 12:02:26 -0700720void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) const {
721 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800722}
723
chaviwa76b2712017-09-20 12:02:26 -0700724void Layer::draw(const RenderArea& renderArea) const {
725 onDraw(renderArea, Region(renderArea.getBounds()), false);
Dan Stozac7014012014-02-14 15:03:43 -0800726}
727
David Sodman41fdfc92017-11-06 16:09:56 -0800728void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
729 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800730 auto& engine(mFlinger->getRenderEngine());
David Sodman9eeae692017-11-02 10:53:32 -0700731 computeGeometry(renderArea, getBE().mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700732 engine.setupFillWithColor(red, green, blue, alpha);
David Sodman9eeae692017-11-02 10:53:32 -0700733 engine.drawMesh(getBE().mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800734}
735
chaviwa76b2712017-09-20 12:02:26 -0700736void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
David Sodman41fdfc92017-11-06 16:09:56 -0800737 clearWithOpenGL(renderArea, 0, 0, 0, 0);
Mathias Agopian13127d82013-03-05 17:47:11 -0800738}
739
David Sodman41fdfc92017-11-06 16:09:56 -0800740void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type, bool callIntoHwc) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700741 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800742 ALOGE("setCompositionType called without a valid HWC layer");
743 return;
744 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700745 auto& hwcInfo = getBE().mHwcLayers[hwcId];
Dan Stoza9e56aa02015-11-02 13:00:03 -0800746 auto& hwcLayer = hwcInfo.layer;
David Sodman41fdfc92017-11-06 16:09:56 -0800747 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(), to_string(type).c_str(),
748 static_cast<int>(callIntoHwc));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800749 if (hwcInfo.compositionType != type) {
750 ALOGV(" actually setting");
751 hwcInfo.compositionType = type;
752 if (callIntoHwc) {
753 auto error = hwcLayer->setCompositionType(type);
David Sodman41fdfc92017-11-06 16:09:56 -0800754 ALOGE_IF(error != HWC2::Error::None,
755 "[%s] Failed to set "
756 "composition type %s: %s (%d)",
757 mName.string(), to_string(type).c_str(), to_string(error).c_str(),
758 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800759 }
760 }
761}
762
763HWC2::Composition Layer::getCompositionType(int32_t hwcId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -0700764 if (hwcId == DisplayDevice::DISPLAY_ID_INVALID) {
765 // If we're querying the composition type for a display that does not
766 // have a HWC counterpart, then it will always be Client
767 return HWC2::Composition::Client;
768 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700769 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700770 ALOGE("getCompositionType called with an invalid HWC layer");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800771 return HWC2::Composition::Invalid;
772 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700773 return getBE().mHwcLayers.at(hwcId).compositionType;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800774}
775
776void Layer::setClearClientTarget(int32_t hwcId, bool clear) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700777 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800778 ALOGE("setClearClientTarget called without a valid HWC layer");
779 return;
780 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700781 getBE().mHwcLayers[hwcId].clearClientTarget = clear;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800782}
783
784bool Layer::getClearClientTarget(int32_t hwcId) const {
David Sodman6f65f3e2017-11-03 14:28:09 -0700785 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800786 ALOGE("getClearClientTarget called without a valid HWC layer");
787 return false;
788 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700789 return getBE().mHwcLayers.at(hwcId).clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800790}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800791
Dan Stozacac35382016-01-27 12:21:06 -0800792bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
793 if (point->getFrameNumber() <= mCurrentFrameNumber) {
794 // Don't bother with a SyncPoint, since we've already latched the
795 // relevant frame
796 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700797 }
798
Dan Stozacac35382016-01-27 12:21:06 -0800799 Mutex::Autolock lock(mLocalSyncPointMutex);
800 mLocalSyncPoints.push_back(point);
801 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700802}
803
Mathias Agopian13127d82013-03-05 17:47:11 -0800804void Layer::setFiltering(bool filtering) {
805 mFiltering = filtering;
806}
807
808bool Layer::getFiltering() const {
809 return mFiltering;
810}
811
Mathias Agopian13127d82013-03-05 17:47:11 -0800812// ----------------------------------------------------------------------------
813// local state
814// ----------------------------------------------------------------------------
815
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000816static void boundPoint(vec2* point, const Rect& crop) {
817 if (point->x < crop.left) {
818 point->x = crop.left;
819 }
820 if (point->x > crop.right) {
821 point->x = crop.right;
822 }
823 if (point->y < crop.top) {
824 point->y = crop.top;
825 }
826 if (point->y > crop.bottom) {
827 point->y = crop.bottom;
828 }
829}
830
chaviwa76b2712017-09-20 12:02:26 -0700831void Layer::computeGeometry(const RenderArea& renderArea, Mesh& mesh,
832 bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700833 const Layer::State& s(getDrawingState());
chaviwa76b2712017-09-20 12:02:26 -0700834 const Transform renderAreaTransform(renderArea.getTransform());
835 const uint32_t height = renderArea.getHeight();
Dan Stoza80d61162017-12-20 15:57:52 -0800836 FloatRect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700837
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000838 vec2 lt = vec2(win.left, win.top);
839 vec2 lb = vec2(win.left, win.bottom);
840 vec2 rb = vec2(win.right, win.bottom);
841 vec2 rt = vec2(win.right, win.top);
842
Robert Carr1f0a16a2016-10-24 16:27:39 -0700843 Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000844 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700845 lt = layerTransform.transform(lt);
846 lb = layerTransform.transform(lb);
847 rb = layerTransform.transform(rb);
848 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000849 }
850
Robert Carrb5d3d262016-03-25 15:08:13 -0700851 if (!s.finalCrop.isEmpty()) {
852 boundPoint(&lt, s.finalCrop);
853 boundPoint(&lb, s.finalCrop);
854 boundPoint(&rb, s.finalCrop);
855 boundPoint(&rt, s.finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000856 }
857
Mathias Agopianff2ed702013-09-01 21:36:12 -0700858 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700859 position[0] = renderAreaTransform.transform(lt);
860 position[1] = renderAreaTransform.transform(lb);
861 position[2] = renderAreaTransform.transform(rb);
862 position[3] = renderAreaTransform.transform(rt);
David Sodman41fdfc92017-11-06 16:09:56 -0800863 for (size_t i = 0; i < 4; i++) {
chaviwa76b2712017-09-20 12:02:26 -0700864 position[i].y = height - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -0800865 }
866}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800867
David Sodman41fdfc92017-11-06 16:09:56 -0800868bool Layer::isSecure() const {
Dan Stoza23116082015-06-18 14:58:39 -0700869 const Layer::State& s(mDrawingState);
870 return (s.flags & layer_state_t::eLayerSecure);
871}
872
Mathias Agopian13127d82013-03-05 17:47:11 -0800873void Layer::setVisibleRegion(const Region& visibleRegion) {
874 // always called from main thread
875 this->visibleRegion = visibleRegion;
876}
877
878void Layer::setCoveredRegion(const Region& coveredRegion) {
879 // always called from main thread
880 this->coveredRegion = coveredRegion;
881}
882
David Sodman41fdfc92017-11-06 16:09:56 -0800883void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800884 // always called from main thread
885 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
886}
887
Robert Carre5f4f692018-01-12 13:12:28 -0800888void Layer::clearVisibilityRegions() {
889 visibleRegion.clear();
890 visibleNonTransparentRegion.clear();
891 coveredRegion.clear();
892}
893
Mathias Agopian13127d82013-03-05 17:47:11 -0800894// ----------------------------------------------------------------------------
895// transaction
896// ----------------------------------------------------------------------------
897
Dan Stoza7dde5992015-05-22 09:51:44 -0700898void Layer::pushPendingState() {
899 if (!mCurrentState.modified) {
900 return;
901 }
902
Dan Stoza7dde5992015-05-22 09:51:44 -0700903 // If this transaction is waiting on the receipt of a frame, generate a sync
904 // point and send it to the remote layer.
Robert Carr0d480722017-01-10 16:42:54 -0800905 if (mCurrentState.barrierLayer != nullptr) {
906 sp<Layer> barrierLayer = mCurrentState.barrierLayer.promote();
907 if (barrierLayer == nullptr) {
908 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700909 // If we can't promote the layer we are intended to wait on,
910 // then it is expired or otherwise invalid. Allow this transaction
911 // to be applied as per normal (no synchronization).
Robert Carr0d480722017-01-10 16:42:54 -0800912 mCurrentState.barrierLayer = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800913 } else {
David Sodman41fdfc92017-11-06 16:09:56 -0800914 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber);
Robert Carr0d480722017-01-10 16:42:54 -0800915 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800916 mRemoteSyncPoints.push_back(std::move(syncPoint));
917 } else {
918 // We already missed the frame we're supposed to synchronize
919 // on, so go ahead and apply the state update
Robert Carr0d480722017-01-10 16:42:54 -0800920 mCurrentState.barrierLayer = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800921 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700922 }
923
Dan Stoza7dde5992015-05-22 09:51:44 -0700924 // Wake us up to check if the frame has been received
925 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700926 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700927 }
928 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700929 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700930}
931
Pablo Ceballos05289c22016-04-14 15:49:55 -0700932void Layer::popPendingState(State* stateToCommit) {
933 auto oldFlags = stateToCommit->flags;
934 *stateToCommit = mPendingStates[0];
David Sodman41fdfc92017-11-06 16:09:56 -0800935 stateToCommit->flags =
936 (oldFlags & ~stateToCommit->mask) | (stateToCommit->flags & stateToCommit->mask);
Dan Stoza7dde5992015-05-22 09:51:44 -0700937
938 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700939 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700940}
941
Pablo Ceballos05289c22016-04-14 15:49:55 -0700942bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700943 bool stateUpdateAvailable = false;
944 while (!mPendingStates.empty()) {
Robert Carr0d480722017-01-10 16:42:54 -0800945 if (mPendingStates[0].barrierLayer != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700946 if (mRemoteSyncPoints.empty()) {
947 // If we don't have a sync point for this, apply it anyway. It
948 // will be visually wrong, but it should keep us from getting
949 // into too much trouble.
950 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700951 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700952 stateUpdateAvailable = true;
953 continue;
954 }
955
David Sodman41fdfc92017-11-06 16:09:56 -0800956 if (mRemoteSyncPoints.front()->getFrameNumber() != mPendingStates[0].frameNumber) {
957 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800958
959 // Signal our end of the sync point and then dispose of it
960 mRemoteSyncPoints.front()->setTransactionApplied();
961 mRemoteSyncPoints.pop_front();
962 continue;
963 }
964
Dan Stoza7dde5992015-05-22 09:51:44 -0700965 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
966 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700967 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700968 stateUpdateAvailable = true;
969
970 // Signal our end of the sync point and then dispose of it
971 mRemoteSyncPoints.front()->setTransactionApplied();
972 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800973 } else {
974 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700975 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700976 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700977 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700978 stateUpdateAvailable = true;
979 }
980 }
981
982 // If we still have pending updates, wake SurfaceFlinger back up and point
983 // it at this layer so we can process them
984 if (!mPendingStates.empty()) {
985 setTransactionFlags(eTransactionNeeded);
986 mFlinger->setTransactionFlags(eTraversalNeeded);
987 }
988
989 mCurrentState.modified = false;
990 return stateUpdateAvailable;
991}
992
Mathias Agopian13127d82013-03-05 17:47:11 -0800993uint32_t Layer::doTransaction(uint32_t flags) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800994 ATRACE_CALL();
995
Dan Stoza7dde5992015-05-22 09:51:44 -0700996 pushPendingState();
Pablo Ceballos05289c22016-04-14 15:49:55 -0700997 Layer::State c = getCurrentState();
998 if (!applyPendingStates(&c)) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700999 return 0;
1000 }
1001
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001002 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001003
David Sodman41fdfc92017-11-06 16:09:56 -08001004 const bool sizeChanged = (c.requested.w != s.requested.w) || (c.requested.h != s.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -07001005
David Sodmaneb085e02017-10-05 18:49:04 -07001006 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001007 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +00001008 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -08001009 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
1010 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1011 " requested={ wh={%4u,%4u} }}\n"
1012 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1013 " requested={ wh={%4u,%4u} }}\n",
David Sodman9eeae692017-11-02 10:53:32 -07001014 this, getName().string(), mCurrentTransform,
1015 getEffectiveScalingMode(), c.active.w, c.active.h, c.crop.left, c.crop.top,
1016 c.crop.right, c.crop.bottom, c.crop.getWidth(), c.crop.getHeight(), c.requested.w,
1017 c.requested.h, s.active.w, s.active.h, s.crop.left, s.crop.top, s.crop.right,
1018 s.crop.bottom, s.crop.getWidth(), s.crop.getHeight(), s.requested.w,
1019 s.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001020
Jamie Gennis2a0d5b62011-09-26 16:54:44 -07001021 // record the new size, form this point on, when the client request
1022 // a buffer, it'll get the new size.
David Sodmaneb085e02017-10-05 18:49:04 -07001023 setDefaultBufferSize(c.requested.w, c.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001024 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001025
Robert Carre392b552017-09-19 12:16:05 -07001026 // Don't let Layer::doTransaction update the drawing state
1027 // if we have a pending resize, unless we are in fixed-size mode.
1028 // the drawing state will be updated only once we receive a buffer
1029 // with the correct size.
1030 //
1031 // In particular, we want to make sure the clip (which is part
1032 // of the geometry state) is latched together with the size but is
1033 // latched immediately when no resizing is involved.
1034 //
1035 // If a sideband stream is attached, however, we want to skip this
1036 // optimization so that transactions aren't missed when a buffer
1037 // never arrives
1038 //
1039 // In the case that we don't have a buffer we ignore other factors
1040 // and avoid entering the resizePending state. At a high level the
1041 // resizePending state is to avoid applying the state of the new buffer
1042 // to the old buffer. However in the state where we don't have an old buffer
1043 // there is no such concern but we may still be being used as a parent layer.
David Sodman41fdfc92017-11-06 16:09:56 -08001044 const bool resizePending = ((c.requested.w != c.active.w) || (c.requested.h != c.active.h)) &&
David Sodman0cc69182017-11-17 12:12:07 -08001045 (getBE().compositionInfo.mBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001046 if (!isFixedSize()) {
David Sodman0cc69182017-11-17 12:12:07 -08001047 if (resizePending && getBE().compositionInfo.hwc.sidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001048 flags |= eDontUpdateGeometryState;
1049 }
1050 }
1051
Robert Carr7bf247e2017-05-18 14:02:49 -07001052 // Here we apply various requested geometry states, depending on our
1053 // latching configuration. See Layer.h for a detailed discussion of
1054 // how geometry latching is controlled.
1055 if (!(flags & eDontUpdateGeometryState)) {
Pablo Ceballos7d052572016-06-02 17:46:05 -07001056 Layer::State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001057
1058 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1059 // mode, which causes attributes which normally latch regardless of scaling mode,
1060 // to be delayed. We copy the requested state to the active state making sure
1061 // to respect these rules (again see Layer.h for a detailed discussion).
1062 //
1063 // There is an awkward asymmetry in the handling of the crop states in the position
1064 // states, as can be seen below. Largely this arises from position and transform
1065 // being stored in the same data structure while having different latching rules.
1066 // b/38182305
1067 //
1068 // Careful that "c" and editCurrentState may not begin as equivalent due to
1069 // applyPendingStates in the presence of deferred transactions.
1070 if (mFreezeGeometryUpdates) {
Robert Carr82364e32016-05-15 11:27:47 -07001071 float tx = c.active.transform.tx();
1072 float ty = c.active.transform.ty();
1073 c.active = c.requested;
1074 c.active.transform.set(tx, ty);
1075 editCurrentState.active = c.active;
1076 } else {
1077 editCurrentState.active = editCurrentState.requested;
1078 c.active = c.requested;
1079 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001080 }
1081
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001082 if (s.active != c.active) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001083 // invalidate and recompute the visible regions if needed
1084 flags |= Layer::eVisibleRegion;
1085 }
1086
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001087 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001088 // invalidate and recompute the visible regions if needed
1089 flags |= eVisibleRegion;
1090 this->contentDirty = true;
1091
1092 // we may use linear filtering, if the matrix scales us
Robert Carr3dcabfa2016-03-01 18:36:58 -08001093 const uint8_t type = c.active.transform.getType();
David Sodman41fdfc92017-11-06 16:09:56 -08001094 mNeedsFiltering = (!c.active.transform.preserveRects() || (type >= Transform::SCALE));
Mathias Agopian13127d82013-03-05 17:47:11 -08001095 }
1096
Dan Stozac8145172016-04-28 16:29:06 -07001097 // If the layer is hidden, signal and clear out all local sync points so
1098 // that transactions for layers depending on this layer's frames becoming
1099 // visible are not blocked
1100 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001101 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001102 }
1103
Mathias Agopian13127d82013-03-05 17:47:11 -08001104 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001105 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001106 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001107}
1108
Pablo Ceballos05289c22016-04-14 15:49:55 -07001109void Layer::commitTransaction(const State& stateToCommit) {
1110 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001111}
1112
Mathias Agopian13127d82013-03-05 17:47:11 -08001113uint32_t Layer::getTransactionFlags(uint32_t flags) {
1114 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1115}
1116
1117uint32_t Layer::setTransactionFlags(uint32_t flags) {
1118 return android_atomic_or(flags, &mTransactionFlags);
1119}
1120
Robert Carr82364e32016-05-15 11:27:47 -07001121bool Layer::setPosition(float x, float y, bool immediate) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001122 if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001123 return false;
1124 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001125
1126 // We update the requested and active position simultaneously because
1127 // we want to apply the position portion of the transform matrix immediately,
1128 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Robert Carr3dcabfa2016-03-01 18:36:58 -08001129 mCurrentState.requested.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001130 if (immediate && !mFreezeGeometryUpdates) {
1131 // Here we directly update the active state
1132 // unlike other setters, because we store it within
1133 // the transform, but use different latching rules.
1134 // b/38182305
Robert Carr82364e32016-05-15 11:27:47 -07001135 mCurrentState.active.transform.set(x, y);
1136 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001137 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001138
Dan Stoza7dde5992015-05-22 09:51:44 -07001139 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001140 setTransactionFlags(eTransactionNeeded);
1141 return true;
1142}
Robert Carr82364e32016-05-15 11:27:47 -07001143
Robert Carr1f0a16a2016-10-24 16:27:39 -07001144bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1145 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1146 if (idx < 0) {
1147 return false;
1148 }
1149 if (childLayer->setLayer(z)) {
1150 mCurrentChildren.removeAt(idx);
1151 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001152 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001153 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001154 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001155}
1156
Robert Carr503c7042017-09-27 15:06:08 -07001157bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1158 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1159 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1160 if (idx < 0) {
1161 return false;
1162 }
1163 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1164 mCurrentChildren.removeAt(idx);
1165 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001166 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001167 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001168 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001169}
1170
Robert Carrae060832016-11-28 10:51:00 -08001171bool Layer::setLayer(int32_t z) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001172 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001173 mCurrentState.sequence++;
1174 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001175 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001176
1177 // Discard all relative layering.
1178 if (mCurrentState.zOrderRelativeOf != nullptr) {
1179 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1180 if (strongRelative != nullptr) {
1181 strongRelative->removeZOrderRelative(this);
1182 }
1183 mCurrentState.zOrderRelativeOf = nullptr;
1184 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001185 setTransactionFlags(eTransactionNeeded);
1186 return true;
1187}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001188
Robert Carrdb66e622017-04-10 16:55:57 -07001189void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1190 mCurrentState.zOrderRelatives.remove(relative);
1191 mCurrentState.sequence++;
1192 mCurrentState.modified = true;
1193 setTransactionFlags(eTransactionNeeded);
1194}
1195
1196void Layer::addZOrderRelative(const wp<Layer>& relative) {
1197 mCurrentState.zOrderRelatives.add(relative);
1198 mCurrentState.modified = true;
1199 mCurrentState.sequence++;
1200 setTransactionFlags(eTransactionNeeded);
1201}
1202
Robert Carr503d2bd2017-12-04 15:49:47 -08001203bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001204 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1205 if (handle == nullptr) {
1206 return false;
1207 }
1208 sp<Layer> relative = handle->owner.promote();
1209 if (relative == nullptr) {
1210 return false;
1211 }
1212
Robert Carr503d2bd2017-12-04 15:49:47 -08001213 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1214 mCurrentState.zOrderRelativeOf == relative) {
1215 return false;
1216 }
1217
Robert Carrdb66e622017-04-10 16:55:57 -07001218 mCurrentState.sequence++;
1219 mCurrentState.modified = true;
Robert Carr503d2bd2017-12-04 15:49:47 -08001220 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001221
chaviw9ab4bd12017-11-03 13:11:00 -07001222 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
1223 if (oldZOrderRelativeOf != nullptr) {
1224 oldZOrderRelativeOf->removeZOrderRelative(this);
1225 }
Robert Carrdb66e622017-04-10 16:55:57 -07001226 mCurrentState.zOrderRelativeOf = relative;
1227 relative->addZOrderRelative(this);
1228
1229 setTransactionFlags(eTransactionNeeded);
1230
1231 return true;
1232}
1233
Mathias Agopian13127d82013-03-05 17:47:11 -08001234bool Layer::setSize(uint32_t w, uint32_t h) {
David Sodman41fdfc92017-11-06 16:09:56 -08001235 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001236 mCurrentState.requested.w = w;
1237 mCurrentState.requested.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001238 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001239 setTransactionFlags(eTransactionNeeded);
1240 return true;
1241}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001242bool Layer::setAlpha(float alpha) {
David Sodman41fdfc92017-11-06 16:09:56 -08001243 if (mCurrentState.color.a == alpha) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001244 mCurrentState.sequence++;
chaviw13fdc492017-06-27 12:40:18 -07001245 mCurrentState.color.a = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001246 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001247 setTransactionFlags(eTransactionNeeded);
1248 return true;
1249}
chaviw13fdc492017-06-27 12:40:18 -07001250
1251bool Layer::setColor(const half3& color) {
David Sodman41fdfc92017-11-06 16:09:56 -08001252 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g &&
1253 color.b == mCurrentState.color.b)
chaviw13fdc492017-06-27 12:40:18 -07001254 return false;
1255
1256 mCurrentState.sequence++;
1257 mCurrentState.color.r = color.r;
1258 mCurrentState.color.g = color.g;
1259 mCurrentState.color.b = color.b;
1260 mCurrentState.modified = true;
1261 setTransactionFlags(eTransactionNeeded);
1262 return true;
1263}
1264
Mathias Agopian13127d82013-03-05 17:47:11 -08001265bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
1266 mCurrentState.sequence++;
David Sodman41fdfc92017-11-06 16:09:56 -08001267 mCurrentState.requested.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001268 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001269 setTransactionFlags(eTransactionNeeded);
1270 return true;
1271}
1272bool Layer::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001273 mCurrentState.requestedTransparentRegion = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001274 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001275 setTransactionFlags(eTransactionNeeded);
1276 return true;
1277}
1278bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1279 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
David Sodman41fdfc92017-11-06 16:09:56 -08001280 if (mCurrentState.flags == newFlags) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001281 mCurrentState.sequence++;
1282 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001283 mCurrentState.mask = mask;
1284 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001285 setTransactionFlags(eTransactionNeeded);
1286 return true;
1287}
Robert Carr99e27f02016-06-16 15:18:02 -07001288
1289bool Layer::setCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001290 if (mCurrentState.requestedCrop == crop) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001291 mCurrentState.sequence++;
Robert Carr99e27f02016-06-16 15:18:02 -07001292 mCurrentState.requestedCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001293 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr99e27f02016-06-16 15:18:02 -07001294 mCurrentState.crop = crop;
1295 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001296 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1297
Dan Stoza7dde5992015-05-22 09:51:44 -07001298 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001299 setTransactionFlags(eTransactionNeeded);
1300 return true;
1301}
Robert Carr8d5227b2017-03-16 15:41:03 -07001302
1303bool Layer::setFinalCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001304 if (mCurrentState.requestedFinalCrop == crop) return false;
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001305 mCurrentState.sequence++;
Robert Carr8d5227b2017-03-16 15:41:03 -07001306 mCurrentState.requestedFinalCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001307 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr8d5227b2017-03-16 15:41:03 -07001308 mCurrentState.finalCrop = crop;
1309 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001310 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1311
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001312 mCurrentState.modified = true;
1313 setTransactionFlags(eTransactionNeeded);
1314 return true;
1315}
Mathias Agopian13127d82013-03-05 17:47:11 -08001316
Robert Carrc3574f72016-03-24 12:19:32 -07001317bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001318 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001319 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001320 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001321 return true;
1322}
1323
rongliuccd34842018-03-14 12:26:23 -07001324void Layer::setInfo(int32_t type, int32_t appId) {
David Sodman41fdfc92017-11-06 16:09:56 -08001325 mCurrentState.appId = appId;
1326 mCurrentState.type = type;
1327 mCurrentState.modified = true;
1328 setTransactionFlags(eTransactionNeeded);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001329}
1330
Mathias Agopian13127d82013-03-05 17:47:11 -08001331bool Layer::setLayerStack(uint32_t layerStack) {
David Sodman41fdfc92017-11-06 16:09:56 -08001332 if (mCurrentState.layerStack == layerStack) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001333 mCurrentState.sequence++;
1334 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001335 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001336 setTransactionFlags(eTransactionNeeded);
1337 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001338}
1339
Peiyong Lin34beb7a2018-03-28 11:57:12 -07001340bool Layer::setDataSpace(ui::Dataspace dataSpace) {
David Sodman41fdfc92017-11-06 16:09:56 -08001341 if (mCurrentState.dataSpace == dataSpace) return false;
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -07001342 mCurrentState.sequence++;
1343 mCurrentState.dataSpace = dataSpace;
1344 mCurrentState.modified = true;
1345 setTransactionFlags(eTransactionNeeded);
1346 return true;
1347}
1348
Peiyong Lin34beb7a2018-03-28 11:57:12 -07001349ui::Dataspace Layer::getDataSpace() const {
Courtney Goeltzenleuchter532b2632017-05-05 16:34:38 -06001350 return mCurrentState.dataSpace;
1351}
1352
Robert Carr1f0a16a2016-10-24 16:27:39 -07001353uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001354 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001355 if (p == nullptr) {
1356 return getDrawingState().layerStack;
1357 }
1358 return p->getLayerStack();
1359}
1360
David Sodman41fdfc92017-11-06 16:09:56 -08001361void Layer::deferTransactionUntil(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001362 mCurrentState.barrierLayer = barrierLayer;
Dan Stoza7dde5992015-05-22 09:51:44 -07001363 mCurrentState.frameNumber = frameNumber;
1364 // We don't set eTransactionNeeded, because just receiving a deferral
1365 // request without any other state updates shouldn't actually induce a delay
1366 mCurrentState.modified = true;
1367 pushPendingState();
Robert Carr0d480722017-01-10 16:42:54 -08001368 mCurrentState.barrierLayer = nullptr;
Dan Stoza792e5292016-02-11 11:43:58 -08001369 mCurrentState.frameNumber = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001370 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001371}
1372
David Sodman41fdfc92017-11-06 16:09:56 -08001373void Layer::deferTransactionUntil(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001374 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
1375 deferTransactionUntil(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001376}
1377
Dan Stozaee44edd2015-03-23 15:50:23 -07001378
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001379// ----------------------------------------------------------------------------
1380// pageflip handling...
1381// ----------------------------------------------------------------------------
1382
Robert Carr1f0a16a2016-10-24 16:27:39 -07001383bool Layer::isHiddenByPolicy() const {
1384 const Layer::State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001385 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001386 if (parent != nullptr && parent->isHiddenByPolicy()) {
1387 return true;
1388 }
1389 return s.flags & layer_state_t::eLayerHidden;
1390}
1391
David Sodman41fdfc92017-11-06 16:09:56 -08001392uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001393 // TODO: should we do something special if mSecure is set?
1394 if (mProtectedByApp) {
1395 // need a hardware-protected path to external video sink
1396 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001397 }
Riley Andrews03414a12014-07-01 14:22:59 -07001398 if (mPotentialCursor) {
1399 usage |= GraphicBuffer::USAGE_CURSOR;
1400 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001401 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001402 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001403}
1404
Mathias Agopian84300952012-11-21 16:02:13 -08001405void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001406 uint32_t orientation = 0;
1407 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001408 // The transform hint is used to improve performance, but we can
1409 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001410 // apply to all displays.
Mathias Agopian42977342012-08-05 00:40:46 -07001411 const Transform& planeTransform(hw->getTransform());
Mathias Agopian4fec8732012-06-29 14:12:52 -07001412 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07001413 if (orientation & Transform::ROT_INVALID) {
1414 orientation = 0;
1415 }
1416 }
David Sodmaneb085e02017-10-05 18:49:04 -07001417 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001418}
1419
Mathias Agopian13127d82013-03-05 17:47:11 -08001420// ----------------------------------------------------------------------------
1421// debugging
1422// ----------------------------------------------------------------------------
1423
Kalle Raitaa099a242017-01-11 11:17:29 -08001424LayerDebugInfo Layer::getLayerDebugInfo() const {
1425 LayerDebugInfo info;
1426 const Layer::State& ds = getDrawingState();
1427 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001428 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001429 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
1430 info.mType = String8(getTypeId());
1431 info.mTransparentRegion = ds.activeTransparentRegion;
1432 info.mVisibleRegion = visibleRegion;
1433 info.mSurfaceDamageRegion = surfaceDamageRegion;
1434 info.mLayerStack = getLayerStack();
1435 info.mX = ds.active.transform.tx();
1436 info.mY = ds.active.transform.ty();
1437 info.mZ = ds.z;
1438 info.mWidth = ds.active.w;
1439 info.mHeight = ds.active.h;
1440 info.mCrop = ds.crop;
1441 info.mFinalCrop = ds.finalCrop;
chaviw13fdc492017-06-27 12:40:18 -07001442 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001443 info.mFlags = ds.flags;
1444 info.mPixelFormat = getPixelFormat();
Peiyong Lin34beb7a2018-03-28 11:57:12 -07001445 info.mDataSpace = static_cast<android_dataspace>(getDataSpace());
Kalle Raitaa099a242017-01-11 11:17:29 -08001446 info.mMatrix[0][0] = ds.active.transform[0][0];
1447 info.mMatrix[0][1] = ds.active.transform[0][1];
1448 info.mMatrix[1][0] = ds.active.transform[1][0];
1449 info.mMatrix[1][1] = ds.active.transform[1][1];
1450 {
David Sodman0cc69182017-11-17 12:12:07 -08001451 sp<const GraphicBuffer> buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001452 if (buffer != 0) {
1453 info.mActiveBufferWidth = buffer->getWidth();
1454 info.mActiveBufferHeight = buffer->getHeight();
1455 info.mActiveBufferStride = buffer->getStride();
1456 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001457 } else {
1458 info.mActiveBufferWidth = 0;
1459 info.mActiveBufferHeight = 0;
1460 info.mActiveBufferStride = 0;
1461 info.mActiveBufferFormat = 0;
1462 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001463 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001464 info.mNumQueuedFrames = getQueuedFrameCount();
1465 info.mRefreshPending = isBufferLatched();
1466 info.mIsOpaque = isOpaque(ds);
1467 info.mContentDirty = contentDirty;
1468 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001469}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001470
Dan Stozae22aec72016-08-01 13:20:59 -07001471void Layer::miniDumpHeader(String8& result) {
1472 result.append("----------------------------------------");
1473 result.append("---------------------------------------\n");
1474 result.append(" Layer name\n");
1475 result.append(" Z | ");
1476 result.append(" Comp Type | ");
1477 result.append(" Disp Frame (LTRB) | ");
1478 result.append(" Source Crop (LTRB)\n");
1479 result.append("----------------------------------------");
1480 result.append("---------------------------------------\n");
1481}
1482
1483void Layer::miniDump(String8& result, int32_t hwcId) const {
David Sodman6f65f3e2017-11-03 14:28:09 -07001484 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stozae22aec72016-08-01 13:20:59 -07001485 return;
1486 }
1487
1488 String8 name;
1489 if (mName.length() > 77) {
1490 std::string shortened;
1491 shortened.append(mName.string(), 36);
1492 shortened.append("[...]");
1493 shortened.append(mName.string() + (mName.length() - 36), 36);
1494 name = shortened.c_str();
1495 } else {
1496 name = mName;
1497 }
1498
1499 result.appendFormat(" %s\n", name.string());
1500
1501 const Layer::State& layerState(getDrawingState());
David Sodman6f65f3e2017-11-03 14:28:09 -07001502 const LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers.at(hwcId);
Chia-I Wu1e043612018-03-01 09:45:09 -08001503 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
1504 result.appendFormat(" rel %6d | ", layerState.z);
1505 } else {
1506 result.appendFormat(" %10d | ", layerState.z);
1507 }
David Sodman41fdfc92017-11-06 16:09:56 -08001508 result.appendFormat("%10s | ", to_string(getCompositionType(hwcId)).c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001509 const Rect& frame = hwcInfo.displayFrame;
David Sodman41fdfc92017-11-06 16:09:56 -08001510 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Dan Stoza5a423ea2017-02-16 14:10:39 -08001511 const FloatRect& crop = hwcInfo.sourceCrop;
David Sodman41fdfc92017-11-06 16:09:56 -08001512 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 -07001513
1514 result.append("- - - - - - - - - - - - - - - - - - - - ");
1515 result.append("- - - - - - - - - - - - - - - - - - - -\n");
1516}
Dan Stozae22aec72016-08-01 13:20:59 -07001517
Svetoslavd85084b2014-03-20 10:28:31 -07001518void Layer::dumpFrameStats(String8& result) const {
1519 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001520}
1521
Svetoslavd85084b2014-03-20 10:28:31 -07001522void Layer::clearFrameStats() {
1523 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001524}
1525
Jamie Gennis6547ff42013-07-16 20:12:42 -07001526void Layer::logFrameStats() {
1527 mFrameTracker.logAndResetStats(mName);
1528}
1529
Svetoslavd85084b2014-03-20 10:28:31 -07001530void Layer::getFrameStats(FrameStats* outStats) const {
1531 mFrameTracker.getStats(outStats);
1532}
1533
Brian Andersond6927fb2016-07-23 23:37:30 -07001534void Layer::dumpFrameEvents(String8& result) {
David Sodman41fdfc92017-11-06 16:09:56 -08001535 result.appendFormat("- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001536 Mutex::Autolock lock(mFrameEventHistoryMutex);
1537 mFrameEventHistory.checkFencesForCompletion();
1538 mFrameEventHistory.dump(result);
1539}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001540
Brian Anderson5ea5e592016-12-01 16:54:33 -08001541void Layer::onDisconnect() {
1542 Mutex::Autolock lock(mFrameEventHistoryMutex);
1543 mFrameEventHistory.onDisconnect();
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001544 mTimeStats.onDisconnect(getName().c_str());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001545}
1546
Brian Anderson3890c392016-07-25 12:48:08 -07001547void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001548 FrameEventHistoryDelta* outDelta) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001549 if (newTimestamps) {
1550 mTimeStats.setPostTime(getName().c_str(), newTimestamps->frameNumber,
1551 newTimestamps->postedTime);
1552 }
1553
Brian Andersond6927fb2016-07-23 23:37:30 -07001554 Mutex::Autolock lock(mFrameEventHistoryMutex);
1555 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001556 // If there are any unsignaled fences in the aquire timeline at this
1557 // point, the previously queued frame hasn't been latched yet. Go ahead
1558 // and try to get the signal time here so the syscall is taken out of
1559 // the main thread's critical path.
1560 mAcquireTimeline.updateSignalTimes();
1561 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001562 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001563 mFrameEventHistory.addQueue(*newTimestamps);
1564 }
1565
Brian Anderson3890c392016-07-25 12:48:08 -07001566 if (outDelta) {
1567 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001568 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001569}
Dan Stozae77c7662016-05-13 11:37:28 -07001570
Chia-I Wu98f1c102017-05-30 14:54:08 -07001571size_t Layer::getChildrenCount() const {
1572 size_t count = 0;
1573 for (const sp<Layer>& child : mCurrentChildren) {
1574 count += 1 + child->getChildrenCount();
1575 }
1576 return count;
1577}
1578
Robert Carr1f0a16a2016-10-24 16:27:39 -07001579void Layer::addChild(const sp<Layer>& layer) {
1580 mCurrentChildren.add(layer);
1581 layer->setParent(this);
1582}
1583
1584ssize_t Layer::removeChild(const sp<Layer>& layer) {
1585 layer->setParent(nullptr);
1586 return mCurrentChildren.remove(layer);
1587}
1588
Robert Carr1db73f62016-12-21 12:58:51 -08001589bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1590 sp<Handle> handle = nullptr;
1591 sp<Layer> newParent = nullptr;
1592 if (newParentHandle == nullptr) {
1593 return false;
1594 }
1595 handle = static_cast<Handle*>(newParentHandle.get());
1596 newParent = handle->owner.promote();
1597 if (newParent == nullptr) {
1598 ALOGE("Unable to promote Layer handle");
1599 return false;
1600 }
1601
1602 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001603 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001604
1605 sp<Client> client(child->mClientRef.promote());
1606 if (client != nullptr) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001607 client->updateParent(newParent);
Robert Carr1db73f62016-12-21 12:58:51 -08001608 }
1609 }
1610 mCurrentChildren.clear();
1611
1612 return true;
1613}
1614
Robert Carr15eae092018-03-23 13:43:53 -07001615void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001616 for (const sp<Layer>& child : mDrawingChildren) {
1617 child->mDrawingParent = newParent;
1618 }
1619}
1620
chaviwf1961f72017-09-18 16:41:07 -07001621bool Layer::reparent(const sp<IBinder>& newParentHandle) {
1622 if (newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001623 return false;
1624 }
1625
1626 auto handle = static_cast<Handle*>(newParentHandle.get());
1627 sp<Layer> newParent = handle->owner.promote();
1628 if (newParent == nullptr) {
1629 ALOGE("Unable to promote Layer handle");
1630 return false;
1631 }
1632
chaviwf1961f72017-09-18 16:41:07 -07001633 sp<Layer> parent = getParent();
1634 if (parent != nullptr) {
1635 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001636 }
chaviwf1961f72017-09-18 16:41:07 -07001637 newParent->addChild(this);
chaviw06178942017-07-27 10:25:59 -07001638
chaviwf1961f72017-09-18 16:41:07 -07001639 sp<Client> client(mClientRef.promote());
chaviw06178942017-07-27 10:25:59 -07001640 sp<Client> newParentClient(newParent->mClientRef.promote());
1641
chaviwf1961f72017-09-18 16:41:07 -07001642 if (client != newParentClient) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001643 client->updateParent(newParent);
chaviw06178942017-07-27 10:25:59 -07001644 }
1645
chaviw06178942017-07-27 10:25:59 -07001646 return true;
1647}
1648
Robert Carr9524cb32017-02-13 11:32:32 -08001649bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001650 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001651 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001652 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001653 if (client != nullptr && parentClient != client) {
Robert Carr7f619b22017-11-06 12:56:35 -08001654 client->detachLayer(child.get());
1655 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001656 }
Robert Carr7f619b22017-11-06 12:56:35 -08001657 }
Robert Carr9524cb32017-02-13 11:32:32 -08001658
1659 return true;
1660}
1661
Chia-I Wu11481472018-05-04 10:43:19 -07001662bool Layer::isLegacyDataSpace() const {
1663 // return true when no higher bits are set
1664 return !(mDrawingState.dataSpace & (ui::Dataspace::STANDARD_MASK |
1665 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001666}
1667
Robert Carr1f0a16a2016-10-24 16:27:39 -07001668void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001669 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001670}
1671
1672void Layer::clearSyncPoints() {
1673 for (const auto& child : mCurrentChildren) {
1674 child->clearSyncPoints();
1675 }
1676
1677 Mutex::Autolock lock(mLocalSyncPointMutex);
1678 for (auto& point : mLocalSyncPoints) {
1679 point->setFrameAvailable();
1680 }
1681 mLocalSyncPoints.clear();
1682}
1683
1684int32_t Layer::getZ() const {
1685 return mDrawingState.z;
1686}
1687
Robert Carr29abff82017-12-04 13:51:20 -08001688bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1689 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1690 const State& state = useDrawing ? mDrawingState : mCurrentState;
1691 return state.zOrderRelativeOf != nullptr;
1692}
1693
David Sodman41fdfc92017-11-06 16:09:56 -08001694__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001695 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001696 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1697 "makeTraversalList received invalid stateSet");
1698 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1699 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1700 const State& state = useDrawing ? mDrawingState : mCurrentState;
1701
Robert Carr29abff82017-12-04 13:51:20 -08001702 if (state.zOrderRelatives.size() == 0) {
1703 *outSkipRelativeZUsers = true;
1704 return children;
1705 }
1706
Robert Carrdb66e622017-04-10 16:55:57 -07001707 LayerVector traverse;
Dan Stoza412903f2017-04-27 13:42:17 -07001708 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001709 sp<Layer> strongRelative = weakRelative.promote();
1710 if (strongRelative != nullptr) {
1711 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001712 }
1713 }
1714
Dan Stoza412903f2017-04-27 13:42:17 -07001715 for (const sp<Layer>& child : children) {
Robert Carr503c7042017-09-27 15:06:08 -07001716 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1717 if (childState.zOrderRelativeOf != nullptr) {
1718 continue;
1719 }
Robert Carrdb66e622017-04-10 16:55:57 -07001720 traverse.add(child);
1721 }
1722
1723 return traverse;
1724}
1725
Robert Carr1f0a16a2016-10-24 16:27:39 -07001726/**
Robert Carrdb66e622017-04-10 16:55:57 -07001727 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001728 */
Dan Stoza412903f2017-04-27 13:42:17 -07001729void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001730 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1731 // produce a new list for traversing, including our relatives, and not including our children
1732 // who are relatives of another surface. In the case that there are no relative Z,
1733 // makeTraversalList returns our children directly to avoid significant overhead.
1734 // However in this case we need to take the responsibility for filtering children which
1735 // are relatives of another surface here.
1736 bool skipRelativeZUsers = false;
1737 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001738
Robert Carr1f0a16a2016-10-24 16:27:39 -07001739 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001740 for (; i < list.size(); i++) {
1741 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001742 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1743 continue;
1744 }
1745
Robert Carrdb66e622017-04-10 16:55:57 -07001746 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001747 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001748 }
Dan Stoza412903f2017-04-27 13:42:17 -07001749 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001750 }
Robert Carr29abff82017-12-04 13:51:20 -08001751
Dan Stoza412903f2017-04-27 13:42:17 -07001752 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001753 for (; i < list.size(); i++) {
1754 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001755
1756 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1757 continue;
1758 }
Dan Stoza412903f2017-04-27 13:42:17 -07001759 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001760 }
1761}
1762
1763/**
Robert Carrdb66e622017-04-10 16:55:57 -07001764 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001765 */
Dan Stoza412903f2017-04-27 13:42:17 -07001766void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1767 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001768 // See traverseInZOrder for documentation.
1769 bool skipRelativeZUsers = false;
1770 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001771
Robert Carr1f0a16a2016-10-24 16:27:39 -07001772 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001773 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001774 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001775
1776 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1777 continue;
1778 }
1779
Robert Carrdb66e622017-04-10 16:55:57 -07001780 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001781 break;
1782 }
Dan Stoza412903f2017-04-27 13:42:17 -07001783 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001784 }
Dan Stoza412903f2017-04-27 13:42:17 -07001785 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001786 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001787 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001788
1789 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1790 continue;
1791 }
1792
Dan Stoza412903f2017-04-27 13:42:17 -07001793 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001794 }
1795}
1796
chaviw4b129c22018-04-09 16:19:43 -07001797LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1798 const std::vector<Layer*>& layersInTree) {
1799 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1800 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001801 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1802 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
chaviw4b129c22018-04-09 16:19:43 -07001803 const State& state = useDrawing ? mDrawingState : mCurrentState;
1804
1805 LayerVector traverse;
1806 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1807 sp<Layer> strongRelative = weakRelative.promote();
1808 // Only add relative layers that are also descendents of the top most parent of the tree.
1809 // If a relative layer is not a descendent, then it should be ignored.
1810 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1811 traverse.add(strongRelative);
1812 }
1813 }
1814
1815 for (const sp<Layer>& child : children) {
1816 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1817 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1818 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1819 // the child here since it won't be added later as a relative.
1820 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1821 childState.zOrderRelativeOf.promote().get())) {
1822 continue;
1823 }
1824 traverse.add(child);
1825 }
1826
1827 return traverse;
1828}
1829
1830void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1831 LayerVector::StateSet stateSet,
1832 const LayerVector::Visitor& visitor) {
1833 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001834
1835 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001836 for (; i < list.size(); i++) {
1837 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001838 if (relative->getZ() >= 0) {
1839 break;
1840 }
chaviw4b129c22018-04-09 16:19:43 -07001841 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001842 }
chaviw4b129c22018-04-09 16:19:43 -07001843
chaviwa76b2712017-09-20 12:02:26 -07001844 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001845 for (; i < list.size(); i++) {
1846 const auto& relative = list[i];
1847 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001848 }
1849}
1850
chaviw4b129c22018-04-09 16:19:43 -07001851std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1852 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1853 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1854
1855 std::vector<Layer*> layersInTree = {this};
1856 for (size_t i = 0; i < children.size(); i++) {
1857 const auto& child = children[i];
1858 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1859 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1860 }
1861
1862 return layersInTree;
1863}
1864
1865void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1866 const LayerVector::Visitor& visitor) {
1867 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1868 std::sort(layersInTree.begin(), layersInTree.end());
1869 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1870}
1871
Robert Carr1f0a16a2016-10-24 16:27:39 -07001872Transform Layer::getTransform() const {
1873 Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001874 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001875 if (p != nullptr) {
1876 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07001877
1878 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
1879 // it isFixedSize) then there may be additional scaling not accounted
1880 // for in the transform. We need to mirror this scaling in child surfaces
1881 // or we will break the contract where WM can treat child surfaces as
1882 // pixels in the parent surface.
David Sodman0cc69182017-11-17 12:12:07 -08001883 if (p->isFixedSize() && p->getBE().compositionInfo.mBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07001884 int bufferWidth;
1885 int bufferHeight;
1886 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
David Sodman0cc69182017-11-17 12:12:07 -08001887 bufferWidth = p->getBE().compositionInfo.mBuffer->getWidth();
1888 bufferHeight = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001889 } else {
David Sodman0cc69182017-11-17 12:12:07 -08001890 bufferHeight = p->getBE().compositionInfo.mBuffer->getWidth();
1891 bufferWidth = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001892 }
David Sodman41fdfc92017-11-06 16:09:56 -08001893 float sx = p->getDrawingState().active.w / static_cast<float>(bufferWidth);
1894 float sy = p->getDrawingState().active.h / static_cast<float>(bufferHeight);
Robert Carr9b429f42017-04-17 14:56:57 -07001895 Transform extraParentScaling;
1896 extraParentScaling.set(sx, 0, 0, sy);
1897 t = t * extraParentScaling;
1898 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001899 }
1900 return t * getDrawingState().active.transform;
1901}
1902
chaviw13fdc492017-06-27 12:40:18 -07001903half Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001904 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07001905
chaviw13fdc492017-06-27 12:40:18 -07001906 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1907 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001908}
Robert Carr6452f122017-03-21 10:41:29 -07001909
chaviw13fdc492017-06-27 12:40:18 -07001910half4 Layer::getColor() const {
1911 const half4 color(getDrawingState().color);
1912 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001913}
Robert Carr6452f122017-03-21 10:41:29 -07001914
Robert Carr1f0a16a2016-10-24 16:27:39 -07001915void Layer::commitChildList() {
1916 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1917 const auto& child = mCurrentChildren[i];
1918 child->commitChildList();
1919 }
1920 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001921 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001922}
1923
chaviw1d044282017-09-27 12:19:28 -07001924void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
1925 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1926 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1927 const State& state = useDrawing ? mDrawingState : mCurrentState;
1928
1929 Transform requestedTransform = state.active.transform;
1930 Transform transform = getTransform();
1931
1932 layerInfo->set_id(sequence);
1933 layerInfo->set_name(getName().c_str());
1934 layerInfo->set_type(String8(getTypeId()));
1935
1936 for (const auto& child : children) {
1937 layerInfo->add_children(child->sequence);
1938 }
1939
1940 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1941 sp<Layer> strongRelative = weakRelative.promote();
1942 if (strongRelative != nullptr) {
1943 layerInfo->add_relatives(strongRelative->sequence);
1944 }
1945 }
1946
1947 LayerProtoHelper::writeToProto(state.activeTransparentRegion,
1948 layerInfo->mutable_transparent_region());
1949 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
1950 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
1951
1952 layerInfo->set_layer_stack(getLayerStack());
1953 layerInfo->set_z(state.z);
1954
1955 PositionProto* position = layerInfo->mutable_position();
1956 position->set_x(transform.tx());
1957 position->set_y(transform.ty());
1958
1959 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
1960 requestedPosition->set_x(requestedTransform.tx());
1961 requestedPosition->set_y(requestedTransform.ty());
1962
1963 SizeProto* size = layerInfo->mutable_size();
1964 size->set_w(state.active.w);
1965 size->set_h(state.active.h);
1966
1967 LayerProtoHelper::writeToProto(state.crop, layerInfo->mutable_crop());
1968 LayerProtoHelper::writeToProto(state.finalCrop, layerInfo->mutable_final_crop());
1969
1970 layerInfo->set_is_opaque(isOpaque(state));
1971 layerInfo->set_invalidate(contentDirty);
Peiyong Lin34beb7a2018-03-28 11:57:12 -07001972 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(getDataSpace())));
chaviw1d044282017-09-27 12:19:28 -07001973 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1974 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
1975 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
1976 layerInfo->set_flags(state.flags);
1977
1978 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1979 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
1980
Jorim Jaggi8e0af362017-11-14 16:28:28 +01001981 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07001982 if (parent != nullptr) {
1983 layerInfo->set_parent(parent->sequence);
1984 }
1985
1986 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1987 if (zOrderRelativeOf != nullptr) {
1988 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1989 }
1990
David Sodman0cc69182017-11-17 12:12:07 -08001991 auto buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001992 if (buffer != nullptr) {
1993 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
chaviw1d044282017-09-27 12:19:28 -07001994 }
1995
1996 layerInfo->set_queued_frames(getQueuedFrameCount());
1997 layerInfo->set_refresh_pending(isBufferLatched());
rongliuccd34842018-03-14 12:26:23 -07001998 layerInfo->set_window_type(state.type);
1999 layerInfo->set_app_id(state.appId);
chaviw1d044282017-09-27 12:19:28 -07002000}
2001
Yiwei Zhang068e31b2018-02-21 13:02:45 -08002002void Layer::writeToProto(LayerProto* layerInfo, int32_t hwcId) {
2003 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