blob: 610423047b1473d03ccc37ac2e0f928ceadff134 [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>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026
Mathias Agopiana67932f2011-04-20 14:20:59 -070027#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070028#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070029#include <cutils/properties.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030
31#include <utils/Errors.h>
32#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080033#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080034#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080035#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036
Courtney Goeltzenleuchter36c44dc2017-04-14 09:33:16 -060037#include <ui/DebugUtils.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070038#include <ui/GraphicBuffer.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039#include <ui/PixelFormat.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080040
Dan Stoza6b9454d2014-11-07 16:00:59 -080041#include <gui/BufferItem.h>
Kalle Raitaa099a242017-01-11 11:17:29 -080042#include <gui/LayerDebugInfo.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080043#include <gui/Surface.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044
Mathias Agopian3e25fd82013-04-22 17:52:16 +020045#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070046#include "DisplayDevice.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047#include "Layer.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070048#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070049#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050#include "SurfaceFlinger.h"
David Sodman41fdfc92017-11-06 16:09:56 -080051#include "clz.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052
Mathias Agopian1b031492012-06-20 17:51:20 -070053#include "DisplayHardware/HWComposer.h"
54
Mathias Agopian875d8e12013-06-07 15:35:48 -070055#include "RenderEngine/RenderEngine.h"
56
Dan Stozac5da2712016-07-20 15:38:12 -070057#include <mutex>
chaviw1d044282017-09-27 12:19:28 -070058#include "LayerProtoHelper.h"
Dan Stozac5da2712016-07-20 15:38:12 -070059
David Sodman41fdfc92017-11-06 16:09:56 -080060#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062namespace android {
63
David Sodman9eeae692017-11-02 10:53:32 -070064LayerBE::LayerBE()
David Sodman0cc69182017-11-17 12:12:07 -080065 : mMesh(Mesh::TRIANGLE_FAN, 4, 2, 2) {
David Sodman9eeae692017-11-02 10:53:32 -070066}
67
68
Mathias Agopian13127d82013-03-05 17:47:11 -080069int32_t Layer::sSequence = 1;
70
David Sodman41fdfc92017-11-06 16:09:56 -080071Layer::Layer(SurfaceFlinger* flinger, const sp<Client>& client, const String8& name, uint32_t w,
72 uint32_t h, uint32_t flags)
David Sodman0c69cad2017-08-21 12:12:51 -070073 : contentDirty(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080074 sequence(uint32_t(android_atomic_inc(&sSequence))),
75 mFlinger(flinger),
Mathias Agopian13127d82013-03-05 17:47:11 -080076 mPremultipliedAlpha(true),
David Sodman0c69cad2017-08-21 12:12:51 -070077 mName(name),
Mathias Agopian13127d82013-03-05 17:47:11 -080078 mTransactionFlags(0),
Dan Stoza7dde5992015-05-22 09:51:44 -070079 mPendingStateMutex(),
80 mPendingStates(),
Mathias Agopiana67932f2011-04-20 14:20:59 -070081 mQueuedFrames(0),
Jesse Hall399184a2014-03-03 15:42:54 -080082 mSidebandStreamChanged(false),
Mathias Agopiana9347642017-02-13 16:42:28 -080083 mActiveBufferSlot(BufferQueue::INVALID_BUFFER_SLOT),
Mathias Agopiana67932f2011-04-20 14:20:59 -070084 mCurrentTransform(0),
Robert Carrc3574f72016-03-24 12:19:32 -070085 mOverrideScalingMode(-1),
Mathias Agopiana67932f2011-04-20 14:20:59 -070086 mCurrentOpacity(true),
Dan Stozacac35382016-01-27 12:21:06 -080087 mCurrentFrameNumber(0),
Mathias Agopian82d7ab62012-01-19 18:34:40 -080088 mFrameLatencyNeeded(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080089 mFiltering(false),
90 mNeedsFiltering(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080091 mProtectedByApp(false),
Riley Andrews03414a12014-07-01 14:22:59 -070092 mClientRef(client),
Dan Stozaa4650a52015-05-12 12:56:16 -070093 mPotentialCursor(false),
94 mQueueItemLock(),
95 mQueueItemCondition(),
96 mQueueItems(),
Dan Stoza65476f32015-05-14 09:27:25 -070097 mLastFrameNumberReceived(0),
Robert Carr82364e32016-05-15 11:27:47 -070098 mAutoRefresh(false),
David Sodman41fdfc92017-11-06 16:09:56 -080099 mFreezeGeometryUpdates(false) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800100
Mathias Agopiana67932f2011-04-20 14:20:59 -0700101 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700102
103 uint32_t layerFlags = 0;
David Sodman41fdfc92017-11-06 16:09:56 -0800104 if (flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
105 if (flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
106 if (flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700107
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700108 mName = name;
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700109 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700110
111 mCurrentState.active.w = w;
112 mCurrentState.active.h = h;
David Sodman0c69cad2017-08-21 12:12:51 -0700113 mCurrentState.flags = layerFlags;
Robert Carr3dcabfa2016-03-01 18:36:58 -0800114 mCurrentState.active.transform.set(0, 0);
Robert Carrb5d3d262016-03-25 15:08:13 -0700115 mCurrentState.crop.makeInvalid();
116 mCurrentState.finalCrop.makeInvalid();
Robert Carr7bf247e2017-05-18 14:02:49 -0700117 mCurrentState.requestedFinalCrop = mCurrentState.finalCrop;
118 mCurrentState.requestedCrop = mCurrentState.crop;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700119 mCurrentState.z = 0;
chaviw13fdc492017-06-27 12:40:18 -0700120 mCurrentState.color.a = 1.0f;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700121 mCurrentState.layerStack = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700122 mCurrentState.sequence = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700123 mCurrentState.requested = mCurrentState.active;
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700124 mCurrentState.dataSpace = HAL_DATASPACE_UNKNOWN;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500125 mCurrentState.appId = 0;
126 mCurrentState.type = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700127
128 // drawing state & current state are identical
129 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700130
Dan Stoza9e56aa02015-11-02 13:00:03 -0800131 const auto& hwc = flinger->getHwComposer();
132 const auto& activeConfig = hwc.getActiveConfig(HWC_DISPLAY_PRIMARY);
133 nsecs_t displayPeriod = activeConfig->getVsyncPeriod();
Jamie Gennis6547ff42013-07-16 20:12:42 -0700134 mFrameTracker.setDisplayRefreshPeriod(displayPeriod);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800135
136 CompositorTiming compositorTiming;
137 flinger->getCompositorTiming(&compositorTiming);
138 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
David Sodman9eeae692017-11-02 10:53:32 -0700139
Jamie Gennise8696a42012-01-15 18:54:57 -0800140}
141
David Sodman41fdfc92017-11-06 16:09:56 -0800142void Layer::onFirstRef() {}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700143
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700144Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800145 sp<Client> c(mClientRef.promote());
146 if (c != 0) {
147 c->detachLayer(this);
148 }
149
150 for (auto& point : mRemoteSyncPoints) {
151 point->setTransactionApplied();
152 }
153 for (auto& point : mLocalSyncPoints) {
154 point->setFrameAvailable();
155 }
Jamie Gennis6547ff42013-07-16 20:12:42 -0700156 mFrameTracker.logAndResetStats(mName);
Mathias Agopian96f08192010-06-02 23:28:45 -0700157}
158
Mathias Agopian13127d82013-03-05 17:47:11 -0800159// ---------------------------------------------------------------------------
160// callbacks
161// ---------------------------------------------------------------------------
162
David Sodmaneb085e02017-10-05 18:49:04 -0700163/*
164 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
165 * Layer. So, the implementation is done in BufferLayer. When called on a
166 * ColorLayer object, it's essentially a NOP.
167 */
David Sodmaneb085e02017-10-05 18:49:04 -0700168void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800169
Chia-I Wuc6657022017-08-15 11:18:17 -0700170void Layer::onRemovedFromCurrentState() {
171 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
172
chaviw8b3871a2017-11-01 17:41:01 -0700173 mPendingRemoval = true;
174
Robert Carr5edb1ad2017-04-25 10:54:24 -0700175 if (mCurrentState.zOrderRelativeOf != nullptr) {
176 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
177 if (strongRelative != nullptr) {
178 strongRelative->removeZOrderRelative(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700179 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700180 }
181 mCurrentState.zOrderRelativeOf = nullptr;
182 }
183
Chia-I Wuc6657022017-08-15 11:18:17 -0700184 for (const auto& child : mCurrentChildren) {
185 child->onRemovedFromCurrentState();
186 }
187}
Chia-I Wu38512252017-05-17 14:36:16 -0700188
Chia-I Wuc6657022017-08-15 11:18:17 -0700189void Layer::onRemoved() {
190 // the layer is removed from SF mLayersPendingRemoval
David Sodmaneb085e02017-10-05 18:49:04 -0700191 abandon();
Chia-I Wuc6657022017-08-15 11:18:17 -0700192
Steven Thomasb02664d2017-07-26 18:48:28 -0700193 destroyAllHwcLayers();
Chia-I Wu38512252017-05-17 14:36:16 -0700194
Robert Carr1f0a16a2016-10-24 16:27:39 -0700195 for (const auto& child : mCurrentChildren) {
196 child->onRemoved();
197 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700198}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700199
Mathias Agopian13127d82013-03-05 17:47:11 -0800200// ---------------------------------------------------------------------------
201// set-up
202// ---------------------------------------------------------------------------
203
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700204const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800205 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206}
207
chaviw13fdc492017-06-27 12:40:18 -0700208bool Layer::getPremultipledAlpha() const {
209 return mPremultipliedAlpha;
210}
211
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700212sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800213 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700214 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800215}
216
217// ---------------------------------------------------------------------------
218// h/w composer set-up
219// ---------------------------------------------------------------------------
220
Steven Thomasb02664d2017-07-26 18:48:28 -0700221bool Layer::createHwcLayer(HWComposer* hwc, int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700222 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.count(hwcId) != 0,
David Sodman9eeae692017-11-02 10:53:32 -0700223 "Already have a layer for hwcId %d", hwcId);
Steven Thomasb02664d2017-07-26 18:48:28 -0700224 HWC2::Layer* layer = hwc->createLayer(hwcId);
225 if (!layer) {
226 return false;
227 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700228 LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers[hwcId];
Steven Thomasb02664d2017-07-26 18:48:28 -0700229 hwcInfo.hwc = hwc;
230 hwcInfo.layer = layer;
231 layer->setLayerDestroyedListener(
David Sodman6f65f3e2017-11-03 14:28:09 -0700232 [this, hwcId](HWC2::Layer* /*layer*/) { getBE().mHwcLayers.erase(hwcId); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700233 return true;
234}
235
Chia-I Wu83806892017-11-16 10:50:20 -0800236bool Layer::destroyHwcLayer(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700237 if (getBE().mHwcLayers.count(hwcId) == 0) {
Chia-I Wu83806892017-11-16 10:50:20 -0800238 return false;
Steven Thomasb02664d2017-07-26 18:48:28 -0700239 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700240 auto& hwcInfo = getBE().mHwcLayers[hwcId];
David Sodman41fdfc92017-11-06 16:09:56 -0800241 LOG_ALWAYS_FATAL_IF(hwcInfo.layer == nullptr, "Attempt to destroy null layer");
Steven Thomasb02664d2017-07-26 18:48:28 -0700242 LOG_ALWAYS_FATAL_IF(hwcInfo.hwc == nullptr, "Missing HWComposer");
243 hwcInfo.hwc->destroyLayer(hwcId, hwcInfo.layer);
244 // The layer destroyed listener should have cleared the entry from
245 // mHwcLayers. Verify that.
David Sodman6f65f3e2017-11-03 14:28:09 -0700246 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.count(hwcId) != 0,
247 "Stale layer entry in getBE().mHwcLayers");
Chia-I Wu83806892017-11-16 10:50:20 -0800248 return true;
Steven Thomasb02664d2017-07-26 18:48:28 -0700249}
250
251void Layer::destroyAllHwcLayers() {
David Sodman6f65f3e2017-11-03 14:28:09 -0700252 size_t numLayers = getBE().mHwcLayers.size();
Steven Thomasb02664d2017-07-26 18:48:28 -0700253 for (size_t i = 0; i < numLayers; ++i) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700254 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.empty(), "destroyAllHwcLayers failed");
255 destroyHwcLayer(getBE().mHwcLayers.begin()->first);
Steven Thomasb02664d2017-07-26 18:48:28 -0700256 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700257 LOG_ALWAYS_FATAL_IF(!getBE().mHwcLayers.empty(),
David Sodman41fdfc92017-11-06 16:09:56 -0800258 "All hardware composer layers should have been destroyed");
Steven Thomasb02664d2017-07-26 18:48:28 -0700259}
Steven Thomasb02664d2017-07-26 18:48:28 -0700260
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800261Rect Layer::getContentCrop() const {
262 // this is the crop rectangle that applies to the buffer
263 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700264 Rect crop;
265 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800266 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700267 crop = mCurrentCrop;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800268 } else if (getBE().compositionInfo.mBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800269 // otherwise we use the whole buffer
David Sodman0cc69182017-11-17 12:12:07 -0800270 crop = getBE().compositionInfo.mBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700271 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800272 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700273 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700274 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700275 return crop;
276}
277
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700278static Rect reduce(const Rect& win, const Region& exclude) {
279 if (CC_LIKELY(exclude.isEmpty())) {
280 return win;
281 }
282 if (exclude.isRect()) {
283 return win.reduce(exclude.getBounds());
284 }
285 return Region(win).subtract(exclude).getBounds();
286}
287
Dan Stoza80d61162017-12-20 15:57:52 -0800288static FloatRect reduce(const FloatRect& win, const Region& exclude) {
289 if (CC_LIKELY(exclude.isEmpty())) {
290 return win;
291 }
292 // Convert through Rect (by rounding) for lack of FloatRegion
293 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
294}
295
Robert Carr1f0a16a2016-10-24 16:27:39 -0700296Rect Layer::computeScreenBounds(bool reduceTransparentRegion) const {
297 const Layer::State& s(getDrawingState());
298 Rect win(s.active.w, s.active.h);
299
300 if (!s.crop.isEmpty()) {
301 win.intersect(s.crop, &win);
302 }
303
304 Transform t = getTransform();
305 win = t.transform(win);
306
Robert Carr41b08b52017-06-01 16:11:34 -0700307 if (!s.finalCrop.isEmpty()) {
308 win.intersect(s.finalCrop, &win);
309 }
310
Chia-I Wue41dbe62017-06-13 14:10:56 -0700311 const sp<Layer>& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700312 // Now we need to calculate the parent bounds, so we can clip ourselves to those.
313 // When calculating the parent bounds for purposes of clipping,
314 // we don't need to constrain the parent to its transparent region.
315 // The transparent region is an optimization based on the
316 // buffer contents of the layer, but does not affect the space allocated to
317 // it by policy, and thus children should be allowed to extend into the
318 // parent's transparent region. In fact one of the main uses, is to reduce
319 // buffer allocation size in cases where a child window sits behind a main window
320 // (by marking the hole in the parent window as a transparent region)
321 if (p != nullptr) {
322 Rect bounds = p->computeScreenBounds(false);
323 bounds.intersect(win, &win);
324 }
325
326 if (reduceTransparentRegion) {
327 auto const screenTransparentRegion = t.transform(s.activeTransparentRegion);
328 win = reduce(win, screenTransparentRegion);
329 }
330
331 return win;
332}
333
Dan Stoza80d61162017-12-20 15:57:52 -0800334FloatRect Layer::computeBounds() const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700335 const Layer::State& s(getDrawingState());
Michael Lentine6c925ed2014-09-26 17:55:01 -0700336 return computeBounds(s.activeTransparentRegion);
337}
338
Dan Stoza80d61162017-12-20 15:57:52 -0800339FloatRect Layer::computeBounds(const Region& activeTransparentRegion) const {
Michael Lentine6c925ed2014-09-26 17:55:01 -0700340 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800341 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700342
343 if (!s.crop.isEmpty()) {
344 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800345 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700346
347 Rect bounds = win;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700348 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700349 if (p != nullptr) {
Robert Carrde9ec442017-02-08 17:43:36 -0800350 // Look in computeScreenBounds recursive call for explanation of
351 // why we pass false here.
352 bounds = p->computeScreenBounds(false /* reduceTransparentRegion */);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700353 }
354
355 Transform t = getTransform();
Dan Stoza80d61162017-12-20 15:57:52 -0800356
357 FloatRect floatWin = win.toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700358 if (p != nullptr) {
Dan Stoza80d61162017-12-20 15:57:52 -0800359 floatWin = t.transform(floatWin);
360 floatWin = floatWin.intersect(bounds.toFloatRect());
361 floatWin = t.inverse().transform(floatWin);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700362 }
363
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700364 // subtract the transparent region and snap to the bounds
Dan Stoza80d61162017-12-20 15:57:52 -0800365 return reduce(floatWin, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800366}
367
Robert Carr1f0a16a2016-10-24 16:27:39 -0700368Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& hw) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700369 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800370 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700371 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800372
373 // apply the projection's clipping to the window crop in
374 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700375 // if there are no window scaling involved, this operation will map to full
376 // pixels in the buffer.
377 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
378 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700379
380 Rect activeCrop(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700381 if (!s.crop.isEmpty()) {
Chia-I Wudf7867f2017-07-20 14:24:37 -0700382 activeCrop.intersect(s.crop, &activeCrop);
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700383 }
384
Robert Carr1f0a16a2016-10-24 16:27:39 -0700385 Transform t = getTransform();
386 activeCrop = t.transform(activeCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000387 if (!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
388 activeCrop.clear();
389 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700390 if (!s.finalCrop.isEmpty()) {
David Sodman41fdfc92017-11-06 16:09:56 -0800391 if (!activeCrop.intersect(s.finalCrop, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000392 activeCrop.clear();
393 }
394 }
chaviwb1154d12017-10-31 14:15:36 -0700395
396 const auto& p = mDrawingParent.promote();
397 if (p != nullptr) {
398 auto parentCrop = p->computeInitialCrop(hw);
399 activeCrop.intersect(parentCrop, &activeCrop);
400 }
401
Robert Carr1f0a16a2016-10-24 16:27:39 -0700402 return activeCrop;
403}
404
Dan Stoza5a423ea2017-02-16 14:10:39 -0800405FloatRect Layer::computeCrop(const sp<const DisplayDevice>& hw) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700406 // the content crop is the area of the content that gets scaled to the
407 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800408 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700409
410 // In addition there is a WM-specified crop we pull from our drawing state.
411 const State& s(getDrawingState());
412
413 // Screen space to make reduction to parent crop clearer.
414 Rect activeCrop = computeInitialCrop(hw);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700415 Transform t = getTransform();
416 // Back to layer space to work with the content crop.
417 activeCrop = t.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800418
Michael Lentine28ea2172014-11-19 18:32:37 -0800419 // This needs to be here as transform.transform(Rect) computes the
420 // transformed rect and then takes the bounding box of the result before
421 // returning. This means
422 // transform.inverse().transform(transform.transform(Rect)) != Rect
423 // in which case we need to make sure the final rect is clipped to the
424 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000425 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
426 activeCrop.clear();
427 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800428
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700429 // subtract the transparent region and snap to the bounds
430 activeCrop = reduce(activeCrop, s.activeTransparentRegion);
431
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000432 // Transform the window crop to match the buffer coordinate system,
433 // which means using the inverse of the current transform set on the
434 // SurfaceFlingerConsumer.
435 uint32_t invTransform = mCurrentTransform;
Robert Carrcae605c2017-03-29 12:10:31 -0700436 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000437 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700438 * the code below applies the primary display's inverse transform to the
439 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000440 */
David Sodman41fdfc92017-11-06 16:09:56 -0800441 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000442 // calculate the inverse transform
443 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800444 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800445 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000446 // and apply to the current transform
David Sodman41fdfc92017-11-06 16:09:56 -0800447 invTransform = (Transform(invTransformOrient) * Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800448 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000449
450 int winWidth = s.active.w;
451 int winHeight = s.active.h;
452 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
453 // If the activeCrop has been rotate the ends are rotated but not
454 // the space itself so when transforming ends back we can't rely on
455 // a modification of the axes of rotation. To account for this we
456 // need to reorient the inverse rotation in terms of the current
457 // axes of rotation.
458 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
459 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
460 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800461 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000462 }
463 winWidth = s.active.h;
464 winHeight = s.active.w;
465 }
David Sodman41fdfc92017-11-06 16:09:56 -0800466 const Rect winCrop = activeCrop.transform(invTransform, s.active.w, s.active.h);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000467
468 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800469 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000470 float yScale = crop.getHeight() / float(winHeight);
471
David Sodman41fdfc92017-11-06 16:09:56 -0800472 float insetL = winCrop.left * xScale;
473 float insetT = winCrop.top * yScale;
474 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000475 float insetB = (winHeight - winCrop.bottom) * yScale;
476
David Sodman41fdfc92017-11-06 16:09:56 -0800477 crop.left += insetL;
478 crop.top += insetT;
479 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000480 crop.bottom -= insetB;
481
Mathias Agopian13127d82013-03-05 17:47:11 -0800482 return crop;
483}
484
Robert Carrae060832016-11-28 10:51:00 -0800485void Layer::setGeometry(const sp<const DisplayDevice>& displayDevice, uint32_t z)
Mathias Agopiana350ff92010-08-10 17:14:02 -0700486{
Dan Stoza9e56aa02015-11-02 13:00:03 -0800487 const auto hwcId = displayDevice->getHwcDisplayId();
David Sodman6f65f3e2017-11-03 14:28:09 -0700488 auto& hwcInfo = getBE().mHwcLayers[hwcId];
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700489
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700490 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800491 hwcInfo.forceClientComposition = false;
492
493 if (isSecure() && !displayDevice->isSecure()) {
494 hwcInfo.forceClientComposition = true;
495 }
496
497 auto& hwcLayer = hwcInfo.layer;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700498
Mathias Agopian13127d82013-03-05 17:47:11 -0800499 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700500 const State& s(getDrawingState());
David Revemanecf0fa52017-03-03 11:32:44 -0500501 auto blendMode = HWC2::BlendMode::None;
Robert Carr6452f122017-03-21 10:41:29 -0700502 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800503 blendMode =
504 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800505 }
David Revemanecf0fa52017-03-03 11:32:44 -0500506 auto error = hwcLayer->setBlendMode(blendMode);
David Sodman41fdfc92017-11-06 16:09:56 -0800507 ALOGE_IF(error != HWC2::Error::None,
508 "[%s] Failed to set blend mode %s:"
509 " %s (%d)",
510 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
511 static_cast<int32_t>(error));
Mathias Agopian13127d82013-03-05 17:47:11 -0800512
513 // apply the layer's transform, followed by the display's global transform
514 // here we're guaranteed that the layer's transform preserves rects
Michael Lentine6c925ed2014-09-26 17:55:01 -0700515 Region activeTransparentRegion(s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700516 Transform t = getTransform();
Robert Carrb5d3d262016-03-25 15:08:13 -0700517 if (!s.crop.isEmpty()) {
518 Rect activeCrop(s.crop);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700519 activeCrop = t.transform(activeCrop);
David Sodman41fdfc92017-11-06 16:09:56 -0800520 if (!activeCrop.intersect(displayDevice->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000521 activeCrop.clear();
522 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700523 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800524 // This needs to be here as transform.transform(Rect) computes the
525 // transformed rect and then takes the bounding box of the result before
526 // returning. This means
527 // transform.inverse().transform(transform.transform(Rect)) != Rect
528 // in which case we need to make sure the final rect is clipped to the
529 // display bounds.
David Sodman41fdfc92017-11-06 16:09:56 -0800530 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000531 activeCrop.clear();
532 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700533 // mark regions outside the crop as transparent
534 activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top));
David Sodman41fdfc92017-11-06 16:09:56 -0800535 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom, s.active.w, s.active.h));
536 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
537 activeTransparentRegion.orSelf(
538 Rect(activeCrop.right, activeCrop.top, s.active.w, activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700539 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700540
Dan Stoza80d61162017-12-20 15:57:52 -0800541 // computeBounds returns a FloatRect to provide more accuracy during the
542 // transformation. We then round upon constructing 'frame'.
543 Rect frame{t.transform(computeBounds(activeTransparentRegion))};
Robert Carrb5d3d262016-03-25 15:08:13 -0700544 if (!s.finalCrop.isEmpty()) {
David Sodman41fdfc92017-11-06 16:09:56 -0800545 if (!frame.intersect(s.finalCrop, &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000546 frame.clear();
547 }
548 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000549 if (!frame.intersect(displayDevice->getViewport(), &frame)) {
550 frame.clear();
551 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800552 const Transform& tr(displayDevice->getTransform());
553 Rect transformedFrame = tr.transform(frame);
David Revemanecf0fa52017-03-03 11:32:44 -0500554 error = hwcLayer->setDisplayFrame(transformedFrame);
Dan Stozae22aec72016-08-01 13:20:59 -0700555 if (error != HWC2::Error::None) {
David Sodman41fdfc92017-11-06 16:09:56 -0800556 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
557 transformedFrame.left, transformedFrame.top, transformedFrame.right,
558 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stozae22aec72016-08-01 13:20:59 -0700559 } else {
560 hwcInfo.displayFrame = transformedFrame;
561 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800562
Dan Stoza5a423ea2017-02-16 14:10:39 -0800563 FloatRect sourceCrop = computeCrop(displayDevice);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800564 error = hwcLayer->setSourceCrop(sourceCrop);
Dan Stozae22aec72016-08-01 13:20:59 -0700565 if (error != HWC2::Error::None) {
566 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
David Sodman41fdfc92017-11-06 16:09:56 -0800567 "%s (%d)",
568 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
569 to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stozae22aec72016-08-01 13:20:59 -0700570 } else {
571 hwcInfo.sourceCrop = sourceCrop;
572 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800573
chaviw13fdc492017-06-27 12:40:18 -0700574 float alpha = static_cast<float>(getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -0700575 error = hwcLayer->setPlaneAlpha(alpha);
David Sodman41fdfc92017-11-06 16:09:56 -0800576 ALOGE_IF(error != HWC2::Error::None,
577 "[%s] Failed to set plane alpha %.3f: "
578 "%s (%d)",
579 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800580
Robert Carrae060832016-11-28 10:51:00 -0800581 error = hwcLayer->setZOrder(z);
David Sodman41fdfc92017-11-06 16:09:56 -0800582 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
583 to_string(error).c_str(), static_cast<int32_t>(error));
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500584
Albert Chaulk2a589632017-05-04 16:59:44 -0400585 int type = s.type;
586 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700587 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400588 if (parent.get()) {
589 auto& parentState = parent->getDrawingState();
590 type = parentState.type;
591 appId = parentState.appId;
592 }
593
594 error = hwcLayer->setInfo(type, appId);
David Sodman41fdfc92017-11-06 16:09:56 -0800595 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
596 static_cast<int32_t>(error));
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800597
Mathias Agopian29a367b2011-07-12 14:51:45 -0700598 /*
599 * Transformations are applied in this order:
600 * 1) buffer orientation/flip/mirror
601 * 2) state transformation (window manager)
602 * 3) layer orientation (screen orientation)
603 * (NOTE: the matrices are multiplied in reverse order)
604 */
605
606 const Transform bufferOrientation(mCurrentTransform);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700607 Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700608
Robert Carrcae605c2017-03-29 12:10:31 -0700609 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700610 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700611 * the code below applies the primary display's inverse transform to the
612 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700613 */
David Sodman41fdfc92017-11-06 16:09:56 -0800614 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700615 // calculate the inverse transform
616 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800617 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700618 }
Robert Carrcae605c2017-03-29 12:10:31 -0700619
620 /*
621 * Here we cancel out the orientation component of the WM transform.
622 * The scaling and translate components are already included in our bounds
623 * computation so it's enough to just omit it in the composition.
624 * See comment in onDraw with ref to b/36727915 for why.
625 */
626 transform = Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700627 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700628
Jorim Jaggi5a963262018-01-17 15:57:44 +0100629 // STOPSHIP (b/72106793): If we have less than 25% scaling, HWC usually needs to use the rotator
630 // to handle it. However, there is one guaranteed frame of jank when we switch to using the
631 // rotator. In the meantime, we force GL composition instead until we have a better fix for the
632 // HWC issue.
633 bool extremeScaling = abs(t[0][0]) <= 0.25 || abs(t[1][1]) <= 0.25;
634
Mathias Agopian29a367b2011-07-12 14:51:45 -0700635 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800636 const uint32_t orientation = transform.getOrientation();
Jorim Jaggi5a963262018-01-17 15:57:44 +0100637 if (orientation & Transform::ROT_INVALID || extremeScaling) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800638 // we can only handle simple transformation
639 hwcInfo.forceClientComposition = true;
640 } else {
641 auto transform = static_cast<HWC2::Transform>(orientation);
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800642 hwcInfo.transform = transform;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800643 auto error = hwcLayer->setTransform(transform);
David Sodman41fdfc92017-11-06 16:09:56 -0800644 ALOGE_IF(error != HWC2::Error::None,
645 "[%s] Failed to set transform %s: "
646 "%s (%d)",
647 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
648 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800649 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700650}
651
Dan Stoza9e56aa02015-11-02 13:00:03 -0800652void Layer::forceClientComposition(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700653 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800654 ALOGE("forceClientComposition: no HWC layer found (%d)", hwcId);
655 return;
656 }
657
David Sodman6f65f3e2017-11-03 14:28:09 -0700658 getBE().mHwcLayers[hwcId].forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800659}
Dan Stozaee44edd2015-03-23 15:50:23 -0700660
chaviwc9232ed2017-11-14 15:31:15 -0800661bool Layer::getForceClientComposition(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700662 if (getBE().mHwcLayers.count(hwcId) == 0) {
chaviwc9232ed2017-11-14 15:31:15 -0800663 ALOGE("getForceClientComposition: no HWC layer found (%d)", hwcId);
664 return false;
665 }
666
David Sodman6f65f3e2017-11-03 14:28:09 -0700667 return getBE().mHwcLayers[hwcId].forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800668}
669
Dan Stoza9e56aa02015-11-02 13:00:03 -0800670void Layer::updateCursorPosition(const sp<const DisplayDevice>& displayDevice) {
671 auto hwcId = displayDevice->getHwcDisplayId();
David Sodman6f65f3e2017-11-03 14:28:09 -0700672 if (getBE().mHwcLayers.count(hwcId) == 0 ||
David Sodman9eeae692017-11-02 10:53:32 -0700673 getCompositionType(hwcId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800674 return;
675 }
676
677 // This gives us only the "orientation" component of the transform
678 const State& s(getCurrentState());
679
680 // Apply the layer's transform, followed by the display's global transform
681 // Here we're guaranteed that the layer's transform preserves rects
682 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700683 if (!s.crop.isEmpty()) {
684 win.intersect(s.crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800685 }
686 // Subtract the transparent region and snap to the bounds
687 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700688 Rect frame(getTransform().transform(bounds));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800689 frame.intersect(displayDevice->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700690 if (!s.finalCrop.isEmpty()) {
691 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000692 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800693 auto& displayTransform(displayDevice->getTransform());
694 auto position = displayTransform.transform(frame);
695
David Sodman6f65f3e2017-11-03 14:28:09 -0700696 auto error = getBE().mHwcLayers[hwcId].layer->setCursorPosition(position.left,
David Sodman9eeae692017-11-02 10:53:32 -0700697 position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800698 ALOGE_IF(error != HWC2::Error::None,
699 "[%s] Failed to set cursor position "
700 "to (%d, %d): %s (%d)",
701 mName.string(), position.left, position.top, to_string(error).c_str(),
702 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800703}
Riley Andrews03414a12014-07-01 14:22:59 -0700704
Mathias Agopian13127d82013-03-05 17:47:11 -0800705// ---------------------------------------------------------------------------
706// drawing...
707// ---------------------------------------------------------------------------
708
chaviwa76b2712017-09-20 12:02:26 -0700709void Layer::draw(const RenderArea& renderArea, const Region& clip) const {
710 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800711}
712
chaviwa76b2712017-09-20 12:02:26 -0700713void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) const {
714 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800715}
716
chaviwa76b2712017-09-20 12:02:26 -0700717void Layer::draw(const RenderArea& renderArea) const {
718 onDraw(renderArea, Region(renderArea.getBounds()), false);
Dan Stozac7014012014-02-14 15:03:43 -0800719}
720
David Sodman41fdfc92017-11-06 16:09:56 -0800721void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
722 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800723 auto& engine(mFlinger->getRenderEngine());
David Sodman9eeae692017-11-02 10:53:32 -0700724 computeGeometry(renderArea, getBE().mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700725 engine.setupFillWithColor(red, green, blue, alpha);
David Sodman9eeae692017-11-02 10:53:32 -0700726 engine.drawMesh(getBE().mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800727}
728
chaviwa76b2712017-09-20 12:02:26 -0700729void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
David Sodman41fdfc92017-11-06 16:09:56 -0800730 clearWithOpenGL(renderArea, 0, 0, 0, 0);
Mathias Agopian13127d82013-03-05 17:47:11 -0800731}
732
David Sodman41fdfc92017-11-06 16:09:56 -0800733void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type, bool callIntoHwc) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700734 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800735 ALOGE("setCompositionType called without a valid HWC layer");
736 return;
737 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700738 auto& hwcInfo = getBE().mHwcLayers[hwcId];
Dan Stoza9e56aa02015-11-02 13:00:03 -0800739 auto& hwcLayer = hwcInfo.layer;
David Sodman41fdfc92017-11-06 16:09:56 -0800740 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(), to_string(type).c_str(),
741 static_cast<int>(callIntoHwc));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800742 if (hwcInfo.compositionType != type) {
743 ALOGV(" actually setting");
744 hwcInfo.compositionType = type;
745 if (callIntoHwc) {
746 auto error = hwcLayer->setCompositionType(type);
David Sodman41fdfc92017-11-06 16:09:56 -0800747 ALOGE_IF(error != HWC2::Error::None,
748 "[%s] Failed to set "
749 "composition type %s: %s (%d)",
750 mName.string(), to_string(type).c_str(), to_string(error).c_str(),
751 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800752 }
753 }
754}
755
756HWC2::Composition Layer::getCompositionType(int32_t hwcId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -0700757 if (hwcId == DisplayDevice::DISPLAY_ID_INVALID) {
758 // If we're querying the composition type for a display that does not
759 // have a HWC counterpart, then it will always be Client
760 return HWC2::Composition::Client;
761 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700762 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700763 ALOGE("getCompositionType called with an invalid HWC layer");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800764 return HWC2::Composition::Invalid;
765 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700766 return getBE().mHwcLayers.at(hwcId).compositionType;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800767}
768
769void Layer::setClearClientTarget(int32_t hwcId, bool clear) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700770 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800771 ALOGE("setClearClientTarget called without a valid HWC layer");
772 return;
773 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700774 getBE().mHwcLayers[hwcId].clearClientTarget = clear;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800775}
776
777bool Layer::getClearClientTarget(int32_t hwcId) const {
David Sodman6f65f3e2017-11-03 14:28:09 -0700778 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800779 ALOGE("getClearClientTarget called without a valid HWC layer");
780 return false;
781 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700782 return getBE().mHwcLayers.at(hwcId).clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800783}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800784
Dan Stozacac35382016-01-27 12:21:06 -0800785bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
786 if (point->getFrameNumber() <= mCurrentFrameNumber) {
787 // Don't bother with a SyncPoint, since we've already latched the
788 // relevant frame
789 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700790 }
791
Dan Stozacac35382016-01-27 12:21:06 -0800792 Mutex::Autolock lock(mLocalSyncPointMutex);
793 mLocalSyncPoints.push_back(point);
794 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700795}
796
Mathias Agopian13127d82013-03-05 17:47:11 -0800797void Layer::setFiltering(bool filtering) {
798 mFiltering = filtering;
799}
800
801bool Layer::getFiltering() const {
802 return mFiltering;
803}
804
Mathias Agopian13127d82013-03-05 17:47:11 -0800805// ----------------------------------------------------------------------------
806// local state
807// ----------------------------------------------------------------------------
808
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000809static void boundPoint(vec2* point, const Rect& crop) {
810 if (point->x < crop.left) {
811 point->x = crop.left;
812 }
813 if (point->x > crop.right) {
814 point->x = crop.right;
815 }
816 if (point->y < crop.top) {
817 point->y = crop.top;
818 }
819 if (point->y > crop.bottom) {
820 point->y = crop.bottom;
821 }
822}
823
chaviwa76b2712017-09-20 12:02:26 -0700824void Layer::computeGeometry(const RenderArea& renderArea, Mesh& mesh,
825 bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700826 const Layer::State& s(getDrawingState());
chaviwa76b2712017-09-20 12:02:26 -0700827 const Transform renderAreaTransform(renderArea.getTransform());
828 const uint32_t height = renderArea.getHeight();
Dan Stoza80d61162017-12-20 15:57:52 -0800829 FloatRect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700830
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000831 vec2 lt = vec2(win.left, win.top);
832 vec2 lb = vec2(win.left, win.bottom);
833 vec2 rb = vec2(win.right, win.bottom);
834 vec2 rt = vec2(win.right, win.top);
835
Robert Carr1f0a16a2016-10-24 16:27:39 -0700836 Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000837 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700838 lt = layerTransform.transform(lt);
839 lb = layerTransform.transform(lb);
840 rb = layerTransform.transform(rb);
841 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000842 }
843
Robert Carrb5d3d262016-03-25 15:08:13 -0700844 if (!s.finalCrop.isEmpty()) {
845 boundPoint(&lt, s.finalCrop);
846 boundPoint(&lb, s.finalCrop);
847 boundPoint(&rb, s.finalCrop);
848 boundPoint(&rt, s.finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000849 }
850
Mathias Agopianff2ed702013-09-01 21:36:12 -0700851 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700852 position[0] = renderAreaTransform.transform(lt);
853 position[1] = renderAreaTransform.transform(lb);
854 position[2] = renderAreaTransform.transform(rb);
855 position[3] = renderAreaTransform.transform(rt);
David Sodman41fdfc92017-11-06 16:09:56 -0800856 for (size_t i = 0; i < 4; i++) {
chaviwa76b2712017-09-20 12:02:26 -0700857 position[i].y = height - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -0800858 }
859}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800860
David Sodman41fdfc92017-11-06 16:09:56 -0800861bool Layer::isSecure() const {
Dan Stoza23116082015-06-18 14:58:39 -0700862 const Layer::State& s(mDrawingState);
863 return (s.flags & layer_state_t::eLayerSecure);
864}
865
Mathias Agopian13127d82013-03-05 17:47:11 -0800866void Layer::setVisibleRegion(const Region& visibleRegion) {
867 // always called from main thread
868 this->visibleRegion = visibleRegion;
869}
870
871void Layer::setCoveredRegion(const Region& coveredRegion) {
872 // always called from main thread
873 this->coveredRegion = coveredRegion;
874}
875
David Sodman41fdfc92017-11-06 16:09:56 -0800876void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800877 // always called from main thread
878 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
879}
880
Robert Carre5f4f692018-01-12 13:12:28 -0800881void Layer::clearVisibilityRegions() {
882 visibleRegion.clear();
883 visibleNonTransparentRegion.clear();
884 coveredRegion.clear();
885}
886
Mathias Agopian13127d82013-03-05 17:47:11 -0800887// ----------------------------------------------------------------------------
888// transaction
889// ----------------------------------------------------------------------------
890
Dan Stoza7dde5992015-05-22 09:51:44 -0700891void Layer::pushPendingState() {
892 if (!mCurrentState.modified) {
893 return;
894 }
895
Dan Stoza7dde5992015-05-22 09:51:44 -0700896 // If this transaction is waiting on the receipt of a frame, generate a sync
897 // point and send it to the remote layer.
Robert Carr0d480722017-01-10 16:42:54 -0800898 if (mCurrentState.barrierLayer != nullptr) {
899 sp<Layer> barrierLayer = mCurrentState.barrierLayer.promote();
900 if (barrierLayer == nullptr) {
901 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700902 // If we can't promote the layer we are intended to wait on,
903 // then it is expired or otherwise invalid. Allow this transaction
904 // to be applied as per normal (no synchronization).
Robert Carr0d480722017-01-10 16:42:54 -0800905 mCurrentState.barrierLayer = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800906 } else {
David Sodman41fdfc92017-11-06 16:09:56 -0800907 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber);
Robert Carr0d480722017-01-10 16:42:54 -0800908 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800909 mRemoteSyncPoints.push_back(std::move(syncPoint));
910 } else {
911 // We already missed the frame we're supposed to synchronize
912 // on, so go ahead and apply the state update
Robert Carr0d480722017-01-10 16:42:54 -0800913 mCurrentState.barrierLayer = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800914 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700915 }
916
Dan Stoza7dde5992015-05-22 09:51:44 -0700917 // Wake us up to check if the frame has been received
918 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700919 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700920 }
921 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700922 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700923}
924
Pablo Ceballos05289c22016-04-14 15:49:55 -0700925void Layer::popPendingState(State* stateToCommit) {
926 auto oldFlags = stateToCommit->flags;
927 *stateToCommit = mPendingStates[0];
David Sodman41fdfc92017-11-06 16:09:56 -0800928 stateToCommit->flags =
929 (oldFlags & ~stateToCommit->mask) | (stateToCommit->flags & stateToCommit->mask);
Dan Stoza7dde5992015-05-22 09:51:44 -0700930
931 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700932 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700933}
934
Pablo Ceballos05289c22016-04-14 15:49:55 -0700935bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700936 bool stateUpdateAvailable = false;
937 while (!mPendingStates.empty()) {
Robert Carr0d480722017-01-10 16:42:54 -0800938 if (mPendingStates[0].barrierLayer != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700939 if (mRemoteSyncPoints.empty()) {
940 // If we don't have a sync point for this, apply it anyway. It
941 // will be visually wrong, but it should keep us from getting
942 // into too much trouble.
943 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700944 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700945 stateUpdateAvailable = true;
946 continue;
947 }
948
David Sodman41fdfc92017-11-06 16:09:56 -0800949 if (mRemoteSyncPoints.front()->getFrameNumber() != mPendingStates[0].frameNumber) {
950 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800951
952 // Signal our end of the sync point and then dispose of it
953 mRemoteSyncPoints.front()->setTransactionApplied();
954 mRemoteSyncPoints.pop_front();
955 continue;
956 }
957
Dan Stoza7dde5992015-05-22 09:51:44 -0700958 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
959 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700960 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700961 stateUpdateAvailable = true;
962
963 // Signal our end of the sync point and then dispose of it
964 mRemoteSyncPoints.front()->setTransactionApplied();
965 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800966 } else {
967 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700968 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700969 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700970 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700971 stateUpdateAvailable = true;
972 }
973 }
974
975 // If we still have pending updates, wake SurfaceFlinger back up and point
976 // it at this layer so we can process them
977 if (!mPendingStates.empty()) {
978 setTransactionFlags(eTransactionNeeded);
979 mFlinger->setTransactionFlags(eTraversalNeeded);
980 }
981
982 mCurrentState.modified = false;
983 return stateUpdateAvailable;
984}
985
Mathias Agopian13127d82013-03-05 17:47:11 -0800986uint32_t Layer::doTransaction(uint32_t flags) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800987 ATRACE_CALL();
988
Dan Stoza7dde5992015-05-22 09:51:44 -0700989 pushPendingState();
Pablo Ceballos05289c22016-04-14 15:49:55 -0700990 Layer::State c = getCurrentState();
991 if (!applyPendingStates(&c)) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700992 return 0;
993 }
994
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700995 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800996
David Sodman41fdfc92017-11-06 16:09:56 -0800997 const bool sizeChanged = (c.requested.w != s.requested.w) || (c.requested.h != s.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700998
David Sodmaneb085e02017-10-05 18:49:04 -0700999 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001000 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +00001001 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -08001002 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
1003 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1004 " requested={ wh={%4u,%4u} }}\n"
1005 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1006 " requested={ wh={%4u,%4u} }}\n",
David Sodman9eeae692017-11-02 10:53:32 -07001007 this, getName().string(), mCurrentTransform,
1008 getEffectiveScalingMode(), c.active.w, c.active.h, c.crop.left, c.crop.top,
1009 c.crop.right, c.crop.bottom, c.crop.getWidth(), c.crop.getHeight(), c.requested.w,
1010 c.requested.h, s.active.w, s.active.h, s.crop.left, s.crop.top, s.crop.right,
1011 s.crop.bottom, s.crop.getWidth(), s.crop.getHeight(), s.requested.w,
1012 s.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001013
Jamie Gennis2a0d5b62011-09-26 16:54:44 -07001014 // record the new size, form this point on, when the client request
1015 // a buffer, it'll get the new size.
David Sodmaneb085e02017-10-05 18:49:04 -07001016 setDefaultBufferSize(c.requested.w, c.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001017 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001018
Robert Carre392b552017-09-19 12:16:05 -07001019 // Don't let Layer::doTransaction update the drawing state
1020 // if we have a pending resize, unless we are in fixed-size mode.
1021 // the drawing state will be updated only once we receive a buffer
1022 // with the correct size.
1023 //
1024 // In particular, we want to make sure the clip (which is part
1025 // of the geometry state) is latched together with the size but is
1026 // latched immediately when no resizing is involved.
1027 //
1028 // If a sideband stream is attached, however, we want to skip this
1029 // optimization so that transactions aren't missed when a buffer
1030 // never arrives
1031 //
1032 // In the case that we don't have a buffer we ignore other factors
1033 // and avoid entering the resizePending state. At a high level the
1034 // resizePending state is to avoid applying the state of the new buffer
1035 // to the old buffer. However in the state where we don't have an old buffer
1036 // there is no such concern but we may still be being used as a parent layer.
David Sodman41fdfc92017-11-06 16:09:56 -08001037 const bool resizePending = ((c.requested.w != c.active.w) || (c.requested.h != c.active.h)) &&
David Sodman0cc69182017-11-17 12:12:07 -08001038 (getBE().compositionInfo.mBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001039 if (!isFixedSize()) {
David Sodman0cc69182017-11-17 12:12:07 -08001040 if (resizePending && getBE().compositionInfo.hwc.sidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001041 flags |= eDontUpdateGeometryState;
1042 }
1043 }
1044
Robert Carr7bf247e2017-05-18 14:02:49 -07001045 // Here we apply various requested geometry states, depending on our
1046 // latching configuration. See Layer.h for a detailed discussion of
1047 // how geometry latching is controlled.
1048 if (!(flags & eDontUpdateGeometryState)) {
Pablo Ceballos7d052572016-06-02 17:46:05 -07001049 Layer::State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001050
1051 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1052 // mode, which causes attributes which normally latch regardless of scaling mode,
1053 // to be delayed. We copy the requested state to the active state making sure
1054 // to respect these rules (again see Layer.h for a detailed discussion).
1055 //
1056 // There is an awkward asymmetry in the handling of the crop states in the position
1057 // states, as can be seen below. Largely this arises from position and transform
1058 // being stored in the same data structure while having different latching rules.
1059 // b/38182305
1060 //
1061 // Careful that "c" and editCurrentState may not begin as equivalent due to
1062 // applyPendingStates in the presence of deferred transactions.
1063 if (mFreezeGeometryUpdates) {
Robert Carr82364e32016-05-15 11:27:47 -07001064 float tx = c.active.transform.tx();
1065 float ty = c.active.transform.ty();
1066 c.active = c.requested;
1067 c.active.transform.set(tx, ty);
1068 editCurrentState.active = c.active;
1069 } else {
1070 editCurrentState.active = editCurrentState.requested;
1071 c.active = c.requested;
1072 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001073 }
1074
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001075 if (s.active != c.active) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001076 // invalidate and recompute the visible regions if needed
1077 flags |= Layer::eVisibleRegion;
1078 }
1079
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001080 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001081 // invalidate and recompute the visible regions if needed
1082 flags |= eVisibleRegion;
1083 this->contentDirty = true;
1084
1085 // we may use linear filtering, if the matrix scales us
Robert Carr3dcabfa2016-03-01 18:36:58 -08001086 const uint8_t type = c.active.transform.getType();
David Sodman41fdfc92017-11-06 16:09:56 -08001087 mNeedsFiltering = (!c.active.transform.preserveRects() || (type >= Transform::SCALE));
Mathias Agopian13127d82013-03-05 17:47:11 -08001088 }
1089
Dan Stozac8145172016-04-28 16:29:06 -07001090 // If the layer is hidden, signal and clear out all local sync points so
1091 // that transactions for layers depending on this layer's frames becoming
1092 // visible are not blocked
1093 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001094 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001095 }
1096
Mathias Agopian13127d82013-03-05 17:47:11 -08001097 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001098 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001099 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001100}
1101
Pablo Ceballos05289c22016-04-14 15:49:55 -07001102void Layer::commitTransaction(const State& stateToCommit) {
1103 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001104}
1105
Mathias Agopian13127d82013-03-05 17:47:11 -08001106uint32_t Layer::getTransactionFlags(uint32_t flags) {
1107 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1108}
1109
1110uint32_t Layer::setTransactionFlags(uint32_t flags) {
1111 return android_atomic_or(flags, &mTransactionFlags);
1112}
1113
Robert Carr82364e32016-05-15 11:27:47 -07001114bool Layer::setPosition(float x, float y, bool immediate) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001115 if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001116 return false;
1117 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001118
1119 // We update the requested and active position simultaneously because
1120 // we want to apply the position portion of the transform matrix immediately,
1121 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Robert Carr3dcabfa2016-03-01 18:36:58 -08001122 mCurrentState.requested.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001123 if (immediate && !mFreezeGeometryUpdates) {
1124 // Here we directly update the active state
1125 // unlike other setters, because we store it within
1126 // the transform, but use different latching rules.
1127 // b/38182305
Robert Carr82364e32016-05-15 11:27:47 -07001128 mCurrentState.active.transform.set(x, y);
1129 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001130 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001131
Dan Stoza7dde5992015-05-22 09:51:44 -07001132 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001133 setTransactionFlags(eTransactionNeeded);
1134 return true;
1135}
Robert Carr82364e32016-05-15 11:27:47 -07001136
Robert Carr1f0a16a2016-10-24 16:27:39 -07001137bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1138 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1139 if (idx < 0) {
1140 return false;
1141 }
1142 if (childLayer->setLayer(z)) {
1143 mCurrentChildren.removeAt(idx);
1144 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001145 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001146 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001147 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001148}
1149
Robert Carr503c7042017-09-27 15:06:08 -07001150bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1151 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1152 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1153 if (idx < 0) {
1154 return false;
1155 }
1156 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1157 mCurrentChildren.removeAt(idx);
1158 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001159 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001160 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001161 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001162}
1163
Robert Carrae060832016-11-28 10:51:00 -08001164bool Layer::setLayer(int32_t z) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001165 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001166 mCurrentState.sequence++;
1167 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001168 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001169
1170 // Discard all relative layering.
1171 if (mCurrentState.zOrderRelativeOf != nullptr) {
1172 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1173 if (strongRelative != nullptr) {
1174 strongRelative->removeZOrderRelative(this);
1175 }
1176 mCurrentState.zOrderRelativeOf = nullptr;
1177 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001178 setTransactionFlags(eTransactionNeeded);
1179 return true;
1180}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001181
Robert Carrdb66e622017-04-10 16:55:57 -07001182void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1183 mCurrentState.zOrderRelatives.remove(relative);
1184 mCurrentState.sequence++;
1185 mCurrentState.modified = true;
1186 setTransactionFlags(eTransactionNeeded);
1187}
1188
1189void Layer::addZOrderRelative(const wp<Layer>& relative) {
1190 mCurrentState.zOrderRelatives.add(relative);
1191 mCurrentState.modified = true;
1192 mCurrentState.sequence++;
1193 setTransactionFlags(eTransactionNeeded);
1194}
1195
Robert Carr503d2bd2017-12-04 15:49:47 -08001196bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001197 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1198 if (handle == nullptr) {
1199 return false;
1200 }
1201 sp<Layer> relative = handle->owner.promote();
1202 if (relative == nullptr) {
1203 return false;
1204 }
1205
Robert Carr503d2bd2017-12-04 15:49:47 -08001206 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1207 mCurrentState.zOrderRelativeOf == relative) {
1208 return false;
1209 }
1210
Robert Carrdb66e622017-04-10 16:55:57 -07001211 mCurrentState.sequence++;
1212 mCurrentState.modified = true;
Robert Carr503d2bd2017-12-04 15:49:47 -08001213 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001214
chaviw9ab4bd12017-11-03 13:11:00 -07001215 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
1216 if (oldZOrderRelativeOf != nullptr) {
1217 oldZOrderRelativeOf->removeZOrderRelative(this);
1218 }
Robert Carrdb66e622017-04-10 16:55:57 -07001219 mCurrentState.zOrderRelativeOf = relative;
1220 relative->addZOrderRelative(this);
1221
1222 setTransactionFlags(eTransactionNeeded);
1223
1224 return true;
1225}
1226
Mathias Agopian13127d82013-03-05 17:47:11 -08001227bool Layer::setSize(uint32_t w, uint32_t h) {
David Sodman41fdfc92017-11-06 16:09:56 -08001228 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001229 mCurrentState.requested.w = w;
1230 mCurrentState.requested.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001231 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001232 setTransactionFlags(eTransactionNeeded);
1233 return true;
1234}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001235bool Layer::setAlpha(float alpha) {
David Sodman41fdfc92017-11-06 16:09:56 -08001236 if (mCurrentState.color.a == alpha) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001237 mCurrentState.sequence++;
chaviw13fdc492017-06-27 12:40:18 -07001238 mCurrentState.color.a = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001239 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001240 setTransactionFlags(eTransactionNeeded);
1241 return true;
1242}
chaviw13fdc492017-06-27 12:40:18 -07001243
1244bool Layer::setColor(const half3& color) {
David Sodman41fdfc92017-11-06 16:09:56 -08001245 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g &&
1246 color.b == mCurrentState.color.b)
chaviw13fdc492017-06-27 12:40:18 -07001247 return false;
1248
1249 mCurrentState.sequence++;
1250 mCurrentState.color.r = color.r;
1251 mCurrentState.color.g = color.g;
1252 mCurrentState.color.b = color.b;
1253 mCurrentState.modified = true;
1254 setTransactionFlags(eTransactionNeeded);
1255 return true;
1256}
1257
Mathias Agopian13127d82013-03-05 17:47:11 -08001258bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
1259 mCurrentState.sequence++;
David Sodman41fdfc92017-11-06 16:09:56 -08001260 mCurrentState.requested.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001261 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001262 setTransactionFlags(eTransactionNeeded);
1263 return true;
1264}
1265bool Layer::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001266 mCurrentState.requestedTransparentRegion = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001267 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001268 setTransactionFlags(eTransactionNeeded);
1269 return true;
1270}
1271bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1272 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
David Sodman41fdfc92017-11-06 16:09:56 -08001273 if (mCurrentState.flags == newFlags) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001274 mCurrentState.sequence++;
1275 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001276 mCurrentState.mask = mask;
1277 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001278 setTransactionFlags(eTransactionNeeded);
1279 return true;
1280}
Robert Carr99e27f02016-06-16 15:18:02 -07001281
1282bool Layer::setCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001283 if (mCurrentState.requestedCrop == crop) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001284 mCurrentState.sequence++;
Robert Carr99e27f02016-06-16 15:18:02 -07001285 mCurrentState.requestedCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001286 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr99e27f02016-06-16 15:18:02 -07001287 mCurrentState.crop = crop;
1288 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001289 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1290
Dan Stoza7dde5992015-05-22 09:51:44 -07001291 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001292 setTransactionFlags(eTransactionNeeded);
1293 return true;
1294}
Robert Carr8d5227b2017-03-16 15:41:03 -07001295
1296bool Layer::setFinalCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001297 if (mCurrentState.requestedFinalCrop == crop) return false;
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001298 mCurrentState.sequence++;
Robert Carr8d5227b2017-03-16 15:41:03 -07001299 mCurrentState.requestedFinalCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001300 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr8d5227b2017-03-16 15:41:03 -07001301 mCurrentState.finalCrop = crop;
1302 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001303 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1304
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001305 mCurrentState.modified = true;
1306 setTransactionFlags(eTransactionNeeded);
1307 return true;
1308}
Mathias Agopian13127d82013-03-05 17:47:11 -08001309
Robert Carrc3574f72016-03-24 12:19:32 -07001310bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001311 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001312 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001313 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001314 return true;
1315}
1316
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001317void Layer::setInfo(uint32_t type, uint32_t appId) {
David Sodman41fdfc92017-11-06 16:09:56 -08001318 mCurrentState.appId = appId;
1319 mCurrentState.type = type;
1320 mCurrentState.modified = true;
1321 setTransactionFlags(eTransactionNeeded);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001322}
1323
Mathias Agopian13127d82013-03-05 17:47:11 -08001324bool Layer::setLayerStack(uint32_t layerStack) {
David Sodman41fdfc92017-11-06 16:09:56 -08001325 if (mCurrentState.layerStack == layerStack) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001326 mCurrentState.sequence++;
1327 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001328 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001329 setTransactionFlags(eTransactionNeeded);
1330 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001331}
1332
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -07001333bool Layer::setDataSpace(android_dataspace dataSpace) {
David Sodman41fdfc92017-11-06 16:09:56 -08001334 if (mCurrentState.dataSpace == dataSpace) return false;
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -07001335 mCurrentState.sequence++;
1336 mCurrentState.dataSpace = dataSpace;
1337 mCurrentState.modified = true;
1338 setTransactionFlags(eTransactionNeeded);
1339 return true;
1340}
1341
Courtney Goeltzenleuchter532b2632017-05-05 16:34:38 -06001342android_dataspace Layer::getDataSpace() const {
1343 return mCurrentState.dataSpace;
1344}
1345
Robert Carr1f0a16a2016-10-24 16:27:39 -07001346uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001347 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001348 if (p == nullptr) {
1349 return getDrawingState().layerStack;
1350 }
1351 return p->getLayerStack();
1352}
1353
David Sodman41fdfc92017-11-06 16:09:56 -08001354void Layer::deferTransactionUntil(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001355 mCurrentState.barrierLayer = barrierLayer;
Dan Stoza7dde5992015-05-22 09:51:44 -07001356 mCurrentState.frameNumber = frameNumber;
1357 // We don't set eTransactionNeeded, because just receiving a deferral
1358 // request without any other state updates shouldn't actually induce a delay
1359 mCurrentState.modified = true;
1360 pushPendingState();
Robert Carr0d480722017-01-10 16:42:54 -08001361 mCurrentState.barrierLayer = nullptr;
Dan Stoza792e5292016-02-11 11:43:58 -08001362 mCurrentState.frameNumber = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001363 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001364}
1365
David Sodman41fdfc92017-11-06 16:09:56 -08001366void Layer::deferTransactionUntil(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001367 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
1368 deferTransactionUntil(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001369}
1370
Dan Stozaee44edd2015-03-23 15:50:23 -07001371
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001372// ----------------------------------------------------------------------------
1373// pageflip handling...
1374// ----------------------------------------------------------------------------
1375
Robert Carr1f0a16a2016-10-24 16:27:39 -07001376bool Layer::isHiddenByPolicy() const {
1377 const Layer::State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001378 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001379 if (parent != nullptr && parent->isHiddenByPolicy()) {
1380 return true;
1381 }
1382 return s.flags & layer_state_t::eLayerHidden;
1383}
1384
David Sodman41fdfc92017-11-06 16:09:56 -08001385uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001386 // TODO: should we do something special if mSecure is set?
1387 if (mProtectedByApp) {
1388 // need a hardware-protected path to external video sink
1389 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001390 }
Riley Andrews03414a12014-07-01 14:22:59 -07001391 if (mPotentialCursor) {
1392 usage |= GraphicBuffer::USAGE_CURSOR;
1393 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001394 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001395 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001396}
1397
Mathias Agopian84300952012-11-21 16:02:13 -08001398void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001399 uint32_t orientation = 0;
1400 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001401 // The transform hint is used to improve performance, but we can
1402 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001403 // apply to all displays.
Mathias Agopian42977342012-08-05 00:40:46 -07001404 const Transform& planeTransform(hw->getTransform());
Mathias Agopian4fec8732012-06-29 14:12:52 -07001405 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07001406 if (orientation & Transform::ROT_INVALID) {
1407 orientation = 0;
1408 }
1409 }
David Sodmaneb085e02017-10-05 18:49:04 -07001410 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001411}
1412
Mathias Agopian13127d82013-03-05 17:47:11 -08001413// ----------------------------------------------------------------------------
1414// debugging
1415// ----------------------------------------------------------------------------
1416
Kalle Raitaa099a242017-01-11 11:17:29 -08001417LayerDebugInfo Layer::getLayerDebugInfo() const {
1418 LayerDebugInfo info;
1419 const Layer::State& ds = getDrawingState();
1420 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001421 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001422 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
1423 info.mType = String8(getTypeId());
1424 info.mTransparentRegion = ds.activeTransparentRegion;
1425 info.mVisibleRegion = visibleRegion;
1426 info.mSurfaceDamageRegion = surfaceDamageRegion;
1427 info.mLayerStack = getLayerStack();
1428 info.mX = ds.active.transform.tx();
1429 info.mY = ds.active.transform.ty();
1430 info.mZ = ds.z;
1431 info.mWidth = ds.active.w;
1432 info.mHeight = ds.active.h;
1433 info.mCrop = ds.crop;
1434 info.mFinalCrop = ds.finalCrop;
chaviw13fdc492017-06-27 12:40:18 -07001435 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001436 info.mFlags = ds.flags;
1437 info.mPixelFormat = getPixelFormat();
1438 info.mDataSpace = getDataSpace();
1439 info.mMatrix[0][0] = ds.active.transform[0][0];
1440 info.mMatrix[0][1] = ds.active.transform[0][1];
1441 info.mMatrix[1][0] = ds.active.transform[1][0];
1442 info.mMatrix[1][1] = ds.active.transform[1][1];
1443 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001444 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001445 if (buffer != 0) {
1446 info.mActiveBufferWidth = buffer->getWidth();
1447 info.mActiveBufferHeight = buffer->getHeight();
1448 info.mActiveBufferStride = buffer->getStride();
1449 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001450 } else {
1451 info.mActiveBufferWidth = 0;
1452 info.mActiveBufferHeight = 0;
1453 info.mActiveBufferStride = 0;
1454 info.mActiveBufferFormat = 0;
1455 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001456 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001457 info.mNumQueuedFrames = getQueuedFrameCount();
1458 info.mRefreshPending = isBufferLatched();
1459 info.mIsOpaque = isOpaque(ds);
1460 info.mContentDirty = contentDirty;
1461 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001462}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001463
Dan Stozae22aec72016-08-01 13:20:59 -07001464void Layer::miniDumpHeader(String8& result) {
1465 result.append("----------------------------------------");
1466 result.append("---------------------------------------\n");
1467 result.append(" Layer name\n");
1468 result.append(" Z | ");
1469 result.append(" Comp Type | ");
1470 result.append(" Disp Frame (LTRB) | ");
1471 result.append(" Source Crop (LTRB)\n");
1472 result.append("----------------------------------------");
1473 result.append("---------------------------------------\n");
1474}
1475
1476void Layer::miniDump(String8& result, int32_t hwcId) const {
David Sodman6f65f3e2017-11-03 14:28:09 -07001477 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stozae22aec72016-08-01 13:20:59 -07001478 return;
1479 }
1480
1481 String8 name;
1482 if (mName.length() > 77) {
1483 std::string shortened;
1484 shortened.append(mName.string(), 36);
1485 shortened.append("[...]");
1486 shortened.append(mName.string() + (mName.length() - 36), 36);
1487 name = shortened.c_str();
1488 } else {
1489 name = mName;
1490 }
1491
1492 result.appendFormat(" %s\n", name.string());
1493
1494 const Layer::State& layerState(getDrawingState());
David Sodman6f65f3e2017-11-03 14:28:09 -07001495 const LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers.at(hwcId);
John Reck8c3b6ac2017-08-24 10:25:42 -07001496 result.appendFormat(" %10d | ", layerState.z);
David Sodman41fdfc92017-11-06 16:09:56 -08001497 result.appendFormat("%10s | ", to_string(getCompositionType(hwcId)).c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001498 const Rect& frame = hwcInfo.displayFrame;
David Sodman41fdfc92017-11-06 16:09:56 -08001499 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Dan Stoza5a423ea2017-02-16 14:10:39 -08001500 const FloatRect& crop = hwcInfo.sourceCrop;
David Sodman41fdfc92017-11-06 16:09:56 -08001501 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 -07001502
1503 result.append("- - - - - - - - - - - - - - - - - - - - ");
1504 result.append("- - - - - - - - - - - - - - - - - - - -\n");
1505}
Dan Stozae22aec72016-08-01 13:20:59 -07001506
Svetoslavd85084b2014-03-20 10:28:31 -07001507void Layer::dumpFrameStats(String8& result) const {
1508 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001509}
1510
Svetoslavd85084b2014-03-20 10:28:31 -07001511void Layer::clearFrameStats() {
1512 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001513}
1514
Jamie Gennis6547ff42013-07-16 20:12:42 -07001515void Layer::logFrameStats() {
1516 mFrameTracker.logAndResetStats(mName);
1517}
1518
Svetoslavd85084b2014-03-20 10:28:31 -07001519void Layer::getFrameStats(FrameStats* outStats) const {
1520 mFrameTracker.getStats(outStats);
1521}
1522
Brian Andersond6927fb2016-07-23 23:37:30 -07001523void Layer::dumpFrameEvents(String8& result) {
David Sodman41fdfc92017-11-06 16:09:56 -08001524 result.appendFormat("- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001525 Mutex::Autolock lock(mFrameEventHistoryMutex);
1526 mFrameEventHistory.checkFencesForCompletion();
1527 mFrameEventHistory.dump(result);
1528}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001529
Brian Anderson5ea5e592016-12-01 16:54:33 -08001530void Layer::onDisconnect() {
1531 Mutex::Autolock lock(mFrameEventHistoryMutex);
1532 mFrameEventHistory.onDisconnect();
1533}
1534
Brian Anderson3890c392016-07-25 12:48:08 -07001535void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001536 FrameEventHistoryDelta* outDelta) {
Brian Andersond6927fb2016-07-23 23:37:30 -07001537 Mutex::Autolock lock(mFrameEventHistoryMutex);
1538 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001539 // If there are any unsignaled fences in the aquire timeline at this
1540 // point, the previously queued frame hasn't been latched yet. Go ahead
1541 // and try to get the signal time here so the syscall is taken out of
1542 // the main thread's critical path.
1543 mAcquireTimeline.updateSignalTimes();
1544 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001545 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001546 mFrameEventHistory.addQueue(*newTimestamps);
1547 }
1548
Brian Anderson3890c392016-07-25 12:48:08 -07001549 if (outDelta) {
1550 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001551 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001552}
Dan Stozae77c7662016-05-13 11:37:28 -07001553
Chia-I Wu98f1c102017-05-30 14:54:08 -07001554size_t Layer::getChildrenCount() const {
1555 size_t count = 0;
1556 for (const sp<Layer>& child : mCurrentChildren) {
1557 count += 1 + child->getChildrenCount();
1558 }
1559 return count;
1560}
1561
Robert Carr1f0a16a2016-10-24 16:27:39 -07001562void Layer::addChild(const sp<Layer>& layer) {
1563 mCurrentChildren.add(layer);
1564 layer->setParent(this);
1565}
1566
1567ssize_t Layer::removeChild(const sp<Layer>& layer) {
1568 layer->setParent(nullptr);
1569 return mCurrentChildren.remove(layer);
1570}
1571
Robert Carr1db73f62016-12-21 12:58:51 -08001572bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1573 sp<Handle> handle = nullptr;
1574 sp<Layer> newParent = nullptr;
1575 if (newParentHandle == nullptr) {
1576 return false;
1577 }
1578 handle = static_cast<Handle*>(newParentHandle.get());
1579 newParent = handle->owner.promote();
1580 if (newParent == nullptr) {
1581 ALOGE("Unable to promote Layer handle");
1582 return false;
1583 }
1584
1585 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001586 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001587
1588 sp<Client> client(child->mClientRef.promote());
1589 if (client != nullptr) {
1590 client->setParentLayer(newParent);
1591 }
1592 }
1593 mCurrentChildren.clear();
1594
1595 return true;
1596}
1597
chaviwf1961f72017-09-18 16:41:07 -07001598bool Layer::reparent(const sp<IBinder>& newParentHandle) {
1599 if (newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001600 return false;
1601 }
1602
1603 auto handle = static_cast<Handle*>(newParentHandle.get());
1604 sp<Layer> newParent = handle->owner.promote();
1605 if (newParent == nullptr) {
1606 ALOGE("Unable to promote Layer handle");
1607 return false;
1608 }
1609
chaviwf1961f72017-09-18 16:41:07 -07001610 sp<Layer> parent = getParent();
1611 if (parent != nullptr) {
1612 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001613 }
chaviwf1961f72017-09-18 16:41:07 -07001614 newParent->addChild(this);
chaviw06178942017-07-27 10:25:59 -07001615
chaviwf1961f72017-09-18 16:41:07 -07001616 sp<Client> client(mClientRef.promote());
chaviw06178942017-07-27 10:25:59 -07001617 sp<Client> newParentClient(newParent->mClientRef.promote());
1618
chaviwf1961f72017-09-18 16:41:07 -07001619 if (client != newParentClient) {
1620 client->setParentLayer(newParent);
chaviw06178942017-07-27 10:25:59 -07001621 }
1622
chaviw06178942017-07-27 10:25:59 -07001623 return true;
1624}
1625
Robert Carr9524cb32017-02-13 11:32:32 -08001626bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001627 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001628 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001629 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001630 if (client != nullptr && parentClient != client) {
Robert Carr7f619b22017-11-06 12:56:35 -08001631 client->detachLayer(child.get());
1632 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001633 }
Robert Carr7f619b22017-11-06 12:56:35 -08001634 }
Robert Carr9524cb32017-02-13 11:32:32 -08001635
1636 return true;
1637}
1638
Robert Carr1f0a16a2016-10-24 16:27:39 -07001639void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001640 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001641}
1642
1643void Layer::clearSyncPoints() {
1644 for (const auto& child : mCurrentChildren) {
1645 child->clearSyncPoints();
1646 }
1647
1648 Mutex::Autolock lock(mLocalSyncPointMutex);
1649 for (auto& point : mLocalSyncPoints) {
1650 point->setFrameAvailable();
1651 }
1652 mLocalSyncPoints.clear();
1653}
1654
1655int32_t Layer::getZ() const {
1656 return mDrawingState.z;
1657}
1658
Robert Carr29abff82017-12-04 13:51:20 -08001659bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1660 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1661 const State& state = useDrawing ? mDrawingState : mCurrentState;
1662 return state.zOrderRelativeOf != nullptr;
1663}
1664
David Sodman41fdfc92017-11-06 16:09:56 -08001665__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001666 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001667 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1668 "makeTraversalList received invalid stateSet");
1669 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1670 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1671 const State& state = useDrawing ? mDrawingState : mCurrentState;
1672
Robert Carr29abff82017-12-04 13:51:20 -08001673 if (state.zOrderRelatives.size() == 0) {
1674 *outSkipRelativeZUsers = true;
1675 return children;
1676 }
1677
Robert Carrdb66e622017-04-10 16:55:57 -07001678 LayerVector traverse;
Dan Stoza412903f2017-04-27 13:42:17 -07001679 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001680 sp<Layer> strongRelative = weakRelative.promote();
1681 if (strongRelative != nullptr) {
1682 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001683 }
1684 }
1685
Dan Stoza412903f2017-04-27 13:42:17 -07001686 for (const sp<Layer>& child : children) {
Robert Carr503c7042017-09-27 15:06:08 -07001687 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1688 if (childState.zOrderRelativeOf != nullptr) {
1689 continue;
1690 }
Robert Carrdb66e622017-04-10 16:55:57 -07001691 traverse.add(child);
1692 }
1693
1694 return traverse;
1695}
1696
Robert Carr1f0a16a2016-10-24 16:27:39 -07001697/**
Robert Carrdb66e622017-04-10 16:55:57 -07001698 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001699 */
Dan Stoza412903f2017-04-27 13:42:17 -07001700void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001701 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1702 // produce a new list for traversing, including our relatives, and not including our children
1703 // who are relatives of another surface. In the case that there are no relative Z,
1704 // makeTraversalList returns our children directly to avoid significant overhead.
1705 // However in this case we need to take the responsibility for filtering children which
1706 // are relatives of another surface here.
1707 bool skipRelativeZUsers = false;
1708 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001709
Robert Carr1f0a16a2016-10-24 16:27:39 -07001710 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001711 for (; i < list.size(); i++) {
1712 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001713 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1714 continue;
1715 }
1716
Robert Carrdb66e622017-04-10 16:55:57 -07001717 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001718 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001719 }
Dan Stoza412903f2017-04-27 13:42:17 -07001720 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001721 }
Robert Carr29abff82017-12-04 13:51:20 -08001722
Dan Stoza412903f2017-04-27 13:42:17 -07001723 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001724 for (; i < list.size(); i++) {
1725 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001726
1727 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1728 continue;
1729 }
Dan Stoza412903f2017-04-27 13:42:17 -07001730 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001731 }
1732}
1733
1734/**
Robert Carrdb66e622017-04-10 16:55:57 -07001735 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001736 */
Dan Stoza412903f2017-04-27 13:42:17 -07001737void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1738 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001739 // See traverseInZOrder for documentation.
1740 bool skipRelativeZUsers = false;
1741 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001742
Robert Carr1f0a16a2016-10-24 16:27:39 -07001743 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001744 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001745 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001746
1747 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1748 continue;
1749 }
1750
Robert Carrdb66e622017-04-10 16:55:57 -07001751 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001752 break;
1753 }
Dan Stoza412903f2017-04-27 13:42:17 -07001754 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001755 }
Dan Stoza412903f2017-04-27 13:42:17 -07001756 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001757 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001758 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001759
1760 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1761 continue;
1762 }
1763
Dan Stoza412903f2017-04-27 13:42:17 -07001764 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001765 }
1766}
1767
chaviwa76b2712017-09-20 12:02:26 -07001768/**
1769 * Traverse only children in z order, ignoring relative layers.
1770 */
1771void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1772 const LayerVector::Visitor& visitor) {
1773 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1774 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1775
1776 size_t i = 0;
1777 for (; i < children.size(); i++) {
1778 const auto& relative = children[i];
1779 if (relative->getZ() >= 0) {
1780 break;
1781 }
1782 relative->traverseChildrenInZOrder(stateSet, visitor);
1783 }
1784 visitor(this);
1785 for (; i < children.size(); i++) {
1786 const auto& relative = children[i];
1787 relative->traverseChildrenInZOrder(stateSet, visitor);
1788 }
1789}
1790
Robert Carr1f0a16a2016-10-24 16:27:39 -07001791Transform Layer::getTransform() const {
1792 Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001793 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001794 if (p != nullptr) {
1795 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07001796
1797 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
1798 // it isFixedSize) then there may be additional scaling not accounted
1799 // for in the transform. We need to mirror this scaling in child surfaces
1800 // or we will break the contract where WM can treat child surfaces as
1801 // pixels in the parent surface.
David Sodman0cc69182017-11-17 12:12:07 -08001802 if (p->isFixedSize() && p->getBE().compositionInfo.mBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07001803 int bufferWidth;
1804 int bufferHeight;
1805 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
David Sodman0cc69182017-11-17 12:12:07 -08001806 bufferWidth = p->getBE().compositionInfo.mBuffer->getWidth();
1807 bufferHeight = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001808 } else {
David Sodman0cc69182017-11-17 12:12:07 -08001809 bufferHeight = p->getBE().compositionInfo.mBuffer->getWidth();
1810 bufferWidth = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001811 }
David Sodman41fdfc92017-11-06 16:09:56 -08001812 float sx = p->getDrawingState().active.w / static_cast<float>(bufferWidth);
1813 float sy = p->getDrawingState().active.h / static_cast<float>(bufferHeight);
Robert Carr9b429f42017-04-17 14:56:57 -07001814 Transform extraParentScaling;
1815 extraParentScaling.set(sx, 0, 0, sy);
1816 t = t * extraParentScaling;
1817 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001818 }
1819 return t * getDrawingState().active.transform;
1820}
1821
chaviw13fdc492017-06-27 12:40:18 -07001822half Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001823 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07001824
chaviw13fdc492017-06-27 12:40:18 -07001825 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1826 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001827}
Robert Carr6452f122017-03-21 10:41:29 -07001828
chaviw13fdc492017-06-27 12:40:18 -07001829half4 Layer::getColor() const {
1830 const half4 color(getDrawingState().color);
1831 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001832}
Robert Carr6452f122017-03-21 10:41:29 -07001833
Robert Carr1f0a16a2016-10-24 16:27:39 -07001834void Layer::commitChildList() {
1835 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1836 const auto& child = mCurrentChildren[i];
1837 child->commitChildList();
1838 }
1839 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001840 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001841}
1842
chaviw1d044282017-09-27 12:19:28 -07001843void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
1844 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1845 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1846 const State& state = useDrawing ? mDrawingState : mCurrentState;
1847
1848 Transform requestedTransform = state.active.transform;
1849 Transform transform = getTransform();
1850
1851 layerInfo->set_id(sequence);
1852 layerInfo->set_name(getName().c_str());
1853 layerInfo->set_type(String8(getTypeId()));
1854
1855 for (const auto& child : children) {
1856 layerInfo->add_children(child->sequence);
1857 }
1858
1859 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1860 sp<Layer> strongRelative = weakRelative.promote();
1861 if (strongRelative != nullptr) {
1862 layerInfo->add_relatives(strongRelative->sequence);
1863 }
1864 }
1865
1866 LayerProtoHelper::writeToProto(state.activeTransparentRegion,
1867 layerInfo->mutable_transparent_region());
1868 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
1869 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
1870
1871 layerInfo->set_layer_stack(getLayerStack());
1872 layerInfo->set_z(state.z);
1873
1874 PositionProto* position = layerInfo->mutable_position();
1875 position->set_x(transform.tx());
1876 position->set_y(transform.ty());
1877
1878 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
1879 requestedPosition->set_x(requestedTransform.tx());
1880 requestedPosition->set_y(requestedTransform.ty());
1881
1882 SizeProto* size = layerInfo->mutable_size();
1883 size->set_w(state.active.w);
1884 size->set_h(state.active.h);
1885
1886 LayerProtoHelper::writeToProto(state.crop, layerInfo->mutable_crop());
1887 LayerProtoHelper::writeToProto(state.finalCrop, layerInfo->mutable_final_crop());
1888
1889 layerInfo->set_is_opaque(isOpaque(state));
1890 layerInfo->set_invalidate(contentDirty);
1891 layerInfo->set_dataspace(dataspaceDetails(getDataSpace()));
1892 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1893 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
1894 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
1895 layerInfo->set_flags(state.flags);
1896
1897 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1898 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
1899
Jorim Jaggi8e0af362017-11-14 16:28:28 +01001900 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07001901 if (parent != nullptr) {
1902 layerInfo->set_parent(parent->sequence);
1903 }
1904
1905 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1906 if (zOrderRelativeOf != nullptr) {
1907 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1908 }
1909
David Sodman0cc69182017-11-17 12:12:07 -08001910 auto buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001911 if (buffer != nullptr) {
1912 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
chaviw1d044282017-09-27 12:19:28 -07001913 }
1914
1915 layerInfo->set_queued_frames(getQueuedFrameCount());
1916 layerInfo->set_refresh_pending(isBufferLatched());
1917}
1918
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001919void Layer::writeToProto(LayerProto* layerInfo, int32_t hwcId) {
1920 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
1921
1922 const auto& hwcInfo = getBE().mHwcLayers.at(hwcId);
1923
1924 const Rect& frame = hwcInfo.displayFrame;
1925 LayerProtoHelper::writeToProto(frame, layerInfo->mutable_hwc_frame());
1926
1927 const FloatRect& crop = hwcInfo.sourceCrop;
1928 LayerProtoHelper::writeToProto(crop, layerInfo->mutable_hwc_crop());
1929
1930 const int32_t transform = static_cast<int32_t>(hwcInfo.transform);
1931 layerInfo->set_hwc_transform(transform);
1932}
1933
Mathias Agopian13127d82013-03-05 17:47:11 -08001934// ---------------------------------------------------------------------------
1935
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001936}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07001937
1938#if defined(__gl_h_)
1939#error "don't include gl/gl.h in this file"
1940#endif
1941
1942#if defined(__gl2_h_)
1943#error "don't include gl2/gl2.h in this file"
1944#endif