blob: 2300048f7eb5f422acf80ea75554018abff27783 [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
Dan Stoza9e56aa02015-11-02 13:00:03 -0800133 const auto& hwc = flinger->getHwComposer();
134 const auto& activeConfig = hwc.getActiveConfig(HWC_DISPLAY_PRIMARY);
135 nsecs_t displayPeriod = activeConfig->getVsyncPeriod();
Jamie Gennis6547ff42013-07-16 20:12:42 -0700136 mFrameTracker.setDisplayRefreshPeriod(displayPeriod);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800137
138 CompositorTiming compositorTiming;
139 flinger->getCompositorTiming(&compositorTiming);
140 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
David Sodman9eeae692017-11-02 10:53:32 -0700141
Jamie Gennise8696a42012-01-15 18:54:57 -0800142}
143
David Sodman41fdfc92017-11-06 16:09:56 -0800144void Layer::onFirstRef() {}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700145
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700146Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800147 sp<Client> c(mClientRef.promote());
148 if (c != 0) {
149 c->detachLayer(this);
150 }
151
152 for (auto& point : mRemoteSyncPoints) {
153 point->setTransactionApplied();
154 }
155 for (auto& point : mLocalSyncPoints) {
156 point->setFrameAvailable();
157 }
Jamie Gennis6547ff42013-07-16 20:12:42 -0700158 mFrameTracker.logAndResetStats(mName);
Mathias Agopian96f08192010-06-02 23:28:45 -0700159}
160
Mathias Agopian13127d82013-03-05 17:47:11 -0800161// ---------------------------------------------------------------------------
162// callbacks
163// ---------------------------------------------------------------------------
164
David Sodmaneb085e02017-10-05 18:49:04 -0700165/*
166 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
167 * Layer. So, the implementation is done in BufferLayer. When called on a
168 * ColorLayer object, it's essentially a NOP.
169 */
David Sodmaneb085e02017-10-05 18:49:04 -0700170void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800171
Chia-I Wuc6657022017-08-15 11:18:17 -0700172void Layer::onRemovedFromCurrentState() {
173 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
174
chaviw8b3871a2017-11-01 17:41:01 -0700175 mPendingRemoval = true;
176
Robert Carr5edb1ad2017-04-25 10:54:24 -0700177 if (mCurrentState.zOrderRelativeOf != nullptr) {
178 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
179 if (strongRelative != nullptr) {
180 strongRelative->removeZOrderRelative(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700181 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700182 }
183 mCurrentState.zOrderRelativeOf = nullptr;
184 }
185
Chia-I Wuc6657022017-08-15 11:18:17 -0700186 for (const auto& child : mCurrentChildren) {
187 child->onRemovedFromCurrentState();
188 }
189}
Chia-I Wu38512252017-05-17 14:36:16 -0700190
Chia-I Wuc6657022017-08-15 11:18:17 -0700191void Layer::onRemoved() {
192 // the layer is removed from SF mLayersPendingRemoval
David Sodmaneb085e02017-10-05 18:49:04 -0700193 abandon();
Chia-I Wuc6657022017-08-15 11:18:17 -0700194
Steven Thomasb02664d2017-07-26 18:48:28 -0700195 destroyAllHwcLayers();
Chia-I Wu38512252017-05-17 14:36:16 -0700196
Robert Carr1f0a16a2016-10-24 16:27:39 -0700197 for (const auto& child : mCurrentChildren) {
198 child->onRemoved();
199 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700200}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700201
Mathias Agopian13127d82013-03-05 17:47:11 -0800202// ---------------------------------------------------------------------------
203// set-up
204// ---------------------------------------------------------------------------
205
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700206const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800207 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208}
209
chaviw13fdc492017-06-27 12:40:18 -0700210bool Layer::getPremultipledAlpha() const {
211 return mPremultipliedAlpha;
212}
213
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700214sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800215 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700216 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800217}
218
219// ---------------------------------------------------------------------------
220// h/w composer set-up
221// ---------------------------------------------------------------------------
222
Steven Thomasb02664d2017-07-26 18:48:28 -0700223bool Layer::createHwcLayer(HWComposer* hwc, int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700224 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.count(hwcId) != 0,
David Sodman9eeae692017-11-02 10:53:32 -0700225 "Already have a layer for hwcId %d", hwcId);
Steven Thomasb02664d2017-07-26 18:48:28 -0700226 HWC2::Layer* layer = hwc->createLayer(hwcId);
227 if (!layer) {
228 return false;
229 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700230 LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers[hwcId];
Steven Thomasb02664d2017-07-26 18:48:28 -0700231 hwcInfo.hwc = hwc;
232 hwcInfo.layer = layer;
233 layer->setLayerDestroyedListener(
David Sodman6f65f3e2017-11-03 14:28:09 -0700234 [this, hwcId](HWC2::Layer* /*layer*/) { getBE().mHwcLayers.erase(hwcId); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700235 return true;
236}
237
Chia-I Wu83806892017-11-16 10:50:20 -0800238bool Layer::destroyHwcLayer(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700239 if (getBE().mHwcLayers.count(hwcId) == 0) {
Chia-I Wu83806892017-11-16 10:50:20 -0800240 return false;
Steven Thomasb02664d2017-07-26 18:48:28 -0700241 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700242 auto& hwcInfo = getBE().mHwcLayers[hwcId];
David Sodman41fdfc92017-11-06 16:09:56 -0800243 LOG_ALWAYS_FATAL_IF(hwcInfo.layer == nullptr, "Attempt to destroy null layer");
Steven Thomasb02664d2017-07-26 18:48:28 -0700244 LOG_ALWAYS_FATAL_IF(hwcInfo.hwc == nullptr, "Missing HWComposer");
245 hwcInfo.hwc->destroyLayer(hwcId, hwcInfo.layer);
246 // The layer destroyed listener should have cleared the entry from
247 // mHwcLayers. Verify that.
David Sodman6f65f3e2017-11-03 14:28:09 -0700248 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.count(hwcId) != 0,
249 "Stale layer entry in getBE().mHwcLayers");
Chia-I Wu83806892017-11-16 10:50:20 -0800250 return true;
Steven Thomasb02664d2017-07-26 18:48:28 -0700251}
252
253void Layer::destroyAllHwcLayers() {
David Sodman6f65f3e2017-11-03 14:28:09 -0700254 size_t numLayers = getBE().mHwcLayers.size();
Steven Thomasb02664d2017-07-26 18:48:28 -0700255 for (size_t i = 0; i < numLayers; ++i) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700256 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.empty(), "destroyAllHwcLayers failed");
257 destroyHwcLayer(getBE().mHwcLayers.begin()->first);
Steven Thomasb02664d2017-07-26 18:48:28 -0700258 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700259 LOG_ALWAYS_FATAL_IF(!getBE().mHwcLayers.empty(),
David Sodman41fdfc92017-11-06 16:09:56 -0800260 "All hardware composer layers should have been destroyed");
Steven Thomasb02664d2017-07-26 18:48:28 -0700261}
Steven Thomasb02664d2017-07-26 18:48:28 -0700262
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800263Rect Layer::getContentCrop() const {
264 // this is the crop rectangle that applies to the buffer
265 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700266 Rect crop;
267 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800268 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700269 crop = mCurrentCrop;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800270 } else if (getBE().compositionInfo.mBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800271 // otherwise we use the whole buffer
David Sodman0cc69182017-11-17 12:12:07 -0800272 crop = getBE().compositionInfo.mBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700273 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800274 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700275 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700276 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700277 return crop;
278}
279
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700280static Rect reduce(const Rect& win, const Region& exclude) {
281 if (CC_LIKELY(exclude.isEmpty())) {
282 return win;
283 }
284 if (exclude.isRect()) {
285 return win.reduce(exclude.getBounds());
286 }
287 return Region(win).subtract(exclude).getBounds();
288}
289
Dan Stoza80d61162017-12-20 15:57:52 -0800290static FloatRect reduce(const FloatRect& win, const Region& exclude) {
291 if (CC_LIKELY(exclude.isEmpty())) {
292 return win;
293 }
294 // Convert through Rect (by rounding) for lack of FloatRegion
295 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
296}
297
Robert Carr1f0a16a2016-10-24 16:27:39 -0700298Rect Layer::computeScreenBounds(bool reduceTransparentRegion) const {
299 const Layer::State& s(getDrawingState());
300 Rect win(s.active.w, s.active.h);
301
302 if (!s.crop.isEmpty()) {
303 win.intersect(s.crop, &win);
304 }
305
306 Transform t = getTransform();
307 win = t.transform(win);
308
Robert Carr41b08b52017-06-01 16:11:34 -0700309 if (!s.finalCrop.isEmpty()) {
310 win.intersect(s.finalCrop, &win);
311 }
312
Chia-I Wue41dbe62017-06-13 14:10:56 -0700313 const sp<Layer>& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700314 // Now we need to calculate the parent bounds, so we can clip ourselves to those.
315 // When calculating the parent bounds for purposes of clipping,
316 // we don't need to constrain the parent to its transparent region.
317 // The transparent region is an optimization based on the
318 // buffer contents of the layer, but does not affect the space allocated to
319 // it by policy, and thus children should be allowed to extend into the
320 // parent's transparent region. In fact one of the main uses, is to reduce
321 // buffer allocation size in cases where a child window sits behind a main window
322 // (by marking the hole in the parent window as a transparent region)
323 if (p != nullptr) {
324 Rect bounds = p->computeScreenBounds(false);
325 bounds.intersect(win, &win);
326 }
327
328 if (reduceTransparentRegion) {
329 auto const screenTransparentRegion = t.transform(s.activeTransparentRegion);
330 win = reduce(win, screenTransparentRegion);
331 }
332
333 return win;
334}
335
Dan Stoza80d61162017-12-20 15:57:52 -0800336FloatRect Layer::computeBounds() const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700337 const Layer::State& s(getDrawingState());
Michael Lentine6c925ed2014-09-26 17:55:01 -0700338 return computeBounds(s.activeTransparentRegion);
339}
340
Dan Stoza80d61162017-12-20 15:57:52 -0800341FloatRect Layer::computeBounds(const Region& activeTransparentRegion) const {
Michael Lentine6c925ed2014-09-26 17:55:01 -0700342 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800343 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700344
345 if (!s.crop.isEmpty()) {
346 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800347 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700348
349 Rect bounds = win;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700350 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700351 if (p != nullptr) {
Robert Carrde9ec442017-02-08 17:43:36 -0800352 // Look in computeScreenBounds recursive call for explanation of
353 // why we pass false here.
354 bounds = p->computeScreenBounds(false /* reduceTransparentRegion */);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700355 }
356
357 Transform t = getTransform();
Dan Stoza80d61162017-12-20 15:57:52 -0800358
359 FloatRect floatWin = win.toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700360 if (p != nullptr) {
Dan Stoza80d61162017-12-20 15:57:52 -0800361 floatWin = t.transform(floatWin);
362 floatWin = floatWin.intersect(bounds.toFloatRect());
363 floatWin = t.inverse().transform(floatWin);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700364 }
365
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700366 // subtract the transparent region and snap to the bounds
Dan Stoza80d61162017-12-20 15:57:52 -0800367 return reduce(floatWin, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800368}
369
Robert Carr1f0a16a2016-10-24 16:27:39 -0700370Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& hw) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700371 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800372 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700373 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800374
375 // apply the projection's clipping to the window crop in
376 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700377 // if there are no window scaling involved, this operation will map to full
378 // pixels in the buffer.
379 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
380 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700381
382 Rect activeCrop(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700383 if (!s.crop.isEmpty()) {
Chia-I Wudf7867f2017-07-20 14:24:37 -0700384 activeCrop.intersect(s.crop, &activeCrop);
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700385 }
386
Robert Carr1f0a16a2016-10-24 16:27:39 -0700387 Transform t = getTransform();
388 activeCrop = t.transform(activeCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000389 if (!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
390 activeCrop.clear();
391 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700392 if (!s.finalCrop.isEmpty()) {
David Sodman41fdfc92017-11-06 16:09:56 -0800393 if (!activeCrop.intersect(s.finalCrop, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000394 activeCrop.clear();
395 }
396 }
chaviwb1154d12017-10-31 14:15:36 -0700397
398 const auto& p = mDrawingParent.promote();
399 if (p != nullptr) {
400 auto parentCrop = p->computeInitialCrop(hw);
401 activeCrop.intersect(parentCrop, &activeCrop);
402 }
403
Robert Carr1f0a16a2016-10-24 16:27:39 -0700404 return activeCrop;
405}
406
Dan Stoza5a423ea2017-02-16 14:10:39 -0800407FloatRect Layer::computeCrop(const sp<const DisplayDevice>& hw) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700408 // the content crop is the area of the content that gets scaled to the
409 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800410 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700411
412 // In addition there is a WM-specified crop we pull from our drawing state.
413 const State& s(getDrawingState());
414
415 // Screen space to make reduction to parent crop clearer.
416 Rect activeCrop = computeInitialCrop(hw);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700417 Transform t = getTransform();
418 // Back to layer space to work with the content crop.
419 activeCrop = t.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800420
Michael Lentine28ea2172014-11-19 18:32:37 -0800421 // This needs to be here as transform.transform(Rect) computes the
422 // transformed rect and then takes the bounding box of the result before
423 // returning. This means
424 // transform.inverse().transform(transform.transform(Rect)) != Rect
425 // in which case we need to make sure the final rect is clipped to the
426 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000427 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
428 activeCrop.clear();
429 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800430
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700431 // subtract the transparent region and snap to the bounds
432 activeCrop = reduce(activeCrop, s.activeTransparentRegion);
433
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000434 // Transform the window crop to match the buffer coordinate system,
435 // which means using the inverse of the current transform set on the
436 // SurfaceFlingerConsumer.
437 uint32_t invTransform = mCurrentTransform;
Robert Carrcae605c2017-03-29 12:10:31 -0700438 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000439 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700440 * the code below applies the primary display's inverse transform to the
441 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000442 */
David Sodman41fdfc92017-11-06 16:09:56 -0800443 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000444 // calculate the inverse transform
445 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800446 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800447 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000448 // and apply to the current transform
David Sodman41fdfc92017-11-06 16:09:56 -0800449 invTransform = (Transform(invTransformOrient) * Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800450 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000451
452 int winWidth = s.active.w;
453 int winHeight = s.active.h;
454 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
455 // If the activeCrop has been rotate the ends are rotated but not
456 // the space itself so when transforming ends back we can't rely on
457 // a modification of the axes of rotation. To account for this we
458 // need to reorient the inverse rotation in terms of the current
459 // axes of rotation.
460 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
461 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
462 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800463 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000464 }
465 winWidth = s.active.h;
466 winHeight = s.active.w;
467 }
David Sodman41fdfc92017-11-06 16:09:56 -0800468 const Rect winCrop = activeCrop.transform(invTransform, s.active.w, s.active.h);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000469
470 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800471 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000472 float yScale = crop.getHeight() / float(winHeight);
473
David Sodman41fdfc92017-11-06 16:09:56 -0800474 float insetL = winCrop.left * xScale;
475 float insetT = winCrop.top * yScale;
476 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000477 float insetB = (winHeight - winCrop.bottom) * yScale;
478
David Sodman41fdfc92017-11-06 16:09:56 -0800479 crop.left += insetL;
480 crop.top += insetT;
481 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000482 crop.bottom -= insetB;
483
Mathias Agopian13127d82013-03-05 17:47:11 -0800484 return crop;
485}
486
Robert Carrae060832016-11-28 10:51:00 -0800487void Layer::setGeometry(const sp<const DisplayDevice>& displayDevice, uint32_t z)
Mathias Agopiana350ff92010-08-10 17:14:02 -0700488{
Dan Stoza9e56aa02015-11-02 13:00:03 -0800489 const auto hwcId = displayDevice->getHwcDisplayId();
David Sodman6f65f3e2017-11-03 14:28:09 -0700490 auto& hwcInfo = getBE().mHwcLayers[hwcId];
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700491
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700492 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800493 hwcInfo.forceClientComposition = false;
494
495 if (isSecure() && !displayDevice->isSecure()) {
496 hwcInfo.forceClientComposition = true;
497 }
498
499 auto& hwcLayer = hwcInfo.layer;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700500
Mathias Agopian13127d82013-03-05 17:47:11 -0800501 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700502 const State& s(getDrawingState());
David Revemanecf0fa52017-03-03 11:32:44 -0500503 auto blendMode = HWC2::BlendMode::None;
Robert Carr6452f122017-03-21 10:41:29 -0700504 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800505 blendMode =
506 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800507 }
David Revemanecf0fa52017-03-03 11:32:44 -0500508 auto error = hwcLayer->setBlendMode(blendMode);
David Sodman41fdfc92017-11-06 16:09:56 -0800509 ALOGE_IF(error != HWC2::Error::None,
510 "[%s] Failed to set blend mode %s:"
511 " %s (%d)",
512 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
513 static_cast<int32_t>(error));
Mathias Agopian13127d82013-03-05 17:47:11 -0800514
515 // apply the layer's transform, followed by the display's global transform
516 // here we're guaranteed that the layer's transform preserves rects
Michael Lentine6c925ed2014-09-26 17:55:01 -0700517 Region activeTransparentRegion(s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700518 Transform t = getTransform();
Robert Carrb5d3d262016-03-25 15:08:13 -0700519 if (!s.crop.isEmpty()) {
520 Rect activeCrop(s.crop);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700521 activeCrop = t.transform(activeCrop);
David Sodman41fdfc92017-11-06 16:09:56 -0800522 if (!activeCrop.intersect(displayDevice->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000523 activeCrop.clear();
524 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700525 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800526 // This needs to be here as transform.transform(Rect) computes the
527 // transformed rect and then takes the bounding box of the result before
528 // returning. This means
529 // transform.inverse().transform(transform.transform(Rect)) != Rect
530 // in which case we need to make sure the final rect is clipped to the
531 // display bounds.
David Sodman41fdfc92017-11-06 16:09:56 -0800532 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000533 activeCrop.clear();
534 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700535 // mark regions outside the crop as transparent
536 activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top));
David Sodman41fdfc92017-11-06 16:09:56 -0800537 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom, s.active.w, s.active.h));
538 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
539 activeTransparentRegion.orSelf(
540 Rect(activeCrop.right, activeCrop.top, s.active.w, activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700541 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700542
Dan Stoza80d61162017-12-20 15:57:52 -0800543 // computeBounds returns a FloatRect to provide more accuracy during the
544 // transformation. We then round upon constructing 'frame'.
545 Rect frame{t.transform(computeBounds(activeTransparentRegion))};
Robert Carrb5d3d262016-03-25 15:08:13 -0700546 if (!s.finalCrop.isEmpty()) {
David Sodman41fdfc92017-11-06 16:09:56 -0800547 if (!frame.intersect(s.finalCrop, &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000548 frame.clear();
549 }
550 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000551 if (!frame.intersect(displayDevice->getViewport(), &frame)) {
552 frame.clear();
553 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800554 const Transform& tr(displayDevice->getTransform());
555 Rect transformedFrame = tr.transform(frame);
David Revemanecf0fa52017-03-03 11:32:44 -0500556 error = hwcLayer->setDisplayFrame(transformedFrame);
Dan Stozae22aec72016-08-01 13:20:59 -0700557 if (error != HWC2::Error::None) {
David Sodman41fdfc92017-11-06 16:09:56 -0800558 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
559 transformedFrame.left, transformedFrame.top, transformedFrame.right,
560 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stozae22aec72016-08-01 13:20:59 -0700561 } else {
562 hwcInfo.displayFrame = transformedFrame;
563 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800564
Dan Stoza5a423ea2017-02-16 14:10:39 -0800565 FloatRect sourceCrop = computeCrop(displayDevice);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800566 error = hwcLayer->setSourceCrop(sourceCrop);
Dan Stozae22aec72016-08-01 13:20:59 -0700567 if (error != HWC2::Error::None) {
568 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
David Sodman41fdfc92017-11-06 16:09:56 -0800569 "%s (%d)",
570 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
571 to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stozae22aec72016-08-01 13:20:59 -0700572 } else {
573 hwcInfo.sourceCrop = sourceCrop;
574 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800575
chaviw13fdc492017-06-27 12:40:18 -0700576 float alpha = static_cast<float>(getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -0700577 error = hwcLayer->setPlaneAlpha(alpha);
David Sodman41fdfc92017-11-06 16:09:56 -0800578 ALOGE_IF(error != HWC2::Error::None,
579 "[%s] Failed to set plane alpha %.3f: "
580 "%s (%d)",
581 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800582
Robert Carrae060832016-11-28 10:51:00 -0800583 error = hwcLayer->setZOrder(z);
David Sodman41fdfc92017-11-06 16:09:56 -0800584 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
585 to_string(error).c_str(), static_cast<int32_t>(error));
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500586
Albert Chaulk2a589632017-05-04 16:59:44 -0400587 int type = s.type;
588 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700589 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400590 if (parent.get()) {
591 auto& parentState = parent->getDrawingState();
rongliuccd34842018-03-14 12:26:23 -0700592 if (parentState.type >= 0 || parentState.appId >= 0) {
593 type = parentState.type;
594 appId = parentState.appId;
595 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400596 }
597
598 error = hwcLayer->setInfo(type, appId);
David Sodman41fdfc92017-11-06 16:09:56 -0800599 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
600 static_cast<int32_t>(error));
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800601
Mathias Agopian29a367b2011-07-12 14:51:45 -0700602 /*
603 * Transformations are applied in this order:
604 * 1) buffer orientation/flip/mirror
605 * 2) state transformation (window manager)
606 * 3) layer orientation (screen orientation)
607 * (NOTE: the matrices are multiplied in reverse order)
608 */
609
610 const Transform bufferOrientation(mCurrentTransform);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700611 Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700612
Robert Carrcae605c2017-03-29 12:10:31 -0700613 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700614 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700615 * the code below applies the primary display's inverse transform to the
616 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700617 */
David Sodman41fdfc92017-11-06 16:09:56 -0800618 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700619 // calculate the inverse transform
620 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800621 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700622 }
Robert Carrcae605c2017-03-29 12:10:31 -0700623
624 /*
625 * Here we cancel out the orientation component of the WM transform.
626 * The scaling and translate components are already included in our bounds
627 * computation so it's enough to just omit it in the composition.
628 * See comment in onDraw with ref to b/36727915 for why.
629 */
630 transform = Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700631 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700632
633 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800634 const uint32_t orientation = transform.getOrientation();
Jorim Jaggif3bd94a2018-03-27 15:38:03 +0200635 if (orientation & Transform::ROT_INVALID) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800636 // we can only handle simple transformation
637 hwcInfo.forceClientComposition = true;
638 } else {
639 auto transform = static_cast<HWC2::Transform>(orientation);
Yiwei Zhang068e31b2018-02-21 13:02:45 -0800640 hwcInfo.transform = transform;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800641 auto error = hwcLayer->setTransform(transform);
David Sodman41fdfc92017-11-06 16:09:56 -0800642 ALOGE_IF(error != HWC2::Error::None,
643 "[%s] Failed to set transform %s: "
644 "%s (%d)",
645 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
646 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800647 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700648}
649
Dan Stoza9e56aa02015-11-02 13:00:03 -0800650void Layer::forceClientComposition(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700651 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800652 ALOGE("forceClientComposition: no HWC layer found (%d)", hwcId);
653 return;
654 }
655
David Sodman6f65f3e2017-11-03 14:28:09 -0700656 getBE().mHwcLayers[hwcId].forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800657}
Dan Stozaee44edd2015-03-23 15:50:23 -0700658
chaviwc9232ed2017-11-14 15:31:15 -0800659bool Layer::getForceClientComposition(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700660 if (getBE().mHwcLayers.count(hwcId) == 0) {
chaviwc9232ed2017-11-14 15:31:15 -0800661 ALOGE("getForceClientComposition: no HWC layer found (%d)", hwcId);
662 return false;
663 }
664
David Sodman6f65f3e2017-11-03 14:28:09 -0700665 return getBE().mHwcLayers[hwcId].forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800666}
667
Dan Stoza9e56aa02015-11-02 13:00:03 -0800668void Layer::updateCursorPosition(const sp<const DisplayDevice>& displayDevice) {
669 auto hwcId = displayDevice->getHwcDisplayId();
David Sodman6f65f3e2017-11-03 14:28:09 -0700670 if (getBE().mHwcLayers.count(hwcId) == 0 ||
David Sodman9eeae692017-11-02 10:53:32 -0700671 getCompositionType(hwcId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800672 return;
673 }
674
675 // This gives us only the "orientation" component of the transform
676 const State& s(getCurrentState());
677
678 // Apply the layer's transform, followed by the display's global transform
679 // Here we're guaranteed that the layer's transform preserves rects
680 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700681 if (!s.crop.isEmpty()) {
682 win.intersect(s.crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800683 }
684 // Subtract the transparent region and snap to the bounds
685 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700686 Rect frame(getTransform().transform(bounds));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800687 frame.intersect(displayDevice->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700688 if (!s.finalCrop.isEmpty()) {
689 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000690 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800691 auto& displayTransform(displayDevice->getTransform());
692 auto position = displayTransform.transform(frame);
693
David Sodman6f65f3e2017-11-03 14:28:09 -0700694 auto error = getBE().mHwcLayers[hwcId].layer->setCursorPosition(position.left,
David Sodman9eeae692017-11-02 10:53:32 -0700695 position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800696 ALOGE_IF(error != HWC2::Error::None,
697 "[%s] Failed to set cursor position "
698 "to (%d, %d): %s (%d)",
699 mName.string(), position.left, position.top, to_string(error).c_str(),
700 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800701}
Riley Andrews03414a12014-07-01 14:22:59 -0700702
Mathias Agopian13127d82013-03-05 17:47:11 -0800703// ---------------------------------------------------------------------------
704// drawing...
705// ---------------------------------------------------------------------------
706
chaviwa76b2712017-09-20 12:02:26 -0700707void Layer::draw(const RenderArea& renderArea, const Region& clip) const {
708 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800709}
710
chaviwa76b2712017-09-20 12:02:26 -0700711void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) const {
712 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800713}
714
chaviwa76b2712017-09-20 12:02:26 -0700715void Layer::draw(const RenderArea& renderArea) const {
716 onDraw(renderArea, Region(renderArea.getBounds()), false);
Dan Stozac7014012014-02-14 15:03:43 -0800717}
718
David Sodman41fdfc92017-11-06 16:09:56 -0800719void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
720 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800721 auto& engine(mFlinger->getRenderEngine());
David Sodman9eeae692017-11-02 10:53:32 -0700722 computeGeometry(renderArea, getBE().mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700723 engine.setupFillWithColor(red, green, blue, alpha);
David Sodman9eeae692017-11-02 10:53:32 -0700724 engine.drawMesh(getBE().mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800725}
726
chaviwa76b2712017-09-20 12:02:26 -0700727void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
David Sodman41fdfc92017-11-06 16:09:56 -0800728 clearWithOpenGL(renderArea, 0, 0, 0, 0);
Mathias Agopian13127d82013-03-05 17:47:11 -0800729}
730
David Sodman41fdfc92017-11-06 16:09:56 -0800731void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type, bool callIntoHwc) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700732 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800733 ALOGE("setCompositionType called without a valid HWC layer");
734 return;
735 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700736 auto& hwcInfo = getBE().mHwcLayers[hwcId];
Dan Stoza9e56aa02015-11-02 13:00:03 -0800737 auto& hwcLayer = hwcInfo.layer;
David Sodman41fdfc92017-11-06 16:09:56 -0800738 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(), to_string(type).c_str(),
739 static_cast<int>(callIntoHwc));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800740 if (hwcInfo.compositionType != type) {
741 ALOGV(" actually setting");
742 hwcInfo.compositionType = type;
743 if (callIntoHwc) {
744 auto error = hwcLayer->setCompositionType(type);
David Sodman41fdfc92017-11-06 16:09:56 -0800745 ALOGE_IF(error != HWC2::Error::None,
746 "[%s] Failed to set "
747 "composition type %s: %s (%d)",
748 mName.string(), to_string(type).c_str(), to_string(error).c_str(),
749 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800750 }
751 }
752}
753
754HWC2::Composition Layer::getCompositionType(int32_t hwcId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -0700755 if (hwcId == DisplayDevice::DISPLAY_ID_INVALID) {
756 // If we're querying the composition type for a display that does not
757 // have a HWC counterpart, then it will always be Client
758 return HWC2::Composition::Client;
759 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700760 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700761 ALOGE("getCompositionType called with an invalid HWC layer");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800762 return HWC2::Composition::Invalid;
763 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700764 return getBE().mHwcLayers.at(hwcId).compositionType;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800765}
766
767void Layer::setClearClientTarget(int32_t hwcId, bool clear) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700768 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800769 ALOGE("setClearClientTarget called without a valid HWC layer");
770 return;
771 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700772 getBE().mHwcLayers[hwcId].clearClientTarget = clear;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800773}
774
775bool Layer::getClearClientTarget(int32_t hwcId) const {
David Sodman6f65f3e2017-11-03 14:28:09 -0700776 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800777 ALOGE("getClearClientTarget called without a valid HWC layer");
778 return false;
779 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700780 return getBE().mHwcLayers.at(hwcId).clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800781}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800782
Dan Stozacac35382016-01-27 12:21:06 -0800783bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
784 if (point->getFrameNumber() <= mCurrentFrameNumber) {
785 // Don't bother with a SyncPoint, since we've already latched the
786 // relevant frame
787 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700788 }
789
Dan Stozacac35382016-01-27 12:21:06 -0800790 Mutex::Autolock lock(mLocalSyncPointMutex);
791 mLocalSyncPoints.push_back(point);
792 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700793}
794
Mathias Agopian13127d82013-03-05 17:47:11 -0800795void Layer::setFiltering(bool filtering) {
796 mFiltering = filtering;
797}
798
799bool Layer::getFiltering() const {
800 return mFiltering;
801}
802
Mathias Agopian13127d82013-03-05 17:47:11 -0800803// ----------------------------------------------------------------------------
804// local state
805// ----------------------------------------------------------------------------
806
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000807static void boundPoint(vec2* point, const Rect& crop) {
808 if (point->x < crop.left) {
809 point->x = crop.left;
810 }
811 if (point->x > crop.right) {
812 point->x = crop.right;
813 }
814 if (point->y < crop.top) {
815 point->y = crop.top;
816 }
817 if (point->y > crop.bottom) {
818 point->y = crop.bottom;
819 }
820}
821
chaviwa76b2712017-09-20 12:02:26 -0700822void Layer::computeGeometry(const RenderArea& renderArea, Mesh& mesh,
823 bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700824 const Layer::State& s(getDrawingState());
chaviwa76b2712017-09-20 12:02:26 -0700825 const Transform renderAreaTransform(renderArea.getTransform());
826 const uint32_t height = renderArea.getHeight();
Dan Stoza80d61162017-12-20 15:57:52 -0800827 FloatRect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700828
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000829 vec2 lt = vec2(win.left, win.top);
830 vec2 lb = vec2(win.left, win.bottom);
831 vec2 rb = vec2(win.right, win.bottom);
832 vec2 rt = vec2(win.right, win.top);
833
Robert Carr1f0a16a2016-10-24 16:27:39 -0700834 Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000835 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700836 lt = layerTransform.transform(lt);
837 lb = layerTransform.transform(lb);
838 rb = layerTransform.transform(rb);
839 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000840 }
841
Robert Carrb5d3d262016-03-25 15:08:13 -0700842 if (!s.finalCrop.isEmpty()) {
843 boundPoint(&lt, s.finalCrop);
844 boundPoint(&lb, s.finalCrop);
845 boundPoint(&rb, s.finalCrop);
846 boundPoint(&rt, s.finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000847 }
848
Mathias Agopianff2ed702013-09-01 21:36:12 -0700849 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700850 position[0] = renderAreaTransform.transform(lt);
851 position[1] = renderAreaTransform.transform(lb);
852 position[2] = renderAreaTransform.transform(rb);
853 position[3] = renderAreaTransform.transform(rt);
David Sodman41fdfc92017-11-06 16:09:56 -0800854 for (size_t i = 0; i < 4; i++) {
chaviwa76b2712017-09-20 12:02:26 -0700855 position[i].y = height - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -0800856 }
857}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800858
David Sodman41fdfc92017-11-06 16:09:56 -0800859bool Layer::isSecure() const {
Dan Stoza23116082015-06-18 14:58:39 -0700860 const Layer::State& s(mDrawingState);
861 return (s.flags & layer_state_t::eLayerSecure);
862}
863
Mathias Agopian13127d82013-03-05 17:47:11 -0800864void Layer::setVisibleRegion(const Region& visibleRegion) {
865 // always called from main thread
866 this->visibleRegion = visibleRegion;
867}
868
869void Layer::setCoveredRegion(const Region& coveredRegion) {
870 // always called from main thread
871 this->coveredRegion = coveredRegion;
872}
873
David Sodman41fdfc92017-11-06 16:09:56 -0800874void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800875 // always called from main thread
876 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
877}
878
Robert Carre5f4f692018-01-12 13:12:28 -0800879void Layer::clearVisibilityRegions() {
880 visibleRegion.clear();
881 visibleNonTransparentRegion.clear();
882 coveredRegion.clear();
883}
884
Mathias Agopian13127d82013-03-05 17:47:11 -0800885// ----------------------------------------------------------------------------
886// transaction
887// ----------------------------------------------------------------------------
888
Dan Stoza7dde5992015-05-22 09:51:44 -0700889void Layer::pushPendingState() {
890 if (!mCurrentState.modified) {
891 return;
892 }
893
Dan Stoza7dde5992015-05-22 09:51:44 -0700894 // If this transaction is waiting on the receipt of a frame, generate a sync
895 // point and send it to the remote layer.
Robert Carr0d480722017-01-10 16:42:54 -0800896 if (mCurrentState.barrierLayer != nullptr) {
897 sp<Layer> barrierLayer = mCurrentState.barrierLayer.promote();
898 if (barrierLayer == nullptr) {
899 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700900 // If we can't promote the layer we are intended to wait on,
901 // then it is expired or otherwise invalid. Allow this transaction
902 // to be applied as per normal (no synchronization).
Robert Carr0d480722017-01-10 16:42:54 -0800903 mCurrentState.barrierLayer = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800904 } else {
David Sodman41fdfc92017-11-06 16:09:56 -0800905 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber);
Robert Carr0d480722017-01-10 16:42:54 -0800906 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800907 mRemoteSyncPoints.push_back(std::move(syncPoint));
908 } else {
909 // We already missed the frame we're supposed to synchronize
910 // on, so go ahead and apply the state update
Robert Carr0d480722017-01-10 16:42:54 -0800911 mCurrentState.barrierLayer = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800912 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700913 }
914
Dan Stoza7dde5992015-05-22 09:51:44 -0700915 // Wake us up to check if the frame has been received
916 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700917 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700918 }
919 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700920 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700921}
922
Pablo Ceballos05289c22016-04-14 15:49:55 -0700923void Layer::popPendingState(State* stateToCommit) {
924 auto oldFlags = stateToCommit->flags;
925 *stateToCommit = mPendingStates[0];
David Sodman41fdfc92017-11-06 16:09:56 -0800926 stateToCommit->flags =
927 (oldFlags & ~stateToCommit->mask) | (stateToCommit->flags & stateToCommit->mask);
Dan Stoza7dde5992015-05-22 09:51:44 -0700928
929 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700930 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700931}
932
Pablo Ceballos05289c22016-04-14 15:49:55 -0700933bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700934 bool stateUpdateAvailable = false;
935 while (!mPendingStates.empty()) {
Robert Carr0d480722017-01-10 16:42:54 -0800936 if (mPendingStates[0].barrierLayer != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700937 if (mRemoteSyncPoints.empty()) {
938 // If we don't have a sync point for this, apply it anyway. It
939 // will be visually wrong, but it should keep us from getting
940 // into too much trouble.
941 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700942 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700943 stateUpdateAvailable = true;
944 continue;
945 }
946
David Sodman41fdfc92017-11-06 16:09:56 -0800947 if (mRemoteSyncPoints.front()->getFrameNumber() != mPendingStates[0].frameNumber) {
948 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800949
950 // Signal our end of the sync point and then dispose of it
951 mRemoteSyncPoints.front()->setTransactionApplied();
952 mRemoteSyncPoints.pop_front();
953 continue;
954 }
955
Dan Stoza7dde5992015-05-22 09:51:44 -0700956 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
957 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700958 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700959 stateUpdateAvailable = true;
960
961 // Signal our end of the sync point and then dispose of it
962 mRemoteSyncPoints.front()->setTransactionApplied();
963 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800964 } else {
965 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700966 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700967 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700968 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700969 stateUpdateAvailable = true;
970 }
971 }
972
973 // If we still have pending updates, wake SurfaceFlinger back up and point
974 // it at this layer so we can process them
975 if (!mPendingStates.empty()) {
976 setTransactionFlags(eTransactionNeeded);
977 mFlinger->setTransactionFlags(eTraversalNeeded);
978 }
979
980 mCurrentState.modified = false;
981 return stateUpdateAvailable;
982}
983
Mathias Agopian13127d82013-03-05 17:47:11 -0800984uint32_t Layer::doTransaction(uint32_t flags) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800985 ATRACE_CALL();
986
Dan Stoza7dde5992015-05-22 09:51:44 -0700987 pushPendingState();
Pablo Ceballos05289c22016-04-14 15:49:55 -0700988 Layer::State c = getCurrentState();
989 if (!applyPendingStates(&c)) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700990 return 0;
991 }
992
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700993 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800994
David Sodman41fdfc92017-11-06 16:09:56 -0800995 const bool sizeChanged = (c.requested.w != s.requested.w) || (c.requested.h != s.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700996
David Sodmaneb085e02017-10-05 18:49:04 -0700997 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700998 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000999 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -08001000 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
1001 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1002 " requested={ wh={%4u,%4u} }}\n"
1003 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1004 " requested={ wh={%4u,%4u} }}\n",
David Sodman9eeae692017-11-02 10:53:32 -07001005 this, getName().string(), mCurrentTransform,
1006 getEffectiveScalingMode(), c.active.w, c.active.h, c.crop.left, c.crop.top,
1007 c.crop.right, c.crop.bottom, c.crop.getWidth(), c.crop.getHeight(), c.requested.w,
1008 c.requested.h, s.active.w, s.active.h, s.crop.left, s.crop.top, s.crop.right,
1009 s.crop.bottom, s.crop.getWidth(), s.crop.getHeight(), s.requested.w,
1010 s.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001011
Jamie Gennis2a0d5b62011-09-26 16:54:44 -07001012 // record the new size, form this point on, when the client request
1013 // a buffer, it'll get the new size.
David Sodmaneb085e02017-10-05 18:49:04 -07001014 setDefaultBufferSize(c.requested.w, c.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001015 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001016
Robert Carre392b552017-09-19 12:16:05 -07001017 // Don't let Layer::doTransaction update the drawing state
1018 // if we have a pending resize, unless we are in fixed-size mode.
1019 // the drawing state will be updated only once we receive a buffer
1020 // with the correct size.
1021 //
1022 // In particular, we want to make sure the clip (which is part
1023 // of the geometry state) is latched together with the size but is
1024 // latched immediately when no resizing is involved.
1025 //
1026 // If a sideband stream is attached, however, we want to skip this
1027 // optimization so that transactions aren't missed when a buffer
1028 // never arrives
1029 //
1030 // In the case that we don't have a buffer we ignore other factors
1031 // and avoid entering the resizePending state. At a high level the
1032 // resizePending state is to avoid applying the state of the new buffer
1033 // to the old buffer. However in the state where we don't have an old buffer
1034 // there is no such concern but we may still be being used as a parent layer.
David Sodman41fdfc92017-11-06 16:09:56 -08001035 const bool resizePending = ((c.requested.w != c.active.w) || (c.requested.h != c.active.h)) &&
David Sodman0cc69182017-11-17 12:12:07 -08001036 (getBE().compositionInfo.mBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001037 if (!isFixedSize()) {
David Sodman0cc69182017-11-17 12:12:07 -08001038 if (resizePending && getBE().compositionInfo.hwc.sidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001039 flags |= eDontUpdateGeometryState;
1040 }
1041 }
1042
Robert Carr7bf247e2017-05-18 14:02:49 -07001043 // Here we apply various requested geometry states, depending on our
1044 // latching configuration. See Layer.h for a detailed discussion of
1045 // how geometry latching is controlled.
1046 if (!(flags & eDontUpdateGeometryState)) {
Pablo Ceballos7d052572016-06-02 17:46:05 -07001047 Layer::State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001048
1049 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1050 // mode, which causes attributes which normally latch regardless of scaling mode,
1051 // to be delayed. We copy the requested state to the active state making sure
1052 // to respect these rules (again see Layer.h for a detailed discussion).
1053 //
1054 // There is an awkward asymmetry in the handling of the crop states in the position
1055 // states, as can be seen below. Largely this arises from position and transform
1056 // being stored in the same data structure while having different latching rules.
1057 // b/38182305
1058 //
1059 // Careful that "c" and editCurrentState may not begin as equivalent due to
1060 // applyPendingStates in the presence of deferred transactions.
1061 if (mFreezeGeometryUpdates) {
Robert Carr82364e32016-05-15 11:27:47 -07001062 float tx = c.active.transform.tx();
1063 float ty = c.active.transform.ty();
1064 c.active = c.requested;
1065 c.active.transform.set(tx, ty);
1066 editCurrentState.active = c.active;
1067 } else {
1068 editCurrentState.active = editCurrentState.requested;
1069 c.active = c.requested;
1070 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001071 }
1072
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001073 if (s.active != c.active) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001074 // invalidate and recompute the visible regions if needed
1075 flags |= Layer::eVisibleRegion;
1076 }
1077
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001078 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001079 // invalidate and recompute the visible regions if needed
1080 flags |= eVisibleRegion;
1081 this->contentDirty = true;
1082
1083 // we may use linear filtering, if the matrix scales us
Robert Carr3dcabfa2016-03-01 18:36:58 -08001084 const uint8_t type = c.active.transform.getType();
David Sodman41fdfc92017-11-06 16:09:56 -08001085 mNeedsFiltering = (!c.active.transform.preserveRects() || (type >= Transform::SCALE));
Mathias Agopian13127d82013-03-05 17:47:11 -08001086 }
1087
Dan Stozac8145172016-04-28 16:29:06 -07001088 // If the layer is hidden, signal and clear out all local sync points so
1089 // that transactions for layers depending on this layer's frames becoming
1090 // visible are not blocked
1091 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001092 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001093 }
1094
Mathias Agopian13127d82013-03-05 17:47:11 -08001095 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001096 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001097 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001098}
1099
Pablo Ceballos05289c22016-04-14 15:49:55 -07001100void Layer::commitTransaction(const State& stateToCommit) {
1101 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001102}
1103
Mathias Agopian13127d82013-03-05 17:47:11 -08001104uint32_t Layer::getTransactionFlags(uint32_t flags) {
1105 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1106}
1107
1108uint32_t Layer::setTransactionFlags(uint32_t flags) {
1109 return android_atomic_or(flags, &mTransactionFlags);
1110}
1111
Robert Carr82364e32016-05-15 11:27:47 -07001112bool Layer::setPosition(float x, float y, bool immediate) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001113 if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001114 return false;
1115 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001116
1117 // We update the requested and active position simultaneously because
1118 // we want to apply the position portion of the transform matrix immediately,
1119 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Robert Carr3dcabfa2016-03-01 18:36:58 -08001120 mCurrentState.requested.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001121 if (immediate && !mFreezeGeometryUpdates) {
1122 // Here we directly update the active state
1123 // unlike other setters, because we store it within
1124 // the transform, but use different latching rules.
1125 // b/38182305
Robert Carr82364e32016-05-15 11:27:47 -07001126 mCurrentState.active.transform.set(x, y);
1127 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001128 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001129
Dan Stoza7dde5992015-05-22 09:51:44 -07001130 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001131 setTransactionFlags(eTransactionNeeded);
1132 return true;
1133}
Robert Carr82364e32016-05-15 11:27:47 -07001134
Robert Carr1f0a16a2016-10-24 16:27:39 -07001135bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1136 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1137 if (idx < 0) {
1138 return false;
1139 }
1140 if (childLayer->setLayer(z)) {
1141 mCurrentChildren.removeAt(idx);
1142 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001143 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001144 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001145 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001146}
1147
Robert Carr503c7042017-09-27 15:06:08 -07001148bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1149 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1150 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1151 if (idx < 0) {
1152 return false;
1153 }
1154 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1155 mCurrentChildren.removeAt(idx);
1156 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001157 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001158 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001159 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001160}
1161
Robert Carrae060832016-11-28 10:51:00 -08001162bool Layer::setLayer(int32_t z) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001163 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001164 mCurrentState.sequence++;
1165 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001166 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001167
1168 // Discard all relative layering.
1169 if (mCurrentState.zOrderRelativeOf != nullptr) {
1170 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1171 if (strongRelative != nullptr) {
1172 strongRelative->removeZOrderRelative(this);
1173 }
1174 mCurrentState.zOrderRelativeOf = nullptr;
1175 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001176 setTransactionFlags(eTransactionNeeded);
1177 return true;
1178}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001179
Robert Carrdb66e622017-04-10 16:55:57 -07001180void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1181 mCurrentState.zOrderRelatives.remove(relative);
1182 mCurrentState.sequence++;
1183 mCurrentState.modified = true;
1184 setTransactionFlags(eTransactionNeeded);
1185}
1186
1187void Layer::addZOrderRelative(const wp<Layer>& relative) {
1188 mCurrentState.zOrderRelatives.add(relative);
1189 mCurrentState.modified = true;
1190 mCurrentState.sequence++;
1191 setTransactionFlags(eTransactionNeeded);
1192}
1193
Robert Carr503d2bd2017-12-04 15:49:47 -08001194bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001195 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1196 if (handle == nullptr) {
1197 return false;
1198 }
1199 sp<Layer> relative = handle->owner.promote();
1200 if (relative == nullptr) {
1201 return false;
1202 }
1203
Robert Carr503d2bd2017-12-04 15:49:47 -08001204 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1205 mCurrentState.zOrderRelativeOf == relative) {
1206 return false;
1207 }
1208
Robert Carrdb66e622017-04-10 16:55:57 -07001209 mCurrentState.sequence++;
1210 mCurrentState.modified = true;
Robert Carr503d2bd2017-12-04 15:49:47 -08001211 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001212
chaviw9ab4bd12017-11-03 13:11:00 -07001213 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
1214 if (oldZOrderRelativeOf != nullptr) {
1215 oldZOrderRelativeOf->removeZOrderRelative(this);
1216 }
Robert Carrdb66e622017-04-10 16:55:57 -07001217 mCurrentState.zOrderRelativeOf = relative;
1218 relative->addZOrderRelative(this);
1219
1220 setTransactionFlags(eTransactionNeeded);
1221
1222 return true;
1223}
1224
Mathias Agopian13127d82013-03-05 17:47:11 -08001225bool Layer::setSize(uint32_t w, uint32_t h) {
David Sodman41fdfc92017-11-06 16:09:56 -08001226 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001227 mCurrentState.requested.w = w;
1228 mCurrentState.requested.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001229 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001230 setTransactionFlags(eTransactionNeeded);
1231 return true;
1232}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001233bool Layer::setAlpha(float alpha) {
David Sodman41fdfc92017-11-06 16:09:56 -08001234 if (mCurrentState.color.a == alpha) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001235 mCurrentState.sequence++;
chaviw13fdc492017-06-27 12:40:18 -07001236 mCurrentState.color.a = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001237 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001238 setTransactionFlags(eTransactionNeeded);
1239 return true;
1240}
chaviw13fdc492017-06-27 12:40:18 -07001241
1242bool Layer::setColor(const half3& color) {
David Sodman41fdfc92017-11-06 16:09:56 -08001243 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g &&
1244 color.b == mCurrentState.color.b)
chaviw13fdc492017-06-27 12:40:18 -07001245 return false;
1246
1247 mCurrentState.sequence++;
1248 mCurrentState.color.r = color.r;
1249 mCurrentState.color.g = color.g;
1250 mCurrentState.color.b = color.b;
1251 mCurrentState.modified = true;
1252 setTransactionFlags(eTransactionNeeded);
1253 return true;
1254}
1255
Mathias Agopian13127d82013-03-05 17:47:11 -08001256bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
1257 mCurrentState.sequence++;
David Sodman41fdfc92017-11-06 16:09:56 -08001258 mCurrentState.requested.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001259 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001260 setTransactionFlags(eTransactionNeeded);
1261 return true;
1262}
1263bool Layer::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001264 mCurrentState.requestedTransparentRegion = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001265 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001266 setTransactionFlags(eTransactionNeeded);
1267 return true;
1268}
1269bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1270 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
David Sodman41fdfc92017-11-06 16:09:56 -08001271 if (mCurrentState.flags == newFlags) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001272 mCurrentState.sequence++;
1273 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001274 mCurrentState.mask = mask;
1275 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001276 setTransactionFlags(eTransactionNeeded);
1277 return true;
1278}
Robert Carr99e27f02016-06-16 15:18:02 -07001279
1280bool Layer::setCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001281 if (mCurrentState.requestedCrop == crop) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001282 mCurrentState.sequence++;
Robert Carr99e27f02016-06-16 15:18:02 -07001283 mCurrentState.requestedCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001284 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr99e27f02016-06-16 15:18:02 -07001285 mCurrentState.crop = crop;
1286 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001287 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1288
Dan Stoza7dde5992015-05-22 09:51:44 -07001289 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001290 setTransactionFlags(eTransactionNeeded);
1291 return true;
1292}
Robert Carr8d5227b2017-03-16 15:41:03 -07001293
1294bool Layer::setFinalCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001295 if (mCurrentState.requestedFinalCrop == crop) return false;
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001296 mCurrentState.sequence++;
Robert Carr8d5227b2017-03-16 15:41:03 -07001297 mCurrentState.requestedFinalCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001298 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr8d5227b2017-03-16 15:41:03 -07001299 mCurrentState.finalCrop = crop;
1300 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001301 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1302
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001303 mCurrentState.modified = true;
1304 setTransactionFlags(eTransactionNeeded);
1305 return true;
1306}
Mathias Agopian13127d82013-03-05 17:47:11 -08001307
Robert Carrc3574f72016-03-24 12:19:32 -07001308bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001309 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001310 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001311 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001312 return true;
1313}
1314
rongliuccd34842018-03-14 12:26:23 -07001315void Layer::setInfo(int32_t type, int32_t appId) {
David Sodman41fdfc92017-11-06 16:09:56 -08001316 mCurrentState.appId = appId;
1317 mCurrentState.type = type;
1318 mCurrentState.modified = true;
1319 setTransactionFlags(eTransactionNeeded);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001320}
1321
Mathias Agopian13127d82013-03-05 17:47:11 -08001322bool Layer::setLayerStack(uint32_t layerStack) {
David Sodman41fdfc92017-11-06 16:09:56 -08001323 if (mCurrentState.layerStack == layerStack) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001324 mCurrentState.sequence++;
1325 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001326 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001327 setTransactionFlags(eTransactionNeeded);
1328 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001329}
1330
Peiyong Lin34beb7a2018-03-28 11:57:12 -07001331bool Layer::setDataSpace(ui::Dataspace dataSpace) {
David Sodman41fdfc92017-11-06 16:09:56 -08001332 if (mCurrentState.dataSpace == dataSpace) return false;
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -07001333 mCurrentState.sequence++;
1334 mCurrentState.dataSpace = dataSpace;
1335 mCurrentState.modified = true;
1336 setTransactionFlags(eTransactionNeeded);
1337 return true;
1338}
1339
Peiyong Lin34beb7a2018-03-28 11:57:12 -07001340ui::Dataspace Layer::getDataSpace() const {
Courtney Goeltzenleuchter532b2632017-05-05 16:34:38 -06001341 return mCurrentState.dataSpace;
1342}
1343
Robert Carr1f0a16a2016-10-24 16:27:39 -07001344uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001345 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001346 if (p == nullptr) {
1347 return getDrawingState().layerStack;
1348 }
1349 return p->getLayerStack();
1350}
1351
David Sodman41fdfc92017-11-06 16:09:56 -08001352void Layer::deferTransactionUntil(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001353 mCurrentState.barrierLayer = barrierLayer;
Dan Stoza7dde5992015-05-22 09:51:44 -07001354 mCurrentState.frameNumber = frameNumber;
1355 // We don't set eTransactionNeeded, because just receiving a deferral
1356 // request without any other state updates shouldn't actually induce a delay
1357 mCurrentState.modified = true;
1358 pushPendingState();
Robert Carr0d480722017-01-10 16:42:54 -08001359 mCurrentState.barrierLayer = nullptr;
Dan Stoza792e5292016-02-11 11:43:58 -08001360 mCurrentState.frameNumber = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001361 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001362}
1363
David Sodman41fdfc92017-11-06 16:09:56 -08001364void Layer::deferTransactionUntil(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001365 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
1366 deferTransactionUntil(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001367}
1368
Dan Stozaee44edd2015-03-23 15:50:23 -07001369
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001370// ----------------------------------------------------------------------------
1371// pageflip handling...
1372// ----------------------------------------------------------------------------
1373
Robert Carr1f0a16a2016-10-24 16:27:39 -07001374bool Layer::isHiddenByPolicy() const {
1375 const Layer::State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001376 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001377 if (parent != nullptr && parent->isHiddenByPolicy()) {
1378 return true;
1379 }
1380 return s.flags & layer_state_t::eLayerHidden;
1381}
1382
David Sodman41fdfc92017-11-06 16:09:56 -08001383uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001384 // TODO: should we do something special if mSecure is set?
1385 if (mProtectedByApp) {
1386 // need a hardware-protected path to external video sink
1387 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001388 }
Riley Andrews03414a12014-07-01 14:22:59 -07001389 if (mPotentialCursor) {
1390 usage |= GraphicBuffer::USAGE_CURSOR;
1391 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001392 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001393 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001394}
1395
Mathias Agopian84300952012-11-21 16:02:13 -08001396void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001397 uint32_t orientation = 0;
1398 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001399 // The transform hint is used to improve performance, but we can
1400 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001401 // apply to all displays.
Mathias Agopian42977342012-08-05 00:40:46 -07001402 const Transform& planeTransform(hw->getTransform());
Mathias Agopian4fec8732012-06-29 14:12:52 -07001403 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07001404 if (orientation & Transform::ROT_INVALID) {
1405 orientation = 0;
1406 }
1407 }
David Sodmaneb085e02017-10-05 18:49:04 -07001408 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001409}
1410
Mathias Agopian13127d82013-03-05 17:47:11 -08001411// ----------------------------------------------------------------------------
1412// debugging
1413// ----------------------------------------------------------------------------
1414
Kalle Raitaa099a242017-01-11 11:17:29 -08001415LayerDebugInfo Layer::getLayerDebugInfo() const {
1416 LayerDebugInfo info;
1417 const Layer::State& ds = getDrawingState();
1418 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001419 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001420 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
1421 info.mType = String8(getTypeId());
1422 info.mTransparentRegion = ds.activeTransparentRegion;
1423 info.mVisibleRegion = visibleRegion;
1424 info.mSurfaceDamageRegion = surfaceDamageRegion;
1425 info.mLayerStack = getLayerStack();
1426 info.mX = ds.active.transform.tx();
1427 info.mY = ds.active.transform.ty();
1428 info.mZ = ds.z;
1429 info.mWidth = ds.active.w;
1430 info.mHeight = ds.active.h;
1431 info.mCrop = ds.crop;
1432 info.mFinalCrop = ds.finalCrop;
chaviw13fdc492017-06-27 12:40:18 -07001433 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001434 info.mFlags = ds.flags;
1435 info.mPixelFormat = getPixelFormat();
Peiyong Lin34beb7a2018-03-28 11:57:12 -07001436 info.mDataSpace = static_cast<android_dataspace>(getDataSpace());
Kalle Raitaa099a242017-01-11 11:17:29 -08001437 info.mMatrix[0][0] = ds.active.transform[0][0];
1438 info.mMatrix[0][1] = ds.active.transform[0][1];
1439 info.mMatrix[1][0] = ds.active.transform[1][0];
1440 info.mMatrix[1][1] = ds.active.transform[1][1];
1441 {
David Sodman0cc69182017-11-17 12:12:07 -08001442 sp<const GraphicBuffer> buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001443 if (buffer != 0) {
1444 info.mActiveBufferWidth = buffer->getWidth();
1445 info.mActiveBufferHeight = buffer->getHeight();
1446 info.mActiveBufferStride = buffer->getStride();
1447 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001448 } else {
1449 info.mActiveBufferWidth = 0;
1450 info.mActiveBufferHeight = 0;
1451 info.mActiveBufferStride = 0;
1452 info.mActiveBufferFormat = 0;
1453 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001454 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001455 info.mNumQueuedFrames = getQueuedFrameCount();
1456 info.mRefreshPending = isBufferLatched();
1457 info.mIsOpaque = isOpaque(ds);
1458 info.mContentDirty = contentDirty;
1459 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001460}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001461
Dan Stozae22aec72016-08-01 13:20:59 -07001462void Layer::miniDumpHeader(String8& result) {
1463 result.append("----------------------------------------");
1464 result.append("---------------------------------------\n");
1465 result.append(" Layer name\n");
1466 result.append(" Z | ");
1467 result.append(" Comp Type | ");
1468 result.append(" Disp Frame (LTRB) | ");
1469 result.append(" Source Crop (LTRB)\n");
1470 result.append("----------------------------------------");
1471 result.append("---------------------------------------\n");
1472}
1473
1474void Layer::miniDump(String8& result, int32_t hwcId) const {
David Sodman6f65f3e2017-11-03 14:28:09 -07001475 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stozae22aec72016-08-01 13:20:59 -07001476 return;
1477 }
1478
1479 String8 name;
1480 if (mName.length() > 77) {
1481 std::string shortened;
1482 shortened.append(mName.string(), 36);
1483 shortened.append("[...]");
1484 shortened.append(mName.string() + (mName.length() - 36), 36);
1485 name = shortened.c_str();
1486 } else {
1487 name = mName;
1488 }
1489
1490 result.appendFormat(" %s\n", name.string());
1491
1492 const Layer::State& layerState(getDrawingState());
David Sodman6f65f3e2017-11-03 14:28:09 -07001493 const LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers.at(hwcId);
Chia-I Wu1e043612018-03-01 09:45:09 -08001494 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
1495 result.appendFormat(" rel %6d | ", layerState.z);
1496 } else {
1497 result.appendFormat(" %10d | ", layerState.z);
1498 }
David Sodman41fdfc92017-11-06 16:09:56 -08001499 result.appendFormat("%10s | ", to_string(getCompositionType(hwcId)).c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001500 const Rect& frame = hwcInfo.displayFrame;
David Sodman41fdfc92017-11-06 16:09:56 -08001501 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Dan Stoza5a423ea2017-02-16 14:10:39 -08001502 const FloatRect& crop = hwcInfo.sourceCrop;
David Sodman41fdfc92017-11-06 16:09:56 -08001503 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 -07001504
1505 result.append("- - - - - - - - - - - - - - - - - - - - ");
1506 result.append("- - - - - - - - - - - - - - - - - - - -\n");
1507}
Dan Stozae22aec72016-08-01 13:20:59 -07001508
Svetoslavd85084b2014-03-20 10:28:31 -07001509void Layer::dumpFrameStats(String8& result) const {
1510 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001511}
1512
Svetoslavd85084b2014-03-20 10:28:31 -07001513void Layer::clearFrameStats() {
1514 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001515}
1516
Jamie Gennis6547ff42013-07-16 20:12:42 -07001517void Layer::logFrameStats() {
1518 mFrameTracker.logAndResetStats(mName);
1519}
1520
Svetoslavd85084b2014-03-20 10:28:31 -07001521void Layer::getFrameStats(FrameStats* outStats) const {
1522 mFrameTracker.getStats(outStats);
1523}
1524
Brian Andersond6927fb2016-07-23 23:37:30 -07001525void Layer::dumpFrameEvents(String8& result) {
David Sodman41fdfc92017-11-06 16:09:56 -08001526 result.appendFormat("- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001527 Mutex::Autolock lock(mFrameEventHistoryMutex);
1528 mFrameEventHistory.checkFencesForCompletion();
1529 mFrameEventHistory.dump(result);
1530}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001531
Brian Anderson5ea5e592016-12-01 16:54:33 -08001532void Layer::onDisconnect() {
1533 Mutex::Autolock lock(mFrameEventHistoryMutex);
1534 mFrameEventHistory.onDisconnect();
1535}
1536
Brian Anderson3890c392016-07-25 12:48:08 -07001537void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001538 FrameEventHistoryDelta* outDelta) {
Brian Andersond6927fb2016-07-23 23:37:30 -07001539 Mutex::Autolock lock(mFrameEventHistoryMutex);
1540 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001541 // If there are any unsignaled fences in the aquire timeline at this
1542 // point, the previously queued frame hasn't been latched yet. Go ahead
1543 // and try to get the signal time here so the syscall is taken out of
1544 // the main thread's critical path.
1545 mAcquireTimeline.updateSignalTimes();
1546 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001547 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001548 mFrameEventHistory.addQueue(*newTimestamps);
1549 }
1550
Brian Anderson3890c392016-07-25 12:48:08 -07001551 if (outDelta) {
1552 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001553 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001554}
Dan Stozae77c7662016-05-13 11:37:28 -07001555
Chia-I Wu98f1c102017-05-30 14:54:08 -07001556size_t Layer::getChildrenCount() const {
1557 size_t count = 0;
1558 for (const sp<Layer>& child : mCurrentChildren) {
1559 count += 1 + child->getChildrenCount();
1560 }
1561 return count;
1562}
1563
Robert Carr1f0a16a2016-10-24 16:27:39 -07001564void Layer::addChild(const sp<Layer>& layer) {
1565 mCurrentChildren.add(layer);
1566 layer->setParent(this);
1567}
1568
1569ssize_t Layer::removeChild(const sp<Layer>& layer) {
1570 layer->setParent(nullptr);
1571 return mCurrentChildren.remove(layer);
1572}
1573
Robert Carr1db73f62016-12-21 12:58:51 -08001574bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1575 sp<Handle> handle = nullptr;
1576 sp<Layer> newParent = nullptr;
1577 if (newParentHandle == nullptr) {
1578 return false;
1579 }
1580 handle = static_cast<Handle*>(newParentHandle.get());
1581 newParent = handle->owner.promote();
1582 if (newParent == nullptr) {
1583 ALOGE("Unable to promote Layer handle");
1584 return false;
1585 }
1586
1587 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001588 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001589
1590 sp<Client> client(child->mClientRef.promote());
1591 if (client != nullptr) {
1592 client->setParentLayer(newParent);
1593 }
1594 }
1595 mCurrentChildren.clear();
1596
1597 return true;
1598}
1599
Robert Carr15eae092018-03-23 13:43:53 -07001600void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001601 for (const sp<Layer>& child : mDrawingChildren) {
1602 child->mDrawingParent = newParent;
1603 }
1604}
1605
chaviwf1961f72017-09-18 16:41:07 -07001606bool Layer::reparent(const sp<IBinder>& newParentHandle) {
1607 if (newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001608 return false;
1609 }
1610
1611 auto handle = static_cast<Handle*>(newParentHandle.get());
1612 sp<Layer> newParent = handle->owner.promote();
1613 if (newParent == nullptr) {
1614 ALOGE("Unable to promote Layer handle");
1615 return false;
1616 }
1617
chaviwf1961f72017-09-18 16:41:07 -07001618 sp<Layer> parent = getParent();
1619 if (parent != nullptr) {
1620 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001621 }
chaviwf1961f72017-09-18 16:41:07 -07001622 newParent->addChild(this);
chaviw06178942017-07-27 10:25:59 -07001623
chaviwf1961f72017-09-18 16:41:07 -07001624 sp<Client> client(mClientRef.promote());
chaviw06178942017-07-27 10:25:59 -07001625 sp<Client> newParentClient(newParent->mClientRef.promote());
1626
chaviwf1961f72017-09-18 16:41:07 -07001627 if (client != newParentClient) {
1628 client->setParentLayer(newParent);
chaviw06178942017-07-27 10:25:59 -07001629 }
1630
chaviw06178942017-07-27 10:25:59 -07001631 return true;
1632}
1633
Robert Carr9524cb32017-02-13 11:32:32 -08001634bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001635 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001636 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001637 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001638 if (client != nullptr && parentClient != client) {
Robert Carr7f619b22017-11-06 12:56:35 -08001639 client->detachLayer(child.get());
1640 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001641 }
Robert Carr7f619b22017-11-06 12:56:35 -08001642 }
Robert Carr9524cb32017-02-13 11:32:32 -08001643
1644 return true;
1645}
1646
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001647// Dataspace::UNKNOWN, Dataspace::SRGB, Dataspace::SRGB_LINEAR,
1648// Dataspace::V0_SRGB and Dataspace::V0_SRGB_LINEAR are considered legacy
1649// SRGB data space for now.
1650// Note that Dataspace::V0_SRGB and Dataspace::V0_SRGB_LINEAR are not legacy
1651// data space, however since framework doesn't distinguish them out of legacy
1652// SRGB, we have to treat them as the same for now.
1653bool Layer::isLegacySrgbDataSpace() const {
1654 // TODO(lpy) b/77652630, need to figure out when UNKNOWN can be treated as SRGB.
1655 return mDrawingState.dataSpace == ui::Dataspace::UNKNOWN ||
1656 mDrawingState.dataSpace == ui::Dataspace::SRGB ||
1657 mDrawingState.dataSpace == ui::Dataspace::SRGB_LINEAR ||
1658 mDrawingState.dataSpace == ui::Dataspace::V0_SRGB ||
1659 mDrawingState.dataSpace == ui::Dataspace::V0_SRGB_LINEAR;
1660}
1661
Robert Carr1f0a16a2016-10-24 16:27:39 -07001662void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001663 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001664}
1665
1666void Layer::clearSyncPoints() {
1667 for (const auto& child : mCurrentChildren) {
1668 child->clearSyncPoints();
1669 }
1670
1671 Mutex::Autolock lock(mLocalSyncPointMutex);
1672 for (auto& point : mLocalSyncPoints) {
1673 point->setFrameAvailable();
1674 }
1675 mLocalSyncPoints.clear();
1676}
1677
1678int32_t Layer::getZ() const {
1679 return mDrawingState.z;
1680}
1681
Robert Carr29abff82017-12-04 13:51:20 -08001682bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1683 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1684 const State& state = useDrawing ? mDrawingState : mCurrentState;
1685 return state.zOrderRelativeOf != nullptr;
1686}
1687
David Sodman41fdfc92017-11-06 16:09:56 -08001688__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001689 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001690 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1691 "makeTraversalList received invalid stateSet");
1692 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1693 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1694 const State& state = useDrawing ? mDrawingState : mCurrentState;
1695
Robert Carr29abff82017-12-04 13:51:20 -08001696 if (state.zOrderRelatives.size() == 0) {
1697 *outSkipRelativeZUsers = true;
1698 return children;
1699 }
1700
Robert Carrdb66e622017-04-10 16:55:57 -07001701 LayerVector traverse;
Dan Stoza412903f2017-04-27 13:42:17 -07001702 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001703 sp<Layer> strongRelative = weakRelative.promote();
1704 if (strongRelative != nullptr) {
1705 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001706 }
1707 }
1708
Dan Stoza412903f2017-04-27 13:42:17 -07001709 for (const sp<Layer>& child : children) {
Robert Carr503c7042017-09-27 15:06:08 -07001710 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1711 if (childState.zOrderRelativeOf != nullptr) {
1712 continue;
1713 }
Robert Carrdb66e622017-04-10 16:55:57 -07001714 traverse.add(child);
1715 }
1716
1717 return traverse;
1718}
1719
Robert Carr1f0a16a2016-10-24 16:27:39 -07001720/**
Robert Carrdb66e622017-04-10 16:55:57 -07001721 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001722 */
Dan Stoza412903f2017-04-27 13:42:17 -07001723void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001724 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1725 // produce a new list for traversing, including our relatives, and not including our children
1726 // who are relatives of another surface. In the case that there are no relative Z,
1727 // makeTraversalList returns our children directly to avoid significant overhead.
1728 // However in this case we need to take the responsibility for filtering children which
1729 // are relatives of another surface here.
1730 bool skipRelativeZUsers = false;
1731 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001732
Robert Carr1f0a16a2016-10-24 16:27:39 -07001733 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001734 for (; i < list.size(); i++) {
1735 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001736 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1737 continue;
1738 }
1739
Robert Carrdb66e622017-04-10 16:55:57 -07001740 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001741 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001742 }
Dan Stoza412903f2017-04-27 13:42:17 -07001743 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001744 }
Robert Carr29abff82017-12-04 13:51:20 -08001745
Dan Stoza412903f2017-04-27 13:42:17 -07001746 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001747 for (; i < list.size(); i++) {
1748 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001749
1750 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1751 continue;
1752 }
Dan Stoza412903f2017-04-27 13:42:17 -07001753 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001754 }
1755}
1756
1757/**
Robert Carrdb66e622017-04-10 16:55:57 -07001758 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001759 */
Dan Stoza412903f2017-04-27 13:42:17 -07001760void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1761 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001762 // See traverseInZOrder for documentation.
1763 bool skipRelativeZUsers = false;
1764 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001765
Robert Carr1f0a16a2016-10-24 16:27:39 -07001766 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001767 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001768 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001769
1770 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1771 continue;
1772 }
1773
Robert Carrdb66e622017-04-10 16:55:57 -07001774 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001775 break;
1776 }
Dan Stoza412903f2017-04-27 13:42:17 -07001777 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001778 }
Dan Stoza412903f2017-04-27 13:42:17 -07001779 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001780 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001781 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001782
1783 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1784 continue;
1785 }
1786
Dan Stoza412903f2017-04-27 13:42:17 -07001787 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001788 }
1789}
1790
chaviw4b129c22018-04-09 16:19:43 -07001791LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1792 const std::vector<Layer*>& layersInTree) {
1793 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1794 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001795 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1796 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
chaviw4b129c22018-04-09 16:19:43 -07001797 const State& state = useDrawing ? mDrawingState : mCurrentState;
1798
1799 LayerVector traverse;
1800 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1801 sp<Layer> strongRelative = weakRelative.promote();
1802 // Only add relative layers that are also descendents of the top most parent of the tree.
1803 // If a relative layer is not a descendent, then it should be ignored.
1804 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1805 traverse.add(strongRelative);
1806 }
1807 }
1808
1809 for (const sp<Layer>& child : children) {
1810 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1811 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1812 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1813 // the child here since it won't be added later as a relative.
1814 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1815 childState.zOrderRelativeOf.promote().get())) {
1816 continue;
1817 }
1818 traverse.add(child);
1819 }
1820
1821 return traverse;
1822}
1823
1824void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1825 LayerVector::StateSet stateSet,
1826 const LayerVector::Visitor& visitor) {
1827 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001828
1829 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001830 for (; i < list.size(); i++) {
1831 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001832 if (relative->getZ() >= 0) {
1833 break;
1834 }
chaviw4b129c22018-04-09 16:19:43 -07001835 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001836 }
chaviw4b129c22018-04-09 16:19:43 -07001837
chaviwa76b2712017-09-20 12:02:26 -07001838 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001839 for (; i < list.size(); i++) {
1840 const auto& relative = list[i];
1841 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001842 }
1843}
1844
chaviw4b129c22018-04-09 16:19:43 -07001845std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1846 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1847 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1848
1849 std::vector<Layer*> layersInTree = {this};
1850 for (size_t i = 0; i < children.size(); i++) {
1851 const auto& child = children[i];
1852 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1853 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1854 }
1855
1856 return layersInTree;
1857}
1858
1859void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1860 const LayerVector::Visitor& visitor) {
1861 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1862 std::sort(layersInTree.begin(), layersInTree.end());
1863 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1864}
1865
Robert Carr1f0a16a2016-10-24 16:27:39 -07001866Transform Layer::getTransform() const {
1867 Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001868 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001869 if (p != nullptr) {
1870 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07001871
1872 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
1873 // it isFixedSize) then there may be additional scaling not accounted
1874 // for in the transform. We need to mirror this scaling in child surfaces
1875 // or we will break the contract where WM can treat child surfaces as
1876 // pixels in the parent surface.
David Sodman0cc69182017-11-17 12:12:07 -08001877 if (p->isFixedSize() && p->getBE().compositionInfo.mBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07001878 int bufferWidth;
1879 int bufferHeight;
1880 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
David Sodman0cc69182017-11-17 12:12:07 -08001881 bufferWidth = p->getBE().compositionInfo.mBuffer->getWidth();
1882 bufferHeight = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001883 } else {
David Sodman0cc69182017-11-17 12:12:07 -08001884 bufferHeight = p->getBE().compositionInfo.mBuffer->getWidth();
1885 bufferWidth = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001886 }
David Sodman41fdfc92017-11-06 16:09:56 -08001887 float sx = p->getDrawingState().active.w / static_cast<float>(bufferWidth);
1888 float sy = p->getDrawingState().active.h / static_cast<float>(bufferHeight);
Robert Carr9b429f42017-04-17 14:56:57 -07001889 Transform extraParentScaling;
1890 extraParentScaling.set(sx, 0, 0, sy);
1891 t = t * extraParentScaling;
1892 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001893 }
1894 return t * getDrawingState().active.transform;
1895}
1896
chaviw13fdc492017-06-27 12:40:18 -07001897half Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001898 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07001899
chaviw13fdc492017-06-27 12:40:18 -07001900 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1901 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001902}
Robert Carr6452f122017-03-21 10:41:29 -07001903
chaviw13fdc492017-06-27 12:40:18 -07001904half4 Layer::getColor() const {
1905 const half4 color(getDrawingState().color);
1906 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001907}
Robert Carr6452f122017-03-21 10:41:29 -07001908
Robert Carr1f0a16a2016-10-24 16:27:39 -07001909void Layer::commitChildList() {
1910 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1911 const auto& child = mCurrentChildren[i];
1912 child->commitChildList();
1913 }
1914 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001915 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001916}
1917
chaviw1d044282017-09-27 12:19:28 -07001918void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
1919 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1920 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1921 const State& state = useDrawing ? mDrawingState : mCurrentState;
1922
1923 Transform requestedTransform = state.active.transform;
1924 Transform transform = getTransform();
1925
1926 layerInfo->set_id(sequence);
1927 layerInfo->set_name(getName().c_str());
1928 layerInfo->set_type(String8(getTypeId()));
1929
1930 for (const auto& child : children) {
1931 layerInfo->add_children(child->sequence);
1932 }
1933
1934 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1935 sp<Layer> strongRelative = weakRelative.promote();
1936 if (strongRelative != nullptr) {
1937 layerInfo->add_relatives(strongRelative->sequence);
1938 }
1939 }
1940
1941 LayerProtoHelper::writeToProto(state.activeTransparentRegion,
1942 layerInfo->mutable_transparent_region());
1943 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
1944 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
1945
1946 layerInfo->set_layer_stack(getLayerStack());
1947 layerInfo->set_z(state.z);
1948
1949 PositionProto* position = layerInfo->mutable_position();
1950 position->set_x(transform.tx());
1951 position->set_y(transform.ty());
1952
1953 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
1954 requestedPosition->set_x(requestedTransform.tx());
1955 requestedPosition->set_y(requestedTransform.ty());
1956
1957 SizeProto* size = layerInfo->mutable_size();
1958 size->set_w(state.active.w);
1959 size->set_h(state.active.h);
1960
1961 LayerProtoHelper::writeToProto(state.crop, layerInfo->mutable_crop());
1962 LayerProtoHelper::writeToProto(state.finalCrop, layerInfo->mutable_final_crop());
1963
1964 layerInfo->set_is_opaque(isOpaque(state));
1965 layerInfo->set_invalidate(contentDirty);
Peiyong Lin34beb7a2018-03-28 11:57:12 -07001966 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(getDataSpace())));
chaviw1d044282017-09-27 12:19:28 -07001967 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1968 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
1969 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
1970 layerInfo->set_flags(state.flags);
1971
1972 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1973 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
1974
Jorim Jaggi8e0af362017-11-14 16:28:28 +01001975 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07001976 if (parent != nullptr) {
1977 layerInfo->set_parent(parent->sequence);
1978 }
1979
1980 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1981 if (zOrderRelativeOf != nullptr) {
1982 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1983 }
1984
David Sodman0cc69182017-11-17 12:12:07 -08001985 auto buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001986 if (buffer != nullptr) {
1987 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
chaviw1d044282017-09-27 12:19:28 -07001988 }
1989
1990 layerInfo->set_queued_frames(getQueuedFrameCount());
1991 layerInfo->set_refresh_pending(isBufferLatched());
rongliuccd34842018-03-14 12:26:23 -07001992 layerInfo->set_window_type(state.type);
1993 layerInfo->set_app_id(state.appId);
chaviw1d044282017-09-27 12:19:28 -07001994}
1995
Yiwei Zhang068e31b2018-02-21 13:02:45 -08001996void Layer::writeToProto(LayerProto* layerInfo, int32_t hwcId) {
1997 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
1998
1999 const auto& hwcInfo = getBE().mHwcLayers.at(hwcId);
2000
2001 const Rect& frame = hwcInfo.displayFrame;
2002 LayerProtoHelper::writeToProto(frame, layerInfo->mutable_hwc_frame());
2003
2004 const FloatRect& crop = hwcInfo.sourceCrop;
2005 LayerProtoHelper::writeToProto(crop, layerInfo->mutable_hwc_crop());
2006
2007 const int32_t transform = static_cast<int32_t>(hwcInfo.transform);
2008 layerInfo->set_hwc_transform(transform);
Yiwei Zhang7c64f172018-03-07 14:52:28 -08002009
2010 const int32_t compositionType = static_cast<int32_t>(hwcInfo.compositionType);
2011 layerInfo->set_hwc_composition_type(compositionType);
2012
2013 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
2014 static_cast<BufferLayer*>(this)->isProtected()) {
2015 layerInfo->set_is_protected(true);
2016 } else {
2017 layerInfo->set_is_protected(false);
2018 }
Yiwei Zhang068e31b2018-02-21 13:02:45 -08002019}
2020
Mathias Agopian13127d82013-03-05 17:47:11 -08002021// ---------------------------------------------------------------------------
2022
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002023}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002024
2025#if defined(__gl_h_)
2026#error "don't include gl/gl.h in this file"
2027#endif
2028
2029#if defined(__gl2_h_)
2030#error "don't include gl2/gl2.h in this file"
2031#endif