blob: 58fd86618e4179d92400af699913fe6b56142872 [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.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700382
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800383 FloatRect activeCropFloat = computeBounds();
Peiyong Linefefaac2018-08-17 12:27:51 -0700384 ui::Transform t = getTransform();
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800385 // Transform to screen space.
386 activeCropFloat = t.transform(activeCropFloat);
387 activeCropFloat = activeCropFloat.intersect(display->getViewport().toFloatRect());
388 // Back to layer space to work with the content crop.
389 activeCropFloat = t.inverse().transform(activeCropFloat);
390 // This needs to be here as transform.transform(Rect) computes the
391 // transformed rect and then takes the bounding box of the result before
392 // returning. This means
393 // transform.inverse().transform(transform.transform(Rect)) != Rect
394 // in which case we need to make sure the final rect is clipped to the
395 // display bounds.
396 Rect activeCrop{activeCropFloat};
397 if (!activeCrop.intersect(getBufferSize(s), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000398 activeCrop.clear();
399 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700400 return activeCrop;
401}
402
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700403FloatRect Layer::computeCrop(const sp<const DisplayDevice>& display) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700404 // the content crop is the area of the content that gets scaled to the
405 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800406 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700407
408 // In addition there is a WM-specified crop we pull from our drawing state.
409 const State& s(getDrawingState());
410
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700411 Rect activeCrop = computeInitialCrop(display);
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800412 Rect bufferSize = getBufferSize(s);
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700413
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000414 // Transform the window crop to match the buffer coordinate system,
415 // which means using the inverse of the current transform set on the
416 // SurfaceFlingerConsumer.
417 uint32_t invTransform = mCurrentTransform;
Robert Carrcae605c2017-03-29 12:10:31 -0700418 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000419 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700420 * the code below applies the primary display's inverse transform to the
421 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000422 */
David Sodman41fdfc92017-11-06 16:09:56 -0800423 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000424 // calculate the inverse transform
425 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800426 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800427 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000428 // and apply to the current transform
Peiyong Linefefaac2018-08-17 12:27:51 -0700429 invTransform = (ui::Transform(invTransformOrient) *
430 ui::Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800431 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000432
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800433 int winWidth = bufferSize.getWidth();
434 int winHeight = bufferSize.getHeight();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000435 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
436 // If the activeCrop has been rotate the ends are rotated but not
437 // the space itself so when transforming ends back we can't rely on
438 // a modification of the axes of rotation. To account for this we
439 // need to reorient the inverse rotation in terms of the current
440 // axes of rotation.
441 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
442 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
443 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800444 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000445 }
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800446 std::swap(winWidth, winHeight);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000447 }
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800448 const Rect winCrop =
449 activeCrop.transform(invTransform, bufferSize.getWidth(), bufferSize.getHeight());
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000450
451 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800452 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000453 float yScale = crop.getHeight() / float(winHeight);
454
David Sodman41fdfc92017-11-06 16:09:56 -0800455 float insetL = winCrop.left * xScale;
456 float insetT = winCrop.top * yScale;
457 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000458 float insetB = (winHeight - winCrop.bottom) * yScale;
459
David Sodman41fdfc92017-11-06 16:09:56 -0800460 crop.left += insetL;
461 crop.top += insetT;
462 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000463 crop.bottom -= insetB;
464
Mathias Agopian13127d82013-03-05 17:47:11 -0800465 return crop;
466}
467
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700468void Layer::setGeometry(const sp<const DisplayDevice>& display, uint32_t z) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700469 const auto displayId = display->getId();
Dominik Laskowski075d3172018-05-24 15:50:06 -0700470 LOG_ALWAYS_FATAL_IF(!displayId);
Dominik Laskowski34157762018-10-31 13:07:19 -0700471 RETURN_IF_NO_HWC_LAYER(*displayId);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700472 auto& hwcInfo = getBE().mHwcLayers[*displayId];
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700473
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700474 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800475 hwcInfo.forceClientComposition = false;
476
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700477 if (isSecure() && !display->isSecure()) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800478 hwcInfo.forceClientComposition = true;
479 }
480
David Sodman15094112018-10-11 09:39:37 -0700481 auto& hwcLayer = hwcInfo.layer;
482
Mathias Agopian13127d82013-03-05 17:47:11 -0800483 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700484 const State& s(getDrawingState());
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800485 const int bufferWidth = getBufferSize(s).getWidth();
486 const int bufferHeight = getBufferSize(s).getHeight();
David Revemanecf0fa52017-03-03 11:32:44 -0500487 auto blendMode = HWC2::BlendMode::None;
Robert Carr6452f122017-03-21 10:41:29 -0700488 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800489 blendMode =
490 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800491 }
David Sodman15094112018-10-11 09:39:37 -0700492 auto error = hwcLayer->setBlendMode(blendMode);
493 ALOGE_IF(error != HWC2::Error::None,
494 "[%s] Failed to set blend mode %s:"
495 " %s (%d)",
496 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
497 static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700498 getBE().compositionInfo.hwc.blendMode = blendMode;
Mathias Agopian13127d82013-03-05 17:47:11 -0800499
500 // apply the layer's transform, followed by the display's global transform
501 // here we're guaranteed that the layer's transform preserves rects
Marissa Wall61c58622018-07-18 10:12:20 -0700502 Region activeTransparentRegion(getActiveTransparentRegion(s));
Peiyong Linefefaac2018-08-17 12:27:51 -0700503 ui::Transform t = getTransform();
Marissa Wall61c58622018-07-18 10:12:20 -0700504 Rect activeCrop = getCrop(s);
505 if (!activeCrop.isEmpty()) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700506 activeCrop = t.transform(activeCrop);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700507 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000508 activeCrop.clear();
509 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700510 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800511 // This needs to be here as transform.transform(Rect) computes the
512 // transformed rect and then takes the bounding box of the result before
513 // returning. This means
514 // transform.inverse().transform(transform.transform(Rect)) != Rect
515 // in which case we need to make sure the final rect is clipped to the
516 // display bounds.
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800517 if (!activeCrop.intersect(Rect(bufferWidth, bufferHeight), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000518 activeCrop.clear();
519 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700520 // mark regions outside the crop as transparent
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800521 activeTransparentRegion.orSelf(Rect(0, 0, bufferWidth, activeCrop.top));
522 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom, bufferWidth, bufferHeight));
David Sodman41fdfc92017-11-06 16:09:56 -0800523 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
524 activeTransparentRegion.orSelf(
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800525 Rect(activeCrop.right, activeCrop.top, bufferWidth, activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700526 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700527
Dan Stoza80d61162017-12-20 15:57:52 -0800528 // computeBounds returns a FloatRect to provide more accuracy during the
529 // transformation. We then round upon constructing 'frame'.
530 Rect frame{t.transform(computeBounds(activeTransparentRegion))};
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700531 if (!frame.intersect(display->getViewport(), &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000532 frame.clear();
533 }
Peiyong Linefefaac2018-08-17 12:27:51 -0700534 const ui::Transform& tr = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800535 Rect transformedFrame = tr.transform(frame);
David Sodman15094112018-10-11 09:39:37 -0700536 error = hwcLayer->setDisplayFrame(transformedFrame);
537 if (error != HWC2::Error::None) {
538 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
539 transformedFrame.left, transformedFrame.top, transformedFrame.right,
540 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
541 } else {
542 hwcInfo.displayFrame = transformedFrame;
543 }
David Sodmanba340492018-08-05 21:51:33 -0700544 getBE().compositionInfo.hwc.displayFrame = transformedFrame;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800545
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700546 FloatRect sourceCrop = computeCrop(display);
David Sodman15094112018-10-11 09:39:37 -0700547 error = hwcLayer->setSourceCrop(sourceCrop);
548 if (error != HWC2::Error::None) {
549 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
550 "%s (%d)",
551 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
552 to_string(error).c_str(), static_cast<int32_t>(error));
553 } else {
554 hwcInfo.sourceCrop = sourceCrop;
555 }
David Sodmanba340492018-08-05 21:51:33 -0700556 getBE().compositionInfo.hwc.sourceCrop = sourceCrop;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800557
chaviw13fdc492017-06-27 12:40:18 -0700558 float alpha = static_cast<float>(getAlpha());
David Sodman15094112018-10-11 09:39:37 -0700559 error = hwcLayer->setPlaneAlpha(alpha);
560 ALOGE_IF(error != HWC2::Error::None,
561 "[%s] Failed to set plane alpha %.3f: "
562 "%s (%d)",
563 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700564 getBE().compositionInfo.hwc.alpha = alpha;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800565
David Sodman15094112018-10-11 09:39:37 -0700566 error = hwcLayer->setZOrder(z);
567 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
568 to_string(error).c_str(), static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700569 getBE().compositionInfo.hwc.z = z;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500570
Albert Chaulk2a589632017-05-04 16:59:44 -0400571 int type = s.type;
572 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700573 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400574 if (parent.get()) {
575 auto& parentState = parent->getDrawingState();
rongliucfb187b2018-03-14 12:26:23 -0700576 if (parentState.type >= 0 || parentState.appId >= 0) {
577 type = parentState.type;
578 appId = parentState.appId;
579 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400580 }
581
David Sodman15094112018-10-11 09:39:37 -0700582 error = hwcLayer->setInfo(type, appId);
583 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
584 static_cast<int32_t>(error));
585
David Sodmanba340492018-08-05 21:51:33 -0700586 getBE().compositionInfo.hwc.type = type;
587 getBE().compositionInfo.hwc.appId = appId;
588
Mathias Agopian29a367b2011-07-12 14:51:45 -0700589 /*
590 * Transformations are applied in this order:
591 * 1) buffer orientation/flip/mirror
592 * 2) state transformation (window manager)
593 * 3) layer orientation (screen orientation)
594 * (NOTE: the matrices are multiplied in reverse order)
595 */
596
Peiyong Linefefaac2018-08-17 12:27:51 -0700597 const ui::Transform bufferOrientation(mCurrentTransform);
598 ui::Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700599
Robert Carrcae605c2017-03-29 12:10:31 -0700600 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700601 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700602 * the code below applies the primary display's inverse transform to the
603 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700604 */
David Sodman41fdfc92017-11-06 16:09:56 -0800605 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700606 // calculate the inverse transform
607 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800608 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700609 }
Robert Carrcae605c2017-03-29 12:10:31 -0700610
611 /*
612 * Here we cancel out the orientation component of the WM transform.
613 * The scaling and translate components are already included in our bounds
614 * computation so it's enough to just omit it in the composition.
615 * See comment in onDraw with ref to b/36727915 for why.
616 */
Peiyong Linefefaac2018-08-17 12:27:51 -0700617 transform = ui::Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700618 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700619
620 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800621 const uint32_t orientation = transform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -0700622 if (orientation & ui::Transform::ROT_INVALID) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800623 // we can only handle simple transformation
Lloyd Pique074e8122018-07-26 12:57:23 -0700624 hwcInfo.forceClientComposition = true;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700625 getBE().mHwcLayers[*displayId].compositionType = HWC2::Composition::Client;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800626 } else {
627 auto transform = static_cast<HWC2::Transform>(orientation);
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800628 hwcInfo.transform = transform;
David Sodman15094112018-10-11 09:39:37 -0700629 auto error = hwcLayer->setTransform(transform);
630 ALOGE_IF(error != HWC2::Error::None,
631 "[%s] Failed to set transform %s: "
632 "%s (%d)",
633 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
634 static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700635 getBE().compositionInfo.hwc.transform = transform;
David Sodman4b7c4bc2017-11-17 12:13:59 -0800636 }
637}
638
Dominik Laskowski075d3172018-05-24 15:50:06 -0700639void Layer::forceClientComposition(DisplayId displayId) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700640 RETURN_IF_NO_HWC_LAYER(displayId);
Dominik Laskowski7e045462018-05-30 13:02:02 -0700641 getBE().mHwcLayers[displayId].forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800642}
Dan Stozaee44edd2015-03-23 15:50:23 -0700643
Dominik Laskowski075d3172018-05-24 15:50:06 -0700644bool Layer::getForceClientComposition(DisplayId displayId) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700645 RETURN_IF_NO_HWC_LAYER(displayId, false);
Dominik Laskowski7e045462018-05-30 13:02:02 -0700646 return getBE().mHwcLayers[displayId].forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800647}
648
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700649void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700650 const auto displayId = display->getId();
Dominik Laskowski34157762018-10-31 13:07:19 -0700651 LOG_ALWAYS_FATAL_IF(!displayId);
652 if (!hasHwcLayer(*displayId) || getCompositionType(displayId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800653 return;
654 }
655
656 // This gives us only the "orientation" component of the transform
657 const State& s(getCurrentState());
658
659 // Apply the layer's transform, followed by the display's global transform
660 // Here we're guaranteed that the layer's transform preserves rects
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800661 Rect win = getCroppedBufferSize(s);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800662 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700663 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Robert Carr1f0a16a2016-10-24 16:27:39 -0700664 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700665 frame.intersect(display->getViewport(), &frame);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700666 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800667 auto position = displayTransform.transform(frame);
668
Dominik Laskowski7e045462018-05-30 13:02:02 -0700669 auto error =
Dominik Laskowski075d3172018-05-24 15:50:06 -0700670 getBE().mHwcLayers[*displayId].layer->setCursorPosition(position.left, position.top);
671
David Sodman41fdfc92017-11-06 16:09:56 -0800672 ALOGE_IF(error != HWC2::Error::None,
673 "[%s] Failed to set cursor position "
674 "to (%d, %d): %s (%d)",
675 mName.string(), position.left, position.top, to_string(error).c_str(),
676 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800677}
Riley Andrews03414a12014-07-01 14:22:59 -0700678
Mathias Agopian13127d82013-03-05 17:47:11 -0800679// ---------------------------------------------------------------------------
680// drawing...
681// ---------------------------------------------------------------------------
682
Marissa Wall61c58622018-07-18 10:12:20 -0700683void Layer::draw(const RenderArea& renderArea, const Region& clip) {
chaviwa76b2712017-09-20 12:02:26 -0700684 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800685}
686
Marissa Wall61c58622018-07-18 10:12:20 -0700687void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) {
chaviwa76b2712017-09-20 12:02:26 -0700688 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800689}
690
David Sodman41fdfc92017-11-06 16:09:56 -0800691void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
692 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800693 auto& engine(mFlinger->getRenderEngine());
David Sodman9eeae692017-11-02 10:53:32 -0700694 computeGeometry(renderArea, getBE().mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700695 engine.setupFillWithColor(red, green, blue, alpha);
David Sodman9eeae692017-11-02 10:53:32 -0700696 engine.drawMesh(getBE().mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800697}
698
David Sodmanc1498e62018-09-12 14:36:26 -0700699void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
700 clearWithOpenGL(renderArea, 0, 0, 0, 0);
701}
702
Dominik Laskowski075d3172018-05-24 15:50:06 -0700703void Layer::setCompositionType(DisplayId displayId, HWC2::Composition type, bool callIntoHwc) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700704 if (getBE().mHwcLayers.count(displayId) == 0) {
Chia-I Wu30505fb2018-03-26 16:20:31 -0700705 ALOGE("setCompositionType called without a valid HWC layer");
706 return;
David Sodman15094112018-10-11 09:39:37 -0700707 }
708 auto& hwcInfo = getBE().mHwcLayers[displayId];
709 auto& hwcLayer = hwcInfo.layer;
710 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", (hwcLayer)->getId(), to_string(type).c_str(),
711 static_cast<int>(callIntoHwc));
712 if (hwcInfo.compositionType != type) {
713 ALOGV(" actually setting");
714 hwcInfo.compositionType = type;
715 if (callIntoHwc) {
716 auto error = (hwcLayer)->setCompositionType(type);
717 ALOGE_IF(error != HWC2::Error::None,
718 "[%s] Failed to set "
719 "composition type %s: %s (%d)",
720 mName.string(), to_string(type).c_str(), to_string(error).c_str(),
721 static_cast<int32_t>(error));
Chia-I Wu30505fb2018-03-26 16:20:31 -0700722 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800723 }
724}
725
Dominik Laskowski075d3172018-05-24 15:50:06 -0700726HWC2::Composition Layer::getCompositionType(const std::optional<DisplayId>& displayId) const {
727 if (!displayId) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700728 // If we're querying the composition type for a display that does not
729 // have a HWC counterpart, then it will always be Client
730 return HWC2::Composition::Client;
731 }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700732 if (getBE().mHwcLayers.count(*displayId) == 0) {
733 ALOGE("getCompositionType called with an invalid HWC layer");
734 return HWC2::Composition::Invalid;
735 }
736 return getBE().mHwcLayers.at(*displayId).compositionType;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800737}
738
Dominik Laskowski075d3172018-05-24 15:50:06 -0700739void Layer::setClearClientTarget(DisplayId displayId, bool clear) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700740 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800741 ALOGE("setClearClientTarget called without a valid HWC layer");
742 return;
743 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700744 getBE().mHwcLayers[displayId].clearClientTarget = clear;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800745}
746
Dominik Laskowski075d3172018-05-24 15:50:06 -0700747bool Layer::getClearClientTarget(DisplayId displayId) const {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700748 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800749 ALOGE("getClearClientTarget called without a valid HWC layer");
750 return false;
751 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700752 return getBE().mHwcLayers.at(displayId).clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800753}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800754
Dan Stozacac35382016-01-27 12:21:06 -0800755bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
756 if (point->getFrameNumber() <= mCurrentFrameNumber) {
757 // Don't bother with a SyncPoint, since we've already latched the
758 // relevant frame
759 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700760 }
Robert Carr2e102c92018-10-23 12:11:15 -0700761 if (isRemovedFromCurrentState()) {
762 return false;
763 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700764
Dan Stozacac35382016-01-27 12:21:06 -0800765 Mutex::Autolock lock(mLocalSyncPointMutex);
766 mLocalSyncPoints.push_back(point);
767 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700768}
769
Mathias Agopian13127d82013-03-05 17:47:11 -0800770// ----------------------------------------------------------------------------
771// local state
772// ----------------------------------------------------------------------------
773
Peiyong Lin833074a2018-08-28 11:53:54 -0700774void Layer::computeGeometry(const RenderArea& renderArea,
775 renderengine::Mesh& mesh,
chaviwa76b2712017-09-20 12:02:26 -0700776 bool useIdentityTransform) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700777 const ui::Transform renderAreaTransform(renderArea.getTransform());
Dan Stoza80d61162017-12-20 15:57:52 -0800778 FloatRect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700779
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000780 vec2 lt = vec2(win.left, win.top);
781 vec2 lb = vec2(win.left, win.bottom);
782 vec2 rb = vec2(win.right, win.bottom);
783 vec2 rt = vec2(win.right, win.top);
784
Peiyong Linefefaac2018-08-17 12:27:51 -0700785 ui::Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000786 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700787 lt = layerTransform.transform(lt);
788 lb = layerTransform.transform(lb);
789 rb = layerTransform.transform(rb);
790 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000791 }
792
Peiyong Lin833074a2018-08-28 11:53:54 -0700793 renderengine::Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700794 position[0] = renderAreaTransform.transform(lt);
795 position[1] = renderAreaTransform.transform(lb);
796 position[2] = renderAreaTransform.transform(rb);
797 position[3] = renderAreaTransform.transform(rt);
Mathias Agopian13127d82013-03-05 17:47:11 -0800798}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800799
David Sodman41fdfc92017-11-06 16:09:56 -0800800bool Layer::isSecure() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000801 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700802 return (s.flags & layer_state_t::eLayerSecure);
803}
804
Mathias Agopian13127d82013-03-05 17:47:11 -0800805void Layer::setVisibleRegion(const Region& visibleRegion) {
806 // always called from main thread
807 this->visibleRegion = visibleRegion;
808}
809
810void Layer::setCoveredRegion(const Region& coveredRegion) {
811 // always called from main thread
812 this->coveredRegion = coveredRegion;
813}
814
David Sodman41fdfc92017-11-06 16:09:56 -0800815void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800816 // always called from main thread
817 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
818}
819
Robert Carre5f4f692018-01-12 13:12:28 -0800820void Layer::clearVisibilityRegions() {
821 visibleRegion.clear();
822 visibleNonTransparentRegion.clear();
823 coveredRegion.clear();
824}
825
Mathias Agopian13127d82013-03-05 17:47:11 -0800826// ----------------------------------------------------------------------------
827// transaction
828// ----------------------------------------------------------------------------
829
Dan Stoza7dde5992015-05-22 09:51:44 -0700830void Layer::pushPendingState() {
831 if (!mCurrentState.modified) {
832 return;
833 }
834
Dan Stoza7dde5992015-05-22 09:51:44 -0700835 // If this transaction is waiting on the receipt of a frame, generate a sync
836 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700837 // We don't allow installing sync points after we are removed from the current state
838 // as we won't be able to signal our end.
839 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700840 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800841 if (barrierLayer == nullptr) {
842 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700843 // If we can't promote the layer we are intended to wait on,
844 // then it is expired or otherwise invalid. Allow this transaction
845 // to be applied as per normal (no synchronization).
Marissa Wallf58c14b2018-07-24 10:50:43 -0700846 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800847 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700848 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy);
Robert Carr0d480722017-01-10 16:42:54 -0800849 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800850 mRemoteSyncPoints.push_back(std::move(syncPoint));
851 } else {
852 // We already missed the frame we're supposed to synchronize
853 // on, so go ahead and apply the state update
Marissa Wallf58c14b2018-07-24 10:50:43 -0700854 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800855 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700856 }
857
Dan Stoza7dde5992015-05-22 09:51:44 -0700858 // Wake us up to check if the frame has been received
859 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700860 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700861 }
862 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700863 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700864}
865
Pablo Ceballos05289c22016-04-14 15:49:55 -0700866void Layer::popPendingState(State* stateToCommit) {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700867 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700868
869 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700870 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700871}
872
Pablo Ceballos05289c22016-04-14 15:49:55 -0700873bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700874 bool stateUpdateAvailable = false;
875 while (!mPendingStates.empty()) {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700876 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700877 if (mRemoteSyncPoints.empty()) {
878 // If we don't have a sync point for this, apply it anyway. It
879 // will be visually wrong, but it should keep us from getting
880 // into too much trouble.
881 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700882 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700883 stateUpdateAvailable = true;
884 continue;
885 }
886
Marissa Wallf58c14b2018-07-24 10:50:43 -0700887 if (mRemoteSyncPoints.front()->getFrameNumber() !=
888 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800889 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800890
891 // Signal our end of the sync point and then dispose of it
892 mRemoteSyncPoints.front()->setTransactionApplied();
893 mRemoteSyncPoints.pop_front();
894 continue;
895 }
896
Dan Stoza7dde5992015-05-22 09:51:44 -0700897 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
898 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700899 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700900 stateUpdateAvailable = true;
901
902 // Signal our end of the sync point and then dispose of it
903 mRemoteSyncPoints.front()->setTransactionApplied();
904 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800905 } else {
906 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700907 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700908 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700909 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700910 stateUpdateAvailable = true;
911 }
912 }
913
914 // If we still have pending updates, wake SurfaceFlinger back up and point
915 // it at this layer so we can process them
916 if (!mPendingStates.empty()) {
917 setTransactionFlags(eTransactionNeeded);
918 mFlinger->setTransactionFlags(eTraversalNeeded);
919 }
920
921 mCurrentState.modified = false;
922 return stateUpdateAvailable;
923}
924
Marissa Wall61c58622018-07-18 10:12:20 -0700925uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000926 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800927
Marissa Wall61c58622018-07-18 10:12:20 -0700928 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
929 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700930
David Sodmaneb085e02017-10-05 18:49:04 -0700931 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700932 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000933 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800934 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
935 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
936 " requested={ wh={%4u,%4u} }}\n"
937 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
938 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700939 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700940 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
941 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
942 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
943 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
944 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700945 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
946 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
947 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800948 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700949
Robert Carre392b552017-09-19 12:16:05 -0700950 // Don't let Layer::doTransaction update the drawing state
951 // if we have a pending resize, unless we are in fixed-size mode.
952 // the drawing state will be updated only once we receive a buffer
953 // with the correct size.
954 //
955 // In particular, we want to make sure the clip (which is part
956 // of the geometry state) is latched together with the size but is
957 // latched immediately when no resizing is involved.
958 //
959 // If a sideband stream is attached, however, we want to skip this
960 // optimization so that transactions aren't missed when a buffer
961 // never arrives
962 //
963 // In the case that we don't have a buffer we ignore other factors
964 // and avoid entering the resizePending state. At a high level the
965 // resizePending state is to avoid applying the state of the new buffer
966 // to the old buffer. However in the state where we don't have an old buffer
967 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700968 const bool resizePending =
969 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
970 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
David Sodman0cc69182017-11-17 12:12:07 -0800971 (getBE().compositionInfo.mBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700972 if (!isFixedSize()) {
David Sodman0cc69182017-11-17 12:12:07 -0800973 if (resizePending && getBE().compositionInfo.hwc.sidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700974 flags |= eDontUpdateGeometryState;
975 }
976 }
977
Robert Carr7bf247e2017-05-18 14:02:49 -0700978 // Here we apply various requested geometry states, depending on our
979 // latching configuration. See Layer.h for a detailed discussion of
980 // how geometry latching is controlled.
981 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000982 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700983
984 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
985 // mode, which causes attributes which normally latch regardless of scaling mode,
986 // to be delayed. We copy the requested state to the active state making sure
987 // to respect these rules (again see Layer.h for a detailed discussion).
988 //
989 // There is an awkward asymmetry in the handling of the crop states in the position
990 // states, as can be seen below. Largely this arises from position and transform
991 // being stored in the same data structure while having different latching rules.
992 // b/38182305
993 //
Marissa Wall61c58622018-07-18 10:12:20 -0700994 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700995 // applyPendingStates in the presence of deferred transactions.
996 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -0700997 float tx = stateToCommit->active_legacy.transform.tx();
998 float ty = stateToCommit->active_legacy.transform.ty();
999 stateToCommit->active_legacy = stateToCommit->requested_legacy;
1000 stateToCommit->active_legacy.transform.set(tx, ty);
1001 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001002 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001003 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -07001004 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001005 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001006 }
1007
Marissa Wall61c58622018-07-18 10:12:20 -07001008 return flags;
1009}
1010
1011uint32_t Layer::doTransaction(uint32_t flags) {
1012 ATRACE_CALL();
1013
chaviw5aedec92018-10-22 10:40:38 -07001014 if (mLayerDetached) {
1015 return 0;
1016 }
1017
Marissa Wall61c58622018-07-18 10:12:20 -07001018 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +00001019 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -07001020 if (!applyPendingStates(&c)) {
1021 return 0;
1022 }
1023
1024 flags = doTransactionResize(flags, &c);
1025
Alec Mourib416efd2018-09-06 21:01:59 +00001026 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -07001027
1028 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001029 // invalidate and recompute the visible regions if needed
1030 flags |= Layer::eVisibleRegion;
1031 }
1032
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001033 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001034 // invalidate and recompute the visible regions if needed
1035 flags |= eVisibleRegion;
1036 this->contentDirty = true;
1037
1038 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -07001039 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -07001040 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -08001041 }
1042
Dan Stozac8145172016-04-28 16:29:06 -07001043 // If the layer is hidden, signal and clear out all local sync points so
1044 // that transactions for layers depending on this layer's frames becoming
1045 // visible are not blocked
1046 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001047 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001048 }
1049
Robert Carr720e5062018-07-30 17:45:14 -07001050 if (mCurrentState.inputInfoChanged) {
1051 flags |= eInputInfoChanged;
1052 mCurrentState.inputInfoChanged = false;
1053 }
1054
Mathias Agopian13127d82013-03-05 17:47:11 -08001055 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001056 commitTransaction(c);
Marissa Walle2ffb422018-10-12 11:33:52 -07001057 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -08001058 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001059}
1060
Pablo Ceballos05289c22016-04-14 15:49:55 -07001061void Layer::commitTransaction(const State& stateToCommit) {
1062 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001063}
1064
Mathias Agopian13127d82013-03-05 17:47:11 -08001065uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Piquef1c675b2018-09-12 20:45:39 -07001066 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -08001067}
1068
1069uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Piquef1c675b2018-09-12 20:45:39 -07001070 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -08001071}
1072
Robert Carr82364e32016-05-15 11:27:47 -07001073bool Layer::setPosition(float x, float y, bool immediate) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001074 if (mCurrentState.requested_legacy.transform.tx() == x &&
1075 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001076 return false;
1077 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001078
1079 // We update the requested and active position simultaneously because
1080 // we want to apply the position portion of the transform matrix immediately,
1081 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Marissa Wallf58c14b2018-07-24 10:50:43 -07001082 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001083 if (immediate && !mFreezeGeometryUpdates) {
1084 // Here we directly update the active state
1085 // unlike other setters, because we store it within
1086 // the transform, but use different latching rules.
1087 // b/38182305
Marissa Wallf58c14b2018-07-24 10:50:43 -07001088 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001089 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001090 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001091
Dan Stoza7dde5992015-05-22 09:51:44 -07001092 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001093 setTransactionFlags(eTransactionNeeded);
1094 return true;
1095}
Robert Carr82364e32016-05-15 11:27:47 -07001096
Robert Carr1f0a16a2016-10-24 16:27:39 -07001097bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1098 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1099 if (idx < 0) {
1100 return false;
1101 }
1102 if (childLayer->setLayer(z)) {
1103 mCurrentChildren.removeAt(idx);
1104 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001105 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001106 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001107 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001108}
1109
Robert Carr503c7042017-09-27 15:06:08 -07001110bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1111 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1112 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1113 if (idx < 0) {
1114 return false;
1115 }
1116 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1117 mCurrentChildren.removeAt(idx);
1118 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001119 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001120 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001121 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001122}
1123
Robert Carrae060832016-11-28 10:51:00 -08001124bool Layer::setLayer(int32_t z) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001125 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001126 mCurrentState.sequence++;
1127 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001128 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001129
1130 // Discard all relative layering.
1131 if (mCurrentState.zOrderRelativeOf != nullptr) {
1132 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1133 if (strongRelative != nullptr) {
1134 strongRelative->removeZOrderRelative(this);
1135 }
1136 mCurrentState.zOrderRelativeOf = nullptr;
1137 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001138 setTransactionFlags(eTransactionNeeded);
1139 return true;
1140}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001141
Robert Carrdb66e622017-04-10 16:55:57 -07001142void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1143 mCurrentState.zOrderRelatives.remove(relative);
1144 mCurrentState.sequence++;
1145 mCurrentState.modified = true;
1146 setTransactionFlags(eTransactionNeeded);
1147}
1148
1149void Layer::addZOrderRelative(const wp<Layer>& relative) {
1150 mCurrentState.zOrderRelatives.add(relative);
1151 mCurrentState.modified = true;
1152 mCurrentState.sequence++;
1153 setTransactionFlags(eTransactionNeeded);
1154}
1155
Robert Carr503d2bd2017-12-04 15:49:47 -08001156bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001157 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1158 if (handle == nullptr) {
1159 return false;
1160 }
1161 sp<Layer> relative = handle->owner.promote();
1162 if (relative == nullptr) {
1163 return false;
1164 }
1165
Robert Carr503d2bd2017-12-04 15:49:47 -08001166 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1167 mCurrentState.zOrderRelativeOf == relative) {
1168 return false;
1169 }
1170
Robert Carrdb66e622017-04-10 16:55:57 -07001171 mCurrentState.sequence++;
1172 mCurrentState.modified = true;
Robert Carr503d2bd2017-12-04 15:49:47 -08001173 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001174
chaviw9ab4bd12017-11-03 13:11:00 -07001175 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
1176 if (oldZOrderRelativeOf != nullptr) {
1177 oldZOrderRelativeOf->removeZOrderRelative(this);
1178 }
Robert Carrdb66e622017-04-10 16:55:57 -07001179 mCurrentState.zOrderRelativeOf = relative;
1180 relative->addZOrderRelative(this);
1181
1182 setTransactionFlags(eTransactionNeeded);
1183
1184 return true;
1185}
1186
Mathias Agopian13127d82013-03-05 17:47:11 -08001187bool Layer::setSize(uint32_t w, uint32_t h) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001188 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
1189 return false;
1190 mCurrentState.requested_legacy.w = w;
1191 mCurrentState.requested_legacy.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001192 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001193 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001194
1195 // record the new size, from this point on, when the client request
1196 // a buffer, it'll get the new size.
1197 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001198 return true;
1199}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001200bool Layer::setAlpha(float alpha) {
David Sodman41fdfc92017-11-06 16:09:56 -08001201 if (mCurrentState.color.a == alpha) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001202 mCurrentState.sequence++;
chaviw13fdc492017-06-27 12:40:18 -07001203 mCurrentState.color.a = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001204 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001205 setTransactionFlags(eTransactionNeeded);
1206 return true;
1207}
chaviw13fdc492017-06-27 12:40:18 -07001208
1209bool Layer::setColor(const half3& color) {
David Sodman41fdfc92017-11-06 16:09:56 -08001210 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g &&
1211 color.b == mCurrentState.color.b)
chaviw13fdc492017-06-27 12:40:18 -07001212 return false;
1213
1214 mCurrentState.sequence++;
1215 mCurrentState.color.r = color.r;
1216 mCurrentState.color.g = color.g;
1217 mCurrentState.color.b = color.b;
1218 mCurrentState.modified = true;
1219 setTransactionFlags(eTransactionNeeded);
1220 return true;
1221}
1222
Robert Carrd4ae7f32018-06-07 16:10:57 -07001223bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1224 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001225 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001226 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1227
1228 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1229 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1230 return false;
1231 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001232 mCurrentState.sequence++;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001233 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1234 matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001235 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001236 setTransactionFlags(eTransactionNeeded);
1237 return true;
1238}
Marissa Wall61c58622018-07-18 10:12:20 -07001239
Mathias Agopian13127d82013-03-05 17:47:11 -08001240bool Layer::setTransparentRegionHint(const Region& transparent) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001241 mCurrentState.requestedTransparentRegion_legacy = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001242 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001243 setTransactionFlags(eTransactionNeeded);
1244 return true;
1245}
1246bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1247 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
David Sodman41fdfc92017-11-06 16:09:56 -08001248 if (mCurrentState.flags == newFlags) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001249 mCurrentState.sequence++;
1250 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001251 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001252 setTransactionFlags(eTransactionNeeded);
1253 return true;
1254}
Robert Carr99e27f02016-06-16 15:18:02 -07001255
Marissa Wallf58c14b2018-07-24 10:50:43 -07001256bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
1257 if (mCurrentState.requestedCrop_legacy == crop) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001258 mCurrentState.sequence++;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001259 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001260 if (immediate && !mFreezeGeometryUpdates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001261 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001262 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001263 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1264
Dan Stoza7dde5992015-05-22 09:51:44 -07001265 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001266 setTransactionFlags(eTransactionNeeded);
1267 return true;
1268}
Robert Carr8d5227b2017-03-16 15:41:03 -07001269
Robert Carrc3574f72016-03-24 12:19:32 -07001270bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001271 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001272 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001273 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001274 return true;
1275}
1276
rongliucfb187b2018-03-14 12:26:23 -07001277void Layer::setInfo(int32_t type, int32_t appId) {
David Sodman41fdfc92017-11-06 16:09:56 -08001278 mCurrentState.appId = appId;
1279 mCurrentState.type = type;
1280 mCurrentState.modified = true;
1281 setTransactionFlags(eTransactionNeeded);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001282}
1283
Mathias Agopian13127d82013-03-05 17:47:11 -08001284bool Layer::setLayerStack(uint32_t layerStack) {
David Sodman41fdfc92017-11-06 16:09:56 -08001285 if (mCurrentState.layerStack == layerStack) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001286 mCurrentState.sequence++;
1287 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001288 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001289 setTransactionFlags(eTransactionNeeded);
1290 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001291}
1292
Robert Carr1f0a16a2016-10-24 16:27:39 -07001293uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001294 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001295 if (p == nullptr) {
1296 return getDrawingState().layerStack;
1297 }
1298 return p->getLayerStack();
1299}
1300
Marissa Wallf58c14b2018-07-24 10:50:43 -07001301void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
1302 mCurrentState.barrierLayer_legacy = barrierLayer;
1303 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001304 // We don't set eTransactionNeeded, because just receiving a deferral
1305 // request without any other state updates shouldn't actually induce a delay
1306 mCurrentState.modified = true;
1307 pushPendingState();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001308 mCurrentState.barrierLayer_legacy = nullptr;
1309 mCurrentState.frameNumber_legacy = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001310 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001311}
1312
Marissa Wallf58c14b2018-07-24 10:50:43 -07001313void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001314 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001315 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001316}
1317
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001318// ----------------------------------------------------------------------------
1319// pageflip handling...
1320// ----------------------------------------------------------------------------
1321
Robert Carr1f0a16a2016-10-24 16:27:39 -07001322bool Layer::isHiddenByPolicy() const {
Alec Mourib416efd2018-09-06 21:01:59 +00001323 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001324 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001325 if (parent != nullptr && parent->isHiddenByPolicy()) {
1326 return true;
1327 }
1328 return s.flags & layer_state_t::eLayerHidden;
1329}
1330
David Sodman41fdfc92017-11-06 16:09:56 -08001331uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001332 // TODO: should we do something special if mSecure is set?
1333 if (mProtectedByApp) {
1334 // need a hardware-protected path to external video sink
1335 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001336 }
Riley Andrews03414a12014-07-01 14:22:59 -07001337 if (mPotentialCursor) {
1338 usage |= GraphicBuffer::USAGE_CURSOR;
1339 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001340 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001341 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001342}
1343
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001344void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001345 uint32_t orientation = 0;
Vishnu Nairf2deb822018-11-12 17:53:48 -08001346 // Disable setting transform hint if the debug flag is set or if the
1347 // getTransformToDisplayInverse flag is set and the client wants to submit buffers
1348 // in one orientation.
1349 if (!mFlinger->mDebugDisableTransformHint && !getTransformToDisplayInverse()) {
Mathias Agopian84300952012-11-21 16:02:13 -08001350 // The transform hint is used to improve performance, but we can
1351 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001352 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001353 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001354 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001355 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001356 orientation = 0;
1357 }
1358 }
David Sodmaneb085e02017-10-05 18:49:04 -07001359 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001360}
1361
Mathias Agopian13127d82013-03-05 17:47:11 -08001362// ----------------------------------------------------------------------------
1363// debugging
1364// ----------------------------------------------------------------------------
1365
Marissa Wall61c58622018-07-18 10:12:20 -07001366// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001367LayerDebugInfo Layer::getLayerDebugInfo() const {
1368 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001369 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001370 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001371 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001372 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
1373 info.mType = String8(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001374 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001375 info.mVisibleRegion = visibleRegion;
1376 info.mSurfaceDamageRegion = surfaceDamageRegion;
1377 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001378 info.mX = ds.active_legacy.transform.tx();
1379 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001380 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001381 info.mWidth = ds.active_legacy.w;
1382 info.mHeight = ds.active_legacy.h;
1383 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001384 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001385 info.mFlags = ds.flags;
1386 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001387 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001388 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1389 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1390 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1391 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001392 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001393 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001394 if (buffer != 0) {
1395 info.mActiveBufferWidth = buffer->getWidth();
1396 info.mActiveBufferHeight = buffer->getHeight();
1397 info.mActiveBufferStride = buffer->getStride();
1398 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001399 } else {
1400 info.mActiveBufferWidth = 0;
1401 info.mActiveBufferHeight = 0;
1402 info.mActiveBufferStride = 0;
1403 info.mActiveBufferFormat = 0;
1404 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001405 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001406 info.mNumQueuedFrames = getQueuedFrameCount();
1407 info.mRefreshPending = isBufferLatched();
1408 info.mIsOpaque = isOpaque(ds);
1409 info.mContentDirty = contentDirty;
1410 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001411}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001412
Dan Stozae22aec72016-08-01 13:20:59 -07001413void Layer::miniDumpHeader(String8& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001414 result.append("-------------------------------");
1415 result.append("-------------------------------");
1416 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001417 result.append(" Layer name\n");
1418 result.append(" Z | ");
1419 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001420 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001421 result.append(" Disp Frame (LTRB) | ");
1422 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001423 result.append("-------------------------------");
1424 result.append("-------------------------------");
1425 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001426}
1427
Dominik Laskowski075d3172018-05-24 15:50:06 -07001428void Layer::miniDump(String8& result, DisplayId displayId) const {
Dominik Laskowski34157762018-10-31 13:07:19 -07001429 if (!hasHwcLayer(displayId)) {
Dan Stozae22aec72016-08-01 13:20:59 -07001430 return;
1431 }
1432
1433 String8 name;
1434 if (mName.length() > 77) {
1435 std::string shortened;
1436 shortened.append(mName.string(), 36);
1437 shortened.append("[...]");
1438 shortened.append(mName.string() + (mName.length() - 36), 36);
1439 name = shortened.c_str();
1440 } else {
1441 name = mName;
1442 }
1443
1444 result.appendFormat(" %s\n", name.string());
1445
Alec Mourib416efd2018-09-06 21:01:59 +00001446 const State& layerState(getDrawingState());
Lloyd Pique074e8122018-07-26 12:57:23 -07001447 const LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers.at(displayId);
Chia-I Wu1e043612018-03-01 09:45:09 -08001448 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
1449 result.appendFormat(" rel %6d | ", layerState.z);
1450 } else {
1451 result.appendFormat(" %10d | ", layerState.z);
1452 }
Dominik Laskowski7e045462018-05-30 13:02:02 -07001453 result.appendFormat("%10s | ", to_string(getCompositionType(displayId)).c_str());
Lloyd Pique074e8122018-07-26 12:57:23 -07001454 result.appendFormat("%10s | ", to_string(hwcInfo.transform).c_str());
1455 const Rect& frame = hwcInfo.displayFrame;
David Sodman41fdfc92017-11-06 16:09:56 -08001456 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique074e8122018-07-26 12:57:23 -07001457 const FloatRect& crop = hwcInfo.sourceCrop;
David Sodman41fdfc92017-11-06 16:09:56 -08001458 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 -07001459
David Sodman7e4ae112018-02-09 15:02:28 -08001460 result.append("- - - - - - - - - - - - - - - -\n");
1461
1462 std::string compositionInfoStr;
1463 getBE().compositionInfo.dump(compositionInfoStr, "compositionInfo");
1464 result.append(compositionInfoStr.c_str());
1465
Yichi Chen6ca35192018-05-29 12:20:43 +08001466 result.append("- - - - - - - - - - - - - - - -");
1467 result.append("- - - - - - - - - - - - - - - -");
1468 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001469}
Dan Stozae22aec72016-08-01 13:20:59 -07001470
Svetoslavd85084b2014-03-20 10:28:31 -07001471void Layer::dumpFrameStats(String8& result) const {
1472 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001473}
1474
Svetoslavd85084b2014-03-20 10:28:31 -07001475void Layer::clearFrameStats() {
1476 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001477}
1478
Jamie Gennis6547ff42013-07-16 20:12:42 -07001479void Layer::logFrameStats() {
1480 mFrameTracker.logAndResetStats(mName);
1481}
1482
Svetoslavd85084b2014-03-20 10:28:31 -07001483void Layer::getFrameStats(FrameStats* outStats) const {
1484 mFrameTracker.getStats(outStats);
1485}
1486
Brian Andersond6927fb2016-07-23 23:37:30 -07001487void Layer::dumpFrameEvents(String8& result) {
David Sodman41fdfc92017-11-06 16:09:56 -08001488 result.appendFormat("- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001489 Mutex::Autolock lock(mFrameEventHistoryMutex);
1490 mFrameEventHistory.checkFencesForCompletion();
1491 mFrameEventHistory.dump(result);
1492}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001493
Brian Anderson5ea5e592016-12-01 16:54:33 -08001494void Layer::onDisconnect() {
1495 Mutex::Autolock lock(mFrameEventHistoryMutex);
1496 mFrameEventHistory.onDisconnect();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -07001497 mTimeStats.onDisconnect(getSequence());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001498}
1499
Brian Anderson3890c392016-07-25 12:48:08 -07001500void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001501 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001502 if (newTimestamps) {
Yiwei Zhang8e8fe522018-11-02 18:34:07 -07001503 mTimeStats.setPostTime(getSequence(), newTimestamps->frameNumber, getName().c_str(),
1504 newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001505 }
1506
Brian Andersond6927fb2016-07-23 23:37:30 -07001507 Mutex::Autolock lock(mFrameEventHistoryMutex);
1508 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001509 // If there are any unsignaled fences in the aquire timeline at this
1510 // point, the previously queued frame hasn't been latched yet. Go ahead
1511 // and try to get the signal time here so the syscall is taken out of
1512 // the main thread's critical path.
1513 mAcquireTimeline.updateSignalTimes();
1514 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001515 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001516 mFrameEventHistory.addQueue(*newTimestamps);
1517 }
1518
Brian Anderson3890c392016-07-25 12:48:08 -07001519 if (outDelta) {
1520 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001521 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001522}
Dan Stozae77c7662016-05-13 11:37:28 -07001523
Chia-I Wu98f1c102017-05-30 14:54:08 -07001524size_t Layer::getChildrenCount() const {
1525 size_t count = 0;
1526 for (const sp<Layer>& child : mCurrentChildren) {
1527 count += 1 + child->getChildrenCount();
1528 }
1529 return count;
1530}
1531
Robert Carr1f0a16a2016-10-24 16:27:39 -07001532void Layer::addChild(const sp<Layer>& layer) {
1533 mCurrentChildren.add(layer);
1534 layer->setParent(this);
1535}
1536
1537ssize_t Layer::removeChild(const sp<Layer>& layer) {
1538 layer->setParent(nullptr);
1539 return mCurrentChildren.remove(layer);
1540}
1541
Robert Carr1db73f62016-12-21 12:58:51 -08001542bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1543 sp<Handle> handle = nullptr;
1544 sp<Layer> newParent = nullptr;
1545 if (newParentHandle == nullptr) {
1546 return false;
1547 }
1548 handle = static_cast<Handle*>(newParentHandle.get());
1549 newParent = handle->owner.promote();
1550 if (newParent == nullptr) {
1551 ALOGE("Unable to promote Layer handle");
1552 return false;
1553 }
1554
chaviw5aedec92018-10-22 10:40:38 -07001555 if (attachChildren()) {
1556 setTransactionFlags(eTransactionNeeded);
1557 }
Robert Carr1db73f62016-12-21 12:58:51 -08001558 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001559 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001560
1561 sp<Client> client(child->mClientRef.promote());
1562 if (client != nullptr) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001563 client->updateParent(newParent);
Robert Carr1db73f62016-12-21 12:58:51 -08001564 }
1565 }
1566 mCurrentChildren.clear();
1567
1568 return true;
1569}
1570
Robert Carr15eae092018-03-23 13:43:53 -07001571void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001572 for (const sp<Layer>& child : mDrawingChildren) {
1573 child->mDrawingParent = newParent;
1574 }
1575}
1576
chaviwf1961f72017-09-18 16:41:07 -07001577bool Layer::reparent(const sp<IBinder>& newParentHandle) {
1578 if (newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001579 return false;
1580 }
1581
1582 auto handle = static_cast<Handle*>(newParentHandle.get());
1583 sp<Layer> newParent = handle->owner.promote();
1584 if (newParent == nullptr) {
1585 ALOGE("Unable to promote Layer handle");
1586 return false;
1587 }
1588
chaviwf1961f72017-09-18 16:41:07 -07001589 sp<Layer> parent = getParent();
1590 if (parent != nullptr) {
1591 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001592 }
chaviwf1961f72017-09-18 16:41:07 -07001593 newParent->addChild(this);
chaviw06178942017-07-27 10:25:59 -07001594
chaviw61626f22018-11-15 16:26:27 -08001595 if (!newParent->isRemovedFromCurrentState()) {
1596 addToCurrentState();
1597 }
1598
chaviwf1961f72017-09-18 16:41:07 -07001599 sp<Client> client(mClientRef.promote());
chaviw06178942017-07-27 10:25:59 -07001600 sp<Client> newParentClient(newParent->mClientRef.promote());
1601
chaviwf1961f72017-09-18 16:41:07 -07001602 if (client != newParentClient) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001603 client->updateParent(newParent);
chaviw06178942017-07-27 10:25:59 -07001604 }
1605
chaviw5aedec92018-10-22 10:40:38 -07001606 if (mLayerDetached) {
1607 mLayerDetached = false;
1608 setTransactionFlags(eTransactionNeeded);
1609 }
1610 if (attachChildren()) {
1611 setTransactionFlags(eTransactionNeeded);
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) {
chaviw5aedec92018-10-22 10:40:38 -07001621 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001622 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
chaviw5aedec92018-10-22 10:40:38 -07001629bool Layer::attachChildren() {
1630 bool changed = false;
1631 for (const sp<Layer>& child : mCurrentChildren) {
1632 sp<Client> parentClient = mClientRef.promote();
1633 sp<Client> client(child->mClientRef.promote());
1634 if (client != nullptr && parentClient != client) {
1635 if (child->mLayerDetached) {
1636 child->mLayerDetached = false;
1637 changed = true;
1638 }
1639 changed |= child->attachChildren();
1640 }
1641 }
1642
1643 return changed;
1644}
1645
Peiyong Lind3788632018-09-18 16:01:31 -07001646bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001647 static const mat4 identityMatrix = mat4();
1648
Peiyong Lind3788632018-09-18 16:01:31 -07001649 if (mCurrentState.colorTransform == matrix) {
1650 return false;
1651 }
1652 ++mCurrentState.sequence;
1653 mCurrentState.colorTransform = matrix;
Peiyong Lin747321c2018-10-01 10:03:11 -07001654 mCurrentState.hasColorTransform = matrix != identityMatrix;
1655 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001656 setTransactionFlags(eTransactionNeeded);
1657 return true;
1658}
1659
1660const mat4& Layer::getColorTransform() const {
1661 return getDrawingState().colorTransform;
1662}
1663
1664bool Layer::hasColorTransform() const {
Peiyong Lin747321c2018-10-01 10:03:11 -07001665 return getDrawingState().hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001666}
1667
Chia-I Wu11481472018-05-04 10:43:19 -07001668bool Layer::isLegacyDataSpace() const {
1669 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001670 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001671 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001672}
1673
Robert Carr1f0a16a2016-10-24 16:27:39 -07001674void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001675 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001676}
1677
1678void Layer::clearSyncPoints() {
1679 for (const auto& child : mCurrentChildren) {
1680 child->clearSyncPoints();
1681 }
1682
1683 Mutex::Autolock lock(mLocalSyncPointMutex);
1684 for (auto& point : mLocalSyncPoints) {
1685 point->setFrameAvailable();
1686 }
1687 mLocalSyncPoints.clear();
1688}
1689
1690int32_t Layer::getZ() const {
1691 return mDrawingState.z;
1692}
1693
Robert Carr29abff82017-12-04 13:51:20 -08001694bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1695 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1696 const State& state = useDrawing ? mDrawingState : mCurrentState;
1697 return state.zOrderRelativeOf != nullptr;
1698}
1699
David Sodman41fdfc92017-11-06 16:09:56 -08001700__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001701 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001702 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1703 "makeTraversalList received invalid stateSet");
1704 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1705 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1706 const State& state = useDrawing ? mDrawingState : mCurrentState;
1707
Robert Carr29abff82017-12-04 13:51:20 -08001708 if (state.zOrderRelatives.size() == 0) {
1709 *outSkipRelativeZUsers = true;
1710 return children;
1711 }
1712
chaviwfd462612018-05-31 16:11:27 -07001713 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001714 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001715 sp<Layer> strongRelative = weakRelative.promote();
1716 if (strongRelative != nullptr) {
1717 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001718 }
1719 }
1720
Dan Stoza412903f2017-04-27 13:42:17 -07001721 for (const sp<Layer>& child : children) {
Robert Carr503c7042017-09-27 15:06:08 -07001722 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1723 if (childState.zOrderRelativeOf != nullptr) {
1724 continue;
1725 }
Robert Carrdb66e622017-04-10 16:55:57 -07001726 traverse.add(child);
1727 }
1728
1729 return traverse;
1730}
1731
Robert Carr1f0a16a2016-10-24 16:27:39 -07001732/**
Robert Carrdb66e622017-04-10 16:55:57 -07001733 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001734 */
Dan Stoza412903f2017-04-27 13:42:17 -07001735void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001736 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1737 // produce a new list for traversing, including our relatives, and not including our children
1738 // who are relatives of another surface. In the case that there are no relative Z,
1739 // makeTraversalList returns our children directly to avoid significant overhead.
1740 // However in this case we need to take the responsibility for filtering children which
1741 // are relatives of another surface here.
1742 bool skipRelativeZUsers = false;
1743 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001744
Robert Carr1f0a16a2016-10-24 16:27:39 -07001745 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001746 for (; i < list.size(); i++) {
1747 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001748 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1749 continue;
1750 }
1751
Robert Carrdb66e622017-04-10 16:55:57 -07001752 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001753 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001754 }
Dan Stoza412903f2017-04-27 13:42:17 -07001755 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001756 }
Robert Carr29abff82017-12-04 13:51:20 -08001757
Dan Stoza412903f2017-04-27 13:42:17 -07001758 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001759 for (; i < list.size(); i++) {
1760 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001761
1762 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1763 continue;
1764 }
Dan Stoza412903f2017-04-27 13:42:17 -07001765 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001766 }
1767}
1768
1769/**
Robert Carrdb66e622017-04-10 16:55:57 -07001770 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001771 */
Dan Stoza412903f2017-04-27 13:42:17 -07001772void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1773 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001774 // See traverseInZOrder for documentation.
1775 bool skipRelativeZUsers = false;
1776 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001777
Robert Carr1f0a16a2016-10-24 16:27:39 -07001778 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001779 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001780 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001781
1782 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1783 continue;
1784 }
1785
Robert Carrdb66e622017-04-10 16:55:57 -07001786 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001787 break;
1788 }
Dan Stoza412903f2017-04-27 13:42:17 -07001789 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001790 }
Dan Stoza412903f2017-04-27 13:42:17 -07001791 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001792 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001793 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001794
1795 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1796 continue;
1797 }
1798
Dan Stoza412903f2017-04-27 13:42:17 -07001799 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001800 }
1801}
1802
chaviw4b129c22018-04-09 16:19:43 -07001803LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1804 const std::vector<Layer*>& layersInTree) {
1805 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1806 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001807 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1808 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
chaviw4b129c22018-04-09 16:19:43 -07001809 const State& state = useDrawing ? mDrawingState : mCurrentState;
1810
chaviwfd462612018-05-31 16:11:27 -07001811 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001812 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1813 sp<Layer> strongRelative = weakRelative.promote();
1814 // Only add relative layers that are also descendents of the top most parent of the tree.
1815 // If a relative layer is not a descendent, then it should be ignored.
1816 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1817 traverse.add(strongRelative);
1818 }
1819 }
1820
1821 for (const sp<Layer>& child : children) {
1822 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1823 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1824 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1825 // the child here since it won't be added later as a relative.
1826 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1827 childState.zOrderRelativeOf.promote().get())) {
1828 continue;
1829 }
1830 traverse.add(child);
1831 }
1832
1833 return traverse;
1834}
1835
1836void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1837 LayerVector::StateSet stateSet,
1838 const LayerVector::Visitor& visitor) {
1839 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001840
1841 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001842 for (; i < list.size(); i++) {
1843 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001844 if (relative->getZ() >= 0) {
1845 break;
1846 }
chaviw4b129c22018-04-09 16:19:43 -07001847 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001848 }
chaviw4b129c22018-04-09 16:19:43 -07001849
chaviwa76b2712017-09-20 12:02:26 -07001850 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001851 for (; i < list.size(); i++) {
1852 const auto& relative = list[i];
1853 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001854 }
1855}
1856
chaviw4b129c22018-04-09 16:19:43 -07001857std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1858 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1859 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1860
1861 std::vector<Layer*> layersInTree = {this};
1862 for (size_t i = 0; i < children.size(); i++) {
1863 const auto& child = children[i];
1864 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1865 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1866 }
1867
1868 return layersInTree;
1869}
1870
1871void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1872 const LayerVector::Visitor& visitor) {
1873 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1874 std::sort(layersInTree.begin(), layersInTree.end());
1875 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1876}
1877
Peiyong Linefefaac2018-08-17 12:27:51 -07001878ui::Transform Layer::getTransform() const {
1879 ui::Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001880 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001881 if (p != nullptr) {
1882 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07001883
1884 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
1885 // it isFixedSize) then there may be additional scaling not accounted
1886 // for in the transform. We need to mirror this scaling in child surfaces
1887 // or we will break the contract where WM can treat child surfaces as
1888 // pixels in the parent surface.
David Sodman0cc69182017-11-17 12:12:07 -08001889 if (p->isFixedSize() && p->getBE().compositionInfo.mBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07001890 int bufferWidth;
1891 int bufferHeight;
1892 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
David Sodman0cc69182017-11-17 12:12:07 -08001893 bufferWidth = p->getBE().compositionInfo.mBuffer->getWidth();
1894 bufferHeight = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001895 } else {
David Sodman0cc69182017-11-17 12:12:07 -08001896 bufferHeight = p->getBE().compositionInfo.mBuffer->getWidth();
1897 bufferWidth = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001898 }
Marissa Wall61c58622018-07-18 10:12:20 -07001899 float sx = p->getActiveWidth(p->getDrawingState()) / static_cast<float>(bufferWidth);
1900 float sy = p->getActiveHeight(p->getDrawingState()) / static_cast<float>(bufferHeight);
Peiyong Linefefaac2018-08-17 12:27:51 -07001901 ui::Transform extraParentScaling;
Robert Carr9b429f42017-04-17 14:56:57 -07001902 extraParentScaling.set(sx, 0, 0, sy);
1903 t = t * extraParentScaling;
1904 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001905 }
Marissa Wall61c58622018-07-18 10:12:20 -07001906 return t * getActiveTransform(getDrawingState());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001907}
1908
chaviw13fdc492017-06-27 12:40:18 -07001909half Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001910 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07001911
chaviw13fdc492017-06-27 12:40:18 -07001912 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1913 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001914}
Robert Carr6452f122017-03-21 10:41:29 -07001915
chaviw13fdc492017-06-27 12:40:18 -07001916half4 Layer::getColor() const {
1917 const half4 color(getDrawingState().color);
1918 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001919}
Robert Carr6452f122017-03-21 10:41:29 -07001920
Robert Carr1f0a16a2016-10-24 16:27:39 -07001921void Layer::commitChildList() {
1922 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1923 const auto& child = mCurrentChildren[i];
1924 child->commitChildList();
1925 }
1926 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001927 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001928}
1929
Robert Carr720e5062018-07-30 17:45:14 -07001930void Layer::setInputInfo(const InputWindowInfo& info) {
1931 mCurrentState.inputInfo = info;
1932 mCurrentState.modified = true;
1933 mCurrentState.inputInfoChanged = true;
1934 setTransactionFlags(eTransactionNeeded);
1935}
1936
chaviw1d044282017-09-27 12:19:28 -07001937void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
1938 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1939 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1940 const State& state = useDrawing ? mDrawingState : mCurrentState;
1941
Peiyong Linefefaac2018-08-17 12:27:51 -07001942 ui::Transform requestedTransform = state.active_legacy.transform;
1943 ui::Transform transform = getTransform();
chaviw1d044282017-09-27 12:19:28 -07001944
1945 layerInfo->set_id(sequence);
1946 layerInfo->set_name(getName().c_str());
1947 layerInfo->set_type(String8(getTypeId()));
1948
1949 for (const auto& child : children) {
1950 layerInfo->add_children(child->sequence);
1951 }
1952
1953 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1954 sp<Layer> strongRelative = weakRelative.promote();
1955 if (strongRelative != nullptr) {
1956 layerInfo->add_relatives(strongRelative->sequence);
1957 }
1958 }
1959
Marissa Wallf58c14b2018-07-24 10:50:43 -07001960 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
chaviw1d044282017-09-27 12:19:28 -07001961 layerInfo->mutable_transparent_region());
1962 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
1963 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
1964
1965 layerInfo->set_layer_stack(getLayerStack());
1966 layerInfo->set_z(state.z);
1967
1968 PositionProto* position = layerInfo->mutable_position();
1969 position->set_x(transform.tx());
1970 position->set_y(transform.ty());
1971
1972 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
1973 requestedPosition->set_x(requestedTransform.tx());
1974 requestedPosition->set_y(requestedTransform.ty());
1975
1976 SizeProto* size = layerInfo->mutable_size();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001977 size->set_w(state.active_legacy.w);
1978 size->set_h(state.active_legacy.h);
chaviw1d044282017-09-27 12:19:28 -07001979
Marissa Wallf58c14b2018-07-24 10:50:43 -07001980 LayerProtoHelper::writeToProto(state.crop_legacy, layerInfo->mutable_crop());
chaviw1d044282017-09-27 12:19:28 -07001981
1982 layerInfo->set_is_opaque(isOpaque(state));
1983 layerInfo->set_invalidate(contentDirty);
Chia-I Wu01591c92018-05-22 12:03:00 -07001984
1985 // XXX (b/79210409) mCurrentDataSpace is not protected
1986 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
1987
chaviw1d044282017-09-27 12:19:28 -07001988 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1989 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
1990 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
1991 layerInfo->set_flags(state.flags);
1992
1993 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1994 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
1995
Jorim Jaggi8e0af362017-11-14 16:28:28 +01001996 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07001997 if (parent != nullptr) {
1998 layerInfo->set_parent(parent->sequence);
1999 }
2000
2001 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2002 if (zOrderRelativeOf != nullptr) {
2003 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
2004 }
2005
Chia-I Wu01591c92018-05-22 12:03:00 -07002006 // XXX getBE().compositionInfo.mBuffer is not protected
David Sodman0cc69182017-11-17 12:12:07 -08002007 auto buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08002008 if (buffer != nullptr) {
2009 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
Peiyong Linefefaac2018-08-17 12:27:51 -07002010 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
Yichi Chen6ca35192018-05-29 12:20:43 +08002011 layerInfo->mutable_buffer_transform());
chaviw1d044282017-09-27 12:19:28 -07002012 }
2013
2014 layerInfo->set_queued_frames(getQueuedFrameCount());
2015 layerInfo->set_refresh_pending(isBufferLatched());
rongliucfb187b2018-03-14 12:26:23 -07002016 layerInfo->set_window_type(state.type);
2017 layerInfo->set_app_id(state.appId);
chaviwadc40c22018-07-10 16:57:27 -07002018 layerInfo->set_curr_frame(mCurrentFrameNumber);
Vishnu Nairf2deb822018-11-12 17:53:48 -08002019 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
chaviwadc40c22018-07-10 16:57:27 -07002020
2021 for (const auto& pendingState : mPendingStates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07002022 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
chaviwadc40c22018-07-10 16:57:27 -07002023 if (barrierLayer != nullptr) {
2024 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
2025 barrierLayerProto->set_id(barrierLayer->sequence);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002026 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
chaviwadc40c22018-07-10 16:57:27 -07002027 }
2028 }
chaviw1d044282017-09-27 12:19:28 -07002029}
2030
Dominik Laskowski075d3172018-05-24 15:50:06 -07002031void Layer::writeToProto(LayerProto* layerInfo, DisplayId displayId) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07002032 if (!hasHwcLayer(displayId)) {
2033 return;
2034 }
2035
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002036 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
2037
Dominik Laskowski7e045462018-05-30 13:02:02 -07002038 const auto& hwcInfo = getBE().mHwcLayers.at(displayId);
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002039
2040 const Rect& frame = hwcInfo.displayFrame;
2041 LayerProtoHelper::writeToProto(frame, layerInfo->mutable_hwc_frame());
2042
2043 const FloatRect& crop = hwcInfo.sourceCrop;
2044 LayerProtoHelper::writeToProto(crop, layerInfo->mutable_hwc_crop());
2045
2046 const int32_t transform = static_cast<int32_t>(hwcInfo.transform);
2047 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002048
2049 const int32_t compositionType = static_cast<int32_t>(hwcInfo.compositionType);
2050 layerInfo->set_hwc_composition_type(compositionType);
2051
2052 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
2053 static_cast<BufferLayer*>(this)->isProtected()) {
2054 layerInfo->set_is_protected(true);
2055 } else {
2056 layerInfo->set_is_protected(false);
2057 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002058}
2059
Robert Carr2e102c92018-10-23 12:11:15 -07002060bool Layer::isRemovedFromCurrentState() const {
2061 return mRemovedFromCurrentState;
2062}
2063
Robert Carr720e5062018-07-30 17:45:14 -07002064InputWindowInfo Layer::fillInputInfo(const Rect& screenBounds) {
2065 InputWindowInfo info = mDrawingState.inputInfo;
Robert Carr5cb25782018-11-14 14:01:42 -08002066 info.frameLeft = screenBounds.left + info.surfaceInset;
2067 info.frameTop = screenBounds.top + info.surfaceInset;
2068 info.frameRight = screenBounds.right - info.surfaceInset;
2069 info.frameBottom = screenBounds.bottom - info.surfaceInset;
Robert Carr720e5062018-07-30 17:45:14 -07002070
Robert Carre07e1032018-11-26 12:55:53 -08002071 ui::Transform t = getTransform();
Riddle Hsu39d4aa52018-11-30 20:46:53 +08002072 const float xScale = t.sx();
2073 const float yScale = t.sy();
2074 if (xScale != 1.0f || yScale != 1.0f) {
2075 info.windowXScale *= 1.0f / xScale;
2076 info.windowYScale *= 1.0f / yScale;
2077 info.touchableRegion.scaleSelf(xScale, yScale);
2078 }
Robert Carre07e1032018-11-26 12:55:53 -08002079
Robert Carr5cb25782018-11-14 14:01:42 -08002080 info.touchableRegion = info.touchableRegion.translate(
Robert Carr720e5062018-07-30 17:45:14 -07002081 screenBounds.left,
2082 screenBounds.top);
2083 info.visible = isVisible();
2084 return info;
2085}
2086
2087bool Layer::hasInput() const {
Robert Carr5c8a0262018-10-03 16:30:44 -07002088 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002089}
2090
Mathias Agopian13127d82013-03-05 17:47:11 -08002091// ---------------------------------------------------------------------------
2092
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002093}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002094
2095#if defined(__gl_h_)
2096#error "don't include gl/gl.h in this file"
2097#endif
2098
2099#if defined(__gl2_h_)
2100#error "don't include gl2/gl2.h in this file"
2101#endif