blob: 2ac42e83d8c5aa2458e7b4d1793ea7c7ffeb6074 [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"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080055#include "TimeStats/TimeStats.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070056
Peiyong Lincbc184f2018-08-22 13:24:10 -070057#include <renderengine/RenderEngine.h>
Mathias Agopian875d8e12013-06-07 15:35:48 -070058
Dan Stozac5da2712016-07-20 15:38:12 -070059#include <mutex>
chaviw1d044282017-09-27 12:19:28 -070060#include "LayerProtoHelper.h"
Dan Stozac5da2712016-07-20 15:38:12 -070061
David Sodman41fdfc92017-11-06 16:09:56 -080062#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064namespace android {
65
Lloyd Piquef1c675b2018-09-12 20:45:39 -070066std::atomic<int32_t> Layer::sSequence{1};
Mathias Agopian13127d82013-03-05 17:47:11 -080067
Lloyd Pique42ab75e2018-09-12 20:46:03 -070068Layer::Layer(const LayerCreationArgs& args)
69 : mFlinger(args.flinger),
70 mName(args.name),
71 mClientRef(args.client),
72 mBE{this, args.name.string()} {
Mathias Agopiana67932f2011-04-20 14:20:59 -070073 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070074
75 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -070076 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
77 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
78 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070079
Dan Stozaf7ba41a2017-05-10 15:11:11 -070080 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070081
Lloyd Pique42ab75e2018-09-12 20:46:03 -070082 mCurrentState.active_legacy.w = args.w;
83 mCurrentState.active_legacy.h = args.h;
David Sodman0c69cad2017-08-21 12:12:51 -070084 mCurrentState.flags = layerFlags;
Marissa Wallf58c14b2018-07-24 10:50:43 -070085 mCurrentState.active_legacy.transform.set(0, 0);
86 mCurrentState.crop_legacy.makeInvalid();
Marissa Wallf58c14b2018-07-24 10:50:43 -070087 mCurrentState.requestedCrop_legacy = mCurrentState.crop_legacy;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070088 mCurrentState.z = 0;
chaviw13fdc492017-06-27 12:40:18 -070089 mCurrentState.color.a = 1.0f;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070090 mCurrentState.layerStack = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070091 mCurrentState.sequence = 0;
Marissa Wallf58c14b2018-07-24 10:50:43 -070092 mCurrentState.requested_legacy = mCurrentState.active_legacy;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -050093 mCurrentState.appId = 0;
94 mCurrentState.type = 0;
Marissa Wall61c58622018-07-18 10:12:20 -070095 mCurrentState.active.w = 0;
96 mCurrentState.active.h = 0;
97 mCurrentState.active.transform.set(0, 0);
98 mCurrentState.transform = 0;
99 mCurrentState.transformToDisplayInverse = false;
100 mCurrentState.crop.makeInvalid();
101 mCurrentState.acquireFence = new Fence(-1);
102 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
103 mCurrentState.hdrMetadata.validTypes = 0;
104 mCurrentState.surfaceDamageRegion.clear();
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700105 mCurrentState.cornerRadius = 0.0f;
Marissa Wall61c58622018-07-18 10:12:20 -0700106 mCurrentState.api = -1;
Peiyong Lin747321c2018-10-01 10:03:11 -0700107 mCurrentState.hasColorTransform = false;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700108
109 // drawing state & current state are identical
110 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700111
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800112 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700113 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800114 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200115 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700116
117 mFlinger->onLayerCreated();
Dan Stoza436ccf32018-06-21 12:10:12 -0700118}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700119
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700120Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800121 sp<Client> c(mClientRef.promote());
122 if (c != 0) {
123 c->detachLayer(this);
124 }
125
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000126 mFrameTracker.logAndResetStats(mName);
Robert Carr2e102c92018-10-23 12:11:15 -0700127
chaviw61626f22018-11-15 16:26:27 -0800128 destroyAllHwcLayersPlusChildren();
Robert Carr2e102c92018-10-23 12:11:15 -0700129
130 mFlinger->onLayerDestroyed();
Mathias Agopian96f08192010-06-02 23:28:45 -0700131}
132
Mathias Agopian13127d82013-03-05 17:47:11 -0800133// ---------------------------------------------------------------------------
134// callbacks
135// ---------------------------------------------------------------------------
136
David Sodmaneb085e02017-10-05 18:49:04 -0700137/*
138 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
139 * Layer. So, the implementation is done in BufferLayer. When called on a
140 * ColorLayer object, it's essentially a NOP.
141 */
David Sodmaneb085e02017-10-05 18:49:04 -0700142void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800143
Chia-I Wuc6657022017-08-15 11:18:17 -0700144void Layer::onRemovedFromCurrentState() {
Robert Carr2e102c92018-10-23 12:11:15 -0700145 mRemovedFromCurrentState = true;
146
Chia-I Wuc6657022017-08-15 11:18:17 -0700147 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
Robert Carr5edb1ad2017-04-25 10:54:24 -0700148 if (mCurrentState.zOrderRelativeOf != nullptr) {
149 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
150 if (strongRelative != nullptr) {
151 strongRelative->removeZOrderRelative(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700152 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700153 }
154 mCurrentState.zOrderRelativeOf = nullptr;
155 }
Rob Carr4bba3702018-10-08 21:53:30 +0000156
Robert Carr2e102c92018-10-23 12:11:15 -0700157 // Since we are no longer reachable from CurrentState SurfaceFlinger
158 // will no longer invoke doTransaction for us, and so we will
159 // never finish applying transactions. We signal the sync point
160 // now so that another layer will not become indefinitely
161 // blocked.
162 for (auto& point: mRemoteSyncPoints) {
163 point->setTransactionApplied();
164 }
165 mRemoteSyncPoints.clear();
166
167 {
168 Mutex::Autolock syncLock(mLocalSyncPointMutex);
169 for (auto& point : mLocalSyncPoints) {
170 point->setFrameAvailable();
171 }
172 mLocalSyncPoints.clear();
173 }
174
Chia-I Wuc6657022017-08-15 11:18:17 -0700175 for (const auto& child : mCurrentChildren) {
176 child->onRemovedFromCurrentState();
177 }
178}
Chia-I Wu38512252017-05-17 14:36:16 -0700179
chaviw61626f22018-11-15 16:26:27 -0800180void Layer::addToCurrentState() {
181 mRemovedFromCurrentState = false;
182
183 for (const auto& child : mCurrentChildren) {
184 child->addToCurrentState();
185 }
186}
187
Mathias Agopian13127d82013-03-05 17:47:11 -0800188// ---------------------------------------------------------------------------
189// set-up
190// ---------------------------------------------------------------------------
191
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700192const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800193 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800194}
195
chaviw13fdc492017-06-27 12:40:18 -0700196bool Layer::getPremultipledAlpha() const {
197 return mPremultipliedAlpha;
198}
199
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700200sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800201 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700202 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800203}
204
205// ---------------------------------------------------------------------------
206// h/w composer set-up
207// ---------------------------------------------------------------------------
208
Dominik Laskowski075d3172018-05-24 15:50:06 -0700209bool Layer::createHwcLayer(HWComposer* hwc, DisplayId displayId) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700210 LOG_ALWAYS_FATAL_IF(hasHwcLayer(displayId), "Already have a layer for display %s",
211 to_string(displayId).c_str());
David Sodmanb8aaea12017-12-14 15:54:51 -0800212 auto layer = std::shared_ptr<HWC2::Layer>(
213 hwc->createLayer(displayId),
214 [hwc, displayId](HWC2::Layer* layer) {
215 hwc->destroyLayer(displayId, layer); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700216 if (!layer) {
217 return false;
218 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700219 LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers[displayId];
Steven Thomasb02664d2017-07-26 18:48:28 -0700220 hwcInfo.hwc = hwc;
221 hwcInfo.layer = layer;
David Sodmanf6a38932018-05-25 15:27:50 -0700222 layer->setLayerDestroyedListener(
Dominik Laskowski7e045462018-05-30 13:02:02 -0700223 [this, displayId](HWC2::Layer* /*layer*/) { getBE().mHwcLayers.erase(displayId); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700224 return true;
225}
226
Dominik Laskowski075d3172018-05-24 15:50:06 -0700227bool Layer::destroyHwcLayer(DisplayId displayId) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700228 if (!hasHwcLayer(displayId)) {
Chia-I Wu83806892017-11-16 10:50:20 -0800229 return false;
Steven Thomasb02664d2017-07-26 18:48:28 -0700230 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700231 auto& hwcInfo = getBE().mHwcLayers[displayId];
David Sodman41fdfc92017-11-06 16:09:56 -0800232 LOG_ALWAYS_FATAL_IF(hwcInfo.layer == nullptr, "Attempt to destroy null layer");
Steven Thomasb02664d2017-07-26 18:48:28 -0700233 LOG_ALWAYS_FATAL_IF(hwcInfo.hwc == nullptr, "Missing HWComposer");
David Sodmanb8aaea12017-12-14 15:54:51 -0800234 hwcInfo.layer = nullptr;
235
Chia-I Wu83806892017-11-16 10:50:20 -0800236 return true;
Steven Thomasb02664d2017-07-26 18:48:28 -0700237}
238
chaviw61626f22018-11-15 16:26:27 -0800239void Layer::destroyHwcLayersForAllDisplays() {
David Sodman6f65f3e2017-11-03 14:28:09 -0700240 size_t numLayers = getBE().mHwcLayers.size();
Steven Thomasb02664d2017-07-26 18:48:28 -0700241 for (size_t i = 0; i < numLayers; ++i) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700242 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.empty(), "destroyAllHwcLayers failed");
243 destroyHwcLayer(getBE().mHwcLayers.begin()->first);
Steven Thomasb02664d2017-07-26 18:48:28 -0700244 }
chaviw61626f22018-11-15 16:26:27 -0800245}
246
247void Layer::destroyAllHwcLayersPlusChildren() {
248 destroyHwcLayersForAllDisplays();
David Sodman6f65f3e2017-11-03 14:28:09 -0700249 LOG_ALWAYS_FATAL_IF(!getBE().mHwcLayers.empty(),
David Sodman41fdfc92017-11-06 16:09:56 -0800250 "All hardware composer layers should have been destroyed");
Robert Carr2e102c92018-10-23 12:11:15 -0700251
252 for (const sp<Layer>& child : mDrawingChildren) {
chaviw61626f22018-11-15 16:26:27 -0800253 child->destroyAllHwcLayersPlusChildren();
Robert Carr2e102c92018-10-23 12:11:15 -0700254 }
Steven Thomasb02664d2017-07-26 18:48:28 -0700255}
Steven Thomasb02664d2017-07-26 18:48:28 -0700256
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800257Rect Layer::getContentCrop() const {
258 // this is the crop rectangle that applies to the buffer
259 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700260 Rect crop;
261 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800262 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700263 crop = mCurrentCrop;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800264 } else if (getBE().compositionInfo.mBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800265 // otherwise we use the whole buffer
David Sodman0cc69182017-11-17 12:12:07 -0800266 crop = getBE().compositionInfo.mBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700267 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800268 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700269 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700270 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700271 return crop;
272}
273
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700274static Rect reduce(const Rect& win, const Region& exclude) {
275 if (CC_LIKELY(exclude.isEmpty())) {
276 return win;
277 }
278 if (exclude.isRect()) {
279 return win.reduce(exclude.getBounds());
280 }
281 return Region(win).subtract(exclude).getBounds();
282}
283
Dan Stoza80d61162017-12-20 15:57:52 -0800284static FloatRect reduce(const FloatRect& win, const Region& exclude) {
285 if (CC_LIKELY(exclude.isEmpty())) {
286 return win;
287 }
288 // Convert through Rect (by rounding) for lack of FloatRegion
289 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
290}
291
Vishnu Nair60356342018-11-13 13:00:45 -0800292Rect Layer::computeScreenBounds() const {
293 FloatRect bounds = computeBounds();
Peiyong Linefefaac2018-08-17 12:27:51 -0700294 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800295 // Transform to screen space.
296 bounds = t.transform(bounds);
297 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700298}
299
Dan Stoza80d61162017-12-20 15:57:52 -0800300FloatRect Layer::computeBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000301 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700302 return computeBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700303}
304
Dan Stoza80d61162017-12-20 15:57:52 -0800305FloatRect Layer::computeBounds(const Region& activeTransparentRegion) const {
Alec Mourib416efd2018-09-06 21:01:59 +0000306 const State& s(getDrawingState());
Vishnu Nair60356342018-11-13 13:00:45 -0800307 Rect bounds = getCroppedBufferSize(s);
308 FloatRect floatBounds = bounds.toFloatRect();
309 if (bounds.isValid()) {
310 // Layer has bounds. Pass in our bounds as a special case. Then pass on to our parents so
311 // that they can clip it.
312 floatBounds = cropChildBounds(floatBounds);
313 } else {
314 // Layer does not have bounds, so we fill to our parent bounds. This is done by getting our
315 // parent bounds and inverting the transform to get the maximum bounds we can have that
316 // will fit within our parent bounds.
317 const auto& p = mDrawingParent.promote();
318 if (p != nullptr) {
319 ui::Transform t = s.active_legacy.transform;
320 // When calculating the parent bounds for purposes of clipping, we don't need to
321 // constrain the parent to its transparent region. The transparent region is an
322 // optimization based on the buffer contents of the layer, but does not affect the
323 // space allocated to it by policy, and thus children should be allowed to extend into
324 // the parent's transparent region.
325 // One of the main uses is a parent window with a child sitting behind the parent
326 // window, marked by a transparent region. When computing the parent bounds from the
327 // parent's perspective we pass in the transparent region to reduce buffer allocation
328 // size. When computing the parent bounds from the child's perspective, we pass in an
329 // empty transparent region in order to extend into the the parent bounds.
330 floatBounds = p->computeBounds(Region());
331 // Transform back to layer space.
332 floatBounds = t.inverse().transform(floatBounds);
333 }
334 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700335
Vishnu Nair60356342018-11-13 13:00:45 -0800336 // Subtract the transparent region and snap to the bounds.
337 return reduce(floatBounds, activeTransparentRegion);
338}
339
340FloatRect Layer::cropChildBounds(const FloatRect& childBounds) const {
341 const State& s(getDrawingState());
342 Rect bounds = getCroppedBufferSize(s);
343 FloatRect croppedBounds = childBounds;
344
345 // If the layer has bounds, then crop the passed in child bounds and pass
346 // it to our parents so they can crop it as well. If the layer has no bounds,
347 // then pass on the child bounds.
348 if (bounds.isValid()) {
349 croppedBounds = croppedBounds.intersect(bounds.toFloatRect());
Mathias Agopian13127d82013-03-05 17:47:11 -0800350 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700351
Chia-I Wue41dbe62017-06-13 14:10:56 -0700352 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700353 if (p != nullptr) {
Vishnu Nair60356342018-11-13 13:00:45 -0800354 // Transform to parent space and allow parent layer to crop the
355 // child bounds as well.
356 ui::Transform t = s.active_legacy.transform;
357 croppedBounds = t.transform(croppedBounds);
358 croppedBounds = p->cropChildBounds(croppedBounds);
359 croppedBounds = t.inverse().transform(croppedBounds);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700360 }
Vishnu Nair60356342018-11-13 13:00:45 -0800361 return croppedBounds;
362}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700363
Vishnu Nair60356342018-11-13 13:00:45 -0800364Rect Layer::getCroppedBufferSize(const State& s) const {
365 Rect size = getBufferSize(s);
366 Rect crop = getCrop(s);
367 if (!crop.isEmpty() && size.isValid()) {
368 size.intersect(crop, &size);
369 } else if (!crop.isEmpty()) {
370 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700371 }
Vishnu Nair60356342018-11-13 13:00:45 -0800372 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800373}
374
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700375Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& display) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700376 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800377 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700378 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800379
380 // apply the projection's clipping to the window crop in
381 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700382 // if there are no window scaling involved, this operation will map to full
383 // pixels in the buffer.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700384
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800385 FloatRect activeCropFloat = computeBounds();
Peiyong Linefefaac2018-08-17 12:27:51 -0700386 ui::Transform t = getTransform();
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800387 // Transform to screen space.
388 activeCropFloat = t.transform(activeCropFloat);
389 activeCropFloat = activeCropFloat.intersect(display->getViewport().toFloatRect());
390 // Back to layer space to work with the content crop.
391 activeCropFloat = t.inverse().transform(activeCropFloat);
392 // This needs to be here as transform.transform(Rect) computes the
393 // transformed rect and then takes the bounding box of the result before
394 // returning. This means
395 // transform.inverse().transform(transform.transform(Rect)) != Rect
396 // in which case we need to make sure the final rect is clipped to the
397 // display bounds.
398 Rect activeCrop{activeCropFloat};
399 if (!activeCrop.intersect(getBufferSize(s), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000400 activeCrop.clear();
401 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700402 return activeCrop;
403}
404
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700405void Layer::setupRoundedCornersCropCoordinates(Rect win,
406 const FloatRect& roundedCornersCrop) const {
407 // Translate win by the rounded corners rect coordinates, to have all values in
408 // layer coordinate space.
409 win.left -= roundedCornersCrop.left;
410 win.right -= roundedCornersCrop.left;
411 win.top -= roundedCornersCrop.top;
412 win.bottom -= roundedCornersCrop.top;
413
414 renderengine::Mesh::VertexArray<vec2> cropCoords(getBE().mMesh.getCropCoordArray<vec2>());
415 cropCoords[0] = vec2(win.left, win.top);
416 cropCoords[1] = vec2(win.left, win.top + win.getHeight());
417 cropCoords[2] = vec2(win.right, win.top + win.getHeight());
418 cropCoords[3] = vec2(win.right, win.top);
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700419}
420
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700421FloatRect Layer::computeCrop(const sp<const DisplayDevice>& display) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700422 // the content crop is the area of the content that gets scaled to the
423 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800424 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700425
426 // In addition there is a WM-specified crop we pull from our drawing state.
427 const State& s(getDrawingState());
428
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700429 Rect activeCrop = computeInitialCrop(display);
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800430 Rect bufferSize = getBufferSize(s);
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700431
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000432 // Transform the window crop to match the buffer coordinate system,
433 // which means using the inverse of the current transform set on the
434 // SurfaceFlingerConsumer.
435 uint32_t invTransform = mCurrentTransform;
Robert Carrcae605c2017-03-29 12:10:31 -0700436 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000437 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700438 * the code below applies the primary display's inverse transform to the
439 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000440 */
David Sodman41fdfc92017-11-06 16:09:56 -0800441 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000442 // calculate the inverse transform
443 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800444 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800445 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000446 // and apply to the current transform
Peiyong Linefefaac2018-08-17 12:27:51 -0700447 invTransform = (ui::Transform(invTransformOrient) *
448 ui::Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800449 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000450
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800451 int winWidth = bufferSize.getWidth();
452 int winHeight = bufferSize.getHeight();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000453 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
454 // If the activeCrop has been rotate the ends are rotated but not
455 // the space itself so when transforming ends back we can't rely on
456 // a modification of the axes of rotation. To account for this we
457 // need to reorient the inverse rotation in terms of the current
458 // axes of rotation.
459 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
460 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
461 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800462 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000463 }
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800464 std::swap(winWidth, winHeight);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000465 }
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800466 const Rect winCrop =
467 activeCrop.transform(invTransform, bufferSize.getWidth(), bufferSize.getHeight());
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000468
469 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800470 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000471 float yScale = crop.getHeight() / float(winHeight);
472
David Sodman41fdfc92017-11-06 16:09:56 -0800473 float insetL = winCrop.left * xScale;
474 float insetT = winCrop.top * yScale;
475 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000476 float insetB = (winHeight - winCrop.bottom) * yScale;
477
David Sodman41fdfc92017-11-06 16:09:56 -0800478 crop.left += insetL;
479 crop.top += insetT;
480 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000481 crop.bottom -= insetB;
482
Mathias Agopian13127d82013-03-05 17:47:11 -0800483 return crop;
484}
485
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700486void Layer::setGeometry(const sp<const DisplayDevice>& display, uint32_t z) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700487 const auto displayId = display->getId();
Dominik Laskowski075d3172018-05-24 15:50:06 -0700488 LOG_ALWAYS_FATAL_IF(!displayId);
Dominik Laskowski34157762018-10-31 13:07:19 -0700489 RETURN_IF_NO_HWC_LAYER(*displayId);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700490 auto& hwcInfo = getBE().mHwcLayers[*displayId];
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700491
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700492 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800493 hwcInfo.forceClientComposition = false;
494
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700495 if (isSecure() && !display->isSecure()) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800496 hwcInfo.forceClientComposition = true;
497 }
498
David Sodman15094112018-10-11 09:39:37 -0700499 auto& hwcLayer = hwcInfo.layer;
500
Mathias Agopian13127d82013-03-05 17:47:11 -0800501 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700502 const State& s(getDrawingState());
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800503 const int bufferWidth = getBufferSize(s).getWidth();
504 const int bufferHeight = getBufferSize(s).getHeight();
David Revemanecf0fa52017-03-03 11:32:44 -0500505 auto blendMode = HWC2::BlendMode::None;
Robert Carr6452f122017-03-21 10:41:29 -0700506 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800507 blendMode =
508 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800509 }
David Sodman15094112018-10-11 09:39:37 -0700510 auto error = hwcLayer->setBlendMode(blendMode);
511 ALOGE_IF(error != HWC2::Error::None,
512 "[%s] Failed to set blend mode %s:"
513 " %s (%d)",
514 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
515 static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700516 getBE().compositionInfo.hwc.blendMode = blendMode;
Mathias Agopian13127d82013-03-05 17:47:11 -0800517
518 // apply the layer's transform, followed by the display's global transform
519 // here we're guaranteed that the layer's transform preserves rects
Marissa Wall61c58622018-07-18 10:12:20 -0700520 Region activeTransparentRegion(getActiveTransparentRegion(s));
Peiyong Linefefaac2018-08-17 12:27:51 -0700521 ui::Transform t = getTransform();
Marissa Wall61c58622018-07-18 10:12:20 -0700522 Rect activeCrop = getCrop(s);
523 if (!activeCrop.isEmpty()) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700524 activeCrop = t.transform(activeCrop);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700525 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000526 activeCrop.clear();
527 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700528 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800529 // This needs to be here as transform.transform(Rect) computes the
530 // transformed rect and then takes the bounding box of the result before
531 // returning. This means
532 // transform.inverse().transform(transform.transform(Rect)) != Rect
533 // in which case we need to make sure the final rect is clipped to the
534 // display bounds.
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800535 if (!activeCrop.intersect(Rect(bufferWidth, bufferHeight), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000536 activeCrop.clear();
537 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700538 // mark regions outside the crop as transparent
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800539 activeTransparentRegion.orSelf(Rect(0, 0, bufferWidth, activeCrop.top));
540 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom, bufferWidth, bufferHeight));
David Sodman41fdfc92017-11-06 16:09:56 -0800541 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
542 activeTransparentRegion.orSelf(
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800543 Rect(activeCrop.right, activeCrop.top, bufferWidth, activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700544 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700545
Dan Stoza80d61162017-12-20 15:57:52 -0800546 // computeBounds returns a FloatRect to provide more accuracy during the
547 // transformation. We then round upon constructing 'frame'.
548 Rect frame{t.transform(computeBounds(activeTransparentRegion))};
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700549 if (!frame.intersect(display->getViewport(), &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000550 frame.clear();
551 }
Peiyong Linefefaac2018-08-17 12:27:51 -0700552 const ui::Transform& tr = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800553 Rect transformedFrame = tr.transform(frame);
David Sodman15094112018-10-11 09:39:37 -0700554 error = hwcLayer->setDisplayFrame(transformedFrame);
555 if (error != HWC2::Error::None) {
556 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
557 transformedFrame.left, transformedFrame.top, transformedFrame.right,
558 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
559 } else {
560 hwcInfo.displayFrame = transformedFrame;
561 }
David Sodmanba340492018-08-05 21:51:33 -0700562 getBE().compositionInfo.hwc.displayFrame = transformedFrame;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800563
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700564 FloatRect sourceCrop = computeCrop(display);
David Sodman15094112018-10-11 09:39:37 -0700565 error = hwcLayer->setSourceCrop(sourceCrop);
566 if (error != HWC2::Error::None) {
567 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
568 "%s (%d)",
569 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
570 to_string(error).c_str(), static_cast<int32_t>(error));
571 } else {
572 hwcInfo.sourceCrop = sourceCrop;
573 }
David Sodmanba340492018-08-05 21:51:33 -0700574 getBE().compositionInfo.hwc.sourceCrop = sourceCrop;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800575
chaviw13fdc492017-06-27 12:40:18 -0700576 float alpha = static_cast<float>(getAlpha());
David Sodman15094112018-10-11 09:39:37 -0700577 error = hwcLayer->setPlaneAlpha(alpha);
578 ALOGE_IF(error != HWC2::Error::None,
579 "[%s] Failed to set plane alpha %.3f: "
580 "%s (%d)",
581 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700582 getBE().compositionInfo.hwc.alpha = alpha;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800583
David Sodman15094112018-10-11 09:39:37 -0700584 error = hwcLayer->setZOrder(z);
585 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
586 to_string(error).c_str(), static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700587 getBE().compositionInfo.hwc.z = z;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500588
Albert Chaulk2a589632017-05-04 16:59:44 -0400589 int type = s.type;
590 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700591 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400592 if (parent.get()) {
593 auto& parentState = parent->getDrawingState();
rongliucfb187b2018-03-14 12:26:23 -0700594 if (parentState.type >= 0 || parentState.appId >= 0) {
595 type = parentState.type;
596 appId = parentState.appId;
597 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400598 }
599
David Sodman15094112018-10-11 09:39:37 -0700600 error = hwcLayer->setInfo(type, appId);
601 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
602 static_cast<int32_t>(error));
603
David Sodmanba340492018-08-05 21:51:33 -0700604 getBE().compositionInfo.hwc.type = type;
605 getBE().compositionInfo.hwc.appId = appId;
606
Mathias Agopian29a367b2011-07-12 14:51:45 -0700607 /*
608 * Transformations are applied in this order:
609 * 1) buffer orientation/flip/mirror
610 * 2) state transformation (window manager)
611 * 3) layer orientation (screen orientation)
612 * (NOTE: the matrices are multiplied in reverse order)
613 */
614
Peiyong Linefefaac2018-08-17 12:27:51 -0700615 const ui::Transform bufferOrientation(mCurrentTransform);
616 ui::Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700617
Robert Carrcae605c2017-03-29 12:10:31 -0700618 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700619 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700620 * the code below applies the primary display's inverse transform to the
621 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700622 */
David Sodman41fdfc92017-11-06 16:09:56 -0800623 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700624 // calculate the inverse transform
625 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800626 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700627 }
Robert Carrcae605c2017-03-29 12:10:31 -0700628
629 /*
630 * Here we cancel out the orientation component of the WM transform.
631 * The scaling and translate components are already included in our bounds
632 * computation so it's enough to just omit it in the composition.
633 * See comment in onDraw with ref to b/36727915 for why.
634 */
Peiyong Linefefaac2018-08-17 12:27:51 -0700635 transform = ui::Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700636 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700637
638 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800639 const uint32_t orientation = transform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -0700640 if (orientation & ui::Transform::ROT_INVALID) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800641 // we can only handle simple transformation
Lloyd Pique074e8122018-07-26 12:57:23 -0700642 hwcInfo.forceClientComposition = true;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700643 getBE().mHwcLayers[*displayId].compositionType = HWC2::Composition::Client;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800644 } else {
645 auto transform = static_cast<HWC2::Transform>(orientation);
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800646 hwcInfo.transform = transform;
David Sodman15094112018-10-11 09:39:37 -0700647 auto error = hwcLayer->setTransform(transform);
648 ALOGE_IF(error != HWC2::Error::None,
649 "[%s] Failed to set transform %s: "
650 "%s (%d)",
651 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
652 static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700653 getBE().compositionInfo.hwc.transform = transform;
David Sodman4b7c4bc2017-11-17 12:13:59 -0800654 }
655}
656
Dominik Laskowski075d3172018-05-24 15:50:06 -0700657void Layer::forceClientComposition(DisplayId displayId) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700658 RETURN_IF_NO_HWC_LAYER(displayId);
Dominik Laskowski7e045462018-05-30 13:02:02 -0700659 getBE().mHwcLayers[displayId].forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800660}
Dan Stozaee44edd2015-03-23 15:50:23 -0700661
Dominik Laskowski075d3172018-05-24 15:50:06 -0700662bool Layer::getForceClientComposition(DisplayId displayId) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700663 RETURN_IF_NO_HWC_LAYER(displayId, false);
Dominik Laskowski7e045462018-05-30 13:02:02 -0700664 return getBE().mHwcLayers[displayId].forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800665}
666
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700667void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700668 const auto displayId = display->getId();
Dominik Laskowski34157762018-10-31 13:07:19 -0700669 LOG_ALWAYS_FATAL_IF(!displayId);
670 if (!hasHwcLayer(*displayId) || getCompositionType(displayId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800671 return;
672 }
673
674 // This gives us only the "orientation" component of the transform
675 const State& s(getCurrentState());
676
677 // Apply the layer's transform, followed by the display's global transform
678 // Here we're guaranteed that the layer's transform preserves rects
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800679 Rect win = getCroppedBufferSize(s);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800680 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700681 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Robert Carr1f0a16a2016-10-24 16:27:39 -0700682 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700683 frame.intersect(display->getViewport(), &frame);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700684 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800685 auto position = displayTransform.transform(frame);
686
Dominik Laskowski7e045462018-05-30 13:02:02 -0700687 auto error =
Dominik Laskowski075d3172018-05-24 15:50:06 -0700688 getBE().mHwcLayers[*displayId].layer->setCursorPosition(position.left, position.top);
689
David Sodman41fdfc92017-11-06 16:09:56 -0800690 ALOGE_IF(error != HWC2::Error::None,
691 "[%s] Failed to set cursor position "
692 "to (%d, %d): %s (%d)",
693 mName.string(), position.left, position.top, to_string(error).c_str(),
694 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800695}
Riley Andrews03414a12014-07-01 14:22:59 -0700696
Mathias Agopian13127d82013-03-05 17:47:11 -0800697// ---------------------------------------------------------------------------
698// drawing...
699// ---------------------------------------------------------------------------
700
Marissa Wall61c58622018-07-18 10:12:20 -0700701void Layer::draw(const RenderArea& renderArea, const Region& clip) {
chaviwa76b2712017-09-20 12:02:26 -0700702 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800703}
704
Marissa Wall61c58622018-07-18 10:12:20 -0700705void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) {
chaviwa76b2712017-09-20 12:02:26 -0700706 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800707}
708
David Sodman41fdfc92017-11-06 16:09:56 -0800709void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
710 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800711 auto& engine(mFlinger->getRenderEngine());
David Sodman9eeae692017-11-02 10:53:32 -0700712 computeGeometry(renderArea, getBE().mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700713 engine.setupFillWithColor(red, green, blue, alpha);
David Sodman9eeae692017-11-02 10:53:32 -0700714 engine.drawMesh(getBE().mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800715}
716
David Sodmanc1498e62018-09-12 14:36:26 -0700717void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
718 clearWithOpenGL(renderArea, 0, 0, 0, 0);
719}
720
Dominik Laskowski075d3172018-05-24 15:50:06 -0700721void Layer::setCompositionType(DisplayId displayId, HWC2::Composition type, bool callIntoHwc) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700722 if (getBE().mHwcLayers.count(displayId) == 0) {
Chia-I Wu30505fb2018-03-26 16:20:31 -0700723 ALOGE("setCompositionType called without a valid HWC layer");
724 return;
David Sodman15094112018-10-11 09:39:37 -0700725 }
726 auto& hwcInfo = getBE().mHwcLayers[displayId];
727 auto& hwcLayer = hwcInfo.layer;
728 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", (hwcLayer)->getId(), to_string(type).c_str(),
729 static_cast<int>(callIntoHwc));
730 if (hwcInfo.compositionType != type) {
731 ALOGV(" actually setting");
732 hwcInfo.compositionType = type;
733 if (callIntoHwc) {
734 auto error = (hwcLayer)->setCompositionType(type);
735 ALOGE_IF(error != HWC2::Error::None,
736 "[%s] Failed to set "
737 "composition type %s: %s (%d)",
738 mName.string(), to_string(type).c_str(), to_string(error).c_str(),
739 static_cast<int32_t>(error));
Chia-I Wu30505fb2018-03-26 16:20:31 -0700740 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800741 }
742}
743
Dominik Laskowski075d3172018-05-24 15:50:06 -0700744HWC2::Composition Layer::getCompositionType(const std::optional<DisplayId>& displayId) const {
745 if (!displayId) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700746 // If we're querying the composition type for a display that does not
747 // have a HWC counterpart, then it will always be Client
748 return HWC2::Composition::Client;
749 }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700750 if (getBE().mHwcLayers.count(*displayId) == 0) {
751 ALOGE("getCompositionType called with an invalid HWC layer");
752 return HWC2::Composition::Invalid;
753 }
754 return getBE().mHwcLayers.at(*displayId).compositionType;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800755}
756
Dominik Laskowski075d3172018-05-24 15:50:06 -0700757void Layer::setClearClientTarget(DisplayId displayId, bool clear) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700758 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800759 ALOGE("setClearClientTarget called without a valid HWC layer");
760 return;
761 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700762 getBE().mHwcLayers[displayId].clearClientTarget = clear;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800763}
764
Dominik Laskowski075d3172018-05-24 15:50:06 -0700765bool Layer::getClearClientTarget(DisplayId displayId) const {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700766 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800767 ALOGE("getClearClientTarget called without a valid HWC layer");
768 return false;
769 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700770 return getBE().mHwcLayers.at(displayId).clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800771}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800772
Dan Stozacac35382016-01-27 12:21:06 -0800773bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
774 if (point->getFrameNumber() <= mCurrentFrameNumber) {
775 // Don't bother with a SyncPoint, since we've already latched the
776 // relevant frame
777 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700778 }
Robert Carr2e102c92018-10-23 12:11:15 -0700779 if (isRemovedFromCurrentState()) {
780 return false;
781 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700782
Dan Stozacac35382016-01-27 12:21:06 -0800783 Mutex::Autolock lock(mLocalSyncPointMutex);
784 mLocalSyncPoints.push_back(point);
785 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700786}
787
Mathias Agopian13127d82013-03-05 17:47:11 -0800788// ----------------------------------------------------------------------------
789// local state
790// ----------------------------------------------------------------------------
791
Peiyong Lin833074a2018-08-28 11:53:54 -0700792void Layer::computeGeometry(const RenderArea& renderArea,
793 renderengine::Mesh& mesh,
chaviwa76b2712017-09-20 12:02:26 -0700794 bool useIdentityTransform) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700795 const ui::Transform renderAreaTransform(renderArea.getTransform());
Dan Stoza80d61162017-12-20 15:57:52 -0800796 FloatRect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700797
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000798 vec2 lt = vec2(win.left, win.top);
799 vec2 lb = vec2(win.left, win.bottom);
800 vec2 rb = vec2(win.right, win.bottom);
801 vec2 rt = vec2(win.right, win.top);
802
Peiyong Linefefaac2018-08-17 12:27:51 -0700803 ui::Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000804 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700805 lt = layerTransform.transform(lt);
806 lb = layerTransform.transform(lb);
807 rb = layerTransform.transform(rb);
808 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000809 }
810
Peiyong Lin833074a2018-08-28 11:53:54 -0700811 renderengine::Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700812 position[0] = renderAreaTransform.transform(lt);
813 position[1] = renderAreaTransform.transform(lb);
814 position[2] = renderAreaTransform.transform(rb);
815 position[3] = renderAreaTransform.transform(rt);
Mathias Agopian13127d82013-03-05 17:47:11 -0800816}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800817
David Sodman41fdfc92017-11-06 16:09:56 -0800818bool Layer::isSecure() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000819 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700820 return (s.flags & layer_state_t::eLayerSecure);
821}
822
Mathias Agopian13127d82013-03-05 17:47:11 -0800823void Layer::setVisibleRegion(const Region& visibleRegion) {
824 // always called from main thread
825 this->visibleRegion = visibleRegion;
826}
827
828void Layer::setCoveredRegion(const Region& coveredRegion) {
829 // always called from main thread
830 this->coveredRegion = coveredRegion;
831}
832
David Sodman41fdfc92017-11-06 16:09:56 -0800833void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800834 // always called from main thread
835 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
836}
837
Robert Carre5f4f692018-01-12 13:12:28 -0800838void Layer::clearVisibilityRegions() {
839 visibleRegion.clear();
840 visibleNonTransparentRegion.clear();
841 coveredRegion.clear();
842}
843
Mathias Agopian13127d82013-03-05 17:47:11 -0800844// ----------------------------------------------------------------------------
845// transaction
846// ----------------------------------------------------------------------------
847
Dan Stoza7dde5992015-05-22 09:51:44 -0700848void Layer::pushPendingState() {
849 if (!mCurrentState.modified) {
850 return;
851 }
852
Dan Stoza7dde5992015-05-22 09:51:44 -0700853 // If this transaction is waiting on the receipt of a frame, generate a sync
854 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700855 // We don't allow installing sync points after we are removed from the current state
856 // as we won't be able to signal our end.
857 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700858 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800859 if (barrierLayer == nullptr) {
860 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700861 // If we can't promote the layer we are intended to wait on,
862 // then it is expired or otherwise invalid. Allow this transaction
863 // to be applied as per normal (no synchronization).
Marissa Wallf58c14b2018-07-24 10:50:43 -0700864 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800865 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700866 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy);
Robert Carr0d480722017-01-10 16:42:54 -0800867 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800868 mRemoteSyncPoints.push_back(std::move(syncPoint));
869 } else {
870 // We already missed the frame we're supposed to synchronize
871 // on, so go ahead and apply the state update
Marissa Wallf58c14b2018-07-24 10:50:43 -0700872 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800873 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700874 }
875
Dan Stoza7dde5992015-05-22 09:51:44 -0700876 // Wake us up to check if the frame has been received
877 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700878 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700879 }
880 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700881 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700882}
883
Pablo Ceballos05289c22016-04-14 15:49:55 -0700884void Layer::popPendingState(State* stateToCommit) {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700885 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700886
887 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700888 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700889}
890
Pablo Ceballos05289c22016-04-14 15:49:55 -0700891bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700892 bool stateUpdateAvailable = false;
893 while (!mPendingStates.empty()) {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700894 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700895 if (mRemoteSyncPoints.empty()) {
896 // If we don't have a sync point for this, apply it anyway. It
897 // will be visually wrong, but it should keep us from getting
898 // into too much trouble.
899 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700900 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700901 stateUpdateAvailable = true;
902 continue;
903 }
904
Marissa Wallf58c14b2018-07-24 10:50:43 -0700905 if (mRemoteSyncPoints.front()->getFrameNumber() !=
906 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800907 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800908
909 // Signal our end of the sync point and then dispose of it
910 mRemoteSyncPoints.front()->setTransactionApplied();
911 mRemoteSyncPoints.pop_front();
912 continue;
913 }
914
Dan Stoza7dde5992015-05-22 09:51:44 -0700915 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
916 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700917 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700918 stateUpdateAvailable = true;
919
920 // Signal our end of the sync point and then dispose of it
921 mRemoteSyncPoints.front()->setTransactionApplied();
922 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800923 } else {
924 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700925 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700926 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700927 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700928 stateUpdateAvailable = true;
929 }
930 }
931
932 // If we still have pending updates, wake SurfaceFlinger back up and point
933 // it at this layer so we can process them
934 if (!mPendingStates.empty()) {
935 setTransactionFlags(eTransactionNeeded);
936 mFlinger->setTransactionFlags(eTraversalNeeded);
937 }
938
939 mCurrentState.modified = false;
940 return stateUpdateAvailable;
941}
942
Marissa Wall61c58622018-07-18 10:12:20 -0700943uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000944 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800945
Marissa Wall61c58622018-07-18 10:12:20 -0700946 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
947 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700948
David Sodmaneb085e02017-10-05 18:49:04 -0700949 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700950 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000951 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800952 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
953 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
954 " requested={ wh={%4u,%4u} }}\n"
955 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
956 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700957 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700958 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
959 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
960 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
961 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
962 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700963 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
964 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
965 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800966 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700967
Robert Carre392b552017-09-19 12:16:05 -0700968 // Don't let Layer::doTransaction update the drawing state
969 // if we have a pending resize, unless we are in fixed-size mode.
970 // the drawing state will be updated only once we receive a buffer
971 // with the correct size.
972 //
973 // In particular, we want to make sure the clip (which is part
974 // of the geometry state) is latched together with the size but is
975 // latched immediately when no resizing is involved.
976 //
977 // If a sideband stream is attached, however, we want to skip this
978 // optimization so that transactions aren't missed when a buffer
979 // never arrives
980 //
981 // In the case that we don't have a buffer we ignore other factors
982 // and avoid entering the resizePending state. At a high level the
983 // resizePending state is to avoid applying the state of the new buffer
984 // to the old buffer. However in the state where we don't have an old buffer
985 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700986 const bool resizePending =
987 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
988 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
David Sodman0cc69182017-11-17 12:12:07 -0800989 (getBE().compositionInfo.mBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700990 if (!isFixedSize()) {
David Sodman0cc69182017-11-17 12:12:07 -0800991 if (resizePending && getBE().compositionInfo.hwc.sidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700992 flags |= eDontUpdateGeometryState;
993 }
994 }
995
Robert Carr7bf247e2017-05-18 14:02:49 -0700996 // Here we apply various requested geometry states, depending on our
997 // latching configuration. See Layer.h for a detailed discussion of
998 // how geometry latching is controlled.
999 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +00001000 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001001
1002 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1003 // mode, which causes attributes which normally latch regardless of scaling mode,
1004 // to be delayed. We copy the requested state to the active state making sure
1005 // to respect these rules (again see Layer.h for a detailed discussion).
1006 //
1007 // There is an awkward asymmetry in the handling of the crop states in the position
1008 // states, as can be seen below. Largely this arises from position and transform
1009 // being stored in the same data structure while having different latching rules.
1010 // b/38182305
1011 //
Marissa Wall61c58622018-07-18 10:12:20 -07001012 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -07001013 // applyPendingStates in the presence of deferred transactions.
1014 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -07001015 float tx = stateToCommit->active_legacy.transform.tx();
1016 float ty = stateToCommit->active_legacy.transform.ty();
1017 stateToCommit->active_legacy = stateToCommit->requested_legacy;
1018 stateToCommit->active_legacy.transform.set(tx, ty);
1019 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001020 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001021 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -07001022 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001023 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001024 }
1025
Marissa Wall61c58622018-07-18 10:12:20 -07001026 return flags;
1027}
1028
1029uint32_t Layer::doTransaction(uint32_t flags) {
1030 ATRACE_CALL();
1031
chaviw5aedec92018-10-22 10:40:38 -07001032 if (mLayerDetached) {
1033 return 0;
1034 }
1035
Marissa Wall61c58622018-07-18 10:12:20 -07001036 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +00001037 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -07001038 if (!applyPendingStates(&c)) {
1039 return 0;
1040 }
1041
1042 flags = doTransactionResize(flags, &c);
1043
Alec Mourib416efd2018-09-06 21:01:59 +00001044 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -07001045
1046 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001047 // invalidate and recompute the visible regions if needed
1048 flags |= Layer::eVisibleRegion;
1049 }
1050
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001051 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001052 // invalidate and recompute the visible regions if needed
1053 flags |= eVisibleRegion;
1054 this->contentDirty = true;
1055
1056 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -07001057 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -07001058 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -08001059 }
1060
Dan Stozac8145172016-04-28 16:29:06 -07001061 // If the layer is hidden, signal and clear out all local sync points so
1062 // that transactions for layers depending on this layer's frames becoming
1063 // visible are not blocked
1064 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001065 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001066 }
1067
Robert Carr720e5062018-07-30 17:45:14 -07001068 if (mCurrentState.inputInfoChanged) {
1069 flags |= eInputInfoChanged;
1070 mCurrentState.inputInfoChanged = false;
1071 }
1072
Mathias Agopian13127d82013-03-05 17:47:11 -08001073 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001074 commitTransaction(c);
Marissa Walle2ffb422018-10-12 11:33:52 -07001075 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -08001076 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001077}
1078
Pablo Ceballos05289c22016-04-14 15:49:55 -07001079void Layer::commitTransaction(const State& stateToCommit) {
1080 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001081}
1082
Mathias Agopian13127d82013-03-05 17:47:11 -08001083uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Piquef1c675b2018-09-12 20:45:39 -07001084 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -08001085}
1086
1087uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Piquef1c675b2018-09-12 20:45:39 -07001088 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -08001089}
1090
Robert Carr82364e32016-05-15 11:27:47 -07001091bool Layer::setPosition(float x, float y, bool immediate) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001092 if (mCurrentState.requested_legacy.transform.tx() == x &&
1093 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001094 return false;
1095 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001096
1097 // We update the requested and active position simultaneously because
1098 // we want to apply the position portion of the transform matrix immediately,
1099 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Marissa Wallf58c14b2018-07-24 10:50:43 -07001100 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001101 if (immediate && !mFreezeGeometryUpdates) {
1102 // Here we directly update the active state
1103 // unlike other setters, because we store it within
1104 // the transform, but use different latching rules.
1105 // b/38182305
Marissa Wallf58c14b2018-07-24 10:50:43 -07001106 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001107 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001108 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001109
Dan Stoza7dde5992015-05-22 09:51:44 -07001110 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001111 setTransactionFlags(eTransactionNeeded);
1112 return true;
1113}
Robert Carr82364e32016-05-15 11:27:47 -07001114
Robert Carr1f0a16a2016-10-24 16:27:39 -07001115bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1116 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1117 if (idx < 0) {
1118 return false;
1119 }
1120 if (childLayer->setLayer(z)) {
1121 mCurrentChildren.removeAt(idx);
1122 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001123 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001124 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001125 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001126}
1127
Robert Carr503c7042017-09-27 15:06:08 -07001128bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1129 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1130 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1131 if (idx < 0) {
1132 return false;
1133 }
1134 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1135 mCurrentChildren.removeAt(idx);
1136 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001137 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001138 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001139 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001140}
1141
Robert Carrae060832016-11-28 10:51:00 -08001142bool Layer::setLayer(int32_t z) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001143 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001144 mCurrentState.sequence++;
1145 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001146 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001147
1148 // Discard all relative layering.
1149 if (mCurrentState.zOrderRelativeOf != nullptr) {
1150 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1151 if (strongRelative != nullptr) {
1152 strongRelative->removeZOrderRelative(this);
1153 }
1154 mCurrentState.zOrderRelativeOf = nullptr;
1155 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001156 setTransactionFlags(eTransactionNeeded);
1157 return true;
1158}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001159
Robert Carrdb66e622017-04-10 16:55:57 -07001160void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1161 mCurrentState.zOrderRelatives.remove(relative);
1162 mCurrentState.sequence++;
1163 mCurrentState.modified = true;
1164 setTransactionFlags(eTransactionNeeded);
1165}
1166
1167void Layer::addZOrderRelative(const wp<Layer>& relative) {
1168 mCurrentState.zOrderRelatives.add(relative);
1169 mCurrentState.modified = true;
1170 mCurrentState.sequence++;
1171 setTransactionFlags(eTransactionNeeded);
1172}
1173
Robert Carr503d2bd2017-12-04 15:49:47 -08001174bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001175 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1176 if (handle == nullptr) {
1177 return false;
1178 }
1179 sp<Layer> relative = handle->owner.promote();
1180 if (relative == nullptr) {
1181 return false;
1182 }
1183
Robert Carr503d2bd2017-12-04 15:49:47 -08001184 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1185 mCurrentState.zOrderRelativeOf == relative) {
1186 return false;
1187 }
1188
Robert Carrdb66e622017-04-10 16:55:57 -07001189 mCurrentState.sequence++;
1190 mCurrentState.modified = true;
Robert Carr503d2bd2017-12-04 15:49:47 -08001191 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001192
chaviw9ab4bd12017-11-03 13:11:00 -07001193 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
1194 if (oldZOrderRelativeOf != nullptr) {
1195 oldZOrderRelativeOf->removeZOrderRelative(this);
1196 }
Robert Carrdb66e622017-04-10 16:55:57 -07001197 mCurrentState.zOrderRelativeOf = relative;
1198 relative->addZOrderRelative(this);
1199
1200 setTransactionFlags(eTransactionNeeded);
1201
1202 return true;
1203}
1204
Mathias Agopian13127d82013-03-05 17:47:11 -08001205bool Layer::setSize(uint32_t w, uint32_t h) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001206 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
1207 return false;
1208 mCurrentState.requested_legacy.w = w;
1209 mCurrentState.requested_legacy.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001210 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001211 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001212
1213 // record the new size, from this point on, when the client request
1214 // a buffer, it'll get the new size.
1215 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001216 return true;
1217}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001218bool Layer::setAlpha(float alpha) {
David Sodman41fdfc92017-11-06 16:09:56 -08001219 if (mCurrentState.color.a == alpha) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001220 mCurrentState.sequence++;
chaviw13fdc492017-06-27 12:40:18 -07001221 mCurrentState.color.a = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001222 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001223 setTransactionFlags(eTransactionNeeded);
1224 return true;
1225}
chaviw13fdc492017-06-27 12:40:18 -07001226
1227bool Layer::setColor(const half3& color) {
David Sodman41fdfc92017-11-06 16:09:56 -08001228 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g &&
1229 color.b == mCurrentState.color.b)
chaviw13fdc492017-06-27 12:40:18 -07001230 return false;
1231
1232 mCurrentState.sequence++;
1233 mCurrentState.color.r = color.r;
1234 mCurrentState.color.g = color.g;
1235 mCurrentState.color.b = color.b;
1236 mCurrentState.modified = true;
1237 setTransactionFlags(eTransactionNeeded);
1238 return true;
1239}
1240
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001241bool Layer::setCornerRadius(float cornerRadius) {
1242 if (mCurrentState.cornerRadius == cornerRadius)
1243 return false;
1244
1245 mCurrentState.sequence++;
1246 mCurrentState.cornerRadius = cornerRadius;
1247 mCurrentState.modified = true;
1248 setTransactionFlags(eTransactionNeeded);
1249 return true;
1250}
1251
Robert Carrd4ae7f32018-06-07 16:10:57 -07001252bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1253 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001254 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001255 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1256
1257 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1258 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1259 return false;
1260 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001261 mCurrentState.sequence++;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001262 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1263 matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001264 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001265 setTransactionFlags(eTransactionNeeded);
1266 return true;
1267}
Marissa Wall61c58622018-07-18 10:12:20 -07001268
Mathias Agopian13127d82013-03-05 17:47:11 -08001269bool Layer::setTransparentRegionHint(const Region& transparent) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001270 mCurrentState.requestedTransparentRegion_legacy = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001271 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001272 setTransactionFlags(eTransactionNeeded);
1273 return true;
1274}
1275bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1276 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
David Sodman41fdfc92017-11-06 16:09:56 -08001277 if (mCurrentState.flags == newFlags) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001278 mCurrentState.sequence++;
1279 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001280 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001281 setTransactionFlags(eTransactionNeeded);
1282 return true;
1283}
Robert Carr99e27f02016-06-16 15:18:02 -07001284
Marissa Wallf58c14b2018-07-24 10:50:43 -07001285bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
1286 if (mCurrentState.requestedCrop_legacy == crop) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001287 mCurrentState.sequence++;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001288 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001289 if (immediate && !mFreezeGeometryUpdates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001290 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001291 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001292 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1293
Dan Stoza7dde5992015-05-22 09:51:44 -07001294 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001295 setTransactionFlags(eTransactionNeeded);
1296 return true;
1297}
Robert Carr8d5227b2017-03-16 15:41:03 -07001298
Robert Carrc3574f72016-03-24 12:19:32 -07001299bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001300 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001301 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001302 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001303 return true;
1304}
1305
rongliucfb187b2018-03-14 12:26:23 -07001306void Layer::setInfo(int32_t type, int32_t appId) {
David Sodman41fdfc92017-11-06 16:09:56 -08001307 mCurrentState.appId = appId;
1308 mCurrentState.type = type;
1309 mCurrentState.modified = true;
1310 setTransactionFlags(eTransactionNeeded);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001311}
1312
Mathias Agopian13127d82013-03-05 17:47:11 -08001313bool Layer::setLayerStack(uint32_t layerStack) {
David Sodman41fdfc92017-11-06 16:09:56 -08001314 if (mCurrentState.layerStack == layerStack) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001315 mCurrentState.sequence++;
1316 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001317 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001318 setTransactionFlags(eTransactionNeeded);
1319 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001320}
1321
Robert Carr1f0a16a2016-10-24 16:27:39 -07001322uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001323 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001324 if (p == nullptr) {
1325 return getDrawingState().layerStack;
1326 }
1327 return p->getLayerStack();
1328}
1329
Marissa Wallf58c14b2018-07-24 10:50:43 -07001330void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
1331 mCurrentState.barrierLayer_legacy = barrierLayer;
1332 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001333 // We don't set eTransactionNeeded, because just receiving a deferral
1334 // request without any other state updates shouldn't actually induce a delay
1335 mCurrentState.modified = true;
1336 pushPendingState();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001337 mCurrentState.barrierLayer_legacy = nullptr;
1338 mCurrentState.frameNumber_legacy = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001339 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001340}
1341
Marissa Wallf58c14b2018-07-24 10:50:43 -07001342void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001343 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001344 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001345}
1346
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001347// ----------------------------------------------------------------------------
1348// pageflip handling...
1349// ----------------------------------------------------------------------------
1350
Robert Carr1f0a16a2016-10-24 16:27:39 -07001351bool Layer::isHiddenByPolicy() const {
Alec Mourib416efd2018-09-06 21:01:59 +00001352 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001353 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001354 if (parent != nullptr && parent->isHiddenByPolicy()) {
1355 return true;
1356 }
1357 return s.flags & layer_state_t::eLayerHidden;
1358}
1359
David Sodman41fdfc92017-11-06 16:09:56 -08001360uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001361 // TODO: should we do something special if mSecure is set?
1362 if (mProtectedByApp) {
1363 // need a hardware-protected path to external video sink
1364 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001365 }
Riley Andrews03414a12014-07-01 14:22:59 -07001366 if (mPotentialCursor) {
1367 usage |= GraphicBuffer::USAGE_CURSOR;
1368 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001369 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001370 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001371}
1372
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001373void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001374 uint32_t orientation = 0;
Vishnu Nairf2deb822018-11-12 17:53:48 -08001375 // Disable setting transform hint if the debug flag is set or if the
1376 // getTransformToDisplayInverse flag is set and the client wants to submit buffers
1377 // in one orientation.
1378 if (!mFlinger->mDebugDisableTransformHint && !getTransformToDisplayInverse()) {
Mathias Agopian84300952012-11-21 16:02:13 -08001379 // The transform hint is used to improve performance, but we can
1380 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001381 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001382 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001383 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001384 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001385 orientation = 0;
1386 }
1387 }
David Sodmaneb085e02017-10-05 18:49:04 -07001388 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001389}
1390
Mathias Agopian13127d82013-03-05 17:47:11 -08001391// ----------------------------------------------------------------------------
1392// debugging
1393// ----------------------------------------------------------------------------
1394
Marissa Wall61c58622018-07-18 10:12:20 -07001395// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001396LayerDebugInfo Layer::getLayerDebugInfo() const {
1397 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001398 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001399 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001400 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001401 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
1402 info.mType = String8(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001403 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001404 info.mVisibleRegion = visibleRegion;
1405 info.mSurfaceDamageRegion = surfaceDamageRegion;
1406 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001407 info.mX = ds.active_legacy.transform.tx();
1408 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001409 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001410 info.mWidth = ds.active_legacy.w;
1411 info.mHeight = ds.active_legacy.h;
1412 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001413 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001414 info.mFlags = ds.flags;
1415 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001416 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001417 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1418 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1419 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1420 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001421 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001422 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001423 if (buffer != 0) {
1424 info.mActiveBufferWidth = buffer->getWidth();
1425 info.mActiveBufferHeight = buffer->getHeight();
1426 info.mActiveBufferStride = buffer->getStride();
1427 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001428 } else {
1429 info.mActiveBufferWidth = 0;
1430 info.mActiveBufferHeight = 0;
1431 info.mActiveBufferStride = 0;
1432 info.mActiveBufferFormat = 0;
1433 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001434 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001435 info.mNumQueuedFrames = getQueuedFrameCount();
1436 info.mRefreshPending = isBufferLatched();
1437 info.mIsOpaque = isOpaque(ds);
1438 info.mContentDirty = contentDirty;
1439 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001440}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001441
Dan Stozae22aec72016-08-01 13:20:59 -07001442void Layer::miniDumpHeader(String8& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001443 result.append("-------------------------------");
1444 result.append("-------------------------------");
1445 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001446 result.append(" Layer name\n");
1447 result.append(" Z | ");
1448 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001449 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001450 result.append(" Disp Frame (LTRB) | ");
1451 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001452 result.append("-------------------------------");
1453 result.append("-------------------------------");
1454 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001455}
1456
Dominik Laskowski075d3172018-05-24 15:50:06 -07001457void Layer::miniDump(String8& result, DisplayId displayId) const {
Dominik Laskowski34157762018-10-31 13:07:19 -07001458 if (!hasHwcLayer(displayId)) {
Dan Stozae22aec72016-08-01 13:20:59 -07001459 return;
1460 }
1461
1462 String8 name;
1463 if (mName.length() > 77) {
1464 std::string shortened;
1465 shortened.append(mName.string(), 36);
1466 shortened.append("[...]");
1467 shortened.append(mName.string() + (mName.length() - 36), 36);
1468 name = shortened.c_str();
1469 } else {
1470 name = mName;
1471 }
1472
1473 result.appendFormat(" %s\n", name.string());
1474
Alec Mourib416efd2018-09-06 21:01:59 +00001475 const State& layerState(getDrawingState());
Lloyd Pique074e8122018-07-26 12:57:23 -07001476 const LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers.at(displayId);
Chia-I Wu1e043612018-03-01 09:45:09 -08001477 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
1478 result.appendFormat(" rel %6d | ", layerState.z);
1479 } else {
1480 result.appendFormat(" %10d | ", layerState.z);
1481 }
Dominik Laskowski7e045462018-05-30 13:02:02 -07001482 result.appendFormat("%10s | ", to_string(getCompositionType(displayId)).c_str());
Lloyd Pique074e8122018-07-26 12:57:23 -07001483 result.appendFormat("%10s | ", to_string(hwcInfo.transform).c_str());
1484 const Rect& frame = hwcInfo.displayFrame;
David Sodman41fdfc92017-11-06 16:09:56 -08001485 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique074e8122018-07-26 12:57:23 -07001486 const FloatRect& crop = hwcInfo.sourceCrop;
David Sodman41fdfc92017-11-06 16:09:56 -08001487 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 -07001488
David Sodman7e4ae112018-02-09 15:02:28 -08001489 result.append("- - - - - - - - - - - - - - - -\n");
1490
1491 std::string compositionInfoStr;
1492 getBE().compositionInfo.dump(compositionInfoStr, "compositionInfo");
1493 result.append(compositionInfoStr.c_str());
1494
Yichi Chen6ca35192018-05-29 12:20:43 +08001495 result.append("- - - - - - - - - - - - - - - -");
1496 result.append("- - - - - - - - - - - - - - - -");
1497 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001498}
Dan Stozae22aec72016-08-01 13:20:59 -07001499
Svetoslavd85084b2014-03-20 10:28:31 -07001500void Layer::dumpFrameStats(String8& result) const {
1501 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001502}
1503
Svetoslavd85084b2014-03-20 10:28:31 -07001504void Layer::clearFrameStats() {
1505 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001506}
1507
Jamie Gennis6547ff42013-07-16 20:12:42 -07001508void Layer::logFrameStats() {
1509 mFrameTracker.logAndResetStats(mName);
1510}
1511
Svetoslavd85084b2014-03-20 10:28:31 -07001512void Layer::getFrameStats(FrameStats* outStats) const {
1513 mFrameTracker.getStats(outStats);
1514}
1515
Brian Andersond6927fb2016-07-23 23:37:30 -07001516void Layer::dumpFrameEvents(String8& result) {
David Sodman41fdfc92017-11-06 16:09:56 -08001517 result.appendFormat("- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001518 Mutex::Autolock lock(mFrameEventHistoryMutex);
1519 mFrameEventHistory.checkFencesForCompletion();
1520 mFrameEventHistory.dump(result);
1521}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001522
Brian Anderson5ea5e592016-12-01 16:54:33 -08001523void Layer::onDisconnect() {
1524 Mutex::Autolock lock(mFrameEventHistoryMutex);
1525 mFrameEventHistory.onDisconnect();
Yiwei Zhangaf8ee942018-11-22 00:15:23 -08001526 mFlinger->mTimeStats->onDestroy(getSequence());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001527}
1528
Brian Anderson3890c392016-07-25 12:48:08 -07001529void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001530 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001531 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001532 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1533 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001534 }
1535
Brian Andersond6927fb2016-07-23 23:37:30 -07001536 Mutex::Autolock lock(mFrameEventHistoryMutex);
1537 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001538 // If there are any unsignaled fences in the aquire timeline at this
1539 // point, the previously queued frame hasn't been latched yet. Go ahead
1540 // and try to get the signal time here so the syscall is taken out of
1541 // the main thread's critical path.
1542 mAcquireTimeline.updateSignalTimes();
1543 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001544 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001545 mFrameEventHistory.addQueue(*newTimestamps);
1546 }
1547
Brian Anderson3890c392016-07-25 12:48:08 -07001548 if (outDelta) {
1549 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001550 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001551}
Dan Stozae77c7662016-05-13 11:37:28 -07001552
Chia-I Wu98f1c102017-05-30 14:54:08 -07001553size_t Layer::getChildrenCount() const {
1554 size_t count = 0;
1555 for (const sp<Layer>& child : mCurrentChildren) {
1556 count += 1 + child->getChildrenCount();
1557 }
1558 return count;
1559}
1560
Robert Carr1f0a16a2016-10-24 16:27:39 -07001561void Layer::addChild(const sp<Layer>& layer) {
1562 mCurrentChildren.add(layer);
1563 layer->setParent(this);
1564}
1565
1566ssize_t Layer::removeChild(const sp<Layer>& layer) {
1567 layer->setParent(nullptr);
1568 return mCurrentChildren.remove(layer);
1569}
1570
Robert Carr1db73f62016-12-21 12:58:51 -08001571bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1572 sp<Handle> handle = nullptr;
1573 sp<Layer> newParent = nullptr;
1574 if (newParentHandle == nullptr) {
1575 return false;
1576 }
1577 handle = static_cast<Handle*>(newParentHandle.get());
1578 newParent = handle->owner.promote();
1579 if (newParent == nullptr) {
1580 ALOGE("Unable to promote Layer handle");
1581 return false;
1582 }
1583
chaviw5aedec92018-10-22 10:40:38 -07001584 if (attachChildren()) {
1585 setTransactionFlags(eTransactionNeeded);
1586 }
Robert Carr1db73f62016-12-21 12:58:51 -08001587 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001588 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001589
1590 sp<Client> client(child->mClientRef.promote());
1591 if (client != nullptr) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001592 client->updateParent(newParent);
Robert Carr1db73f62016-12-21 12:58:51 -08001593 }
1594 }
1595 mCurrentChildren.clear();
1596
1597 return true;
1598}
1599
Robert Carr15eae092018-03-23 13:43:53 -07001600void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001601 for (const sp<Layer>& child : mDrawingChildren) {
1602 child->mDrawingParent = newParent;
1603 }
1604}
1605
chaviwf1961f72017-09-18 16:41:07 -07001606bool Layer::reparent(const sp<IBinder>& newParentHandle) {
1607 if (newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001608 return false;
1609 }
1610
1611 auto handle = static_cast<Handle*>(newParentHandle.get());
1612 sp<Layer> newParent = handle->owner.promote();
1613 if (newParent == nullptr) {
1614 ALOGE("Unable to promote Layer handle");
1615 return false;
1616 }
1617
chaviwf1961f72017-09-18 16:41:07 -07001618 sp<Layer> parent = getParent();
1619 if (parent != nullptr) {
1620 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001621 }
chaviwf1961f72017-09-18 16:41:07 -07001622 newParent->addChild(this);
chaviw06178942017-07-27 10:25:59 -07001623
chaviw61626f22018-11-15 16:26:27 -08001624 if (!newParent->isRemovedFromCurrentState()) {
1625 addToCurrentState();
1626 }
1627
chaviwf1961f72017-09-18 16:41:07 -07001628 sp<Client> client(mClientRef.promote());
chaviw06178942017-07-27 10:25:59 -07001629 sp<Client> newParentClient(newParent->mClientRef.promote());
1630
chaviwf1961f72017-09-18 16:41:07 -07001631 if (client != newParentClient) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001632 client->updateParent(newParent);
chaviw06178942017-07-27 10:25:59 -07001633 }
1634
chaviw5aedec92018-10-22 10:40:38 -07001635 if (mLayerDetached) {
1636 mLayerDetached = false;
1637 setTransactionFlags(eTransactionNeeded);
1638 }
1639 if (attachChildren()) {
1640 setTransactionFlags(eTransactionNeeded);
1641 }
chaviw06178942017-07-27 10:25:59 -07001642 return true;
1643}
1644
Robert Carr9524cb32017-02-13 11:32:32 -08001645bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001646 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001647 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001648 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001649 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001650 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001651 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001652 }
Robert Carr7f619b22017-11-06 12:56:35 -08001653 }
Robert Carr9524cb32017-02-13 11:32:32 -08001654
1655 return true;
1656}
1657
chaviw5aedec92018-10-22 10:40:38 -07001658bool Layer::attachChildren() {
1659 bool changed = false;
1660 for (const sp<Layer>& child : mCurrentChildren) {
1661 sp<Client> parentClient = mClientRef.promote();
1662 sp<Client> client(child->mClientRef.promote());
1663 if (client != nullptr && parentClient != client) {
1664 if (child->mLayerDetached) {
1665 child->mLayerDetached = false;
1666 changed = true;
1667 }
1668 changed |= child->attachChildren();
1669 }
1670 }
1671
1672 return changed;
1673}
1674
Peiyong Lind3788632018-09-18 16:01:31 -07001675bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001676 static const mat4 identityMatrix = mat4();
1677
Peiyong Lind3788632018-09-18 16:01:31 -07001678 if (mCurrentState.colorTransform == matrix) {
1679 return false;
1680 }
1681 ++mCurrentState.sequence;
1682 mCurrentState.colorTransform = matrix;
Peiyong Lin747321c2018-10-01 10:03:11 -07001683 mCurrentState.hasColorTransform = matrix != identityMatrix;
1684 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001685 setTransactionFlags(eTransactionNeeded);
1686 return true;
1687}
1688
1689const mat4& Layer::getColorTransform() const {
1690 return getDrawingState().colorTransform;
1691}
1692
1693bool Layer::hasColorTransform() const {
Peiyong Lin747321c2018-10-01 10:03:11 -07001694 return getDrawingState().hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001695}
1696
Chia-I Wu11481472018-05-04 10:43:19 -07001697bool Layer::isLegacyDataSpace() const {
1698 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001699 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001700 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001701}
1702
Robert Carr1f0a16a2016-10-24 16:27:39 -07001703void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001704 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001705}
1706
1707void Layer::clearSyncPoints() {
1708 for (const auto& child : mCurrentChildren) {
1709 child->clearSyncPoints();
1710 }
1711
1712 Mutex::Autolock lock(mLocalSyncPointMutex);
1713 for (auto& point : mLocalSyncPoints) {
1714 point->setFrameAvailable();
1715 }
1716 mLocalSyncPoints.clear();
1717}
1718
1719int32_t Layer::getZ() const {
1720 return mDrawingState.z;
1721}
1722
Robert Carr29abff82017-12-04 13:51:20 -08001723bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1724 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1725 const State& state = useDrawing ? mDrawingState : mCurrentState;
1726 return state.zOrderRelativeOf != nullptr;
1727}
1728
David Sodman41fdfc92017-11-06 16:09:56 -08001729__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001730 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001731 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1732 "makeTraversalList received invalid stateSet");
1733 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1734 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1735 const State& state = useDrawing ? mDrawingState : mCurrentState;
1736
Robert Carr29abff82017-12-04 13:51:20 -08001737 if (state.zOrderRelatives.size() == 0) {
1738 *outSkipRelativeZUsers = true;
1739 return children;
1740 }
1741
chaviwfd462612018-05-31 16:11:27 -07001742 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001743 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001744 sp<Layer> strongRelative = weakRelative.promote();
1745 if (strongRelative != nullptr) {
1746 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001747 }
1748 }
1749
Dan Stoza412903f2017-04-27 13:42:17 -07001750 for (const sp<Layer>& child : children) {
Robert Carr503c7042017-09-27 15:06:08 -07001751 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1752 if (childState.zOrderRelativeOf != nullptr) {
1753 continue;
1754 }
Robert Carrdb66e622017-04-10 16:55:57 -07001755 traverse.add(child);
1756 }
1757
1758 return traverse;
1759}
1760
Robert Carr1f0a16a2016-10-24 16:27:39 -07001761/**
Robert Carrdb66e622017-04-10 16:55:57 -07001762 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001763 */
Dan Stoza412903f2017-04-27 13:42:17 -07001764void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001765 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1766 // produce a new list for traversing, including our relatives, and not including our children
1767 // who are relatives of another surface. In the case that there are no relative Z,
1768 // makeTraversalList returns our children directly to avoid significant overhead.
1769 // However in this case we need to take the responsibility for filtering children which
1770 // are relatives of another surface here.
1771 bool skipRelativeZUsers = false;
1772 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001773
Robert Carr1f0a16a2016-10-24 16:27:39 -07001774 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001775 for (; i < list.size(); i++) {
1776 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001777 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1778 continue;
1779 }
1780
Robert Carrdb66e622017-04-10 16:55:57 -07001781 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001782 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001783 }
Dan Stoza412903f2017-04-27 13:42:17 -07001784 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001785 }
Robert Carr29abff82017-12-04 13:51:20 -08001786
Dan Stoza412903f2017-04-27 13:42:17 -07001787 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001788 for (; i < list.size(); i++) {
1789 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001790
1791 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1792 continue;
1793 }
Dan Stoza412903f2017-04-27 13:42:17 -07001794 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001795 }
1796}
1797
1798/**
Robert Carrdb66e622017-04-10 16:55:57 -07001799 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001800 */
Dan Stoza412903f2017-04-27 13:42:17 -07001801void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1802 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001803 // See traverseInZOrder for documentation.
1804 bool skipRelativeZUsers = false;
1805 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001806
Robert Carr1f0a16a2016-10-24 16:27:39 -07001807 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001808 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001809 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001810
1811 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1812 continue;
1813 }
1814
Robert Carrdb66e622017-04-10 16:55:57 -07001815 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001816 break;
1817 }
Dan Stoza412903f2017-04-27 13:42:17 -07001818 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001819 }
Dan Stoza412903f2017-04-27 13:42:17 -07001820 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001821 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001822 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001823
1824 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1825 continue;
1826 }
1827
Dan Stoza412903f2017-04-27 13:42:17 -07001828 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001829 }
1830}
1831
chaviw4b129c22018-04-09 16:19:43 -07001832LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1833 const std::vector<Layer*>& layersInTree) {
1834 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1835 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001836 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1837 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
chaviw4b129c22018-04-09 16:19:43 -07001838 const State& state = useDrawing ? mDrawingState : mCurrentState;
1839
chaviwfd462612018-05-31 16:11:27 -07001840 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001841 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1842 sp<Layer> strongRelative = weakRelative.promote();
1843 // Only add relative layers that are also descendents of the top most parent of the tree.
1844 // If a relative layer is not a descendent, then it should be ignored.
1845 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1846 traverse.add(strongRelative);
1847 }
1848 }
1849
1850 for (const sp<Layer>& child : children) {
1851 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1852 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1853 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1854 // the child here since it won't be added later as a relative.
1855 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1856 childState.zOrderRelativeOf.promote().get())) {
1857 continue;
1858 }
1859 traverse.add(child);
1860 }
1861
1862 return traverse;
1863}
1864
1865void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1866 LayerVector::StateSet stateSet,
1867 const LayerVector::Visitor& visitor) {
1868 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001869
1870 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001871 for (; i < list.size(); i++) {
1872 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001873 if (relative->getZ() >= 0) {
1874 break;
1875 }
chaviw4b129c22018-04-09 16:19:43 -07001876 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001877 }
chaviw4b129c22018-04-09 16:19:43 -07001878
chaviwa76b2712017-09-20 12:02:26 -07001879 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001880 for (; i < list.size(); i++) {
1881 const auto& relative = list[i];
1882 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001883 }
1884}
1885
chaviw4b129c22018-04-09 16:19:43 -07001886std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1887 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1888 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1889
1890 std::vector<Layer*> layersInTree = {this};
1891 for (size_t i = 0; i < children.size(); i++) {
1892 const auto& child = children[i];
1893 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1894 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1895 }
1896
1897 return layersInTree;
1898}
1899
1900void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1901 const LayerVector::Visitor& visitor) {
1902 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1903 std::sort(layersInTree.begin(), layersInTree.end());
1904 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1905}
1906
Peiyong Linefefaac2018-08-17 12:27:51 -07001907ui::Transform Layer::getTransform() const {
1908 ui::Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001909 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001910 if (p != nullptr) {
1911 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07001912
1913 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
1914 // it isFixedSize) then there may be additional scaling not accounted
1915 // for in the transform. We need to mirror this scaling in child surfaces
1916 // or we will break the contract where WM can treat child surfaces as
1917 // pixels in the parent surface.
David Sodman0cc69182017-11-17 12:12:07 -08001918 if (p->isFixedSize() && p->getBE().compositionInfo.mBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07001919 int bufferWidth;
1920 int bufferHeight;
1921 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
David Sodman0cc69182017-11-17 12:12:07 -08001922 bufferWidth = p->getBE().compositionInfo.mBuffer->getWidth();
1923 bufferHeight = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001924 } else {
David Sodman0cc69182017-11-17 12:12:07 -08001925 bufferHeight = p->getBE().compositionInfo.mBuffer->getWidth();
1926 bufferWidth = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001927 }
Marissa Wall61c58622018-07-18 10:12:20 -07001928 float sx = p->getActiveWidth(p->getDrawingState()) / static_cast<float>(bufferWidth);
1929 float sy = p->getActiveHeight(p->getDrawingState()) / static_cast<float>(bufferHeight);
Peiyong Linefefaac2018-08-17 12:27:51 -07001930 ui::Transform extraParentScaling;
Robert Carr9b429f42017-04-17 14:56:57 -07001931 extraParentScaling.set(sx, 0, 0, sy);
1932 t = t * extraParentScaling;
1933 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001934 }
Marissa Wall61c58622018-07-18 10:12:20 -07001935 return t * getActiveTransform(getDrawingState());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001936}
1937
chaviw13fdc492017-06-27 12:40:18 -07001938half Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001939 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07001940
chaviw13fdc492017-06-27 12:40:18 -07001941 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1942 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001943}
Robert Carr6452f122017-03-21 10:41:29 -07001944
chaviw13fdc492017-06-27 12:40:18 -07001945half4 Layer::getColor() const {
1946 const half4 color(getDrawingState().color);
1947 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001948}
Robert Carr6452f122017-03-21 10:41:29 -07001949
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001950Layer::RoundedCornerState Layer::getRoundedCornerState() const {
1951 const auto& p = mDrawingParent.promote();
1952 if (p != nullptr) {
1953 RoundedCornerState parentState = p->getRoundedCornerState();
1954 if (parentState.radius > 0) {
1955 ui::Transform t = getActiveTransform(getDrawingState());
1956 t = t.inverse();
1957 parentState.cropRect = t.transform(parentState.cropRect);
1958 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1959 // but a transform matrix can define horizontal and vertical scales.
1960 // Let's take the average between both of them and pass into the shader, practically we
1961 // never do this type of transformation on windows anyway.
1962 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
1963 return parentState;
1964 }
1965 }
1966 const float radius = getDrawingState().cornerRadius;
1967 return radius > 0 ? RoundedCornerState(computeBounds(), radius) : RoundedCornerState();
1968}
1969
Robert Carr1f0a16a2016-10-24 16:27:39 -07001970void Layer::commitChildList() {
1971 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1972 const auto& child = mCurrentChildren[i];
1973 child->commitChildList();
1974 }
1975 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001976 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001977}
1978
Robert Carr720e5062018-07-30 17:45:14 -07001979void Layer::setInputInfo(const InputWindowInfo& info) {
1980 mCurrentState.inputInfo = info;
1981 mCurrentState.modified = true;
1982 mCurrentState.inputInfoChanged = true;
1983 setTransactionFlags(eTransactionNeeded);
1984}
1985
chaviw1d044282017-09-27 12:19:28 -07001986void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
1987 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1988 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1989 const State& state = useDrawing ? mDrawingState : mCurrentState;
1990
Peiyong Linefefaac2018-08-17 12:27:51 -07001991 ui::Transform requestedTransform = state.active_legacy.transform;
1992 ui::Transform transform = getTransform();
chaviw1d044282017-09-27 12:19:28 -07001993
1994 layerInfo->set_id(sequence);
1995 layerInfo->set_name(getName().c_str());
1996 layerInfo->set_type(String8(getTypeId()));
1997
1998 for (const auto& child : children) {
1999 layerInfo->add_children(child->sequence);
2000 }
2001
2002 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
2003 sp<Layer> strongRelative = weakRelative.promote();
2004 if (strongRelative != nullptr) {
2005 layerInfo->add_relatives(strongRelative->sequence);
2006 }
2007 }
2008
Marissa Wallf58c14b2018-07-24 10:50:43 -07002009 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
chaviw1d044282017-09-27 12:19:28 -07002010 layerInfo->mutable_transparent_region());
2011 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
2012 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
2013
2014 layerInfo->set_layer_stack(getLayerStack());
2015 layerInfo->set_z(state.z);
2016
2017 PositionProto* position = layerInfo->mutable_position();
2018 position->set_x(transform.tx());
2019 position->set_y(transform.ty());
2020
2021 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
2022 requestedPosition->set_x(requestedTransform.tx());
2023 requestedPosition->set_y(requestedTransform.ty());
2024
2025 SizeProto* size = layerInfo->mutable_size();
Marissa Wallf58c14b2018-07-24 10:50:43 -07002026 size->set_w(state.active_legacy.w);
2027 size->set_h(state.active_legacy.h);
chaviw1d044282017-09-27 12:19:28 -07002028
Marissa Wallf58c14b2018-07-24 10:50:43 -07002029 LayerProtoHelper::writeToProto(state.crop_legacy, layerInfo->mutable_crop());
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002030 layerInfo->set_corner_radius(getRoundedCornerState().radius);
chaviw1d044282017-09-27 12:19:28 -07002031
2032 layerInfo->set_is_opaque(isOpaque(state));
2033 layerInfo->set_invalidate(contentDirty);
Chia-I Wu01591c92018-05-22 12:03:00 -07002034
2035 // XXX (b/79210409) mCurrentDataSpace is not protected
2036 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
2037
chaviw1d044282017-09-27 12:19:28 -07002038 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
2039 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
2040 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
2041 layerInfo->set_flags(state.flags);
2042
2043 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
2044 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
2045
Jorim Jaggi8e0af362017-11-14 16:28:28 +01002046 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07002047 if (parent != nullptr) {
2048 layerInfo->set_parent(parent->sequence);
2049 }
2050
2051 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2052 if (zOrderRelativeOf != nullptr) {
2053 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
2054 }
2055
Chia-I Wu01591c92018-05-22 12:03:00 -07002056 // XXX getBE().compositionInfo.mBuffer is not protected
David Sodman0cc69182017-11-17 12:12:07 -08002057 auto buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08002058 if (buffer != nullptr) {
2059 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
Peiyong Linefefaac2018-08-17 12:27:51 -07002060 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
Yichi Chen6ca35192018-05-29 12:20:43 +08002061 layerInfo->mutable_buffer_transform());
chaviw1d044282017-09-27 12:19:28 -07002062 }
2063
2064 layerInfo->set_queued_frames(getQueuedFrameCount());
2065 layerInfo->set_refresh_pending(isBufferLatched());
rongliucfb187b2018-03-14 12:26:23 -07002066 layerInfo->set_window_type(state.type);
2067 layerInfo->set_app_id(state.appId);
chaviwadc40c22018-07-10 16:57:27 -07002068 layerInfo->set_curr_frame(mCurrentFrameNumber);
Vishnu Nairf2deb822018-11-12 17:53:48 -08002069 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
chaviwadc40c22018-07-10 16:57:27 -07002070
2071 for (const auto& pendingState : mPendingStates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07002072 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
chaviwadc40c22018-07-10 16:57:27 -07002073 if (barrierLayer != nullptr) {
2074 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
2075 barrierLayerProto->set_id(barrierLayer->sequence);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002076 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
chaviwadc40c22018-07-10 16:57:27 -07002077 }
2078 }
chaviw1d044282017-09-27 12:19:28 -07002079}
2080
Dominik Laskowski075d3172018-05-24 15:50:06 -07002081void Layer::writeToProto(LayerProto* layerInfo, DisplayId displayId) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07002082 if (!hasHwcLayer(displayId)) {
2083 return;
2084 }
2085
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002086 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
2087
Dominik Laskowski7e045462018-05-30 13:02:02 -07002088 const auto& hwcInfo = getBE().mHwcLayers.at(displayId);
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002089
2090 const Rect& frame = hwcInfo.displayFrame;
2091 LayerProtoHelper::writeToProto(frame, layerInfo->mutable_hwc_frame());
2092
2093 const FloatRect& crop = hwcInfo.sourceCrop;
2094 LayerProtoHelper::writeToProto(crop, layerInfo->mutable_hwc_crop());
2095
2096 const int32_t transform = static_cast<int32_t>(hwcInfo.transform);
2097 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002098
2099 const int32_t compositionType = static_cast<int32_t>(hwcInfo.compositionType);
2100 layerInfo->set_hwc_composition_type(compositionType);
2101
2102 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
2103 static_cast<BufferLayer*>(this)->isProtected()) {
2104 layerInfo->set_is_protected(true);
2105 } else {
2106 layerInfo->set_is_protected(false);
2107 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002108}
2109
Robert Carr2e102c92018-10-23 12:11:15 -07002110bool Layer::isRemovedFromCurrentState() const {
2111 return mRemovedFromCurrentState;
2112}
2113
Robert Carr720e5062018-07-30 17:45:14 -07002114InputWindowInfo Layer::fillInputInfo(const Rect& screenBounds) {
2115 InputWindowInfo info = mDrawingState.inputInfo;
Robert Carr5cb25782018-11-14 14:01:42 -08002116 info.frameLeft = screenBounds.left + info.surfaceInset;
2117 info.frameTop = screenBounds.top + info.surfaceInset;
2118 info.frameRight = screenBounds.right - info.surfaceInset;
2119 info.frameBottom = screenBounds.bottom - info.surfaceInset;
Robert Carr720e5062018-07-30 17:45:14 -07002120
Robert Carre07e1032018-11-26 12:55:53 -08002121 ui::Transform t = getTransform();
2122 info.windowXScale *= 1.0f / t.sx();
2123 info.windowYScale *= 1.0f / t.sy();
2124
2125 info.touchableRegion.scaleSelf(t.sx(), t.sy());
2126
Robert Carr5cb25782018-11-14 14:01:42 -08002127 info.touchableRegion = info.touchableRegion.translate(
Robert Carr720e5062018-07-30 17:45:14 -07002128 screenBounds.left,
2129 screenBounds.top);
2130 info.visible = isVisible();
2131 return info;
2132}
2133
2134bool Layer::hasInput() const {
Robert Carr5c8a0262018-10-03 16:30:44 -07002135 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002136}
2137
Mathias Agopian13127d82013-03-05 17:47:11 -08002138// ---------------------------------------------------------------------------
2139
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002140}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002141
2142#if defined(__gl_h_)
2143#error "don't include gl/gl.h in this file"
2144#endif
2145
2146#if defined(__gl2_h_)
2147#error "don't include gl2/gl2.h in this file"
2148#endif