blob: 92a77b069c72f199222c274533d1eccc79031c56 [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();
104 mCurrentState.api = -1;
Peiyong Lin747321c2018-10-01 10:03:11 -0700105 mCurrentState.hasColorTransform = false;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700106
107 // drawing state & current state are identical
108 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700109
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800110 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700111 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800112 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200113 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700114
115 mFlinger->onLayerCreated();
Dan Stoza436ccf32018-06-21 12:10:12 -0700116}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700117
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700118Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800119 sp<Client> c(mClientRef.promote());
120 if (c != 0) {
121 c->detachLayer(this);
122 }
123
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000124 mFrameTracker.logAndResetStats(mName);
Robert Carr2e102c92018-10-23 12:11:15 -0700125
chaviw61626f22018-11-15 16:26:27 -0800126 destroyAllHwcLayersPlusChildren();
Robert Carr2e102c92018-10-23 12:11:15 -0700127
128 mFlinger->onLayerDestroyed();
Mathias Agopian96f08192010-06-02 23:28:45 -0700129}
130
Mathias Agopian13127d82013-03-05 17:47:11 -0800131// ---------------------------------------------------------------------------
132// callbacks
133// ---------------------------------------------------------------------------
134
David Sodmaneb085e02017-10-05 18:49:04 -0700135/*
136 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
137 * Layer. So, the implementation is done in BufferLayer. When called on a
138 * ColorLayer object, it's essentially a NOP.
139 */
David Sodmaneb085e02017-10-05 18:49:04 -0700140void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800141
Chia-I Wuc6657022017-08-15 11:18:17 -0700142void Layer::onRemovedFromCurrentState() {
Robert Carr2e102c92018-10-23 12:11:15 -0700143 mRemovedFromCurrentState = true;
144
Chia-I Wuc6657022017-08-15 11:18:17 -0700145 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
Robert Carr5edb1ad2017-04-25 10:54:24 -0700146 if (mCurrentState.zOrderRelativeOf != nullptr) {
147 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
148 if (strongRelative != nullptr) {
149 strongRelative->removeZOrderRelative(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700150 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700151 }
152 mCurrentState.zOrderRelativeOf = nullptr;
153 }
Rob Carr4bba3702018-10-08 21:53:30 +0000154
Robert Carr2e102c92018-10-23 12:11:15 -0700155 // Since we are no longer reachable from CurrentState SurfaceFlinger
156 // will no longer invoke doTransaction for us, and so we will
157 // never finish applying transactions. We signal the sync point
158 // now so that another layer will not become indefinitely
159 // blocked.
160 for (auto& point: mRemoteSyncPoints) {
161 point->setTransactionApplied();
162 }
163 mRemoteSyncPoints.clear();
164
165 {
166 Mutex::Autolock syncLock(mLocalSyncPointMutex);
167 for (auto& point : mLocalSyncPoints) {
168 point->setFrameAvailable();
169 }
170 mLocalSyncPoints.clear();
171 }
172
Chia-I Wuc6657022017-08-15 11:18:17 -0700173 for (const auto& child : mCurrentChildren) {
174 child->onRemovedFromCurrentState();
175 }
176}
Chia-I Wu38512252017-05-17 14:36:16 -0700177
chaviw61626f22018-11-15 16:26:27 -0800178void Layer::addToCurrentState() {
179 mRemovedFromCurrentState = false;
180
181 for (const auto& child : mCurrentChildren) {
182 child->addToCurrentState();
183 }
184}
185
Mathias Agopian13127d82013-03-05 17:47:11 -0800186// ---------------------------------------------------------------------------
187// set-up
188// ---------------------------------------------------------------------------
189
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700190const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800191 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800192}
193
chaviw13fdc492017-06-27 12:40:18 -0700194bool Layer::getPremultipledAlpha() const {
195 return mPremultipliedAlpha;
196}
197
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700198sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800199 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700200 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800201}
202
203// ---------------------------------------------------------------------------
204// h/w composer set-up
205// ---------------------------------------------------------------------------
206
Dominik Laskowski075d3172018-05-24 15:50:06 -0700207bool Layer::createHwcLayer(HWComposer* hwc, DisplayId displayId) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700208 LOG_ALWAYS_FATAL_IF(hasHwcLayer(displayId), "Already have a layer for display %s",
209 to_string(displayId).c_str());
David Sodmanb8aaea12017-12-14 15:54:51 -0800210 auto layer = std::shared_ptr<HWC2::Layer>(
211 hwc->createLayer(displayId),
212 [hwc, displayId](HWC2::Layer* layer) {
213 hwc->destroyLayer(displayId, layer); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700214 if (!layer) {
215 return false;
216 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700217 LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers[displayId];
Steven Thomasb02664d2017-07-26 18:48:28 -0700218 hwcInfo.hwc = hwc;
219 hwcInfo.layer = layer;
David Sodmanf6a38932018-05-25 15:27:50 -0700220 layer->setLayerDestroyedListener(
Dominik Laskowski7e045462018-05-30 13:02:02 -0700221 [this, displayId](HWC2::Layer* /*layer*/) { getBE().mHwcLayers.erase(displayId); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700222 return true;
223}
224
Dominik Laskowski075d3172018-05-24 15:50:06 -0700225bool Layer::destroyHwcLayer(DisplayId displayId) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700226 if (!hasHwcLayer(displayId)) {
Chia-I Wu83806892017-11-16 10:50:20 -0800227 return false;
Steven Thomasb02664d2017-07-26 18:48:28 -0700228 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700229 auto& hwcInfo = getBE().mHwcLayers[displayId];
David Sodman41fdfc92017-11-06 16:09:56 -0800230 LOG_ALWAYS_FATAL_IF(hwcInfo.layer == nullptr, "Attempt to destroy null layer");
Steven Thomasb02664d2017-07-26 18:48:28 -0700231 LOG_ALWAYS_FATAL_IF(hwcInfo.hwc == nullptr, "Missing HWComposer");
David Sodmanb8aaea12017-12-14 15:54:51 -0800232 hwcInfo.layer = nullptr;
233
Chia-I Wu83806892017-11-16 10:50:20 -0800234 return true;
Steven Thomasb02664d2017-07-26 18:48:28 -0700235}
236
chaviw61626f22018-11-15 16:26:27 -0800237void Layer::destroyHwcLayersForAllDisplays() {
David Sodman6f65f3e2017-11-03 14:28:09 -0700238 size_t numLayers = getBE().mHwcLayers.size();
Steven Thomasb02664d2017-07-26 18:48:28 -0700239 for (size_t i = 0; i < numLayers; ++i) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700240 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.empty(), "destroyAllHwcLayers failed");
241 destroyHwcLayer(getBE().mHwcLayers.begin()->first);
Steven Thomasb02664d2017-07-26 18:48:28 -0700242 }
chaviw61626f22018-11-15 16:26:27 -0800243}
244
245void Layer::destroyAllHwcLayersPlusChildren() {
246 destroyHwcLayersForAllDisplays();
David Sodman6f65f3e2017-11-03 14:28:09 -0700247 LOG_ALWAYS_FATAL_IF(!getBE().mHwcLayers.empty(),
David Sodman41fdfc92017-11-06 16:09:56 -0800248 "All hardware composer layers should have been destroyed");
Robert Carr2e102c92018-10-23 12:11:15 -0700249
250 for (const sp<Layer>& child : mDrawingChildren) {
chaviw61626f22018-11-15 16:26:27 -0800251 child->destroyAllHwcLayersPlusChildren();
Robert Carr2e102c92018-10-23 12:11:15 -0700252 }
Steven Thomasb02664d2017-07-26 18:48:28 -0700253}
Steven Thomasb02664d2017-07-26 18:48:28 -0700254
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800255Rect Layer::getContentCrop() const {
256 // this is the crop rectangle that applies to the buffer
257 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700258 Rect crop;
259 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800260 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700261 crop = mCurrentCrop;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800262 } else if (getBE().compositionInfo.mBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800263 // otherwise we use the whole buffer
David Sodman0cc69182017-11-17 12:12:07 -0800264 crop = getBE().compositionInfo.mBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700265 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800266 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700267 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700268 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700269 return crop;
270}
271
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700272static Rect reduce(const Rect& win, const Region& exclude) {
273 if (CC_LIKELY(exclude.isEmpty())) {
274 return win;
275 }
276 if (exclude.isRect()) {
277 return win.reduce(exclude.getBounds());
278 }
279 return Region(win).subtract(exclude).getBounds();
280}
281
Dan Stoza80d61162017-12-20 15:57:52 -0800282static FloatRect reduce(const FloatRect& win, const Region& exclude) {
283 if (CC_LIKELY(exclude.isEmpty())) {
284 return win;
285 }
286 // Convert through Rect (by rounding) for lack of FloatRegion
287 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
288}
289
Vishnu Nair60356342018-11-13 13:00:45 -0800290Rect Layer::computeScreenBounds() const {
291 FloatRect bounds = computeBounds();
Peiyong Linefefaac2018-08-17 12:27:51 -0700292 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800293 // Transform to screen space.
294 bounds = t.transform(bounds);
295 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700296}
297
Dan Stoza80d61162017-12-20 15:57:52 -0800298FloatRect Layer::computeBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000299 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700300 return computeBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700301}
302
Dan Stoza80d61162017-12-20 15:57:52 -0800303FloatRect Layer::computeBounds(const Region& activeTransparentRegion) const {
Alec Mourib416efd2018-09-06 21:01:59 +0000304 const State& s(getDrawingState());
Vishnu Nair60356342018-11-13 13:00:45 -0800305 Rect bounds = getCroppedBufferSize(s);
306 FloatRect floatBounds = bounds.toFloatRect();
307 if (bounds.isValid()) {
308 // Layer has bounds. Pass in our bounds as a special case. Then pass on to our parents so
309 // that they can clip it.
310 floatBounds = cropChildBounds(floatBounds);
311 } else {
312 // Layer does not have bounds, so we fill to our parent bounds. This is done by getting our
313 // parent bounds and inverting the transform to get the maximum bounds we can have that
314 // will fit within our parent bounds.
315 const auto& p = mDrawingParent.promote();
316 if (p != nullptr) {
317 ui::Transform t = s.active_legacy.transform;
318 // When calculating the parent bounds for purposes of clipping, we don't need to
319 // constrain the parent to its transparent region. The transparent region is an
320 // optimization based on the buffer contents of the layer, but does not affect the
321 // space allocated to it by policy, and thus children should be allowed to extend into
322 // the parent's transparent region.
323 // One of the main uses is a parent window with a child sitting behind the parent
324 // window, marked by a transparent region. When computing the parent bounds from the
325 // parent's perspective we pass in the transparent region to reduce buffer allocation
326 // size. When computing the parent bounds from the child's perspective, we pass in an
327 // empty transparent region in order to extend into the the parent bounds.
328 floatBounds = p->computeBounds(Region());
329 // Transform back to layer space.
330 floatBounds = t.inverse().transform(floatBounds);
331 }
332 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700333
Vishnu Nair60356342018-11-13 13:00:45 -0800334 // Subtract the transparent region and snap to the bounds.
335 return reduce(floatBounds, activeTransparentRegion);
336}
337
338FloatRect Layer::cropChildBounds(const FloatRect& childBounds) const {
339 const State& s(getDrawingState());
340 Rect bounds = getCroppedBufferSize(s);
341 FloatRect croppedBounds = childBounds;
342
343 // If the layer has bounds, then crop the passed in child bounds and pass
344 // it to our parents so they can crop it as well. If the layer has no bounds,
345 // then pass on the child bounds.
346 if (bounds.isValid()) {
347 croppedBounds = croppedBounds.intersect(bounds.toFloatRect());
Mathias Agopian13127d82013-03-05 17:47:11 -0800348 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700349
Chia-I Wue41dbe62017-06-13 14:10:56 -0700350 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700351 if (p != nullptr) {
Vishnu Nair60356342018-11-13 13:00:45 -0800352 // Transform to parent space and allow parent layer to crop the
353 // child bounds as well.
354 ui::Transform t = s.active_legacy.transform;
355 croppedBounds = t.transform(croppedBounds);
356 croppedBounds = p->cropChildBounds(croppedBounds);
357 croppedBounds = t.inverse().transform(croppedBounds);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700358 }
Vishnu Nair60356342018-11-13 13:00:45 -0800359 return croppedBounds;
360}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700361
Vishnu Nair60356342018-11-13 13:00:45 -0800362Rect Layer::getCroppedBufferSize(const State& s) const {
363 Rect size = getBufferSize(s);
364 Rect crop = getCrop(s);
365 if (!crop.isEmpty() && size.isValid()) {
366 size.intersect(crop, &size);
367 } else if (!crop.isEmpty()) {
368 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700369 }
Vishnu Nair60356342018-11-13 13:00:45 -0800370 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800371}
372
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700373Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& display) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700374 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800375 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700376 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800377
378 // apply the projection's clipping to the window crop in
379 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700380 // if there are no window scaling involved, this operation will map to full
381 // pixels in the buffer.
382 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
383 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700384
Marissa Wall61c58622018-07-18 10:12:20 -0700385 Rect activeCrop(getActiveWidth(s), getActiveHeight(s));
386 Rect crop = getCrop(s);
387 if (!crop.isEmpty()) {
388 activeCrop.intersect(crop, &activeCrop);
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700389 }
390
Peiyong Linefefaac2018-08-17 12:27:51 -0700391 ui::Transform t = getTransform();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700392 activeCrop = t.transform(activeCrop);
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700393 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000394 activeCrop.clear();
395 }
chaviwb1154d12017-10-31 14:15:36 -0700396
397 const auto& p = mDrawingParent.promote();
398 if (p != nullptr) {
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700399 auto parentCrop = p->computeInitialCrop(display);
chaviwb1154d12017-10-31 14:15:36 -0700400 activeCrop.intersect(parentCrop, &activeCrop);
401 }
402
Robert Carr1f0a16a2016-10-24 16:27:39 -0700403 return activeCrop;
404}
405
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700406FloatRect Layer::computeCrop(const sp<const DisplayDevice>& display) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700407 // the content crop is the area of the content that gets scaled to the
408 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800409 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700410
411 // In addition there is a WM-specified crop we pull from our drawing state.
412 const State& s(getDrawingState());
413
414 // Screen space to make reduction to parent crop clearer.
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700415 Rect activeCrop = computeInitialCrop(display);
Peiyong Linefefaac2018-08-17 12:27:51 -0700416 ui::Transform t = getTransform();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700417 // Back to layer space to work with the content crop.
418 activeCrop = t.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800419
Michael Lentine28ea2172014-11-19 18:32:37 -0800420 // This needs to be here as transform.transform(Rect) computes the
421 // transformed rect and then takes the bounding box of the result before
422 // returning. This means
423 // transform.inverse().transform(transform.transform(Rect)) != Rect
424 // in which case we need to make sure the final rect is clipped to the
425 // display bounds.
Marissa Wall61c58622018-07-18 10:12:20 -0700426 if (!activeCrop.intersect(Rect(getActiveWidth(s), getActiveHeight(s)), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000427 activeCrop.clear();
428 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800429
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700430 // subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700431 activeCrop = reduce(activeCrop, getActiveTransparentRegion(s));
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700432
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000433 // Transform the window crop to match the buffer coordinate system,
434 // which means using the inverse of the current transform set on the
435 // SurfaceFlingerConsumer.
436 uint32_t invTransform = mCurrentTransform;
Robert Carrcae605c2017-03-29 12:10:31 -0700437 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000438 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700439 * the code below applies the primary display's inverse transform to the
440 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000441 */
David Sodman41fdfc92017-11-06 16:09:56 -0800442 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000443 // calculate the inverse transform
444 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800445 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800446 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000447 // and apply to the current transform
Peiyong Linefefaac2018-08-17 12:27:51 -0700448 invTransform = (ui::Transform(invTransformOrient) *
449 ui::Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800450 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000451
Marissa Wall61c58622018-07-18 10:12:20 -0700452 int winWidth = getActiveWidth(s);
453 int winHeight = getActiveHeight(s);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000454 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
455 // If the activeCrop has been rotate the ends are rotated but not
456 // the space itself so when transforming ends back we can't rely on
457 // a modification of the axes of rotation. To account for this we
458 // need to reorient the inverse rotation in terms of the current
459 // axes of rotation.
460 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
461 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
462 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800463 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000464 }
Marissa Wall61c58622018-07-18 10:12:20 -0700465 winWidth = getActiveHeight(s);
466 winHeight = getActiveWidth(s);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000467 }
Marissa Wall61c58622018-07-18 10:12:20 -0700468 const Rect winCrop = activeCrop.transform(invTransform, getActiveWidth(s), getActiveHeight(s));
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000469
470 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800471 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000472 float yScale = crop.getHeight() / float(winHeight);
473
David Sodman41fdfc92017-11-06 16:09:56 -0800474 float insetL = winCrop.left * xScale;
475 float insetT = winCrop.top * yScale;
476 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000477 float insetB = (winHeight - winCrop.bottom) * yScale;
478
David Sodman41fdfc92017-11-06 16:09:56 -0800479 crop.left += insetL;
480 crop.top += insetT;
481 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000482 crop.bottom -= insetB;
483
Mathias Agopian13127d82013-03-05 17:47:11 -0800484 return crop;
485}
486
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700487void Layer::setGeometry(const sp<const DisplayDevice>& display, uint32_t z) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700488 const auto displayId = display->getId();
Dominik Laskowski075d3172018-05-24 15:50:06 -0700489 LOG_ALWAYS_FATAL_IF(!displayId);
Dominik Laskowski34157762018-10-31 13:07:19 -0700490 RETURN_IF_NO_HWC_LAYER(*displayId);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700491 auto& hwcInfo = getBE().mHwcLayers[*displayId];
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700492
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700493 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800494 hwcInfo.forceClientComposition = false;
495
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700496 if (isSecure() && !display->isSecure()) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800497 hwcInfo.forceClientComposition = true;
498 }
499
David Sodman15094112018-10-11 09:39:37 -0700500 auto& hwcLayer = hwcInfo.layer;
501
Mathias Agopian13127d82013-03-05 17:47:11 -0800502 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700503 const State& s(getDrawingState());
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.
Marissa Wall61c58622018-07-18 10:12:20 -0700534 if (!activeCrop.intersect(Rect(getActiveWidth(s), getActiveHeight(s)), &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
Marissa Wall61c58622018-07-18 10:12:20 -0700538 activeTransparentRegion.orSelf(Rect(0, 0, getActiveWidth(s), activeCrop.top));
Marissa Wallf58c14b2018-07-24 10:50:43 -0700539 activeTransparentRegion.orSelf(
Marissa Wall61c58622018-07-18 10:12:20 -0700540 Rect(0, activeCrop.bottom, getActiveWidth(s), getActiveHeight(s)));
David Sodman41fdfc92017-11-06 16:09:56 -0800541 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
542 activeTransparentRegion.orSelf(
Marissa Wall61c58622018-07-18 10:12:20 -0700543 Rect(activeCrop.right, activeCrop.top, getActiveWidth(s), activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700544 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700545
Dan Stoza80d61162017-12-20 15:57:52 -0800546 // computeBounds returns a FloatRect to provide more accuracy during the
547 // transformation. We then round upon constructing 'frame'.
548 Rect frame{t.transform(computeBounds(activeTransparentRegion))};
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700549 if (!frame.intersect(display->getViewport(), &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000550 frame.clear();
551 }
Peiyong Linefefaac2018-08-17 12:27:51 -0700552 const ui::Transform& tr = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800553 Rect transformedFrame = tr.transform(frame);
David Sodman15094112018-10-11 09:39:37 -0700554 error = hwcLayer->setDisplayFrame(transformedFrame);
555 if (error != HWC2::Error::None) {
556 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
557 transformedFrame.left, transformedFrame.top, transformedFrame.right,
558 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
559 } else {
560 hwcInfo.displayFrame = transformedFrame;
561 }
David Sodmanba340492018-08-05 21:51:33 -0700562 getBE().compositionInfo.hwc.displayFrame = transformedFrame;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800563
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700564 FloatRect sourceCrop = computeCrop(display);
David Sodman15094112018-10-11 09:39:37 -0700565 error = hwcLayer->setSourceCrop(sourceCrop);
566 if (error != HWC2::Error::None) {
567 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
568 "%s (%d)",
569 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
570 to_string(error).c_str(), static_cast<int32_t>(error));
571 } else {
572 hwcInfo.sourceCrop = sourceCrop;
573 }
David Sodmanba340492018-08-05 21:51:33 -0700574 getBE().compositionInfo.hwc.sourceCrop = sourceCrop;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800575
chaviw13fdc492017-06-27 12:40:18 -0700576 float alpha = static_cast<float>(getAlpha());
David Sodman15094112018-10-11 09:39:37 -0700577 error = hwcLayer->setPlaneAlpha(alpha);
578 ALOGE_IF(error != HWC2::Error::None,
579 "[%s] Failed to set plane alpha %.3f: "
580 "%s (%d)",
581 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700582 getBE().compositionInfo.hwc.alpha = alpha;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800583
David Sodman15094112018-10-11 09:39:37 -0700584 error = hwcLayer->setZOrder(z);
585 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
586 to_string(error).c_str(), static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700587 getBE().compositionInfo.hwc.z = z;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500588
Albert Chaulk2a589632017-05-04 16:59:44 -0400589 int type = s.type;
590 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700591 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400592 if (parent.get()) {
593 auto& parentState = parent->getDrawingState();
rongliucfb187b2018-03-14 12:26:23 -0700594 if (parentState.type >= 0 || parentState.appId >= 0) {
595 type = parentState.type;
596 appId = parentState.appId;
597 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400598 }
599
David Sodman15094112018-10-11 09:39:37 -0700600 error = hwcLayer->setInfo(type, appId);
601 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
602 static_cast<int32_t>(error));
603
David Sodmanba340492018-08-05 21:51:33 -0700604 getBE().compositionInfo.hwc.type = type;
605 getBE().compositionInfo.hwc.appId = appId;
606
Mathias Agopian29a367b2011-07-12 14:51:45 -0700607 /*
608 * Transformations are applied in this order:
609 * 1) buffer orientation/flip/mirror
610 * 2) state transformation (window manager)
611 * 3) layer orientation (screen orientation)
612 * (NOTE: the matrices are multiplied in reverse order)
613 */
614
Peiyong Linefefaac2018-08-17 12:27:51 -0700615 const ui::Transform bufferOrientation(mCurrentTransform);
616 ui::Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700617
Robert Carrcae605c2017-03-29 12:10:31 -0700618 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700619 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700620 * the code below applies the primary display's inverse transform to the
621 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700622 */
David Sodman41fdfc92017-11-06 16:09:56 -0800623 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700624 // calculate the inverse transform
625 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800626 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700627 }
Robert Carrcae605c2017-03-29 12:10:31 -0700628
629 /*
630 * Here we cancel out the orientation component of the WM transform.
631 * The scaling and translate components are already included in our bounds
632 * computation so it's enough to just omit it in the composition.
633 * See comment in onDraw with ref to b/36727915 for why.
634 */
Peiyong Linefefaac2018-08-17 12:27:51 -0700635 transform = ui::Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700636 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700637
638 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800639 const uint32_t orientation = transform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -0700640 if (orientation & ui::Transform::ROT_INVALID) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800641 // we can only handle simple transformation
Lloyd Pique074e8122018-07-26 12:57:23 -0700642 hwcInfo.forceClientComposition = true;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700643 getBE().mHwcLayers[*displayId].compositionType = HWC2::Composition::Client;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800644 } else {
645 auto transform = static_cast<HWC2::Transform>(orientation);
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800646 hwcInfo.transform = transform;
David Sodman15094112018-10-11 09:39:37 -0700647 auto error = hwcLayer->setTransform(transform);
648 ALOGE_IF(error != HWC2::Error::None,
649 "[%s] Failed to set transform %s: "
650 "%s (%d)",
651 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
652 static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700653 getBE().compositionInfo.hwc.transform = transform;
David Sodman4b7c4bc2017-11-17 12:13:59 -0800654 }
655}
656
Dominik Laskowski075d3172018-05-24 15:50:06 -0700657void Layer::forceClientComposition(DisplayId displayId) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700658 RETURN_IF_NO_HWC_LAYER(displayId);
Dominik Laskowski7e045462018-05-30 13:02:02 -0700659 getBE().mHwcLayers[displayId].forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800660}
Dan Stozaee44edd2015-03-23 15:50:23 -0700661
Dominik Laskowski075d3172018-05-24 15:50:06 -0700662bool Layer::getForceClientComposition(DisplayId displayId) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700663 RETURN_IF_NO_HWC_LAYER(displayId, false);
Dominik Laskowski7e045462018-05-30 13:02:02 -0700664 return getBE().mHwcLayers[displayId].forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800665}
666
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700667void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700668 const auto displayId = display->getId();
Dominik Laskowski34157762018-10-31 13:07:19 -0700669 LOG_ALWAYS_FATAL_IF(!displayId);
670 if (!hasHwcLayer(*displayId) || getCompositionType(displayId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800671 return;
672 }
673
674 // This gives us only the "orientation" component of the transform
675 const State& s(getCurrentState());
676
677 // Apply the layer's transform, followed by the display's global transform
678 // Here we're guaranteed that the layer's transform preserves rects
Marissa Wall61c58622018-07-18 10:12:20 -0700679 Rect win(getActiveWidth(s), getActiveHeight(s));
680 Rect crop = getCrop(s);
681 if (!crop.isEmpty()) {
682 win.intersect(crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800683 }
684 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700685 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Robert Carr1f0a16a2016-10-24 16:27:39 -0700686 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700687 frame.intersect(display->getViewport(), &frame);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700688 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800689 auto position = displayTransform.transform(frame);
690
Dominik Laskowski7e045462018-05-30 13:02:02 -0700691 auto error =
Dominik Laskowski075d3172018-05-24 15:50:06 -0700692 getBE().mHwcLayers[*displayId].layer->setCursorPosition(position.left, position.top);
693
David Sodman41fdfc92017-11-06 16:09:56 -0800694 ALOGE_IF(error != HWC2::Error::None,
695 "[%s] Failed to set cursor position "
696 "to (%d, %d): %s (%d)",
697 mName.string(), position.left, position.top, to_string(error).c_str(),
698 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800699}
Riley Andrews03414a12014-07-01 14:22:59 -0700700
Mathias Agopian13127d82013-03-05 17:47:11 -0800701// ---------------------------------------------------------------------------
702// drawing...
703// ---------------------------------------------------------------------------
704
Marissa Wall61c58622018-07-18 10:12:20 -0700705void Layer::draw(const RenderArea& renderArea, const Region& clip) {
chaviwa76b2712017-09-20 12:02:26 -0700706 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800707}
708
Marissa Wall61c58622018-07-18 10:12:20 -0700709void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) {
chaviwa76b2712017-09-20 12:02:26 -0700710 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800711}
712
David Sodman41fdfc92017-11-06 16:09:56 -0800713void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
714 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800715 auto& engine(mFlinger->getRenderEngine());
David Sodman9eeae692017-11-02 10:53:32 -0700716 computeGeometry(renderArea, getBE().mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700717 engine.setupFillWithColor(red, green, blue, alpha);
David Sodman9eeae692017-11-02 10:53:32 -0700718 engine.drawMesh(getBE().mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800719}
720
David Sodmanc1498e62018-09-12 14:36:26 -0700721void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
722 clearWithOpenGL(renderArea, 0, 0, 0, 0);
723}
724
Dominik Laskowski075d3172018-05-24 15:50:06 -0700725void Layer::setCompositionType(DisplayId displayId, HWC2::Composition type, bool callIntoHwc) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700726 if (getBE().mHwcLayers.count(displayId) == 0) {
Chia-I Wu30505fb2018-03-26 16:20:31 -0700727 ALOGE("setCompositionType called without a valid HWC layer");
728 return;
David Sodman15094112018-10-11 09:39:37 -0700729 }
730 auto& hwcInfo = getBE().mHwcLayers[displayId];
731 auto& hwcLayer = hwcInfo.layer;
732 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", (hwcLayer)->getId(), to_string(type).c_str(),
733 static_cast<int>(callIntoHwc));
734 if (hwcInfo.compositionType != type) {
735 ALOGV(" actually setting");
736 hwcInfo.compositionType = type;
737 if (callIntoHwc) {
738 auto error = (hwcLayer)->setCompositionType(type);
739 ALOGE_IF(error != HWC2::Error::None,
740 "[%s] Failed to set "
741 "composition type %s: %s (%d)",
742 mName.string(), to_string(type).c_str(), to_string(error).c_str(),
743 static_cast<int32_t>(error));
Chia-I Wu30505fb2018-03-26 16:20:31 -0700744 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800745 }
746}
747
Dominik Laskowski075d3172018-05-24 15:50:06 -0700748HWC2::Composition Layer::getCompositionType(const std::optional<DisplayId>& displayId) const {
749 if (!displayId) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700750 // If we're querying the composition type for a display that does not
751 // have a HWC counterpart, then it will always be Client
752 return HWC2::Composition::Client;
753 }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700754 if (getBE().mHwcLayers.count(*displayId) == 0) {
755 ALOGE("getCompositionType called with an invalid HWC layer");
756 return HWC2::Composition::Invalid;
757 }
758 return getBE().mHwcLayers.at(*displayId).compositionType;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800759}
760
Dominik Laskowski075d3172018-05-24 15:50:06 -0700761void Layer::setClearClientTarget(DisplayId displayId, bool clear) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700762 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800763 ALOGE("setClearClientTarget called without a valid HWC layer");
764 return;
765 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700766 getBE().mHwcLayers[displayId].clearClientTarget = clear;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800767}
768
Dominik Laskowski075d3172018-05-24 15:50:06 -0700769bool Layer::getClearClientTarget(DisplayId displayId) const {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700770 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800771 ALOGE("getClearClientTarget called without a valid HWC layer");
772 return false;
773 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700774 return getBE().mHwcLayers.at(displayId).clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800775}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800776
Dan Stozacac35382016-01-27 12:21:06 -0800777bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
778 if (point->getFrameNumber() <= mCurrentFrameNumber) {
779 // Don't bother with a SyncPoint, since we've already latched the
780 // relevant frame
781 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700782 }
Robert Carr2e102c92018-10-23 12:11:15 -0700783 if (isRemovedFromCurrentState()) {
784 return false;
785 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700786
Dan Stozacac35382016-01-27 12:21:06 -0800787 Mutex::Autolock lock(mLocalSyncPointMutex);
788 mLocalSyncPoints.push_back(point);
789 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700790}
791
Mathias Agopian13127d82013-03-05 17:47:11 -0800792// ----------------------------------------------------------------------------
793// local state
794// ----------------------------------------------------------------------------
795
Peiyong Lin833074a2018-08-28 11:53:54 -0700796void Layer::computeGeometry(const RenderArea& renderArea,
797 renderengine::Mesh& mesh,
chaviwa76b2712017-09-20 12:02:26 -0700798 bool useIdentityTransform) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700799 const ui::Transform renderAreaTransform(renderArea.getTransform());
Dan Stoza80d61162017-12-20 15:57:52 -0800800 FloatRect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700801
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000802 vec2 lt = vec2(win.left, win.top);
803 vec2 lb = vec2(win.left, win.bottom);
804 vec2 rb = vec2(win.right, win.bottom);
805 vec2 rt = vec2(win.right, win.top);
806
Peiyong Linefefaac2018-08-17 12:27:51 -0700807 ui::Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000808 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700809 lt = layerTransform.transform(lt);
810 lb = layerTransform.transform(lb);
811 rb = layerTransform.transform(rb);
812 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000813 }
814
Peiyong Lin833074a2018-08-28 11:53:54 -0700815 renderengine::Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700816 position[0] = renderAreaTransform.transform(lt);
817 position[1] = renderAreaTransform.transform(lb);
818 position[2] = renderAreaTransform.transform(rb);
819 position[3] = renderAreaTransform.transform(rt);
Mathias Agopian13127d82013-03-05 17:47:11 -0800820}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800821
David Sodman41fdfc92017-11-06 16:09:56 -0800822bool Layer::isSecure() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000823 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700824 return (s.flags & layer_state_t::eLayerSecure);
825}
826
Mathias Agopian13127d82013-03-05 17:47:11 -0800827void Layer::setVisibleRegion(const Region& visibleRegion) {
828 // always called from main thread
829 this->visibleRegion = visibleRegion;
830}
831
832void Layer::setCoveredRegion(const Region& coveredRegion) {
833 // always called from main thread
834 this->coveredRegion = coveredRegion;
835}
836
David Sodman41fdfc92017-11-06 16:09:56 -0800837void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800838 // always called from main thread
839 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
840}
841
Robert Carre5f4f692018-01-12 13:12:28 -0800842void Layer::clearVisibilityRegions() {
843 visibleRegion.clear();
844 visibleNonTransparentRegion.clear();
845 coveredRegion.clear();
846}
847
Mathias Agopian13127d82013-03-05 17:47:11 -0800848// ----------------------------------------------------------------------------
849// transaction
850// ----------------------------------------------------------------------------
851
Dan Stoza7dde5992015-05-22 09:51:44 -0700852void Layer::pushPendingState() {
853 if (!mCurrentState.modified) {
854 return;
855 }
856
Dan Stoza7dde5992015-05-22 09:51:44 -0700857 // If this transaction is waiting on the receipt of a frame, generate a sync
858 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700859 // We don't allow installing sync points after we are removed from the current state
860 // as we won't be able to signal our end.
861 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700862 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800863 if (barrierLayer == nullptr) {
864 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700865 // If we can't promote the layer we are intended to wait on,
866 // then it is expired or otherwise invalid. Allow this transaction
867 // to be applied as per normal (no synchronization).
Marissa Wallf58c14b2018-07-24 10:50:43 -0700868 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800869 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700870 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy);
Robert Carr0d480722017-01-10 16:42:54 -0800871 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800872 mRemoteSyncPoints.push_back(std::move(syncPoint));
873 } else {
874 // We already missed the frame we're supposed to synchronize
875 // on, so go ahead and apply the state update
Marissa Wallf58c14b2018-07-24 10:50:43 -0700876 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800877 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700878 }
879
Dan Stoza7dde5992015-05-22 09:51:44 -0700880 // Wake us up to check if the frame has been received
881 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700882 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700883 }
884 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700885 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700886}
887
Pablo Ceballos05289c22016-04-14 15:49:55 -0700888void Layer::popPendingState(State* stateToCommit) {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700889 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700890
891 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700892 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700893}
894
Pablo Ceballos05289c22016-04-14 15:49:55 -0700895bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700896 bool stateUpdateAvailable = false;
897 while (!mPendingStates.empty()) {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700898 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700899 if (mRemoteSyncPoints.empty()) {
900 // If we don't have a sync point for this, apply it anyway. It
901 // will be visually wrong, but it should keep us from getting
902 // into too much trouble.
903 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700904 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700905 stateUpdateAvailable = true;
906 continue;
907 }
908
Marissa Wallf58c14b2018-07-24 10:50:43 -0700909 if (mRemoteSyncPoints.front()->getFrameNumber() !=
910 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800911 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800912
913 // Signal our end of the sync point and then dispose of it
914 mRemoteSyncPoints.front()->setTransactionApplied();
915 mRemoteSyncPoints.pop_front();
916 continue;
917 }
918
Dan Stoza7dde5992015-05-22 09:51:44 -0700919 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
920 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700921 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700922 stateUpdateAvailable = true;
923
924 // Signal our end of the sync point and then dispose of it
925 mRemoteSyncPoints.front()->setTransactionApplied();
926 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800927 } else {
928 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700929 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700930 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700931 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700932 stateUpdateAvailable = true;
933 }
934 }
935
936 // If we still have pending updates, wake SurfaceFlinger back up and point
937 // it at this layer so we can process them
938 if (!mPendingStates.empty()) {
939 setTransactionFlags(eTransactionNeeded);
940 mFlinger->setTransactionFlags(eTraversalNeeded);
941 }
942
943 mCurrentState.modified = false;
944 return stateUpdateAvailable;
945}
946
Marissa Wall61c58622018-07-18 10:12:20 -0700947uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000948 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800949
Marissa Wall61c58622018-07-18 10:12:20 -0700950 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
951 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700952
David Sodmaneb085e02017-10-05 18:49:04 -0700953 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700954 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000955 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800956 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
957 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
958 " requested={ wh={%4u,%4u} }}\n"
959 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
960 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700961 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700962 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
963 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
964 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
965 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
966 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700967 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
968 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
969 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800970 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700971
Robert Carre392b552017-09-19 12:16:05 -0700972 // Don't let Layer::doTransaction update the drawing state
973 // if we have a pending resize, unless we are in fixed-size mode.
974 // the drawing state will be updated only once we receive a buffer
975 // with the correct size.
976 //
977 // In particular, we want to make sure the clip (which is part
978 // of the geometry state) is latched together with the size but is
979 // latched immediately when no resizing is involved.
980 //
981 // If a sideband stream is attached, however, we want to skip this
982 // optimization so that transactions aren't missed when a buffer
983 // never arrives
984 //
985 // In the case that we don't have a buffer we ignore other factors
986 // and avoid entering the resizePending state. At a high level the
987 // resizePending state is to avoid applying the state of the new buffer
988 // to the old buffer. However in the state where we don't have an old buffer
989 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700990 const bool resizePending =
991 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
992 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
David Sodman0cc69182017-11-17 12:12:07 -0800993 (getBE().compositionInfo.mBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700994 if (!isFixedSize()) {
David Sodman0cc69182017-11-17 12:12:07 -0800995 if (resizePending && getBE().compositionInfo.hwc.sidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700996 flags |= eDontUpdateGeometryState;
997 }
998 }
999
Robert Carr7bf247e2017-05-18 14:02:49 -07001000 // Here we apply various requested geometry states, depending on our
1001 // latching configuration. See Layer.h for a detailed discussion of
1002 // how geometry latching is controlled.
1003 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +00001004 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001005
1006 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1007 // mode, which causes attributes which normally latch regardless of scaling mode,
1008 // to be delayed. We copy the requested state to the active state making sure
1009 // to respect these rules (again see Layer.h for a detailed discussion).
1010 //
1011 // There is an awkward asymmetry in the handling of the crop states in the position
1012 // states, as can be seen below. Largely this arises from position and transform
1013 // being stored in the same data structure while having different latching rules.
1014 // b/38182305
1015 //
Marissa Wall61c58622018-07-18 10:12:20 -07001016 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -07001017 // applyPendingStates in the presence of deferred transactions.
1018 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -07001019 float tx = stateToCommit->active_legacy.transform.tx();
1020 float ty = stateToCommit->active_legacy.transform.ty();
1021 stateToCommit->active_legacy = stateToCommit->requested_legacy;
1022 stateToCommit->active_legacy.transform.set(tx, ty);
1023 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001024 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001025 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -07001026 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001027 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001028 }
1029
Marissa Wall61c58622018-07-18 10:12:20 -07001030 return flags;
1031}
1032
1033uint32_t Layer::doTransaction(uint32_t flags) {
1034 ATRACE_CALL();
1035
1036 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +00001037 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -07001038 if (!applyPendingStates(&c)) {
1039 return 0;
1040 }
1041
1042 flags = doTransactionResize(flags, &c);
1043
Alec Mourib416efd2018-09-06 21:01:59 +00001044 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -07001045
1046 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001047 // invalidate and recompute the visible regions if needed
1048 flags |= Layer::eVisibleRegion;
1049 }
1050
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001051 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001052 // invalidate and recompute the visible regions if needed
1053 flags |= eVisibleRegion;
1054 this->contentDirty = true;
1055
1056 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -07001057 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -07001058 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -08001059 }
1060
Dan Stozac8145172016-04-28 16:29:06 -07001061 // If the layer is hidden, signal and clear out all local sync points so
1062 // that transactions for layers depending on this layer's frames becoming
1063 // visible are not blocked
1064 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001065 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001066 }
1067
Mathias Agopian13127d82013-03-05 17:47:11 -08001068 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001069 commitTransaction(c);
Marissa Walle2ffb422018-10-12 11:33:52 -07001070 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -08001071 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001072}
1073
Pablo Ceballos05289c22016-04-14 15:49:55 -07001074void Layer::commitTransaction(const State& stateToCommit) {
1075 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001076}
1077
Mathias Agopian13127d82013-03-05 17:47:11 -08001078uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Piquef1c675b2018-09-12 20:45:39 -07001079 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -08001080}
1081
1082uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Piquef1c675b2018-09-12 20:45:39 -07001083 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -08001084}
1085
Robert Carr82364e32016-05-15 11:27:47 -07001086bool Layer::setPosition(float x, float y, bool immediate) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001087 if (mCurrentState.requested_legacy.transform.tx() == x &&
1088 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001089 return false;
1090 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001091
1092 // We update the requested and active position simultaneously because
1093 // we want to apply the position portion of the transform matrix immediately,
1094 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Marissa Wallf58c14b2018-07-24 10:50:43 -07001095 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001096 if (immediate && !mFreezeGeometryUpdates) {
1097 // Here we directly update the active state
1098 // unlike other setters, because we store it within
1099 // the transform, but use different latching rules.
1100 // b/38182305
Marissa Wallf58c14b2018-07-24 10:50:43 -07001101 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001102 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001103 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001104
Dan Stoza7dde5992015-05-22 09:51:44 -07001105 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001106 setTransactionFlags(eTransactionNeeded);
1107 return true;
1108}
Robert Carr82364e32016-05-15 11:27:47 -07001109
Robert Carr1f0a16a2016-10-24 16:27:39 -07001110bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1111 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1112 if (idx < 0) {
1113 return false;
1114 }
1115 if (childLayer->setLayer(z)) {
1116 mCurrentChildren.removeAt(idx);
1117 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001118 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001119 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001120 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001121}
1122
Robert Carr503c7042017-09-27 15:06:08 -07001123bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1124 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1125 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1126 if (idx < 0) {
1127 return false;
1128 }
1129 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1130 mCurrentChildren.removeAt(idx);
1131 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001132 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001133 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001134 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001135}
1136
Robert Carrae060832016-11-28 10:51:00 -08001137bool Layer::setLayer(int32_t z) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001138 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001139 mCurrentState.sequence++;
1140 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001141 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001142
1143 // Discard all relative layering.
1144 if (mCurrentState.zOrderRelativeOf != nullptr) {
1145 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1146 if (strongRelative != nullptr) {
1147 strongRelative->removeZOrderRelative(this);
1148 }
1149 mCurrentState.zOrderRelativeOf = nullptr;
1150 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001151 setTransactionFlags(eTransactionNeeded);
1152 return true;
1153}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001154
Robert Carrdb66e622017-04-10 16:55:57 -07001155void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1156 mCurrentState.zOrderRelatives.remove(relative);
1157 mCurrentState.sequence++;
1158 mCurrentState.modified = true;
1159 setTransactionFlags(eTransactionNeeded);
1160}
1161
1162void Layer::addZOrderRelative(const wp<Layer>& relative) {
1163 mCurrentState.zOrderRelatives.add(relative);
1164 mCurrentState.modified = true;
1165 mCurrentState.sequence++;
1166 setTransactionFlags(eTransactionNeeded);
1167}
1168
Robert Carr503d2bd2017-12-04 15:49:47 -08001169bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001170 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1171 if (handle == nullptr) {
1172 return false;
1173 }
1174 sp<Layer> relative = handle->owner.promote();
1175 if (relative == nullptr) {
1176 return false;
1177 }
1178
Robert Carr503d2bd2017-12-04 15:49:47 -08001179 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1180 mCurrentState.zOrderRelativeOf == relative) {
1181 return false;
1182 }
1183
Robert Carrdb66e622017-04-10 16:55:57 -07001184 mCurrentState.sequence++;
1185 mCurrentState.modified = true;
Robert Carr503d2bd2017-12-04 15:49:47 -08001186 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001187
chaviw9ab4bd12017-11-03 13:11:00 -07001188 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
1189 if (oldZOrderRelativeOf != nullptr) {
1190 oldZOrderRelativeOf->removeZOrderRelative(this);
1191 }
Robert Carrdb66e622017-04-10 16:55:57 -07001192 mCurrentState.zOrderRelativeOf = relative;
1193 relative->addZOrderRelative(this);
1194
1195 setTransactionFlags(eTransactionNeeded);
1196
1197 return true;
1198}
1199
Mathias Agopian13127d82013-03-05 17:47:11 -08001200bool Layer::setSize(uint32_t w, uint32_t h) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001201 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
1202 return false;
1203 mCurrentState.requested_legacy.w = w;
1204 mCurrentState.requested_legacy.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001205 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001206 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001207
1208 // record the new size, from this point on, when the client request
1209 // a buffer, it'll get the new size.
1210 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001211 return true;
1212}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001213bool Layer::setAlpha(float alpha) {
David Sodman41fdfc92017-11-06 16:09:56 -08001214 if (mCurrentState.color.a == alpha) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001215 mCurrentState.sequence++;
chaviw13fdc492017-06-27 12:40:18 -07001216 mCurrentState.color.a = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001217 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001218 setTransactionFlags(eTransactionNeeded);
1219 return true;
1220}
chaviw13fdc492017-06-27 12:40:18 -07001221
1222bool Layer::setColor(const half3& color) {
David Sodman41fdfc92017-11-06 16:09:56 -08001223 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g &&
1224 color.b == mCurrentState.color.b)
chaviw13fdc492017-06-27 12:40:18 -07001225 return false;
1226
1227 mCurrentState.sequence++;
1228 mCurrentState.color.r = color.r;
1229 mCurrentState.color.g = color.g;
1230 mCurrentState.color.b = color.b;
1231 mCurrentState.modified = true;
1232 setTransactionFlags(eTransactionNeeded);
1233 return true;
1234}
1235
Robert Carrd4ae7f32018-06-07 16:10:57 -07001236bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1237 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001238 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001239 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1240
1241 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1242 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1243 return false;
1244 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001245 mCurrentState.sequence++;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001246 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1247 matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001248 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001249 setTransactionFlags(eTransactionNeeded);
1250 return true;
1251}
Marissa Wall61c58622018-07-18 10:12:20 -07001252
Mathias Agopian13127d82013-03-05 17:47:11 -08001253bool Layer::setTransparentRegionHint(const Region& transparent) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001254 mCurrentState.requestedTransparentRegion_legacy = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001255 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001256 setTransactionFlags(eTransactionNeeded);
1257 return true;
1258}
1259bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1260 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
David Sodman41fdfc92017-11-06 16:09:56 -08001261 if (mCurrentState.flags == newFlags) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001262 mCurrentState.sequence++;
1263 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001264 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001265 setTransactionFlags(eTransactionNeeded);
1266 return true;
1267}
Robert Carr99e27f02016-06-16 15:18:02 -07001268
Marissa Wallf58c14b2018-07-24 10:50:43 -07001269bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
1270 if (mCurrentState.requestedCrop_legacy == crop) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001271 mCurrentState.sequence++;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001272 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001273 if (immediate && !mFreezeGeometryUpdates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001274 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001275 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001276 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1277
Dan Stoza7dde5992015-05-22 09:51:44 -07001278 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001279 setTransactionFlags(eTransactionNeeded);
1280 return true;
1281}
Robert Carr8d5227b2017-03-16 15:41:03 -07001282
Robert Carrc3574f72016-03-24 12:19:32 -07001283bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001284 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001285 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001286 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001287 return true;
1288}
1289
rongliucfb187b2018-03-14 12:26:23 -07001290void Layer::setInfo(int32_t type, int32_t appId) {
David Sodman41fdfc92017-11-06 16:09:56 -08001291 mCurrentState.appId = appId;
1292 mCurrentState.type = type;
1293 mCurrentState.modified = true;
1294 setTransactionFlags(eTransactionNeeded);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001295}
1296
Mathias Agopian13127d82013-03-05 17:47:11 -08001297bool Layer::setLayerStack(uint32_t layerStack) {
David Sodman41fdfc92017-11-06 16:09:56 -08001298 if (mCurrentState.layerStack == layerStack) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001299 mCurrentState.sequence++;
1300 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001301 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001302 setTransactionFlags(eTransactionNeeded);
1303 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001304}
1305
Robert Carr1f0a16a2016-10-24 16:27:39 -07001306uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001307 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001308 if (p == nullptr) {
1309 return getDrawingState().layerStack;
1310 }
1311 return p->getLayerStack();
1312}
1313
Marissa Wallf58c14b2018-07-24 10:50:43 -07001314void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
1315 mCurrentState.barrierLayer_legacy = barrierLayer;
1316 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001317 // We don't set eTransactionNeeded, because just receiving a deferral
1318 // request without any other state updates shouldn't actually induce a delay
1319 mCurrentState.modified = true;
1320 pushPendingState();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001321 mCurrentState.barrierLayer_legacy = nullptr;
1322 mCurrentState.frameNumber_legacy = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001323 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001324}
1325
Marissa Wallf58c14b2018-07-24 10:50:43 -07001326void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001327 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001328 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001329}
1330
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001331// ----------------------------------------------------------------------------
1332// pageflip handling...
1333// ----------------------------------------------------------------------------
1334
Robert Carr1f0a16a2016-10-24 16:27:39 -07001335bool Layer::isHiddenByPolicy() const {
Alec Mourib416efd2018-09-06 21:01:59 +00001336 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001337 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001338 if (parent != nullptr && parent->isHiddenByPolicy()) {
1339 return true;
1340 }
1341 return s.flags & layer_state_t::eLayerHidden;
1342}
1343
David Sodman41fdfc92017-11-06 16:09:56 -08001344uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001345 // TODO: should we do something special if mSecure is set?
1346 if (mProtectedByApp) {
1347 // need a hardware-protected path to external video sink
1348 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001349 }
Riley Andrews03414a12014-07-01 14:22:59 -07001350 if (mPotentialCursor) {
1351 usage |= GraphicBuffer::USAGE_CURSOR;
1352 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001353 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001354 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001355}
1356
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001357void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001358 uint32_t orientation = 0;
1359 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001360 // The transform hint is used to improve performance, but we can
1361 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001362 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001363 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001364 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001365 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001366 orientation = 0;
1367 }
1368 }
David Sodmaneb085e02017-10-05 18:49:04 -07001369 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001370}
1371
Mathias Agopian13127d82013-03-05 17:47:11 -08001372// ----------------------------------------------------------------------------
1373// debugging
1374// ----------------------------------------------------------------------------
1375
Marissa Wall61c58622018-07-18 10:12:20 -07001376// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001377LayerDebugInfo Layer::getLayerDebugInfo() const {
1378 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001379 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001380 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001381 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001382 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
1383 info.mType = String8(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001384 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001385 info.mVisibleRegion = visibleRegion;
1386 info.mSurfaceDamageRegion = surfaceDamageRegion;
1387 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001388 info.mX = ds.active_legacy.transform.tx();
1389 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001390 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001391 info.mWidth = ds.active_legacy.w;
1392 info.mHeight = ds.active_legacy.h;
1393 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001394 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001395 info.mFlags = ds.flags;
1396 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001397 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001398 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1399 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1400 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1401 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001402 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001403 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001404 if (buffer != 0) {
1405 info.mActiveBufferWidth = buffer->getWidth();
1406 info.mActiveBufferHeight = buffer->getHeight();
1407 info.mActiveBufferStride = buffer->getStride();
1408 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001409 } else {
1410 info.mActiveBufferWidth = 0;
1411 info.mActiveBufferHeight = 0;
1412 info.mActiveBufferStride = 0;
1413 info.mActiveBufferFormat = 0;
1414 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001415 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001416 info.mNumQueuedFrames = getQueuedFrameCount();
1417 info.mRefreshPending = isBufferLatched();
1418 info.mIsOpaque = isOpaque(ds);
1419 info.mContentDirty = contentDirty;
1420 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001421}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001422
Dan Stozae22aec72016-08-01 13:20:59 -07001423void Layer::miniDumpHeader(String8& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001424 result.append("-------------------------------");
1425 result.append("-------------------------------");
1426 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001427 result.append(" Layer name\n");
1428 result.append(" Z | ");
1429 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001430 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001431 result.append(" Disp Frame (LTRB) | ");
1432 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001433 result.append("-------------------------------");
1434 result.append("-------------------------------");
1435 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001436}
1437
Dominik Laskowski075d3172018-05-24 15:50:06 -07001438void Layer::miniDump(String8& result, DisplayId displayId) const {
Dominik Laskowski34157762018-10-31 13:07:19 -07001439 if (!hasHwcLayer(displayId)) {
Dan Stozae22aec72016-08-01 13:20:59 -07001440 return;
1441 }
1442
1443 String8 name;
1444 if (mName.length() > 77) {
1445 std::string shortened;
1446 shortened.append(mName.string(), 36);
1447 shortened.append("[...]");
1448 shortened.append(mName.string() + (mName.length() - 36), 36);
1449 name = shortened.c_str();
1450 } else {
1451 name = mName;
1452 }
1453
1454 result.appendFormat(" %s\n", name.string());
1455
Alec Mourib416efd2018-09-06 21:01:59 +00001456 const State& layerState(getDrawingState());
Lloyd Pique074e8122018-07-26 12:57:23 -07001457 const LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers.at(displayId);
Chia-I Wu1e043612018-03-01 09:45:09 -08001458 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
1459 result.appendFormat(" rel %6d | ", layerState.z);
1460 } else {
1461 result.appendFormat(" %10d | ", layerState.z);
1462 }
Dominik Laskowski7e045462018-05-30 13:02:02 -07001463 result.appendFormat("%10s | ", to_string(getCompositionType(displayId)).c_str());
Lloyd Pique074e8122018-07-26 12:57:23 -07001464 result.appendFormat("%10s | ", to_string(hwcInfo.transform).c_str());
1465 const Rect& frame = hwcInfo.displayFrame;
David Sodman41fdfc92017-11-06 16:09:56 -08001466 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique074e8122018-07-26 12:57:23 -07001467 const FloatRect& crop = hwcInfo.sourceCrop;
David Sodman41fdfc92017-11-06 16:09:56 -08001468 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 -07001469
David Sodman7e4ae112018-02-09 15:02:28 -08001470 result.append("- - - - - - - - - - - - - - - -\n");
1471
1472 std::string compositionInfoStr;
1473 getBE().compositionInfo.dump(compositionInfoStr, "compositionInfo");
1474 result.append(compositionInfoStr.c_str());
1475
Yichi Chen6ca35192018-05-29 12:20:43 +08001476 result.append("- - - - - - - - - - - - - - - -");
1477 result.append("- - - - - - - - - - - - - - - -");
1478 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001479}
Dan Stozae22aec72016-08-01 13:20:59 -07001480
Svetoslavd85084b2014-03-20 10:28:31 -07001481void Layer::dumpFrameStats(String8& result) const {
1482 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001483}
1484
Svetoslavd85084b2014-03-20 10:28:31 -07001485void Layer::clearFrameStats() {
1486 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001487}
1488
Jamie Gennis6547ff42013-07-16 20:12:42 -07001489void Layer::logFrameStats() {
1490 mFrameTracker.logAndResetStats(mName);
1491}
1492
Svetoslavd85084b2014-03-20 10:28:31 -07001493void Layer::getFrameStats(FrameStats* outStats) const {
1494 mFrameTracker.getStats(outStats);
1495}
1496
Brian Andersond6927fb2016-07-23 23:37:30 -07001497void Layer::dumpFrameEvents(String8& result) {
David Sodman41fdfc92017-11-06 16:09:56 -08001498 result.appendFormat("- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001499 Mutex::Autolock lock(mFrameEventHistoryMutex);
1500 mFrameEventHistory.checkFencesForCompletion();
1501 mFrameEventHistory.dump(result);
1502}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001503
Brian Anderson5ea5e592016-12-01 16:54:33 -08001504void Layer::onDisconnect() {
1505 Mutex::Autolock lock(mFrameEventHistoryMutex);
1506 mFrameEventHistory.onDisconnect();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -07001507 mTimeStats.onDisconnect(getSequence());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001508}
1509
Brian Anderson3890c392016-07-25 12:48:08 -07001510void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001511 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001512 if (newTimestamps) {
Yiwei Zhang8e8fe522018-11-02 18:34:07 -07001513 mTimeStats.setPostTime(getSequence(), newTimestamps->frameNumber, getName().c_str(),
1514 newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001515 }
1516
Brian Andersond6927fb2016-07-23 23:37:30 -07001517 Mutex::Autolock lock(mFrameEventHistoryMutex);
1518 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001519 // If there are any unsignaled fences in the aquire timeline at this
1520 // point, the previously queued frame hasn't been latched yet. Go ahead
1521 // and try to get the signal time here so the syscall is taken out of
1522 // the main thread's critical path.
1523 mAcquireTimeline.updateSignalTimes();
1524 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001525 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001526 mFrameEventHistory.addQueue(*newTimestamps);
1527 }
1528
Brian Anderson3890c392016-07-25 12:48:08 -07001529 if (outDelta) {
1530 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001531 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001532}
Dan Stozae77c7662016-05-13 11:37:28 -07001533
Chia-I Wu98f1c102017-05-30 14:54:08 -07001534size_t Layer::getChildrenCount() const {
1535 size_t count = 0;
1536 for (const sp<Layer>& child : mCurrentChildren) {
1537 count += 1 + child->getChildrenCount();
1538 }
1539 return count;
1540}
1541
Robert Carr1f0a16a2016-10-24 16:27:39 -07001542void Layer::addChild(const sp<Layer>& layer) {
1543 mCurrentChildren.add(layer);
1544 layer->setParent(this);
1545}
1546
1547ssize_t Layer::removeChild(const sp<Layer>& layer) {
1548 layer->setParent(nullptr);
1549 return mCurrentChildren.remove(layer);
1550}
1551
Robert Carr1db73f62016-12-21 12:58:51 -08001552bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1553 sp<Handle> handle = nullptr;
1554 sp<Layer> newParent = nullptr;
1555 if (newParentHandle == nullptr) {
1556 return false;
1557 }
1558 handle = static_cast<Handle*>(newParentHandle.get());
1559 newParent = handle->owner.promote();
1560 if (newParent == nullptr) {
1561 ALOGE("Unable to promote Layer handle");
1562 return false;
1563 }
1564
1565 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001566 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001567
1568 sp<Client> client(child->mClientRef.promote());
1569 if (client != nullptr) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001570 client->updateParent(newParent);
Robert Carr1db73f62016-12-21 12:58:51 -08001571 }
1572 }
1573 mCurrentChildren.clear();
1574
1575 return true;
1576}
1577
Robert Carr15eae092018-03-23 13:43:53 -07001578void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001579 for (const sp<Layer>& child : mDrawingChildren) {
1580 child->mDrawingParent = newParent;
1581 }
1582}
1583
chaviwf1961f72017-09-18 16:41:07 -07001584bool Layer::reparent(const sp<IBinder>& newParentHandle) {
1585 if (newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001586 return false;
1587 }
1588
1589 auto handle = static_cast<Handle*>(newParentHandle.get());
1590 sp<Layer> newParent = handle->owner.promote();
1591 if (newParent == nullptr) {
1592 ALOGE("Unable to promote Layer handle");
1593 return false;
1594 }
1595
chaviwf1961f72017-09-18 16:41:07 -07001596 sp<Layer> parent = getParent();
1597 if (parent != nullptr) {
1598 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001599 }
chaviwf1961f72017-09-18 16:41:07 -07001600 newParent->addChild(this);
chaviw06178942017-07-27 10:25:59 -07001601
chaviw61626f22018-11-15 16:26:27 -08001602 if (!newParent->isRemovedFromCurrentState()) {
1603 addToCurrentState();
1604 }
1605
chaviwf1961f72017-09-18 16:41:07 -07001606 sp<Client> client(mClientRef.promote());
chaviw06178942017-07-27 10:25:59 -07001607 sp<Client> newParentClient(newParent->mClientRef.promote());
1608
chaviwf1961f72017-09-18 16:41:07 -07001609 if (client != newParentClient) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001610 client->updateParent(newParent);
chaviw06178942017-07-27 10:25:59 -07001611 }
1612
chaviw06178942017-07-27 10:25:59 -07001613 return true;
1614}
1615
Robert Carr9524cb32017-02-13 11:32:32 -08001616bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001617 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001618 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001619 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001620 if (client != nullptr && parentClient != client) {
Robert Carr7f619b22017-11-06 12:56:35 -08001621 client->detachLayer(child.get());
1622 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001623 }
Robert Carr7f619b22017-11-06 12:56:35 -08001624 }
Robert Carr9524cb32017-02-13 11:32:32 -08001625
1626 return true;
1627}
1628
Peiyong Lind3788632018-09-18 16:01:31 -07001629bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001630 static const mat4 identityMatrix = mat4();
1631
Peiyong Lind3788632018-09-18 16:01:31 -07001632 if (mCurrentState.colorTransform == matrix) {
1633 return false;
1634 }
1635 ++mCurrentState.sequence;
1636 mCurrentState.colorTransform = matrix;
Peiyong Lin747321c2018-10-01 10:03:11 -07001637 mCurrentState.hasColorTransform = matrix != identityMatrix;
1638 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001639 setTransactionFlags(eTransactionNeeded);
1640 return true;
1641}
1642
1643const mat4& Layer::getColorTransform() const {
1644 return getDrawingState().colorTransform;
1645}
1646
1647bool Layer::hasColorTransform() const {
Peiyong Lin747321c2018-10-01 10:03:11 -07001648 return getDrawingState().hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001649}
1650
Chia-I Wu11481472018-05-04 10:43:19 -07001651bool Layer::isLegacyDataSpace() const {
1652 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001653 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001654 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001655}
1656
Robert Carr1f0a16a2016-10-24 16:27:39 -07001657void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001658 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001659}
1660
1661void Layer::clearSyncPoints() {
1662 for (const auto& child : mCurrentChildren) {
1663 child->clearSyncPoints();
1664 }
1665
1666 Mutex::Autolock lock(mLocalSyncPointMutex);
1667 for (auto& point : mLocalSyncPoints) {
1668 point->setFrameAvailable();
1669 }
1670 mLocalSyncPoints.clear();
1671}
1672
1673int32_t Layer::getZ() const {
1674 return mDrawingState.z;
1675}
1676
Robert Carr29abff82017-12-04 13:51:20 -08001677bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1678 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1679 const State& state = useDrawing ? mDrawingState : mCurrentState;
1680 return state.zOrderRelativeOf != nullptr;
1681}
1682
David Sodman41fdfc92017-11-06 16:09:56 -08001683__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001684 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001685 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1686 "makeTraversalList received invalid stateSet");
1687 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1688 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1689 const State& state = useDrawing ? mDrawingState : mCurrentState;
1690
Robert Carr29abff82017-12-04 13:51:20 -08001691 if (state.zOrderRelatives.size() == 0) {
1692 *outSkipRelativeZUsers = true;
1693 return children;
1694 }
1695
chaviwfd462612018-05-31 16:11:27 -07001696 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001697 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001698 sp<Layer> strongRelative = weakRelative.promote();
1699 if (strongRelative != nullptr) {
1700 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001701 }
1702 }
1703
Dan Stoza412903f2017-04-27 13:42:17 -07001704 for (const sp<Layer>& child : children) {
Robert Carr503c7042017-09-27 15:06:08 -07001705 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1706 if (childState.zOrderRelativeOf != nullptr) {
1707 continue;
1708 }
Robert Carrdb66e622017-04-10 16:55:57 -07001709 traverse.add(child);
1710 }
1711
1712 return traverse;
1713}
1714
Robert Carr1f0a16a2016-10-24 16:27:39 -07001715/**
Robert Carrdb66e622017-04-10 16:55:57 -07001716 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001717 */
Dan Stoza412903f2017-04-27 13:42:17 -07001718void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001719 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1720 // produce a new list for traversing, including our relatives, and not including our children
1721 // who are relatives of another surface. In the case that there are no relative Z,
1722 // makeTraversalList returns our children directly to avoid significant overhead.
1723 // However in this case we need to take the responsibility for filtering children which
1724 // are relatives of another surface here.
1725 bool skipRelativeZUsers = false;
1726 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001727
Robert Carr1f0a16a2016-10-24 16:27:39 -07001728 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001729 for (; i < list.size(); i++) {
1730 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001731 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1732 continue;
1733 }
1734
Robert Carrdb66e622017-04-10 16:55:57 -07001735 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001736 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001737 }
Dan Stoza412903f2017-04-27 13:42:17 -07001738 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001739 }
Robert Carr29abff82017-12-04 13:51:20 -08001740
Dan Stoza412903f2017-04-27 13:42:17 -07001741 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001742 for (; i < list.size(); i++) {
1743 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001744
1745 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1746 continue;
1747 }
Dan Stoza412903f2017-04-27 13:42:17 -07001748 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001749 }
1750}
1751
1752/**
Robert Carrdb66e622017-04-10 16:55:57 -07001753 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001754 */
Dan Stoza412903f2017-04-27 13:42:17 -07001755void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1756 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001757 // See traverseInZOrder for documentation.
1758 bool skipRelativeZUsers = false;
1759 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001760
Robert Carr1f0a16a2016-10-24 16:27:39 -07001761 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001762 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001763 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001764
1765 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1766 continue;
1767 }
1768
Robert Carrdb66e622017-04-10 16:55:57 -07001769 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001770 break;
1771 }
Dan Stoza412903f2017-04-27 13:42:17 -07001772 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001773 }
Dan Stoza412903f2017-04-27 13:42:17 -07001774 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001775 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001776 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001777
1778 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1779 continue;
1780 }
1781
Dan Stoza412903f2017-04-27 13:42:17 -07001782 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001783 }
1784}
1785
chaviw4b129c22018-04-09 16:19:43 -07001786LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1787 const std::vector<Layer*>& layersInTree) {
1788 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1789 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001790 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1791 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
chaviw4b129c22018-04-09 16:19:43 -07001792 const State& state = useDrawing ? mDrawingState : mCurrentState;
1793
chaviwfd462612018-05-31 16:11:27 -07001794 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001795 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1796 sp<Layer> strongRelative = weakRelative.promote();
1797 // Only add relative layers that are also descendents of the top most parent of the tree.
1798 // If a relative layer is not a descendent, then it should be ignored.
1799 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1800 traverse.add(strongRelative);
1801 }
1802 }
1803
1804 for (const sp<Layer>& child : children) {
1805 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1806 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1807 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1808 // the child here since it won't be added later as a relative.
1809 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1810 childState.zOrderRelativeOf.promote().get())) {
1811 continue;
1812 }
1813 traverse.add(child);
1814 }
1815
1816 return traverse;
1817}
1818
1819void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1820 LayerVector::StateSet stateSet,
1821 const LayerVector::Visitor& visitor) {
1822 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001823
1824 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001825 for (; i < list.size(); i++) {
1826 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001827 if (relative->getZ() >= 0) {
1828 break;
1829 }
chaviw4b129c22018-04-09 16:19:43 -07001830 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001831 }
chaviw4b129c22018-04-09 16:19:43 -07001832
chaviwa76b2712017-09-20 12:02:26 -07001833 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001834 for (; i < list.size(); i++) {
1835 const auto& relative = list[i];
1836 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001837 }
1838}
1839
chaviw4b129c22018-04-09 16:19:43 -07001840std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1841 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1842 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1843
1844 std::vector<Layer*> layersInTree = {this};
1845 for (size_t i = 0; i < children.size(); i++) {
1846 const auto& child = children[i];
1847 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1848 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1849 }
1850
1851 return layersInTree;
1852}
1853
1854void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1855 const LayerVector::Visitor& visitor) {
1856 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1857 std::sort(layersInTree.begin(), layersInTree.end());
1858 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1859}
1860
Peiyong Linefefaac2018-08-17 12:27:51 -07001861ui::Transform Layer::getTransform() const {
1862 ui::Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001863 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001864 if (p != nullptr) {
1865 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07001866
1867 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
1868 // it isFixedSize) then there may be additional scaling not accounted
1869 // for in the transform. We need to mirror this scaling in child surfaces
1870 // or we will break the contract where WM can treat child surfaces as
1871 // pixels in the parent surface.
David Sodman0cc69182017-11-17 12:12:07 -08001872 if (p->isFixedSize() && p->getBE().compositionInfo.mBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07001873 int bufferWidth;
1874 int bufferHeight;
1875 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
David Sodman0cc69182017-11-17 12:12:07 -08001876 bufferWidth = p->getBE().compositionInfo.mBuffer->getWidth();
1877 bufferHeight = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001878 } else {
David Sodman0cc69182017-11-17 12:12:07 -08001879 bufferHeight = p->getBE().compositionInfo.mBuffer->getWidth();
1880 bufferWidth = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001881 }
Marissa Wall61c58622018-07-18 10:12:20 -07001882 float sx = p->getActiveWidth(p->getDrawingState()) / static_cast<float>(bufferWidth);
1883 float sy = p->getActiveHeight(p->getDrawingState()) / static_cast<float>(bufferHeight);
Peiyong Linefefaac2018-08-17 12:27:51 -07001884 ui::Transform extraParentScaling;
Robert Carr9b429f42017-04-17 14:56:57 -07001885 extraParentScaling.set(sx, 0, 0, sy);
1886 t = t * extraParentScaling;
1887 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001888 }
Marissa Wall61c58622018-07-18 10:12:20 -07001889 return t * getActiveTransform(getDrawingState());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001890}
1891
chaviw13fdc492017-06-27 12:40:18 -07001892half Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001893 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07001894
chaviw13fdc492017-06-27 12:40:18 -07001895 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1896 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001897}
Robert Carr6452f122017-03-21 10:41:29 -07001898
chaviw13fdc492017-06-27 12:40:18 -07001899half4 Layer::getColor() const {
1900 const half4 color(getDrawingState().color);
1901 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001902}
Robert Carr6452f122017-03-21 10:41:29 -07001903
Robert Carr1f0a16a2016-10-24 16:27:39 -07001904void Layer::commitChildList() {
1905 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1906 const auto& child = mCurrentChildren[i];
1907 child->commitChildList();
1908 }
1909 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001910 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001911}
1912
chaviw1d044282017-09-27 12:19:28 -07001913void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
1914 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1915 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1916 const State& state = useDrawing ? mDrawingState : mCurrentState;
1917
Peiyong Linefefaac2018-08-17 12:27:51 -07001918 ui::Transform requestedTransform = state.active_legacy.transform;
1919 ui::Transform transform = getTransform();
chaviw1d044282017-09-27 12:19:28 -07001920
1921 layerInfo->set_id(sequence);
1922 layerInfo->set_name(getName().c_str());
1923 layerInfo->set_type(String8(getTypeId()));
1924
1925 for (const auto& child : children) {
1926 layerInfo->add_children(child->sequence);
1927 }
1928
1929 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1930 sp<Layer> strongRelative = weakRelative.promote();
1931 if (strongRelative != nullptr) {
1932 layerInfo->add_relatives(strongRelative->sequence);
1933 }
1934 }
1935
Marissa Wallf58c14b2018-07-24 10:50:43 -07001936 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
chaviw1d044282017-09-27 12:19:28 -07001937 layerInfo->mutable_transparent_region());
1938 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
1939 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
1940
1941 layerInfo->set_layer_stack(getLayerStack());
1942 layerInfo->set_z(state.z);
1943
1944 PositionProto* position = layerInfo->mutable_position();
1945 position->set_x(transform.tx());
1946 position->set_y(transform.ty());
1947
1948 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
1949 requestedPosition->set_x(requestedTransform.tx());
1950 requestedPosition->set_y(requestedTransform.ty());
1951
1952 SizeProto* size = layerInfo->mutable_size();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001953 size->set_w(state.active_legacy.w);
1954 size->set_h(state.active_legacy.h);
chaviw1d044282017-09-27 12:19:28 -07001955
Marissa Wallf58c14b2018-07-24 10:50:43 -07001956 LayerProtoHelper::writeToProto(state.crop_legacy, layerInfo->mutable_crop());
chaviw1d044282017-09-27 12:19:28 -07001957
1958 layerInfo->set_is_opaque(isOpaque(state));
1959 layerInfo->set_invalidate(contentDirty);
Chia-I Wu01591c92018-05-22 12:03:00 -07001960
1961 // XXX (b/79210409) mCurrentDataSpace is not protected
1962 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
1963
chaviw1d044282017-09-27 12:19:28 -07001964 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1965 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
1966 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
1967 layerInfo->set_flags(state.flags);
1968
1969 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1970 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
1971
Jorim Jaggi8e0af362017-11-14 16:28:28 +01001972 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07001973 if (parent != nullptr) {
1974 layerInfo->set_parent(parent->sequence);
1975 }
1976
1977 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1978 if (zOrderRelativeOf != nullptr) {
1979 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1980 }
1981
Chia-I Wu01591c92018-05-22 12:03:00 -07001982 // XXX getBE().compositionInfo.mBuffer is not protected
David Sodman0cc69182017-11-17 12:12:07 -08001983 auto buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001984 if (buffer != nullptr) {
1985 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
Peiyong Linefefaac2018-08-17 12:27:51 -07001986 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
Yichi Chen6ca35192018-05-29 12:20:43 +08001987 layerInfo->mutable_buffer_transform());
chaviw1d044282017-09-27 12:19:28 -07001988 }
1989
1990 layerInfo->set_queued_frames(getQueuedFrameCount());
1991 layerInfo->set_refresh_pending(isBufferLatched());
rongliucfb187b2018-03-14 12:26:23 -07001992 layerInfo->set_window_type(state.type);
1993 layerInfo->set_app_id(state.appId);
chaviwadc40c22018-07-10 16:57:27 -07001994 layerInfo->set_curr_frame(mCurrentFrameNumber);
1995
1996 for (const auto& pendingState : mPendingStates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001997 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
chaviwadc40c22018-07-10 16:57:27 -07001998 if (barrierLayer != nullptr) {
1999 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
2000 barrierLayerProto->set_id(barrierLayer->sequence);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002001 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
chaviwadc40c22018-07-10 16:57:27 -07002002 }
2003 }
chaviw1d044282017-09-27 12:19:28 -07002004}
2005
Dominik Laskowski075d3172018-05-24 15:50:06 -07002006void Layer::writeToProto(LayerProto* layerInfo, DisplayId displayId) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07002007 if (!hasHwcLayer(displayId)) {
2008 return;
2009 }
2010
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002011 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
2012
Dominik Laskowski7e045462018-05-30 13:02:02 -07002013 const auto& hwcInfo = getBE().mHwcLayers.at(displayId);
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002014
2015 const Rect& frame = hwcInfo.displayFrame;
2016 LayerProtoHelper::writeToProto(frame, layerInfo->mutable_hwc_frame());
2017
2018 const FloatRect& crop = hwcInfo.sourceCrop;
2019 LayerProtoHelper::writeToProto(crop, layerInfo->mutable_hwc_crop());
2020
2021 const int32_t transform = static_cast<int32_t>(hwcInfo.transform);
2022 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002023
2024 const int32_t compositionType = static_cast<int32_t>(hwcInfo.compositionType);
2025 layerInfo->set_hwc_composition_type(compositionType);
2026
2027 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
2028 static_cast<BufferLayer*>(this)->isProtected()) {
2029 layerInfo->set_is_protected(true);
2030 } else {
2031 layerInfo->set_is_protected(false);
2032 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002033}
2034
Robert Carr2e102c92018-10-23 12:11:15 -07002035bool Layer::isRemovedFromCurrentState() const {
2036 return mRemovedFromCurrentState;
2037}
2038
Mathias Agopian13127d82013-03-05 17:47:11 -08002039// ---------------------------------------------------------------------------
2040
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002041}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002042
2043#if defined(__gl_h_)
2044#error "don't include gl/gl.h in this file"
2045#endif
2046
2047#if defined(__gl2_h_)
2048#error "don't include gl2/gl2.h in this file"
2049#endif