blob: eeee0ae863de80d211d0d3b5bbfd9276cc2567a9 [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;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700117
118 // drawing state & current state are identical
119 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700120
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800121 CompositorTiming compositorTiming;
122 flinger->getCompositorTiming(&compositorTiming);
123 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jamie Gennise8696a42012-01-15 18:54:57 -0800124}
125
Dan Stoza436ccf32018-06-21 12:10:12 -0700126void Layer::onFirstRef() NO_THREAD_SAFETY_ANALYSIS {
127 if (!isCreatedFromMainThread()) {
128 // Grab the SF state lock during this since it's the only way to safely access HWC
129 mFlinger->mStateLock.lock();
130 }
131
132 const auto& hwc = mFlinger->getHwComposer();
133 const auto& activeConfig = hwc.getActiveConfig(HWC_DISPLAY_PRIMARY);
134 nsecs_t displayPeriod = activeConfig->getVsyncPeriod();
135 mFrameTracker.setDisplayRefreshPeriod(displayPeriod);
136
137 if (!isCreatedFromMainThread()) {
138 mFlinger->mStateLock.unlock();
139 }
140}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700141
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700142Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800143 sp<Client> c(mClientRef.promote());
144 if (c != 0) {
145 c->detachLayer(this);
146 }
147
148 for (auto& point : mRemoteSyncPoints) {
149 point->setTransactionApplied();
150 }
151 for (auto& point : mLocalSyncPoints) {
152 point->setFrameAvailable();
153 }
Jamie Gennis6547ff42013-07-16 20:12:42 -0700154 mFrameTracker.logAndResetStats(mName);
Mathias Agopian96f08192010-06-02 23:28:45 -0700155}
156
Mathias Agopian13127d82013-03-05 17:47:11 -0800157// ---------------------------------------------------------------------------
158// callbacks
159// ---------------------------------------------------------------------------
160
David Sodmaneb085e02017-10-05 18:49:04 -0700161/*
162 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
163 * Layer. So, the implementation is done in BufferLayer. When called on a
164 * ColorLayer object, it's essentially a NOP.
165 */
David Sodmaneb085e02017-10-05 18:49:04 -0700166void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800167
Chia-I Wuc6657022017-08-15 11:18:17 -0700168void Layer::onRemovedFromCurrentState() {
169 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
170
chaviw8b3871a2017-11-01 17:41:01 -0700171 mPendingRemoval = true;
172
Robert Carr5edb1ad2017-04-25 10:54:24 -0700173 if (mCurrentState.zOrderRelativeOf != nullptr) {
174 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
175 if (strongRelative != nullptr) {
176 strongRelative->removeZOrderRelative(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700177 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700178 }
179 mCurrentState.zOrderRelativeOf = nullptr;
180 }
181
Chia-I Wuc6657022017-08-15 11:18:17 -0700182 for (const auto& child : mCurrentChildren) {
183 child->onRemovedFromCurrentState();
184 }
185}
Chia-I Wu38512252017-05-17 14:36:16 -0700186
Chia-I Wuc6657022017-08-15 11:18:17 -0700187void Layer::onRemoved() {
188 // the layer is removed from SF mLayersPendingRemoval
David Sodmaneb085e02017-10-05 18:49:04 -0700189 abandon();
Chia-I Wuc6657022017-08-15 11:18:17 -0700190
Steven Thomasb02664d2017-07-26 18:48:28 -0700191 destroyAllHwcLayers();
Chia-I Wu38512252017-05-17 14:36:16 -0700192
Robert Carr1f0a16a2016-10-24 16:27:39 -0700193 for (const auto& child : mCurrentChildren) {
194 child->onRemoved();
195 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700196}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700197
Mathias Agopian13127d82013-03-05 17:47:11 -0800198// ---------------------------------------------------------------------------
199// set-up
200// ---------------------------------------------------------------------------
201
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700202const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800203 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800204}
205
chaviw13fdc492017-06-27 12:40:18 -0700206bool Layer::getPremultipledAlpha() const {
207 return mPremultipliedAlpha;
208}
209
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700210sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800211 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700212 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800213}
214
215// ---------------------------------------------------------------------------
216// h/w composer set-up
217// ---------------------------------------------------------------------------
218
Dominik Laskowski7e045462018-05-30 13:02:02 -0700219bool Layer::createHwcLayer(HWComposer* hwc, int32_t displayId) {
220 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.count(displayId) != 0,
221 "Already have a layer for display %d", displayId);
222 HWC2::Layer* layer = hwc->createLayer(displayId);
Steven Thomasb02664d2017-07-26 18:48:28 -0700223 if (!layer) {
224 return false;
225 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700226 LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers[displayId];
Steven Thomasb02664d2017-07-26 18:48:28 -0700227 hwcInfo.hwc = hwc;
228 hwcInfo.layer = layer;
David Sodmanf6a38932018-05-25 15:27:50 -0700229 layer->setLayerDestroyedListener(
Dominik Laskowski7e045462018-05-30 13:02:02 -0700230 [this, displayId](HWC2::Layer* /*layer*/) { getBE().mHwcLayers.erase(displayId); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700231 return true;
232}
233
Dominik Laskowski7e045462018-05-30 13:02:02 -0700234bool Layer::destroyHwcLayer(int32_t displayId) {
235 if (getBE().mHwcLayers.count(displayId) == 0) {
Chia-I Wu83806892017-11-16 10:50:20 -0800236 return false;
Steven Thomasb02664d2017-07-26 18:48:28 -0700237 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700238 auto& hwcInfo = getBE().mHwcLayers[displayId];
David Sodman41fdfc92017-11-06 16:09:56 -0800239 LOG_ALWAYS_FATAL_IF(hwcInfo.layer == nullptr, "Attempt to destroy null layer");
Steven Thomasb02664d2017-07-26 18:48:28 -0700240 LOG_ALWAYS_FATAL_IF(hwcInfo.hwc == nullptr, "Missing HWComposer");
Dominik Laskowski7e045462018-05-30 13:02:02 -0700241 hwcInfo.hwc->destroyLayer(displayId, hwcInfo.layer);
David Sodmanf6a38932018-05-25 15:27:50 -0700242 // The layer destroyed listener should have cleared the entry from
243 // mHwcLayers. Verify that.
Dominik Laskowski7e045462018-05-30 13:02:02 -0700244 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.count(displayId) != 0,
David Sodmanf6a38932018-05-25 15:27:50 -0700245 "Stale layer entry in getBE().mHwcLayers");
Chia-I Wu83806892017-11-16 10:50:20 -0800246 return true;
Steven Thomasb02664d2017-07-26 18:48:28 -0700247}
248
249void Layer::destroyAllHwcLayers() {
David Sodman6f65f3e2017-11-03 14:28:09 -0700250 size_t numLayers = getBE().mHwcLayers.size();
Steven Thomasb02664d2017-07-26 18:48:28 -0700251 for (size_t i = 0; i < numLayers; ++i) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700252 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.empty(), "destroyAllHwcLayers failed");
253 destroyHwcLayer(getBE().mHwcLayers.begin()->first);
Steven Thomasb02664d2017-07-26 18:48:28 -0700254 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700255 LOG_ALWAYS_FATAL_IF(!getBE().mHwcLayers.empty(),
David Sodman41fdfc92017-11-06 16:09:56 -0800256 "All hardware composer layers should have been destroyed");
Steven Thomasb02664d2017-07-26 18:48:28 -0700257}
Steven Thomasb02664d2017-07-26 18:48:28 -0700258
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800259Rect Layer::getContentCrop() const {
260 // this is the crop rectangle that applies to the buffer
261 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700262 Rect crop;
263 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800264 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700265 crop = mCurrentCrop;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800266 } else if (getBE().compositionInfo.mBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800267 // otherwise we use the whole buffer
David Sodman0cc69182017-11-17 12:12:07 -0800268 crop = getBE().compositionInfo.mBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700269 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800270 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700271 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700272 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700273 return crop;
274}
275
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700276static Rect reduce(const Rect& win, const Region& exclude) {
277 if (CC_LIKELY(exclude.isEmpty())) {
278 return win;
279 }
280 if (exclude.isRect()) {
281 return win.reduce(exclude.getBounds());
282 }
283 return Region(win).subtract(exclude).getBounds();
284}
285
Dan Stoza80d61162017-12-20 15:57:52 -0800286static FloatRect reduce(const FloatRect& win, const Region& exclude) {
287 if (CC_LIKELY(exclude.isEmpty())) {
288 return win;
289 }
290 // Convert through Rect (by rounding) for lack of FloatRegion
291 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
292}
293
Robert Carr1f0a16a2016-10-24 16:27:39 -0700294Rect Layer::computeScreenBounds(bool reduceTransparentRegion) const {
295 const Layer::State& s(getDrawingState());
Marissa Wallf58c14b2018-07-24 10:50:43 -0700296 Rect win(s.active_legacy.w, s.active_legacy.h);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700297
Marissa Wallf58c14b2018-07-24 10:50:43 -0700298 if (!s.crop_legacy.isEmpty()) {
299 win.intersect(s.crop_legacy, &win);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700300 }
301
302 Transform t = getTransform();
303 win = t.transform(win);
304
Marissa Wallf58c14b2018-07-24 10:50:43 -0700305 if (!s.finalCrop_legacy.isEmpty()) {
306 win.intersect(s.finalCrop_legacy, &win);
Robert Carr41b08b52017-06-01 16:11:34 -0700307 }
308
Chia-I Wue41dbe62017-06-13 14:10:56 -0700309 const sp<Layer>& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700310 // Now we need to calculate the parent bounds, so we can clip ourselves to those.
311 // When calculating the parent bounds for purposes of clipping,
312 // we don't need to constrain the parent to its transparent region.
313 // The transparent region is an optimization based on the
314 // buffer contents of the layer, but does not affect the space allocated to
315 // it by policy, and thus children should be allowed to extend into the
316 // parent's transparent region. In fact one of the main uses, is to reduce
317 // buffer allocation size in cases where a child window sits behind a main window
318 // (by marking the hole in the parent window as a transparent region)
319 if (p != nullptr) {
320 Rect bounds = p->computeScreenBounds(false);
321 bounds.intersect(win, &win);
322 }
323
324 if (reduceTransparentRegion) {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700325 auto const screenTransparentRegion = t.transform(s.activeTransparentRegion_legacy);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700326 win = reduce(win, screenTransparentRegion);
327 }
328
329 return win;
330}
331
Dan Stoza80d61162017-12-20 15:57:52 -0800332FloatRect Layer::computeBounds() const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700333 const Layer::State& s(getDrawingState());
Marissa Wallf58c14b2018-07-24 10:50:43 -0700334 return computeBounds(s.activeTransparentRegion_legacy);
Michael Lentine6c925ed2014-09-26 17:55:01 -0700335}
336
Dan Stoza80d61162017-12-20 15:57:52 -0800337FloatRect Layer::computeBounds(const Region& activeTransparentRegion) const {
Michael Lentine6c925ed2014-09-26 17:55:01 -0700338 const Layer::State& s(getDrawingState());
Marissa Wallf58c14b2018-07-24 10:50:43 -0700339 Rect win(s.active_legacy.w, s.active_legacy.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700340
Marissa Wallf58c14b2018-07-24 10:50:43 -0700341 if (!s.crop_legacy.isEmpty()) {
342 win.intersect(s.crop_legacy, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800343 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700344
Chia-I Wue41dbe62017-06-13 14:10:56 -0700345 const auto& p = mDrawingParent.promote();
Robert Carrd4ae7f32018-06-07 16:10:57 -0700346 FloatRect floatWin = win.toFloatRect();
347 FloatRect parentBounds = floatWin;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700348 if (p != nullptr) {
Robert Carrd4ae7f32018-06-07 16:10:57 -0700349 // We pass an empty Region here for reasons mirroring that of the case described in
350 // the computeScreenBounds reduceTransparentRegion=false case.
351 parentBounds = p->computeBounds(Region());
Robert Carr1f0a16a2016-10-24 16:27:39 -0700352 }
353
Marissa Wallf58c14b2018-07-24 10:50:43 -0700354 Transform t = s.active_legacy.transform;
Dan Stoza80d61162017-12-20 15:57:52 -0800355
Marissa Wallf58c14b2018-07-24 10:50:43 -0700356 if (p != nullptr || !s.finalCrop_legacy.isEmpty()) {
Dan Stoza80d61162017-12-20 15:57:52 -0800357 floatWin = t.transform(floatWin);
Robert Carrd4ae7f32018-06-07 16:10:57 -0700358 floatWin = floatWin.intersect(parentBounds);
359
Marissa Wallf58c14b2018-07-24 10:50:43 -0700360 if (!s.finalCrop_legacy.isEmpty()) {
361 floatWin = floatWin.intersect(s.finalCrop_legacy.toFloatRect());
Robert Carrd4ae7f32018-06-07 16:10:57 -0700362 }
Dan Stoza80d61162017-12-20 15:57:52 -0800363 floatWin = t.inverse().transform(floatWin);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700364 }
365
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700366 // subtract the transparent region and snap to the bounds
Dan Stoza80d61162017-12-20 15:57:52 -0800367 return reduce(floatWin, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800368}
369
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700370Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& display) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700371 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800372 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700373 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800374
375 // apply the projection's clipping to the window crop in
376 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700377 // if there are no window scaling involved, this operation will map to full
378 // pixels in the buffer.
379 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
380 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700381
Marissa Wallf58c14b2018-07-24 10:50:43 -0700382 Rect activeCrop(s.active_legacy.w, s.active_legacy.h);
383 if (!s.crop_legacy.isEmpty()) {
384 activeCrop.intersect(s.crop_legacy, &activeCrop);
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700385 }
386
Robert Carr1f0a16a2016-10-24 16:27:39 -0700387 Transform t = getTransform();
388 activeCrop = t.transform(activeCrop);
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700389 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000390 activeCrop.clear();
391 }
Marissa Wallf58c14b2018-07-24 10:50:43 -0700392 if (!s.finalCrop_legacy.isEmpty()) {
393 if (!activeCrop.intersect(s.finalCrop_legacy, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000394 activeCrop.clear();
395 }
396 }
chaviwb1154d12017-10-31 14:15:36 -0700397
398 const auto& p = mDrawingParent.promote();
399 if (p != nullptr) {
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700400 auto parentCrop = p->computeInitialCrop(display);
chaviwb1154d12017-10-31 14:15:36 -0700401 activeCrop.intersect(parentCrop, &activeCrop);
402 }
403
Robert Carr1f0a16a2016-10-24 16:27:39 -0700404 return activeCrop;
405}
406
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700407FloatRect Layer::computeCrop(const sp<const DisplayDevice>& display) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700408 // the content crop is the area of the content that gets scaled to the
409 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800410 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700411
412 // In addition there is a WM-specified crop we pull from our drawing state.
413 const State& s(getDrawingState());
414
415 // Screen space to make reduction to parent crop clearer.
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700416 Rect activeCrop = computeInitialCrop(display);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700417 Transform t = getTransform();
418 // Back to layer space to work with the content crop.
419 activeCrop = t.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800420
Michael Lentine28ea2172014-11-19 18:32:37 -0800421 // This needs to be here as transform.transform(Rect) computes the
422 // transformed rect and then takes the bounding box of the result before
423 // returning. This means
424 // transform.inverse().transform(transform.transform(Rect)) != Rect
425 // in which case we need to make sure the final rect is clipped to the
426 // display bounds.
Marissa Wallf58c14b2018-07-24 10:50:43 -0700427 if (!activeCrop.intersect(Rect(s.active_legacy.w, s.active_legacy.h), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000428 activeCrop.clear();
429 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800430
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700431 // subtract the transparent region and snap to the bounds
Marissa Wallf58c14b2018-07-24 10:50:43 -0700432 activeCrop = reduce(activeCrop, s.activeTransparentRegion_legacy);
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700433
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000434 // Transform the window crop to match the buffer coordinate system,
435 // which means using the inverse of the current transform set on the
436 // SurfaceFlingerConsumer.
437 uint32_t invTransform = mCurrentTransform;
Robert Carrcae605c2017-03-29 12:10:31 -0700438 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000439 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700440 * the code below applies the primary display's inverse transform to the
441 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000442 */
David Sodman41fdfc92017-11-06 16:09:56 -0800443 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000444 // calculate the inverse transform
445 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800446 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800447 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000448 // and apply to the current transform
David Sodman41fdfc92017-11-06 16:09:56 -0800449 invTransform = (Transform(invTransformOrient) * Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800450 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000451
Marissa Wallf58c14b2018-07-24 10:50:43 -0700452 int winWidth = s.active_legacy.w;
453 int winHeight = s.active_legacy.h;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000454 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
455 // If the activeCrop has been rotate the ends are rotated but not
456 // the space itself so when transforming ends back we can't rely on
457 // a modification of the axes of rotation. To account for this we
458 // need to reorient the inverse rotation in terms of the current
459 // axes of rotation.
460 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
461 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
462 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800463 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000464 }
Marissa Wallf58c14b2018-07-24 10:50:43 -0700465 winWidth = s.active_legacy.h;
466 winHeight = s.active_legacy.w;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000467 }
Marissa Wallf58c14b2018-07-24 10:50:43 -0700468 const Rect winCrop = activeCrop.transform(invTransform, s.active_legacy.w, s.active_legacy.h);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000469
470 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800471 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000472 float yScale = crop.getHeight() / float(winHeight);
473
David Sodman41fdfc92017-11-06 16:09:56 -0800474 float insetL = winCrop.left * xScale;
475 float insetT = winCrop.top * yScale;
476 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000477 float insetB = (winHeight - winCrop.bottom) * yScale;
478
David Sodman41fdfc92017-11-06 16:09:56 -0800479 crop.left += insetL;
480 crop.top += insetT;
481 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000482 crop.bottom -= insetB;
483
Mathias Agopian13127d82013-03-05 17:47:11 -0800484 return crop;
485}
486
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700487void Layer::setGeometry(const sp<const DisplayDevice>& display, uint32_t z) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700488 const auto displayId = display->getId();
Peiyong Lin91b1df22018-06-18 18:00:16 -0700489 if (!hasHwcLayer(displayId)) {
490 ALOGE("[%s] failed to setGeometry: no HWC layer found (%d)",
491 mName.string(), displayId);
492 return;
493 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700494 auto& hwcInfo = getBE().mHwcLayers[displayId];
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700495
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700496 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800497 hwcInfo.forceClientComposition = false;
498
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700499 if (isSecure() && !display->isSecure()) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800500 hwcInfo.forceClientComposition = true;
501 }
502
Lloyd Pique074e8122018-07-26 12:57:23 -0700503 auto& hwcLayer = hwcInfo.layer;
504
Mathias Agopian13127d82013-03-05 17:47:11 -0800505 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700506 const State& s(getDrawingState());
David Revemanecf0fa52017-03-03 11:32:44 -0500507 auto blendMode = HWC2::BlendMode::None;
Robert Carr6452f122017-03-21 10:41:29 -0700508 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800509 blendMode =
510 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800511 }
Lloyd Pique074e8122018-07-26 12:57:23 -0700512 auto error = hwcLayer->setBlendMode(blendMode);
513 ALOGE_IF(error != HWC2::Error::None,
514 "[%s] Failed to set blend mode %s:"
515 " %s (%d)",
516 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
517 static_cast<int32_t>(error));
Mathias Agopian13127d82013-03-05 17:47:11 -0800518
519 // apply the layer's transform, followed by the display's global transform
520 // here we're guaranteed that the layer's transform preserves rects
Marissa Wallf58c14b2018-07-24 10:50:43 -0700521 Region activeTransparentRegion(s.activeTransparentRegion_legacy);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700522 Transform t = getTransform();
Marissa Wallf58c14b2018-07-24 10:50:43 -0700523 if (!s.crop_legacy.isEmpty()) {
524 Rect activeCrop(s.crop_legacy);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700525 activeCrop = t.transform(activeCrop);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700526 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000527 activeCrop.clear();
528 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700529 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800530 // This needs to be here as transform.transform(Rect) computes the
531 // transformed rect and then takes the bounding box of the result before
532 // returning. This means
533 // transform.inverse().transform(transform.transform(Rect)) != Rect
534 // in which case we need to make sure the final rect is clipped to the
535 // display bounds.
Marissa Wallf58c14b2018-07-24 10:50:43 -0700536 if (!activeCrop.intersect(Rect(s.active_legacy.w, s.active_legacy.h), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000537 activeCrop.clear();
538 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700539 // mark regions outside the crop as transparent
Marissa Wallf58c14b2018-07-24 10:50:43 -0700540 activeTransparentRegion.orSelf(Rect(0, 0, s.active_legacy.w, activeCrop.top));
541 activeTransparentRegion.orSelf(
542 Rect(0, activeCrop.bottom, s.active_legacy.w, s.active_legacy.h));
David Sodman41fdfc92017-11-06 16:09:56 -0800543 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
544 activeTransparentRegion.orSelf(
Marissa Wallf58c14b2018-07-24 10:50:43 -0700545 Rect(activeCrop.right, activeCrop.top, s.active_legacy.w, activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700546 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700547
Dan Stoza80d61162017-12-20 15:57:52 -0800548 // computeBounds returns a FloatRect to provide more accuracy during the
549 // transformation. We then round upon constructing 'frame'.
550 Rect frame{t.transform(computeBounds(activeTransparentRegion))};
Marissa Wallf58c14b2018-07-24 10:50:43 -0700551 if (!s.finalCrop_legacy.isEmpty()) {
552 if (!frame.intersect(s.finalCrop_legacy, &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000553 frame.clear();
554 }
555 }
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700556 if (!frame.intersect(display->getViewport(), &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000557 frame.clear();
558 }
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700559 const Transform& tr = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800560 Rect transformedFrame = tr.transform(frame);
Lloyd Pique074e8122018-07-26 12:57:23 -0700561 error = hwcLayer->setDisplayFrame(transformedFrame);
562 if (error != HWC2::Error::None) {
563 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
564 transformedFrame.left, transformedFrame.top, transformedFrame.right,
565 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
566 } else {
567 hwcInfo.displayFrame = transformedFrame;
568 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800569
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700570 FloatRect sourceCrop = computeCrop(display);
Lloyd Pique074e8122018-07-26 12:57:23 -0700571 error = hwcLayer->setSourceCrop(sourceCrop);
572 if (error != HWC2::Error::None) {
573 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
574 "%s (%d)",
575 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
576 to_string(error).c_str(), static_cast<int32_t>(error));
577 } else {
578 hwcInfo.sourceCrop = sourceCrop;
579 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800580
chaviw13fdc492017-06-27 12:40:18 -0700581 float alpha = static_cast<float>(getAlpha());
Lloyd Pique074e8122018-07-26 12:57:23 -0700582 error = hwcLayer->setPlaneAlpha(alpha);
583 ALOGE_IF(error != HWC2::Error::None,
584 "[%s] Failed to set plane alpha %.3f: "
585 "%s (%d)",
586 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800587
Lloyd Pique074e8122018-07-26 12:57:23 -0700588 error = hwcLayer->setZOrder(z);
589 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
590 to_string(error).c_str(), static_cast<int32_t>(error));
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500591
Albert Chaulk2a589632017-05-04 16:59:44 -0400592 int type = s.type;
593 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700594 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400595 if (parent.get()) {
596 auto& parentState = parent->getDrawingState();
rongliucfb187b2018-03-14 12:26:23 -0700597 if (parentState.type >= 0 || parentState.appId >= 0) {
598 type = parentState.type;
599 appId = parentState.appId;
600 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400601 }
602
Lloyd Pique074e8122018-07-26 12:57:23 -0700603 error = hwcLayer->setInfo(type, appId);
604 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
605 static_cast<int32_t>(error));
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800606
Mathias Agopian29a367b2011-07-12 14:51:45 -0700607 /*
608 * Transformations are applied in this order:
609 * 1) buffer orientation/flip/mirror
610 * 2) state transformation (window manager)
611 * 3) layer orientation (screen orientation)
612 * (NOTE: the matrices are multiplied in reverse order)
613 */
614
615 const Transform bufferOrientation(mCurrentTransform);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700616 Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700617
Robert Carrcae605c2017-03-29 12:10:31 -0700618 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700619 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700620 * the code below applies the primary display's inverse transform to the
621 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700622 */
David Sodman41fdfc92017-11-06 16:09:56 -0800623 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700624 // calculate the inverse transform
625 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800626 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700627 }
Robert Carrcae605c2017-03-29 12:10:31 -0700628
629 /*
630 * Here we cancel out the orientation component of the WM transform.
631 * The scaling and translate components are already included in our bounds
632 * computation so it's enough to just omit it in the composition.
633 * See comment in onDraw with ref to b/36727915 for why.
634 */
635 transform = Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700636 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700637
638 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800639 const uint32_t orientation = transform.getOrientation();
Jorim Jaggif3bd94a2018-03-27 15:38:03 +0200640 if (orientation & Transform::ROT_INVALID) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800641 // we can only handle simple transformation
Lloyd Pique074e8122018-07-26 12:57:23 -0700642 hwcInfo.forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800643 } else {
644 auto transform = static_cast<HWC2::Transform>(orientation);
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800645 hwcInfo.transform = transform;
Lloyd Pique074e8122018-07-26 12:57:23 -0700646 auto error = hwcLayer->setTransform(transform);
647 ALOGE_IF(error != HWC2::Error::None,
648 "[%s] Failed to set transform %s: "
649 "%s (%d)",
650 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
651 static_cast<int32_t>(error));
David Sodman4b7c4bc2017-11-17 12:13:59 -0800652 }
653}
654
Dominik Laskowski7e045462018-05-30 13:02:02 -0700655void Layer::forceClientComposition(int32_t displayId) {
656 if (getBE().mHwcLayers.count(displayId) == 0) {
657 ALOGE("forceClientComposition: no HWC layer found (%d)", displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800658 return;
659 }
660
Dominik Laskowski7e045462018-05-30 13:02:02 -0700661 getBE().mHwcLayers[displayId].forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800662}
Dan Stozaee44edd2015-03-23 15:50:23 -0700663
Dominik Laskowski7e045462018-05-30 13:02:02 -0700664bool Layer::getForceClientComposition(int32_t displayId) {
665 if (getBE().mHwcLayers.count(displayId) == 0) {
666 ALOGE("getForceClientComposition: no HWC layer found (%d)", displayId);
chaviwc9232ed2017-11-14 15:31:15 -0800667 return false;
668 }
669
Dominik Laskowski7e045462018-05-30 13:02:02 -0700670 return getBE().mHwcLayers[displayId].forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800671}
672
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700673void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700674 const auto displayId = display->getId();
675 if (getBE().mHwcLayers.count(displayId) == 0 ||
676 getCompositionType(displayId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800677 return;
678 }
679
680 // This gives us only the "orientation" component of the transform
681 const State& s(getCurrentState());
682
683 // Apply the layer's transform, followed by the display's global transform
684 // Here we're guaranteed that the layer's transform preserves rects
Marissa Wallf58c14b2018-07-24 10:50:43 -0700685 Rect win(s.active_legacy.w, s.active_legacy.h);
686 if (!s.crop_legacy.isEmpty()) {
687 win.intersect(s.crop_legacy, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800688 }
689 // Subtract the transparent region and snap to the bounds
Marissa Wallf58c14b2018-07-24 10:50:43 -0700690 Rect bounds = reduce(win, s.activeTransparentRegion_legacy);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700691 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700692 frame.intersect(display->getViewport(), &frame);
Marissa Wallf58c14b2018-07-24 10:50:43 -0700693 if (!s.finalCrop_legacy.isEmpty()) {
694 frame.intersect(s.finalCrop_legacy, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000695 }
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700696 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800697 auto position = displayTransform.transform(frame);
698
Dominik Laskowski7e045462018-05-30 13:02:02 -0700699 auto error =
700 getBE().mHwcLayers[displayId].layer->setCursorPosition(position.left, position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800701 ALOGE_IF(error != HWC2::Error::None,
702 "[%s] Failed to set cursor position "
703 "to (%d, %d): %s (%d)",
704 mName.string(), position.left, position.top, to_string(error).c_str(),
705 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800706}
Riley Andrews03414a12014-07-01 14:22:59 -0700707
Mathias Agopian13127d82013-03-05 17:47:11 -0800708// ---------------------------------------------------------------------------
709// drawing...
710// ---------------------------------------------------------------------------
711
chaviwa76b2712017-09-20 12:02:26 -0700712void Layer::draw(const RenderArea& renderArea, const Region& clip) const {
713 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800714}
715
chaviwa76b2712017-09-20 12:02:26 -0700716void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) const {
717 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800718}
719
chaviwa76b2712017-09-20 12:02:26 -0700720void Layer::draw(const RenderArea& renderArea) const {
721 onDraw(renderArea, Region(renderArea.getBounds()), false);
Dan Stozac7014012014-02-14 15:03:43 -0800722}
723
David Sodman41fdfc92017-11-06 16:09:56 -0800724void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
725 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800726 auto& engine(mFlinger->getRenderEngine());
David Sodman9eeae692017-11-02 10:53:32 -0700727 computeGeometry(renderArea, getBE().mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700728 engine.setupFillWithColor(red, green, blue, alpha);
David Sodman9eeae692017-11-02 10:53:32 -0700729 engine.drawMesh(getBE().mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800730}
731
chaviwa76b2712017-09-20 12:02:26 -0700732void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
David Sodman41fdfc92017-11-06 16:09:56 -0800733 clearWithOpenGL(renderArea, 0, 0, 0, 0);
Mathias Agopian13127d82013-03-05 17:47:11 -0800734}
735
Dominik Laskowski7e045462018-05-30 13:02:02 -0700736void Layer::setCompositionType(int32_t displayId, HWC2::Composition type, bool callIntoHwc) {
737 if (getBE().mHwcLayers.count(displayId) == 0) {
Chia-I Wu30505fb2018-03-26 16:20:31 -0700738 ALOGE("setCompositionType called without a valid HWC layer");
739 return;
740 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700741 auto& hwcInfo = getBE().mHwcLayers[displayId];
Chia-I Wu30505fb2018-03-26 16:20:31 -0700742 auto& hwcLayer = hwcInfo.layer;
David Sodmanf6a38932018-05-25 15:27:50 -0700743 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(), to_string(type).c_str(),
Chia-I Wu30505fb2018-03-26 16:20:31 -0700744 static_cast<int>(callIntoHwc));
745 if (hwcInfo.compositionType != type) {
746 ALOGV(" actually setting");
747 hwcInfo.compositionType = type;
748 if (callIntoHwc) {
David Sodmanf6a38932018-05-25 15:27:50 -0700749 auto error = hwcLayer->setCompositionType(type);
Chia-I Wu30505fb2018-03-26 16:20:31 -0700750 ALOGE_IF(error != HWC2::Error::None,
751 "[%s] Failed to set "
752 "composition type %s: %s (%d)",
753 mName.string(), to_string(type).c_str(), to_string(error).c_str(),
754 static_cast<int32_t>(error));
755 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800756 }
757}
758
Dominik Laskowski7e045462018-05-30 13:02:02 -0700759HWC2::Composition Layer::getCompositionType(int32_t displayId) const {
760 if (displayId == DisplayDevice::DISPLAY_ID_INVALID) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700761 // If we're querying the composition type for a display that does not
762 // have a HWC counterpart, then it will always be Client
763 return HWC2::Composition::Client;
764 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700765 if (getBE().mHwcLayers.count(displayId) == 0) {
Chia-I Wu30505fb2018-03-26 16:20:31 -0700766 ALOGE("getCompositionType called with an invalid HWC layer");
767 return HWC2::Composition::Invalid;
768 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700769 return getBE().mHwcLayers.at(displayId).compositionType;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800770}
771
Dominik Laskowski7e045462018-05-30 13:02:02 -0700772void Layer::setClearClientTarget(int32_t displayId, bool clear) {
773 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800774 ALOGE("setClearClientTarget called without a valid HWC layer");
775 return;
776 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700777 getBE().mHwcLayers[displayId].clearClientTarget = clear;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800778}
779
Dominik Laskowski7e045462018-05-30 13:02:02 -0700780bool Layer::getClearClientTarget(int32_t displayId) const {
781 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800782 ALOGE("getClearClientTarget called without a valid HWC layer");
783 return false;
784 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700785 return getBE().mHwcLayers.at(displayId).clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800786}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800787
Dan Stozacac35382016-01-27 12:21:06 -0800788bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
789 if (point->getFrameNumber() <= mCurrentFrameNumber) {
790 // Don't bother with a SyncPoint, since we've already latched the
791 // relevant frame
792 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700793 }
794
Dan Stozacac35382016-01-27 12:21:06 -0800795 Mutex::Autolock lock(mLocalSyncPointMutex);
796 mLocalSyncPoints.push_back(point);
797 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700798}
799
Mathias Agopian13127d82013-03-05 17:47:11 -0800800void Layer::setFiltering(bool filtering) {
801 mFiltering = filtering;
802}
803
804bool Layer::getFiltering() const {
805 return mFiltering;
806}
807
Mathias Agopian13127d82013-03-05 17:47:11 -0800808// ----------------------------------------------------------------------------
809// local state
810// ----------------------------------------------------------------------------
811
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000812static void boundPoint(vec2* point, const Rect& crop) {
813 if (point->x < crop.left) {
814 point->x = crop.left;
815 }
816 if (point->x > crop.right) {
817 point->x = crop.right;
818 }
819 if (point->y < crop.top) {
820 point->y = crop.top;
821 }
822 if (point->y > crop.bottom) {
823 point->y = crop.bottom;
824 }
825}
826
chaviwa76b2712017-09-20 12:02:26 -0700827void Layer::computeGeometry(const RenderArea& renderArea, Mesh& mesh,
828 bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700829 const Layer::State& s(getDrawingState());
chaviwa76b2712017-09-20 12:02:26 -0700830 const Transform renderAreaTransform(renderArea.getTransform());
831 const uint32_t height = renderArea.getHeight();
Dan Stoza80d61162017-12-20 15:57:52 -0800832 FloatRect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700833
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000834 vec2 lt = vec2(win.left, win.top);
835 vec2 lb = vec2(win.left, win.bottom);
836 vec2 rb = vec2(win.right, win.bottom);
837 vec2 rt = vec2(win.right, win.top);
838
Robert Carr1f0a16a2016-10-24 16:27:39 -0700839 Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000840 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700841 lt = layerTransform.transform(lt);
842 lb = layerTransform.transform(lb);
843 rb = layerTransform.transform(rb);
844 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000845 }
846
Marissa Wallf58c14b2018-07-24 10:50:43 -0700847 if (!s.finalCrop_legacy.isEmpty()) {
848 boundPoint(&lt, s.finalCrop_legacy);
849 boundPoint(&lb, s.finalCrop_legacy);
850 boundPoint(&rb, s.finalCrop_legacy);
851 boundPoint(&rt, s.finalCrop_legacy);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000852 }
853
Mathias Agopianff2ed702013-09-01 21:36:12 -0700854 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700855 position[0] = renderAreaTransform.transform(lt);
856 position[1] = renderAreaTransform.transform(lb);
857 position[2] = renderAreaTransform.transform(rb);
858 position[3] = renderAreaTransform.transform(rt);
David Sodman41fdfc92017-11-06 16:09:56 -0800859 for (size_t i = 0; i < 4; i++) {
chaviwa76b2712017-09-20 12:02:26 -0700860 position[i].y = height - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -0800861 }
862}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800863
David Sodman41fdfc92017-11-06 16:09:56 -0800864bool Layer::isSecure() const {
Dan Stoza23116082015-06-18 14:58:39 -0700865 const Layer::State& s(mDrawingState);
866 return (s.flags & layer_state_t::eLayerSecure);
867}
868
Mathias Agopian13127d82013-03-05 17:47:11 -0800869void Layer::setVisibleRegion(const Region& visibleRegion) {
870 // always called from main thread
871 this->visibleRegion = visibleRegion;
872}
873
874void Layer::setCoveredRegion(const Region& coveredRegion) {
875 // always called from main thread
876 this->coveredRegion = coveredRegion;
877}
878
David Sodman41fdfc92017-11-06 16:09:56 -0800879void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800880 // always called from main thread
881 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
882}
883
Robert Carre5f4f692018-01-12 13:12:28 -0800884void Layer::clearVisibilityRegions() {
885 visibleRegion.clear();
886 visibleNonTransparentRegion.clear();
887 coveredRegion.clear();
888}
889
Mathias Agopian13127d82013-03-05 17:47:11 -0800890// ----------------------------------------------------------------------------
891// transaction
892// ----------------------------------------------------------------------------
893
Dan Stoza7dde5992015-05-22 09:51:44 -0700894void Layer::pushPendingState() {
895 if (!mCurrentState.modified) {
896 return;
897 }
898
Dan Stoza7dde5992015-05-22 09:51:44 -0700899 // If this transaction is waiting on the receipt of a frame, generate a sync
900 // point and send it to the remote layer.
Marissa Wallf58c14b2018-07-24 10:50:43 -0700901 if (mCurrentState.barrierLayer_legacy != nullptr) {
902 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800903 if (barrierLayer == nullptr) {
904 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700905 // If we can't promote the layer we are intended to wait on,
906 // then it is expired or otherwise invalid. Allow this transaction
907 // to be applied as per normal (no synchronization).
Marissa Wallf58c14b2018-07-24 10:50:43 -0700908 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800909 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700910 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy);
Robert Carr0d480722017-01-10 16:42:54 -0800911 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800912 mRemoteSyncPoints.push_back(std::move(syncPoint));
913 } else {
914 // We already missed the frame we're supposed to synchronize
915 // on, so go ahead and apply the state update
Marissa Wallf58c14b2018-07-24 10:50:43 -0700916 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800917 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700918 }
919
Dan Stoza7dde5992015-05-22 09:51:44 -0700920 // Wake us up to check if the frame has been received
921 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700922 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700923 }
924 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700925 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700926}
927
Pablo Ceballos05289c22016-04-14 15:49:55 -0700928void Layer::popPendingState(State* stateToCommit) {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700929 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700930
931 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700932 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700933}
934
Pablo Ceballos05289c22016-04-14 15:49:55 -0700935bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700936 bool stateUpdateAvailable = false;
937 while (!mPendingStates.empty()) {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700938 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700939 if (mRemoteSyncPoints.empty()) {
940 // If we don't have a sync point for this, apply it anyway. It
941 // will be visually wrong, but it should keep us from getting
942 // into too much trouble.
943 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700944 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700945 stateUpdateAvailable = true;
946 continue;
947 }
948
Marissa Wallf58c14b2018-07-24 10:50:43 -0700949 if (mRemoteSyncPoints.front()->getFrameNumber() !=
950 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800951 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800952
953 // Signal our end of the sync point and then dispose of it
954 mRemoteSyncPoints.front()->setTransactionApplied();
955 mRemoteSyncPoints.pop_front();
956 continue;
957 }
958
Dan Stoza7dde5992015-05-22 09:51:44 -0700959 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
960 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700961 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700962 stateUpdateAvailable = true;
963
964 // Signal our end of the sync point and then dispose of it
965 mRemoteSyncPoints.front()->setTransactionApplied();
966 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800967 } else {
968 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700969 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700970 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700971 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700972 stateUpdateAvailable = true;
973 }
974 }
975
976 // If we still have pending updates, wake SurfaceFlinger back up and point
977 // it at this layer so we can process them
978 if (!mPendingStates.empty()) {
979 setTransactionFlags(eTransactionNeeded);
980 mFlinger->setTransactionFlags(eTraversalNeeded);
981 }
982
983 mCurrentState.modified = false;
984 return stateUpdateAvailable;
985}
986
Mathias Agopian13127d82013-03-05 17:47:11 -0800987uint32_t Layer::doTransaction(uint32_t flags) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800988 ATRACE_CALL();
989
Dan Stoza7dde5992015-05-22 09:51:44 -0700990 pushPendingState();
Pablo Ceballos05289c22016-04-14 15:49:55 -0700991 Layer::State c = getCurrentState();
992 if (!applyPendingStates(&c)) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700993 return 0;
994 }
995
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700996 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800997
Marissa Wallf58c14b2018-07-24 10:50:43 -0700998 const bool sizeChanged = (c.requested_legacy.w != s.requested_legacy.w) ||
999 (c.requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -07001000
David Sodmaneb085e02017-10-05 18:49:04 -07001001 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001002 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +00001003 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -08001004 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
1005 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1006 " requested={ wh={%4u,%4u} }}\n"
1007 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1008 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -07001009 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
1010 c.active_legacy.w, c.active_legacy.h, c.crop_legacy.left, c.crop_legacy.top,
1011 c.crop_legacy.right, c.crop_legacy.bottom, c.crop_legacy.getWidth(),
1012 c.crop_legacy.getHeight(), c.requested_legacy.w, c.requested_legacy.h,
1013 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
1014 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
1015 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001016
Jamie Gennis2a0d5b62011-09-26 16:54:44 -07001017 // record the new size, form this point on, when the client request
1018 // a buffer, it'll get the new size.
Marissa Wallf58c14b2018-07-24 10:50:43 -07001019 setDefaultBufferSize(c.requested_legacy.w, c.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001020 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001021
Robert Carre392b552017-09-19 12:16:05 -07001022 // Don't let Layer::doTransaction update the drawing state
1023 // if we have a pending resize, unless we are in fixed-size mode.
1024 // the drawing state will be updated only once we receive a buffer
1025 // with the correct size.
1026 //
1027 // In particular, we want to make sure the clip (which is part
1028 // of the geometry state) is latched together with the size but is
1029 // latched immediately when no resizing is involved.
1030 //
1031 // If a sideband stream is attached, however, we want to skip this
1032 // optimization so that transactions aren't missed when a buffer
1033 // never arrives
1034 //
1035 // In the case that we don't have a buffer we ignore other factors
1036 // and avoid entering the resizePending state. At a high level the
1037 // resizePending state is to avoid applying the state of the new buffer
1038 // to the old buffer. However in the state where we don't have an old buffer
1039 // there is no such concern but we may still be being used as a parent layer.
Marissa Wallf58c14b2018-07-24 10:50:43 -07001040 const bool resizePending = ((c.requested_legacy.w != c.active_legacy.w) ||
1041 (c.requested_legacy.h != c.active_legacy.h)) &&
David Sodman0cc69182017-11-17 12:12:07 -08001042 (getBE().compositionInfo.mBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001043 if (!isFixedSize()) {
David Sodman0cc69182017-11-17 12:12:07 -08001044 if (resizePending && getBE().compositionInfo.hwc.sidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001045 flags |= eDontUpdateGeometryState;
1046 }
1047 }
1048
Robert Carr7bf247e2017-05-18 14:02:49 -07001049 // Here we apply various requested geometry states, depending on our
1050 // latching configuration. See Layer.h for a detailed discussion of
1051 // how geometry latching is controlled.
1052 if (!(flags & eDontUpdateGeometryState)) {
Pablo Ceballos7d052572016-06-02 17:46:05 -07001053 Layer::State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001054
1055 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1056 // mode, which causes attributes which normally latch regardless of scaling mode,
1057 // to be delayed. We copy the requested state to the active state making sure
1058 // to respect these rules (again see Layer.h for a detailed discussion).
1059 //
1060 // There is an awkward asymmetry in the handling of the crop states in the position
1061 // states, as can be seen below. Largely this arises from position and transform
1062 // being stored in the same data structure while having different latching rules.
1063 // b/38182305
1064 //
1065 // Careful that "c" and editCurrentState may not begin as equivalent due to
1066 // applyPendingStates in the presence of deferred transactions.
1067 if (mFreezeGeometryUpdates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001068 float tx = c.active_legacy.transform.tx();
1069 float ty = c.active_legacy.transform.ty();
1070 c.active_legacy = c.requested_legacy;
1071 c.active_legacy.transform.set(tx, ty);
1072 editCurrentState.active_legacy = c.active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001073 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001074 editCurrentState.active_legacy = editCurrentState.requested_legacy;
1075 c.active_legacy = c.requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001076 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001077 }
1078
Marissa Wallf58c14b2018-07-24 10:50:43 -07001079 if (s.active_legacy != c.active_legacy) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001080 // invalidate and recompute the visible regions if needed
1081 flags |= Layer::eVisibleRegion;
1082 }
1083
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001084 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001085 // invalidate and recompute the visible regions if needed
1086 flags |= eVisibleRegion;
1087 this->contentDirty = true;
1088
1089 // we may use linear filtering, if the matrix scales us
Marissa Wallf58c14b2018-07-24 10:50:43 -07001090 const uint8_t type = c.active_legacy.transform.getType();
1091 mNeedsFiltering =
1092 (!c.active_legacy.transform.preserveRects() || (type >= Transform::SCALE));
Mathias Agopian13127d82013-03-05 17:47:11 -08001093 }
1094
Dan Stozac8145172016-04-28 16:29:06 -07001095 // If the layer is hidden, signal and clear out all local sync points so
1096 // that transactions for layers depending on this layer's frames becoming
1097 // visible are not blocked
1098 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001099 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001100 }
1101
Mathias Agopian13127d82013-03-05 17:47:11 -08001102 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001103 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001104 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001105}
1106
Pablo Ceballos05289c22016-04-14 15:49:55 -07001107void Layer::commitTransaction(const State& stateToCommit) {
1108 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001109}
1110
Mathias Agopian13127d82013-03-05 17:47:11 -08001111uint32_t Layer::getTransactionFlags(uint32_t flags) {
1112 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1113}
1114
1115uint32_t Layer::setTransactionFlags(uint32_t flags) {
1116 return android_atomic_or(flags, &mTransactionFlags);
1117}
1118
Robert Carr82364e32016-05-15 11:27:47 -07001119bool Layer::setPosition(float x, float y, bool immediate) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001120 if (mCurrentState.requested_legacy.transform.tx() == x &&
1121 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001122 return false;
1123 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001124
1125 // We update the requested and active position simultaneously because
1126 // we want to apply the position portion of the transform matrix immediately,
1127 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Marissa Wallf58c14b2018-07-24 10:50:43 -07001128 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001129 if (immediate && !mFreezeGeometryUpdates) {
1130 // Here we directly update the active state
1131 // unlike other setters, because we store it within
1132 // the transform, but use different latching rules.
1133 // b/38182305
Marissa Wallf58c14b2018-07-24 10:50:43 -07001134 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001135 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001136 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001137
Dan Stoza7dde5992015-05-22 09:51:44 -07001138 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001139 setTransactionFlags(eTransactionNeeded);
1140 return true;
1141}
Robert Carr82364e32016-05-15 11:27:47 -07001142
Robert Carr1f0a16a2016-10-24 16:27:39 -07001143bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1144 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1145 if (idx < 0) {
1146 return false;
1147 }
1148 if (childLayer->setLayer(z)) {
1149 mCurrentChildren.removeAt(idx);
1150 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001151 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001152 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001153 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001154}
1155
Robert Carr503c7042017-09-27 15:06:08 -07001156bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1157 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1158 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1159 if (idx < 0) {
1160 return false;
1161 }
1162 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1163 mCurrentChildren.removeAt(idx);
1164 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001165 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001166 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001167 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001168}
1169
Robert Carrae060832016-11-28 10:51:00 -08001170bool Layer::setLayer(int32_t z) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001171 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001172 mCurrentState.sequence++;
1173 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001174 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001175
1176 // Discard all relative layering.
1177 if (mCurrentState.zOrderRelativeOf != nullptr) {
1178 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1179 if (strongRelative != nullptr) {
1180 strongRelative->removeZOrderRelative(this);
1181 }
1182 mCurrentState.zOrderRelativeOf = nullptr;
1183 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001184 setTransactionFlags(eTransactionNeeded);
1185 return true;
1186}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001187
Robert Carrdb66e622017-04-10 16:55:57 -07001188void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1189 mCurrentState.zOrderRelatives.remove(relative);
1190 mCurrentState.sequence++;
1191 mCurrentState.modified = true;
1192 setTransactionFlags(eTransactionNeeded);
1193}
1194
1195void Layer::addZOrderRelative(const wp<Layer>& relative) {
1196 mCurrentState.zOrderRelatives.add(relative);
1197 mCurrentState.modified = true;
1198 mCurrentState.sequence++;
1199 setTransactionFlags(eTransactionNeeded);
1200}
1201
Robert Carr503d2bd2017-12-04 15:49:47 -08001202bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001203 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1204 if (handle == nullptr) {
1205 return false;
1206 }
1207 sp<Layer> relative = handle->owner.promote();
1208 if (relative == nullptr) {
1209 return false;
1210 }
1211
Robert Carr503d2bd2017-12-04 15:49:47 -08001212 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1213 mCurrentState.zOrderRelativeOf == relative) {
1214 return false;
1215 }
1216
Robert Carrdb66e622017-04-10 16:55:57 -07001217 mCurrentState.sequence++;
1218 mCurrentState.modified = true;
Robert Carr503d2bd2017-12-04 15:49:47 -08001219 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001220
chaviw9ab4bd12017-11-03 13:11:00 -07001221 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
1222 if (oldZOrderRelativeOf != nullptr) {
1223 oldZOrderRelativeOf->removeZOrderRelative(this);
1224 }
Robert Carrdb66e622017-04-10 16:55:57 -07001225 mCurrentState.zOrderRelativeOf = relative;
1226 relative->addZOrderRelative(this);
1227
1228 setTransactionFlags(eTransactionNeeded);
1229
1230 return true;
1231}
1232
Mathias Agopian13127d82013-03-05 17:47:11 -08001233bool Layer::setSize(uint32_t w, uint32_t h) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001234 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
1235 return false;
1236 mCurrentState.requested_legacy.w = w;
1237 mCurrentState.requested_legacy.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001238 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001239 setTransactionFlags(eTransactionNeeded);
1240 return true;
1241}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001242bool Layer::setAlpha(float alpha) {
David Sodman41fdfc92017-11-06 16:09:56 -08001243 if (mCurrentState.color.a == alpha) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001244 mCurrentState.sequence++;
chaviw13fdc492017-06-27 12:40:18 -07001245 mCurrentState.color.a = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001246 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001247 setTransactionFlags(eTransactionNeeded);
1248 return true;
1249}
chaviw13fdc492017-06-27 12:40:18 -07001250
1251bool Layer::setColor(const half3& color) {
David Sodman41fdfc92017-11-06 16:09:56 -08001252 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g &&
1253 color.b == mCurrentState.color.b)
chaviw13fdc492017-06-27 12:40:18 -07001254 return false;
1255
1256 mCurrentState.sequence++;
1257 mCurrentState.color.r = color.r;
1258 mCurrentState.color.g = color.g;
1259 mCurrentState.color.b = color.b;
1260 mCurrentState.modified = true;
1261 setTransactionFlags(eTransactionNeeded);
1262 return true;
1263}
1264
Robert Carrd4ae7f32018-06-07 16:10:57 -07001265bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1266 bool allowNonRectPreservingTransforms) {
1267 Transform t;
1268 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1269
1270 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1271 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1272 return false;
1273 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001274 mCurrentState.sequence++;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001275 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1276 matrix.dsdy);
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}
1281bool Layer::setTransparentRegionHint(const Region& transparent) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001282 mCurrentState.requestedTransparentRegion_legacy = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001283 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001284 setTransactionFlags(eTransactionNeeded);
1285 return true;
1286}
1287bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1288 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
David Sodman41fdfc92017-11-06 16:09:56 -08001289 if (mCurrentState.flags == newFlags) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001290 mCurrentState.sequence++;
1291 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001292 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001293 setTransactionFlags(eTransactionNeeded);
1294 return true;
1295}
Robert Carr99e27f02016-06-16 15:18:02 -07001296
Marissa Wallf58c14b2018-07-24 10:50:43 -07001297bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
1298 if (mCurrentState.requestedCrop_legacy == crop) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001299 mCurrentState.sequence++;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001300 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001301 if (immediate && !mFreezeGeometryUpdates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001302 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001303 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001304 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1305
Dan Stoza7dde5992015-05-22 09:51:44 -07001306 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001307 setTransactionFlags(eTransactionNeeded);
1308 return true;
1309}
Robert Carr8d5227b2017-03-16 15:41:03 -07001310
Marissa Wallf58c14b2018-07-24 10:50:43 -07001311bool Layer::setFinalCrop_legacy(const Rect& crop, bool immediate) {
1312 if (mCurrentState.requestedFinalCrop_legacy == crop) return false;
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001313 mCurrentState.sequence++;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001314 mCurrentState.requestedFinalCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001315 if (immediate && !mFreezeGeometryUpdates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001316 mCurrentState.finalCrop_legacy = crop;
Robert Carr8d5227b2017-03-16 15:41:03 -07001317 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001318 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1319
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001320 mCurrentState.modified = true;
1321 setTransactionFlags(eTransactionNeeded);
1322 return true;
1323}
Mathias Agopian13127d82013-03-05 17:47:11 -08001324
Robert Carrc3574f72016-03-24 12:19:32 -07001325bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001326 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001327 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001328 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001329 return true;
1330}
1331
rongliucfb187b2018-03-14 12:26:23 -07001332void Layer::setInfo(int32_t type, int32_t appId) {
David Sodman41fdfc92017-11-06 16:09:56 -08001333 mCurrentState.appId = appId;
1334 mCurrentState.type = type;
1335 mCurrentState.modified = true;
1336 setTransactionFlags(eTransactionNeeded);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001337}
1338
Mathias Agopian13127d82013-03-05 17:47:11 -08001339bool Layer::setLayerStack(uint32_t layerStack) {
David Sodman41fdfc92017-11-06 16:09:56 -08001340 if (mCurrentState.layerStack == layerStack) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001341 mCurrentState.sequence++;
1342 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001343 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001344 setTransactionFlags(eTransactionNeeded);
1345 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001346}
1347
Robert Carr1f0a16a2016-10-24 16:27:39 -07001348uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001349 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001350 if (p == nullptr) {
1351 return getDrawingState().layerStack;
1352 }
1353 return p->getLayerStack();
1354}
1355
Marissa Wallf58c14b2018-07-24 10:50:43 -07001356void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
1357 mCurrentState.barrierLayer_legacy = barrierLayer;
1358 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001359 // We don't set eTransactionNeeded, because just receiving a deferral
1360 // request without any other state updates shouldn't actually induce a delay
1361 mCurrentState.modified = true;
1362 pushPendingState();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001363 mCurrentState.barrierLayer_legacy = nullptr;
1364 mCurrentState.frameNumber_legacy = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001365 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001366}
1367
Marissa Wallf58c14b2018-07-24 10:50:43 -07001368void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001369 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001370 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001371}
1372
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001373// ----------------------------------------------------------------------------
1374// pageflip handling...
1375// ----------------------------------------------------------------------------
1376
Robert Carr1f0a16a2016-10-24 16:27:39 -07001377bool Layer::isHiddenByPolicy() const {
1378 const Layer::State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001379 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001380 if (parent != nullptr && parent->isHiddenByPolicy()) {
1381 return true;
1382 }
1383 return s.flags & layer_state_t::eLayerHidden;
1384}
1385
David Sodman41fdfc92017-11-06 16:09:56 -08001386uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001387 // TODO: should we do something special if mSecure is set?
1388 if (mProtectedByApp) {
1389 // need a hardware-protected path to external video sink
1390 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001391 }
Riley Andrews03414a12014-07-01 14:22:59 -07001392 if (mPotentialCursor) {
1393 usage |= GraphicBuffer::USAGE_CURSOR;
1394 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001395 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001396 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001397}
1398
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001399void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001400 uint32_t orientation = 0;
1401 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001402 // The transform hint is used to improve performance, but we can
1403 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001404 // apply to all displays.
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001405 const Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001406 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07001407 if (orientation & Transform::ROT_INVALID) {
1408 orientation = 0;
1409 }
1410 }
David Sodmaneb085e02017-10-05 18:49:04 -07001411 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001412}
1413
Mathias Agopian13127d82013-03-05 17:47:11 -08001414// ----------------------------------------------------------------------------
1415// debugging
1416// ----------------------------------------------------------------------------
1417
Kalle Raitaa099a242017-01-11 11:17:29 -08001418LayerDebugInfo Layer::getLayerDebugInfo() const {
1419 LayerDebugInfo info;
1420 const Layer::State& ds = getDrawingState();
1421 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001422 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001423 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
1424 info.mType = String8(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001425 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001426 info.mVisibleRegion = visibleRegion;
1427 info.mSurfaceDamageRegion = surfaceDamageRegion;
1428 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001429 info.mX = ds.active_legacy.transform.tx();
1430 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001431 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001432 info.mWidth = ds.active_legacy.w;
1433 info.mHeight = ds.active_legacy.h;
1434 info.mCrop = ds.crop_legacy;
1435 info.mFinalCrop = ds.finalCrop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001436 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001437 info.mFlags = ds.flags;
1438 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001439 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001440 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1441 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1442 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1443 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001444 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001445 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001446 if (buffer != 0) {
1447 info.mActiveBufferWidth = buffer->getWidth();
1448 info.mActiveBufferHeight = buffer->getHeight();
1449 info.mActiveBufferStride = buffer->getStride();
1450 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001451 } else {
1452 info.mActiveBufferWidth = 0;
1453 info.mActiveBufferHeight = 0;
1454 info.mActiveBufferStride = 0;
1455 info.mActiveBufferFormat = 0;
1456 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001457 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001458 info.mNumQueuedFrames = getQueuedFrameCount();
1459 info.mRefreshPending = isBufferLatched();
1460 info.mIsOpaque = isOpaque(ds);
1461 info.mContentDirty = contentDirty;
1462 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001463}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001464
Dan Stozae22aec72016-08-01 13:20:59 -07001465void Layer::miniDumpHeader(String8& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001466 result.append("-------------------------------");
1467 result.append("-------------------------------");
1468 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001469 result.append(" Layer name\n");
1470 result.append(" Z | ");
1471 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001472 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001473 result.append(" Disp Frame (LTRB) | ");
1474 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001475 result.append("-------------------------------");
1476 result.append("-------------------------------");
1477 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001478}
1479
Dominik Laskowski7e045462018-05-30 13:02:02 -07001480void Layer::miniDump(String8& result, int32_t displayId) const {
1481 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stozae22aec72016-08-01 13:20:59 -07001482 return;
1483 }
1484
1485 String8 name;
1486 if (mName.length() > 77) {
1487 std::string shortened;
1488 shortened.append(mName.string(), 36);
1489 shortened.append("[...]");
1490 shortened.append(mName.string() + (mName.length() - 36), 36);
1491 name = shortened.c_str();
1492 } else {
1493 name = mName;
1494 }
1495
1496 result.appendFormat(" %s\n", name.string());
1497
1498 const Layer::State& layerState(getDrawingState());
Lloyd Pique074e8122018-07-26 12:57:23 -07001499 const LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers.at(displayId);
Chia-I Wu1e043612018-03-01 09:45:09 -08001500 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
1501 result.appendFormat(" rel %6d | ", layerState.z);
1502 } else {
1503 result.appendFormat(" %10d | ", layerState.z);
1504 }
Dominik Laskowski7e045462018-05-30 13:02:02 -07001505 result.appendFormat("%10s | ", to_string(getCompositionType(displayId)).c_str());
Lloyd Pique074e8122018-07-26 12:57:23 -07001506 result.appendFormat("%10s | ", to_string(hwcInfo.transform).c_str());
1507 const Rect& frame = hwcInfo.displayFrame;
David Sodman41fdfc92017-11-06 16:09:56 -08001508 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique074e8122018-07-26 12:57:23 -07001509 const FloatRect& crop = hwcInfo.sourceCrop;
David Sodman41fdfc92017-11-06 16:09:56 -08001510 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 -07001511
Yichi Chen6ca35192018-05-29 12:20:43 +08001512 result.append("- - - - - - - - - - - - - - - -");
1513 result.append("- - - - - - - - - - - - - - - -");
1514 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001515}
Dan Stozae22aec72016-08-01 13:20:59 -07001516
Svetoslavd85084b2014-03-20 10:28:31 -07001517void Layer::dumpFrameStats(String8& result) const {
1518 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001519}
1520
Svetoslavd85084b2014-03-20 10:28:31 -07001521void Layer::clearFrameStats() {
1522 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001523}
1524
Jamie Gennis6547ff42013-07-16 20:12:42 -07001525void Layer::logFrameStats() {
1526 mFrameTracker.logAndResetStats(mName);
1527}
1528
Svetoslavd85084b2014-03-20 10:28:31 -07001529void Layer::getFrameStats(FrameStats* outStats) const {
1530 mFrameTracker.getStats(outStats);
1531}
1532
Brian Andersond6927fb2016-07-23 23:37:30 -07001533void Layer::dumpFrameEvents(String8& result) {
David Sodman41fdfc92017-11-06 16:09:56 -08001534 result.appendFormat("- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001535 Mutex::Autolock lock(mFrameEventHistoryMutex);
1536 mFrameEventHistory.checkFencesForCompletion();
1537 mFrameEventHistory.dump(result);
1538}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001539
Brian Anderson5ea5e592016-12-01 16:54:33 -08001540void Layer::onDisconnect() {
1541 Mutex::Autolock lock(mFrameEventHistoryMutex);
1542 mFrameEventHistory.onDisconnect();
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001543 mTimeStats.onDisconnect(getName().c_str());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001544}
1545
Brian Anderson3890c392016-07-25 12:48:08 -07001546void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001547 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001548 if (newTimestamps) {
1549 mTimeStats.setPostTime(getName().c_str(), newTimestamps->frameNumber,
1550 newTimestamps->postedTime);
1551 }
1552
Brian Andersond6927fb2016-07-23 23:37:30 -07001553 Mutex::Autolock lock(mFrameEventHistoryMutex);
1554 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001555 // If there are any unsignaled fences in the aquire timeline at this
1556 // point, the previously queued frame hasn't been latched yet. Go ahead
1557 // and try to get the signal time here so the syscall is taken out of
1558 // the main thread's critical path.
1559 mAcquireTimeline.updateSignalTimes();
1560 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001561 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001562 mFrameEventHistory.addQueue(*newTimestamps);
1563 }
1564
Brian Anderson3890c392016-07-25 12:48:08 -07001565 if (outDelta) {
1566 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001567 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001568}
Dan Stozae77c7662016-05-13 11:37:28 -07001569
Chia-I Wu98f1c102017-05-30 14:54:08 -07001570size_t Layer::getChildrenCount() const {
1571 size_t count = 0;
1572 for (const sp<Layer>& child : mCurrentChildren) {
1573 count += 1 + child->getChildrenCount();
1574 }
1575 return count;
1576}
1577
Robert Carr1f0a16a2016-10-24 16:27:39 -07001578void Layer::addChild(const sp<Layer>& layer) {
1579 mCurrentChildren.add(layer);
1580 layer->setParent(this);
1581}
1582
1583ssize_t Layer::removeChild(const sp<Layer>& layer) {
1584 layer->setParent(nullptr);
1585 return mCurrentChildren.remove(layer);
1586}
1587
Robert Carr1db73f62016-12-21 12:58:51 -08001588bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1589 sp<Handle> handle = nullptr;
1590 sp<Layer> newParent = nullptr;
1591 if (newParentHandle == nullptr) {
1592 return false;
1593 }
1594 handle = static_cast<Handle*>(newParentHandle.get());
1595 newParent = handle->owner.promote();
1596 if (newParent == nullptr) {
1597 ALOGE("Unable to promote Layer handle");
1598 return false;
1599 }
1600
1601 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001602 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001603
1604 sp<Client> client(child->mClientRef.promote());
1605 if (client != nullptr) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001606 client->updateParent(newParent);
Robert Carr1db73f62016-12-21 12:58:51 -08001607 }
1608 }
1609 mCurrentChildren.clear();
1610
1611 return true;
1612}
1613
Robert Carr15eae092018-03-23 13:43:53 -07001614void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001615 for (const sp<Layer>& child : mDrawingChildren) {
1616 child->mDrawingParent = newParent;
1617 }
1618}
1619
chaviwf1961f72017-09-18 16:41:07 -07001620bool Layer::reparent(const sp<IBinder>& newParentHandle) {
1621 if (newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001622 return false;
1623 }
1624
1625 auto handle = static_cast<Handle*>(newParentHandle.get());
1626 sp<Layer> newParent = handle->owner.promote();
1627 if (newParent == nullptr) {
1628 ALOGE("Unable to promote Layer handle");
1629 return false;
1630 }
1631
chaviwf1961f72017-09-18 16:41:07 -07001632 sp<Layer> parent = getParent();
1633 if (parent != nullptr) {
1634 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001635 }
chaviwf1961f72017-09-18 16:41:07 -07001636 newParent->addChild(this);
chaviw06178942017-07-27 10:25:59 -07001637
chaviwf1961f72017-09-18 16:41:07 -07001638 sp<Client> client(mClientRef.promote());
chaviw06178942017-07-27 10:25:59 -07001639 sp<Client> newParentClient(newParent->mClientRef.promote());
1640
chaviwf1961f72017-09-18 16:41:07 -07001641 if (client != newParentClient) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001642 client->updateParent(newParent);
chaviw06178942017-07-27 10:25:59 -07001643 }
1644
chaviw06178942017-07-27 10:25:59 -07001645 return true;
1646}
1647
Robert Carr9524cb32017-02-13 11:32:32 -08001648bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001649 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001650 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001651 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001652 if (client != nullptr && parentClient != client) {
Robert Carr7f619b22017-11-06 12:56:35 -08001653 client->detachLayer(child.get());
1654 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001655 }
Robert Carr7f619b22017-11-06 12:56:35 -08001656 }
Robert Carr9524cb32017-02-13 11:32:32 -08001657
1658 return true;
1659}
1660
Chia-I Wu11481472018-05-04 10:43:19 -07001661bool Layer::isLegacyDataSpace() const {
1662 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001663 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001664 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001665}
1666
Robert Carr1f0a16a2016-10-24 16:27:39 -07001667void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001668 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001669}
1670
1671void Layer::clearSyncPoints() {
1672 for (const auto& child : mCurrentChildren) {
1673 child->clearSyncPoints();
1674 }
1675
1676 Mutex::Autolock lock(mLocalSyncPointMutex);
1677 for (auto& point : mLocalSyncPoints) {
1678 point->setFrameAvailable();
1679 }
1680 mLocalSyncPoints.clear();
1681}
1682
1683int32_t Layer::getZ() const {
1684 return mDrawingState.z;
1685}
1686
Robert Carr29abff82017-12-04 13:51:20 -08001687bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1688 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1689 const State& state = useDrawing ? mDrawingState : mCurrentState;
1690 return state.zOrderRelativeOf != nullptr;
1691}
1692
David Sodman41fdfc92017-11-06 16:09:56 -08001693__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001694 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001695 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1696 "makeTraversalList received invalid stateSet");
1697 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1698 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1699 const State& state = useDrawing ? mDrawingState : mCurrentState;
1700
Robert Carr29abff82017-12-04 13:51:20 -08001701 if (state.zOrderRelatives.size() == 0) {
1702 *outSkipRelativeZUsers = true;
1703 return children;
1704 }
1705
chaviwfd462612018-05-31 16:11:27 -07001706 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001707 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001708 sp<Layer> strongRelative = weakRelative.promote();
1709 if (strongRelative != nullptr) {
1710 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001711 }
1712 }
1713
Dan Stoza412903f2017-04-27 13:42:17 -07001714 for (const sp<Layer>& child : children) {
Robert Carr503c7042017-09-27 15:06:08 -07001715 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1716 if (childState.zOrderRelativeOf != nullptr) {
1717 continue;
1718 }
Robert Carrdb66e622017-04-10 16:55:57 -07001719 traverse.add(child);
1720 }
1721
1722 return traverse;
1723}
1724
Robert Carr1f0a16a2016-10-24 16:27:39 -07001725/**
Robert Carrdb66e622017-04-10 16:55:57 -07001726 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001727 */
Dan Stoza412903f2017-04-27 13:42:17 -07001728void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001729 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1730 // produce a new list for traversing, including our relatives, and not including our children
1731 // who are relatives of another surface. In the case that there are no relative Z,
1732 // makeTraversalList returns our children directly to avoid significant overhead.
1733 // However in this case we need to take the responsibility for filtering children which
1734 // are relatives of another surface here.
1735 bool skipRelativeZUsers = false;
1736 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001737
Robert Carr1f0a16a2016-10-24 16:27:39 -07001738 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001739 for (; i < list.size(); i++) {
1740 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001741 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1742 continue;
1743 }
1744
Robert Carrdb66e622017-04-10 16:55:57 -07001745 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001746 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001747 }
Dan Stoza412903f2017-04-27 13:42:17 -07001748 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001749 }
Robert Carr29abff82017-12-04 13:51:20 -08001750
Dan Stoza412903f2017-04-27 13:42:17 -07001751 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001752 for (; i < list.size(); i++) {
1753 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001754
1755 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1756 continue;
1757 }
Dan Stoza412903f2017-04-27 13:42:17 -07001758 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001759 }
1760}
1761
1762/**
Robert Carrdb66e622017-04-10 16:55:57 -07001763 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001764 */
Dan Stoza412903f2017-04-27 13:42:17 -07001765void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1766 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001767 // See traverseInZOrder for documentation.
1768 bool skipRelativeZUsers = false;
1769 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001770
Robert Carr1f0a16a2016-10-24 16:27:39 -07001771 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001772 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001773 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001774
1775 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1776 continue;
1777 }
1778
Robert Carrdb66e622017-04-10 16:55:57 -07001779 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001780 break;
1781 }
Dan Stoza412903f2017-04-27 13:42:17 -07001782 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001783 }
Dan Stoza412903f2017-04-27 13:42:17 -07001784 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001785 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001786 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001787
1788 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1789 continue;
1790 }
1791
Dan Stoza412903f2017-04-27 13:42:17 -07001792 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001793 }
1794}
1795
chaviw4b129c22018-04-09 16:19:43 -07001796LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1797 const std::vector<Layer*>& layersInTree) {
1798 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1799 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001800 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1801 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
chaviw4b129c22018-04-09 16:19:43 -07001802 const State& state = useDrawing ? mDrawingState : mCurrentState;
1803
chaviwfd462612018-05-31 16:11:27 -07001804 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001805 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1806 sp<Layer> strongRelative = weakRelative.promote();
1807 // Only add relative layers that are also descendents of the top most parent of the tree.
1808 // If a relative layer is not a descendent, then it should be ignored.
1809 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1810 traverse.add(strongRelative);
1811 }
1812 }
1813
1814 for (const sp<Layer>& child : children) {
1815 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1816 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1817 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1818 // the child here since it won't be added later as a relative.
1819 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1820 childState.zOrderRelativeOf.promote().get())) {
1821 continue;
1822 }
1823 traverse.add(child);
1824 }
1825
1826 return traverse;
1827}
1828
1829void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1830 LayerVector::StateSet stateSet,
1831 const LayerVector::Visitor& visitor) {
1832 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001833
1834 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001835 for (; i < list.size(); i++) {
1836 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001837 if (relative->getZ() >= 0) {
1838 break;
1839 }
chaviw4b129c22018-04-09 16:19:43 -07001840 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001841 }
chaviw4b129c22018-04-09 16:19:43 -07001842
chaviwa76b2712017-09-20 12:02:26 -07001843 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001844 for (; i < list.size(); i++) {
1845 const auto& relative = list[i];
1846 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001847 }
1848}
1849
chaviw4b129c22018-04-09 16:19:43 -07001850std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1851 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1852 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1853
1854 std::vector<Layer*> layersInTree = {this};
1855 for (size_t i = 0; i < children.size(); i++) {
1856 const auto& child = children[i];
1857 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1858 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1859 }
1860
1861 return layersInTree;
1862}
1863
1864void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1865 const LayerVector::Visitor& visitor) {
1866 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1867 std::sort(layersInTree.begin(), layersInTree.end());
1868 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1869}
1870
Robert Carr1f0a16a2016-10-24 16:27:39 -07001871Transform Layer::getTransform() const {
1872 Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001873 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001874 if (p != nullptr) {
1875 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07001876
1877 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
1878 // it isFixedSize) then there may be additional scaling not accounted
1879 // for in the transform. We need to mirror this scaling in child surfaces
1880 // or we will break the contract where WM can treat child surfaces as
1881 // pixels in the parent surface.
David Sodman0cc69182017-11-17 12:12:07 -08001882 if (p->isFixedSize() && p->getBE().compositionInfo.mBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07001883 int bufferWidth;
1884 int bufferHeight;
1885 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
David Sodman0cc69182017-11-17 12:12:07 -08001886 bufferWidth = p->getBE().compositionInfo.mBuffer->getWidth();
1887 bufferHeight = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001888 } else {
David Sodman0cc69182017-11-17 12:12:07 -08001889 bufferHeight = p->getBE().compositionInfo.mBuffer->getWidth();
1890 bufferWidth = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001891 }
Marissa Wallf58c14b2018-07-24 10:50:43 -07001892 float sx = p->getDrawingState().active_legacy.w / static_cast<float>(bufferWidth);
1893 float sy = p->getDrawingState().active_legacy.h / static_cast<float>(bufferHeight);
Robert Carr9b429f42017-04-17 14:56:57 -07001894 Transform extraParentScaling;
1895 extraParentScaling.set(sx, 0, 0, sy);
1896 t = t * extraParentScaling;
1897 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001898 }
Marissa Wallf58c14b2018-07-24 10:50:43 -07001899 return t * getDrawingState().active_legacy.transform;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001900}
1901
chaviw13fdc492017-06-27 12:40:18 -07001902half Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001903 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07001904
chaviw13fdc492017-06-27 12:40:18 -07001905 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1906 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001907}
Robert Carr6452f122017-03-21 10:41:29 -07001908
chaviw13fdc492017-06-27 12:40:18 -07001909half4 Layer::getColor() const {
1910 const half4 color(getDrawingState().color);
1911 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001912}
Robert Carr6452f122017-03-21 10:41:29 -07001913
Robert Carr1f0a16a2016-10-24 16:27:39 -07001914void Layer::commitChildList() {
1915 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1916 const auto& child = mCurrentChildren[i];
1917 child->commitChildList();
1918 }
1919 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001920 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001921}
1922
chaviw1d044282017-09-27 12:19:28 -07001923void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
1924 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1925 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1926 const State& state = useDrawing ? mDrawingState : mCurrentState;
1927
Marissa Wallf58c14b2018-07-24 10:50:43 -07001928 Transform requestedTransform = state.active_legacy.transform;
chaviw1d044282017-09-27 12:19:28 -07001929 Transform transform = getTransform();
1930
1931 layerInfo->set_id(sequence);
1932 layerInfo->set_name(getName().c_str());
1933 layerInfo->set_type(String8(getTypeId()));
1934
1935 for (const auto& child : children) {
1936 layerInfo->add_children(child->sequence);
1937 }
1938
1939 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1940 sp<Layer> strongRelative = weakRelative.promote();
1941 if (strongRelative != nullptr) {
1942 layerInfo->add_relatives(strongRelative->sequence);
1943 }
1944 }
1945
Marissa Wallf58c14b2018-07-24 10:50:43 -07001946 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
chaviw1d044282017-09-27 12:19:28 -07001947 layerInfo->mutable_transparent_region());
1948 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
1949 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
1950
1951 layerInfo->set_layer_stack(getLayerStack());
1952 layerInfo->set_z(state.z);
1953
1954 PositionProto* position = layerInfo->mutable_position();
1955 position->set_x(transform.tx());
1956 position->set_y(transform.ty());
1957
1958 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
1959 requestedPosition->set_x(requestedTransform.tx());
1960 requestedPosition->set_y(requestedTransform.ty());
1961
1962 SizeProto* size = layerInfo->mutable_size();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001963 size->set_w(state.active_legacy.w);
1964 size->set_h(state.active_legacy.h);
chaviw1d044282017-09-27 12:19:28 -07001965
Marissa Wallf58c14b2018-07-24 10:50:43 -07001966 LayerProtoHelper::writeToProto(state.crop_legacy, layerInfo->mutable_crop());
1967 LayerProtoHelper::writeToProto(state.finalCrop_legacy, layerInfo->mutable_final_crop());
chaviw1d044282017-09-27 12:19:28 -07001968
1969 layerInfo->set_is_opaque(isOpaque(state));
1970 layerInfo->set_invalidate(contentDirty);
Chia-I Wu01591c92018-05-22 12:03:00 -07001971
1972 // XXX (b/79210409) mCurrentDataSpace is not protected
1973 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
1974
chaviw1d044282017-09-27 12:19:28 -07001975 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1976 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
1977 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
1978 layerInfo->set_flags(state.flags);
1979
1980 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1981 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
1982
Jorim Jaggi8e0af362017-11-14 16:28:28 +01001983 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07001984 if (parent != nullptr) {
1985 layerInfo->set_parent(parent->sequence);
1986 }
1987
1988 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1989 if (zOrderRelativeOf != nullptr) {
1990 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1991 }
1992
Chia-I Wu01591c92018-05-22 12:03:00 -07001993 // XXX getBE().compositionInfo.mBuffer is not protected
David Sodman0cc69182017-11-17 12:12:07 -08001994 auto buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001995 if (buffer != nullptr) {
1996 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
Yichi Chen6ca35192018-05-29 12:20:43 +08001997 LayerProtoHelper::writeToProto(Transform(mCurrentTransform),
1998 layerInfo->mutable_buffer_transform());
chaviw1d044282017-09-27 12:19:28 -07001999 }
2000
2001 layerInfo->set_queued_frames(getQueuedFrameCount());
2002 layerInfo->set_refresh_pending(isBufferLatched());
rongliucfb187b2018-03-14 12:26:23 -07002003 layerInfo->set_window_type(state.type);
2004 layerInfo->set_app_id(state.appId);
chaviwadc40c22018-07-10 16:57:27 -07002005 layerInfo->set_curr_frame(mCurrentFrameNumber);
2006
2007 for (const auto& pendingState : mPendingStates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07002008 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
chaviwadc40c22018-07-10 16:57:27 -07002009 if (barrierLayer != nullptr) {
2010 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
2011 barrierLayerProto->set_id(barrierLayer->sequence);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002012 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
chaviwadc40c22018-07-10 16:57:27 -07002013 }
2014 }
chaviw1d044282017-09-27 12:19:28 -07002015}
2016
Dominik Laskowski7e045462018-05-30 13:02:02 -07002017void Layer::writeToProto(LayerProto* layerInfo, int32_t displayId) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07002018 if (!hasHwcLayer(displayId)) {
2019 return;
2020 }
2021
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002022 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
2023
Dominik Laskowski7e045462018-05-30 13:02:02 -07002024 const auto& hwcInfo = getBE().mHwcLayers.at(displayId);
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002025
2026 const Rect& frame = hwcInfo.displayFrame;
2027 LayerProtoHelper::writeToProto(frame, layerInfo->mutable_hwc_frame());
2028
2029 const FloatRect& crop = hwcInfo.sourceCrop;
2030 LayerProtoHelper::writeToProto(crop, layerInfo->mutable_hwc_crop());
2031
2032 const int32_t transform = static_cast<int32_t>(hwcInfo.transform);
2033 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002034
2035 const int32_t compositionType = static_cast<int32_t>(hwcInfo.compositionType);
2036 layerInfo->set_hwc_composition_type(compositionType);
2037
2038 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
2039 static_cast<BufferLayer*>(this)->isProtected()) {
2040 layerInfo->set_is_protected(true);
2041 } else {
2042 layerInfo->set_is_protected(false);
2043 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002044}
2045
Mathias Agopian13127d82013-03-05 17:47:11 -08002046// ---------------------------------------------------------------------------
2047
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002048}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002049
2050#if defined(__gl_h_)
2051#error "don't include gl/gl.h in this file"
2052#endif
2053
2054#if defined(__gl2_h_)
2055#error "don't include gl2/gl2.h in this file"
2056#endif