blob: fef53bb452c98693873479efda643c9b53b32a75 [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);
David Sodmanb8aaea12017-12-14 15:54:51 -0800233 auto layer = std::shared_ptr<HWC2::Layer>(
234 hwc->createLayer(displayId),
235 [hwc, displayId](HWC2::Layer* layer) {
236 hwc->destroyLayer(displayId, layer); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700237 if (!layer) {
238 return false;
239 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700240 LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers[displayId];
Steven Thomasb02664d2017-07-26 18:48:28 -0700241 hwcInfo.hwc = hwc;
242 hwcInfo.layer = layer;
David Sodmanf6a38932018-05-25 15:27:50 -0700243 layer->setLayerDestroyedListener(
Dominik Laskowski7e045462018-05-30 13:02:02 -0700244 [this, displayId](HWC2::Layer* /*layer*/) { getBE().mHwcLayers.erase(displayId); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700245 return true;
246}
247
Dominik Laskowski7e045462018-05-30 13:02:02 -0700248bool Layer::destroyHwcLayer(int32_t displayId) {
249 if (getBE().mHwcLayers.count(displayId) == 0) {
Chia-I Wu83806892017-11-16 10:50:20 -0800250 return false;
Steven Thomasb02664d2017-07-26 18:48:28 -0700251 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700252 auto& hwcInfo = getBE().mHwcLayers[displayId];
David Sodman41fdfc92017-11-06 16:09:56 -0800253 LOG_ALWAYS_FATAL_IF(hwcInfo.layer == nullptr, "Attempt to destroy null layer");
Steven Thomasb02664d2017-07-26 18:48:28 -0700254 LOG_ALWAYS_FATAL_IF(hwcInfo.hwc == nullptr, "Missing HWComposer");
David Sodmanb8aaea12017-12-14 15:54:51 -0800255 hwcInfo.layer = nullptr;
256
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 =
David Sodmanb8aaea12017-12-14 15:54:51 -0800719 (getBE().mHwcLayers[displayId].layer)->setCursorPosition(
720 position.left, position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800721 ALOGE_IF(error != HWC2::Error::None,
722 "[%s] Failed to set cursor position "
723 "to (%d, %d): %s (%d)",
724 mName.string(), position.left, position.top, to_string(error).c_str(),
725 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800726}
Riley Andrews03414a12014-07-01 14:22:59 -0700727
Mathias Agopian13127d82013-03-05 17:47:11 -0800728// ---------------------------------------------------------------------------
729// drawing...
730// ---------------------------------------------------------------------------
731
Marissa Wall61c58622018-07-18 10:12:20 -0700732void Layer::draw(const RenderArea& renderArea, const Region& clip) {
chaviwa76b2712017-09-20 12:02:26 -0700733 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800734}
735
Marissa Wall61c58622018-07-18 10:12:20 -0700736void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) {
chaviwa76b2712017-09-20 12:02:26 -0700737 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800738}
739
Marissa Wall61c58622018-07-18 10:12:20 -0700740void Layer::draw(const RenderArea& renderArea) {
chaviwa76b2712017-09-20 12:02:26 -0700741 onDraw(renderArea, Region(renderArea.getBounds()), false);
Dan Stozac7014012014-02-14 15:03:43 -0800742}
743
David Sodman41fdfc92017-11-06 16:09:56 -0800744void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
745 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800746 auto& engine(mFlinger->getRenderEngine());
David Sodman9eeae692017-11-02 10:53:32 -0700747 computeGeometry(renderArea, getBE().mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700748 engine.setupFillWithColor(red, green, blue, alpha);
David Sodman9eeae692017-11-02 10:53:32 -0700749 engine.drawMesh(getBE().mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800750}
751
chaviwa76b2712017-09-20 12:02:26 -0700752void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
David Sodman41fdfc92017-11-06 16:09:56 -0800753 clearWithOpenGL(renderArea, 0, 0, 0, 0);
Mathias Agopian13127d82013-03-05 17:47:11 -0800754}
755
Dominik Laskowski7e045462018-05-30 13:02:02 -0700756void Layer::setCompositionType(int32_t displayId, HWC2::Composition type, bool callIntoHwc) {
757 if (getBE().mHwcLayers.count(displayId) == 0) {
Chia-I Wu30505fb2018-03-26 16:20:31 -0700758 ALOGE("setCompositionType called without a valid HWC layer");
759 return;
760 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700761 auto& hwcInfo = getBE().mHwcLayers[displayId];
Chia-I Wu30505fb2018-03-26 16:20:31 -0700762 auto& hwcLayer = hwcInfo.layer;
David Sodmanb8aaea12017-12-14 15:54:51 -0800763 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", (hwcLayer)->getId(), to_string(type).c_str(),
Chia-I Wu30505fb2018-03-26 16:20:31 -0700764 static_cast<int>(callIntoHwc));
765 if (hwcInfo.compositionType != type) {
766 ALOGV(" actually setting");
767 hwcInfo.compositionType = type;
768 if (callIntoHwc) {
David Sodmanb8aaea12017-12-14 15:54:51 -0800769 auto error = (hwcLayer)->setCompositionType(type);
Chia-I Wu30505fb2018-03-26 16:20:31 -0700770 ALOGE_IF(error != HWC2::Error::None,
771 "[%s] Failed to set "
772 "composition type %s: %s (%d)",
773 mName.string(), to_string(type).c_str(), to_string(error).c_str(),
774 static_cast<int32_t>(error));
775 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800776 }
777}
778
Dominik Laskowski7e045462018-05-30 13:02:02 -0700779HWC2::Composition Layer::getCompositionType(int32_t displayId) const {
780 if (displayId == DisplayDevice::DISPLAY_ID_INVALID) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700781 // If we're querying the composition type for a display that does not
782 // have a HWC counterpart, then it will always be Client
783 return HWC2::Composition::Client;
784 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700785 if (getBE().mHwcLayers.count(displayId) == 0) {
Chia-I Wu30505fb2018-03-26 16:20:31 -0700786 ALOGE("getCompositionType called with an invalid HWC layer");
787 return HWC2::Composition::Invalid;
788 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700789 return getBE().mHwcLayers.at(displayId).compositionType;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800790}
791
Dominik Laskowski7e045462018-05-30 13:02:02 -0700792void Layer::setClearClientTarget(int32_t displayId, bool clear) {
793 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800794 ALOGE("setClearClientTarget called without a valid HWC layer");
795 return;
796 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700797 getBE().mHwcLayers[displayId].clearClientTarget = clear;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800798}
799
Dominik Laskowski7e045462018-05-30 13:02:02 -0700800bool Layer::getClearClientTarget(int32_t displayId) const {
801 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800802 ALOGE("getClearClientTarget called without a valid HWC layer");
803 return false;
804 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700805 return getBE().mHwcLayers.at(displayId).clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800806}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800807
Dan Stozacac35382016-01-27 12:21:06 -0800808bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
809 if (point->getFrameNumber() <= mCurrentFrameNumber) {
810 // Don't bother with a SyncPoint, since we've already latched the
811 // relevant frame
812 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700813 }
814
Dan Stozacac35382016-01-27 12:21:06 -0800815 Mutex::Autolock lock(mLocalSyncPointMutex);
816 mLocalSyncPoints.push_back(point);
817 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700818}
819
Mathias Agopian13127d82013-03-05 17:47:11 -0800820void Layer::setFiltering(bool filtering) {
821 mFiltering = filtering;
822}
823
824bool Layer::getFiltering() const {
825 return mFiltering;
826}
827
Mathias Agopian13127d82013-03-05 17:47:11 -0800828// ----------------------------------------------------------------------------
829// local state
830// ----------------------------------------------------------------------------
831
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000832static void boundPoint(vec2* point, const Rect& crop) {
833 if (point->x < crop.left) {
834 point->x = crop.left;
835 }
836 if (point->x > crop.right) {
837 point->x = crop.right;
838 }
839 if (point->y < crop.top) {
840 point->y = crop.top;
841 }
842 if (point->y > crop.bottom) {
843 point->y = crop.bottom;
844 }
845}
846
chaviwa76b2712017-09-20 12:02:26 -0700847void Layer::computeGeometry(const RenderArea& renderArea, Mesh& mesh,
848 bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700849 const Layer::State& s(getDrawingState());
chaviwa76b2712017-09-20 12:02:26 -0700850 const Transform renderAreaTransform(renderArea.getTransform());
851 const uint32_t height = renderArea.getHeight();
Dan Stoza80d61162017-12-20 15:57:52 -0800852 FloatRect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700853
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000854 vec2 lt = vec2(win.left, win.top);
855 vec2 lb = vec2(win.left, win.bottom);
856 vec2 rb = vec2(win.right, win.bottom);
857 vec2 rt = vec2(win.right, win.top);
858
Robert Carr1f0a16a2016-10-24 16:27:39 -0700859 Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000860 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700861 lt = layerTransform.transform(lt);
862 lb = layerTransform.transform(lb);
863 rb = layerTransform.transform(rb);
864 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000865 }
866
Marissa Wall61c58622018-07-18 10:12:20 -0700867 Rect finalCrop = getFinalCrop(s);
868 if (!finalCrop.isEmpty()) {
869 boundPoint(&lt, finalCrop);
870 boundPoint(&lb, finalCrop);
871 boundPoint(&rb, finalCrop);
872 boundPoint(&rt, finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000873 }
874
Mathias Agopianff2ed702013-09-01 21:36:12 -0700875 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700876 position[0] = renderAreaTransform.transform(lt);
877 position[1] = renderAreaTransform.transform(lb);
878 position[2] = renderAreaTransform.transform(rb);
879 position[3] = renderAreaTransform.transform(rt);
David Sodman41fdfc92017-11-06 16:09:56 -0800880 for (size_t i = 0; i < 4; i++) {
chaviwa76b2712017-09-20 12:02:26 -0700881 position[i].y = height - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -0800882 }
883}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800884
David Sodman41fdfc92017-11-06 16:09:56 -0800885bool Layer::isSecure() const {
Dan Stoza23116082015-06-18 14:58:39 -0700886 const Layer::State& s(mDrawingState);
887 return (s.flags & layer_state_t::eLayerSecure);
888}
889
Mathias Agopian13127d82013-03-05 17:47:11 -0800890void Layer::setVisibleRegion(const Region& visibleRegion) {
891 // always called from main thread
892 this->visibleRegion = visibleRegion;
893}
894
895void Layer::setCoveredRegion(const Region& coveredRegion) {
896 // always called from main thread
897 this->coveredRegion = coveredRegion;
898}
899
David Sodman41fdfc92017-11-06 16:09:56 -0800900void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800901 // always called from main thread
902 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
903}
904
Robert Carre5f4f692018-01-12 13:12:28 -0800905void Layer::clearVisibilityRegions() {
906 visibleRegion.clear();
907 visibleNonTransparentRegion.clear();
908 coveredRegion.clear();
909}
910
Mathias Agopian13127d82013-03-05 17:47:11 -0800911// ----------------------------------------------------------------------------
912// transaction
913// ----------------------------------------------------------------------------
914
Dan Stoza7dde5992015-05-22 09:51:44 -0700915void Layer::pushPendingState() {
916 if (!mCurrentState.modified) {
917 return;
918 }
919
Dan Stoza7dde5992015-05-22 09:51:44 -0700920 // If this transaction is waiting on the receipt of a frame, generate a sync
921 // point and send it to the remote layer.
Marissa Wallf58c14b2018-07-24 10:50:43 -0700922 if (mCurrentState.barrierLayer_legacy != nullptr) {
923 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800924 if (barrierLayer == nullptr) {
925 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700926 // If we can't promote the layer we are intended to wait on,
927 // then it is expired or otherwise invalid. Allow this transaction
928 // to be applied as per normal (no synchronization).
Marissa Wallf58c14b2018-07-24 10:50:43 -0700929 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800930 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700931 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy);
Robert Carr0d480722017-01-10 16:42:54 -0800932 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800933 mRemoteSyncPoints.push_back(std::move(syncPoint));
934 } else {
935 // We already missed the frame we're supposed to synchronize
936 // on, so go ahead and apply the state update
Marissa Wallf58c14b2018-07-24 10:50:43 -0700937 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800938 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700939 }
940
Dan Stoza7dde5992015-05-22 09:51:44 -0700941 // Wake us up to check if the frame has been received
942 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700943 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700944 }
945 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700946 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700947}
948
Pablo Ceballos05289c22016-04-14 15:49:55 -0700949void Layer::popPendingState(State* stateToCommit) {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700950 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700951
952 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700953 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700954}
955
Pablo Ceballos05289c22016-04-14 15:49:55 -0700956bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700957 bool stateUpdateAvailable = false;
958 while (!mPendingStates.empty()) {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700959 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700960 if (mRemoteSyncPoints.empty()) {
961 // If we don't have a sync point for this, apply it anyway. It
962 // will be visually wrong, but it should keep us from getting
963 // into too much trouble.
964 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700965 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700966 stateUpdateAvailable = true;
967 continue;
968 }
969
Marissa Wallf58c14b2018-07-24 10:50:43 -0700970 if (mRemoteSyncPoints.front()->getFrameNumber() !=
971 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800972 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800973
974 // Signal our end of the sync point and then dispose of it
975 mRemoteSyncPoints.front()->setTransactionApplied();
976 mRemoteSyncPoints.pop_front();
977 continue;
978 }
979
Dan Stoza7dde5992015-05-22 09:51:44 -0700980 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
981 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700982 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700983 stateUpdateAvailable = true;
984
985 // Signal our end of the sync point and then dispose of it
986 mRemoteSyncPoints.front()->setTransactionApplied();
987 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800988 } else {
989 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700990 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700991 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700992 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700993 stateUpdateAvailable = true;
994 }
995 }
996
997 // If we still have pending updates, wake SurfaceFlinger back up and point
998 // it at this layer so we can process them
999 if (!mPendingStates.empty()) {
1000 setTransactionFlags(eTransactionNeeded);
1001 mFlinger->setTransactionFlags(eTraversalNeeded);
1002 }
1003
1004 mCurrentState.modified = false;
1005 return stateUpdateAvailable;
1006}
1007
Marissa Wall61c58622018-07-18 10:12:20 -07001008uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001009 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001010
Marissa Wall61c58622018-07-18 10:12:20 -07001011 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
1012 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -07001013
David Sodmaneb085e02017-10-05 18:49:04 -07001014 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001015 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +00001016 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -08001017 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
1018 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1019 " requested={ wh={%4u,%4u} }}\n"
1020 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1021 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -07001022 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -07001023 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
1024 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
1025 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
1026 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
1027 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -07001028 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
1029 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
1030 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001031
Jamie Gennis2a0d5b62011-09-26 16:54:44 -07001032 // record the new size, form this point on, when the client request
1033 // a buffer, it'll get the new size.
Marissa Wall61c58622018-07-18 10:12:20 -07001034 setDefaultBufferSize(stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001035 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001036
Robert Carre392b552017-09-19 12:16:05 -07001037 // Don't let Layer::doTransaction update the drawing state
1038 // if we have a pending resize, unless we are in fixed-size mode.
1039 // the drawing state will be updated only once we receive a buffer
1040 // with the correct size.
1041 //
1042 // In particular, we want to make sure the clip (which is part
1043 // of the geometry state) is latched together with the size but is
1044 // latched immediately when no resizing is involved.
1045 //
1046 // If a sideband stream is attached, however, we want to skip this
1047 // optimization so that transactions aren't missed when a buffer
1048 // never arrives
1049 //
1050 // In the case that we don't have a buffer we ignore other factors
1051 // and avoid entering the resizePending state. At a high level the
1052 // resizePending state is to avoid applying the state of the new buffer
1053 // to the old buffer. However in the state where we don't have an old buffer
1054 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -07001055 const bool resizePending =
1056 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
1057 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
David Sodman0cc69182017-11-17 12:12:07 -08001058 (getBE().compositionInfo.mBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001059 if (!isFixedSize()) {
David Sodman0cc69182017-11-17 12:12:07 -08001060 if (resizePending && getBE().compositionInfo.hwc.sidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001061 flags |= eDontUpdateGeometryState;
1062 }
1063 }
1064
Robert Carr7bf247e2017-05-18 14:02:49 -07001065 // Here we apply various requested geometry states, depending on our
1066 // latching configuration. See Layer.h for a detailed discussion of
1067 // how geometry latching is controlled.
1068 if (!(flags & eDontUpdateGeometryState)) {
Pablo Ceballos7d052572016-06-02 17:46:05 -07001069 Layer::State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001070
1071 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1072 // mode, which causes attributes which normally latch regardless of scaling mode,
1073 // to be delayed. We copy the requested state to the active state making sure
1074 // to respect these rules (again see Layer.h for a detailed discussion).
1075 //
1076 // There is an awkward asymmetry in the handling of the crop states in the position
1077 // states, as can be seen below. Largely this arises from position and transform
1078 // being stored in the same data structure while having different latching rules.
1079 // b/38182305
1080 //
Marissa Wall61c58622018-07-18 10:12:20 -07001081 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -07001082 // applyPendingStates in the presence of deferred transactions.
1083 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -07001084 float tx = stateToCommit->active_legacy.transform.tx();
1085 float ty = stateToCommit->active_legacy.transform.ty();
1086 stateToCommit->active_legacy = stateToCommit->requested_legacy;
1087 stateToCommit->active_legacy.transform.set(tx, ty);
1088 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001089 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001090 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -07001091 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001092 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001093 }
1094
Marissa Wall61c58622018-07-18 10:12:20 -07001095 return flags;
1096}
1097
1098uint32_t Layer::doTransaction(uint32_t flags) {
1099 ATRACE_CALL();
1100
1101 pushPendingState();
1102 Layer::State c = getCurrentState();
1103 if (!applyPendingStates(&c)) {
1104 return 0;
1105 }
1106
1107 flags = doTransactionResize(flags, &c);
1108
1109 const Layer::State& s(getDrawingState());
1110
1111 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001112 // invalidate and recompute the visible regions if needed
1113 flags |= Layer::eVisibleRegion;
1114 }
1115
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001116 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001117 // invalidate and recompute the visible regions if needed
1118 flags |= eVisibleRegion;
1119 this->contentDirty = true;
1120
1121 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -07001122 const uint8_t type = getActiveTransform(c).getType();
1123 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || (type >= Transform::SCALE));
Mathias Agopian13127d82013-03-05 17:47:11 -08001124 }
1125
Dan Stozac8145172016-04-28 16:29:06 -07001126 // If the layer is hidden, signal and clear out all local sync points so
1127 // that transactions for layers depending on this layer's frames becoming
1128 // visible are not blocked
1129 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001130 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001131 }
1132
Mathias Agopian13127d82013-03-05 17:47:11 -08001133 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001134 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001135 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001136}
1137
Pablo Ceballos05289c22016-04-14 15:49:55 -07001138void Layer::commitTransaction(const State& stateToCommit) {
1139 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001140}
1141
Mathias Agopian13127d82013-03-05 17:47:11 -08001142uint32_t Layer::getTransactionFlags(uint32_t flags) {
1143 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1144}
1145
1146uint32_t Layer::setTransactionFlags(uint32_t flags) {
1147 return android_atomic_or(flags, &mTransactionFlags);
1148}
1149
Robert Carr82364e32016-05-15 11:27:47 -07001150bool Layer::setPosition(float x, float y, bool immediate) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001151 if (mCurrentState.requested_legacy.transform.tx() == x &&
1152 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001153 return false;
1154 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001155
1156 // We update the requested and active position simultaneously because
1157 // we want to apply the position portion of the transform matrix immediately,
1158 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Marissa Wallf58c14b2018-07-24 10:50:43 -07001159 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001160 if (immediate && !mFreezeGeometryUpdates) {
1161 // Here we directly update the active state
1162 // unlike other setters, because we store it within
1163 // the transform, but use different latching rules.
1164 // b/38182305
Marissa Wallf58c14b2018-07-24 10:50:43 -07001165 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001166 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001167 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001168
Dan Stoza7dde5992015-05-22 09:51:44 -07001169 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001170 setTransactionFlags(eTransactionNeeded);
1171 return true;
1172}
Robert Carr82364e32016-05-15 11:27:47 -07001173
Robert Carr1f0a16a2016-10-24 16:27:39 -07001174bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1175 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1176 if (idx < 0) {
1177 return false;
1178 }
1179 if (childLayer->setLayer(z)) {
1180 mCurrentChildren.removeAt(idx);
1181 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001182 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001183 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001184 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001185}
1186
Robert Carr503c7042017-09-27 15:06:08 -07001187bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1188 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1189 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1190 if (idx < 0) {
1191 return false;
1192 }
1193 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1194 mCurrentChildren.removeAt(idx);
1195 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001196 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001197 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001198 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001199}
1200
Robert Carrae060832016-11-28 10:51:00 -08001201bool Layer::setLayer(int32_t z) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001202 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001203 mCurrentState.sequence++;
1204 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001205 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001206
1207 // Discard all relative layering.
1208 if (mCurrentState.zOrderRelativeOf != nullptr) {
1209 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1210 if (strongRelative != nullptr) {
1211 strongRelative->removeZOrderRelative(this);
1212 }
1213 mCurrentState.zOrderRelativeOf = nullptr;
1214 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001215 setTransactionFlags(eTransactionNeeded);
1216 return true;
1217}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001218
Robert Carrdb66e622017-04-10 16:55:57 -07001219void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1220 mCurrentState.zOrderRelatives.remove(relative);
1221 mCurrentState.sequence++;
1222 mCurrentState.modified = true;
1223 setTransactionFlags(eTransactionNeeded);
1224}
1225
1226void Layer::addZOrderRelative(const wp<Layer>& relative) {
1227 mCurrentState.zOrderRelatives.add(relative);
1228 mCurrentState.modified = true;
1229 mCurrentState.sequence++;
1230 setTransactionFlags(eTransactionNeeded);
1231}
1232
Robert Carr503d2bd2017-12-04 15:49:47 -08001233bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001234 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1235 if (handle == nullptr) {
1236 return false;
1237 }
1238 sp<Layer> relative = handle->owner.promote();
1239 if (relative == nullptr) {
1240 return false;
1241 }
1242
Robert Carr503d2bd2017-12-04 15:49:47 -08001243 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1244 mCurrentState.zOrderRelativeOf == relative) {
1245 return false;
1246 }
1247
Robert Carrdb66e622017-04-10 16:55:57 -07001248 mCurrentState.sequence++;
1249 mCurrentState.modified = true;
Robert Carr503d2bd2017-12-04 15:49:47 -08001250 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001251
chaviw9ab4bd12017-11-03 13:11:00 -07001252 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
1253 if (oldZOrderRelativeOf != nullptr) {
1254 oldZOrderRelativeOf->removeZOrderRelative(this);
1255 }
Robert Carrdb66e622017-04-10 16:55:57 -07001256 mCurrentState.zOrderRelativeOf = relative;
1257 relative->addZOrderRelative(this);
1258
1259 setTransactionFlags(eTransactionNeeded);
1260
1261 return true;
1262}
1263
Mathias Agopian13127d82013-03-05 17:47:11 -08001264bool Layer::setSize(uint32_t w, uint32_t h) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001265 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
1266 return false;
1267 mCurrentState.requested_legacy.w = w;
1268 mCurrentState.requested_legacy.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001269 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001270 setTransactionFlags(eTransactionNeeded);
1271 return true;
1272}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001273bool Layer::setAlpha(float alpha) {
David Sodman41fdfc92017-11-06 16:09:56 -08001274 if (mCurrentState.color.a == alpha) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001275 mCurrentState.sequence++;
chaviw13fdc492017-06-27 12:40:18 -07001276 mCurrentState.color.a = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001277 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001278 setTransactionFlags(eTransactionNeeded);
1279 return true;
1280}
chaviw13fdc492017-06-27 12:40:18 -07001281
1282bool Layer::setColor(const half3& color) {
David Sodman41fdfc92017-11-06 16:09:56 -08001283 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g &&
1284 color.b == mCurrentState.color.b)
chaviw13fdc492017-06-27 12:40:18 -07001285 return false;
1286
1287 mCurrentState.sequence++;
1288 mCurrentState.color.r = color.r;
1289 mCurrentState.color.g = color.g;
1290 mCurrentState.color.b = color.b;
1291 mCurrentState.modified = true;
1292 setTransactionFlags(eTransactionNeeded);
1293 return true;
1294}
1295
Robert Carrd4ae7f32018-06-07 16:10:57 -07001296bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1297 bool allowNonRectPreservingTransforms) {
1298 Transform t;
1299 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1300
1301 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1302 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1303 return false;
1304 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001305 mCurrentState.sequence++;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001306 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1307 matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001308 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001309 setTransactionFlags(eTransactionNeeded);
1310 return true;
1311}
Marissa Wall61c58622018-07-18 10:12:20 -07001312
Mathias Agopian13127d82013-03-05 17:47:11 -08001313bool Layer::setTransparentRegionHint(const Region& transparent) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001314 mCurrentState.requestedTransparentRegion_legacy = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001315 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001316 setTransactionFlags(eTransactionNeeded);
1317 return true;
1318}
1319bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1320 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
David Sodman41fdfc92017-11-06 16:09:56 -08001321 if (mCurrentState.flags == newFlags) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001322 mCurrentState.sequence++;
1323 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001324 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001325 setTransactionFlags(eTransactionNeeded);
1326 return true;
1327}
Robert Carr99e27f02016-06-16 15:18:02 -07001328
Marissa Wallf58c14b2018-07-24 10:50:43 -07001329bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
1330 if (mCurrentState.requestedCrop_legacy == crop) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001331 mCurrentState.sequence++;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001332 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001333 if (immediate && !mFreezeGeometryUpdates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001334 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001335 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001336 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1337
Dan Stoza7dde5992015-05-22 09:51:44 -07001338 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001339 setTransactionFlags(eTransactionNeeded);
1340 return true;
1341}
Robert Carr8d5227b2017-03-16 15:41:03 -07001342
Marissa Wallf58c14b2018-07-24 10:50:43 -07001343bool Layer::setFinalCrop_legacy(const Rect& crop, bool immediate) {
1344 if (mCurrentState.requestedFinalCrop_legacy == crop) return false;
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001345 mCurrentState.sequence++;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001346 mCurrentState.requestedFinalCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001347 if (immediate && !mFreezeGeometryUpdates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001348 mCurrentState.finalCrop_legacy = crop;
Robert Carr8d5227b2017-03-16 15:41:03 -07001349 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001350 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1351
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001352 mCurrentState.modified = true;
1353 setTransactionFlags(eTransactionNeeded);
1354 return true;
1355}
Mathias Agopian13127d82013-03-05 17:47:11 -08001356
Robert Carrc3574f72016-03-24 12:19:32 -07001357bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001358 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001359 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001360 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001361 return true;
1362}
1363
rongliucfb187b2018-03-14 12:26:23 -07001364void Layer::setInfo(int32_t type, int32_t appId) {
David Sodman41fdfc92017-11-06 16:09:56 -08001365 mCurrentState.appId = appId;
1366 mCurrentState.type = type;
1367 mCurrentState.modified = true;
1368 setTransactionFlags(eTransactionNeeded);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001369}
1370
Mathias Agopian13127d82013-03-05 17:47:11 -08001371bool Layer::setLayerStack(uint32_t layerStack) {
David Sodman41fdfc92017-11-06 16:09:56 -08001372 if (mCurrentState.layerStack == layerStack) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001373 mCurrentState.sequence++;
1374 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001375 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001376 setTransactionFlags(eTransactionNeeded);
1377 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001378}
1379
Robert Carr1f0a16a2016-10-24 16:27:39 -07001380uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001381 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001382 if (p == nullptr) {
1383 return getDrawingState().layerStack;
1384 }
1385 return p->getLayerStack();
1386}
1387
Marissa Wallf58c14b2018-07-24 10:50:43 -07001388void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
1389 mCurrentState.barrierLayer_legacy = barrierLayer;
1390 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001391 // We don't set eTransactionNeeded, because just receiving a deferral
1392 // request without any other state updates shouldn't actually induce a delay
1393 mCurrentState.modified = true;
1394 pushPendingState();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001395 mCurrentState.barrierLayer_legacy = nullptr;
1396 mCurrentState.frameNumber_legacy = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001397 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001398}
1399
Marissa Wallf58c14b2018-07-24 10:50:43 -07001400void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001401 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001402 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001403}
1404
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001405// ----------------------------------------------------------------------------
1406// pageflip handling...
1407// ----------------------------------------------------------------------------
1408
Robert Carr1f0a16a2016-10-24 16:27:39 -07001409bool Layer::isHiddenByPolicy() const {
1410 const Layer::State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001411 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001412 if (parent != nullptr && parent->isHiddenByPolicy()) {
1413 return true;
1414 }
1415 return s.flags & layer_state_t::eLayerHidden;
1416}
1417
David Sodman41fdfc92017-11-06 16:09:56 -08001418uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001419 // TODO: should we do something special if mSecure is set?
1420 if (mProtectedByApp) {
1421 // need a hardware-protected path to external video sink
1422 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001423 }
Riley Andrews03414a12014-07-01 14:22:59 -07001424 if (mPotentialCursor) {
1425 usage |= GraphicBuffer::USAGE_CURSOR;
1426 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001427 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001428 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001429}
1430
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001431void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001432 uint32_t orientation = 0;
1433 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001434 // The transform hint is used to improve performance, but we can
1435 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001436 // apply to all displays.
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001437 const Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001438 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07001439 if (orientation & Transform::ROT_INVALID) {
1440 orientation = 0;
1441 }
1442 }
David Sodmaneb085e02017-10-05 18:49:04 -07001443 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001444}
1445
Mathias Agopian13127d82013-03-05 17:47:11 -08001446// ----------------------------------------------------------------------------
1447// debugging
1448// ----------------------------------------------------------------------------
1449
Marissa Wall61c58622018-07-18 10:12:20 -07001450// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001451LayerDebugInfo Layer::getLayerDebugInfo() const {
1452 LayerDebugInfo info;
1453 const Layer::State& ds = getDrawingState();
1454 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001455 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001456 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
1457 info.mType = String8(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001458 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001459 info.mVisibleRegion = visibleRegion;
1460 info.mSurfaceDamageRegion = surfaceDamageRegion;
1461 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001462 info.mX = ds.active_legacy.transform.tx();
1463 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001464 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001465 info.mWidth = ds.active_legacy.w;
1466 info.mHeight = ds.active_legacy.h;
1467 info.mCrop = ds.crop_legacy;
1468 info.mFinalCrop = ds.finalCrop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001469 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001470 info.mFlags = ds.flags;
1471 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001472 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001473 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1474 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1475 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1476 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001477 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001478 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001479 if (buffer != 0) {
1480 info.mActiveBufferWidth = buffer->getWidth();
1481 info.mActiveBufferHeight = buffer->getHeight();
1482 info.mActiveBufferStride = buffer->getStride();
1483 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001484 } else {
1485 info.mActiveBufferWidth = 0;
1486 info.mActiveBufferHeight = 0;
1487 info.mActiveBufferStride = 0;
1488 info.mActiveBufferFormat = 0;
1489 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001490 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001491 info.mNumQueuedFrames = getQueuedFrameCount();
1492 info.mRefreshPending = isBufferLatched();
1493 info.mIsOpaque = isOpaque(ds);
1494 info.mContentDirty = contentDirty;
1495 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001496}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001497
Dan Stozae22aec72016-08-01 13:20:59 -07001498void Layer::miniDumpHeader(String8& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001499 result.append("-------------------------------");
1500 result.append("-------------------------------");
1501 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001502 result.append(" Layer name\n");
1503 result.append(" Z | ");
1504 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001505 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001506 result.append(" Disp Frame (LTRB) | ");
1507 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001508 result.append("-------------------------------");
1509 result.append("-------------------------------");
1510 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001511}
1512
Dominik Laskowski7e045462018-05-30 13:02:02 -07001513void Layer::miniDump(String8& result, int32_t displayId) const {
1514 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stozae22aec72016-08-01 13:20:59 -07001515 return;
1516 }
1517
1518 String8 name;
1519 if (mName.length() > 77) {
1520 std::string shortened;
1521 shortened.append(mName.string(), 36);
1522 shortened.append("[...]");
1523 shortened.append(mName.string() + (mName.length() - 36), 36);
1524 name = shortened.c_str();
1525 } else {
1526 name = mName;
1527 }
1528
1529 result.appendFormat(" %s\n", name.string());
1530
1531 const Layer::State& layerState(getDrawingState());
Lloyd Pique074e8122018-07-26 12:57:23 -07001532 const LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers.at(displayId);
Chia-I Wu1e043612018-03-01 09:45:09 -08001533 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
1534 result.appendFormat(" rel %6d | ", layerState.z);
1535 } else {
1536 result.appendFormat(" %10d | ", layerState.z);
1537 }
Dominik Laskowski7e045462018-05-30 13:02:02 -07001538 result.appendFormat("%10s | ", to_string(getCompositionType(displayId)).c_str());
Lloyd Pique074e8122018-07-26 12:57:23 -07001539 result.appendFormat("%10s | ", to_string(hwcInfo.transform).c_str());
1540 const Rect& frame = hwcInfo.displayFrame;
David Sodman41fdfc92017-11-06 16:09:56 -08001541 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique074e8122018-07-26 12:57:23 -07001542 const FloatRect& crop = hwcInfo.sourceCrop;
David Sodman41fdfc92017-11-06 16:09:56 -08001543 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 -07001544
Yichi Chen6ca35192018-05-29 12:20:43 +08001545 result.append("- - - - - - - - - - - - - - - -");
1546 result.append("- - - - - - - - - - - - - - - -");
1547 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001548}
Dan Stozae22aec72016-08-01 13:20:59 -07001549
Svetoslavd85084b2014-03-20 10:28:31 -07001550void Layer::dumpFrameStats(String8& result) const {
1551 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001552}
1553
Svetoslavd85084b2014-03-20 10:28:31 -07001554void Layer::clearFrameStats() {
1555 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001556}
1557
Jamie Gennis6547ff42013-07-16 20:12:42 -07001558void Layer::logFrameStats() {
1559 mFrameTracker.logAndResetStats(mName);
1560}
1561
Svetoslavd85084b2014-03-20 10:28:31 -07001562void Layer::getFrameStats(FrameStats* outStats) const {
1563 mFrameTracker.getStats(outStats);
1564}
1565
Brian Andersond6927fb2016-07-23 23:37:30 -07001566void Layer::dumpFrameEvents(String8& result) {
David Sodman41fdfc92017-11-06 16:09:56 -08001567 result.appendFormat("- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001568 Mutex::Autolock lock(mFrameEventHistoryMutex);
1569 mFrameEventHistory.checkFencesForCompletion();
1570 mFrameEventHistory.dump(result);
1571}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001572
Brian Anderson5ea5e592016-12-01 16:54:33 -08001573void Layer::onDisconnect() {
1574 Mutex::Autolock lock(mFrameEventHistoryMutex);
1575 mFrameEventHistory.onDisconnect();
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001576 mTimeStats.onDisconnect(getName().c_str());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001577}
1578
Brian Anderson3890c392016-07-25 12:48:08 -07001579void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001580 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001581 if (newTimestamps) {
1582 mTimeStats.setPostTime(getName().c_str(), newTimestamps->frameNumber,
1583 newTimestamps->postedTime);
1584 }
1585
Brian Andersond6927fb2016-07-23 23:37:30 -07001586 Mutex::Autolock lock(mFrameEventHistoryMutex);
1587 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001588 // If there are any unsignaled fences in the aquire timeline at this
1589 // point, the previously queued frame hasn't been latched yet. Go ahead
1590 // and try to get the signal time here so the syscall is taken out of
1591 // the main thread's critical path.
1592 mAcquireTimeline.updateSignalTimes();
1593 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001594 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001595 mFrameEventHistory.addQueue(*newTimestamps);
1596 }
1597
Brian Anderson3890c392016-07-25 12:48:08 -07001598 if (outDelta) {
1599 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001600 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001601}
Dan Stozae77c7662016-05-13 11:37:28 -07001602
Chia-I Wu98f1c102017-05-30 14:54:08 -07001603size_t Layer::getChildrenCount() const {
1604 size_t count = 0;
1605 for (const sp<Layer>& child : mCurrentChildren) {
1606 count += 1 + child->getChildrenCount();
1607 }
1608 return count;
1609}
1610
Robert Carr1f0a16a2016-10-24 16:27:39 -07001611void Layer::addChild(const sp<Layer>& layer) {
1612 mCurrentChildren.add(layer);
1613 layer->setParent(this);
1614}
1615
1616ssize_t Layer::removeChild(const sp<Layer>& layer) {
1617 layer->setParent(nullptr);
1618 return mCurrentChildren.remove(layer);
1619}
1620
Robert Carr1db73f62016-12-21 12:58:51 -08001621bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1622 sp<Handle> handle = nullptr;
1623 sp<Layer> newParent = nullptr;
1624 if (newParentHandle == nullptr) {
1625 return false;
1626 }
1627 handle = static_cast<Handle*>(newParentHandle.get());
1628 newParent = handle->owner.promote();
1629 if (newParent == nullptr) {
1630 ALOGE("Unable to promote Layer handle");
1631 return false;
1632 }
1633
1634 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001635 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001636
1637 sp<Client> client(child->mClientRef.promote());
1638 if (client != nullptr) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001639 client->updateParent(newParent);
Robert Carr1db73f62016-12-21 12:58:51 -08001640 }
1641 }
1642 mCurrentChildren.clear();
1643
1644 return true;
1645}
1646
Robert Carr15eae092018-03-23 13:43:53 -07001647void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001648 for (const sp<Layer>& child : mDrawingChildren) {
1649 child->mDrawingParent = newParent;
1650 }
1651}
1652
chaviwf1961f72017-09-18 16:41:07 -07001653bool Layer::reparent(const sp<IBinder>& newParentHandle) {
1654 if (newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001655 return false;
1656 }
1657
1658 auto handle = static_cast<Handle*>(newParentHandle.get());
1659 sp<Layer> newParent = handle->owner.promote();
1660 if (newParent == nullptr) {
1661 ALOGE("Unable to promote Layer handle");
1662 return false;
1663 }
1664
chaviwf1961f72017-09-18 16:41:07 -07001665 sp<Layer> parent = getParent();
1666 if (parent != nullptr) {
1667 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001668 }
chaviwf1961f72017-09-18 16:41:07 -07001669 newParent->addChild(this);
chaviw06178942017-07-27 10:25:59 -07001670
chaviwf1961f72017-09-18 16:41:07 -07001671 sp<Client> client(mClientRef.promote());
chaviw06178942017-07-27 10:25:59 -07001672 sp<Client> newParentClient(newParent->mClientRef.promote());
1673
chaviwf1961f72017-09-18 16:41:07 -07001674 if (client != newParentClient) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001675 client->updateParent(newParent);
chaviw06178942017-07-27 10:25:59 -07001676 }
1677
chaviw06178942017-07-27 10:25:59 -07001678 return true;
1679}
1680
Robert Carr9524cb32017-02-13 11:32:32 -08001681bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001682 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001683 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001684 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001685 if (client != nullptr && parentClient != client) {
Robert Carr7f619b22017-11-06 12:56:35 -08001686 client->detachLayer(child.get());
1687 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001688 }
Robert Carr7f619b22017-11-06 12:56:35 -08001689 }
Robert Carr9524cb32017-02-13 11:32:32 -08001690
1691 return true;
1692}
1693
Chia-I Wu11481472018-05-04 10:43:19 -07001694bool Layer::isLegacyDataSpace() const {
1695 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001696 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001697 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001698}
1699
Robert Carr1f0a16a2016-10-24 16:27:39 -07001700void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001701 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001702}
1703
1704void Layer::clearSyncPoints() {
1705 for (const auto& child : mCurrentChildren) {
1706 child->clearSyncPoints();
1707 }
1708
1709 Mutex::Autolock lock(mLocalSyncPointMutex);
1710 for (auto& point : mLocalSyncPoints) {
1711 point->setFrameAvailable();
1712 }
1713 mLocalSyncPoints.clear();
1714}
1715
1716int32_t Layer::getZ() const {
1717 return mDrawingState.z;
1718}
1719
Robert Carr29abff82017-12-04 13:51:20 -08001720bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1721 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1722 const State& state = useDrawing ? mDrawingState : mCurrentState;
1723 return state.zOrderRelativeOf != nullptr;
1724}
1725
David Sodman41fdfc92017-11-06 16:09:56 -08001726__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001727 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001728 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1729 "makeTraversalList received invalid stateSet");
1730 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1731 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1732 const State& state = useDrawing ? mDrawingState : mCurrentState;
1733
Robert Carr29abff82017-12-04 13:51:20 -08001734 if (state.zOrderRelatives.size() == 0) {
1735 *outSkipRelativeZUsers = true;
1736 return children;
1737 }
1738
chaviwfd462612018-05-31 16:11:27 -07001739 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001740 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001741 sp<Layer> strongRelative = weakRelative.promote();
1742 if (strongRelative != nullptr) {
1743 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001744 }
1745 }
1746
Dan Stoza412903f2017-04-27 13:42:17 -07001747 for (const sp<Layer>& child : children) {
Robert Carr503c7042017-09-27 15:06:08 -07001748 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1749 if (childState.zOrderRelativeOf != nullptr) {
1750 continue;
1751 }
Robert Carrdb66e622017-04-10 16:55:57 -07001752 traverse.add(child);
1753 }
1754
1755 return traverse;
1756}
1757
Robert Carr1f0a16a2016-10-24 16:27:39 -07001758/**
Robert Carrdb66e622017-04-10 16:55:57 -07001759 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001760 */
Dan Stoza412903f2017-04-27 13:42:17 -07001761void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001762 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1763 // produce a new list for traversing, including our relatives, and not including our children
1764 // who are relatives of another surface. In the case that there are no relative Z,
1765 // makeTraversalList returns our children directly to avoid significant overhead.
1766 // However in this case we need to take the responsibility for filtering children which
1767 // are relatives of another surface here.
1768 bool skipRelativeZUsers = false;
1769 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001770
Robert Carr1f0a16a2016-10-24 16:27:39 -07001771 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001772 for (; i < list.size(); i++) {
1773 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001774 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1775 continue;
1776 }
1777
Robert Carrdb66e622017-04-10 16:55:57 -07001778 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001779 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001780 }
Dan Stoza412903f2017-04-27 13:42:17 -07001781 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001782 }
Robert Carr29abff82017-12-04 13:51:20 -08001783
Dan Stoza412903f2017-04-27 13:42:17 -07001784 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001785 for (; i < list.size(); i++) {
1786 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001787
1788 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1789 continue;
1790 }
Dan Stoza412903f2017-04-27 13:42:17 -07001791 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001792 }
1793}
1794
1795/**
Robert Carrdb66e622017-04-10 16:55:57 -07001796 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001797 */
Dan Stoza412903f2017-04-27 13:42:17 -07001798void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1799 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001800 // See traverseInZOrder for documentation.
1801 bool skipRelativeZUsers = false;
1802 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001803
Robert Carr1f0a16a2016-10-24 16:27:39 -07001804 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001805 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001806 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001807
1808 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1809 continue;
1810 }
1811
Robert Carrdb66e622017-04-10 16:55:57 -07001812 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001813 break;
1814 }
Dan Stoza412903f2017-04-27 13:42:17 -07001815 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001816 }
Dan Stoza412903f2017-04-27 13:42:17 -07001817 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001818 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001819 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001820
1821 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1822 continue;
1823 }
1824
Dan Stoza412903f2017-04-27 13:42:17 -07001825 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001826 }
1827}
1828
chaviw4b129c22018-04-09 16:19:43 -07001829LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1830 const std::vector<Layer*>& layersInTree) {
1831 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1832 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001833 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1834 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
chaviw4b129c22018-04-09 16:19:43 -07001835 const State& state = useDrawing ? mDrawingState : mCurrentState;
1836
chaviwfd462612018-05-31 16:11:27 -07001837 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001838 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1839 sp<Layer> strongRelative = weakRelative.promote();
1840 // Only add relative layers that are also descendents of the top most parent of the tree.
1841 // If a relative layer is not a descendent, then it should be ignored.
1842 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1843 traverse.add(strongRelative);
1844 }
1845 }
1846
1847 for (const sp<Layer>& child : children) {
1848 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1849 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1850 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1851 // the child here since it won't be added later as a relative.
1852 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1853 childState.zOrderRelativeOf.promote().get())) {
1854 continue;
1855 }
1856 traverse.add(child);
1857 }
1858
1859 return traverse;
1860}
1861
1862void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1863 LayerVector::StateSet stateSet,
1864 const LayerVector::Visitor& visitor) {
1865 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001866
1867 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001868 for (; i < list.size(); i++) {
1869 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001870 if (relative->getZ() >= 0) {
1871 break;
1872 }
chaviw4b129c22018-04-09 16:19:43 -07001873 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001874 }
chaviw4b129c22018-04-09 16:19:43 -07001875
chaviwa76b2712017-09-20 12:02:26 -07001876 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001877 for (; i < list.size(); i++) {
1878 const auto& relative = list[i];
1879 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001880 }
1881}
1882
chaviw4b129c22018-04-09 16:19:43 -07001883std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1884 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1885 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1886
1887 std::vector<Layer*> layersInTree = {this};
1888 for (size_t i = 0; i < children.size(); i++) {
1889 const auto& child = children[i];
1890 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1891 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1892 }
1893
1894 return layersInTree;
1895}
1896
1897void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1898 const LayerVector::Visitor& visitor) {
1899 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1900 std::sort(layersInTree.begin(), layersInTree.end());
1901 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1902}
1903
Robert Carr1f0a16a2016-10-24 16:27:39 -07001904Transform Layer::getTransform() const {
1905 Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001906 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001907 if (p != nullptr) {
1908 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07001909
1910 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
1911 // it isFixedSize) then there may be additional scaling not accounted
1912 // for in the transform. We need to mirror this scaling in child surfaces
1913 // or we will break the contract where WM can treat child surfaces as
1914 // pixels in the parent surface.
David Sodman0cc69182017-11-17 12:12:07 -08001915 if (p->isFixedSize() && p->getBE().compositionInfo.mBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07001916 int bufferWidth;
1917 int bufferHeight;
1918 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
David Sodman0cc69182017-11-17 12:12:07 -08001919 bufferWidth = p->getBE().compositionInfo.mBuffer->getWidth();
1920 bufferHeight = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001921 } else {
David Sodman0cc69182017-11-17 12:12:07 -08001922 bufferHeight = p->getBE().compositionInfo.mBuffer->getWidth();
1923 bufferWidth = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001924 }
Marissa Wall61c58622018-07-18 10:12:20 -07001925 float sx = p->getActiveWidth(p->getDrawingState()) / static_cast<float>(bufferWidth);
1926 float sy = p->getActiveHeight(p->getDrawingState()) / static_cast<float>(bufferHeight);
Robert Carr9b429f42017-04-17 14:56:57 -07001927 Transform extraParentScaling;
1928 extraParentScaling.set(sx, 0, 0, sy);
1929 t = t * extraParentScaling;
1930 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001931 }
Marissa Wall61c58622018-07-18 10:12:20 -07001932 return t * getActiveTransform(getDrawingState());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001933}
1934
chaviw13fdc492017-06-27 12:40:18 -07001935half Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001936 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07001937
chaviw13fdc492017-06-27 12:40:18 -07001938 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1939 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001940}
Robert Carr6452f122017-03-21 10:41:29 -07001941
chaviw13fdc492017-06-27 12:40:18 -07001942half4 Layer::getColor() const {
1943 const half4 color(getDrawingState().color);
1944 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001945}
Robert Carr6452f122017-03-21 10:41:29 -07001946
Robert Carr1f0a16a2016-10-24 16:27:39 -07001947void Layer::commitChildList() {
1948 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1949 const auto& child = mCurrentChildren[i];
1950 child->commitChildList();
1951 }
1952 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001953 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001954}
1955
chaviw1d044282017-09-27 12:19:28 -07001956void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
1957 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1958 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1959 const State& state = useDrawing ? mDrawingState : mCurrentState;
1960
Marissa Wallf58c14b2018-07-24 10:50:43 -07001961 Transform requestedTransform = state.active_legacy.transform;
chaviw1d044282017-09-27 12:19:28 -07001962 Transform transform = getTransform();
1963
1964 layerInfo->set_id(sequence);
1965 layerInfo->set_name(getName().c_str());
1966 layerInfo->set_type(String8(getTypeId()));
1967
1968 for (const auto& child : children) {
1969 layerInfo->add_children(child->sequence);
1970 }
1971
1972 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1973 sp<Layer> strongRelative = weakRelative.promote();
1974 if (strongRelative != nullptr) {
1975 layerInfo->add_relatives(strongRelative->sequence);
1976 }
1977 }
1978
Marissa Wallf58c14b2018-07-24 10:50:43 -07001979 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
chaviw1d044282017-09-27 12:19:28 -07001980 layerInfo->mutable_transparent_region());
1981 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
1982 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
1983
1984 layerInfo->set_layer_stack(getLayerStack());
1985 layerInfo->set_z(state.z);
1986
1987 PositionProto* position = layerInfo->mutable_position();
1988 position->set_x(transform.tx());
1989 position->set_y(transform.ty());
1990
1991 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
1992 requestedPosition->set_x(requestedTransform.tx());
1993 requestedPosition->set_y(requestedTransform.ty());
1994
1995 SizeProto* size = layerInfo->mutable_size();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001996 size->set_w(state.active_legacy.w);
1997 size->set_h(state.active_legacy.h);
chaviw1d044282017-09-27 12:19:28 -07001998
Marissa Wallf58c14b2018-07-24 10:50:43 -07001999 LayerProtoHelper::writeToProto(state.crop_legacy, layerInfo->mutable_crop());
2000 LayerProtoHelper::writeToProto(state.finalCrop_legacy, layerInfo->mutable_final_crop());
chaviw1d044282017-09-27 12:19:28 -07002001
2002 layerInfo->set_is_opaque(isOpaque(state));
2003 layerInfo->set_invalidate(contentDirty);
Chia-I Wu01591c92018-05-22 12:03:00 -07002004
2005 // XXX (b/79210409) mCurrentDataSpace is not protected
2006 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
2007
chaviw1d044282017-09-27 12:19:28 -07002008 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
2009 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
2010 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
2011 layerInfo->set_flags(state.flags);
2012
2013 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
2014 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
2015
Jorim Jaggi8e0af362017-11-14 16:28:28 +01002016 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07002017 if (parent != nullptr) {
2018 layerInfo->set_parent(parent->sequence);
2019 }
2020
2021 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2022 if (zOrderRelativeOf != nullptr) {
2023 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
2024 }
2025
Chia-I Wu01591c92018-05-22 12:03:00 -07002026 // XXX getBE().compositionInfo.mBuffer is not protected
David Sodman0cc69182017-11-17 12:12:07 -08002027 auto buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08002028 if (buffer != nullptr) {
2029 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
Yichi Chen6ca35192018-05-29 12:20:43 +08002030 LayerProtoHelper::writeToProto(Transform(mCurrentTransform),
2031 layerInfo->mutable_buffer_transform());
chaviw1d044282017-09-27 12:19:28 -07002032 }
2033
2034 layerInfo->set_queued_frames(getQueuedFrameCount());
2035 layerInfo->set_refresh_pending(isBufferLatched());
rongliucfb187b2018-03-14 12:26:23 -07002036 layerInfo->set_window_type(state.type);
2037 layerInfo->set_app_id(state.appId);
chaviwadc40c22018-07-10 16:57:27 -07002038 layerInfo->set_curr_frame(mCurrentFrameNumber);
2039
2040 for (const auto& pendingState : mPendingStates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07002041 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
chaviwadc40c22018-07-10 16:57:27 -07002042 if (barrierLayer != nullptr) {
2043 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
2044 barrierLayerProto->set_id(barrierLayer->sequence);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002045 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
chaviwadc40c22018-07-10 16:57:27 -07002046 }
2047 }
chaviw1d044282017-09-27 12:19:28 -07002048}
2049
Dominik Laskowski7e045462018-05-30 13:02:02 -07002050void Layer::writeToProto(LayerProto* layerInfo, int32_t displayId) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07002051 if (!hasHwcLayer(displayId)) {
2052 return;
2053 }
2054
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002055 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
2056
Dominik Laskowski7e045462018-05-30 13:02:02 -07002057 const auto& hwcInfo = getBE().mHwcLayers.at(displayId);
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002058
2059 const Rect& frame = hwcInfo.displayFrame;
2060 LayerProtoHelper::writeToProto(frame, layerInfo->mutable_hwc_frame());
2061
2062 const FloatRect& crop = hwcInfo.sourceCrop;
2063 LayerProtoHelper::writeToProto(crop, layerInfo->mutable_hwc_crop());
2064
2065 const int32_t transform = static_cast<int32_t>(hwcInfo.transform);
2066 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002067
2068 const int32_t compositionType = static_cast<int32_t>(hwcInfo.compositionType);
2069 layerInfo->set_hwc_composition_type(compositionType);
2070
2071 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
2072 static_cast<BufferLayer*>(this)->isProtected()) {
2073 layerInfo->set_is_protected(true);
2074 } else {
2075 layerInfo->set_is_protected(false);
2076 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002077}
2078
Mathias Agopian13127d82013-03-05 17:47:11 -08002079// ---------------------------------------------------------------------------
2080
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002081}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002082
2083#if defined(__gl_h_)
2084#error "don't include gl/gl.h in this file"
2085#endif
2086
2087#if defined(__gl2_h_)
2088#error "don't include gl2/gl2.h in this file"
2089#endif