blob: ee9ee78fae579a84a5fb9d0acbd11e2049516301 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dan Stoza9e56aa02015-11-02 13:00:03 -080017//#define LOG_NDEBUG 0
18#undef LOG_TAG
19#define LOG_TAG "Layer"
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080020#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21
Mathias Agopian13127d82013-03-05 17:47:11 -080022#include <math.h>
David Sodman41fdfc92017-11-06 16:09:56 -080023#include <stdint.h>
24#include <stdlib.h>
25#include <sys/types.h>
chaviw4b129c22018-04-09 16:19:43 -070026#include <algorithm>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027
Mathias Agopiana67932f2011-04-20 14:20:59 -070028#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070029#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070030#include <cutils/properties.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080031
32#include <utils/Errors.h>
33#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080034#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080036#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037
Courtney Goeltzenleuchter36c44dc2017-04-14 09:33:16 -060038#include <ui/DebugUtils.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070039#include <ui/GraphicBuffer.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080040#include <ui/PixelFormat.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080041
Dan Stoza6b9454d2014-11-07 16:00:59 -080042#include <gui/BufferItem.h>
Kalle Raitaa099a242017-01-11 11:17:29 -080043#include <gui/LayerDebugInfo.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080044#include <gui/Surface.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045
Yiwei Zhang60d1a192018-03-07 14:52:28 -080046#include "BufferLayer.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020047#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070048#include "DisplayDevice.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include "Layer.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070050#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070051#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052#include "SurfaceFlinger.h"
David Sodman41fdfc92017-11-06 16:09:56 -080053#include "clz.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054
Mathias Agopian1b031492012-06-20 17:51:20 -070055#include "DisplayHardware/HWComposer.h"
56
Mathias Agopian875d8e12013-06-07 15:35:48 -070057#include "RenderEngine/RenderEngine.h"
58
Dan Stozac5da2712016-07-20 15:38:12 -070059#include <mutex>
chaviw1d044282017-09-27 12:19:28 -070060#include "LayerProtoHelper.h"
Dan Stozac5da2712016-07-20 15:38:12 -070061
David Sodman41fdfc92017-11-06 16:09:56 -080062#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064namespace android {
65
Mathias Agopian13127d82013-03-05 17:47:11 -080066int32_t Layer::sSequence = 1;
67
David Sodman41fdfc92017-11-06 16:09:56 -080068Layer::Layer(SurfaceFlinger* flinger, const sp<Client>& client, const String8& name, uint32_t w,
69 uint32_t h, uint32_t flags)
David Sodman0c69cad2017-08-21 12:12:51 -070070 : contentDirty(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080071 sequence(uint32_t(android_atomic_inc(&sSequence))),
72 mFlinger(flinger),
Mathias Agopian13127d82013-03-05 17:47:11 -080073 mPremultipliedAlpha(true),
David Sodman0c69cad2017-08-21 12:12:51 -070074 mName(name),
Mathias Agopian13127d82013-03-05 17:47:11 -080075 mTransactionFlags(0),
Dan Stoza7dde5992015-05-22 09:51:44 -070076 mPendingStateMutex(),
77 mPendingStates(),
Mathias Agopiana67932f2011-04-20 14:20:59 -070078 mCurrentTransform(0),
Robert Carrc3574f72016-03-24 12:19:32 -070079 mOverrideScalingMode(-1),
Dan Stozacac35382016-01-27 12:21:06 -080080 mCurrentFrameNumber(0),
Mathias Agopian82d7ab62012-01-19 18:34:40 -080081 mFrameLatencyNeeded(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080082 mFiltering(false),
83 mNeedsFiltering(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080084 mProtectedByApp(false),
Riley Andrews03414a12014-07-01 14:22:59 -070085 mClientRef(client),
Dan Stozaa4650a52015-05-12 12:56:16 -070086 mPotentialCursor(false),
David Sodmanb8af7922017-12-21 15:17:55 -080087 mFreezeGeometryUpdates(false),
chaviwfd462612018-05-31 16:11:27 -070088 mCurrentChildren(LayerVector::StateSet::Current),
89 mDrawingChildren(LayerVector::StateSet::Drawing),
David Sodman2b727ac2017-12-21 14:28:08 -080090 mBE{this, name.string()} {
Dan Stoza9e56aa02015-11-02 13:00:03 -080091
Mathias Agopiana67932f2011-04-20 14:20:59 -070092 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070093
94 uint32_t layerFlags = 0;
David Sodman41fdfc92017-11-06 16:09:56 -080095 if (flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
96 if (flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
97 if (flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070098
Mathias Agopian4d9b8222013-03-12 17:11:48 -070099 mName = name;
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700100 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700101
Marissa Wallf58c14b2018-07-24 10:50:43 -0700102 mCurrentState.active_legacy.w = w;
103 mCurrentState.active_legacy.h = h;
David Sodman0c69cad2017-08-21 12:12:51 -0700104 mCurrentState.flags = layerFlags;
Marissa Wallf58c14b2018-07-24 10:50:43 -0700105 mCurrentState.active_legacy.transform.set(0, 0);
106 mCurrentState.crop_legacy.makeInvalid();
107 mCurrentState.finalCrop_legacy.makeInvalid();
108 mCurrentState.requestedFinalCrop_legacy = mCurrentState.finalCrop_legacy;
109 mCurrentState.requestedCrop_legacy = mCurrentState.crop_legacy;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700110 mCurrentState.z = 0;
chaviw13fdc492017-06-27 12:40:18 -0700111 mCurrentState.color.a = 1.0f;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700112 mCurrentState.layerStack = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700113 mCurrentState.sequence = 0;
Marissa Wallf58c14b2018-07-24 10:50:43 -0700114 mCurrentState.requested_legacy = mCurrentState.active_legacy;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500115 mCurrentState.appId = 0;
116 mCurrentState.type = 0;
Marissa Wall61c58622018-07-18 10:12:20 -0700117 mCurrentState.active.w = 0;
118 mCurrentState.active.h = 0;
119 mCurrentState.active.transform.set(0, 0);
120 mCurrentState.transform = 0;
121 mCurrentState.transformToDisplayInverse = false;
122 mCurrentState.crop.makeInvalid();
123 mCurrentState.acquireFence = new Fence(-1);
124 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
125 mCurrentState.hdrMetadata.validTypes = 0;
126 mCurrentState.surfaceDamageRegion.clear();
127 mCurrentState.api = -1;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700128
129 // drawing state & current state are identical
130 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700131
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800132 CompositorTiming compositorTiming;
133 flinger->getCompositorTiming(&compositorTiming);
134 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jamie Gennise8696a42012-01-15 18:54:57 -0800135}
136
Dan Stoza436ccf32018-06-21 12:10:12 -0700137void Layer::onFirstRef() NO_THREAD_SAFETY_ANALYSIS {
138 if (!isCreatedFromMainThread()) {
139 // Grab the SF state lock during this since it's the only way to safely access HWC
140 mFlinger->mStateLock.lock();
141 }
142
143 const auto& hwc = mFlinger->getHwComposer();
144 const auto& activeConfig = hwc.getActiveConfig(HWC_DISPLAY_PRIMARY);
145 nsecs_t displayPeriod = activeConfig->getVsyncPeriod();
146 mFrameTracker.setDisplayRefreshPeriod(displayPeriod);
147
148 if (!isCreatedFromMainThread()) {
149 mFlinger->mStateLock.unlock();
150 }
151}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700152
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700153Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800154 sp<Client> c(mClientRef.promote());
155 if (c != 0) {
156 c->detachLayer(this);
157 }
158
159 for (auto& point : mRemoteSyncPoints) {
160 point->setTransactionApplied();
161 }
162 for (auto& point : mLocalSyncPoints) {
163 point->setFrameAvailable();
164 }
Jamie Gennis6547ff42013-07-16 20:12:42 -0700165 mFrameTracker.logAndResetStats(mName);
Mathias Agopian96f08192010-06-02 23:28:45 -0700166}
167
Mathias Agopian13127d82013-03-05 17:47:11 -0800168// ---------------------------------------------------------------------------
169// callbacks
170// ---------------------------------------------------------------------------
171
David Sodmaneb085e02017-10-05 18:49:04 -0700172/*
173 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
174 * Layer. So, the implementation is done in BufferLayer. When called on a
175 * ColorLayer object, it's essentially a NOP.
176 */
David Sodmaneb085e02017-10-05 18:49:04 -0700177void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800178
Chia-I Wuc6657022017-08-15 11:18:17 -0700179void Layer::onRemovedFromCurrentState() {
180 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
181
chaviw8b3871a2017-11-01 17:41:01 -0700182 mPendingRemoval = true;
183
Robert Carr5edb1ad2017-04-25 10:54:24 -0700184 if (mCurrentState.zOrderRelativeOf != nullptr) {
185 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
186 if (strongRelative != nullptr) {
187 strongRelative->removeZOrderRelative(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700188 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700189 }
190 mCurrentState.zOrderRelativeOf = nullptr;
191 }
192
Chia-I Wuc6657022017-08-15 11:18:17 -0700193 for (const auto& child : mCurrentChildren) {
194 child->onRemovedFromCurrentState();
195 }
196}
Chia-I Wu38512252017-05-17 14:36:16 -0700197
Chia-I Wuc6657022017-08-15 11:18:17 -0700198void Layer::onRemoved() {
199 // the layer is removed from SF mLayersPendingRemoval
David Sodmaneb085e02017-10-05 18:49:04 -0700200 abandon();
Chia-I Wuc6657022017-08-15 11:18:17 -0700201
Steven Thomasb02664d2017-07-26 18:48:28 -0700202 destroyAllHwcLayers();
Chia-I Wu38512252017-05-17 14:36:16 -0700203
Robert Carr1f0a16a2016-10-24 16:27:39 -0700204 for (const auto& child : mCurrentChildren) {
205 child->onRemoved();
206 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700207}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700208
Mathias Agopian13127d82013-03-05 17:47:11 -0800209// ---------------------------------------------------------------------------
210// set-up
211// ---------------------------------------------------------------------------
212
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700213const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800214 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800215}
216
chaviw13fdc492017-06-27 12:40:18 -0700217bool Layer::getPremultipledAlpha() const {
218 return mPremultipliedAlpha;
219}
220
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700221sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800222 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700223 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800224}
225
226// ---------------------------------------------------------------------------
227// h/w composer set-up
228// ---------------------------------------------------------------------------
229
Dominik Laskowski7e045462018-05-30 13:02:02 -0700230bool Layer::createHwcLayer(HWComposer* hwc, int32_t displayId) {
231 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.count(displayId) != 0,
232 "Already have a layer for display %d", displayId);
233 HWC2::Layer* layer = hwc->createLayer(displayId);
Steven Thomasb02664d2017-07-26 18:48:28 -0700234 if (!layer) {
235 return false;
236 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700237 LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers[displayId];
Steven Thomasb02664d2017-07-26 18:48:28 -0700238 hwcInfo.hwc = hwc;
239 hwcInfo.layer = layer;
David Sodmanf6a38932018-05-25 15:27:50 -0700240 layer->setLayerDestroyedListener(
Dominik Laskowski7e045462018-05-30 13:02:02 -0700241 [this, displayId](HWC2::Layer* /*layer*/) { getBE().mHwcLayers.erase(displayId); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700242 return true;
243}
244
Dominik Laskowski7e045462018-05-30 13:02:02 -0700245bool Layer::destroyHwcLayer(int32_t displayId) {
246 if (getBE().mHwcLayers.count(displayId) == 0) {
Chia-I Wu83806892017-11-16 10:50:20 -0800247 return false;
Steven Thomasb02664d2017-07-26 18:48:28 -0700248 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700249 auto& hwcInfo = getBE().mHwcLayers[displayId];
David Sodman41fdfc92017-11-06 16:09:56 -0800250 LOG_ALWAYS_FATAL_IF(hwcInfo.layer == nullptr, "Attempt to destroy null layer");
Steven Thomasb02664d2017-07-26 18:48:28 -0700251 LOG_ALWAYS_FATAL_IF(hwcInfo.hwc == nullptr, "Missing HWComposer");
Dominik Laskowski7e045462018-05-30 13:02:02 -0700252 hwcInfo.hwc->destroyLayer(displayId, hwcInfo.layer);
David Sodmanf6a38932018-05-25 15:27:50 -0700253 // The layer destroyed listener should have cleared the entry from
254 // mHwcLayers. Verify that.
Dominik Laskowski7e045462018-05-30 13:02:02 -0700255 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.count(displayId) != 0,
David Sodmanf6a38932018-05-25 15:27:50 -0700256 "Stale layer entry in getBE().mHwcLayers");
Chia-I Wu83806892017-11-16 10:50:20 -0800257 return true;
Steven Thomasb02664d2017-07-26 18:48:28 -0700258}
259
260void Layer::destroyAllHwcLayers() {
David Sodman6f65f3e2017-11-03 14:28:09 -0700261 size_t numLayers = getBE().mHwcLayers.size();
Steven Thomasb02664d2017-07-26 18:48:28 -0700262 for (size_t i = 0; i < numLayers; ++i) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700263 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.empty(), "destroyAllHwcLayers failed");
264 destroyHwcLayer(getBE().mHwcLayers.begin()->first);
Steven Thomasb02664d2017-07-26 18:48:28 -0700265 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700266 LOG_ALWAYS_FATAL_IF(!getBE().mHwcLayers.empty(),
David Sodman41fdfc92017-11-06 16:09:56 -0800267 "All hardware composer layers should have been destroyed");
Steven Thomasb02664d2017-07-26 18:48:28 -0700268}
Steven Thomasb02664d2017-07-26 18:48:28 -0700269
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800270Rect Layer::getContentCrop() const {
271 // this is the crop rectangle that applies to the buffer
272 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700273 Rect crop;
274 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800275 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700276 crop = mCurrentCrop;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800277 } else if (getBE().compositionInfo.mBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800278 // otherwise we use the whole buffer
David Sodman0cc69182017-11-17 12:12:07 -0800279 crop = getBE().compositionInfo.mBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700280 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800281 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700282 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700283 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700284 return crop;
285}
286
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700287static Rect reduce(const Rect& win, const Region& exclude) {
288 if (CC_LIKELY(exclude.isEmpty())) {
289 return win;
290 }
291 if (exclude.isRect()) {
292 return win.reduce(exclude.getBounds());
293 }
294 return Region(win).subtract(exclude).getBounds();
295}
296
Dan Stoza80d61162017-12-20 15:57:52 -0800297static FloatRect reduce(const FloatRect& win, const Region& exclude) {
298 if (CC_LIKELY(exclude.isEmpty())) {
299 return win;
300 }
301 // Convert through Rect (by rounding) for lack of FloatRegion
302 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
303}
304
Robert Carr1f0a16a2016-10-24 16:27:39 -0700305Rect Layer::computeScreenBounds(bool reduceTransparentRegion) const {
306 const Layer::State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700307 Rect win(getActiveWidth(s), getActiveHeight(s));
Robert Carr1f0a16a2016-10-24 16:27:39 -0700308
Marissa Wall61c58622018-07-18 10:12:20 -0700309 Rect crop = getCrop(s);
310 if (!crop.isEmpty()) {
311 win.intersect(crop, &win);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700312 }
313
314 Transform t = getTransform();
315 win = t.transform(win);
316
Marissa Wall61c58622018-07-18 10:12:20 -0700317 Rect finalCrop = getFinalCrop(s);
318 if (!finalCrop.isEmpty()) {
319 win.intersect(finalCrop, &win);
Robert Carr41b08b52017-06-01 16:11:34 -0700320 }
321
Chia-I Wue41dbe62017-06-13 14:10:56 -0700322 const sp<Layer>& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700323 // Now we need to calculate the parent bounds, so we can clip ourselves to those.
324 // When calculating the parent bounds for purposes of clipping,
325 // we don't need to constrain the parent to its transparent region.
326 // The transparent region is an optimization based on the
327 // buffer contents of the layer, but does not affect the space allocated to
328 // it by policy, and thus children should be allowed to extend into the
329 // parent's transparent region. In fact one of the main uses, is to reduce
330 // buffer allocation size in cases where a child window sits behind a main window
331 // (by marking the hole in the parent window as a transparent region)
332 if (p != nullptr) {
333 Rect bounds = p->computeScreenBounds(false);
334 bounds.intersect(win, &win);
335 }
336
337 if (reduceTransparentRegion) {
Marissa Wall61c58622018-07-18 10:12:20 -0700338 auto const screenTransparentRegion = t.transform(getActiveTransparentRegion(s));
Robert Carr1f0a16a2016-10-24 16:27:39 -0700339 win = reduce(win, screenTransparentRegion);
340 }
341
342 return win;
343}
344
Dan Stoza80d61162017-12-20 15:57:52 -0800345FloatRect Layer::computeBounds() const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700346 const Layer::State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700347 return computeBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700348}
349
Dan Stoza80d61162017-12-20 15:57:52 -0800350FloatRect Layer::computeBounds(const Region& activeTransparentRegion) const {
Michael Lentine6c925ed2014-09-26 17:55:01 -0700351 const Layer::State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700352 Rect win(getActiveWidth(s), getActiveHeight(s));
Robert Carrb5d3d262016-03-25 15:08:13 -0700353
Marissa Wall61c58622018-07-18 10:12:20 -0700354 Rect crop = getCrop(s);
355 if (!crop.isEmpty()) {
356 win.intersect(crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800357 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700358
Chia-I Wue41dbe62017-06-13 14:10:56 -0700359 const auto& p = mDrawingParent.promote();
Robert Carrd4ae7f32018-06-07 16:10:57 -0700360 FloatRect floatWin = win.toFloatRect();
361 FloatRect parentBounds = floatWin;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700362 if (p != nullptr) {
Robert Carrd4ae7f32018-06-07 16:10:57 -0700363 // We pass an empty Region here for reasons mirroring that of the case described in
364 // the computeScreenBounds reduceTransparentRegion=false case.
365 parentBounds = p->computeBounds(Region());
Robert Carr1f0a16a2016-10-24 16:27:39 -0700366 }
367
Marissa Wallf58c14b2018-07-24 10:50:43 -0700368 Transform t = s.active_legacy.transform;
Dan Stoza80d61162017-12-20 15:57:52 -0800369
Marissa Wallf58c14b2018-07-24 10:50:43 -0700370 if (p != nullptr || !s.finalCrop_legacy.isEmpty()) {
Dan Stoza80d61162017-12-20 15:57:52 -0800371 floatWin = t.transform(floatWin);
Robert Carrd4ae7f32018-06-07 16:10:57 -0700372 floatWin = floatWin.intersect(parentBounds);
373
Marissa Wallf58c14b2018-07-24 10:50:43 -0700374 if (!s.finalCrop_legacy.isEmpty()) {
375 floatWin = floatWin.intersect(s.finalCrop_legacy.toFloatRect());
Robert Carrd4ae7f32018-06-07 16:10:57 -0700376 }
Dan Stoza80d61162017-12-20 15:57:52 -0800377 floatWin = t.inverse().transform(floatWin);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700378 }
379
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700380 // subtract the transparent region and snap to the bounds
Dan Stoza80d61162017-12-20 15:57:52 -0800381 return reduce(floatWin, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800382}
383
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700384Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& display) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700385 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800386 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700387 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800388
389 // apply the projection's clipping to the window crop in
390 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700391 // if there are no window scaling involved, this operation will map to full
392 // pixels in the buffer.
393 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
394 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700395
Marissa Wall61c58622018-07-18 10:12:20 -0700396 Rect activeCrop(getActiveWidth(s), getActiveHeight(s));
397 Rect crop = getCrop(s);
398 if (!crop.isEmpty()) {
399 activeCrop.intersect(crop, &activeCrop);
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700400 }
401
Robert Carr1f0a16a2016-10-24 16:27:39 -0700402 Transform t = getTransform();
403 activeCrop = t.transform(activeCrop);
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700404 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000405 activeCrop.clear();
406 }
Marissa Wall61c58622018-07-18 10:12:20 -0700407 Rect finalCrop = getFinalCrop(s);
408 if (!finalCrop.isEmpty()) {
409 if (!activeCrop.intersect(finalCrop, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000410 activeCrop.clear();
411 }
412 }
chaviwb1154d12017-10-31 14:15:36 -0700413
414 const auto& p = mDrawingParent.promote();
415 if (p != nullptr) {
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700416 auto parentCrop = p->computeInitialCrop(display);
chaviwb1154d12017-10-31 14:15:36 -0700417 activeCrop.intersect(parentCrop, &activeCrop);
418 }
419
Robert Carr1f0a16a2016-10-24 16:27:39 -0700420 return activeCrop;
421}
422
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700423FloatRect Layer::computeCrop(const sp<const DisplayDevice>& display) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700424 // the content crop is the area of the content that gets scaled to the
425 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800426 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700427
428 // In addition there is a WM-specified crop we pull from our drawing state.
429 const State& s(getDrawingState());
430
431 // Screen space to make reduction to parent crop clearer.
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700432 Rect activeCrop = computeInitialCrop(display);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700433 Transform t = getTransform();
434 // Back to layer space to work with the content crop.
435 activeCrop = t.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800436
Michael Lentine28ea2172014-11-19 18:32:37 -0800437 // This needs to be here as transform.transform(Rect) computes the
438 // transformed rect and then takes the bounding box of the result before
439 // returning. This means
440 // transform.inverse().transform(transform.transform(Rect)) != Rect
441 // in which case we need to make sure the final rect is clipped to the
442 // display bounds.
Marissa Wall61c58622018-07-18 10:12:20 -0700443 if (!activeCrop.intersect(Rect(getActiveWidth(s), getActiveHeight(s)), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000444 activeCrop.clear();
445 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800446
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700447 // subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700448 activeCrop = reduce(activeCrop, getActiveTransparentRegion(s));
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700449
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000450 // Transform the window crop to match the buffer coordinate system,
451 // which means using the inverse of the current transform set on the
452 // SurfaceFlingerConsumer.
453 uint32_t invTransform = mCurrentTransform;
Robert Carrcae605c2017-03-29 12:10:31 -0700454 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000455 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700456 * the code below applies the primary display's inverse transform to the
457 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000458 */
David Sodman41fdfc92017-11-06 16:09:56 -0800459 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000460 // calculate the inverse transform
461 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800462 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800463 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000464 // and apply to the current transform
David Sodman41fdfc92017-11-06 16:09:56 -0800465 invTransform = (Transform(invTransformOrient) * Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800466 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000467
Marissa Wall61c58622018-07-18 10:12:20 -0700468 int winWidth = getActiveWidth(s);
469 int winHeight = getActiveHeight(s);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000470 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
471 // If the activeCrop has been rotate the ends are rotated but not
472 // the space itself so when transforming ends back we can't rely on
473 // a modification of the axes of rotation. To account for this we
474 // need to reorient the inverse rotation in terms of the current
475 // axes of rotation.
476 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
477 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
478 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800479 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000480 }
Marissa Wall61c58622018-07-18 10:12:20 -0700481 winWidth = getActiveHeight(s);
482 winHeight = getActiveWidth(s);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000483 }
Marissa Wall61c58622018-07-18 10:12:20 -0700484 const Rect winCrop = activeCrop.transform(invTransform, getActiveWidth(s), getActiveHeight(s));
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000485
486 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800487 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000488 float yScale = crop.getHeight() / float(winHeight);
489
David Sodman41fdfc92017-11-06 16:09:56 -0800490 float insetL = winCrop.left * xScale;
491 float insetT = winCrop.top * yScale;
492 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000493 float insetB = (winHeight - winCrop.bottom) * yScale;
494
David Sodman41fdfc92017-11-06 16:09:56 -0800495 crop.left += insetL;
496 crop.top += insetT;
497 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000498 crop.bottom -= insetB;
499
Mathias Agopian13127d82013-03-05 17:47:11 -0800500 return crop;
501}
502
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700503void Layer::setGeometry(const sp<const DisplayDevice>& display, uint32_t z) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700504 const auto displayId = display->getId();
Peiyong Lin91b1df22018-06-18 18:00:16 -0700505 if (!hasHwcLayer(displayId)) {
506 ALOGE("[%s] failed to setGeometry: no HWC layer found (%d)",
507 mName.string(), displayId);
508 return;
509 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700510 auto& hwcInfo = getBE().mHwcLayers[displayId];
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700511
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700512 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800513 hwcInfo.forceClientComposition = false;
514
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700515 if (isSecure() && !display->isSecure()) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800516 hwcInfo.forceClientComposition = true;
517 }
518
Lloyd Pique074e8122018-07-26 12:57:23 -0700519 auto& hwcLayer = hwcInfo.layer;
520
Mathias Agopian13127d82013-03-05 17:47:11 -0800521 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700522 const State& s(getDrawingState());
David Revemanecf0fa52017-03-03 11:32:44 -0500523 auto blendMode = HWC2::BlendMode::None;
Robert Carr6452f122017-03-21 10:41:29 -0700524 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800525 blendMode =
526 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800527 }
Lloyd Pique074e8122018-07-26 12:57:23 -0700528 auto error = hwcLayer->setBlendMode(blendMode);
529 ALOGE_IF(error != HWC2::Error::None,
530 "[%s] Failed to set blend mode %s:"
531 " %s (%d)",
532 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
533 static_cast<int32_t>(error));
Mathias Agopian13127d82013-03-05 17:47:11 -0800534
535 // apply the layer's transform, followed by the display's global transform
536 // here we're guaranteed that the layer's transform preserves rects
Marissa Wall61c58622018-07-18 10:12:20 -0700537 Region activeTransparentRegion(getActiveTransparentRegion(s));
Robert Carr1f0a16a2016-10-24 16:27:39 -0700538 Transform t = getTransform();
Marissa Wall61c58622018-07-18 10:12:20 -0700539 Rect activeCrop = getCrop(s);
540 if (!activeCrop.isEmpty()) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700541 activeCrop = t.transform(activeCrop);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700542 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000543 activeCrop.clear();
544 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700545 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800546 // This needs to be here as transform.transform(Rect) computes the
547 // transformed rect and then takes the bounding box of the result before
548 // returning. This means
549 // transform.inverse().transform(transform.transform(Rect)) != Rect
550 // in which case we need to make sure the final rect is clipped to the
551 // display bounds.
Marissa Wall61c58622018-07-18 10:12:20 -0700552 if (!activeCrop.intersect(Rect(getActiveWidth(s), getActiveHeight(s)), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000553 activeCrop.clear();
554 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700555 // mark regions outside the crop as transparent
Marissa Wall61c58622018-07-18 10:12:20 -0700556 activeTransparentRegion.orSelf(Rect(0, 0, getActiveWidth(s), activeCrop.top));
Marissa Wallf58c14b2018-07-24 10:50:43 -0700557 activeTransparentRegion.orSelf(
Marissa Wall61c58622018-07-18 10:12:20 -0700558 Rect(0, activeCrop.bottom, getActiveWidth(s), getActiveHeight(s)));
David Sodman41fdfc92017-11-06 16:09:56 -0800559 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
560 activeTransparentRegion.orSelf(
Marissa Wall61c58622018-07-18 10:12:20 -0700561 Rect(activeCrop.right, activeCrop.top, getActiveWidth(s), activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700562 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700563
Dan Stoza80d61162017-12-20 15:57:52 -0800564 // computeBounds returns a FloatRect to provide more accuracy during the
565 // transformation. We then round upon constructing 'frame'.
566 Rect frame{t.transform(computeBounds(activeTransparentRegion))};
Marissa Wall61c58622018-07-18 10:12:20 -0700567 Rect finalCrop = getFinalCrop(s);
568 if (!finalCrop.isEmpty()) {
569 if (!frame.intersect(finalCrop, &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000570 frame.clear();
571 }
572 }
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700573 if (!frame.intersect(display->getViewport(), &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000574 frame.clear();
575 }
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700576 const Transform& tr = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800577 Rect transformedFrame = tr.transform(frame);
Lloyd Pique074e8122018-07-26 12:57:23 -0700578 error = hwcLayer->setDisplayFrame(transformedFrame);
579 if (error != HWC2::Error::None) {
580 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
581 transformedFrame.left, transformedFrame.top, transformedFrame.right,
582 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
583 } else {
584 hwcInfo.displayFrame = transformedFrame;
585 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800586
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700587 FloatRect sourceCrop = computeCrop(display);
Lloyd Pique074e8122018-07-26 12:57:23 -0700588 error = hwcLayer->setSourceCrop(sourceCrop);
589 if (error != HWC2::Error::None) {
590 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
591 "%s (%d)",
592 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
593 to_string(error).c_str(), static_cast<int32_t>(error));
594 } else {
595 hwcInfo.sourceCrop = sourceCrop;
596 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800597
chaviw13fdc492017-06-27 12:40:18 -0700598 float alpha = static_cast<float>(getAlpha());
Lloyd Pique074e8122018-07-26 12:57:23 -0700599 error = hwcLayer->setPlaneAlpha(alpha);
600 ALOGE_IF(error != HWC2::Error::None,
601 "[%s] Failed to set plane alpha %.3f: "
602 "%s (%d)",
603 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800604
Lloyd Pique074e8122018-07-26 12:57:23 -0700605 error = hwcLayer->setZOrder(z);
606 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
607 to_string(error).c_str(), static_cast<int32_t>(error));
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500608
Albert Chaulk2a589632017-05-04 16:59:44 -0400609 int type = s.type;
610 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700611 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400612 if (parent.get()) {
613 auto& parentState = parent->getDrawingState();
rongliucfb187b2018-03-14 12:26:23 -0700614 if (parentState.type >= 0 || parentState.appId >= 0) {
615 type = parentState.type;
616 appId = parentState.appId;
617 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400618 }
619
Lloyd Pique074e8122018-07-26 12:57:23 -0700620 error = hwcLayer->setInfo(type, appId);
621 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
622 static_cast<int32_t>(error));
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800623
Mathias Agopian29a367b2011-07-12 14:51:45 -0700624 /*
625 * Transformations are applied in this order:
626 * 1) buffer orientation/flip/mirror
627 * 2) state transformation (window manager)
628 * 3) layer orientation (screen orientation)
629 * (NOTE: the matrices are multiplied in reverse order)
630 */
631
632 const Transform bufferOrientation(mCurrentTransform);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700633 Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700634
Robert Carrcae605c2017-03-29 12:10:31 -0700635 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700636 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700637 * the code below applies the primary display's inverse transform to the
638 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700639 */
David Sodman41fdfc92017-11-06 16:09:56 -0800640 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700641 // calculate the inverse transform
642 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800643 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700644 }
Robert Carrcae605c2017-03-29 12:10:31 -0700645
646 /*
647 * Here we cancel out the orientation component of the WM transform.
648 * The scaling and translate components are already included in our bounds
649 * computation so it's enough to just omit it in the composition.
650 * See comment in onDraw with ref to b/36727915 for why.
651 */
652 transform = Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700653 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700654
655 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800656 const uint32_t orientation = transform.getOrientation();
Jorim Jaggif3bd94a2018-03-27 15:38:03 +0200657 if (orientation & Transform::ROT_INVALID) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800658 // we can only handle simple transformation
Lloyd Pique074e8122018-07-26 12:57:23 -0700659 hwcInfo.forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800660 } else {
661 auto transform = static_cast<HWC2::Transform>(orientation);
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800662 hwcInfo.transform = transform;
Lloyd Pique074e8122018-07-26 12:57:23 -0700663 auto error = hwcLayer->setTransform(transform);
664 ALOGE_IF(error != HWC2::Error::None,
665 "[%s] Failed to set transform %s: "
666 "%s (%d)",
667 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
668 static_cast<int32_t>(error));
David Sodman4b7c4bc2017-11-17 12:13:59 -0800669 }
670}
671
Dominik Laskowski7e045462018-05-30 13:02:02 -0700672void Layer::forceClientComposition(int32_t displayId) {
673 if (getBE().mHwcLayers.count(displayId) == 0) {
674 ALOGE("forceClientComposition: no HWC layer found (%d)", displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800675 return;
676 }
677
Dominik Laskowski7e045462018-05-30 13:02:02 -0700678 getBE().mHwcLayers[displayId].forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800679}
Dan Stozaee44edd2015-03-23 15:50:23 -0700680
Dominik Laskowski7e045462018-05-30 13:02:02 -0700681bool Layer::getForceClientComposition(int32_t displayId) {
682 if (getBE().mHwcLayers.count(displayId) == 0) {
683 ALOGE("getForceClientComposition: no HWC layer found (%d)", displayId);
chaviwc9232ed2017-11-14 15:31:15 -0800684 return false;
685 }
686
Dominik Laskowski7e045462018-05-30 13:02:02 -0700687 return getBE().mHwcLayers[displayId].forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800688}
689
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700690void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700691 const auto displayId = display->getId();
692 if (getBE().mHwcLayers.count(displayId) == 0 ||
693 getCompositionType(displayId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800694 return;
695 }
696
697 // This gives us only the "orientation" component of the transform
698 const State& s(getCurrentState());
699
700 // Apply the layer's transform, followed by the display's global transform
701 // Here we're guaranteed that the layer's transform preserves rects
Marissa Wall61c58622018-07-18 10:12:20 -0700702 Rect win(getActiveWidth(s), getActiveHeight(s));
703 Rect crop = getCrop(s);
704 if (!crop.isEmpty()) {
705 win.intersect(crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800706 }
707 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700708 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Robert Carr1f0a16a2016-10-24 16:27:39 -0700709 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700710 frame.intersect(display->getViewport(), &frame);
Marissa Wall61c58622018-07-18 10:12:20 -0700711 Rect finalCrop = getFinalCrop(s);
712 if (!finalCrop.isEmpty()) {
713 frame.intersect(finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000714 }
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700715 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800716 auto position = displayTransform.transform(frame);
717
Dominik Laskowski7e045462018-05-30 13:02:02 -0700718 auto error =
719 getBE().mHwcLayers[displayId].layer->setCursorPosition(position.left, position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800720 ALOGE_IF(error != HWC2::Error::None,
721 "[%s] Failed to set cursor position "
722 "to (%d, %d): %s (%d)",
723 mName.string(), position.left, position.top, to_string(error).c_str(),
724 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800725}
Riley Andrews03414a12014-07-01 14:22:59 -0700726
Mathias Agopian13127d82013-03-05 17:47:11 -0800727// ---------------------------------------------------------------------------
728// drawing...
729// ---------------------------------------------------------------------------
730
Marissa Wall61c58622018-07-18 10:12:20 -0700731void Layer::draw(const RenderArea& renderArea, const Region& clip) {
chaviwa76b2712017-09-20 12:02:26 -0700732 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800733}
734
Marissa Wall61c58622018-07-18 10:12:20 -0700735void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) {
chaviwa76b2712017-09-20 12:02:26 -0700736 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800737}
738
Marissa Wall61c58622018-07-18 10:12:20 -0700739void Layer::draw(const RenderArea& renderArea) {
chaviwa76b2712017-09-20 12:02:26 -0700740 onDraw(renderArea, Region(renderArea.getBounds()), false);
Dan Stozac7014012014-02-14 15:03:43 -0800741}
742
David Sodman41fdfc92017-11-06 16:09:56 -0800743void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
744 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800745 auto& engine(mFlinger->getRenderEngine());
David Sodman9eeae692017-11-02 10:53:32 -0700746 computeGeometry(renderArea, getBE().mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700747 engine.setupFillWithColor(red, green, blue, alpha);
David Sodman9eeae692017-11-02 10:53:32 -0700748 engine.drawMesh(getBE().mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800749}
750
chaviwa76b2712017-09-20 12:02:26 -0700751void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
David Sodman41fdfc92017-11-06 16:09:56 -0800752 clearWithOpenGL(renderArea, 0, 0, 0, 0);
Mathias Agopian13127d82013-03-05 17:47:11 -0800753}
754
Dominik Laskowski7e045462018-05-30 13:02:02 -0700755void Layer::setCompositionType(int32_t displayId, HWC2::Composition type, bool callIntoHwc) {
756 if (getBE().mHwcLayers.count(displayId) == 0) {
Chia-I Wu30505fb2018-03-26 16:20:31 -0700757 ALOGE("setCompositionType called without a valid HWC layer");
758 return;
759 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700760 auto& hwcInfo = getBE().mHwcLayers[displayId];
Chia-I Wu30505fb2018-03-26 16:20:31 -0700761 auto& hwcLayer = hwcInfo.layer;
David Sodmanf6a38932018-05-25 15:27:50 -0700762 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(), to_string(type).c_str(),
Chia-I Wu30505fb2018-03-26 16:20:31 -0700763 static_cast<int>(callIntoHwc));
764 if (hwcInfo.compositionType != type) {
765 ALOGV(" actually setting");
766 hwcInfo.compositionType = type;
767 if (callIntoHwc) {
David Sodmanf6a38932018-05-25 15:27:50 -0700768 auto error = hwcLayer->setCompositionType(type);
Chia-I Wu30505fb2018-03-26 16:20:31 -0700769 ALOGE_IF(error != HWC2::Error::None,
770 "[%s] Failed to set "
771 "composition type %s: %s (%d)",
772 mName.string(), to_string(type).c_str(), to_string(error).c_str(),
773 static_cast<int32_t>(error));
774 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800775 }
776}
777
Dominik Laskowski7e045462018-05-30 13:02:02 -0700778HWC2::Composition Layer::getCompositionType(int32_t displayId) const {
779 if (displayId == DisplayDevice::DISPLAY_ID_INVALID) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700780 // If we're querying the composition type for a display that does not
781 // have a HWC counterpart, then it will always be Client
782 return HWC2::Composition::Client;
783 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700784 if (getBE().mHwcLayers.count(displayId) == 0) {
Chia-I Wu30505fb2018-03-26 16:20:31 -0700785 ALOGE("getCompositionType called with an invalid HWC layer");
786 return HWC2::Composition::Invalid;
787 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700788 return getBE().mHwcLayers.at(displayId).compositionType;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800789}
790
Dominik Laskowski7e045462018-05-30 13:02:02 -0700791void Layer::setClearClientTarget(int32_t displayId, bool clear) {
792 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800793 ALOGE("setClearClientTarget called without a valid HWC layer");
794 return;
795 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700796 getBE().mHwcLayers[displayId].clearClientTarget = clear;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800797}
798
Dominik Laskowski7e045462018-05-30 13:02:02 -0700799bool Layer::getClearClientTarget(int32_t displayId) const {
800 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800801 ALOGE("getClearClientTarget called without a valid HWC layer");
802 return false;
803 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700804 return getBE().mHwcLayers.at(displayId).clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800805}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800806
Dan Stozacac35382016-01-27 12:21:06 -0800807bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
808 if (point->getFrameNumber() <= mCurrentFrameNumber) {
809 // Don't bother with a SyncPoint, since we've already latched the
810 // relevant frame
811 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700812 }
813
Dan Stozacac35382016-01-27 12:21:06 -0800814 Mutex::Autolock lock(mLocalSyncPointMutex);
815 mLocalSyncPoints.push_back(point);
816 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700817}
818
Mathias Agopian13127d82013-03-05 17:47:11 -0800819void Layer::setFiltering(bool filtering) {
820 mFiltering = filtering;
821}
822
823bool Layer::getFiltering() const {
824 return mFiltering;
825}
826
Mathias Agopian13127d82013-03-05 17:47:11 -0800827// ----------------------------------------------------------------------------
828// local state
829// ----------------------------------------------------------------------------
830
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000831static void boundPoint(vec2* point, const Rect& crop) {
832 if (point->x < crop.left) {
833 point->x = crop.left;
834 }
835 if (point->x > crop.right) {
836 point->x = crop.right;
837 }
838 if (point->y < crop.top) {
839 point->y = crop.top;
840 }
841 if (point->y > crop.bottom) {
842 point->y = crop.bottom;
843 }
844}
845
chaviwa76b2712017-09-20 12:02:26 -0700846void Layer::computeGeometry(const RenderArea& renderArea, Mesh& mesh,
847 bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700848 const Layer::State& s(getDrawingState());
chaviwa76b2712017-09-20 12:02:26 -0700849 const Transform renderAreaTransform(renderArea.getTransform());
850 const uint32_t height = renderArea.getHeight();
Dan Stoza80d61162017-12-20 15:57:52 -0800851 FloatRect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700852
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000853 vec2 lt = vec2(win.left, win.top);
854 vec2 lb = vec2(win.left, win.bottom);
855 vec2 rb = vec2(win.right, win.bottom);
856 vec2 rt = vec2(win.right, win.top);
857
Robert Carr1f0a16a2016-10-24 16:27:39 -0700858 Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000859 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700860 lt = layerTransform.transform(lt);
861 lb = layerTransform.transform(lb);
862 rb = layerTransform.transform(rb);
863 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000864 }
865
Marissa Wall61c58622018-07-18 10:12:20 -0700866 Rect finalCrop = getFinalCrop(s);
867 if (!finalCrop.isEmpty()) {
868 boundPoint(&lt, finalCrop);
869 boundPoint(&lb, finalCrop);
870 boundPoint(&rb, finalCrop);
871 boundPoint(&rt, finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000872 }
873
Mathias Agopianff2ed702013-09-01 21:36:12 -0700874 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700875 position[0] = renderAreaTransform.transform(lt);
876 position[1] = renderAreaTransform.transform(lb);
877 position[2] = renderAreaTransform.transform(rb);
878 position[3] = renderAreaTransform.transform(rt);
David Sodman41fdfc92017-11-06 16:09:56 -0800879 for (size_t i = 0; i < 4; i++) {
chaviwa76b2712017-09-20 12:02:26 -0700880 position[i].y = height - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -0800881 }
882}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800883
David Sodman41fdfc92017-11-06 16:09:56 -0800884bool Layer::isSecure() const {
Dan Stoza23116082015-06-18 14:58:39 -0700885 const Layer::State& s(mDrawingState);
886 return (s.flags & layer_state_t::eLayerSecure);
887}
888
Mathias Agopian13127d82013-03-05 17:47:11 -0800889void Layer::setVisibleRegion(const Region& visibleRegion) {
890 // always called from main thread
891 this->visibleRegion = visibleRegion;
892}
893
894void Layer::setCoveredRegion(const Region& coveredRegion) {
895 // always called from main thread
896 this->coveredRegion = coveredRegion;
897}
898
David Sodman41fdfc92017-11-06 16:09:56 -0800899void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800900 // always called from main thread
901 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
902}
903
Robert Carre5f4f692018-01-12 13:12:28 -0800904void Layer::clearVisibilityRegions() {
905 visibleRegion.clear();
906 visibleNonTransparentRegion.clear();
907 coveredRegion.clear();
908}
909
Mathias Agopian13127d82013-03-05 17:47:11 -0800910// ----------------------------------------------------------------------------
911// transaction
912// ----------------------------------------------------------------------------
913
Dan Stoza7dde5992015-05-22 09:51:44 -0700914void Layer::pushPendingState() {
915 if (!mCurrentState.modified) {
916 return;
917 }
918
Dan Stoza7dde5992015-05-22 09:51:44 -0700919 // If this transaction is waiting on the receipt of a frame, generate a sync
920 // point and send it to the remote layer.
Marissa Wallf58c14b2018-07-24 10:50:43 -0700921 if (mCurrentState.barrierLayer_legacy != nullptr) {
922 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800923 if (barrierLayer == nullptr) {
924 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700925 // If we can't promote the layer we are intended to wait on,
926 // then it is expired or otherwise invalid. Allow this transaction
927 // to be applied as per normal (no synchronization).
Marissa Wallf58c14b2018-07-24 10:50:43 -0700928 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800929 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700930 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy);
Robert Carr0d480722017-01-10 16:42:54 -0800931 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800932 mRemoteSyncPoints.push_back(std::move(syncPoint));
933 } else {
934 // We already missed the frame we're supposed to synchronize
935 // on, so go ahead and apply the state update
Marissa Wallf58c14b2018-07-24 10:50:43 -0700936 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800937 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700938 }
939
Dan Stoza7dde5992015-05-22 09:51:44 -0700940 // Wake us up to check if the frame has been received
941 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700942 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700943 }
944 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700945 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700946}
947
Pablo Ceballos05289c22016-04-14 15:49:55 -0700948void Layer::popPendingState(State* stateToCommit) {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700949 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700950
951 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700952 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700953}
954
Pablo Ceballos05289c22016-04-14 15:49:55 -0700955bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700956 bool stateUpdateAvailable = false;
957 while (!mPendingStates.empty()) {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700958 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700959 if (mRemoteSyncPoints.empty()) {
960 // If we don't have a sync point for this, apply it anyway. It
961 // will be visually wrong, but it should keep us from getting
962 // into too much trouble.
963 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700964 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700965 stateUpdateAvailable = true;
966 continue;
967 }
968
Marissa Wallf58c14b2018-07-24 10:50:43 -0700969 if (mRemoteSyncPoints.front()->getFrameNumber() !=
970 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800971 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800972
973 // Signal our end of the sync point and then dispose of it
974 mRemoteSyncPoints.front()->setTransactionApplied();
975 mRemoteSyncPoints.pop_front();
976 continue;
977 }
978
Dan Stoza7dde5992015-05-22 09:51:44 -0700979 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
980 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700981 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700982 stateUpdateAvailable = true;
983
984 // Signal our end of the sync point and then dispose of it
985 mRemoteSyncPoints.front()->setTransactionApplied();
986 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800987 } else {
988 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700989 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700990 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700991 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700992 stateUpdateAvailable = true;
993 }
994 }
995
996 // If we still have pending updates, wake SurfaceFlinger back up and point
997 // it at this layer so we can process them
998 if (!mPendingStates.empty()) {
999 setTransactionFlags(eTransactionNeeded);
1000 mFlinger->setTransactionFlags(eTraversalNeeded);
1001 }
1002
1003 mCurrentState.modified = false;
1004 return stateUpdateAvailable;
1005}
1006
Marissa Wall61c58622018-07-18 10:12:20 -07001007uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001008 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001009
Marissa Wall61c58622018-07-18 10:12:20 -07001010 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
1011 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -07001012
David Sodmaneb085e02017-10-05 18:49:04 -07001013 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001014 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +00001015 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -08001016 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
1017 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1018 " requested={ wh={%4u,%4u} }}\n"
1019 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1020 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -07001021 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -07001022 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
1023 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
1024 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
1025 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
1026 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -07001027 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
1028 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
1029 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001030
Jamie Gennis2a0d5b62011-09-26 16:54:44 -07001031 // record the new size, form this point on, when the client request
1032 // a buffer, it'll get the new size.
Marissa Wall61c58622018-07-18 10:12:20 -07001033 setDefaultBufferSize(stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001034 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001035
Robert Carre392b552017-09-19 12:16:05 -07001036 // Don't let Layer::doTransaction update the drawing state
1037 // if we have a pending resize, unless we are in fixed-size mode.
1038 // the drawing state will be updated only once we receive a buffer
1039 // with the correct size.
1040 //
1041 // In particular, we want to make sure the clip (which is part
1042 // of the geometry state) is latched together with the size but is
1043 // latched immediately when no resizing is involved.
1044 //
1045 // If a sideband stream is attached, however, we want to skip this
1046 // optimization so that transactions aren't missed when a buffer
1047 // never arrives
1048 //
1049 // In the case that we don't have a buffer we ignore other factors
1050 // and avoid entering the resizePending state. At a high level the
1051 // resizePending state is to avoid applying the state of the new buffer
1052 // to the old buffer. However in the state where we don't have an old buffer
1053 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -07001054 const bool resizePending =
1055 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
1056 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
David Sodman0cc69182017-11-17 12:12:07 -08001057 (getBE().compositionInfo.mBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001058 if (!isFixedSize()) {
David Sodman0cc69182017-11-17 12:12:07 -08001059 if (resizePending && getBE().compositionInfo.hwc.sidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001060 flags |= eDontUpdateGeometryState;
1061 }
1062 }
1063
Robert Carr7bf247e2017-05-18 14:02:49 -07001064 // Here we apply various requested geometry states, depending on our
1065 // latching configuration. See Layer.h for a detailed discussion of
1066 // how geometry latching is controlled.
1067 if (!(flags & eDontUpdateGeometryState)) {
Pablo Ceballos7d052572016-06-02 17:46:05 -07001068 Layer::State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001069
1070 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1071 // mode, which causes attributes which normally latch regardless of scaling mode,
1072 // to be delayed. We copy the requested state to the active state making sure
1073 // to respect these rules (again see Layer.h for a detailed discussion).
1074 //
1075 // There is an awkward asymmetry in the handling of the crop states in the position
1076 // states, as can be seen below. Largely this arises from position and transform
1077 // being stored in the same data structure while having different latching rules.
1078 // b/38182305
1079 //
Marissa Wall61c58622018-07-18 10:12:20 -07001080 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -07001081 // applyPendingStates in the presence of deferred transactions.
1082 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -07001083 float tx = stateToCommit->active_legacy.transform.tx();
1084 float ty = stateToCommit->active_legacy.transform.ty();
1085 stateToCommit->active_legacy = stateToCommit->requested_legacy;
1086 stateToCommit->active_legacy.transform.set(tx, ty);
1087 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001088 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001089 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -07001090 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001091 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001092 }
1093
Marissa Wall61c58622018-07-18 10:12:20 -07001094 return flags;
1095}
1096
1097uint32_t Layer::doTransaction(uint32_t flags) {
1098 ATRACE_CALL();
1099
1100 pushPendingState();
1101 Layer::State c = getCurrentState();
1102 if (!applyPendingStates(&c)) {
1103 return 0;
1104 }
1105
1106 flags = doTransactionResize(flags, &c);
1107
1108 const Layer::State& s(getDrawingState());
1109
1110 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001111 // invalidate and recompute the visible regions if needed
1112 flags |= Layer::eVisibleRegion;
1113 }
1114
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001115 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001116 // invalidate and recompute the visible regions if needed
1117 flags |= eVisibleRegion;
1118 this->contentDirty = true;
1119
1120 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -07001121 const uint8_t type = getActiveTransform(c).getType();
1122 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || (type >= Transform::SCALE));
Mathias Agopian13127d82013-03-05 17:47:11 -08001123 }
1124
Dan Stozac8145172016-04-28 16:29:06 -07001125 // If the layer is hidden, signal and clear out all local sync points so
1126 // that transactions for layers depending on this layer's frames becoming
1127 // visible are not blocked
1128 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001129 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001130 }
1131
Mathias Agopian13127d82013-03-05 17:47:11 -08001132 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001133 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001134 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001135}
1136
Pablo Ceballos05289c22016-04-14 15:49:55 -07001137void Layer::commitTransaction(const State& stateToCommit) {
1138 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001139}
1140
Mathias Agopian13127d82013-03-05 17:47:11 -08001141uint32_t Layer::getTransactionFlags(uint32_t flags) {
1142 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1143}
1144
1145uint32_t Layer::setTransactionFlags(uint32_t flags) {
1146 return android_atomic_or(flags, &mTransactionFlags);
1147}
1148
Robert Carr82364e32016-05-15 11:27:47 -07001149bool Layer::setPosition(float x, float y, bool immediate) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001150 if (mCurrentState.requested_legacy.transform.tx() == x &&
1151 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001152 return false;
1153 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001154
1155 // We update the requested and active position simultaneously because
1156 // we want to apply the position portion of the transform matrix immediately,
1157 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Marissa Wallf58c14b2018-07-24 10:50:43 -07001158 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001159 if (immediate && !mFreezeGeometryUpdates) {
1160 // Here we directly update the active state
1161 // unlike other setters, because we store it within
1162 // the transform, but use different latching rules.
1163 // b/38182305
Marissa Wallf58c14b2018-07-24 10:50:43 -07001164 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001165 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001166 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001167
Dan Stoza7dde5992015-05-22 09:51:44 -07001168 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001169 setTransactionFlags(eTransactionNeeded);
1170 return true;
1171}
Robert Carr82364e32016-05-15 11:27:47 -07001172
Robert Carr1f0a16a2016-10-24 16:27:39 -07001173bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1174 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1175 if (idx < 0) {
1176 return false;
1177 }
1178 if (childLayer->setLayer(z)) {
1179 mCurrentChildren.removeAt(idx);
1180 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001181 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001182 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001183 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001184}
1185
Robert Carr503c7042017-09-27 15:06:08 -07001186bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1187 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1188 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1189 if (idx < 0) {
1190 return false;
1191 }
1192 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1193 mCurrentChildren.removeAt(idx);
1194 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001195 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001196 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001197 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001198}
1199
Robert Carrae060832016-11-28 10:51:00 -08001200bool Layer::setLayer(int32_t z) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001201 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001202 mCurrentState.sequence++;
1203 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001204 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001205
1206 // Discard all relative layering.
1207 if (mCurrentState.zOrderRelativeOf != nullptr) {
1208 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1209 if (strongRelative != nullptr) {
1210 strongRelative->removeZOrderRelative(this);
1211 }
1212 mCurrentState.zOrderRelativeOf = nullptr;
1213 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001214 setTransactionFlags(eTransactionNeeded);
1215 return true;
1216}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001217
Robert Carrdb66e622017-04-10 16:55:57 -07001218void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1219 mCurrentState.zOrderRelatives.remove(relative);
1220 mCurrentState.sequence++;
1221 mCurrentState.modified = true;
1222 setTransactionFlags(eTransactionNeeded);
1223}
1224
1225void Layer::addZOrderRelative(const wp<Layer>& relative) {
1226 mCurrentState.zOrderRelatives.add(relative);
1227 mCurrentState.modified = true;
1228 mCurrentState.sequence++;
1229 setTransactionFlags(eTransactionNeeded);
1230}
1231
Robert Carr503d2bd2017-12-04 15:49:47 -08001232bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001233 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1234 if (handle == nullptr) {
1235 return false;
1236 }
1237 sp<Layer> relative = handle->owner.promote();
1238 if (relative == nullptr) {
1239 return false;
1240 }
1241
Robert Carr503d2bd2017-12-04 15:49:47 -08001242 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1243 mCurrentState.zOrderRelativeOf == relative) {
1244 return false;
1245 }
1246
Robert Carrdb66e622017-04-10 16:55:57 -07001247 mCurrentState.sequence++;
1248 mCurrentState.modified = true;
Robert Carr503d2bd2017-12-04 15:49:47 -08001249 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001250
chaviw9ab4bd12017-11-03 13:11:00 -07001251 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
1252 if (oldZOrderRelativeOf != nullptr) {
1253 oldZOrderRelativeOf->removeZOrderRelative(this);
1254 }
Robert Carrdb66e622017-04-10 16:55:57 -07001255 mCurrentState.zOrderRelativeOf = relative;
1256 relative->addZOrderRelative(this);
1257
1258 setTransactionFlags(eTransactionNeeded);
1259
1260 return true;
1261}
1262
Mathias Agopian13127d82013-03-05 17:47:11 -08001263bool Layer::setSize(uint32_t w, uint32_t h) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001264 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
1265 return false;
1266 mCurrentState.requested_legacy.w = w;
1267 mCurrentState.requested_legacy.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001268 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001269 setTransactionFlags(eTransactionNeeded);
1270 return true;
1271}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001272bool Layer::setAlpha(float alpha) {
David Sodman41fdfc92017-11-06 16:09:56 -08001273 if (mCurrentState.color.a == alpha) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001274 mCurrentState.sequence++;
chaviw13fdc492017-06-27 12:40:18 -07001275 mCurrentState.color.a = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001276 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001277 setTransactionFlags(eTransactionNeeded);
1278 return true;
1279}
chaviw13fdc492017-06-27 12:40:18 -07001280
1281bool Layer::setColor(const half3& color) {
David Sodman41fdfc92017-11-06 16:09:56 -08001282 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g &&
1283 color.b == mCurrentState.color.b)
chaviw13fdc492017-06-27 12:40:18 -07001284 return false;
1285
1286 mCurrentState.sequence++;
1287 mCurrentState.color.r = color.r;
1288 mCurrentState.color.g = color.g;
1289 mCurrentState.color.b = color.b;
1290 mCurrentState.modified = true;
1291 setTransactionFlags(eTransactionNeeded);
1292 return true;
1293}
1294
Robert Carrd4ae7f32018-06-07 16:10:57 -07001295bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1296 bool allowNonRectPreservingTransforms) {
1297 Transform t;
1298 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1299
1300 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1301 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1302 return false;
1303 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001304 mCurrentState.sequence++;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001305 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1306 matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001307 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001308 setTransactionFlags(eTransactionNeeded);
1309 return true;
1310}
Marissa Wall61c58622018-07-18 10:12:20 -07001311
Mathias Agopian13127d82013-03-05 17:47:11 -08001312bool Layer::setTransparentRegionHint(const Region& transparent) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001313 mCurrentState.requestedTransparentRegion_legacy = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001314 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001315 setTransactionFlags(eTransactionNeeded);
1316 return true;
1317}
1318bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1319 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
David Sodman41fdfc92017-11-06 16:09:56 -08001320 if (mCurrentState.flags == newFlags) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001321 mCurrentState.sequence++;
1322 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001323 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001324 setTransactionFlags(eTransactionNeeded);
1325 return true;
1326}
Robert Carr99e27f02016-06-16 15:18:02 -07001327
Marissa Wallf58c14b2018-07-24 10:50:43 -07001328bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
1329 if (mCurrentState.requestedCrop_legacy == crop) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001330 mCurrentState.sequence++;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001331 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001332 if (immediate && !mFreezeGeometryUpdates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001333 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001334 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001335 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1336
Dan Stoza7dde5992015-05-22 09:51:44 -07001337 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001338 setTransactionFlags(eTransactionNeeded);
1339 return true;
1340}
Robert Carr8d5227b2017-03-16 15:41:03 -07001341
Marissa Wallf58c14b2018-07-24 10:50:43 -07001342bool Layer::setFinalCrop_legacy(const Rect& crop, bool immediate) {
1343 if (mCurrentState.requestedFinalCrop_legacy == crop) return false;
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001344 mCurrentState.sequence++;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001345 mCurrentState.requestedFinalCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001346 if (immediate && !mFreezeGeometryUpdates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001347 mCurrentState.finalCrop_legacy = crop;
Robert Carr8d5227b2017-03-16 15:41:03 -07001348 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001349 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1350
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001351 mCurrentState.modified = true;
1352 setTransactionFlags(eTransactionNeeded);
1353 return true;
1354}
Mathias Agopian13127d82013-03-05 17:47:11 -08001355
Robert Carrc3574f72016-03-24 12:19:32 -07001356bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001357 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001358 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001359 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001360 return true;
1361}
1362
rongliucfb187b2018-03-14 12:26:23 -07001363void Layer::setInfo(int32_t type, int32_t appId) {
David Sodman41fdfc92017-11-06 16:09:56 -08001364 mCurrentState.appId = appId;
1365 mCurrentState.type = type;
1366 mCurrentState.modified = true;
1367 setTransactionFlags(eTransactionNeeded);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001368}
1369
Mathias Agopian13127d82013-03-05 17:47:11 -08001370bool Layer::setLayerStack(uint32_t layerStack) {
David Sodman41fdfc92017-11-06 16:09:56 -08001371 if (mCurrentState.layerStack == layerStack) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001372 mCurrentState.sequence++;
1373 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001374 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001375 setTransactionFlags(eTransactionNeeded);
1376 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001377}
1378
Robert Carr1f0a16a2016-10-24 16:27:39 -07001379uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001380 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001381 if (p == nullptr) {
1382 return getDrawingState().layerStack;
1383 }
1384 return p->getLayerStack();
1385}
1386
Marissa Wallf58c14b2018-07-24 10:50:43 -07001387void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
1388 mCurrentState.barrierLayer_legacy = barrierLayer;
1389 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001390 // We don't set eTransactionNeeded, because just receiving a deferral
1391 // request without any other state updates shouldn't actually induce a delay
1392 mCurrentState.modified = true;
1393 pushPendingState();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001394 mCurrentState.barrierLayer_legacy = nullptr;
1395 mCurrentState.frameNumber_legacy = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001396 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001397}
1398
Marissa Wallf58c14b2018-07-24 10:50:43 -07001399void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001400 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001401 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001402}
1403
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001404// ----------------------------------------------------------------------------
1405// pageflip handling...
1406// ----------------------------------------------------------------------------
1407
Robert Carr1f0a16a2016-10-24 16:27:39 -07001408bool Layer::isHiddenByPolicy() const {
1409 const Layer::State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001410 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001411 if (parent != nullptr && parent->isHiddenByPolicy()) {
1412 return true;
1413 }
1414 return s.flags & layer_state_t::eLayerHidden;
1415}
1416
David Sodman41fdfc92017-11-06 16:09:56 -08001417uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001418 // TODO: should we do something special if mSecure is set?
1419 if (mProtectedByApp) {
1420 // need a hardware-protected path to external video sink
1421 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001422 }
Riley Andrews03414a12014-07-01 14:22:59 -07001423 if (mPotentialCursor) {
1424 usage |= GraphicBuffer::USAGE_CURSOR;
1425 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001426 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001427 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001428}
1429
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001430void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001431 uint32_t orientation = 0;
1432 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001433 // The transform hint is used to improve performance, but we can
1434 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001435 // apply to all displays.
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001436 const Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001437 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07001438 if (orientation & Transform::ROT_INVALID) {
1439 orientation = 0;
1440 }
1441 }
David Sodmaneb085e02017-10-05 18:49:04 -07001442 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001443}
1444
Mathias Agopian13127d82013-03-05 17:47:11 -08001445// ----------------------------------------------------------------------------
1446// debugging
1447// ----------------------------------------------------------------------------
1448
Marissa Wall61c58622018-07-18 10:12:20 -07001449// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001450LayerDebugInfo Layer::getLayerDebugInfo() const {
1451 LayerDebugInfo info;
1452 const Layer::State& ds = getDrawingState();
1453 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001454 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001455 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
1456 info.mType = String8(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001457 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001458 info.mVisibleRegion = visibleRegion;
1459 info.mSurfaceDamageRegion = surfaceDamageRegion;
1460 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001461 info.mX = ds.active_legacy.transform.tx();
1462 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001463 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001464 info.mWidth = ds.active_legacy.w;
1465 info.mHeight = ds.active_legacy.h;
1466 info.mCrop = ds.crop_legacy;
1467 info.mFinalCrop = ds.finalCrop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001468 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001469 info.mFlags = ds.flags;
1470 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001471 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001472 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1473 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1474 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1475 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001476 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001477 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001478 if (buffer != 0) {
1479 info.mActiveBufferWidth = buffer->getWidth();
1480 info.mActiveBufferHeight = buffer->getHeight();
1481 info.mActiveBufferStride = buffer->getStride();
1482 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001483 } else {
1484 info.mActiveBufferWidth = 0;
1485 info.mActiveBufferHeight = 0;
1486 info.mActiveBufferStride = 0;
1487 info.mActiveBufferFormat = 0;
1488 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001489 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001490 info.mNumQueuedFrames = getQueuedFrameCount();
1491 info.mRefreshPending = isBufferLatched();
1492 info.mIsOpaque = isOpaque(ds);
1493 info.mContentDirty = contentDirty;
1494 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001495}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001496
Dan Stozae22aec72016-08-01 13:20:59 -07001497void Layer::miniDumpHeader(String8& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001498 result.append("-------------------------------");
1499 result.append("-------------------------------");
1500 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001501 result.append(" Layer name\n");
1502 result.append(" Z | ");
1503 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001504 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001505 result.append(" Disp Frame (LTRB) | ");
1506 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001507 result.append("-------------------------------");
1508 result.append("-------------------------------");
1509 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001510}
1511
Dominik Laskowski7e045462018-05-30 13:02:02 -07001512void Layer::miniDump(String8& result, int32_t displayId) const {
1513 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stozae22aec72016-08-01 13:20:59 -07001514 return;
1515 }
1516
1517 String8 name;
1518 if (mName.length() > 77) {
1519 std::string shortened;
1520 shortened.append(mName.string(), 36);
1521 shortened.append("[...]");
1522 shortened.append(mName.string() + (mName.length() - 36), 36);
1523 name = shortened.c_str();
1524 } else {
1525 name = mName;
1526 }
1527
1528 result.appendFormat(" %s\n", name.string());
1529
1530 const Layer::State& layerState(getDrawingState());
Lloyd Pique074e8122018-07-26 12:57:23 -07001531 const LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers.at(displayId);
Chia-I Wu1e043612018-03-01 09:45:09 -08001532 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
1533 result.appendFormat(" rel %6d | ", layerState.z);
1534 } else {
1535 result.appendFormat(" %10d | ", layerState.z);
1536 }
Dominik Laskowski7e045462018-05-30 13:02:02 -07001537 result.appendFormat("%10s | ", to_string(getCompositionType(displayId)).c_str());
Lloyd Pique074e8122018-07-26 12:57:23 -07001538 result.appendFormat("%10s | ", to_string(hwcInfo.transform).c_str());
1539 const Rect& frame = hwcInfo.displayFrame;
David Sodman41fdfc92017-11-06 16:09:56 -08001540 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique074e8122018-07-26 12:57:23 -07001541 const FloatRect& crop = hwcInfo.sourceCrop;
David Sodman41fdfc92017-11-06 16:09:56 -08001542 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 -07001543
Yichi Chen6ca35192018-05-29 12:20:43 +08001544 result.append("- - - - - - - - - - - - - - - -");
1545 result.append("- - - - - - - - - - - - - - - -");
1546 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001547}
Dan Stozae22aec72016-08-01 13:20:59 -07001548
Svetoslavd85084b2014-03-20 10:28:31 -07001549void Layer::dumpFrameStats(String8& result) const {
1550 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001551}
1552
Svetoslavd85084b2014-03-20 10:28:31 -07001553void Layer::clearFrameStats() {
1554 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001555}
1556
Jamie Gennis6547ff42013-07-16 20:12:42 -07001557void Layer::logFrameStats() {
1558 mFrameTracker.logAndResetStats(mName);
1559}
1560
Svetoslavd85084b2014-03-20 10:28:31 -07001561void Layer::getFrameStats(FrameStats* outStats) const {
1562 mFrameTracker.getStats(outStats);
1563}
1564
Brian Andersond6927fb2016-07-23 23:37:30 -07001565void Layer::dumpFrameEvents(String8& result) {
David Sodman41fdfc92017-11-06 16:09:56 -08001566 result.appendFormat("- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001567 Mutex::Autolock lock(mFrameEventHistoryMutex);
1568 mFrameEventHistory.checkFencesForCompletion();
1569 mFrameEventHistory.dump(result);
1570}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001571
Brian Anderson5ea5e592016-12-01 16:54:33 -08001572void Layer::onDisconnect() {
1573 Mutex::Autolock lock(mFrameEventHistoryMutex);
1574 mFrameEventHistory.onDisconnect();
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001575 mTimeStats.onDisconnect(getName().c_str());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001576}
1577
Brian Anderson3890c392016-07-25 12:48:08 -07001578void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001579 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001580 if (newTimestamps) {
1581 mTimeStats.setPostTime(getName().c_str(), newTimestamps->frameNumber,
1582 newTimestamps->postedTime);
1583 }
1584
Brian Andersond6927fb2016-07-23 23:37:30 -07001585 Mutex::Autolock lock(mFrameEventHistoryMutex);
1586 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001587 // If there are any unsignaled fences in the aquire timeline at this
1588 // point, the previously queued frame hasn't been latched yet. Go ahead
1589 // and try to get the signal time here so the syscall is taken out of
1590 // the main thread's critical path.
1591 mAcquireTimeline.updateSignalTimes();
1592 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001593 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001594 mFrameEventHistory.addQueue(*newTimestamps);
1595 }
1596
Brian Anderson3890c392016-07-25 12:48:08 -07001597 if (outDelta) {
1598 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001599 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001600}
Dan Stozae77c7662016-05-13 11:37:28 -07001601
Chia-I Wu98f1c102017-05-30 14:54:08 -07001602size_t Layer::getChildrenCount() const {
1603 size_t count = 0;
1604 for (const sp<Layer>& child : mCurrentChildren) {
1605 count += 1 + child->getChildrenCount();
1606 }
1607 return count;
1608}
1609
Robert Carr1f0a16a2016-10-24 16:27:39 -07001610void Layer::addChild(const sp<Layer>& layer) {
1611 mCurrentChildren.add(layer);
1612 layer->setParent(this);
1613}
1614
1615ssize_t Layer::removeChild(const sp<Layer>& layer) {
1616 layer->setParent(nullptr);
1617 return mCurrentChildren.remove(layer);
1618}
1619
Robert Carr1db73f62016-12-21 12:58:51 -08001620bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1621 sp<Handle> handle = nullptr;
1622 sp<Layer> newParent = nullptr;
1623 if (newParentHandle == nullptr) {
1624 return false;
1625 }
1626 handle = static_cast<Handle*>(newParentHandle.get());
1627 newParent = handle->owner.promote();
1628 if (newParent == nullptr) {
1629 ALOGE("Unable to promote Layer handle");
1630 return false;
1631 }
1632
1633 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001634 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001635
1636 sp<Client> client(child->mClientRef.promote());
1637 if (client != nullptr) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001638 client->updateParent(newParent);
Robert Carr1db73f62016-12-21 12:58:51 -08001639 }
1640 }
1641 mCurrentChildren.clear();
1642
1643 return true;
1644}
1645
Robert Carr15eae092018-03-23 13:43:53 -07001646void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001647 for (const sp<Layer>& child : mDrawingChildren) {
1648 child->mDrawingParent = newParent;
1649 }
1650}
1651
chaviwf1961f72017-09-18 16:41:07 -07001652bool Layer::reparent(const sp<IBinder>& newParentHandle) {
1653 if (newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001654 return false;
1655 }
1656
1657 auto handle = static_cast<Handle*>(newParentHandle.get());
1658 sp<Layer> newParent = handle->owner.promote();
1659 if (newParent == nullptr) {
1660 ALOGE("Unable to promote Layer handle");
1661 return false;
1662 }
1663
chaviwf1961f72017-09-18 16:41:07 -07001664 sp<Layer> parent = getParent();
1665 if (parent != nullptr) {
1666 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001667 }
chaviwf1961f72017-09-18 16:41:07 -07001668 newParent->addChild(this);
chaviw06178942017-07-27 10:25:59 -07001669
chaviwf1961f72017-09-18 16:41:07 -07001670 sp<Client> client(mClientRef.promote());
chaviw06178942017-07-27 10:25:59 -07001671 sp<Client> newParentClient(newParent->mClientRef.promote());
1672
chaviwf1961f72017-09-18 16:41:07 -07001673 if (client != newParentClient) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001674 client->updateParent(newParent);
chaviw06178942017-07-27 10:25:59 -07001675 }
1676
chaviw06178942017-07-27 10:25:59 -07001677 return true;
1678}
1679
Robert Carr9524cb32017-02-13 11:32:32 -08001680bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001681 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001682 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001683 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001684 if (client != nullptr && parentClient != client) {
Robert Carr7f619b22017-11-06 12:56:35 -08001685 client->detachLayer(child.get());
1686 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001687 }
Robert Carr7f619b22017-11-06 12:56:35 -08001688 }
Robert Carr9524cb32017-02-13 11:32:32 -08001689
1690 return true;
1691}
1692
Chia-I Wu11481472018-05-04 10:43:19 -07001693bool Layer::isLegacyDataSpace() const {
1694 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001695 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001696 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001697}
1698
Robert Carr1f0a16a2016-10-24 16:27:39 -07001699void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001700 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001701}
1702
1703void Layer::clearSyncPoints() {
1704 for (const auto& child : mCurrentChildren) {
1705 child->clearSyncPoints();
1706 }
1707
1708 Mutex::Autolock lock(mLocalSyncPointMutex);
1709 for (auto& point : mLocalSyncPoints) {
1710 point->setFrameAvailable();
1711 }
1712 mLocalSyncPoints.clear();
1713}
1714
1715int32_t Layer::getZ() const {
1716 return mDrawingState.z;
1717}
1718
Robert Carr29abff82017-12-04 13:51:20 -08001719bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1720 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1721 const State& state = useDrawing ? mDrawingState : mCurrentState;
1722 return state.zOrderRelativeOf != nullptr;
1723}
1724
David Sodman41fdfc92017-11-06 16:09:56 -08001725__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001726 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001727 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1728 "makeTraversalList received invalid stateSet");
1729 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1730 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1731 const State& state = useDrawing ? mDrawingState : mCurrentState;
1732
Robert Carr29abff82017-12-04 13:51:20 -08001733 if (state.zOrderRelatives.size() == 0) {
1734 *outSkipRelativeZUsers = true;
1735 return children;
1736 }
1737
chaviwfd462612018-05-31 16:11:27 -07001738 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001739 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001740 sp<Layer> strongRelative = weakRelative.promote();
1741 if (strongRelative != nullptr) {
1742 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001743 }
1744 }
1745
Dan Stoza412903f2017-04-27 13:42:17 -07001746 for (const sp<Layer>& child : children) {
Robert Carr503c7042017-09-27 15:06:08 -07001747 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1748 if (childState.zOrderRelativeOf != nullptr) {
1749 continue;
1750 }
Robert Carrdb66e622017-04-10 16:55:57 -07001751 traverse.add(child);
1752 }
1753
1754 return traverse;
1755}
1756
Robert Carr1f0a16a2016-10-24 16:27:39 -07001757/**
Robert Carrdb66e622017-04-10 16:55:57 -07001758 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001759 */
Dan Stoza412903f2017-04-27 13:42:17 -07001760void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001761 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1762 // produce a new list for traversing, including our relatives, and not including our children
1763 // who are relatives of another surface. In the case that there are no relative Z,
1764 // makeTraversalList returns our children directly to avoid significant overhead.
1765 // However in this case we need to take the responsibility for filtering children which
1766 // are relatives of another surface here.
1767 bool skipRelativeZUsers = false;
1768 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001769
Robert Carr1f0a16a2016-10-24 16:27:39 -07001770 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001771 for (; i < list.size(); i++) {
1772 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001773 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1774 continue;
1775 }
1776
Robert Carrdb66e622017-04-10 16:55:57 -07001777 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001778 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001779 }
Dan Stoza412903f2017-04-27 13:42:17 -07001780 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001781 }
Robert Carr29abff82017-12-04 13:51:20 -08001782
Dan Stoza412903f2017-04-27 13:42:17 -07001783 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001784 for (; i < list.size(); i++) {
1785 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001786
1787 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1788 continue;
1789 }
Dan Stoza412903f2017-04-27 13:42:17 -07001790 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001791 }
1792}
1793
1794/**
Robert Carrdb66e622017-04-10 16:55:57 -07001795 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001796 */
Dan Stoza412903f2017-04-27 13:42:17 -07001797void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1798 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001799 // See traverseInZOrder for documentation.
1800 bool skipRelativeZUsers = false;
1801 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001802
Robert Carr1f0a16a2016-10-24 16:27:39 -07001803 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001804 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001805 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001806
1807 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1808 continue;
1809 }
1810
Robert Carrdb66e622017-04-10 16:55:57 -07001811 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001812 break;
1813 }
Dan Stoza412903f2017-04-27 13:42:17 -07001814 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001815 }
Dan Stoza412903f2017-04-27 13:42:17 -07001816 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001817 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001818 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001819
1820 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1821 continue;
1822 }
1823
Dan Stoza412903f2017-04-27 13:42:17 -07001824 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001825 }
1826}
1827
chaviw4b129c22018-04-09 16:19:43 -07001828LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1829 const std::vector<Layer*>& layersInTree) {
1830 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1831 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001832 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1833 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
chaviw4b129c22018-04-09 16:19:43 -07001834 const State& state = useDrawing ? mDrawingState : mCurrentState;
1835
chaviwfd462612018-05-31 16:11:27 -07001836 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001837 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1838 sp<Layer> strongRelative = weakRelative.promote();
1839 // Only add relative layers that are also descendents of the top most parent of the tree.
1840 // If a relative layer is not a descendent, then it should be ignored.
1841 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1842 traverse.add(strongRelative);
1843 }
1844 }
1845
1846 for (const sp<Layer>& child : children) {
1847 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1848 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1849 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1850 // the child here since it won't be added later as a relative.
1851 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1852 childState.zOrderRelativeOf.promote().get())) {
1853 continue;
1854 }
1855 traverse.add(child);
1856 }
1857
1858 return traverse;
1859}
1860
1861void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1862 LayerVector::StateSet stateSet,
1863 const LayerVector::Visitor& visitor) {
1864 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001865
1866 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001867 for (; i < list.size(); i++) {
1868 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001869 if (relative->getZ() >= 0) {
1870 break;
1871 }
chaviw4b129c22018-04-09 16:19:43 -07001872 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001873 }
chaviw4b129c22018-04-09 16:19:43 -07001874
chaviwa76b2712017-09-20 12:02:26 -07001875 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001876 for (; i < list.size(); i++) {
1877 const auto& relative = list[i];
1878 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001879 }
1880}
1881
chaviw4b129c22018-04-09 16:19:43 -07001882std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1883 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1884 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1885
1886 std::vector<Layer*> layersInTree = {this};
1887 for (size_t i = 0; i < children.size(); i++) {
1888 const auto& child = children[i];
1889 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1890 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1891 }
1892
1893 return layersInTree;
1894}
1895
1896void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1897 const LayerVector::Visitor& visitor) {
1898 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1899 std::sort(layersInTree.begin(), layersInTree.end());
1900 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1901}
1902
Robert Carr1f0a16a2016-10-24 16:27:39 -07001903Transform Layer::getTransform() const {
1904 Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001905 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001906 if (p != nullptr) {
1907 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07001908
1909 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
1910 // it isFixedSize) then there may be additional scaling not accounted
1911 // for in the transform. We need to mirror this scaling in child surfaces
1912 // or we will break the contract where WM can treat child surfaces as
1913 // pixels in the parent surface.
David Sodman0cc69182017-11-17 12:12:07 -08001914 if (p->isFixedSize() && p->getBE().compositionInfo.mBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07001915 int bufferWidth;
1916 int bufferHeight;
1917 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
David Sodman0cc69182017-11-17 12:12:07 -08001918 bufferWidth = p->getBE().compositionInfo.mBuffer->getWidth();
1919 bufferHeight = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001920 } else {
David Sodman0cc69182017-11-17 12:12:07 -08001921 bufferHeight = p->getBE().compositionInfo.mBuffer->getWidth();
1922 bufferWidth = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001923 }
Marissa Wall61c58622018-07-18 10:12:20 -07001924 float sx = p->getActiveWidth(p->getDrawingState()) / static_cast<float>(bufferWidth);
1925 float sy = p->getActiveHeight(p->getDrawingState()) / static_cast<float>(bufferHeight);
Robert Carr9b429f42017-04-17 14:56:57 -07001926 Transform extraParentScaling;
1927 extraParentScaling.set(sx, 0, 0, sy);
1928 t = t * extraParentScaling;
1929 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001930 }
Marissa Wall61c58622018-07-18 10:12:20 -07001931 return t * getActiveTransform(getDrawingState());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001932}
1933
chaviw13fdc492017-06-27 12:40:18 -07001934half Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001935 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07001936
chaviw13fdc492017-06-27 12:40:18 -07001937 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1938 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001939}
Robert Carr6452f122017-03-21 10:41:29 -07001940
chaviw13fdc492017-06-27 12:40:18 -07001941half4 Layer::getColor() const {
1942 const half4 color(getDrawingState().color);
1943 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001944}
Robert Carr6452f122017-03-21 10:41:29 -07001945
Robert Carr1f0a16a2016-10-24 16:27:39 -07001946void Layer::commitChildList() {
1947 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1948 const auto& child = mCurrentChildren[i];
1949 child->commitChildList();
1950 }
1951 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001952 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001953}
1954
chaviw1d044282017-09-27 12:19:28 -07001955void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
1956 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1957 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1958 const State& state = useDrawing ? mDrawingState : mCurrentState;
1959
Marissa Wallf58c14b2018-07-24 10:50:43 -07001960 Transform requestedTransform = state.active_legacy.transform;
chaviw1d044282017-09-27 12:19:28 -07001961 Transform transform = getTransform();
1962
1963 layerInfo->set_id(sequence);
1964 layerInfo->set_name(getName().c_str());
1965 layerInfo->set_type(String8(getTypeId()));
1966
1967 for (const auto& child : children) {
1968 layerInfo->add_children(child->sequence);
1969 }
1970
1971 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1972 sp<Layer> strongRelative = weakRelative.promote();
1973 if (strongRelative != nullptr) {
1974 layerInfo->add_relatives(strongRelative->sequence);
1975 }
1976 }
1977
Marissa Wallf58c14b2018-07-24 10:50:43 -07001978 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
chaviw1d044282017-09-27 12:19:28 -07001979 layerInfo->mutable_transparent_region());
1980 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
1981 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
1982
1983 layerInfo->set_layer_stack(getLayerStack());
1984 layerInfo->set_z(state.z);
1985
1986 PositionProto* position = layerInfo->mutable_position();
1987 position->set_x(transform.tx());
1988 position->set_y(transform.ty());
1989
1990 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
1991 requestedPosition->set_x(requestedTransform.tx());
1992 requestedPosition->set_y(requestedTransform.ty());
1993
1994 SizeProto* size = layerInfo->mutable_size();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001995 size->set_w(state.active_legacy.w);
1996 size->set_h(state.active_legacy.h);
chaviw1d044282017-09-27 12:19:28 -07001997
Marissa Wallf58c14b2018-07-24 10:50:43 -07001998 LayerProtoHelper::writeToProto(state.crop_legacy, layerInfo->mutable_crop());
1999 LayerProtoHelper::writeToProto(state.finalCrop_legacy, layerInfo->mutable_final_crop());
chaviw1d044282017-09-27 12:19:28 -07002000
2001 layerInfo->set_is_opaque(isOpaque(state));
2002 layerInfo->set_invalidate(contentDirty);
Chia-I Wu01591c92018-05-22 12:03:00 -07002003
2004 // XXX (b/79210409) mCurrentDataSpace is not protected
2005 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
2006
chaviw1d044282017-09-27 12:19:28 -07002007 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
2008 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
2009 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
2010 layerInfo->set_flags(state.flags);
2011
2012 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
2013 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
2014
Jorim Jaggi8e0af362017-11-14 16:28:28 +01002015 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07002016 if (parent != nullptr) {
2017 layerInfo->set_parent(parent->sequence);
2018 }
2019
2020 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2021 if (zOrderRelativeOf != nullptr) {
2022 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
2023 }
2024
Chia-I Wu01591c92018-05-22 12:03:00 -07002025 // XXX getBE().compositionInfo.mBuffer is not protected
David Sodman0cc69182017-11-17 12:12:07 -08002026 auto buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08002027 if (buffer != nullptr) {
2028 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
Yichi Chen6ca35192018-05-29 12:20:43 +08002029 LayerProtoHelper::writeToProto(Transform(mCurrentTransform),
2030 layerInfo->mutable_buffer_transform());
chaviw1d044282017-09-27 12:19:28 -07002031 }
2032
2033 layerInfo->set_queued_frames(getQueuedFrameCount());
2034 layerInfo->set_refresh_pending(isBufferLatched());
rongliucfb187b2018-03-14 12:26:23 -07002035 layerInfo->set_window_type(state.type);
2036 layerInfo->set_app_id(state.appId);
chaviwadc40c22018-07-10 16:57:27 -07002037 layerInfo->set_curr_frame(mCurrentFrameNumber);
2038
2039 for (const auto& pendingState : mPendingStates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07002040 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
chaviwadc40c22018-07-10 16:57:27 -07002041 if (barrierLayer != nullptr) {
2042 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
2043 barrierLayerProto->set_id(barrierLayer->sequence);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002044 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
chaviwadc40c22018-07-10 16:57:27 -07002045 }
2046 }
chaviw1d044282017-09-27 12:19:28 -07002047}
2048
Dominik Laskowski7e045462018-05-30 13:02:02 -07002049void Layer::writeToProto(LayerProto* layerInfo, int32_t displayId) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07002050 if (!hasHwcLayer(displayId)) {
2051 return;
2052 }
2053
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002054 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
2055
Dominik Laskowski7e045462018-05-30 13:02:02 -07002056 const auto& hwcInfo = getBE().mHwcLayers.at(displayId);
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002057
2058 const Rect& frame = hwcInfo.displayFrame;
2059 LayerProtoHelper::writeToProto(frame, layerInfo->mutable_hwc_frame());
2060
2061 const FloatRect& crop = hwcInfo.sourceCrop;
2062 LayerProtoHelper::writeToProto(crop, layerInfo->mutable_hwc_crop());
2063
2064 const int32_t transform = static_cast<int32_t>(hwcInfo.transform);
2065 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002066
2067 const int32_t compositionType = static_cast<int32_t>(hwcInfo.compositionType);
2068 layerInfo->set_hwc_composition_type(compositionType);
2069
2070 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
2071 static_cast<BufferLayer*>(this)->isProtected()) {
2072 layerInfo->set_is_protected(true);
2073 } else {
2074 layerInfo->set_is_protected(false);
2075 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002076}
2077
Mathias Agopian13127d82013-03-05 17:47:11 -08002078// ---------------------------------------------------------------------------
2079
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002080}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002081
2082#if defined(__gl_h_)
2083#error "don't include gl/gl.h in this file"
2084#endif
2085
2086#if defined(__gl2_h_)
2087#error "don't include gl2/gl2.h in this file"
2088#endif