blob: 3be7f47b34d7392c655b085e0e48ae4227b1e14e [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>
Mathias Agopiana9347642017-02-13 16:42:28 -080042#include <gui/BufferQueue.h>
Kalle Raitaa099a242017-01-11 11:17:29 -080043#include <gui/LayerDebugInfo.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080044#include <gui/Surface.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045
Mathias Agopian3e25fd82013-04-22 17:52:16 +020046#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070047#include "DisplayDevice.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048#include "Layer.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070049#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070050#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051#include "SurfaceFlinger.h"
David Sodman41fdfc92017-11-06 16:09:56 -080052#include "clz.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053
Mathias Agopian1b031492012-06-20 17:51:20 -070054#include "DisplayHardware/HWComposer.h"
55
Mathias Agopian875d8e12013-06-07 15:35:48 -070056#include "RenderEngine/RenderEngine.h"
57
Dan Stozac5da2712016-07-20 15:38:12 -070058#include <mutex>
chaviw1d044282017-09-27 12:19:28 -070059#include "LayerProtoHelper.h"
Dan Stozac5da2712016-07-20 15:38:12 -070060
David Sodman41fdfc92017-11-06 16:09:56 -080061#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063namespace android {
64
Mathias Agopian13127d82013-03-05 17:47:11 -080065int32_t Layer::sSequence = 1;
66
David Sodman41fdfc92017-11-06 16:09:56 -080067Layer::Layer(SurfaceFlinger* flinger, const sp<Client>& client, const String8& name, uint32_t w,
68 uint32_t h, uint32_t flags)
David Sodman0c69cad2017-08-21 12:12:51 -070069 : contentDirty(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080070 sequence(uint32_t(android_atomic_inc(&sSequence))),
71 mFlinger(flinger),
Mathias Agopian13127d82013-03-05 17:47:11 -080072 mPremultipliedAlpha(true),
David Sodman0c69cad2017-08-21 12:12:51 -070073 mName(name),
Mathias Agopian13127d82013-03-05 17:47:11 -080074 mTransactionFlags(0),
Dan Stoza7dde5992015-05-22 09:51:44 -070075 mPendingStateMutex(),
76 mPendingStates(),
Mathias Agopiana67932f2011-04-20 14:20:59 -070077 mQueuedFrames(0),
Jesse Hall399184a2014-03-03 15:42:54 -080078 mSidebandStreamChanged(false),
Mathias Agopiana9347642017-02-13 16:42:28 -080079 mActiveBufferSlot(BufferQueue::INVALID_BUFFER_SLOT),
Mathias Agopiana67932f2011-04-20 14:20:59 -070080 mCurrentTransform(0),
Robert Carrc3574f72016-03-24 12:19:32 -070081 mOverrideScalingMode(-1),
Mathias Agopiana67932f2011-04-20 14:20:59 -070082 mCurrentOpacity(true),
Dan Stozacac35382016-01-27 12:21:06 -080083 mCurrentFrameNumber(0),
Mathias Agopian82d7ab62012-01-19 18:34:40 -080084 mFrameLatencyNeeded(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080085 mFiltering(false),
86 mNeedsFiltering(false),
Mathias Agopian5cdc8992013-08-13 20:51:23 -070087 mMesh(Mesh::TRIANGLE_FAN, 4, 2, 2),
Mathias Agopian13127d82013-03-05 17:47:11 -080088 mProtectedByApp(false),
Riley Andrews03414a12014-07-01 14:22:59 -070089 mClientRef(client),
Dan Stozaa4650a52015-05-12 12:56:16 -070090 mPotentialCursor(false),
91 mQueueItemLock(),
92 mQueueItemCondition(),
93 mQueueItems(),
Dan Stoza65476f32015-05-14 09:27:25 -070094 mLastFrameNumberReceived(0),
Robert Carr82364e32016-05-15 11:27:47 -070095 mAutoRefresh(false),
David Sodman41fdfc92017-11-06 16:09:56 -080096 mFreezeGeometryUpdates(false) {
Dan Stoza9e56aa02015-11-02 13:00:03 -080097
Mathias Agopiana67932f2011-04-20 14:20:59 -070098 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070099
100 uint32_t layerFlags = 0;
David Sodman41fdfc92017-11-06 16:09:56 -0800101 if (flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
102 if (flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
103 if (flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700104
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700105 mName = name;
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700106 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700107
108 mCurrentState.active.w = w;
109 mCurrentState.active.h = h;
David Sodman0c69cad2017-08-21 12:12:51 -0700110 mCurrentState.flags = layerFlags;
Robert Carr3dcabfa2016-03-01 18:36:58 -0800111 mCurrentState.active.transform.set(0, 0);
Robert Carrb5d3d262016-03-25 15:08:13 -0700112 mCurrentState.crop.makeInvalid();
113 mCurrentState.finalCrop.makeInvalid();
Robert Carr7bf247e2017-05-18 14:02:49 -0700114 mCurrentState.requestedFinalCrop = mCurrentState.finalCrop;
115 mCurrentState.requestedCrop = mCurrentState.crop;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700116 mCurrentState.z = 0;
chaviw13fdc492017-06-27 12:40:18 -0700117 mCurrentState.color.a = 1.0f;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700118 mCurrentState.layerStack = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700119 mCurrentState.sequence = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700120 mCurrentState.requested = mCurrentState.active;
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -0700121 mCurrentState.dataSpace = HAL_DATASPACE_UNKNOWN;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500122 mCurrentState.appId = 0;
123 mCurrentState.type = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700124
125 // drawing state & current state are identical
126 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700127
Dan Stoza9e56aa02015-11-02 13:00:03 -0800128 const auto& hwc = flinger->getHwComposer();
129 const auto& activeConfig = hwc.getActiveConfig(HWC_DISPLAY_PRIMARY);
130 nsecs_t displayPeriod = activeConfig->getVsyncPeriod();
Jamie Gennis6547ff42013-07-16 20:12:42 -0700131 mFrameTracker.setDisplayRefreshPeriod(displayPeriod);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800132
133 CompositorTiming compositorTiming;
134 flinger->getCompositorTiming(&compositorTiming);
135 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jamie Gennise8696a42012-01-15 18:54:57 -0800136}
137
David Sodman41fdfc92017-11-06 16:09:56 -0800138void Layer::onFirstRef() {}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700139
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700140Layer::~Layer() {
Jamie Gennis6547ff42013-07-16 20:12:42 -0700141 mFrameTracker.logAndResetStats(mName);
Mathias Agopian96f08192010-06-02 23:28:45 -0700142}
143
Mathias Agopian13127d82013-03-05 17:47:11 -0800144// ---------------------------------------------------------------------------
145// callbacks
146// ---------------------------------------------------------------------------
147
David Sodmaneb085e02017-10-05 18:49:04 -0700148/*
149 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
150 * Layer. So, the implementation is done in BufferLayer. When called on a
151 * ColorLayer object, it's essentially a NOP.
152 */
David Sodmaneb085e02017-10-05 18:49:04 -0700153void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800154
Chia-I Wuc6657022017-08-15 11:18:17 -0700155void Layer::onRemovedFromCurrentState() {
156 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
157
chaviw8b3871a2017-11-01 17:41:01 -0700158 mPendingRemoval = true;
159
Robert Carr5edb1ad2017-04-25 10:54:24 -0700160 if (mCurrentState.zOrderRelativeOf != nullptr) {
161 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
162 if (strongRelative != nullptr) {
163 strongRelative->removeZOrderRelative(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700164 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700165 }
166 mCurrentState.zOrderRelativeOf = nullptr;
167 }
168
Chia-I Wuc6657022017-08-15 11:18:17 -0700169 for (const auto& child : mCurrentChildren) {
170 child->onRemovedFromCurrentState();
171 }
172}
Chia-I Wu38512252017-05-17 14:36:16 -0700173
Chia-I Wuc6657022017-08-15 11:18:17 -0700174void Layer::onRemoved() {
175 // the layer is removed from SF mLayersPendingRemoval
David Sodmaneb085e02017-10-05 18:49:04 -0700176 abandon();
Chia-I Wuc6657022017-08-15 11:18:17 -0700177
Steven Thomasb02664d2017-07-26 18:48:28 -0700178 destroyAllHwcLayers();
Chia-I Wu38512252017-05-17 14:36:16 -0700179
Robert Carr1f0a16a2016-10-24 16:27:39 -0700180 for (const auto& child : mCurrentChildren) {
181 child->onRemoved();
182 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700183}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700184
Mathias Agopian13127d82013-03-05 17:47:11 -0800185// ---------------------------------------------------------------------------
186// set-up
187// ---------------------------------------------------------------------------
188
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700189const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800190 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800191}
192
chaviw13fdc492017-06-27 12:40:18 -0700193bool Layer::getPremultipledAlpha() const {
194 return mPremultipliedAlpha;
195}
196
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700197sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800198 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700199 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800200}
201
202// ---------------------------------------------------------------------------
203// h/w composer set-up
204// ---------------------------------------------------------------------------
205
Steven Thomasb02664d2017-07-26 18:48:28 -0700206bool Layer::createHwcLayer(HWComposer* hwc, int32_t hwcId) {
David Sodman41fdfc92017-11-06 16:09:56 -0800207 LOG_ALWAYS_FATAL_IF(mHwcLayers.count(hwcId) != 0, "Already have a layer for hwcId %d", hwcId);
Steven Thomasb02664d2017-07-26 18:48:28 -0700208 HWC2::Layer* layer = hwc->createLayer(hwcId);
209 if (!layer) {
210 return false;
211 }
212 HWCInfo& hwcInfo = mHwcLayers[hwcId];
213 hwcInfo.hwc = hwc;
214 hwcInfo.layer = layer;
215 layer->setLayerDestroyedListener(
David Sodman41fdfc92017-11-06 16:09:56 -0800216 [this, hwcId](HWC2::Layer* /*layer*/) { mHwcLayers.erase(hwcId); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700217 return true;
218}
219
Chia-I Wu83806892017-11-16 10:50:20 -0800220bool Layer::destroyHwcLayer(int32_t hwcId) {
Steven Thomasb02664d2017-07-26 18:48:28 -0700221 if (mHwcLayers.count(hwcId) == 0) {
Chia-I Wu83806892017-11-16 10:50:20 -0800222 return false;
Steven Thomasb02664d2017-07-26 18:48:28 -0700223 }
224 auto& hwcInfo = mHwcLayers[hwcId];
David Sodman41fdfc92017-11-06 16:09:56 -0800225 LOG_ALWAYS_FATAL_IF(hwcInfo.layer == nullptr, "Attempt to destroy null layer");
Steven Thomasb02664d2017-07-26 18:48:28 -0700226 LOG_ALWAYS_FATAL_IF(hwcInfo.hwc == nullptr, "Missing HWComposer");
227 hwcInfo.hwc->destroyLayer(hwcId, hwcInfo.layer);
228 // The layer destroyed listener should have cleared the entry from
229 // mHwcLayers. Verify that.
David Sodman41fdfc92017-11-06 16:09:56 -0800230 LOG_ALWAYS_FATAL_IF(mHwcLayers.count(hwcId) != 0, "Stale layer entry in mHwcLayers");
Chia-I Wu83806892017-11-16 10:50:20 -0800231
232 return true;
Steven Thomasb02664d2017-07-26 18:48:28 -0700233}
234
235void Layer::destroyAllHwcLayers() {
236 size_t numLayers = mHwcLayers.size();
237 for (size_t i = 0; i < numLayers; ++i) {
238 LOG_ALWAYS_FATAL_IF(mHwcLayers.empty(), "destroyAllHwcLayers failed");
239 destroyHwcLayer(mHwcLayers.begin()->first);
240 }
241 LOG_ALWAYS_FATAL_IF(!mHwcLayers.empty(),
David Sodman41fdfc92017-11-06 16:09:56 -0800242 "All hardware composer layers should have been destroyed");
Steven Thomasb02664d2017-07-26 18:48:28 -0700243}
Steven Thomasb02664d2017-07-26 18:48:28 -0700244
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800245Rect Layer::getContentCrop() const {
246 // this is the crop rectangle that applies to the buffer
247 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700248 Rect crop;
249 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800250 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700251 crop = mCurrentCrop;
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800252 } else if (mActiveBuffer != NULL) {
253 // otherwise we use the whole buffer
254 crop = mActiveBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700255 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800256 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700257 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700258 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700259 return crop;
260}
261
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700262static Rect reduce(const Rect& win, const Region& exclude) {
263 if (CC_LIKELY(exclude.isEmpty())) {
264 return win;
265 }
266 if (exclude.isRect()) {
267 return win.reduce(exclude.getBounds());
268 }
269 return Region(win).subtract(exclude).getBounds();
270}
271
Robert Carr1f0a16a2016-10-24 16:27:39 -0700272Rect Layer::computeScreenBounds(bool reduceTransparentRegion) const {
273 const Layer::State& s(getDrawingState());
274 Rect win(s.active.w, s.active.h);
275
276 if (!s.crop.isEmpty()) {
277 win.intersect(s.crop, &win);
278 }
279
280 Transform t = getTransform();
281 win = t.transform(win);
282
Robert Carr41b08b52017-06-01 16:11:34 -0700283 if (!s.finalCrop.isEmpty()) {
284 win.intersect(s.finalCrop, &win);
285 }
286
Chia-I Wue41dbe62017-06-13 14:10:56 -0700287 const sp<Layer>& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700288 // Now we need to calculate the parent bounds, so we can clip ourselves to those.
289 // When calculating the parent bounds for purposes of clipping,
290 // we don't need to constrain the parent to its transparent region.
291 // The transparent region is an optimization based on the
292 // buffer contents of the layer, but does not affect the space allocated to
293 // it by policy, and thus children should be allowed to extend into the
294 // parent's transparent region. In fact one of the main uses, is to reduce
295 // buffer allocation size in cases where a child window sits behind a main window
296 // (by marking the hole in the parent window as a transparent region)
297 if (p != nullptr) {
298 Rect bounds = p->computeScreenBounds(false);
299 bounds.intersect(win, &win);
300 }
301
302 if (reduceTransparentRegion) {
303 auto const screenTransparentRegion = t.transform(s.activeTransparentRegion);
304 win = reduce(win, screenTransparentRegion);
305 }
306
307 return win;
308}
309
Mathias Agopian13127d82013-03-05 17:47:11 -0800310Rect Layer::computeBounds() const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700311 const Layer::State& s(getDrawingState());
Michael Lentine6c925ed2014-09-26 17:55:01 -0700312 return computeBounds(s.activeTransparentRegion);
313}
314
315Rect Layer::computeBounds(const Region& activeTransparentRegion) const {
316 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800317 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700318
319 if (!s.crop.isEmpty()) {
320 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800321 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700322
323 Rect bounds = win;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700324 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700325 if (p != nullptr) {
Robert Carrde9ec442017-02-08 17:43:36 -0800326 // Look in computeScreenBounds recursive call for explanation of
327 // why we pass false here.
328 bounds = p->computeScreenBounds(false /* reduceTransparentRegion */);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700329 }
330
331 Transform t = getTransform();
332 if (p != nullptr) {
333 win = t.transform(win);
334 win.intersect(bounds, &win);
335 win = t.inverse().transform(win);
336 }
337
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700338 // subtract the transparent region and snap to the bounds
Michael Lentine6c925ed2014-09-26 17:55:01 -0700339 return reduce(win, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800340}
341
Robert Carr1f0a16a2016-10-24 16:27:39 -0700342Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& hw) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700343 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800344 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700345 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800346
347 // apply the projection's clipping to the window crop in
348 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700349 // if there are no window scaling involved, this operation will map to full
350 // pixels in the buffer.
351 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
352 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700353
354 Rect activeCrop(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700355 if (!s.crop.isEmpty()) {
Chia-I Wudf7867f2017-07-20 14:24:37 -0700356 activeCrop.intersect(s.crop, &activeCrop);
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700357 }
358
Robert Carr1f0a16a2016-10-24 16:27:39 -0700359 Transform t = getTransform();
360 activeCrop = t.transform(activeCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000361 if (!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
362 activeCrop.clear();
363 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700364 if (!s.finalCrop.isEmpty()) {
David Sodman41fdfc92017-11-06 16:09:56 -0800365 if (!activeCrop.intersect(s.finalCrop, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000366 activeCrop.clear();
367 }
368 }
chaviwb1154d12017-10-31 14:15:36 -0700369
370 const auto& p = mDrawingParent.promote();
371 if (p != nullptr) {
372 auto parentCrop = p->computeInitialCrop(hw);
373 activeCrop.intersect(parentCrop, &activeCrop);
374 }
375
Robert Carr1f0a16a2016-10-24 16:27:39 -0700376 return activeCrop;
377}
378
Dan Stoza5a423ea2017-02-16 14:10:39 -0800379FloatRect Layer::computeCrop(const sp<const DisplayDevice>& hw) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700380 // the content crop is the area of the content that gets scaled to the
381 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800382 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700383
384 // In addition there is a WM-specified crop we pull from our drawing state.
385 const State& s(getDrawingState());
386
387 // Screen space to make reduction to parent crop clearer.
388 Rect activeCrop = computeInitialCrop(hw);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700389 Transform t = getTransform();
390 // Back to layer space to work with the content crop.
391 activeCrop = t.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800392
Michael Lentine28ea2172014-11-19 18:32:37 -0800393 // This needs to be here as transform.transform(Rect) computes the
394 // transformed rect and then takes the bounding box of the result before
395 // returning. This means
396 // transform.inverse().transform(transform.transform(Rect)) != Rect
397 // in which case we need to make sure the final rect is clipped to the
398 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000399 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
400 activeCrop.clear();
401 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800402
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700403 // subtract the transparent region and snap to the bounds
404 activeCrop = reduce(activeCrop, s.activeTransparentRegion);
405
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000406 // Transform the window crop to match the buffer coordinate system,
407 // which means using the inverse of the current transform set on the
408 // SurfaceFlingerConsumer.
409 uint32_t invTransform = mCurrentTransform;
Robert Carrcae605c2017-03-29 12:10:31 -0700410 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000411 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700412 * the code below applies the primary display's inverse transform to the
413 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000414 */
David Sodman41fdfc92017-11-06 16:09:56 -0800415 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000416 // calculate the inverse transform
417 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800418 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800419 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000420 // and apply to the current transform
David Sodman41fdfc92017-11-06 16:09:56 -0800421 invTransform = (Transform(invTransformOrient) * Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800422 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000423
424 int winWidth = s.active.w;
425 int winHeight = s.active.h;
426 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
427 // If the activeCrop has been rotate the ends are rotated but not
428 // the space itself so when transforming ends back we can't rely on
429 // a modification of the axes of rotation. To account for this we
430 // need to reorient the inverse rotation in terms of the current
431 // axes of rotation.
432 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
433 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
434 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800435 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000436 }
437 winWidth = s.active.h;
438 winHeight = s.active.w;
439 }
David Sodman41fdfc92017-11-06 16:09:56 -0800440 const Rect winCrop = activeCrop.transform(invTransform, s.active.w, s.active.h);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000441
442 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800443 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000444 float yScale = crop.getHeight() / float(winHeight);
445
David Sodman41fdfc92017-11-06 16:09:56 -0800446 float insetL = winCrop.left * xScale;
447 float insetT = winCrop.top * yScale;
448 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000449 float insetB = (winHeight - winCrop.bottom) * yScale;
450
David Sodman41fdfc92017-11-06 16:09:56 -0800451 crop.left += insetL;
452 crop.top += insetT;
453 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000454 crop.bottom -= insetB;
455
Mathias Agopian13127d82013-03-05 17:47:11 -0800456 return crop;
457}
458
Robert Carrae060832016-11-28 10:51:00 -0800459void Layer::setGeometry(const sp<const DisplayDevice>& displayDevice, uint32_t z)
Mathias Agopiana350ff92010-08-10 17:14:02 -0700460{
Dan Stoza9e56aa02015-11-02 13:00:03 -0800461 const auto hwcId = displayDevice->getHwcDisplayId();
462 auto& hwcInfo = mHwcLayers[hwcId];
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700463
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700464 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800465 hwcInfo.forceClientComposition = false;
466
467 if (isSecure() && !displayDevice->isSecure()) {
468 hwcInfo.forceClientComposition = true;
469 }
470
471 auto& hwcLayer = hwcInfo.layer;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700472
Mathias Agopian13127d82013-03-05 17:47:11 -0800473 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700474 const State& s(getDrawingState());
David Revemanecf0fa52017-03-03 11:32:44 -0500475 auto blendMode = HWC2::BlendMode::None;
Robert Carr6452f122017-03-21 10:41:29 -0700476 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800477 blendMode =
478 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800479 }
David Revemanecf0fa52017-03-03 11:32:44 -0500480 auto error = hwcLayer->setBlendMode(blendMode);
David Sodman41fdfc92017-11-06 16:09:56 -0800481 ALOGE_IF(error != HWC2::Error::None,
482 "[%s] Failed to set blend mode %s:"
483 " %s (%d)",
484 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
485 static_cast<int32_t>(error));
Mathias Agopian13127d82013-03-05 17:47:11 -0800486
487 // apply the layer's transform, followed by the display's global transform
488 // here we're guaranteed that the layer's transform preserves rects
Michael Lentine6c925ed2014-09-26 17:55:01 -0700489 Region activeTransparentRegion(s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700490 Transform t = getTransform();
Robert Carrb5d3d262016-03-25 15:08:13 -0700491 if (!s.crop.isEmpty()) {
492 Rect activeCrop(s.crop);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700493 activeCrop = t.transform(activeCrop);
David Sodman41fdfc92017-11-06 16:09:56 -0800494 if (!activeCrop.intersect(displayDevice->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000495 activeCrop.clear();
496 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700497 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800498 // This needs to be here as transform.transform(Rect) computes the
499 // transformed rect and then takes the bounding box of the result before
500 // returning. This means
501 // transform.inverse().transform(transform.transform(Rect)) != Rect
502 // in which case we need to make sure the final rect is clipped to the
503 // display bounds.
David Sodman41fdfc92017-11-06 16:09:56 -0800504 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000505 activeCrop.clear();
506 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700507 // mark regions outside the crop as transparent
508 activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top));
David Sodman41fdfc92017-11-06 16:09:56 -0800509 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom, s.active.w, s.active.h));
510 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
511 activeTransparentRegion.orSelf(
512 Rect(activeCrop.right, activeCrop.top, s.active.w, activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700513 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700514
515 Rect frame(t.transform(computeBounds(activeTransparentRegion)));
Robert Carrb5d3d262016-03-25 15:08:13 -0700516 if (!s.finalCrop.isEmpty()) {
David Sodman41fdfc92017-11-06 16:09:56 -0800517 if (!frame.intersect(s.finalCrop, &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000518 frame.clear();
519 }
520 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000521 if (!frame.intersect(displayDevice->getViewport(), &frame)) {
522 frame.clear();
523 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800524 const Transform& tr(displayDevice->getTransform());
525 Rect transformedFrame = tr.transform(frame);
David Revemanecf0fa52017-03-03 11:32:44 -0500526 error = hwcLayer->setDisplayFrame(transformedFrame);
Dan Stozae22aec72016-08-01 13:20:59 -0700527 if (error != HWC2::Error::None) {
David Sodman41fdfc92017-11-06 16:09:56 -0800528 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
529 transformedFrame.left, transformedFrame.top, transformedFrame.right,
530 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stozae22aec72016-08-01 13:20:59 -0700531 } else {
532 hwcInfo.displayFrame = transformedFrame;
533 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800534
Dan Stoza5a423ea2017-02-16 14:10:39 -0800535 FloatRect sourceCrop = computeCrop(displayDevice);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800536 error = hwcLayer->setSourceCrop(sourceCrop);
Dan Stozae22aec72016-08-01 13:20:59 -0700537 if (error != HWC2::Error::None) {
538 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
David Sodman41fdfc92017-11-06 16:09:56 -0800539 "%s (%d)",
540 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
541 to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stozae22aec72016-08-01 13:20:59 -0700542 } else {
543 hwcInfo.sourceCrop = sourceCrop;
544 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800545
chaviw13fdc492017-06-27 12:40:18 -0700546 float alpha = static_cast<float>(getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -0700547 error = hwcLayer->setPlaneAlpha(alpha);
David Sodman41fdfc92017-11-06 16:09:56 -0800548 ALOGE_IF(error != HWC2::Error::None,
549 "[%s] Failed to set plane alpha %.3f: "
550 "%s (%d)",
551 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800552
Robert Carrae060832016-11-28 10:51:00 -0800553 error = hwcLayer->setZOrder(z);
David Sodman41fdfc92017-11-06 16:09:56 -0800554 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
555 to_string(error).c_str(), static_cast<int32_t>(error));
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500556
Albert Chaulk2a589632017-05-04 16:59:44 -0400557 int type = s.type;
558 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700559 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400560 if (parent.get()) {
561 auto& parentState = parent->getDrawingState();
562 type = parentState.type;
563 appId = parentState.appId;
564 }
565
566 error = hwcLayer->setInfo(type, appId);
David Sodman41fdfc92017-11-06 16:09:56 -0800567 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
568 static_cast<int32_t>(error));
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800569
Mathias Agopian29a367b2011-07-12 14:51:45 -0700570 /*
571 * Transformations are applied in this order:
572 * 1) buffer orientation/flip/mirror
573 * 2) state transformation (window manager)
574 * 3) layer orientation (screen orientation)
575 * (NOTE: the matrices are multiplied in reverse order)
576 */
577
578 const Transform bufferOrientation(mCurrentTransform);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700579 Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700580
Robert Carrcae605c2017-03-29 12:10:31 -0700581 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700582 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700583 * the code below applies the primary display's inverse transform to the
584 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700585 */
David Sodman41fdfc92017-11-06 16:09:56 -0800586 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700587 // calculate the inverse transform
588 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800589 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700590 }
Robert Carrcae605c2017-03-29 12:10:31 -0700591
592 /*
593 * Here we cancel out the orientation component of the WM transform.
594 * The scaling and translate components are already included in our bounds
595 * computation so it's enough to just omit it in the composition.
596 * See comment in onDraw with ref to b/36727915 for why.
597 */
598 transform = Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700599 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700600
601 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800602 const uint32_t orientation = transform.getOrientation();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800603 if (orientation & Transform::ROT_INVALID) {
604 // we can only handle simple transformation
605 hwcInfo.forceClientComposition = true;
606 } else {
607 auto transform = static_cast<HWC2::Transform>(orientation);
608 auto error = hwcLayer->setTransform(transform);
David Sodman41fdfc92017-11-06 16:09:56 -0800609 ALOGE_IF(error != HWC2::Error::None,
610 "[%s] Failed to set transform %s: "
611 "%s (%d)",
612 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
613 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800614 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700615}
616
Dan Stoza9e56aa02015-11-02 13:00:03 -0800617void Layer::forceClientComposition(int32_t hwcId) {
618 if (mHwcLayers.count(hwcId) == 0) {
619 ALOGE("forceClientComposition: no HWC layer found (%d)", hwcId);
620 return;
621 }
622
623 mHwcLayers[hwcId].forceClientComposition = true;
624}
Dan Stozaee44edd2015-03-23 15:50:23 -0700625
chaviwc9232ed2017-11-14 15:31:15 -0800626bool Layer::getForceClientComposition(int32_t hwcId) {
627 if (mHwcLayers.count(hwcId) == 0) {
628 ALOGE("getForceClientComposition: no HWC layer found (%d)", hwcId);
629 return false;
630 }
631
632 return mHwcLayers[hwcId].forceClientComposition;
633}
634
Dan Stoza9e56aa02015-11-02 13:00:03 -0800635void Layer::updateCursorPosition(const sp<const DisplayDevice>& displayDevice) {
636 auto hwcId = displayDevice->getHwcDisplayId();
David Sodman41fdfc92017-11-06 16:09:56 -0800637 if (mHwcLayers.count(hwcId) == 0 || getCompositionType(hwcId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800638 return;
639 }
640
641 // This gives us only the "orientation" component of the transform
642 const State& s(getCurrentState());
643
644 // Apply the layer's transform, followed by the display's global transform
645 // Here we're guaranteed that the layer's transform preserves rects
646 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700647 if (!s.crop.isEmpty()) {
648 win.intersect(s.crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800649 }
650 // Subtract the transparent region and snap to the bounds
651 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700652 Rect frame(getTransform().transform(bounds));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800653 frame.intersect(displayDevice->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700654 if (!s.finalCrop.isEmpty()) {
655 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000656 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800657 auto& displayTransform(displayDevice->getTransform());
658 auto position = displayTransform.transform(frame);
659
David Sodman41fdfc92017-11-06 16:09:56 -0800660 auto error = mHwcLayers[hwcId].layer->setCursorPosition(position.left, position.top);
661 ALOGE_IF(error != HWC2::Error::None,
662 "[%s] Failed to set cursor position "
663 "to (%d, %d): %s (%d)",
664 mName.string(), position.left, position.top, to_string(error).c_str(),
665 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800666}
Riley Andrews03414a12014-07-01 14:22:59 -0700667
Mathias Agopian13127d82013-03-05 17:47:11 -0800668// ---------------------------------------------------------------------------
669// drawing...
670// ---------------------------------------------------------------------------
671
chaviwa76b2712017-09-20 12:02:26 -0700672void Layer::draw(const RenderArea& renderArea, const Region& clip) const {
673 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800674}
675
chaviwa76b2712017-09-20 12:02:26 -0700676void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) const {
677 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800678}
679
chaviwa76b2712017-09-20 12:02:26 -0700680void Layer::draw(const RenderArea& renderArea) const {
681 onDraw(renderArea, Region(renderArea.getBounds()), false);
Dan Stozac7014012014-02-14 15:03:43 -0800682}
683
David Sodman41fdfc92017-11-06 16:09:56 -0800684void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
685 float alpha) const {
Mathias Agopian19733a32013-08-28 18:13:56 -0700686 RenderEngine& engine(mFlinger->getRenderEngine());
chaviwa76b2712017-09-20 12:02:26 -0700687 computeGeometry(renderArea, mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700688 engine.setupFillWithColor(red, green, blue, alpha);
689 engine.drawMesh(mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800690}
691
chaviwa76b2712017-09-20 12:02:26 -0700692void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
David Sodman41fdfc92017-11-06 16:09:56 -0800693 clearWithOpenGL(renderArea, 0, 0, 0, 0);
Mathias Agopian13127d82013-03-05 17:47:11 -0800694}
695
David Sodman41fdfc92017-11-06 16:09:56 -0800696void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type, bool callIntoHwc) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800697 if (mHwcLayers.count(hwcId) == 0) {
698 ALOGE("setCompositionType called without a valid HWC layer");
699 return;
700 }
701 auto& hwcInfo = mHwcLayers[hwcId];
702 auto& hwcLayer = hwcInfo.layer;
David Sodman41fdfc92017-11-06 16:09:56 -0800703 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(), to_string(type).c_str(),
704 static_cast<int>(callIntoHwc));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800705 if (hwcInfo.compositionType != type) {
706 ALOGV(" actually setting");
707 hwcInfo.compositionType = type;
708 if (callIntoHwc) {
709 auto error = hwcLayer->setCompositionType(type);
David Sodman41fdfc92017-11-06 16:09:56 -0800710 ALOGE_IF(error != HWC2::Error::None,
711 "[%s] Failed to set "
712 "composition type %s: %s (%d)",
713 mName.string(), to_string(type).c_str(), to_string(error).c_str(),
714 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800715 }
716 }
717}
718
719HWC2::Composition Layer::getCompositionType(int32_t hwcId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -0700720 if (hwcId == DisplayDevice::DISPLAY_ID_INVALID) {
721 // If we're querying the composition type for a display that does not
722 // have a HWC counterpart, then it will always be Client
723 return HWC2::Composition::Client;
724 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800725 if (mHwcLayers.count(hwcId) == 0) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700726 ALOGE("getCompositionType called with an invalid HWC layer");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800727 return HWC2::Composition::Invalid;
728 }
729 return mHwcLayers.at(hwcId).compositionType;
730}
731
732void Layer::setClearClientTarget(int32_t hwcId, bool clear) {
733 if (mHwcLayers.count(hwcId) == 0) {
734 ALOGE("setClearClientTarget called without a valid HWC layer");
735 return;
736 }
737 mHwcLayers[hwcId].clearClientTarget = clear;
738}
739
740bool Layer::getClearClientTarget(int32_t hwcId) const {
741 if (mHwcLayers.count(hwcId) == 0) {
742 ALOGE("getClearClientTarget called without a valid HWC layer");
743 return false;
744 }
745 return mHwcLayers.at(hwcId).clearClientTarget;
746}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800747
Dan Stozacac35382016-01-27 12:21:06 -0800748bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
749 if (point->getFrameNumber() <= mCurrentFrameNumber) {
750 // Don't bother with a SyncPoint, since we've already latched the
751 // relevant frame
752 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700753 }
754
Dan Stozacac35382016-01-27 12:21:06 -0800755 Mutex::Autolock lock(mLocalSyncPointMutex);
756 mLocalSyncPoints.push_back(point);
757 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700758}
759
Mathias Agopian13127d82013-03-05 17:47:11 -0800760void Layer::setFiltering(bool filtering) {
761 mFiltering = filtering;
762}
763
764bool Layer::getFiltering() const {
765 return mFiltering;
766}
767
Mathias Agopian13127d82013-03-05 17:47:11 -0800768// ----------------------------------------------------------------------------
769// local state
770// ----------------------------------------------------------------------------
771
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000772static void boundPoint(vec2* point, const Rect& crop) {
773 if (point->x < crop.left) {
774 point->x = crop.left;
775 }
776 if (point->x > crop.right) {
777 point->x = crop.right;
778 }
779 if (point->y < crop.top) {
780 point->y = crop.top;
781 }
782 if (point->y > crop.bottom) {
783 point->y = crop.bottom;
784 }
785}
786
chaviwa76b2712017-09-20 12:02:26 -0700787void Layer::computeGeometry(const RenderArea& renderArea, Mesh& mesh,
788 bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700789 const Layer::State& s(getDrawingState());
chaviwa76b2712017-09-20 12:02:26 -0700790 const Transform renderAreaTransform(renderArea.getTransform());
791 const uint32_t height = renderArea.getHeight();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700792 Rect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700793
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000794 vec2 lt = vec2(win.left, win.top);
795 vec2 lb = vec2(win.left, win.bottom);
796 vec2 rb = vec2(win.right, win.bottom);
797 vec2 rt = vec2(win.right, win.top);
798
Robert Carr1f0a16a2016-10-24 16:27:39 -0700799 Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000800 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700801 lt = layerTransform.transform(lt);
802 lb = layerTransform.transform(lb);
803 rb = layerTransform.transform(rb);
804 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000805 }
806
Robert Carrb5d3d262016-03-25 15:08:13 -0700807 if (!s.finalCrop.isEmpty()) {
808 boundPoint(&lt, s.finalCrop);
809 boundPoint(&lb, s.finalCrop);
810 boundPoint(&rb, s.finalCrop);
811 boundPoint(&rt, s.finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000812 }
813
Mathias Agopianff2ed702013-09-01 21:36:12 -0700814 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700815 position[0] = renderAreaTransform.transform(lt);
816 position[1] = renderAreaTransform.transform(lb);
817 position[2] = renderAreaTransform.transform(rb);
818 position[3] = renderAreaTransform.transform(rt);
David Sodman41fdfc92017-11-06 16:09:56 -0800819 for (size_t i = 0; i < 4; i++) {
chaviwa76b2712017-09-20 12:02:26 -0700820 position[i].y = height - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -0800821 }
822}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800823
David Sodman41fdfc92017-11-06 16:09:56 -0800824bool Layer::isSecure() const {
Dan Stoza23116082015-06-18 14:58:39 -0700825 const Layer::State& s(mDrawingState);
826 return (s.flags & layer_state_t::eLayerSecure);
827}
828
Mathias Agopian13127d82013-03-05 17:47:11 -0800829void Layer::setVisibleRegion(const Region& visibleRegion) {
830 // always called from main thread
831 this->visibleRegion = visibleRegion;
832}
833
834void Layer::setCoveredRegion(const Region& coveredRegion) {
835 // always called from main thread
836 this->coveredRegion = coveredRegion;
837}
838
David Sodman41fdfc92017-11-06 16:09:56 -0800839void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800840 // always called from main thread
841 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
842}
843
844// ----------------------------------------------------------------------------
845// transaction
846// ----------------------------------------------------------------------------
847
Dan Stoza7dde5992015-05-22 09:51:44 -0700848void Layer::pushPendingState() {
849 if (!mCurrentState.modified) {
850 return;
851 }
852
Dan Stoza7dde5992015-05-22 09:51:44 -0700853 // If this transaction is waiting on the receipt of a frame, generate a sync
854 // point and send it to the remote layer.
Robert Carr0d480722017-01-10 16:42:54 -0800855 if (mCurrentState.barrierLayer != nullptr) {
856 sp<Layer> barrierLayer = mCurrentState.barrierLayer.promote();
857 if (barrierLayer == nullptr) {
858 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700859 // If we can't promote the layer we are intended to wait on,
860 // then it is expired or otherwise invalid. Allow this transaction
861 // to be applied as per normal (no synchronization).
Robert Carr0d480722017-01-10 16:42:54 -0800862 mCurrentState.barrierLayer = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800863 } else {
David Sodman41fdfc92017-11-06 16:09:56 -0800864 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber);
Robert Carr0d480722017-01-10 16:42:54 -0800865 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800866 mRemoteSyncPoints.push_back(std::move(syncPoint));
867 } else {
868 // We already missed the frame we're supposed to synchronize
869 // on, so go ahead and apply the state update
Robert Carr0d480722017-01-10 16:42:54 -0800870 mCurrentState.barrierLayer = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800871 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700872 }
873
Dan Stoza7dde5992015-05-22 09:51:44 -0700874 // Wake us up to check if the frame has been received
875 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700876 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700877 }
878 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700879 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700880}
881
Pablo Ceballos05289c22016-04-14 15:49:55 -0700882void Layer::popPendingState(State* stateToCommit) {
883 auto oldFlags = stateToCommit->flags;
884 *stateToCommit = mPendingStates[0];
David Sodman41fdfc92017-11-06 16:09:56 -0800885 stateToCommit->flags =
886 (oldFlags & ~stateToCommit->mask) | (stateToCommit->flags & stateToCommit->mask);
Dan Stoza7dde5992015-05-22 09:51:44 -0700887
888 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700889 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700890}
891
Pablo Ceballos05289c22016-04-14 15:49:55 -0700892bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700893 bool stateUpdateAvailable = false;
894 while (!mPendingStates.empty()) {
Robert Carr0d480722017-01-10 16:42:54 -0800895 if (mPendingStates[0].barrierLayer != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700896 if (mRemoteSyncPoints.empty()) {
897 // If we don't have a sync point for this, apply it anyway. It
898 // will be visually wrong, but it should keep us from getting
899 // into too much trouble.
900 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700901 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700902 stateUpdateAvailable = true;
903 continue;
904 }
905
David Sodman41fdfc92017-11-06 16:09:56 -0800906 if (mRemoteSyncPoints.front()->getFrameNumber() != mPendingStates[0].frameNumber) {
907 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800908
909 // Signal our end of the sync point and then dispose of it
910 mRemoteSyncPoints.front()->setTransactionApplied();
911 mRemoteSyncPoints.pop_front();
912 continue;
913 }
914
Dan Stoza7dde5992015-05-22 09:51:44 -0700915 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
916 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700917 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700918 stateUpdateAvailable = true;
919
920 // Signal our end of the sync point and then dispose of it
921 mRemoteSyncPoints.front()->setTransactionApplied();
922 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800923 } else {
924 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700925 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700926 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700927 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700928 stateUpdateAvailable = true;
929 }
930 }
931
932 // If we still have pending updates, wake SurfaceFlinger back up and point
933 // it at this layer so we can process them
934 if (!mPendingStates.empty()) {
935 setTransactionFlags(eTransactionNeeded);
936 mFlinger->setTransactionFlags(eTraversalNeeded);
937 }
938
939 mCurrentState.modified = false;
940 return stateUpdateAvailable;
941}
942
Mathias Agopian13127d82013-03-05 17:47:11 -0800943uint32_t Layer::doTransaction(uint32_t flags) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800944 ATRACE_CALL();
945
Dan Stoza7dde5992015-05-22 09:51:44 -0700946 pushPendingState();
Pablo Ceballos05289c22016-04-14 15:49:55 -0700947 Layer::State c = getCurrentState();
948 if (!applyPendingStates(&c)) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700949 return 0;
950 }
951
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700952 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800953
David Sodman41fdfc92017-11-06 16:09:56 -0800954 const bool sizeChanged = (c.requested.w != s.requested.w) || (c.requested.h != s.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700955
David Sodmaneb085e02017-10-05 18:49:04 -0700956 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700957 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000958 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800959 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
960 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
961 " requested={ wh={%4u,%4u} }}\n"
962 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
963 " requested={ wh={%4u,%4u} }}\n",
964 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(), c.active.w,
965 c.active.h, c.crop.left, c.crop.top, c.crop.right, c.crop.bottom,
966 c.crop.getWidth(), c.crop.getHeight(), c.requested.w, c.requested.h, s.active.w,
967 s.active.h, s.crop.left, s.crop.top, s.crop.right, s.crop.bottom,
968 s.crop.getWidth(), s.crop.getHeight(), s.requested.w, s.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800969
Jamie Gennis2a0d5b62011-09-26 16:54:44 -0700970 // record the new size, form this point on, when the client request
971 // a buffer, it'll get the new size.
David Sodmaneb085e02017-10-05 18:49:04 -0700972 setDefaultBufferSize(c.requested.w, c.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800973 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700974
Robert Carre392b552017-09-19 12:16:05 -0700975 // Don't let Layer::doTransaction update the drawing state
976 // if we have a pending resize, unless we are in fixed-size mode.
977 // the drawing state will be updated only once we receive a buffer
978 // with the correct size.
979 //
980 // In particular, we want to make sure the clip (which is part
981 // of the geometry state) is latched together with the size but is
982 // latched immediately when no resizing is involved.
983 //
984 // If a sideband stream is attached, however, we want to skip this
985 // optimization so that transactions aren't missed when a buffer
986 // never arrives
987 //
988 // In the case that we don't have a buffer we ignore other factors
989 // and avoid entering the resizePending state. At a high level the
990 // resizePending state is to avoid applying the state of the new buffer
991 // to the old buffer. However in the state where we don't have an old buffer
992 // there is no such concern but we may still be being used as a parent layer.
David Sodman41fdfc92017-11-06 16:09:56 -0800993 const bool resizePending = ((c.requested.w != c.active.w) || (c.requested.h != c.active.h)) &&
994 (mActiveBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700995 if (!isFixedSize()) {
Dan Stoza9e9b0442015-04-22 14:59:08 -0700996 if (resizePending && mSidebandStream == NULL) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700997 flags |= eDontUpdateGeometryState;
998 }
999 }
1000
Robert Carr7bf247e2017-05-18 14:02:49 -07001001 // Here we apply various requested geometry states, depending on our
1002 // latching configuration. See Layer.h for a detailed discussion of
1003 // how geometry latching is controlled.
1004 if (!(flags & eDontUpdateGeometryState)) {
Pablo Ceballos7d052572016-06-02 17:46:05 -07001005 Layer::State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001006
1007 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1008 // mode, which causes attributes which normally latch regardless of scaling mode,
1009 // to be delayed. We copy the requested state to the active state making sure
1010 // to respect these rules (again see Layer.h for a detailed discussion).
1011 //
1012 // There is an awkward asymmetry in the handling of the crop states in the position
1013 // states, as can be seen below. Largely this arises from position and transform
1014 // being stored in the same data structure while having different latching rules.
1015 // b/38182305
1016 //
1017 // Careful that "c" and editCurrentState may not begin as equivalent due to
1018 // applyPendingStates in the presence of deferred transactions.
1019 if (mFreezeGeometryUpdates) {
Robert Carr82364e32016-05-15 11:27:47 -07001020 float tx = c.active.transform.tx();
1021 float ty = c.active.transform.ty();
1022 c.active = c.requested;
1023 c.active.transform.set(tx, ty);
1024 editCurrentState.active = c.active;
1025 } else {
1026 editCurrentState.active = editCurrentState.requested;
1027 c.active = c.requested;
1028 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001029 }
1030
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001031 if (s.active != c.active) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001032 // invalidate and recompute the visible regions if needed
1033 flags |= Layer::eVisibleRegion;
1034 }
1035
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001036 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001037 // invalidate and recompute the visible regions if needed
1038 flags |= eVisibleRegion;
1039 this->contentDirty = true;
1040
1041 // we may use linear filtering, if the matrix scales us
Robert Carr3dcabfa2016-03-01 18:36:58 -08001042 const uint8_t type = c.active.transform.getType();
David Sodman41fdfc92017-11-06 16:09:56 -08001043 mNeedsFiltering = (!c.active.transform.preserveRects() || (type >= Transform::SCALE));
Mathias Agopian13127d82013-03-05 17:47:11 -08001044 }
1045
Dan Stozac8145172016-04-28 16:29:06 -07001046 // If the layer is hidden, signal and clear out all local sync points so
1047 // that transactions for layers depending on this layer's frames becoming
1048 // visible are not blocked
1049 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001050 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001051 }
1052
Mathias Agopian13127d82013-03-05 17:47:11 -08001053 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001054 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001055 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001056}
1057
Pablo Ceballos05289c22016-04-14 15:49:55 -07001058void Layer::commitTransaction(const State& stateToCommit) {
1059 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001060}
1061
Mathias Agopian13127d82013-03-05 17:47:11 -08001062uint32_t Layer::getTransactionFlags(uint32_t flags) {
1063 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1064}
1065
1066uint32_t Layer::setTransactionFlags(uint32_t flags) {
1067 return android_atomic_or(flags, &mTransactionFlags);
1068}
1069
Robert Carr82364e32016-05-15 11:27:47 -07001070bool Layer::setPosition(float x, float y, bool immediate) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001071 if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001072 return false;
1073 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001074
1075 // We update the requested and active position simultaneously because
1076 // we want to apply the position portion of the transform matrix immediately,
1077 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Robert Carr3dcabfa2016-03-01 18:36:58 -08001078 mCurrentState.requested.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001079 if (immediate && !mFreezeGeometryUpdates) {
1080 // Here we directly update the active state
1081 // unlike other setters, because we store it within
1082 // the transform, but use different latching rules.
1083 // b/38182305
Robert Carr82364e32016-05-15 11:27:47 -07001084 mCurrentState.active.transform.set(x, y);
1085 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001086 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001087
Dan Stoza7dde5992015-05-22 09:51:44 -07001088 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001089 setTransactionFlags(eTransactionNeeded);
1090 return true;
1091}
Robert Carr82364e32016-05-15 11:27:47 -07001092
Robert Carr1f0a16a2016-10-24 16:27:39 -07001093bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1094 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1095 if (idx < 0) {
1096 return false;
1097 }
1098 if (childLayer->setLayer(z)) {
1099 mCurrentChildren.removeAt(idx);
1100 mCurrentChildren.add(childLayer);
1101 }
1102 return true;
1103}
1104
Robert Carr503c7042017-09-27 15:06:08 -07001105bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1106 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1107 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1108 if (idx < 0) {
1109 return false;
1110 }
1111 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1112 mCurrentChildren.removeAt(idx);
1113 mCurrentChildren.add(childLayer);
1114 }
1115 return true;
1116}
1117
Robert Carrae060832016-11-28 10:51:00 -08001118bool Layer::setLayer(int32_t z) {
David Sodman41fdfc92017-11-06 16:09:56 -08001119 if (mCurrentState.z == z) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001120 mCurrentState.sequence++;
1121 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001122 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001123
1124 // Discard all relative layering.
1125 if (mCurrentState.zOrderRelativeOf != nullptr) {
1126 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1127 if (strongRelative != nullptr) {
1128 strongRelative->removeZOrderRelative(this);
1129 }
1130 mCurrentState.zOrderRelativeOf = nullptr;
1131 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001132 setTransactionFlags(eTransactionNeeded);
1133 return true;
1134}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001135
Robert Carrdb66e622017-04-10 16:55:57 -07001136void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1137 mCurrentState.zOrderRelatives.remove(relative);
1138 mCurrentState.sequence++;
1139 mCurrentState.modified = true;
1140 setTransactionFlags(eTransactionNeeded);
1141}
1142
1143void Layer::addZOrderRelative(const wp<Layer>& relative) {
1144 mCurrentState.zOrderRelatives.add(relative);
1145 mCurrentState.modified = true;
1146 mCurrentState.sequence++;
1147 setTransactionFlags(eTransactionNeeded);
1148}
1149
1150bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t z) {
1151 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1152 if (handle == nullptr) {
1153 return false;
1154 }
1155 sp<Layer> relative = handle->owner.promote();
1156 if (relative == nullptr) {
1157 return false;
1158 }
1159
1160 mCurrentState.sequence++;
1161 mCurrentState.modified = true;
1162 mCurrentState.z = z;
1163
chaviw9ab4bd12017-11-03 13:11:00 -07001164 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
1165 if (oldZOrderRelativeOf != nullptr) {
1166 oldZOrderRelativeOf->removeZOrderRelative(this);
1167 }
Robert Carrdb66e622017-04-10 16:55:57 -07001168 mCurrentState.zOrderRelativeOf = relative;
1169 relative->addZOrderRelative(this);
1170
1171 setTransactionFlags(eTransactionNeeded);
1172
1173 return true;
1174}
1175
Mathias Agopian13127d82013-03-05 17:47:11 -08001176bool Layer::setSize(uint32_t w, uint32_t h) {
David Sodman41fdfc92017-11-06 16:09:56 -08001177 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001178 mCurrentState.requested.w = w;
1179 mCurrentState.requested.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001180 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001181 setTransactionFlags(eTransactionNeeded);
1182 return true;
1183}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001184bool Layer::setAlpha(float alpha) {
David Sodman41fdfc92017-11-06 16:09:56 -08001185 if (mCurrentState.color.a == alpha) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001186 mCurrentState.sequence++;
chaviw13fdc492017-06-27 12:40:18 -07001187 mCurrentState.color.a = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001188 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001189 setTransactionFlags(eTransactionNeeded);
1190 return true;
1191}
chaviw13fdc492017-06-27 12:40:18 -07001192
1193bool Layer::setColor(const half3& color) {
David Sodman41fdfc92017-11-06 16:09:56 -08001194 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g &&
1195 color.b == mCurrentState.color.b)
chaviw13fdc492017-06-27 12:40:18 -07001196 return false;
1197
1198 mCurrentState.sequence++;
1199 mCurrentState.color.r = color.r;
1200 mCurrentState.color.g = color.g;
1201 mCurrentState.color.b = color.b;
1202 mCurrentState.modified = true;
1203 setTransactionFlags(eTransactionNeeded);
1204 return true;
1205}
1206
Mathias Agopian13127d82013-03-05 17:47:11 -08001207bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
1208 mCurrentState.sequence++;
David Sodman41fdfc92017-11-06 16:09:56 -08001209 mCurrentState.requested.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001210 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001211 setTransactionFlags(eTransactionNeeded);
1212 return true;
1213}
1214bool Layer::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001215 mCurrentState.requestedTransparentRegion = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001216 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001217 setTransactionFlags(eTransactionNeeded);
1218 return true;
1219}
1220bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1221 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
David Sodman41fdfc92017-11-06 16:09:56 -08001222 if (mCurrentState.flags == newFlags) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001223 mCurrentState.sequence++;
1224 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001225 mCurrentState.mask = mask;
1226 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001227 setTransactionFlags(eTransactionNeeded);
1228 return true;
1229}
Robert Carr99e27f02016-06-16 15:18:02 -07001230
1231bool Layer::setCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001232 if (mCurrentState.requestedCrop == crop) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001233 mCurrentState.sequence++;
Robert Carr99e27f02016-06-16 15:18:02 -07001234 mCurrentState.requestedCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001235 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr99e27f02016-06-16 15:18:02 -07001236 mCurrentState.crop = crop;
1237 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001238 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1239
Dan Stoza7dde5992015-05-22 09:51:44 -07001240 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001241 setTransactionFlags(eTransactionNeeded);
1242 return true;
1243}
Robert Carr8d5227b2017-03-16 15:41:03 -07001244
1245bool Layer::setFinalCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001246 if (mCurrentState.requestedFinalCrop == crop) return false;
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001247 mCurrentState.sequence++;
Robert Carr8d5227b2017-03-16 15:41:03 -07001248 mCurrentState.requestedFinalCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001249 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr8d5227b2017-03-16 15:41:03 -07001250 mCurrentState.finalCrop = crop;
1251 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001252 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1253
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001254 mCurrentState.modified = true;
1255 setTransactionFlags(eTransactionNeeded);
1256 return true;
1257}
Mathias Agopian13127d82013-03-05 17:47:11 -08001258
Robert Carrc3574f72016-03-24 12:19:32 -07001259bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001260 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001261 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001262 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001263 return true;
1264}
1265
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001266void Layer::setInfo(uint32_t type, uint32_t appId) {
David Sodman41fdfc92017-11-06 16:09:56 -08001267 mCurrentState.appId = appId;
1268 mCurrentState.type = type;
1269 mCurrentState.modified = true;
1270 setTransactionFlags(eTransactionNeeded);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001271}
1272
Mathias Agopian13127d82013-03-05 17:47:11 -08001273bool Layer::setLayerStack(uint32_t layerStack) {
David Sodman41fdfc92017-11-06 16:09:56 -08001274 if (mCurrentState.layerStack == layerStack) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001275 mCurrentState.sequence++;
1276 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001277 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001278 setTransactionFlags(eTransactionNeeded);
1279 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001280}
1281
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -07001282bool Layer::setDataSpace(android_dataspace dataSpace) {
David Sodman41fdfc92017-11-06 16:09:56 -08001283 if (mCurrentState.dataSpace == dataSpace) return false;
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -07001284 mCurrentState.sequence++;
1285 mCurrentState.dataSpace = dataSpace;
1286 mCurrentState.modified = true;
1287 setTransactionFlags(eTransactionNeeded);
1288 return true;
1289}
1290
Courtney Goeltzenleuchter532b2632017-05-05 16:34:38 -06001291android_dataspace Layer::getDataSpace() const {
1292 return mCurrentState.dataSpace;
1293}
1294
Robert Carr1f0a16a2016-10-24 16:27:39 -07001295uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001296 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001297 if (p == nullptr) {
1298 return getDrawingState().layerStack;
1299 }
1300 return p->getLayerStack();
1301}
1302
David Sodman41fdfc92017-11-06 16:09:56 -08001303void Layer::deferTransactionUntil(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001304 mCurrentState.barrierLayer = barrierLayer;
Dan Stoza7dde5992015-05-22 09:51:44 -07001305 mCurrentState.frameNumber = frameNumber;
1306 // We don't set eTransactionNeeded, because just receiving a deferral
1307 // request without any other state updates shouldn't actually induce a delay
1308 mCurrentState.modified = true;
1309 pushPendingState();
Robert Carr0d480722017-01-10 16:42:54 -08001310 mCurrentState.barrierLayer = nullptr;
Dan Stoza792e5292016-02-11 11:43:58 -08001311 mCurrentState.frameNumber = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001312 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001313}
1314
David Sodman41fdfc92017-11-06 16:09:56 -08001315void Layer::deferTransactionUntil(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001316 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
1317 deferTransactionUntil(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001318}
1319
Dan Stozaee44edd2015-03-23 15:50:23 -07001320
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001321// ----------------------------------------------------------------------------
1322// pageflip handling...
1323// ----------------------------------------------------------------------------
1324
Robert Carr1f0a16a2016-10-24 16:27:39 -07001325bool Layer::isHiddenByPolicy() const {
1326 const Layer::State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001327 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001328 if (parent != nullptr && parent->isHiddenByPolicy()) {
1329 return true;
1330 }
1331 return s.flags & layer_state_t::eLayerHidden;
1332}
1333
David Sodman41fdfc92017-11-06 16:09:56 -08001334uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001335 // TODO: should we do something special if mSecure is set?
1336 if (mProtectedByApp) {
1337 // need a hardware-protected path to external video sink
1338 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001339 }
Riley Andrews03414a12014-07-01 14:22:59 -07001340 if (mPotentialCursor) {
1341 usage |= GraphicBuffer::USAGE_CURSOR;
1342 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001343 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001344 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001345}
1346
Mathias Agopian84300952012-11-21 16:02:13 -08001347void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001348 uint32_t orientation = 0;
1349 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001350 // The transform hint is used to improve performance, but we can
1351 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001352 // apply to all displays.
Mathias Agopian42977342012-08-05 00:40:46 -07001353 const Transform& planeTransform(hw->getTransform());
Mathias Agopian4fec8732012-06-29 14:12:52 -07001354 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07001355 if (orientation & Transform::ROT_INVALID) {
1356 orientation = 0;
1357 }
1358 }
David Sodmaneb085e02017-10-05 18:49:04 -07001359 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001360}
1361
Mathias Agopian13127d82013-03-05 17:47:11 -08001362// ----------------------------------------------------------------------------
1363// debugging
1364// ----------------------------------------------------------------------------
1365
Kalle Raitaa099a242017-01-11 11:17:29 -08001366LayerDebugInfo Layer::getLayerDebugInfo() const {
1367 LayerDebugInfo info;
1368 const Layer::State& ds = getDrawingState();
1369 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001370 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001371 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
1372 info.mType = String8(getTypeId());
1373 info.mTransparentRegion = ds.activeTransparentRegion;
1374 info.mVisibleRegion = visibleRegion;
1375 info.mSurfaceDamageRegion = surfaceDamageRegion;
1376 info.mLayerStack = getLayerStack();
1377 info.mX = ds.active.transform.tx();
1378 info.mY = ds.active.transform.ty();
1379 info.mZ = ds.z;
1380 info.mWidth = ds.active.w;
1381 info.mHeight = ds.active.h;
1382 info.mCrop = ds.crop;
1383 info.mFinalCrop = ds.finalCrop;
chaviw13fdc492017-06-27 12:40:18 -07001384 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001385 info.mFlags = ds.flags;
1386 info.mPixelFormat = getPixelFormat();
1387 info.mDataSpace = getDataSpace();
1388 info.mMatrix[0][0] = ds.active.transform[0][0];
1389 info.mMatrix[0][1] = ds.active.transform[0][1];
1390 info.mMatrix[1][0] = ds.active.transform[1][0];
1391 info.mMatrix[1][1] = ds.active.transform[1][1];
1392 {
1393 sp<const GraphicBuffer> activeBuffer = getActiveBuffer();
1394 if (activeBuffer != 0) {
1395 info.mActiveBufferWidth = activeBuffer->getWidth();
1396 info.mActiveBufferHeight = activeBuffer->getHeight();
1397 info.mActiveBufferStride = activeBuffer->getStride();
1398 info.mActiveBufferFormat = activeBuffer->format;
1399 } else {
1400 info.mActiveBufferWidth = 0;
1401 info.mActiveBufferHeight = 0;
1402 info.mActiveBufferStride = 0;
1403 info.mActiveBufferFormat = 0;
1404 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001405 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001406 info.mNumQueuedFrames = getQueuedFrameCount();
1407 info.mRefreshPending = isBufferLatched();
1408 info.mIsOpaque = isOpaque(ds);
1409 info.mContentDirty = contentDirty;
1410 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001411}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001412
Dan Stozae22aec72016-08-01 13:20:59 -07001413void Layer::miniDumpHeader(String8& result) {
1414 result.append("----------------------------------------");
1415 result.append("---------------------------------------\n");
1416 result.append(" Layer name\n");
1417 result.append(" Z | ");
1418 result.append(" Comp Type | ");
1419 result.append(" Disp Frame (LTRB) | ");
1420 result.append(" Source Crop (LTRB)\n");
1421 result.append("----------------------------------------");
1422 result.append("---------------------------------------\n");
1423}
1424
1425void Layer::miniDump(String8& result, int32_t hwcId) const {
1426 if (mHwcLayers.count(hwcId) == 0) {
1427 return;
1428 }
1429
1430 String8 name;
1431 if (mName.length() > 77) {
1432 std::string shortened;
1433 shortened.append(mName.string(), 36);
1434 shortened.append("[...]");
1435 shortened.append(mName.string() + (mName.length() - 36), 36);
1436 name = shortened.c_str();
1437 } else {
1438 name = mName;
1439 }
1440
1441 result.appendFormat(" %s\n", name.string());
1442
1443 const Layer::State& layerState(getDrawingState());
1444 const HWCInfo& hwcInfo = mHwcLayers.at(hwcId);
John Reck8c3b6ac2017-08-24 10:25:42 -07001445 result.appendFormat(" %10d | ", layerState.z);
David Sodman41fdfc92017-11-06 16:09:56 -08001446 result.appendFormat("%10s | ", to_string(getCompositionType(hwcId)).c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001447 const Rect& frame = hwcInfo.displayFrame;
David Sodman41fdfc92017-11-06 16:09:56 -08001448 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Dan Stoza5a423ea2017-02-16 14:10:39 -08001449 const FloatRect& crop = hwcInfo.sourceCrop;
David Sodman41fdfc92017-11-06 16:09:56 -08001450 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 -07001451
1452 result.append("- - - - - - - - - - - - - - - - - - - - ");
1453 result.append("- - - - - - - - - - - - - - - - - - - -\n");
1454}
Dan Stozae22aec72016-08-01 13:20:59 -07001455
Svetoslavd85084b2014-03-20 10:28:31 -07001456void Layer::dumpFrameStats(String8& result) const {
1457 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001458}
1459
Svetoslavd85084b2014-03-20 10:28:31 -07001460void Layer::clearFrameStats() {
1461 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001462}
1463
Jamie Gennis6547ff42013-07-16 20:12:42 -07001464void Layer::logFrameStats() {
1465 mFrameTracker.logAndResetStats(mName);
1466}
1467
Svetoslavd85084b2014-03-20 10:28:31 -07001468void Layer::getFrameStats(FrameStats* outStats) const {
1469 mFrameTracker.getStats(outStats);
1470}
1471
Brian Andersond6927fb2016-07-23 23:37:30 -07001472void Layer::dumpFrameEvents(String8& result) {
David Sodman41fdfc92017-11-06 16:09:56 -08001473 result.appendFormat("- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001474 Mutex::Autolock lock(mFrameEventHistoryMutex);
1475 mFrameEventHistory.checkFencesForCompletion();
1476 mFrameEventHistory.dump(result);
1477}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001478
Brian Anderson5ea5e592016-12-01 16:54:33 -08001479void Layer::onDisconnect() {
1480 Mutex::Autolock lock(mFrameEventHistoryMutex);
1481 mFrameEventHistory.onDisconnect();
1482}
1483
Brian Anderson3890c392016-07-25 12:48:08 -07001484void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001485 FrameEventHistoryDelta* outDelta) {
Brian Andersond6927fb2016-07-23 23:37:30 -07001486 Mutex::Autolock lock(mFrameEventHistoryMutex);
1487 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001488 // If there are any unsignaled fences in the aquire timeline at this
1489 // point, the previously queued frame hasn't been latched yet. Go ahead
1490 // and try to get the signal time here so the syscall is taken out of
1491 // the main thread's critical path.
1492 mAcquireTimeline.updateSignalTimes();
1493 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001494 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001495 mFrameEventHistory.addQueue(*newTimestamps);
1496 }
1497
Brian Anderson3890c392016-07-25 12:48:08 -07001498 if (outDelta) {
1499 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001500 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001501}
Dan Stozae77c7662016-05-13 11:37:28 -07001502
Chia-I Wu98f1c102017-05-30 14:54:08 -07001503size_t Layer::getChildrenCount() const {
1504 size_t count = 0;
1505 for (const sp<Layer>& child : mCurrentChildren) {
1506 count += 1 + child->getChildrenCount();
1507 }
1508 return count;
1509}
1510
Robert Carr1f0a16a2016-10-24 16:27:39 -07001511void Layer::addChild(const sp<Layer>& layer) {
1512 mCurrentChildren.add(layer);
1513 layer->setParent(this);
1514}
1515
1516ssize_t Layer::removeChild(const sp<Layer>& layer) {
1517 layer->setParent(nullptr);
1518 return mCurrentChildren.remove(layer);
1519}
1520
Robert Carr1db73f62016-12-21 12:58:51 -08001521bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1522 sp<Handle> handle = nullptr;
1523 sp<Layer> newParent = nullptr;
1524 if (newParentHandle == nullptr) {
1525 return false;
1526 }
1527 handle = static_cast<Handle*>(newParentHandle.get());
1528 newParent = handle->owner.promote();
1529 if (newParent == nullptr) {
1530 ALOGE("Unable to promote Layer handle");
1531 return false;
1532 }
1533
1534 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001535 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001536
1537 sp<Client> client(child->mClientRef.promote());
1538 if (client != nullptr) {
1539 client->setParentLayer(newParent);
1540 }
1541 }
1542 mCurrentChildren.clear();
1543
1544 return true;
1545}
1546
chaviwf1961f72017-09-18 16:41:07 -07001547bool Layer::reparent(const sp<IBinder>& newParentHandle) {
1548 if (newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001549 return false;
1550 }
1551
1552 auto handle = static_cast<Handle*>(newParentHandle.get());
1553 sp<Layer> newParent = handle->owner.promote();
1554 if (newParent == nullptr) {
1555 ALOGE("Unable to promote Layer handle");
1556 return false;
1557 }
1558
chaviwf1961f72017-09-18 16:41:07 -07001559 sp<Layer> parent = getParent();
1560 if (parent != nullptr) {
1561 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001562 }
chaviwf1961f72017-09-18 16:41:07 -07001563 newParent->addChild(this);
chaviw06178942017-07-27 10:25:59 -07001564
chaviwf1961f72017-09-18 16:41:07 -07001565 sp<Client> client(mClientRef.promote());
chaviw06178942017-07-27 10:25:59 -07001566 sp<Client> newParentClient(newParent->mClientRef.promote());
1567
chaviwf1961f72017-09-18 16:41:07 -07001568 if (client != newParentClient) {
1569 client->setParentLayer(newParent);
chaviw06178942017-07-27 10:25:59 -07001570 }
1571
chaviw06178942017-07-27 10:25:59 -07001572 return true;
1573}
1574
Robert Carr9524cb32017-02-13 11:32:32 -08001575bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001576 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001577 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001578 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001579 if (client != nullptr && parentClient != client) {
Robert Carr7f619b22017-11-06 12:56:35 -08001580 client->detachLayer(child.get());
1581 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001582 }
Robert Carr7f619b22017-11-06 12:56:35 -08001583 }
Robert Carr9524cb32017-02-13 11:32:32 -08001584
1585 return true;
1586}
1587
Robert Carr1f0a16a2016-10-24 16:27:39 -07001588void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001589 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001590}
1591
1592void Layer::clearSyncPoints() {
1593 for (const auto& child : mCurrentChildren) {
1594 child->clearSyncPoints();
1595 }
1596
1597 Mutex::Autolock lock(mLocalSyncPointMutex);
1598 for (auto& point : mLocalSyncPoints) {
1599 point->setFrameAvailable();
1600 }
1601 mLocalSyncPoints.clear();
1602}
1603
1604int32_t Layer::getZ() const {
1605 return mDrawingState.z;
1606}
1607
David Sodman41fdfc92017-11-06 16:09:56 -08001608__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
1609 LayerVector::StateSet stateSet) {
Dan Stoza412903f2017-04-27 13:42:17 -07001610 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1611 "makeTraversalList received invalid stateSet");
1612 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1613 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1614 const State& state = useDrawing ? mDrawingState : mCurrentState;
1615
Robert Carrdb66e622017-04-10 16:55:57 -07001616 LayerVector traverse;
Dan Stoza412903f2017-04-27 13:42:17 -07001617 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001618 sp<Layer> strongRelative = weakRelative.promote();
1619 if (strongRelative != nullptr) {
1620 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001621 }
1622 }
1623
Dan Stoza412903f2017-04-27 13:42:17 -07001624 for (const sp<Layer>& child : children) {
Robert Carr503c7042017-09-27 15:06:08 -07001625 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1626 if (childState.zOrderRelativeOf != nullptr) {
1627 continue;
1628 }
Robert Carrdb66e622017-04-10 16:55:57 -07001629 traverse.add(child);
1630 }
1631
1632 return traverse;
1633}
1634
Robert Carr1f0a16a2016-10-24 16:27:39 -07001635/**
Robert Carrdb66e622017-04-10 16:55:57 -07001636 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001637 */
Dan Stoza412903f2017-04-27 13:42:17 -07001638void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
1639 LayerVector list = makeTraversalList(stateSet);
Robert Carrdb66e622017-04-10 16:55:57 -07001640
Robert Carr1f0a16a2016-10-24 16:27:39 -07001641 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001642 for (; i < list.size(); i++) {
1643 const auto& relative = list[i];
1644 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001645 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001646 }
Dan Stoza412903f2017-04-27 13:42:17 -07001647 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001648 }
Dan Stoza412903f2017-04-27 13:42:17 -07001649 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001650 for (; i < list.size(); i++) {
1651 const auto& relative = list[i];
Dan Stoza412903f2017-04-27 13:42:17 -07001652 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001653 }
1654}
1655
1656/**
Robert Carrdb66e622017-04-10 16:55:57 -07001657 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001658 */
Dan Stoza412903f2017-04-27 13:42:17 -07001659void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1660 const LayerVector::Visitor& visitor) {
1661 LayerVector list = makeTraversalList(stateSet);
Robert Carrdb66e622017-04-10 16:55:57 -07001662
Robert Carr1f0a16a2016-10-24 16:27:39 -07001663 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001664 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001665 const auto& relative = list[i];
1666 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001667 break;
1668 }
Dan Stoza412903f2017-04-27 13:42:17 -07001669 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001670 }
Dan Stoza412903f2017-04-27 13:42:17 -07001671 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001672 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001673 const auto& relative = list[i];
Dan Stoza412903f2017-04-27 13:42:17 -07001674 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001675 }
1676}
1677
chaviwa76b2712017-09-20 12:02:26 -07001678/**
1679 * Traverse only children in z order, ignoring relative layers.
1680 */
1681void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1682 const LayerVector::Visitor& visitor) {
1683 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1684 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1685
1686 size_t i = 0;
1687 for (; i < children.size(); i++) {
1688 const auto& relative = children[i];
1689 if (relative->getZ() >= 0) {
1690 break;
1691 }
1692 relative->traverseChildrenInZOrder(stateSet, visitor);
1693 }
1694 visitor(this);
1695 for (; i < children.size(); i++) {
1696 const auto& relative = children[i];
1697 relative->traverseChildrenInZOrder(stateSet, visitor);
1698 }
1699}
1700
Robert Carr1f0a16a2016-10-24 16:27:39 -07001701Transform Layer::getTransform() const {
1702 Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001703 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001704 if (p != nullptr) {
1705 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07001706
1707 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
1708 // it isFixedSize) then there may be additional scaling not accounted
1709 // for in the transform. We need to mirror this scaling in child surfaces
1710 // or we will break the contract where WM can treat child surfaces as
1711 // pixels in the parent surface.
Chia-I Wu0a68b462017-07-18 11:30:05 -07001712 if (p->isFixedSize() && p->mActiveBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07001713 int bufferWidth;
1714 int bufferHeight;
1715 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
1716 bufferWidth = p->mActiveBuffer->getWidth();
1717 bufferHeight = p->mActiveBuffer->getHeight();
1718 } else {
1719 bufferHeight = p->mActiveBuffer->getWidth();
1720 bufferWidth = p->mActiveBuffer->getHeight();
1721 }
David Sodman41fdfc92017-11-06 16:09:56 -08001722 float sx = p->getDrawingState().active.w / static_cast<float>(bufferWidth);
1723 float sy = p->getDrawingState().active.h / static_cast<float>(bufferHeight);
Robert Carr9b429f42017-04-17 14:56:57 -07001724 Transform extraParentScaling;
1725 extraParentScaling.set(sx, 0, 0, sy);
1726 t = t * extraParentScaling;
1727 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001728 }
1729 return t * getDrawingState().active.transform;
1730}
1731
chaviw13fdc492017-06-27 12:40:18 -07001732half Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001733 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07001734
chaviw13fdc492017-06-27 12:40:18 -07001735 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1736 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001737}
Robert Carr6452f122017-03-21 10:41:29 -07001738
chaviw13fdc492017-06-27 12:40:18 -07001739half4 Layer::getColor() const {
1740 const half4 color(getDrawingState().color);
1741 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001742}
Robert Carr6452f122017-03-21 10:41:29 -07001743
Robert Carr1f0a16a2016-10-24 16:27:39 -07001744void Layer::commitChildList() {
1745 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1746 const auto& child = mCurrentChildren[i];
1747 child->commitChildList();
1748 }
1749 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001750 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001751}
1752
chaviw1d044282017-09-27 12:19:28 -07001753void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
1754 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1755 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1756 const State& state = useDrawing ? mDrawingState : mCurrentState;
1757
1758 Transform requestedTransform = state.active.transform;
1759 Transform transform = getTransform();
1760
1761 layerInfo->set_id(sequence);
1762 layerInfo->set_name(getName().c_str());
1763 layerInfo->set_type(String8(getTypeId()));
1764
1765 for (const auto& child : children) {
1766 layerInfo->add_children(child->sequence);
1767 }
1768
1769 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1770 sp<Layer> strongRelative = weakRelative.promote();
1771 if (strongRelative != nullptr) {
1772 layerInfo->add_relatives(strongRelative->sequence);
1773 }
1774 }
1775
1776 LayerProtoHelper::writeToProto(state.activeTransparentRegion,
1777 layerInfo->mutable_transparent_region());
1778 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
1779 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
1780
1781 layerInfo->set_layer_stack(getLayerStack());
1782 layerInfo->set_z(state.z);
1783
1784 PositionProto* position = layerInfo->mutable_position();
1785 position->set_x(transform.tx());
1786 position->set_y(transform.ty());
1787
1788 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
1789 requestedPosition->set_x(requestedTransform.tx());
1790 requestedPosition->set_y(requestedTransform.ty());
1791
1792 SizeProto* size = layerInfo->mutable_size();
1793 size->set_w(state.active.w);
1794 size->set_h(state.active.h);
1795
1796 LayerProtoHelper::writeToProto(state.crop, layerInfo->mutable_crop());
1797 LayerProtoHelper::writeToProto(state.finalCrop, layerInfo->mutable_final_crop());
1798
1799 layerInfo->set_is_opaque(isOpaque(state));
1800 layerInfo->set_invalidate(contentDirty);
1801 layerInfo->set_dataspace(dataspaceDetails(getDataSpace()));
1802 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1803 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
1804 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
1805 layerInfo->set_flags(state.flags);
1806
1807 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1808 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
1809
Jorim Jaggi8e0af362017-11-14 16:28:28 +01001810 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07001811 if (parent != nullptr) {
1812 layerInfo->set_parent(parent->sequence);
1813 }
1814
1815 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1816 if (zOrderRelativeOf != nullptr) {
1817 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1818 }
1819
1820 auto activeBuffer = getActiveBuffer();
1821 if (activeBuffer != nullptr) {
1822 LayerProtoHelper::writeToProto(activeBuffer, layerInfo->mutable_active_buffer());
1823 }
1824
1825 layerInfo->set_queued_frames(getQueuedFrameCount());
1826 layerInfo->set_refresh_pending(isBufferLatched());
1827}
1828
Mathias Agopian13127d82013-03-05 17:47:11 -08001829// ---------------------------------------------------------------------------
1830
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001831}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07001832
1833#if defined(__gl_h_)
1834#error "don't include gl/gl.h in this file"
1835#endif
1836
1837#if defined(__gl2_h_)
1838#error "don't include gl2/gl2.h in this file"
1839#endif