blob: 7b681ea9033cd31ead2523ed5689b0bd2e424896 [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"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053
Mathias Agopian1b031492012-06-20 17:51:20 -070054#include "DisplayHardware/HWComposer.h"
55
Peiyong Lincbc184f2018-08-22 13:24:10 -070056#include <renderengine/RenderEngine.h>
Mathias Agopian875d8e12013-06-07 15:35:48 -070057
Dan Stozac5da2712016-07-20 15:38:12 -070058#include <mutex>
chaviw1d044282017-09-27 12:19:28 -070059#include "LayerProtoHelper.h"
Dan Stozac5da2712016-07-20 15:38:12 -070060
David Sodman41fdfc92017-11-06 16:09:56 -080061#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063namespace android {
64
Lloyd Piquef1c675b2018-09-12 20:45:39 -070065std::atomic<int32_t> Layer::sSequence{1};
Mathias Agopian13127d82013-03-05 17:47:11 -080066
Lloyd Pique42ab75e2018-09-12 20:46:03 -070067Layer::Layer(const LayerCreationArgs& args)
68 : mFlinger(args.flinger),
69 mName(args.name),
70 mClientRef(args.client),
71 mBE{this, args.name.string()} {
Mathias Agopiana67932f2011-04-20 14:20:59 -070072 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070073
74 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -070075 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
76 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
77 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070078
Dan Stozaf7ba41a2017-05-10 15:11:11 -070079 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070080
Lloyd Pique42ab75e2018-09-12 20:46:03 -070081 mCurrentState.active_legacy.w = args.w;
82 mCurrentState.active_legacy.h = args.h;
David Sodman0c69cad2017-08-21 12:12:51 -070083 mCurrentState.flags = layerFlags;
Marissa Wallf58c14b2018-07-24 10:50:43 -070084 mCurrentState.active_legacy.transform.set(0, 0);
85 mCurrentState.crop_legacy.makeInvalid();
Marissa Wallf58c14b2018-07-24 10:50:43 -070086 mCurrentState.requestedCrop_legacy = mCurrentState.crop_legacy;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070087 mCurrentState.z = 0;
chaviw13fdc492017-06-27 12:40:18 -070088 mCurrentState.color.a = 1.0f;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070089 mCurrentState.layerStack = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070090 mCurrentState.sequence = 0;
Marissa Wallf58c14b2018-07-24 10:50:43 -070091 mCurrentState.requested_legacy = mCurrentState.active_legacy;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -050092 mCurrentState.appId = 0;
93 mCurrentState.type = 0;
Marissa Wall61c58622018-07-18 10:12:20 -070094 mCurrentState.active.w = 0;
95 mCurrentState.active.h = 0;
96 mCurrentState.active.transform.set(0, 0);
97 mCurrentState.transform = 0;
98 mCurrentState.transformToDisplayInverse = false;
99 mCurrentState.crop.makeInvalid();
100 mCurrentState.acquireFence = new Fence(-1);
101 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
102 mCurrentState.hdrMetadata.validTypes = 0;
103 mCurrentState.surfaceDamageRegion.clear();
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700104 mCurrentState.cornerRadius = 0.0f;
Marissa Wall61c58622018-07-18 10:12:20 -0700105 mCurrentState.api = -1;
Peiyong Lin747321c2018-10-01 10:03:11 -0700106 mCurrentState.hasColorTransform = false;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700107
108 // drawing state & current state are identical
109 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700110
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800111 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700112 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800113 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200114 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700115
116 mFlinger->onLayerCreated();
Dan Stoza436ccf32018-06-21 12:10:12 -0700117}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700118
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700119Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800120 sp<Client> c(mClientRef.promote());
121 if (c != 0) {
122 c->detachLayer(this);
123 }
124
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000125 mFrameTracker.logAndResetStats(mName);
Robert Carr2e102c92018-10-23 12:11:15 -0700126
chaviw61626f22018-11-15 16:26:27 -0800127 destroyAllHwcLayersPlusChildren();
Robert Carr2e102c92018-10-23 12:11:15 -0700128
129 mFlinger->onLayerDestroyed();
Mathias Agopian96f08192010-06-02 23:28:45 -0700130}
131
Mathias Agopian13127d82013-03-05 17:47:11 -0800132// ---------------------------------------------------------------------------
133// callbacks
134// ---------------------------------------------------------------------------
135
David Sodmaneb085e02017-10-05 18:49:04 -0700136/*
137 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
138 * Layer. So, the implementation is done in BufferLayer. When called on a
139 * ColorLayer object, it's essentially a NOP.
140 */
David Sodmaneb085e02017-10-05 18:49:04 -0700141void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800142
Chia-I Wuc6657022017-08-15 11:18:17 -0700143void Layer::onRemovedFromCurrentState() {
Robert Carr2e102c92018-10-23 12:11:15 -0700144 mRemovedFromCurrentState = true;
145
Chia-I Wuc6657022017-08-15 11:18:17 -0700146 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
Robert Carr5edb1ad2017-04-25 10:54:24 -0700147 if (mCurrentState.zOrderRelativeOf != nullptr) {
148 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
149 if (strongRelative != nullptr) {
150 strongRelative->removeZOrderRelative(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700151 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700152 }
153 mCurrentState.zOrderRelativeOf = nullptr;
154 }
Rob Carr4bba3702018-10-08 21:53:30 +0000155
Robert Carr2e102c92018-10-23 12:11:15 -0700156 // Since we are no longer reachable from CurrentState SurfaceFlinger
157 // will no longer invoke doTransaction for us, and so we will
158 // never finish applying transactions. We signal the sync point
159 // now so that another layer will not become indefinitely
160 // blocked.
161 for (auto& point: mRemoteSyncPoints) {
162 point->setTransactionApplied();
163 }
164 mRemoteSyncPoints.clear();
165
166 {
167 Mutex::Autolock syncLock(mLocalSyncPointMutex);
168 for (auto& point : mLocalSyncPoints) {
169 point->setFrameAvailable();
170 }
171 mLocalSyncPoints.clear();
172 }
173
Chia-I Wuc6657022017-08-15 11:18:17 -0700174 for (const auto& child : mCurrentChildren) {
175 child->onRemovedFromCurrentState();
176 }
177}
Chia-I Wu38512252017-05-17 14:36:16 -0700178
chaviw61626f22018-11-15 16:26:27 -0800179void Layer::addToCurrentState() {
180 mRemovedFromCurrentState = false;
181
182 for (const auto& child : mCurrentChildren) {
183 child->addToCurrentState();
184 }
185}
186
Mathias Agopian13127d82013-03-05 17:47:11 -0800187// ---------------------------------------------------------------------------
188// set-up
189// ---------------------------------------------------------------------------
190
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700191const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800192 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800193}
194
chaviw13fdc492017-06-27 12:40:18 -0700195bool Layer::getPremultipledAlpha() const {
196 return mPremultipliedAlpha;
197}
198
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700199sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800200 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700201 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800202}
203
204// ---------------------------------------------------------------------------
205// h/w composer set-up
206// ---------------------------------------------------------------------------
207
Dominik Laskowski075d3172018-05-24 15:50:06 -0700208bool Layer::createHwcLayer(HWComposer* hwc, DisplayId displayId) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700209 LOG_ALWAYS_FATAL_IF(hasHwcLayer(displayId), "Already have a layer for display %s",
210 to_string(displayId).c_str());
David Sodmanb8aaea12017-12-14 15:54:51 -0800211 auto layer = std::shared_ptr<HWC2::Layer>(
212 hwc->createLayer(displayId),
213 [hwc, displayId](HWC2::Layer* layer) {
214 hwc->destroyLayer(displayId, layer); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700215 if (!layer) {
216 return false;
217 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700218 LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers[displayId];
Steven Thomasb02664d2017-07-26 18:48:28 -0700219 hwcInfo.hwc = hwc;
220 hwcInfo.layer = layer;
David Sodmanf6a38932018-05-25 15:27:50 -0700221 layer->setLayerDestroyedListener(
Dominik Laskowski7e045462018-05-30 13:02:02 -0700222 [this, displayId](HWC2::Layer* /*layer*/) { getBE().mHwcLayers.erase(displayId); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700223 return true;
224}
225
Dominik Laskowski075d3172018-05-24 15:50:06 -0700226bool Layer::destroyHwcLayer(DisplayId displayId) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700227 if (!hasHwcLayer(displayId)) {
Chia-I Wu83806892017-11-16 10:50:20 -0800228 return false;
Steven Thomasb02664d2017-07-26 18:48:28 -0700229 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700230 auto& hwcInfo = getBE().mHwcLayers[displayId];
David Sodman41fdfc92017-11-06 16:09:56 -0800231 LOG_ALWAYS_FATAL_IF(hwcInfo.layer == nullptr, "Attempt to destroy null layer");
Steven Thomasb02664d2017-07-26 18:48:28 -0700232 LOG_ALWAYS_FATAL_IF(hwcInfo.hwc == nullptr, "Missing HWComposer");
David Sodmanb8aaea12017-12-14 15:54:51 -0800233 hwcInfo.layer = nullptr;
234
Chia-I Wu83806892017-11-16 10:50:20 -0800235 return true;
Steven Thomasb02664d2017-07-26 18:48:28 -0700236}
237
chaviw61626f22018-11-15 16:26:27 -0800238void Layer::destroyHwcLayersForAllDisplays() {
David Sodman6f65f3e2017-11-03 14:28:09 -0700239 size_t numLayers = getBE().mHwcLayers.size();
Steven Thomasb02664d2017-07-26 18:48:28 -0700240 for (size_t i = 0; i < numLayers; ++i) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700241 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.empty(), "destroyAllHwcLayers failed");
242 destroyHwcLayer(getBE().mHwcLayers.begin()->first);
Steven Thomasb02664d2017-07-26 18:48:28 -0700243 }
chaviw61626f22018-11-15 16:26:27 -0800244}
245
246void Layer::destroyAllHwcLayersPlusChildren() {
247 destroyHwcLayersForAllDisplays();
David Sodman6f65f3e2017-11-03 14:28:09 -0700248 LOG_ALWAYS_FATAL_IF(!getBE().mHwcLayers.empty(),
David Sodman41fdfc92017-11-06 16:09:56 -0800249 "All hardware composer layers should have been destroyed");
Robert Carr2e102c92018-10-23 12:11:15 -0700250
251 for (const sp<Layer>& child : mDrawingChildren) {
chaviw61626f22018-11-15 16:26:27 -0800252 child->destroyAllHwcLayersPlusChildren();
Robert Carr2e102c92018-10-23 12:11:15 -0700253 }
Steven Thomasb02664d2017-07-26 18:48:28 -0700254}
Steven Thomasb02664d2017-07-26 18:48:28 -0700255
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800256Rect Layer::getContentCrop() const {
257 // this is the crop rectangle that applies to the buffer
258 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700259 Rect crop;
260 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800261 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700262 crop = mCurrentCrop;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800263 } else if (getBE().compositionInfo.mBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800264 // otherwise we use the whole buffer
David Sodman0cc69182017-11-17 12:12:07 -0800265 crop = getBE().compositionInfo.mBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700266 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800267 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700268 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700269 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700270 return crop;
271}
272
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700273static Rect reduce(const Rect& win, const Region& exclude) {
274 if (CC_LIKELY(exclude.isEmpty())) {
275 return win;
276 }
277 if (exclude.isRect()) {
278 return win.reduce(exclude.getBounds());
279 }
280 return Region(win).subtract(exclude).getBounds();
281}
282
Dan Stoza80d61162017-12-20 15:57:52 -0800283static FloatRect reduce(const FloatRect& win, const Region& exclude) {
284 if (CC_LIKELY(exclude.isEmpty())) {
285 return win;
286 }
287 // Convert through Rect (by rounding) for lack of FloatRegion
288 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
289}
290
Vishnu Nair60356342018-11-13 13:00:45 -0800291Rect Layer::computeScreenBounds() const {
292 FloatRect bounds = computeBounds();
Peiyong Linefefaac2018-08-17 12:27:51 -0700293 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800294 // Transform to screen space.
295 bounds = t.transform(bounds);
296 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700297}
298
Dan Stoza80d61162017-12-20 15:57:52 -0800299FloatRect Layer::computeBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000300 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700301 return computeBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700302}
303
Dan Stoza80d61162017-12-20 15:57:52 -0800304FloatRect Layer::computeBounds(const Region& activeTransparentRegion) const {
Alec Mourib416efd2018-09-06 21:01:59 +0000305 const State& s(getDrawingState());
Vishnu Nair60356342018-11-13 13:00:45 -0800306 Rect bounds = getCroppedBufferSize(s);
307 FloatRect floatBounds = bounds.toFloatRect();
308 if (bounds.isValid()) {
309 // Layer has bounds. Pass in our bounds as a special case. Then pass on to our parents so
310 // that they can clip it.
311 floatBounds = cropChildBounds(floatBounds);
312 } else {
313 // Layer does not have bounds, so we fill to our parent bounds. This is done by getting our
314 // parent bounds and inverting the transform to get the maximum bounds we can have that
315 // will fit within our parent bounds.
316 const auto& p = mDrawingParent.promote();
317 if (p != nullptr) {
318 ui::Transform t = s.active_legacy.transform;
319 // When calculating the parent bounds for purposes of clipping, we don't need to
320 // constrain the parent to its transparent region. The transparent region is an
321 // optimization based on the buffer contents of the layer, but does not affect the
322 // space allocated to it by policy, and thus children should be allowed to extend into
323 // the parent's transparent region.
324 // One of the main uses is a parent window with a child sitting behind the parent
325 // window, marked by a transparent region. When computing the parent bounds from the
326 // parent's perspective we pass in the transparent region to reduce buffer allocation
327 // size. When computing the parent bounds from the child's perspective, we pass in an
328 // empty transparent region in order to extend into the the parent bounds.
329 floatBounds = p->computeBounds(Region());
330 // Transform back to layer space.
331 floatBounds = t.inverse().transform(floatBounds);
332 }
333 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700334
Vishnu Nair60356342018-11-13 13:00:45 -0800335 // Subtract the transparent region and snap to the bounds.
336 return reduce(floatBounds, activeTransparentRegion);
337}
338
339FloatRect Layer::cropChildBounds(const FloatRect& childBounds) const {
340 const State& s(getDrawingState());
341 Rect bounds = getCroppedBufferSize(s);
342 FloatRect croppedBounds = childBounds;
343
344 // If the layer has bounds, then crop the passed in child bounds and pass
345 // it to our parents so they can crop it as well. If the layer has no bounds,
346 // then pass on the child bounds.
347 if (bounds.isValid()) {
348 croppedBounds = croppedBounds.intersect(bounds.toFloatRect());
Mathias Agopian13127d82013-03-05 17:47:11 -0800349 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700350
Chia-I Wue41dbe62017-06-13 14:10:56 -0700351 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700352 if (p != nullptr) {
Vishnu Nair60356342018-11-13 13:00:45 -0800353 // Transform to parent space and allow parent layer to crop the
354 // child bounds as well.
355 ui::Transform t = s.active_legacy.transform;
356 croppedBounds = t.transform(croppedBounds);
357 croppedBounds = p->cropChildBounds(croppedBounds);
358 croppedBounds = t.inverse().transform(croppedBounds);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700359 }
Vishnu Nair60356342018-11-13 13:00:45 -0800360 return croppedBounds;
361}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700362
Vishnu Nair60356342018-11-13 13:00:45 -0800363Rect Layer::getCroppedBufferSize(const State& s) const {
364 Rect size = getBufferSize(s);
365 Rect crop = getCrop(s);
366 if (!crop.isEmpty() && size.isValid()) {
367 size.intersect(crop, &size);
368 } else if (!crop.isEmpty()) {
369 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700370 }
Vishnu Nair60356342018-11-13 13:00:45 -0800371 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800372}
373
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700374Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& display) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700375 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800376 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700377 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800378
379 // apply the projection's clipping to the window crop in
380 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700381 // if there are no window scaling involved, this operation will map to full
382 // pixels in the buffer.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700383
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800384 FloatRect activeCropFloat = computeBounds();
Peiyong Linefefaac2018-08-17 12:27:51 -0700385 ui::Transform t = getTransform();
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800386 // Transform to screen space.
387 activeCropFloat = t.transform(activeCropFloat);
388 activeCropFloat = activeCropFloat.intersect(display->getViewport().toFloatRect());
389 // Back to layer space to work with the content crop.
390 activeCropFloat = t.inverse().transform(activeCropFloat);
391 // This needs to be here as transform.transform(Rect) computes the
392 // transformed rect and then takes the bounding box of the result before
393 // returning. This means
394 // transform.inverse().transform(transform.transform(Rect)) != Rect
395 // in which case we need to make sure the final rect is clipped to the
396 // display bounds.
397 Rect activeCrop{activeCropFloat};
398 if (!activeCrop.intersect(getBufferSize(s), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000399 activeCrop.clear();
400 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700401 return activeCrop;
402}
403
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700404void Layer::setupRoundedCornersCropCoordinates(Rect win,
405 const FloatRect& roundedCornersCrop) const {
406 // Translate win by the rounded corners rect coordinates, to have all values in
407 // layer coordinate space.
408 win.left -= roundedCornersCrop.left;
409 win.right -= roundedCornersCrop.left;
410 win.top -= roundedCornersCrop.top;
411 win.bottom -= roundedCornersCrop.top;
412
413 renderengine::Mesh::VertexArray<vec2> cropCoords(getBE().mMesh.getCropCoordArray<vec2>());
414 cropCoords[0] = vec2(win.left, win.top);
415 cropCoords[1] = vec2(win.left, win.top + win.getHeight());
416 cropCoords[2] = vec2(win.right, win.top + win.getHeight());
417 cropCoords[3] = vec2(win.right, win.top);
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700418}
419
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700420FloatRect Layer::computeCrop(const sp<const DisplayDevice>& display) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700421 // the content crop is the area of the content that gets scaled to the
422 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800423 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700424
425 // In addition there is a WM-specified crop we pull from our drawing state.
426 const State& s(getDrawingState());
427
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700428 Rect activeCrop = computeInitialCrop(display);
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800429 Rect bufferSize = getBufferSize(s);
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700430
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000431 // Transform the window crop to match the buffer coordinate system,
432 // which means using the inverse of the current transform set on the
433 // SurfaceFlingerConsumer.
434 uint32_t invTransform = mCurrentTransform;
Robert Carrcae605c2017-03-29 12:10:31 -0700435 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000436 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700437 * the code below applies the primary display's inverse transform to the
438 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000439 */
David Sodman41fdfc92017-11-06 16:09:56 -0800440 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000441 // calculate the inverse transform
442 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800443 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800444 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000445 // and apply to the current transform
Peiyong Linefefaac2018-08-17 12:27:51 -0700446 invTransform = (ui::Transform(invTransformOrient) *
447 ui::Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800448 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000449
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800450 int winWidth = bufferSize.getWidth();
451 int winHeight = bufferSize.getHeight();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000452 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
453 // If the activeCrop has been rotate the ends are rotated but not
454 // the space itself so when transforming ends back we can't rely on
455 // a modification of the axes of rotation. To account for this we
456 // need to reorient the inverse rotation in terms of the current
457 // axes of rotation.
458 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
459 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
460 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800461 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000462 }
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800463 std::swap(winWidth, winHeight);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000464 }
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800465 const Rect winCrop =
466 activeCrop.transform(invTransform, bufferSize.getWidth(), bufferSize.getHeight());
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000467
468 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800469 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000470 float yScale = crop.getHeight() / float(winHeight);
471
David Sodman41fdfc92017-11-06 16:09:56 -0800472 float insetL = winCrop.left * xScale;
473 float insetT = winCrop.top * yScale;
474 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000475 float insetB = (winHeight - winCrop.bottom) * yScale;
476
David Sodman41fdfc92017-11-06 16:09:56 -0800477 crop.left += insetL;
478 crop.top += insetT;
479 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000480 crop.bottom -= insetB;
481
Mathias Agopian13127d82013-03-05 17:47:11 -0800482 return crop;
483}
484
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700485void Layer::setGeometry(const sp<const DisplayDevice>& display, uint32_t z) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700486 const auto displayId = display->getId();
Dominik Laskowski075d3172018-05-24 15:50:06 -0700487 LOG_ALWAYS_FATAL_IF(!displayId);
Dominik Laskowski34157762018-10-31 13:07:19 -0700488 RETURN_IF_NO_HWC_LAYER(*displayId);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700489 auto& hwcInfo = getBE().mHwcLayers[*displayId];
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700490
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700491 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800492 hwcInfo.forceClientComposition = false;
493
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700494 if (isSecure() && !display->isSecure()) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800495 hwcInfo.forceClientComposition = true;
496 }
497
David Sodman15094112018-10-11 09:39:37 -0700498 auto& hwcLayer = hwcInfo.layer;
499
Mathias Agopian13127d82013-03-05 17:47:11 -0800500 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700501 const State& s(getDrawingState());
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800502 const int bufferWidth = getBufferSize(s).getWidth();
503 const int bufferHeight = getBufferSize(s).getHeight();
David Revemanecf0fa52017-03-03 11:32:44 -0500504 auto blendMode = HWC2::BlendMode::None;
Robert Carr6452f122017-03-21 10:41:29 -0700505 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800506 blendMode =
507 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800508 }
David Sodman15094112018-10-11 09:39:37 -0700509 auto error = hwcLayer->setBlendMode(blendMode);
510 ALOGE_IF(error != HWC2::Error::None,
511 "[%s] Failed to set blend mode %s:"
512 " %s (%d)",
513 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
514 static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700515 getBE().compositionInfo.hwc.blendMode = blendMode;
Mathias Agopian13127d82013-03-05 17:47:11 -0800516
517 // apply the layer's transform, followed by the display's global transform
518 // here we're guaranteed that the layer's transform preserves rects
Marissa Wall61c58622018-07-18 10:12:20 -0700519 Region activeTransparentRegion(getActiveTransparentRegion(s));
Peiyong Linefefaac2018-08-17 12:27:51 -0700520 ui::Transform t = getTransform();
Marissa Wall61c58622018-07-18 10:12:20 -0700521 Rect activeCrop = getCrop(s);
522 if (!activeCrop.isEmpty()) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700523 activeCrop = t.transform(activeCrop);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700524 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000525 activeCrop.clear();
526 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700527 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800528 // This needs to be here as transform.transform(Rect) computes the
529 // transformed rect and then takes the bounding box of the result before
530 // returning. This means
531 // transform.inverse().transform(transform.transform(Rect)) != Rect
532 // in which case we need to make sure the final rect is clipped to the
533 // display bounds.
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800534 if (!activeCrop.intersect(Rect(bufferWidth, bufferHeight), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000535 activeCrop.clear();
536 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700537 // mark regions outside the crop as transparent
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800538 activeTransparentRegion.orSelf(Rect(0, 0, bufferWidth, activeCrop.top));
539 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom, bufferWidth, bufferHeight));
David Sodman41fdfc92017-11-06 16:09:56 -0800540 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
541 activeTransparentRegion.orSelf(
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800542 Rect(activeCrop.right, activeCrop.top, bufferWidth, activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700543 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700544
Dan Stoza80d61162017-12-20 15:57:52 -0800545 // computeBounds returns a FloatRect to provide more accuracy during the
546 // transformation. We then round upon constructing 'frame'.
547 Rect frame{t.transform(computeBounds(activeTransparentRegion))};
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700548 if (!frame.intersect(display->getViewport(), &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000549 frame.clear();
550 }
Peiyong Linefefaac2018-08-17 12:27:51 -0700551 const ui::Transform& tr = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800552 Rect transformedFrame = tr.transform(frame);
David Sodman15094112018-10-11 09:39:37 -0700553 error = hwcLayer->setDisplayFrame(transformedFrame);
554 if (error != HWC2::Error::None) {
555 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
556 transformedFrame.left, transformedFrame.top, transformedFrame.right,
557 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
558 } else {
559 hwcInfo.displayFrame = transformedFrame;
560 }
David Sodmanba340492018-08-05 21:51:33 -0700561 getBE().compositionInfo.hwc.displayFrame = transformedFrame;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800562
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700563 FloatRect sourceCrop = computeCrop(display);
David Sodman15094112018-10-11 09:39:37 -0700564 error = hwcLayer->setSourceCrop(sourceCrop);
565 if (error != HWC2::Error::None) {
566 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
567 "%s (%d)",
568 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
569 to_string(error).c_str(), static_cast<int32_t>(error));
570 } else {
571 hwcInfo.sourceCrop = sourceCrop;
572 }
David Sodmanba340492018-08-05 21:51:33 -0700573 getBE().compositionInfo.hwc.sourceCrop = sourceCrop;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800574
chaviw13fdc492017-06-27 12:40:18 -0700575 float alpha = static_cast<float>(getAlpha());
David Sodman15094112018-10-11 09:39:37 -0700576 error = hwcLayer->setPlaneAlpha(alpha);
577 ALOGE_IF(error != HWC2::Error::None,
578 "[%s] Failed to set plane alpha %.3f: "
579 "%s (%d)",
580 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700581 getBE().compositionInfo.hwc.alpha = alpha;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800582
David Sodman15094112018-10-11 09:39:37 -0700583 error = hwcLayer->setZOrder(z);
584 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
585 to_string(error).c_str(), static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700586 getBE().compositionInfo.hwc.z = z;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500587
Albert Chaulk2a589632017-05-04 16:59:44 -0400588 int type = s.type;
589 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700590 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400591 if (parent.get()) {
592 auto& parentState = parent->getDrawingState();
rongliucfb187b2018-03-14 12:26:23 -0700593 if (parentState.type >= 0 || parentState.appId >= 0) {
594 type = parentState.type;
595 appId = parentState.appId;
596 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400597 }
598
David Sodman15094112018-10-11 09:39:37 -0700599 error = hwcLayer->setInfo(type, appId);
600 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
601 static_cast<int32_t>(error));
602
David Sodmanba340492018-08-05 21:51:33 -0700603 getBE().compositionInfo.hwc.type = type;
604 getBE().compositionInfo.hwc.appId = appId;
605
Mathias Agopian29a367b2011-07-12 14:51:45 -0700606 /*
607 * Transformations are applied in this order:
608 * 1) buffer orientation/flip/mirror
609 * 2) state transformation (window manager)
610 * 3) layer orientation (screen orientation)
611 * (NOTE: the matrices are multiplied in reverse order)
612 */
613
Peiyong Linefefaac2018-08-17 12:27:51 -0700614 const ui::Transform bufferOrientation(mCurrentTransform);
615 ui::Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700616
Robert Carrcae605c2017-03-29 12:10:31 -0700617 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700618 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700619 * the code below applies the primary display's inverse transform to the
620 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700621 */
David Sodman41fdfc92017-11-06 16:09:56 -0800622 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700623 // calculate the inverse transform
624 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800625 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700626 }
Robert Carrcae605c2017-03-29 12:10:31 -0700627
628 /*
629 * Here we cancel out the orientation component of the WM transform.
630 * The scaling and translate components are already included in our bounds
631 * computation so it's enough to just omit it in the composition.
632 * See comment in onDraw with ref to b/36727915 for why.
633 */
Peiyong Linefefaac2018-08-17 12:27:51 -0700634 transform = ui::Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700635 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700636
637 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800638 const uint32_t orientation = transform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -0700639 if (orientation & ui::Transform::ROT_INVALID) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800640 // we can only handle simple transformation
Lloyd Pique074e8122018-07-26 12:57:23 -0700641 hwcInfo.forceClientComposition = true;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700642 getBE().mHwcLayers[*displayId].compositionType = HWC2::Composition::Client;
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;
David Sodman15094112018-10-11 09:39:37 -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 Sodmanba340492018-08-05 21:51:33 -0700652 getBE().compositionInfo.hwc.transform = transform;
David Sodman4b7c4bc2017-11-17 12:13:59 -0800653 }
654}
655
Dominik Laskowski075d3172018-05-24 15:50:06 -0700656void Layer::forceClientComposition(DisplayId displayId) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700657 RETURN_IF_NO_HWC_LAYER(displayId);
Dominik Laskowski7e045462018-05-30 13:02:02 -0700658 getBE().mHwcLayers[displayId].forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800659}
Dan Stozaee44edd2015-03-23 15:50:23 -0700660
Dominik Laskowski075d3172018-05-24 15:50:06 -0700661bool Layer::getForceClientComposition(DisplayId displayId) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700662 RETURN_IF_NO_HWC_LAYER(displayId, false);
Dominik Laskowski7e045462018-05-30 13:02:02 -0700663 return getBE().mHwcLayers[displayId].forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800664}
665
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700666void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700667 const auto displayId = display->getId();
Dominik Laskowski34157762018-10-31 13:07:19 -0700668 LOG_ALWAYS_FATAL_IF(!displayId);
669 if (!hasHwcLayer(*displayId) || getCompositionType(displayId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800670 return;
671 }
672
673 // This gives us only the "orientation" component of the transform
674 const State& s(getCurrentState());
675
676 // Apply the layer's transform, followed by the display's global transform
677 // Here we're guaranteed that the layer's transform preserves rects
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800678 Rect win = getCroppedBufferSize(s);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800679 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700680 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Robert Carr1f0a16a2016-10-24 16:27:39 -0700681 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700682 frame.intersect(display->getViewport(), &frame);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700683 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800684 auto position = displayTransform.transform(frame);
685
Dominik Laskowski7e045462018-05-30 13:02:02 -0700686 auto error =
Dominik Laskowski075d3172018-05-24 15:50:06 -0700687 getBE().mHwcLayers[*displayId].layer->setCursorPosition(position.left, position.top);
688
David Sodman41fdfc92017-11-06 16:09:56 -0800689 ALOGE_IF(error != HWC2::Error::None,
690 "[%s] Failed to set cursor position "
691 "to (%d, %d): %s (%d)",
692 mName.string(), position.left, position.top, to_string(error).c_str(),
693 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800694}
Riley Andrews03414a12014-07-01 14:22:59 -0700695
Mathias Agopian13127d82013-03-05 17:47:11 -0800696// ---------------------------------------------------------------------------
697// drawing...
698// ---------------------------------------------------------------------------
699
Marissa Wall61c58622018-07-18 10:12:20 -0700700void Layer::draw(const RenderArea& renderArea, const Region& clip) {
chaviwa76b2712017-09-20 12:02:26 -0700701 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800702}
703
Marissa Wall61c58622018-07-18 10:12:20 -0700704void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) {
chaviwa76b2712017-09-20 12:02:26 -0700705 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800706}
707
David Sodman41fdfc92017-11-06 16:09:56 -0800708void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
709 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800710 auto& engine(mFlinger->getRenderEngine());
David Sodman9eeae692017-11-02 10:53:32 -0700711 computeGeometry(renderArea, getBE().mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700712 engine.setupFillWithColor(red, green, blue, alpha);
David Sodman9eeae692017-11-02 10:53:32 -0700713 engine.drawMesh(getBE().mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800714}
715
David Sodmanc1498e62018-09-12 14:36:26 -0700716void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
717 clearWithOpenGL(renderArea, 0, 0, 0, 0);
718}
719
Dominik Laskowski075d3172018-05-24 15:50:06 -0700720void Layer::setCompositionType(DisplayId displayId, HWC2::Composition type, bool callIntoHwc) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700721 if (getBE().mHwcLayers.count(displayId) == 0) {
Chia-I Wu30505fb2018-03-26 16:20:31 -0700722 ALOGE("setCompositionType called without a valid HWC layer");
723 return;
David Sodman15094112018-10-11 09:39:37 -0700724 }
725 auto& hwcInfo = getBE().mHwcLayers[displayId];
726 auto& hwcLayer = hwcInfo.layer;
727 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", (hwcLayer)->getId(), to_string(type).c_str(),
728 static_cast<int>(callIntoHwc));
729 if (hwcInfo.compositionType != type) {
730 ALOGV(" actually setting");
731 hwcInfo.compositionType = type;
732 if (callIntoHwc) {
733 auto error = (hwcLayer)->setCompositionType(type);
734 ALOGE_IF(error != HWC2::Error::None,
735 "[%s] Failed to set "
736 "composition type %s: %s (%d)",
737 mName.string(), to_string(type).c_str(), to_string(error).c_str(),
738 static_cast<int32_t>(error));
Chia-I Wu30505fb2018-03-26 16:20:31 -0700739 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800740 }
741}
742
Dominik Laskowski075d3172018-05-24 15:50:06 -0700743HWC2::Composition Layer::getCompositionType(const std::optional<DisplayId>& displayId) const {
744 if (!displayId) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700745 // If we're querying the composition type for a display that does not
746 // have a HWC counterpart, then it will always be Client
747 return HWC2::Composition::Client;
748 }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700749 if (getBE().mHwcLayers.count(*displayId) == 0) {
750 ALOGE("getCompositionType called with an invalid HWC layer");
751 return HWC2::Composition::Invalid;
752 }
753 return getBE().mHwcLayers.at(*displayId).compositionType;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800754}
755
Dominik Laskowski075d3172018-05-24 15:50:06 -0700756void Layer::setClearClientTarget(DisplayId displayId, bool clear) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700757 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800758 ALOGE("setClearClientTarget called without a valid HWC layer");
759 return;
760 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700761 getBE().mHwcLayers[displayId].clearClientTarget = clear;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800762}
763
Dominik Laskowski075d3172018-05-24 15:50:06 -0700764bool Layer::getClearClientTarget(DisplayId displayId) const {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700765 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800766 ALOGE("getClearClientTarget called without a valid HWC layer");
767 return false;
768 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700769 return getBE().mHwcLayers.at(displayId).clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800770}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800771
Dan Stozacac35382016-01-27 12:21:06 -0800772bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
773 if (point->getFrameNumber() <= mCurrentFrameNumber) {
774 // Don't bother with a SyncPoint, since we've already latched the
775 // relevant frame
776 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700777 }
Robert Carr2e102c92018-10-23 12:11:15 -0700778 if (isRemovedFromCurrentState()) {
779 return false;
780 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700781
Dan Stozacac35382016-01-27 12:21:06 -0800782 Mutex::Autolock lock(mLocalSyncPointMutex);
783 mLocalSyncPoints.push_back(point);
784 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700785}
786
Mathias Agopian13127d82013-03-05 17:47:11 -0800787// ----------------------------------------------------------------------------
788// local state
789// ----------------------------------------------------------------------------
790
Peiyong Lin833074a2018-08-28 11:53:54 -0700791void Layer::computeGeometry(const RenderArea& renderArea,
792 renderengine::Mesh& mesh,
chaviwa76b2712017-09-20 12:02:26 -0700793 bool useIdentityTransform) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700794 const ui::Transform renderAreaTransform(renderArea.getTransform());
Dan Stoza80d61162017-12-20 15:57:52 -0800795 FloatRect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700796
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000797 vec2 lt = vec2(win.left, win.top);
798 vec2 lb = vec2(win.left, win.bottom);
799 vec2 rb = vec2(win.right, win.bottom);
800 vec2 rt = vec2(win.right, win.top);
801
Peiyong Linefefaac2018-08-17 12:27:51 -0700802 ui::Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000803 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700804 lt = layerTransform.transform(lt);
805 lb = layerTransform.transform(lb);
806 rb = layerTransform.transform(rb);
807 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000808 }
809
Peiyong Lin833074a2018-08-28 11:53:54 -0700810 renderengine::Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700811 position[0] = renderAreaTransform.transform(lt);
812 position[1] = renderAreaTransform.transform(lb);
813 position[2] = renderAreaTransform.transform(rb);
814 position[3] = renderAreaTransform.transform(rt);
Mathias Agopian13127d82013-03-05 17:47:11 -0800815}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800816
David Sodman41fdfc92017-11-06 16:09:56 -0800817bool Layer::isSecure() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000818 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700819 return (s.flags & layer_state_t::eLayerSecure);
820}
821
Mathias Agopian13127d82013-03-05 17:47:11 -0800822void Layer::setVisibleRegion(const Region& visibleRegion) {
823 // always called from main thread
824 this->visibleRegion = visibleRegion;
825}
826
827void Layer::setCoveredRegion(const Region& coveredRegion) {
828 // always called from main thread
829 this->coveredRegion = coveredRegion;
830}
831
David Sodman41fdfc92017-11-06 16:09:56 -0800832void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800833 // always called from main thread
834 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
835}
836
Robert Carre5f4f692018-01-12 13:12:28 -0800837void Layer::clearVisibilityRegions() {
838 visibleRegion.clear();
839 visibleNonTransparentRegion.clear();
840 coveredRegion.clear();
841}
842
Mathias Agopian13127d82013-03-05 17:47:11 -0800843// ----------------------------------------------------------------------------
844// transaction
845// ----------------------------------------------------------------------------
846
Dan Stoza7dde5992015-05-22 09:51:44 -0700847void Layer::pushPendingState() {
848 if (!mCurrentState.modified) {
849 return;
850 }
851
Dan Stoza7dde5992015-05-22 09:51:44 -0700852 // If this transaction is waiting on the receipt of a frame, generate a sync
853 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700854 // We don't allow installing sync points after we are removed from the current state
855 // as we won't be able to signal our end.
856 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700857 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800858 if (barrierLayer == nullptr) {
859 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700860 // If we can't promote the layer we are intended to wait on,
861 // then it is expired or otherwise invalid. Allow this transaction
862 // to be applied as per normal (no synchronization).
Marissa Wallf58c14b2018-07-24 10:50:43 -0700863 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800864 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700865 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy);
Robert Carr0d480722017-01-10 16:42:54 -0800866 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800867 mRemoteSyncPoints.push_back(std::move(syncPoint));
868 } else {
869 // We already missed the frame we're supposed to synchronize
870 // on, so go ahead and apply the state update
Marissa Wallf58c14b2018-07-24 10:50:43 -0700871 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800872 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700873 }
874
Dan Stoza7dde5992015-05-22 09:51:44 -0700875 // Wake us up to check if the frame has been received
876 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700877 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700878 }
879 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700880 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700881}
882
Pablo Ceballos05289c22016-04-14 15:49:55 -0700883void Layer::popPendingState(State* stateToCommit) {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700884 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700885
886 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700887 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700888}
889
Pablo Ceballos05289c22016-04-14 15:49:55 -0700890bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700891 bool stateUpdateAvailable = false;
892 while (!mPendingStates.empty()) {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700893 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700894 if (mRemoteSyncPoints.empty()) {
895 // If we don't have a sync point for this, apply it anyway. It
896 // will be visually wrong, but it should keep us from getting
897 // into too much trouble.
898 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700899 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700900 stateUpdateAvailable = true;
901 continue;
902 }
903
Marissa Wallf58c14b2018-07-24 10:50:43 -0700904 if (mRemoteSyncPoints.front()->getFrameNumber() !=
905 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800906 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800907
908 // Signal our end of the sync point and then dispose of it
909 mRemoteSyncPoints.front()->setTransactionApplied();
910 mRemoteSyncPoints.pop_front();
911 continue;
912 }
913
Dan Stoza7dde5992015-05-22 09:51:44 -0700914 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
915 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700916 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700917 stateUpdateAvailable = true;
918
919 // Signal our end of the sync point and then dispose of it
920 mRemoteSyncPoints.front()->setTransactionApplied();
921 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800922 } else {
923 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700924 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700925 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700926 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700927 stateUpdateAvailable = true;
928 }
929 }
930
931 // If we still have pending updates, wake SurfaceFlinger back up and point
932 // it at this layer so we can process them
933 if (!mPendingStates.empty()) {
934 setTransactionFlags(eTransactionNeeded);
935 mFlinger->setTransactionFlags(eTraversalNeeded);
936 }
937
938 mCurrentState.modified = false;
939 return stateUpdateAvailable;
940}
941
Marissa Wall61c58622018-07-18 10:12:20 -0700942uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000943 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800944
Marissa Wall61c58622018-07-18 10:12:20 -0700945 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
946 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700947
David Sodmaneb085e02017-10-05 18:49:04 -0700948 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700949 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000950 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800951 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
952 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
953 " requested={ wh={%4u,%4u} }}\n"
954 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
955 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700956 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700957 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
958 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
959 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
960 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
961 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700962 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
963 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
964 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800965 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700966
Robert Carre392b552017-09-19 12:16:05 -0700967 // Don't let Layer::doTransaction update the drawing state
968 // if we have a pending resize, unless we are in fixed-size mode.
969 // the drawing state will be updated only once we receive a buffer
970 // with the correct size.
971 //
972 // In particular, we want to make sure the clip (which is part
973 // of the geometry state) is latched together with the size but is
974 // latched immediately when no resizing is involved.
975 //
976 // If a sideband stream is attached, however, we want to skip this
977 // optimization so that transactions aren't missed when a buffer
978 // never arrives
979 //
980 // In the case that we don't have a buffer we ignore other factors
981 // and avoid entering the resizePending state. At a high level the
982 // resizePending state is to avoid applying the state of the new buffer
983 // to the old buffer. However in the state where we don't have an old buffer
984 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700985 const bool resizePending =
986 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
987 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
David Sodman0cc69182017-11-17 12:12:07 -0800988 (getBE().compositionInfo.mBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700989 if (!isFixedSize()) {
David Sodman0cc69182017-11-17 12:12:07 -0800990 if (resizePending && getBE().compositionInfo.hwc.sidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700991 flags |= eDontUpdateGeometryState;
992 }
993 }
994
Robert Carr7bf247e2017-05-18 14:02:49 -0700995 // Here we apply various requested geometry states, depending on our
996 // latching configuration. See Layer.h for a detailed discussion of
997 // how geometry latching is controlled.
998 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000999 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001000
1001 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1002 // mode, which causes attributes which normally latch regardless of scaling mode,
1003 // to be delayed. We copy the requested state to the active state making sure
1004 // to respect these rules (again see Layer.h for a detailed discussion).
1005 //
1006 // There is an awkward asymmetry in the handling of the crop states in the position
1007 // states, as can be seen below. Largely this arises from position and transform
1008 // being stored in the same data structure while having different latching rules.
1009 // b/38182305
1010 //
Marissa Wall61c58622018-07-18 10:12:20 -07001011 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -07001012 // applyPendingStates in the presence of deferred transactions.
1013 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -07001014 float tx = stateToCommit->active_legacy.transform.tx();
1015 float ty = stateToCommit->active_legacy.transform.ty();
1016 stateToCommit->active_legacy = stateToCommit->requested_legacy;
1017 stateToCommit->active_legacy.transform.set(tx, ty);
1018 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001019 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001020 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -07001021 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001022 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001023 }
1024
Marissa Wall61c58622018-07-18 10:12:20 -07001025 return flags;
1026}
1027
1028uint32_t Layer::doTransaction(uint32_t flags) {
1029 ATRACE_CALL();
1030
chaviw5aedec92018-10-22 10:40:38 -07001031 if (mLayerDetached) {
1032 return 0;
1033 }
1034
Marissa Wall61c58622018-07-18 10:12:20 -07001035 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +00001036 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -07001037 if (!applyPendingStates(&c)) {
1038 return 0;
1039 }
1040
1041 flags = doTransactionResize(flags, &c);
1042
Alec Mourib416efd2018-09-06 21:01:59 +00001043 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -07001044
1045 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001046 // invalidate and recompute the visible regions if needed
1047 flags |= Layer::eVisibleRegion;
1048 }
1049
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001050 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001051 // invalidate and recompute the visible regions if needed
1052 flags |= eVisibleRegion;
1053 this->contentDirty = true;
1054
1055 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -07001056 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -07001057 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -08001058 }
1059
Dan Stozac8145172016-04-28 16:29:06 -07001060 // If the layer is hidden, signal and clear out all local sync points so
1061 // that transactions for layers depending on this layer's frames becoming
1062 // visible are not blocked
1063 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001064 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001065 }
1066
Robert Carr720e5062018-07-30 17:45:14 -07001067 if (mCurrentState.inputInfoChanged) {
1068 flags |= eInputInfoChanged;
1069 mCurrentState.inputInfoChanged = false;
1070 }
1071
Mathias Agopian13127d82013-03-05 17:47:11 -08001072 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001073 commitTransaction(c);
Marissa Walle2ffb422018-10-12 11:33:52 -07001074 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -08001075 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001076}
1077
Pablo Ceballos05289c22016-04-14 15:49:55 -07001078void Layer::commitTransaction(const State& stateToCommit) {
1079 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001080}
1081
Mathias Agopian13127d82013-03-05 17:47:11 -08001082uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Piquef1c675b2018-09-12 20:45:39 -07001083 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -08001084}
1085
1086uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Piquef1c675b2018-09-12 20:45:39 -07001087 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -08001088}
1089
Robert Carr82364e32016-05-15 11:27:47 -07001090bool Layer::setPosition(float x, float y, bool immediate) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001091 if (mCurrentState.requested_legacy.transform.tx() == x &&
1092 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001093 return false;
1094 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001095
1096 // We update the requested and active position simultaneously because
1097 // we want to apply the position portion of the transform matrix immediately,
1098 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Marissa Wallf58c14b2018-07-24 10:50:43 -07001099 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001100 if (immediate && !mFreezeGeometryUpdates) {
1101 // Here we directly update the active state
1102 // unlike other setters, because we store it within
1103 // the transform, but use different latching rules.
1104 // b/38182305
Marissa Wallf58c14b2018-07-24 10:50:43 -07001105 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001106 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001107 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001108
Dan Stoza7dde5992015-05-22 09:51:44 -07001109 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001110 setTransactionFlags(eTransactionNeeded);
1111 return true;
1112}
Robert Carr82364e32016-05-15 11:27:47 -07001113
Robert Carr1f0a16a2016-10-24 16:27:39 -07001114bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1115 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1116 if (idx < 0) {
1117 return false;
1118 }
1119 if (childLayer->setLayer(z)) {
1120 mCurrentChildren.removeAt(idx);
1121 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001122 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001123 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001124 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001125}
1126
Robert Carr503c7042017-09-27 15:06:08 -07001127bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1128 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1129 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1130 if (idx < 0) {
1131 return false;
1132 }
1133 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1134 mCurrentChildren.removeAt(idx);
1135 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001136 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001137 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001138 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001139}
1140
Robert Carrae060832016-11-28 10:51:00 -08001141bool Layer::setLayer(int32_t z) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001142 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001143 mCurrentState.sequence++;
1144 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001145 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001146
1147 // Discard all relative layering.
1148 if (mCurrentState.zOrderRelativeOf != nullptr) {
1149 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1150 if (strongRelative != nullptr) {
1151 strongRelative->removeZOrderRelative(this);
1152 }
1153 mCurrentState.zOrderRelativeOf = nullptr;
1154 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001155 setTransactionFlags(eTransactionNeeded);
1156 return true;
1157}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001158
Robert Carrdb66e622017-04-10 16:55:57 -07001159void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1160 mCurrentState.zOrderRelatives.remove(relative);
1161 mCurrentState.sequence++;
1162 mCurrentState.modified = true;
1163 setTransactionFlags(eTransactionNeeded);
1164}
1165
1166void Layer::addZOrderRelative(const wp<Layer>& relative) {
1167 mCurrentState.zOrderRelatives.add(relative);
1168 mCurrentState.modified = true;
1169 mCurrentState.sequence++;
1170 setTransactionFlags(eTransactionNeeded);
1171}
1172
Robert Carr503d2bd2017-12-04 15:49:47 -08001173bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001174 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1175 if (handle == nullptr) {
1176 return false;
1177 }
1178 sp<Layer> relative = handle->owner.promote();
1179 if (relative == nullptr) {
1180 return false;
1181 }
1182
Robert Carr503d2bd2017-12-04 15:49:47 -08001183 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1184 mCurrentState.zOrderRelativeOf == relative) {
1185 return false;
1186 }
1187
Robert Carrdb66e622017-04-10 16:55:57 -07001188 mCurrentState.sequence++;
1189 mCurrentState.modified = true;
Robert Carr503d2bd2017-12-04 15:49:47 -08001190 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001191
chaviw9ab4bd12017-11-03 13:11:00 -07001192 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
1193 if (oldZOrderRelativeOf != nullptr) {
1194 oldZOrderRelativeOf->removeZOrderRelative(this);
1195 }
Robert Carrdb66e622017-04-10 16:55:57 -07001196 mCurrentState.zOrderRelativeOf = relative;
1197 relative->addZOrderRelative(this);
1198
1199 setTransactionFlags(eTransactionNeeded);
1200
1201 return true;
1202}
1203
Mathias Agopian13127d82013-03-05 17:47:11 -08001204bool Layer::setSize(uint32_t w, uint32_t h) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001205 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
1206 return false;
1207 mCurrentState.requested_legacy.w = w;
1208 mCurrentState.requested_legacy.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001209 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001210 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001211
1212 // record the new size, from this point on, when the client request
1213 // a buffer, it'll get the new size.
1214 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001215 return true;
1216}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001217bool Layer::setAlpha(float alpha) {
David Sodman41fdfc92017-11-06 16:09:56 -08001218 if (mCurrentState.color.a == alpha) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001219 mCurrentState.sequence++;
chaviw13fdc492017-06-27 12:40:18 -07001220 mCurrentState.color.a = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001221 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001222 setTransactionFlags(eTransactionNeeded);
1223 return true;
1224}
chaviw13fdc492017-06-27 12:40:18 -07001225
1226bool Layer::setColor(const half3& color) {
David Sodman41fdfc92017-11-06 16:09:56 -08001227 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g &&
1228 color.b == mCurrentState.color.b)
chaviw13fdc492017-06-27 12:40:18 -07001229 return false;
1230
1231 mCurrentState.sequence++;
1232 mCurrentState.color.r = color.r;
1233 mCurrentState.color.g = color.g;
1234 mCurrentState.color.b = color.b;
1235 mCurrentState.modified = true;
1236 setTransactionFlags(eTransactionNeeded);
1237 return true;
1238}
1239
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001240bool Layer::setCornerRadius(float cornerRadius) {
1241 if (mCurrentState.cornerRadius == cornerRadius)
1242 return false;
1243
1244 mCurrentState.sequence++;
1245 mCurrentState.cornerRadius = cornerRadius;
1246 mCurrentState.modified = true;
1247 setTransactionFlags(eTransactionNeeded);
1248 return true;
1249}
1250
Robert Carrd4ae7f32018-06-07 16:10:57 -07001251bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1252 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001253 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001254 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1255
1256 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1257 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1258 return false;
1259 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001260 mCurrentState.sequence++;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001261 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1262 matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001263 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001264 setTransactionFlags(eTransactionNeeded);
1265 return true;
1266}
Marissa Wall61c58622018-07-18 10:12:20 -07001267
Mathias Agopian13127d82013-03-05 17:47:11 -08001268bool Layer::setTransparentRegionHint(const Region& transparent) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001269 mCurrentState.requestedTransparentRegion_legacy = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001270 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001271 setTransactionFlags(eTransactionNeeded);
1272 return true;
1273}
1274bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1275 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
David Sodman41fdfc92017-11-06 16:09:56 -08001276 if (mCurrentState.flags == newFlags) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001277 mCurrentState.sequence++;
1278 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001279 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001280 setTransactionFlags(eTransactionNeeded);
1281 return true;
1282}
Robert Carr99e27f02016-06-16 15:18:02 -07001283
Marissa Wallf58c14b2018-07-24 10:50:43 -07001284bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
1285 if (mCurrentState.requestedCrop_legacy == crop) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001286 mCurrentState.sequence++;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001287 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001288 if (immediate && !mFreezeGeometryUpdates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001289 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001290 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001291 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1292
Dan Stoza7dde5992015-05-22 09:51:44 -07001293 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001294 setTransactionFlags(eTransactionNeeded);
1295 return true;
1296}
Robert Carr8d5227b2017-03-16 15:41:03 -07001297
Robert Carrc3574f72016-03-24 12:19:32 -07001298bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001299 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001300 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001301 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001302 return true;
1303}
1304
rongliucfb187b2018-03-14 12:26:23 -07001305void Layer::setInfo(int32_t type, int32_t appId) {
David Sodman41fdfc92017-11-06 16:09:56 -08001306 mCurrentState.appId = appId;
1307 mCurrentState.type = type;
1308 mCurrentState.modified = true;
1309 setTransactionFlags(eTransactionNeeded);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001310}
1311
Mathias Agopian13127d82013-03-05 17:47:11 -08001312bool Layer::setLayerStack(uint32_t layerStack) {
David Sodman41fdfc92017-11-06 16:09:56 -08001313 if (mCurrentState.layerStack == layerStack) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001314 mCurrentState.sequence++;
1315 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001316 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001317 setTransactionFlags(eTransactionNeeded);
1318 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001319}
1320
Robert Carr1f0a16a2016-10-24 16:27:39 -07001321uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001322 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001323 if (p == nullptr) {
1324 return getDrawingState().layerStack;
1325 }
1326 return p->getLayerStack();
1327}
1328
Marissa Wallf58c14b2018-07-24 10:50:43 -07001329void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
1330 mCurrentState.barrierLayer_legacy = barrierLayer;
1331 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001332 // We don't set eTransactionNeeded, because just receiving a deferral
1333 // request without any other state updates shouldn't actually induce a delay
1334 mCurrentState.modified = true;
1335 pushPendingState();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001336 mCurrentState.barrierLayer_legacy = nullptr;
1337 mCurrentState.frameNumber_legacy = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001338 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001339}
1340
Marissa Wallf58c14b2018-07-24 10:50:43 -07001341void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001342 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001343 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001344}
1345
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001346// ----------------------------------------------------------------------------
1347// pageflip handling...
1348// ----------------------------------------------------------------------------
1349
Robert Carr1f0a16a2016-10-24 16:27:39 -07001350bool Layer::isHiddenByPolicy() const {
Alec Mourib416efd2018-09-06 21:01:59 +00001351 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001352 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001353 if (parent != nullptr && parent->isHiddenByPolicy()) {
1354 return true;
1355 }
1356 return s.flags & layer_state_t::eLayerHidden;
1357}
1358
David Sodman41fdfc92017-11-06 16:09:56 -08001359uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001360 // TODO: should we do something special if mSecure is set?
1361 if (mProtectedByApp) {
1362 // need a hardware-protected path to external video sink
1363 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001364 }
Riley Andrews03414a12014-07-01 14:22:59 -07001365 if (mPotentialCursor) {
1366 usage |= GraphicBuffer::USAGE_CURSOR;
1367 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001368 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001369 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001370}
1371
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001372void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001373 uint32_t orientation = 0;
Vishnu Nairf2deb822018-11-12 17:53:48 -08001374 // Disable setting transform hint if the debug flag is set or if the
1375 // getTransformToDisplayInverse flag is set and the client wants to submit buffers
1376 // in one orientation.
1377 if (!mFlinger->mDebugDisableTransformHint && !getTransformToDisplayInverse()) {
Mathias Agopian84300952012-11-21 16:02:13 -08001378 // The transform hint is used to improve performance, but we can
1379 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001380 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001381 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001382 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001383 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001384 orientation = 0;
1385 }
1386 }
David Sodmaneb085e02017-10-05 18:49:04 -07001387 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001388}
1389
Mathias Agopian13127d82013-03-05 17:47:11 -08001390// ----------------------------------------------------------------------------
1391// debugging
1392// ----------------------------------------------------------------------------
1393
Marissa Wall61c58622018-07-18 10:12:20 -07001394// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001395LayerDebugInfo Layer::getLayerDebugInfo() const {
1396 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001397 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001398 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001399 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001400 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
1401 info.mType = String8(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001402 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001403 info.mVisibleRegion = visibleRegion;
1404 info.mSurfaceDamageRegion = surfaceDamageRegion;
1405 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001406 info.mX = ds.active_legacy.transform.tx();
1407 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001408 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001409 info.mWidth = ds.active_legacy.w;
1410 info.mHeight = ds.active_legacy.h;
1411 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001412 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001413 info.mFlags = ds.flags;
1414 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001415 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001416 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1417 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1418 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1419 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001420 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001421 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001422 if (buffer != 0) {
1423 info.mActiveBufferWidth = buffer->getWidth();
1424 info.mActiveBufferHeight = buffer->getHeight();
1425 info.mActiveBufferStride = buffer->getStride();
1426 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001427 } else {
1428 info.mActiveBufferWidth = 0;
1429 info.mActiveBufferHeight = 0;
1430 info.mActiveBufferStride = 0;
1431 info.mActiveBufferFormat = 0;
1432 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001433 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001434 info.mNumQueuedFrames = getQueuedFrameCount();
1435 info.mRefreshPending = isBufferLatched();
1436 info.mIsOpaque = isOpaque(ds);
1437 info.mContentDirty = contentDirty;
1438 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001439}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001440
Dan Stozae22aec72016-08-01 13:20:59 -07001441void Layer::miniDumpHeader(String8& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001442 result.append("-------------------------------");
1443 result.append("-------------------------------");
1444 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001445 result.append(" Layer name\n");
1446 result.append(" Z | ");
1447 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001448 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001449 result.append(" Disp Frame (LTRB) | ");
1450 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001451 result.append("-------------------------------");
1452 result.append("-------------------------------");
1453 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001454}
1455
Dominik Laskowski075d3172018-05-24 15:50:06 -07001456void Layer::miniDump(String8& result, DisplayId displayId) const {
Dominik Laskowski34157762018-10-31 13:07:19 -07001457 if (!hasHwcLayer(displayId)) {
Dan Stozae22aec72016-08-01 13:20:59 -07001458 return;
1459 }
1460
1461 String8 name;
1462 if (mName.length() > 77) {
1463 std::string shortened;
1464 shortened.append(mName.string(), 36);
1465 shortened.append("[...]");
1466 shortened.append(mName.string() + (mName.length() - 36), 36);
1467 name = shortened.c_str();
1468 } else {
1469 name = mName;
1470 }
1471
1472 result.appendFormat(" %s\n", name.string());
1473
Alec Mourib416efd2018-09-06 21:01:59 +00001474 const State& layerState(getDrawingState());
Lloyd Pique074e8122018-07-26 12:57:23 -07001475 const LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers.at(displayId);
Chia-I Wu1e043612018-03-01 09:45:09 -08001476 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
1477 result.appendFormat(" rel %6d | ", layerState.z);
1478 } else {
1479 result.appendFormat(" %10d | ", layerState.z);
1480 }
Dominik Laskowski7e045462018-05-30 13:02:02 -07001481 result.appendFormat("%10s | ", to_string(getCompositionType(displayId)).c_str());
Lloyd Pique074e8122018-07-26 12:57:23 -07001482 result.appendFormat("%10s | ", to_string(hwcInfo.transform).c_str());
1483 const Rect& frame = hwcInfo.displayFrame;
David Sodman41fdfc92017-11-06 16:09:56 -08001484 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique074e8122018-07-26 12:57:23 -07001485 const FloatRect& crop = hwcInfo.sourceCrop;
David Sodman41fdfc92017-11-06 16:09:56 -08001486 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 -07001487
David Sodman7e4ae112018-02-09 15:02:28 -08001488 result.append("- - - - - - - - - - - - - - - -\n");
1489
1490 std::string compositionInfoStr;
1491 getBE().compositionInfo.dump(compositionInfoStr, "compositionInfo");
1492 result.append(compositionInfoStr.c_str());
1493
Yichi Chen6ca35192018-05-29 12:20:43 +08001494 result.append("- - - - - - - - - - - - - - - -");
1495 result.append("- - - - - - - - - - - - - - - -");
1496 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001497}
Dan Stozae22aec72016-08-01 13:20:59 -07001498
Svetoslavd85084b2014-03-20 10:28:31 -07001499void Layer::dumpFrameStats(String8& result) const {
1500 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001501}
1502
Svetoslavd85084b2014-03-20 10:28:31 -07001503void Layer::clearFrameStats() {
1504 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001505}
1506
Jamie Gennis6547ff42013-07-16 20:12:42 -07001507void Layer::logFrameStats() {
1508 mFrameTracker.logAndResetStats(mName);
1509}
1510
Svetoslavd85084b2014-03-20 10:28:31 -07001511void Layer::getFrameStats(FrameStats* outStats) const {
1512 mFrameTracker.getStats(outStats);
1513}
1514
Brian Andersond6927fb2016-07-23 23:37:30 -07001515void Layer::dumpFrameEvents(String8& result) {
David Sodman41fdfc92017-11-06 16:09:56 -08001516 result.appendFormat("- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001517 Mutex::Autolock lock(mFrameEventHistoryMutex);
1518 mFrameEventHistory.checkFencesForCompletion();
1519 mFrameEventHistory.dump(result);
1520}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001521
Brian Anderson5ea5e592016-12-01 16:54:33 -08001522void Layer::onDisconnect() {
1523 Mutex::Autolock lock(mFrameEventHistoryMutex);
1524 mFrameEventHistory.onDisconnect();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -07001525 mTimeStats.onDisconnect(getSequence());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001526}
1527
Brian Anderson3890c392016-07-25 12:48:08 -07001528void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001529 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001530 if (newTimestamps) {
Yiwei Zhang8e8fe522018-11-02 18:34:07 -07001531 mTimeStats.setPostTime(getSequence(), newTimestamps->frameNumber, getName().c_str(),
1532 newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001533 }
1534
Brian Andersond6927fb2016-07-23 23:37:30 -07001535 Mutex::Autolock lock(mFrameEventHistoryMutex);
1536 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001537 // If there are any unsignaled fences in the aquire timeline at this
1538 // point, the previously queued frame hasn't been latched yet. Go ahead
1539 // and try to get the signal time here so the syscall is taken out of
1540 // the main thread's critical path.
1541 mAcquireTimeline.updateSignalTimes();
1542 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001543 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001544 mFrameEventHistory.addQueue(*newTimestamps);
1545 }
1546
Brian Anderson3890c392016-07-25 12:48:08 -07001547 if (outDelta) {
1548 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001549 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001550}
Dan Stozae77c7662016-05-13 11:37:28 -07001551
Chia-I Wu98f1c102017-05-30 14:54:08 -07001552size_t Layer::getChildrenCount() const {
1553 size_t count = 0;
1554 for (const sp<Layer>& child : mCurrentChildren) {
1555 count += 1 + child->getChildrenCount();
1556 }
1557 return count;
1558}
1559
Robert Carr1f0a16a2016-10-24 16:27:39 -07001560void Layer::addChild(const sp<Layer>& layer) {
1561 mCurrentChildren.add(layer);
1562 layer->setParent(this);
1563}
1564
1565ssize_t Layer::removeChild(const sp<Layer>& layer) {
1566 layer->setParent(nullptr);
1567 return mCurrentChildren.remove(layer);
1568}
1569
Robert Carr1db73f62016-12-21 12:58:51 -08001570bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1571 sp<Handle> handle = nullptr;
1572 sp<Layer> newParent = nullptr;
1573 if (newParentHandle == nullptr) {
1574 return false;
1575 }
1576 handle = static_cast<Handle*>(newParentHandle.get());
1577 newParent = handle->owner.promote();
1578 if (newParent == nullptr) {
1579 ALOGE("Unable to promote Layer handle");
1580 return false;
1581 }
1582
chaviw5aedec92018-10-22 10:40:38 -07001583 if (attachChildren()) {
1584 setTransactionFlags(eTransactionNeeded);
1585 }
Robert Carr1db73f62016-12-21 12:58:51 -08001586 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001587 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001588
1589 sp<Client> client(child->mClientRef.promote());
1590 if (client != nullptr) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001591 client->updateParent(newParent);
Robert Carr1db73f62016-12-21 12:58:51 -08001592 }
1593 }
1594 mCurrentChildren.clear();
1595
1596 return true;
1597}
1598
Robert Carr15eae092018-03-23 13:43:53 -07001599void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001600 for (const sp<Layer>& child : mDrawingChildren) {
1601 child->mDrawingParent = newParent;
1602 }
1603}
1604
chaviwf1961f72017-09-18 16:41:07 -07001605bool Layer::reparent(const sp<IBinder>& newParentHandle) {
1606 if (newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001607 return false;
1608 }
1609
1610 auto handle = static_cast<Handle*>(newParentHandle.get());
1611 sp<Layer> newParent = handle->owner.promote();
1612 if (newParent == nullptr) {
1613 ALOGE("Unable to promote Layer handle");
1614 return false;
1615 }
1616
chaviwf1961f72017-09-18 16:41:07 -07001617 sp<Layer> parent = getParent();
1618 if (parent != nullptr) {
1619 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001620 }
chaviwf1961f72017-09-18 16:41:07 -07001621 newParent->addChild(this);
chaviw06178942017-07-27 10:25:59 -07001622
chaviw61626f22018-11-15 16:26:27 -08001623 if (!newParent->isRemovedFromCurrentState()) {
1624 addToCurrentState();
1625 }
1626
chaviwf1961f72017-09-18 16:41:07 -07001627 sp<Client> client(mClientRef.promote());
chaviw06178942017-07-27 10:25:59 -07001628 sp<Client> newParentClient(newParent->mClientRef.promote());
1629
chaviwf1961f72017-09-18 16:41:07 -07001630 if (client != newParentClient) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001631 client->updateParent(newParent);
chaviw06178942017-07-27 10:25:59 -07001632 }
1633
chaviw5aedec92018-10-22 10:40:38 -07001634 if (mLayerDetached) {
1635 mLayerDetached = false;
1636 setTransactionFlags(eTransactionNeeded);
1637 }
1638 if (attachChildren()) {
1639 setTransactionFlags(eTransactionNeeded);
1640 }
chaviw06178942017-07-27 10:25:59 -07001641 return true;
1642}
1643
Robert Carr9524cb32017-02-13 11:32:32 -08001644bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001645 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001646 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001647 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001648 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001649 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001650 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001651 }
Robert Carr7f619b22017-11-06 12:56:35 -08001652 }
Robert Carr9524cb32017-02-13 11:32:32 -08001653
1654 return true;
1655}
1656
chaviw5aedec92018-10-22 10:40:38 -07001657bool Layer::attachChildren() {
1658 bool changed = false;
1659 for (const sp<Layer>& child : mCurrentChildren) {
1660 sp<Client> parentClient = mClientRef.promote();
1661 sp<Client> client(child->mClientRef.promote());
1662 if (client != nullptr && parentClient != client) {
1663 if (child->mLayerDetached) {
1664 child->mLayerDetached = false;
1665 changed = true;
1666 }
1667 changed |= child->attachChildren();
1668 }
1669 }
1670
1671 return changed;
1672}
1673
Peiyong Lind3788632018-09-18 16:01:31 -07001674bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001675 static const mat4 identityMatrix = mat4();
1676
Peiyong Lind3788632018-09-18 16:01:31 -07001677 if (mCurrentState.colorTransform == matrix) {
1678 return false;
1679 }
1680 ++mCurrentState.sequence;
1681 mCurrentState.colorTransform = matrix;
Peiyong Lin747321c2018-10-01 10:03:11 -07001682 mCurrentState.hasColorTransform = matrix != identityMatrix;
1683 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001684 setTransactionFlags(eTransactionNeeded);
1685 return true;
1686}
1687
chaviwf66724d2018-11-28 16:35:21 -08001688mat4 Layer::getColorTransform() const {
1689 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1690 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1691 colorTransform = parent->getColorTransform() * colorTransform;
1692 }
1693 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001694}
1695
1696bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001697 bool hasColorTransform = getDrawingState().hasColorTransform;
1698 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1699 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1700 }
1701 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001702}
1703
Chia-I Wu11481472018-05-04 10:43:19 -07001704bool Layer::isLegacyDataSpace() const {
1705 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001706 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001707 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001708}
1709
Robert Carr1f0a16a2016-10-24 16:27:39 -07001710void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001711 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001712}
1713
1714void Layer::clearSyncPoints() {
1715 for (const auto& child : mCurrentChildren) {
1716 child->clearSyncPoints();
1717 }
1718
1719 Mutex::Autolock lock(mLocalSyncPointMutex);
1720 for (auto& point : mLocalSyncPoints) {
1721 point->setFrameAvailable();
1722 }
1723 mLocalSyncPoints.clear();
1724}
1725
1726int32_t Layer::getZ() const {
1727 return mDrawingState.z;
1728}
1729
Robert Carr29abff82017-12-04 13:51:20 -08001730bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1731 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1732 const State& state = useDrawing ? mDrawingState : mCurrentState;
1733 return state.zOrderRelativeOf != nullptr;
1734}
1735
David Sodman41fdfc92017-11-06 16:09:56 -08001736__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001737 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001738 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1739 "makeTraversalList received invalid stateSet");
1740 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1741 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1742 const State& state = useDrawing ? mDrawingState : mCurrentState;
1743
Robert Carr29abff82017-12-04 13:51:20 -08001744 if (state.zOrderRelatives.size() == 0) {
1745 *outSkipRelativeZUsers = true;
1746 return children;
1747 }
1748
chaviwfd462612018-05-31 16:11:27 -07001749 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001750 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001751 sp<Layer> strongRelative = weakRelative.promote();
1752 if (strongRelative != nullptr) {
1753 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001754 }
1755 }
1756
Dan Stoza412903f2017-04-27 13:42:17 -07001757 for (const sp<Layer>& child : children) {
Robert Carr503c7042017-09-27 15:06:08 -07001758 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1759 if (childState.zOrderRelativeOf != nullptr) {
1760 continue;
1761 }
Robert Carrdb66e622017-04-10 16:55:57 -07001762 traverse.add(child);
1763 }
1764
1765 return traverse;
1766}
1767
Robert Carr1f0a16a2016-10-24 16:27:39 -07001768/**
Robert Carrdb66e622017-04-10 16:55:57 -07001769 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001770 */
Dan Stoza412903f2017-04-27 13:42:17 -07001771void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001772 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1773 // produce a new list for traversing, including our relatives, and not including our children
1774 // who are relatives of another surface. In the case that there are no relative Z,
1775 // makeTraversalList returns our children directly to avoid significant overhead.
1776 // However in this case we need to take the responsibility for filtering children which
1777 // are relatives of another surface here.
1778 bool skipRelativeZUsers = false;
1779 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001780
Robert Carr1f0a16a2016-10-24 16:27:39 -07001781 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001782 for (; i < list.size(); i++) {
1783 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001784 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1785 continue;
1786 }
1787
Robert Carrdb66e622017-04-10 16:55:57 -07001788 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001789 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001790 }
Dan Stoza412903f2017-04-27 13:42:17 -07001791 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001792 }
Robert Carr29abff82017-12-04 13:51:20 -08001793
Dan Stoza412903f2017-04-27 13:42:17 -07001794 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001795 for (; i < list.size(); i++) {
1796 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001797
1798 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1799 continue;
1800 }
Dan Stoza412903f2017-04-27 13:42:17 -07001801 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001802 }
1803}
1804
1805/**
Robert Carrdb66e622017-04-10 16:55:57 -07001806 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001807 */
Dan Stoza412903f2017-04-27 13:42:17 -07001808void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1809 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001810 // See traverseInZOrder for documentation.
1811 bool skipRelativeZUsers = false;
1812 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001813
Robert Carr1f0a16a2016-10-24 16:27:39 -07001814 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001815 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001816 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001817
1818 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1819 continue;
1820 }
1821
Robert Carrdb66e622017-04-10 16:55:57 -07001822 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001823 break;
1824 }
Dan Stoza412903f2017-04-27 13:42:17 -07001825 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001826 }
Dan Stoza412903f2017-04-27 13:42:17 -07001827 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001828 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001829 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001830
1831 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1832 continue;
1833 }
1834
Dan Stoza412903f2017-04-27 13:42:17 -07001835 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001836 }
1837}
1838
chaviw4b129c22018-04-09 16:19:43 -07001839LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1840 const std::vector<Layer*>& layersInTree) {
1841 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1842 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001843 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1844 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
chaviw4b129c22018-04-09 16:19:43 -07001845 const State& state = useDrawing ? mDrawingState : mCurrentState;
1846
chaviwfd462612018-05-31 16:11:27 -07001847 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001848 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1849 sp<Layer> strongRelative = weakRelative.promote();
1850 // Only add relative layers that are also descendents of the top most parent of the tree.
1851 // If a relative layer is not a descendent, then it should be ignored.
1852 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1853 traverse.add(strongRelative);
1854 }
1855 }
1856
1857 for (const sp<Layer>& child : children) {
1858 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1859 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1860 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1861 // the child here since it won't be added later as a relative.
1862 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1863 childState.zOrderRelativeOf.promote().get())) {
1864 continue;
1865 }
1866 traverse.add(child);
1867 }
1868
1869 return traverse;
1870}
1871
1872void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1873 LayerVector::StateSet stateSet,
1874 const LayerVector::Visitor& visitor) {
1875 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001876
1877 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001878 for (; i < list.size(); i++) {
1879 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001880 if (relative->getZ() >= 0) {
1881 break;
1882 }
chaviw4b129c22018-04-09 16:19:43 -07001883 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001884 }
chaviw4b129c22018-04-09 16:19:43 -07001885
chaviwa76b2712017-09-20 12:02:26 -07001886 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001887 for (; i < list.size(); i++) {
1888 const auto& relative = list[i];
1889 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001890 }
1891}
1892
chaviw4b129c22018-04-09 16:19:43 -07001893std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1894 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1895 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1896
1897 std::vector<Layer*> layersInTree = {this};
1898 for (size_t i = 0; i < children.size(); i++) {
1899 const auto& child = children[i];
1900 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1901 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1902 }
1903
1904 return layersInTree;
1905}
1906
1907void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1908 const LayerVector::Visitor& visitor) {
1909 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1910 std::sort(layersInTree.begin(), layersInTree.end());
1911 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1912}
1913
Peiyong Linefefaac2018-08-17 12:27:51 -07001914ui::Transform Layer::getTransform() const {
1915 ui::Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001916 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001917 if (p != nullptr) {
1918 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07001919
1920 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
1921 // it isFixedSize) then there may be additional scaling not accounted
1922 // for in the transform. We need to mirror this scaling in child surfaces
1923 // or we will break the contract where WM can treat child surfaces as
1924 // pixels in the parent surface.
David Sodman0cc69182017-11-17 12:12:07 -08001925 if (p->isFixedSize() && p->getBE().compositionInfo.mBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07001926 int bufferWidth;
1927 int bufferHeight;
1928 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
David Sodman0cc69182017-11-17 12:12:07 -08001929 bufferWidth = p->getBE().compositionInfo.mBuffer->getWidth();
1930 bufferHeight = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001931 } else {
David Sodman0cc69182017-11-17 12:12:07 -08001932 bufferHeight = p->getBE().compositionInfo.mBuffer->getWidth();
1933 bufferWidth = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001934 }
Marissa Wall61c58622018-07-18 10:12:20 -07001935 float sx = p->getActiveWidth(p->getDrawingState()) / static_cast<float>(bufferWidth);
1936 float sy = p->getActiveHeight(p->getDrawingState()) / static_cast<float>(bufferHeight);
Peiyong Linefefaac2018-08-17 12:27:51 -07001937 ui::Transform extraParentScaling;
Robert Carr9b429f42017-04-17 14:56:57 -07001938 extraParentScaling.set(sx, 0, 0, sy);
1939 t = t * extraParentScaling;
1940 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001941 }
Marissa Wall61c58622018-07-18 10:12:20 -07001942 return t * getActiveTransform(getDrawingState());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001943}
1944
chaviw13fdc492017-06-27 12:40:18 -07001945half Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001946 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07001947
chaviw13fdc492017-06-27 12:40:18 -07001948 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1949 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001950}
Robert Carr6452f122017-03-21 10:41:29 -07001951
chaviw13fdc492017-06-27 12:40:18 -07001952half4 Layer::getColor() const {
1953 const half4 color(getDrawingState().color);
1954 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001955}
Robert Carr6452f122017-03-21 10:41:29 -07001956
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001957Layer::RoundedCornerState Layer::getRoundedCornerState() const {
1958 const auto& p = mDrawingParent.promote();
1959 if (p != nullptr) {
1960 RoundedCornerState parentState = p->getRoundedCornerState();
1961 if (parentState.radius > 0) {
1962 ui::Transform t = getActiveTransform(getDrawingState());
1963 t = t.inverse();
1964 parentState.cropRect = t.transform(parentState.cropRect);
1965 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1966 // but a transform matrix can define horizontal and vertical scales.
1967 // Let's take the average between both of them and pass into the shader, practically we
1968 // never do this type of transformation on windows anyway.
1969 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
1970 return parentState;
1971 }
1972 }
1973 const float radius = getDrawingState().cornerRadius;
1974 return radius > 0 ? RoundedCornerState(computeBounds(), radius) : RoundedCornerState();
1975}
1976
Robert Carr1f0a16a2016-10-24 16:27:39 -07001977void Layer::commitChildList() {
1978 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1979 const auto& child = mCurrentChildren[i];
1980 child->commitChildList();
1981 }
1982 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001983 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001984}
1985
Robert Carr720e5062018-07-30 17:45:14 -07001986void Layer::setInputInfo(const InputWindowInfo& info) {
1987 mCurrentState.inputInfo = info;
1988 mCurrentState.modified = true;
1989 mCurrentState.inputInfoChanged = true;
1990 setTransactionFlags(eTransactionNeeded);
1991}
1992
chaviw1d044282017-09-27 12:19:28 -07001993void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
1994 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1995 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1996 const State& state = useDrawing ? mDrawingState : mCurrentState;
1997
Peiyong Linefefaac2018-08-17 12:27:51 -07001998 ui::Transform requestedTransform = state.active_legacy.transform;
1999 ui::Transform transform = getTransform();
chaviw1d044282017-09-27 12:19:28 -07002000
2001 layerInfo->set_id(sequence);
2002 layerInfo->set_name(getName().c_str());
2003 layerInfo->set_type(String8(getTypeId()));
2004
2005 for (const auto& child : children) {
2006 layerInfo->add_children(child->sequence);
2007 }
2008
2009 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
2010 sp<Layer> strongRelative = weakRelative.promote();
2011 if (strongRelative != nullptr) {
2012 layerInfo->add_relatives(strongRelative->sequence);
2013 }
2014 }
2015
Marissa Wallf58c14b2018-07-24 10:50:43 -07002016 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
chaviw1d044282017-09-27 12:19:28 -07002017 layerInfo->mutable_transparent_region());
2018 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
2019 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
2020
2021 layerInfo->set_layer_stack(getLayerStack());
2022 layerInfo->set_z(state.z);
2023
2024 PositionProto* position = layerInfo->mutable_position();
2025 position->set_x(transform.tx());
2026 position->set_y(transform.ty());
2027
2028 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
2029 requestedPosition->set_x(requestedTransform.tx());
2030 requestedPosition->set_y(requestedTransform.ty());
2031
2032 SizeProto* size = layerInfo->mutable_size();
Marissa Wallf58c14b2018-07-24 10:50:43 -07002033 size->set_w(state.active_legacy.w);
2034 size->set_h(state.active_legacy.h);
chaviw1d044282017-09-27 12:19:28 -07002035
Marissa Wallf58c14b2018-07-24 10:50:43 -07002036 LayerProtoHelper::writeToProto(state.crop_legacy, layerInfo->mutable_crop());
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002037 layerInfo->set_corner_radius(getRoundedCornerState().radius);
chaviw1d044282017-09-27 12:19:28 -07002038
2039 layerInfo->set_is_opaque(isOpaque(state));
2040 layerInfo->set_invalidate(contentDirty);
Chia-I Wu01591c92018-05-22 12:03:00 -07002041
2042 // XXX (b/79210409) mCurrentDataSpace is not protected
2043 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
2044
chaviw1d044282017-09-27 12:19:28 -07002045 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
2046 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
2047 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
2048 layerInfo->set_flags(state.flags);
2049
2050 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
2051 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
2052
Jorim Jaggi8e0af362017-11-14 16:28:28 +01002053 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07002054 if (parent != nullptr) {
2055 layerInfo->set_parent(parent->sequence);
2056 }
2057
2058 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2059 if (zOrderRelativeOf != nullptr) {
2060 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
2061 }
2062
Chia-I Wu01591c92018-05-22 12:03:00 -07002063 // XXX getBE().compositionInfo.mBuffer is not protected
David Sodman0cc69182017-11-17 12:12:07 -08002064 auto buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08002065 if (buffer != nullptr) {
2066 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
Peiyong Linefefaac2018-08-17 12:27:51 -07002067 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
Yichi Chen6ca35192018-05-29 12:20:43 +08002068 layerInfo->mutable_buffer_transform());
chaviw1d044282017-09-27 12:19:28 -07002069 }
2070
2071 layerInfo->set_queued_frames(getQueuedFrameCount());
2072 layerInfo->set_refresh_pending(isBufferLatched());
rongliucfb187b2018-03-14 12:26:23 -07002073 layerInfo->set_window_type(state.type);
2074 layerInfo->set_app_id(state.appId);
chaviwadc40c22018-07-10 16:57:27 -07002075 layerInfo->set_curr_frame(mCurrentFrameNumber);
Vishnu Nairf2deb822018-11-12 17:53:48 -08002076 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
chaviwadc40c22018-07-10 16:57:27 -07002077
2078 for (const auto& pendingState : mPendingStates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07002079 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
chaviwadc40c22018-07-10 16:57:27 -07002080 if (barrierLayer != nullptr) {
2081 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
2082 barrierLayerProto->set_id(barrierLayer->sequence);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002083 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
chaviwadc40c22018-07-10 16:57:27 -07002084 }
2085 }
chaviw1d044282017-09-27 12:19:28 -07002086}
2087
Dominik Laskowski075d3172018-05-24 15:50:06 -07002088void Layer::writeToProto(LayerProto* layerInfo, DisplayId displayId) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07002089 if (!hasHwcLayer(displayId)) {
2090 return;
2091 }
2092
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002093 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
2094
Dominik Laskowski7e045462018-05-30 13:02:02 -07002095 const auto& hwcInfo = getBE().mHwcLayers.at(displayId);
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002096
2097 const Rect& frame = hwcInfo.displayFrame;
2098 LayerProtoHelper::writeToProto(frame, layerInfo->mutable_hwc_frame());
2099
2100 const FloatRect& crop = hwcInfo.sourceCrop;
2101 LayerProtoHelper::writeToProto(crop, layerInfo->mutable_hwc_crop());
2102
2103 const int32_t transform = static_cast<int32_t>(hwcInfo.transform);
2104 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002105
2106 const int32_t compositionType = static_cast<int32_t>(hwcInfo.compositionType);
2107 layerInfo->set_hwc_composition_type(compositionType);
2108
2109 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
2110 static_cast<BufferLayer*>(this)->isProtected()) {
2111 layerInfo->set_is_protected(true);
2112 } else {
2113 layerInfo->set_is_protected(false);
2114 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002115}
2116
Robert Carr2e102c92018-10-23 12:11:15 -07002117bool Layer::isRemovedFromCurrentState() const {
2118 return mRemovedFromCurrentState;
2119}
2120
Robert Carr720e5062018-07-30 17:45:14 -07002121InputWindowInfo Layer::fillInputInfo(const Rect& screenBounds) {
2122 InputWindowInfo info = mDrawingState.inputInfo;
Robert Carr5cb25782018-11-14 14:01:42 -08002123 info.frameLeft = screenBounds.left + info.surfaceInset;
2124 info.frameTop = screenBounds.top + info.surfaceInset;
2125 info.frameRight = screenBounds.right - info.surfaceInset;
2126 info.frameBottom = screenBounds.bottom - info.surfaceInset;
Robert Carr720e5062018-07-30 17:45:14 -07002127
Robert Carre07e1032018-11-26 12:55:53 -08002128 ui::Transform t = getTransform();
2129 info.windowXScale *= 1.0f / t.sx();
2130 info.windowYScale *= 1.0f / t.sy();
2131
2132 info.touchableRegion.scaleSelf(t.sx(), t.sy());
2133
Robert Carr5cb25782018-11-14 14:01:42 -08002134 info.touchableRegion = info.touchableRegion.translate(
Robert Carr720e5062018-07-30 17:45:14 -07002135 screenBounds.left,
2136 screenBounds.top);
2137 info.visible = isVisible();
2138 return info;
2139}
2140
2141bool Layer::hasInput() const {
Robert Carr5c8a0262018-10-03 16:30:44 -07002142 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002143}
2144
Mathias Agopian13127d82013-03-05 17:47:11 -08002145// ---------------------------------------------------------------------------
2146
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002147}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002148
2149#if defined(__gl_h_)
2150#error "don't include gl/gl.h in this file"
2151#endif
2152
2153#if defined(__gl2_h_)
2154#error "don't include gl2/gl2.h in this file"
2155#endif