blob: 2e564e7d944f2edcf78cd7e68cb3d9024ed4451e [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dan Stoza9e56aa02015-11-02 13:00:03 -080017//#define LOG_NDEBUG 0
18#undef LOG_TAG
19#define LOG_TAG "Layer"
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080020#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21
Mathias Agopian13127d82013-03-05 17:47:11 -080022#include <math.h>
David Sodman41fdfc92017-11-06 16:09:56 -080023#include <stdint.h>
24#include <stdlib.h>
25#include <sys/types.h>
chaviw4b129c22018-04-09 16:19:43 -070026#include <algorithm>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027
Mathias Agopiana67932f2011-04-20 14:20:59 -070028#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070029#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070030#include <cutils/properties.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080031
32#include <utils/Errors.h>
33#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080034#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080036#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037
Courtney Goeltzenleuchter36c44dc2017-04-14 09:33:16 -060038#include <ui/DebugUtils.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070039#include <ui/GraphicBuffer.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080040#include <ui/PixelFormat.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080041
Dan Stoza6b9454d2014-11-07 16:00:59 -080042#include <gui/BufferItem.h>
Kalle Raitaa099a242017-01-11 11:17:29 -080043#include <gui/LayerDebugInfo.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080044#include <gui/Surface.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045
Yiwei Zhang60d1a192018-03-07 14:52:28 -080046#include "BufferLayer.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020047#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070048#include "DisplayDevice.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include "Layer.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070050#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070051#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053
Mathias Agopian1b031492012-06-20 17:51:20 -070054#include "DisplayHardware/HWComposer.h"
55
Peiyong Lincbc184f2018-08-22 13:24:10 -070056#include <renderengine/RenderEngine.h>
Mathias Agopian875d8e12013-06-07 15:35:48 -070057
Dan Stozac5da2712016-07-20 15:38:12 -070058#include <mutex>
chaviw1d044282017-09-27 12:19:28 -070059#include "LayerProtoHelper.h"
Dan Stozac5da2712016-07-20 15:38:12 -070060
David Sodman41fdfc92017-11-06 16:09:56 -080061#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063namespace android {
64
Lloyd Piquef1c675b2018-09-12 20:45:39 -070065std::atomic<int32_t> Layer::sSequence{1};
Mathias Agopian13127d82013-03-05 17:47:11 -080066
Lloyd Pique42ab75e2018-09-12 20:46:03 -070067Layer::Layer(const LayerCreationArgs& args)
68 : mFlinger(args.flinger),
69 mName(args.name),
70 mClientRef(args.client),
71 mBE{this, args.name.string()} {
Mathias Agopiana67932f2011-04-20 14:20:59 -070072 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070073
74 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -070075 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
76 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
77 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070078
Dan Stozaf7ba41a2017-05-10 15:11:11 -070079 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070080
Lloyd Pique42ab75e2018-09-12 20:46:03 -070081 mCurrentState.active_legacy.w = args.w;
82 mCurrentState.active_legacy.h = args.h;
David Sodman0c69cad2017-08-21 12:12:51 -070083 mCurrentState.flags = layerFlags;
Marissa Wallf58c14b2018-07-24 10:50:43 -070084 mCurrentState.active_legacy.transform.set(0, 0);
85 mCurrentState.crop_legacy.makeInvalid();
Marissa Wallf58c14b2018-07-24 10:50:43 -070086 mCurrentState.requestedCrop_legacy = mCurrentState.crop_legacy;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070087 mCurrentState.z = 0;
chaviw13fdc492017-06-27 12:40:18 -070088 mCurrentState.color.a = 1.0f;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070089 mCurrentState.layerStack = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070090 mCurrentState.sequence = 0;
Marissa Wallf58c14b2018-07-24 10:50:43 -070091 mCurrentState.requested_legacy = mCurrentState.active_legacy;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -050092 mCurrentState.appId = 0;
93 mCurrentState.type = 0;
Marissa Wall61c58622018-07-18 10:12:20 -070094 mCurrentState.active.w = 0;
95 mCurrentState.active.h = 0;
96 mCurrentState.active.transform.set(0, 0);
97 mCurrentState.transform = 0;
98 mCurrentState.transformToDisplayInverse = false;
99 mCurrentState.crop.makeInvalid();
100 mCurrentState.acquireFence = new Fence(-1);
101 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
102 mCurrentState.hdrMetadata.validTypes = 0;
103 mCurrentState.surfaceDamageRegion.clear();
104 mCurrentState.api = -1;
Peiyong Lin747321c2018-10-01 10:03:11 -0700105 mCurrentState.hasColorTransform = false;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700106
107 // drawing state & current state are identical
108 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700109
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800110 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700111 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800112 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200113 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Dan Stoza436ccf32018-06-21 12:10:12 -0700114}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700115
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700116Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800117 sp<Client> c(mClientRef.promote());
118 if (c != 0) {
119 c->detachLayer(this);
120 }
121
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000122 for (auto& point : mRemoteSyncPoints) {
123 point->setTransactionApplied();
124 }
Rob Carr4bba3702018-10-08 21:53:30 +0000125 for (auto& point : mLocalSyncPoints) {
126 point->setFrameAvailable();
127 }
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000128 mFrameTracker.logAndResetStats(mName);
Mathias Agopian96f08192010-06-02 23:28:45 -0700129}
130
Mathias Agopian13127d82013-03-05 17:47:11 -0800131// ---------------------------------------------------------------------------
132// callbacks
133// ---------------------------------------------------------------------------
134
David Sodmaneb085e02017-10-05 18:49:04 -0700135/*
136 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
137 * Layer. So, the implementation is done in BufferLayer. When called on a
138 * ColorLayer object, it's essentially a NOP.
139 */
David Sodmaneb085e02017-10-05 18:49:04 -0700140void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800141
Chia-I Wuc6657022017-08-15 11:18:17 -0700142void Layer::onRemovedFromCurrentState() {
143 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000144
145 mPendingRemoval = true;
146
Robert Carr5edb1ad2017-04-25 10:54:24 -0700147 if (mCurrentState.zOrderRelativeOf != nullptr) {
148 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
149 if (strongRelative != nullptr) {
150 strongRelative->removeZOrderRelative(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700151 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700152 }
153 mCurrentState.zOrderRelativeOf = nullptr;
154 }
Rob Carr4bba3702018-10-08 21:53:30 +0000155
Chia-I Wuc6657022017-08-15 11:18:17 -0700156 for (const auto& child : mCurrentChildren) {
157 child->onRemovedFromCurrentState();
158 }
159}
Chia-I Wu38512252017-05-17 14:36:16 -0700160
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000161void Layer::onRemoved() {
162 // the layer is removed from SF mLayersPendingRemoval
163 abandon();
164
165 destroyAllHwcLayers();
166
167 for (const auto& child : mCurrentChildren) {
168 child->onRemoved();
169 }
170}
171
Mathias Agopian13127d82013-03-05 17:47:11 -0800172// ---------------------------------------------------------------------------
173// set-up
174// ---------------------------------------------------------------------------
175
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700176const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800177 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800178}
179
chaviw13fdc492017-06-27 12:40:18 -0700180bool Layer::getPremultipledAlpha() const {
181 return mPremultipliedAlpha;
182}
183
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700184sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800185 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700186 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800187}
188
189// ---------------------------------------------------------------------------
190// h/w composer set-up
191// ---------------------------------------------------------------------------
192
Dominik Laskowski7e045462018-05-30 13:02:02 -0700193bool Layer::createHwcLayer(HWComposer* hwc, int32_t displayId) {
194 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.count(displayId) != 0,
195 "Already have a layer for display %d", displayId);
David Sodmanb8aaea12017-12-14 15:54:51 -0800196 auto layer = std::shared_ptr<HWC2::Layer>(
197 hwc->createLayer(displayId),
198 [hwc, displayId](HWC2::Layer* layer) {
199 hwc->destroyLayer(displayId, layer); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700200 if (!layer) {
201 return false;
202 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700203 LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers[displayId];
Steven Thomasb02664d2017-07-26 18:48:28 -0700204 hwcInfo.hwc = hwc;
205 hwcInfo.layer = layer;
David Sodmanf6a38932018-05-25 15:27:50 -0700206 layer->setLayerDestroyedListener(
Dominik Laskowski7e045462018-05-30 13:02:02 -0700207 [this, displayId](HWC2::Layer* /*layer*/) { getBE().mHwcLayers.erase(displayId); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700208 return true;
209}
210
Dominik Laskowski7e045462018-05-30 13:02:02 -0700211bool Layer::destroyHwcLayer(int32_t displayId) {
212 if (getBE().mHwcLayers.count(displayId) == 0) {
Chia-I Wu83806892017-11-16 10:50:20 -0800213 return false;
Steven Thomasb02664d2017-07-26 18:48:28 -0700214 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700215 auto& hwcInfo = getBE().mHwcLayers[displayId];
David Sodman41fdfc92017-11-06 16:09:56 -0800216 LOG_ALWAYS_FATAL_IF(hwcInfo.layer == nullptr, "Attempt to destroy null layer");
Steven Thomasb02664d2017-07-26 18:48:28 -0700217 LOG_ALWAYS_FATAL_IF(hwcInfo.hwc == nullptr, "Missing HWComposer");
David Sodmanb8aaea12017-12-14 15:54:51 -0800218 hwcInfo.layer = nullptr;
219
Chia-I Wu83806892017-11-16 10:50:20 -0800220 return true;
Steven Thomasb02664d2017-07-26 18:48:28 -0700221}
222
223void Layer::destroyAllHwcLayers() {
David Sodman6f65f3e2017-11-03 14:28:09 -0700224 size_t numLayers = getBE().mHwcLayers.size();
Steven Thomasb02664d2017-07-26 18:48:28 -0700225 for (size_t i = 0; i < numLayers; ++i) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700226 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.empty(), "destroyAllHwcLayers failed");
227 destroyHwcLayer(getBE().mHwcLayers.begin()->first);
Steven Thomasb02664d2017-07-26 18:48:28 -0700228 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700229 LOG_ALWAYS_FATAL_IF(!getBE().mHwcLayers.empty(),
David Sodman41fdfc92017-11-06 16:09:56 -0800230 "All hardware composer layers should have been destroyed");
Steven Thomasb02664d2017-07-26 18:48:28 -0700231}
Steven Thomasb02664d2017-07-26 18:48:28 -0700232
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800233Rect Layer::getContentCrop() const {
234 // this is the crop rectangle that applies to the buffer
235 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700236 Rect crop;
237 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800238 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700239 crop = mCurrentCrop;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800240 } else if (getBE().compositionInfo.mBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800241 // otherwise we use the whole buffer
David Sodman0cc69182017-11-17 12:12:07 -0800242 crop = getBE().compositionInfo.mBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700243 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800244 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700245 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700246 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700247 return crop;
248}
249
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700250static Rect reduce(const Rect& win, const Region& exclude) {
251 if (CC_LIKELY(exclude.isEmpty())) {
252 return win;
253 }
254 if (exclude.isRect()) {
255 return win.reduce(exclude.getBounds());
256 }
257 return Region(win).subtract(exclude).getBounds();
258}
259
Dan Stoza80d61162017-12-20 15:57:52 -0800260static FloatRect reduce(const FloatRect& win, const Region& exclude) {
261 if (CC_LIKELY(exclude.isEmpty())) {
262 return win;
263 }
264 // Convert through Rect (by rounding) for lack of FloatRegion
265 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
266}
267
Robert Carr1f0a16a2016-10-24 16:27:39 -0700268Rect Layer::computeScreenBounds(bool reduceTransparentRegion) const {
Alec Mourib416efd2018-09-06 21:01:59 +0000269 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700270 Rect win(getActiveWidth(s), getActiveHeight(s));
Robert Carr1f0a16a2016-10-24 16:27:39 -0700271
Marissa Wall61c58622018-07-18 10:12:20 -0700272 Rect crop = getCrop(s);
273 if (!crop.isEmpty()) {
274 win.intersect(crop, &win);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700275 }
276
Peiyong Linefefaac2018-08-17 12:27:51 -0700277 ui::Transform t = getTransform();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700278 win = t.transform(win);
279
Chia-I Wue41dbe62017-06-13 14:10:56 -0700280 const sp<Layer>& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700281 // Now we need to calculate the parent bounds, so we can clip ourselves to those.
282 // When calculating the parent bounds for purposes of clipping,
283 // we don't need to constrain the parent to its transparent region.
284 // The transparent region is an optimization based on the
285 // buffer contents of the layer, but does not affect the space allocated to
286 // it by policy, and thus children should be allowed to extend into the
287 // parent's transparent region. In fact one of the main uses, is to reduce
288 // buffer allocation size in cases where a child window sits behind a main window
289 // (by marking the hole in the parent window as a transparent region)
290 if (p != nullptr) {
291 Rect bounds = p->computeScreenBounds(false);
292 bounds.intersect(win, &win);
293 }
294
295 if (reduceTransparentRegion) {
Marissa Wall61c58622018-07-18 10:12:20 -0700296 auto const screenTransparentRegion = t.transform(getActiveTransparentRegion(s));
Robert Carr1f0a16a2016-10-24 16:27:39 -0700297 win = reduce(win, screenTransparentRegion);
298 }
299
300 return win;
301}
302
Dan Stoza80d61162017-12-20 15:57:52 -0800303FloatRect Layer::computeBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000304 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700305 return computeBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700306}
307
Dan Stoza80d61162017-12-20 15:57:52 -0800308FloatRect Layer::computeBounds(const Region& activeTransparentRegion) const {
Alec Mourib416efd2018-09-06 21:01:59 +0000309 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700310 Rect win(getActiveWidth(s), getActiveHeight(s));
Robert Carrb5d3d262016-03-25 15:08:13 -0700311
Marissa Wall61c58622018-07-18 10:12:20 -0700312 Rect crop = getCrop(s);
313 if (!crop.isEmpty()) {
314 win.intersect(crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800315 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700316
Chia-I Wue41dbe62017-06-13 14:10:56 -0700317 const auto& p = mDrawingParent.promote();
Robert Carrd4ae7f32018-06-07 16:10:57 -0700318 FloatRect floatWin = win.toFloatRect();
319 FloatRect parentBounds = floatWin;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700320 if (p != nullptr) {
Robert Carrd4ae7f32018-06-07 16:10:57 -0700321 // We pass an empty Region here for reasons mirroring that of the case described in
322 // the computeScreenBounds reduceTransparentRegion=false case.
323 parentBounds = p->computeBounds(Region());
Robert Carr1f0a16a2016-10-24 16:27:39 -0700324 }
325
Peiyong Linefefaac2018-08-17 12:27:51 -0700326 ui::Transform t = s.active_legacy.transform;
Dan Stoza80d61162017-12-20 15:57:52 -0800327
Vishnu Nairdcce0e22018-08-23 08:35:19 -0700328 if (p != nullptr) {
Dan Stoza80d61162017-12-20 15:57:52 -0800329 floatWin = t.transform(floatWin);
Robert Carrd4ae7f32018-06-07 16:10:57 -0700330 floatWin = floatWin.intersect(parentBounds);
Dan Stoza80d61162017-12-20 15:57:52 -0800331 floatWin = t.inverse().transform(floatWin);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700332 }
333
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700334 // subtract the transparent region and snap to the bounds
Dan Stoza80d61162017-12-20 15:57:52 -0800335 return reduce(floatWin, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800336}
337
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700338Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& display) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700339 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800340 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700341 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800342
343 // apply the projection's clipping to the window crop in
344 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700345 // if there are no window scaling involved, this operation will map to full
346 // pixels in the buffer.
347 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
348 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700349
Marissa Wall61c58622018-07-18 10:12:20 -0700350 Rect activeCrop(getActiveWidth(s), getActiveHeight(s));
351 Rect crop = getCrop(s);
352 if (!crop.isEmpty()) {
353 activeCrop.intersect(crop, &activeCrop);
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700354 }
355
Peiyong Linefefaac2018-08-17 12:27:51 -0700356 ui::Transform t = getTransform();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700357 activeCrop = t.transform(activeCrop);
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700358 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000359 activeCrop.clear();
360 }
chaviwb1154d12017-10-31 14:15:36 -0700361
362 const auto& p = mDrawingParent.promote();
363 if (p != nullptr) {
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700364 auto parentCrop = p->computeInitialCrop(display);
chaviwb1154d12017-10-31 14:15:36 -0700365 activeCrop.intersect(parentCrop, &activeCrop);
366 }
367
Robert Carr1f0a16a2016-10-24 16:27:39 -0700368 return activeCrop;
369}
370
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700371FloatRect Layer::computeCrop(const sp<const DisplayDevice>& display) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700372 // the content crop is the area of the content that gets scaled to the
373 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800374 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700375
376 // In addition there is a WM-specified crop we pull from our drawing state.
377 const State& s(getDrawingState());
378
379 // Screen space to make reduction to parent crop clearer.
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700380 Rect activeCrop = computeInitialCrop(display);
Peiyong Linefefaac2018-08-17 12:27:51 -0700381 ui::Transform t = getTransform();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700382 // Back to layer space to work with the content crop.
383 activeCrop = t.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800384
Michael Lentine28ea2172014-11-19 18:32:37 -0800385 // This needs to be here as transform.transform(Rect) computes the
386 // transformed rect and then takes the bounding box of the result before
387 // returning. This means
388 // transform.inverse().transform(transform.transform(Rect)) != Rect
389 // in which case we need to make sure the final rect is clipped to the
390 // display bounds.
Marissa Wall61c58622018-07-18 10:12:20 -0700391 if (!activeCrop.intersect(Rect(getActiveWidth(s), getActiveHeight(s)), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000392 activeCrop.clear();
393 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800394
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700395 // subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700396 activeCrop = reduce(activeCrop, getActiveTransparentRegion(s));
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700397
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000398 // Transform the window crop to match the buffer coordinate system,
399 // which means using the inverse of the current transform set on the
400 // SurfaceFlingerConsumer.
401 uint32_t invTransform = mCurrentTransform;
Robert Carrcae605c2017-03-29 12:10:31 -0700402 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000403 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700404 * the code below applies the primary display's inverse transform to the
405 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000406 */
David Sodman41fdfc92017-11-06 16:09:56 -0800407 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000408 // calculate the inverse transform
409 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800410 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800411 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000412 // and apply to the current transform
Peiyong Linefefaac2018-08-17 12:27:51 -0700413 invTransform = (ui::Transform(invTransformOrient) *
414 ui::Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800415 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000416
Marissa Wall61c58622018-07-18 10:12:20 -0700417 int winWidth = getActiveWidth(s);
418 int winHeight = getActiveHeight(s);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000419 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
420 // If the activeCrop has been rotate the ends are rotated but not
421 // the space itself so when transforming ends back we can't rely on
422 // a modification of the axes of rotation. To account for this we
423 // need to reorient the inverse rotation in terms of the current
424 // axes of rotation.
425 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
426 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
427 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800428 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000429 }
Marissa Wall61c58622018-07-18 10:12:20 -0700430 winWidth = getActiveHeight(s);
431 winHeight = getActiveWidth(s);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000432 }
Marissa Wall61c58622018-07-18 10:12:20 -0700433 const Rect winCrop = activeCrop.transform(invTransform, getActiveWidth(s), getActiveHeight(s));
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000434
435 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800436 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000437 float yScale = crop.getHeight() / float(winHeight);
438
David Sodman41fdfc92017-11-06 16:09:56 -0800439 float insetL = winCrop.left * xScale;
440 float insetT = winCrop.top * yScale;
441 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000442 float insetB = (winHeight - winCrop.bottom) * yScale;
443
David Sodman41fdfc92017-11-06 16:09:56 -0800444 crop.left += insetL;
445 crop.top += insetT;
446 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000447 crop.bottom -= insetB;
448
Mathias Agopian13127d82013-03-05 17:47:11 -0800449 return crop;
450}
451
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700452void Layer::setGeometry(const sp<const DisplayDevice>& display, uint32_t z) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700453 const auto displayId = display->getId();
Peiyong Lin91b1df22018-06-18 18:00:16 -0700454 if (!hasHwcLayer(displayId)) {
455 ALOGE("[%s] failed to setGeometry: no HWC layer found (%d)",
456 mName.string(), displayId);
457 return;
458 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700459 auto& hwcInfo = getBE().mHwcLayers[displayId];
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700460
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700461 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800462 hwcInfo.forceClientComposition = false;
463
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700464 if (isSecure() && !display->isSecure()) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800465 hwcInfo.forceClientComposition = true;
466 }
467
David Sodman15094112018-10-11 09:39:37 -0700468 auto& hwcLayer = hwcInfo.layer;
469
Mathias Agopian13127d82013-03-05 17:47:11 -0800470 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700471 const State& s(getDrawingState());
David Revemanecf0fa52017-03-03 11:32:44 -0500472 auto blendMode = HWC2::BlendMode::None;
Robert Carr6452f122017-03-21 10:41:29 -0700473 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800474 blendMode =
475 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800476 }
David Sodman15094112018-10-11 09:39:37 -0700477 auto error = hwcLayer->setBlendMode(blendMode);
478 ALOGE_IF(error != HWC2::Error::None,
479 "[%s] Failed to set blend mode %s:"
480 " %s (%d)",
481 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
482 static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700483 getBE().compositionInfo.hwc.blendMode = blendMode;
Mathias Agopian13127d82013-03-05 17:47:11 -0800484
485 // apply the layer's transform, followed by the display's global transform
486 // here we're guaranteed that the layer's transform preserves rects
Marissa Wall61c58622018-07-18 10:12:20 -0700487 Region activeTransparentRegion(getActiveTransparentRegion(s));
Peiyong Linefefaac2018-08-17 12:27:51 -0700488 ui::Transform t = getTransform();
Marissa Wall61c58622018-07-18 10:12:20 -0700489 Rect activeCrop = getCrop(s);
490 if (!activeCrop.isEmpty()) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700491 activeCrop = t.transform(activeCrop);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700492 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000493 activeCrop.clear();
494 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700495 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800496 // This needs to be here as transform.transform(Rect) computes the
497 // transformed rect and then takes the bounding box of the result before
498 // returning. This means
499 // transform.inverse().transform(transform.transform(Rect)) != Rect
500 // in which case we need to make sure the final rect is clipped to the
501 // display bounds.
Marissa Wall61c58622018-07-18 10:12:20 -0700502 if (!activeCrop.intersect(Rect(getActiveWidth(s), getActiveHeight(s)), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000503 activeCrop.clear();
504 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700505 // mark regions outside the crop as transparent
Marissa Wall61c58622018-07-18 10:12:20 -0700506 activeTransparentRegion.orSelf(Rect(0, 0, getActiveWidth(s), activeCrop.top));
Marissa Wallf58c14b2018-07-24 10:50:43 -0700507 activeTransparentRegion.orSelf(
Marissa Wall61c58622018-07-18 10:12:20 -0700508 Rect(0, activeCrop.bottom, getActiveWidth(s), getActiveHeight(s)));
David Sodman41fdfc92017-11-06 16:09:56 -0800509 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
510 activeTransparentRegion.orSelf(
Marissa Wall61c58622018-07-18 10:12:20 -0700511 Rect(activeCrop.right, activeCrop.top, getActiveWidth(s), activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700512 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700513
Dan Stoza80d61162017-12-20 15:57:52 -0800514 // computeBounds returns a FloatRect to provide more accuracy during the
515 // transformation. We then round upon constructing 'frame'.
516 Rect frame{t.transform(computeBounds(activeTransparentRegion))};
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700517 if (!frame.intersect(display->getViewport(), &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000518 frame.clear();
519 }
Peiyong Linefefaac2018-08-17 12:27:51 -0700520 const ui::Transform& tr = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800521 Rect transformedFrame = tr.transform(frame);
David Sodman15094112018-10-11 09:39:37 -0700522 error = hwcLayer->setDisplayFrame(transformedFrame);
523 if (error != HWC2::Error::None) {
524 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
525 transformedFrame.left, transformedFrame.top, transformedFrame.right,
526 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
527 } else {
528 hwcInfo.displayFrame = transformedFrame;
529 }
David Sodmanba340492018-08-05 21:51:33 -0700530 getBE().compositionInfo.hwc.displayFrame = transformedFrame;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800531
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700532 FloatRect sourceCrop = computeCrop(display);
David Sodman15094112018-10-11 09:39:37 -0700533 error = hwcLayer->setSourceCrop(sourceCrop);
534 if (error != HWC2::Error::None) {
535 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
536 "%s (%d)",
537 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
538 to_string(error).c_str(), static_cast<int32_t>(error));
539 } else {
540 hwcInfo.sourceCrop = sourceCrop;
541 }
David Sodmanba340492018-08-05 21:51:33 -0700542 getBE().compositionInfo.hwc.sourceCrop = sourceCrop;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800543
chaviw13fdc492017-06-27 12:40:18 -0700544 float alpha = static_cast<float>(getAlpha());
David Sodman15094112018-10-11 09:39:37 -0700545 error = hwcLayer->setPlaneAlpha(alpha);
546 ALOGE_IF(error != HWC2::Error::None,
547 "[%s] Failed to set plane alpha %.3f: "
548 "%s (%d)",
549 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700550 getBE().compositionInfo.hwc.alpha = alpha;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800551
David Sodman15094112018-10-11 09:39:37 -0700552 error = hwcLayer->setZOrder(z);
553 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
554 to_string(error).c_str(), static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700555 getBE().compositionInfo.hwc.z = z;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500556
Albert Chaulk2a589632017-05-04 16:59:44 -0400557 int type = s.type;
558 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700559 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400560 if (parent.get()) {
561 auto& parentState = parent->getDrawingState();
rongliucfb187b2018-03-14 12:26:23 -0700562 if (parentState.type >= 0 || parentState.appId >= 0) {
563 type = parentState.type;
564 appId = parentState.appId;
565 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400566 }
567
David Sodman15094112018-10-11 09:39:37 -0700568 error = hwcLayer->setInfo(type, appId);
569 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
570 static_cast<int32_t>(error));
571
David Sodmanba340492018-08-05 21:51:33 -0700572 getBE().compositionInfo.hwc.type = type;
573 getBE().compositionInfo.hwc.appId = appId;
574
Mathias Agopian29a367b2011-07-12 14:51:45 -0700575 /*
576 * Transformations are applied in this order:
577 * 1) buffer orientation/flip/mirror
578 * 2) state transformation (window manager)
579 * 3) layer orientation (screen orientation)
580 * (NOTE: the matrices are multiplied in reverse order)
581 */
582
Peiyong Linefefaac2018-08-17 12:27:51 -0700583 const ui::Transform bufferOrientation(mCurrentTransform);
584 ui::Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700585
Robert Carrcae605c2017-03-29 12:10:31 -0700586 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700587 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700588 * the code below applies the primary display's inverse transform to the
589 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700590 */
David Sodman41fdfc92017-11-06 16:09:56 -0800591 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700592 // calculate the inverse transform
593 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800594 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700595 }
Robert Carrcae605c2017-03-29 12:10:31 -0700596
597 /*
598 * Here we cancel out the orientation component of the WM transform.
599 * The scaling and translate components are already included in our bounds
600 * computation so it's enough to just omit it in the composition.
601 * See comment in onDraw with ref to b/36727915 for why.
602 */
Peiyong Linefefaac2018-08-17 12:27:51 -0700603 transform = ui::Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700604 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700605
606 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800607 const uint32_t orientation = transform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -0700608 if (orientation & ui::Transform::ROT_INVALID) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800609 // we can only handle simple transformation
Lloyd Pique074e8122018-07-26 12:57:23 -0700610 hwcInfo.forceClientComposition = true;
David Sodman15094112018-10-11 09:39:37 -0700611 getBE().mHwcLayers[displayId].compositionType = HWC2::Composition::Client;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800612 } else {
613 auto transform = static_cast<HWC2::Transform>(orientation);
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800614 hwcInfo.transform = transform;
David Sodman15094112018-10-11 09:39:37 -0700615 auto error = hwcLayer->setTransform(transform);
616 ALOGE_IF(error != HWC2::Error::None,
617 "[%s] Failed to set transform %s: "
618 "%s (%d)",
619 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
620 static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700621 getBE().compositionInfo.hwc.transform = transform;
David Sodman4b7c4bc2017-11-17 12:13:59 -0800622 }
623}
624
Dominik Laskowski7e045462018-05-30 13:02:02 -0700625void Layer::forceClientComposition(int32_t displayId) {
626 if (getBE().mHwcLayers.count(displayId) == 0) {
627 ALOGE("forceClientComposition: no HWC layer found (%d)", displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800628 return;
629 }
630
Dominik Laskowski7e045462018-05-30 13:02:02 -0700631 getBE().mHwcLayers[displayId].forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800632}
Dan Stozaee44edd2015-03-23 15:50:23 -0700633
Dominik Laskowski7e045462018-05-30 13:02:02 -0700634bool Layer::getForceClientComposition(int32_t displayId) {
635 if (getBE().mHwcLayers.count(displayId) == 0) {
636 ALOGE("getForceClientComposition: no HWC layer found (%d)", displayId);
chaviwc9232ed2017-11-14 15:31:15 -0800637 return false;
638 }
639
Dominik Laskowski7e045462018-05-30 13:02:02 -0700640 return getBE().mHwcLayers[displayId].forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800641}
642
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700643void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700644 const auto displayId = display->getId();
645 if (getBE().mHwcLayers.count(displayId) == 0 ||
646 getCompositionType(displayId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800647 return;
648 }
649
650 // This gives us only the "orientation" component of the transform
651 const State& s(getCurrentState());
652
653 // Apply the layer's transform, followed by the display's global transform
654 // Here we're guaranteed that the layer's transform preserves rects
Marissa Wall61c58622018-07-18 10:12:20 -0700655 Rect win(getActiveWidth(s), getActiveHeight(s));
656 Rect crop = getCrop(s);
657 if (!crop.isEmpty()) {
658 win.intersect(crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800659 }
660 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700661 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Robert Carr1f0a16a2016-10-24 16:27:39 -0700662 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700663 frame.intersect(display->getViewport(), &frame);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700664 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800665 auto position = displayTransform.transform(frame);
666
Dominik Laskowski7e045462018-05-30 13:02:02 -0700667 auto error =
David Sodmanb8aaea12017-12-14 15:54:51 -0800668 (getBE().mHwcLayers[displayId].layer)->setCursorPosition(
669 position.left, position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800670 ALOGE_IF(error != HWC2::Error::None,
671 "[%s] Failed to set cursor position "
672 "to (%d, %d): %s (%d)",
673 mName.string(), position.left, position.top, to_string(error).c_str(),
674 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800675}
Riley Andrews03414a12014-07-01 14:22:59 -0700676
Mathias Agopian13127d82013-03-05 17:47:11 -0800677// ---------------------------------------------------------------------------
678// drawing...
679// ---------------------------------------------------------------------------
680
Marissa Wall61c58622018-07-18 10:12:20 -0700681void Layer::draw(const RenderArea& renderArea, const Region& clip) {
chaviwa76b2712017-09-20 12:02:26 -0700682 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800683}
684
Marissa Wall61c58622018-07-18 10:12:20 -0700685void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) {
chaviwa76b2712017-09-20 12:02:26 -0700686 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800687}
688
David Sodman41fdfc92017-11-06 16:09:56 -0800689void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
690 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800691 auto& engine(mFlinger->getRenderEngine());
David Sodman9eeae692017-11-02 10:53:32 -0700692 computeGeometry(renderArea, getBE().mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700693 engine.setupFillWithColor(red, green, blue, alpha);
David Sodman9eeae692017-11-02 10:53:32 -0700694 engine.drawMesh(getBE().mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800695}
696
David Sodmanc1498e62018-09-12 14:36:26 -0700697void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
698 clearWithOpenGL(renderArea, 0, 0, 0, 0);
699}
700
David Sodman15094112018-10-11 09:39:37 -0700701void Layer::setCompositionType(int32_t displayId, HWC2::Composition type, bool callIntoHwc) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700702 if (getBE().mHwcLayers.count(displayId) == 0) {
Chia-I Wu30505fb2018-03-26 16:20:31 -0700703 ALOGE("setCompositionType called without a valid HWC layer");
704 return;
David Sodman15094112018-10-11 09:39:37 -0700705 }
706 auto& hwcInfo = getBE().mHwcLayers[displayId];
707 auto& hwcLayer = hwcInfo.layer;
708 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", (hwcLayer)->getId(), to_string(type).c_str(),
709 static_cast<int>(callIntoHwc));
710 if (hwcInfo.compositionType != type) {
711 ALOGV(" actually setting");
712 hwcInfo.compositionType = type;
713 if (callIntoHwc) {
714 auto error = (hwcLayer)->setCompositionType(type);
715 ALOGE_IF(error != HWC2::Error::None,
716 "[%s] Failed to set "
717 "composition type %s: %s (%d)",
718 mName.string(), to_string(type).c_str(), to_string(error).c_str(),
719 static_cast<int32_t>(error));
Chia-I Wu30505fb2018-03-26 16:20:31 -0700720 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800721 }
722}
723
Dominik Laskowski7e045462018-05-30 13:02:02 -0700724HWC2::Composition Layer::getCompositionType(int32_t displayId) const {
David Sodman15fb96e2018-01-07 10:23:24 -0800725 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700726 // If we're querying the composition type for a display that does not
727 // have a HWC counterpart, then it will always be Client
728 return HWC2::Composition::Client;
729 }
David Sodman15fb96e2018-01-07 10:23:24 -0800730 return getBE().mHwcLayers[displayId].compositionType;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800731}
732
Dominik Laskowski7e045462018-05-30 13:02:02 -0700733void Layer::setClearClientTarget(int32_t displayId, bool clear) {
734 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800735 ALOGE("setClearClientTarget called without a valid HWC layer");
736 return;
737 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700738 getBE().mHwcLayers[displayId].clearClientTarget = clear;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800739}
740
Dominik Laskowski7e045462018-05-30 13:02:02 -0700741bool Layer::getClearClientTarget(int32_t displayId) const {
742 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800743 ALOGE("getClearClientTarget called without a valid HWC layer");
744 return false;
745 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700746 return getBE().mHwcLayers.at(displayId).clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800747}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800748
Dan Stozacac35382016-01-27 12:21:06 -0800749bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
750 if (point->getFrameNumber() <= mCurrentFrameNumber) {
751 // Don't bother with a SyncPoint, since we've already latched the
752 // relevant frame
753 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700754 }
755
Dan Stozacac35382016-01-27 12:21:06 -0800756 Mutex::Autolock lock(mLocalSyncPointMutex);
757 mLocalSyncPoints.push_back(point);
758 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700759}
760
Mathias Agopian13127d82013-03-05 17:47:11 -0800761// ----------------------------------------------------------------------------
762// local state
763// ----------------------------------------------------------------------------
764
Peiyong Lin833074a2018-08-28 11:53:54 -0700765void Layer::computeGeometry(const RenderArea& renderArea,
766 renderengine::Mesh& mesh,
chaviwa76b2712017-09-20 12:02:26 -0700767 bool useIdentityTransform) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700768 const ui::Transform renderAreaTransform(renderArea.getTransform());
Dan Stoza80d61162017-12-20 15:57:52 -0800769 FloatRect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700770
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000771 vec2 lt = vec2(win.left, win.top);
772 vec2 lb = vec2(win.left, win.bottom);
773 vec2 rb = vec2(win.right, win.bottom);
774 vec2 rt = vec2(win.right, win.top);
775
Peiyong Linefefaac2018-08-17 12:27:51 -0700776 ui::Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000777 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700778 lt = layerTransform.transform(lt);
779 lb = layerTransform.transform(lb);
780 rb = layerTransform.transform(rb);
781 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000782 }
783
Peiyong Lin833074a2018-08-28 11:53:54 -0700784 renderengine::Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700785 position[0] = renderAreaTransform.transform(lt);
786 position[1] = renderAreaTransform.transform(lb);
787 position[2] = renderAreaTransform.transform(rb);
788 position[3] = renderAreaTransform.transform(rt);
Mathias Agopian13127d82013-03-05 17:47:11 -0800789}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800790
David Sodman41fdfc92017-11-06 16:09:56 -0800791bool Layer::isSecure() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000792 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700793 return (s.flags & layer_state_t::eLayerSecure);
794}
795
Mathias Agopian13127d82013-03-05 17:47:11 -0800796void Layer::setVisibleRegion(const Region& visibleRegion) {
797 // always called from main thread
798 this->visibleRegion = visibleRegion;
799}
800
801void Layer::setCoveredRegion(const Region& coveredRegion) {
802 // always called from main thread
803 this->coveredRegion = coveredRegion;
804}
805
David Sodman41fdfc92017-11-06 16:09:56 -0800806void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800807 // always called from main thread
808 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
809}
810
Robert Carre5f4f692018-01-12 13:12:28 -0800811void Layer::clearVisibilityRegions() {
812 visibleRegion.clear();
813 visibleNonTransparentRegion.clear();
814 coveredRegion.clear();
815}
816
Mathias Agopian13127d82013-03-05 17:47:11 -0800817// ----------------------------------------------------------------------------
818// transaction
819// ----------------------------------------------------------------------------
820
Dan Stoza7dde5992015-05-22 09:51:44 -0700821void Layer::pushPendingState() {
822 if (!mCurrentState.modified) {
823 return;
824 }
825
Dan Stoza7dde5992015-05-22 09:51:44 -0700826 // If this transaction is waiting on the receipt of a frame, generate a sync
827 // point and send it to the remote layer.
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000828 if (mCurrentState.barrierLayer_legacy != nullptr) {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700829 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800830 if (barrierLayer == nullptr) {
831 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700832 // If we can't promote the layer we are intended to wait on,
833 // then it is expired or otherwise invalid. Allow this transaction
834 // to be applied as per normal (no synchronization).
Marissa Wallf58c14b2018-07-24 10:50:43 -0700835 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800836 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700837 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy);
Robert Carr0d480722017-01-10 16:42:54 -0800838 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800839 mRemoteSyncPoints.push_back(std::move(syncPoint));
840 } else {
841 // We already missed the frame we're supposed to synchronize
842 // on, so go ahead and apply the state update
Marissa Wallf58c14b2018-07-24 10:50:43 -0700843 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800844 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700845 }
846
Dan Stoza7dde5992015-05-22 09:51:44 -0700847 // Wake us up to check if the frame has been received
848 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700849 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700850 }
851 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700852 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700853}
854
Pablo Ceballos05289c22016-04-14 15:49:55 -0700855void Layer::popPendingState(State* stateToCommit) {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700856 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700857
858 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700859 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700860}
861
Pablo Ceballos05289c22016-04-14 15:49:55 -0700862bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700863 bool stateUpdateAvailable = false;
864 while (!mPendingStates.empty()) {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700865 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700866 if (mRemoteSyncPoints.empty()) {
867 // If we don't have a sync point for this, apply it anyway. It
868 // will be visually wrong, but it should keep us from getting
869 // into too much trouble.
870 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700871 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700872 stateUpdateAvailable = true;
873 continue;
874 }
875
Marissa Wallf58c14b2018-07-24 10:50:43 -0700876 if (mRemoteSyncPoints.front()->getFrameNumber() !=
877 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800878 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800879
880 // Signal our end of the sync point and then dispose of it
881 mRemoteSyncPoints.front()->setTransactionApplied();
882 mRemoteSyncPoints.pop_front();
883 continue;
884 }
885
Dan Stoza7dde5992015-05-22 09:51:44 -0700886 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
887 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700888 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700889 stateUpdateAvailable = true;
890
891 // Signal our end of the sync point and then dispose of it
892 mRemoteSyncPoints.front()->setTransactionApplied();
893 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800894 } else {
895 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700896 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700897 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700898 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700899 stateUpdateAvailable = true;
900 }
901 }
902
903 // If we still have pending updates, wake SurfaceFlinger back up and point
904 // it at this layer so we can process them
905 if (!mPendingStates.empty()) {
906 setTransactionFlags(eTransactionNeeded);
907 mFlinger->setTransactionFlags(eTraversalNeeded);
908 }
909
910 mCurrentState.modified = false;
911 return stateUpdateAvailable;
912}
913
Marissa Wall61c58622018-07-18 10:12:20 -0700914uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000915 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800916
Marissa Wall61c58622018-07-18 10:12:20 -0700917 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
918 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700919
David Sodmaneb085e02017-10-05 18:49:04 -0700920 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700921 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000922 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800923 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
924 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
925 " requested={ wh={%4u,%4u} }}\n"
926 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
927 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700928 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700929 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
930 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
931 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
932 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
933 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700934 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
935 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
936 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800937 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700938
Robert Carre392b552017-09-19 12:16:05 -0700939 // Don't let Layer::doTransaction update the drawing state
940 // if we have a pending resize, unless we are in fixed-size mode.
941 // the drawing state will be updated only once we receive a buffer
942 // with the correct size.
943 //
944 // In particular, we want to make sure the clip (which is part
945 // of the geometry state) is latched together with the size but is
946 // latched immediately when no resizing is involved.
947 //
948 // If a sideband stream is attached, however, we want to skip this
949 // optimization so that transactions aren't missed when a buffer
950 // never arrives
951 //
952 // In the case that we don't have a buffer we ignore other factors
953 // and avoid entering the resizePending state. At a high level the
954 // resizePending state is to avoid applying the state of the new buffer
955 // to the old buffer. However in the state where we don't have an old buffer
956 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700957 const bool resizePending =
958 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
959 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
David Sodman0cc69182017-11-17 12:12:07 -0800960 (getBE().compositionInfo.mBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700961 if (!isFixedSize()) {
David Sodman0cc69182017-11-17 12:12:07 -0800962 if (resizePending && getBE().compositionInfo.hwc.sidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700963 flags |= eDontUpdateGeometryState;
964 }
965 }
966
Robert Carr7bf247e2017-05-18 14:02:49 -0700967 // Here we apply various requested geometry states, depending on our
968 // latching configuration. See Layer.h for a detailed discussion of
969 // how geometry latching is controlled.
970 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000971 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700972
973 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
974 // mode, which causes attributes which normally latch regardless of scaling mode,
975 // to be delayed. We copy the requested state to the active state making sure
976 // to respect these rules (again see Layer.h for a detailed discussion).
977 //
978 // There is an awkward asymmetry in the handling of the crop states in the position
979 // states, as can be seen below. Largely this arises from position and transform
980 // being stored in the same data structure while having different latching rules.
981 // b/38182305
982 //
Marissa Wall61c58622018-07-18 10:12:20 -0700983 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700984 // applyPendingStates in the presence of deferred transactions.
985 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -0700986 float tx = stateToCommit->active_legacy.transform.tx();
987 float ty = stateToCommit->active_legacy.transform.ty();
988 stateToCommit->active_legacy = stateToCommit->requested_legacy;
989 stateToCommit->active_legacy.transform.set(tx, ty);
990 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700991 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700992 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -0700993 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700994 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800995 }
996
Marissa Wall61c58622018-07-18 10:12:20 -0700997 return flags;
998}
999
1000uint32_t Layer::doTransaction(uint32_t flags) {
1001 ATRACE_CALL();
1002
1003 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +00001004 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -07001005 if (!applyPendingStates(&c)) {
1006 return 0;
1007 }
1008
1009 flags = doTransactionResize(flags, &c);
1010
Alec Mourib416efd2018-09-06 21:01:59 +00001011 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -07001012
1013 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001014 // invalidate and recompute the visible regions if needed
1015 flags |= Layer::eVisibleRegion;
1016 }
1017
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001018 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001019 // invalidate and recompute the visible regions if needed
1020 flags |= eVisibleRegion;
1021 this->contentDirty = true;
1022
1023 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -07001024 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -07001025 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -08001026 }
1027
Dan Stozac8145172016-04-28 16:29:06 -07001028 // If the layer is hidden, signal and clear out all local sync points so
1029 // that transactions for layers depending on this layer's frames becoming
1030 // visible are not blocked
1031 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001032 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001033 }
1034
Mathias Agopian13127d82013-03-05 17:47:11 -08001035 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001036 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001037 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001038}
1039
Pablo Ceballos05289c22016-04-14 15:49:55 -07001040void Layer::commitTransaction(const State& stateToCommit) {
1041 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001042}
1043
Mathias Agopian13127d82013-03-05 17:47:11 -08001044uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Piquef1c675b2018-09-12 20:45:39 -07001045 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -08001046}
1047
1048uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Piquef1c675b2018-09-12 20:45:39 -07001049 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -08001050}
1051
Robert Carr82364e32016-05-15 11:27:47 -07001052bool Layer::setPosition(float x, float y, bool immediate) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001053 if (mCurrentState.requested_legacy.transform.tx() == x &&
1054 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001055 return false;
1056 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001057
1058 // We update the requested and active position simultaneously because
1059 // we want to apply the position portion of the transform matrix immediately,
1060 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Marissa Wallf58c14b2018-07-24 10:50:43 -07001061 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001062 if (immediate && !mFreezeGeometryUpdates) {
1063 // Here we directly update the active state
1064 // unlike other setters, because we store it within
1065 // the transform, but use different latching rules.
1066 // b/38182305
Marissa Wallf58c14b2018-07-24 10:50:43 -07001067 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001068 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001069 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001070
Dan Stoza7dde5992015-05-22 09:51:44 -07001071 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001072 setTransactionFlags(eTransactionNeeded);
1073 return true;
1074}
Robert Carr82364e32016-05-15 11:27:47 -07001075
Robert Carr1f0a16a2016-10-24 16:27:39 -07001076bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1077 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1078 if (idx < 0) {
1079 return false;
1080 }
1081 if (childLayer->setLayer(z)) {
1082 mCurrentChildren.removeAt(idx);
1083 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001084 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001085 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001086 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001087}
1088
Robert Carr503c7042017-09-27 15:06:08 -07001089bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1090 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1091 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1092 if (idx < 0) {
1093 return false;
1094 }
1095 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1096 mCurrentChildren.removeAt(idx);
1097 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001098 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001099 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001100 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001101}
1102
Robert Carrae060832016-11-28 10:51:00 -08001103bool Layer::setLayer(int32_t z) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001104 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001105 mCurrentState.sequence++;
1106 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001107 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001108
1109 // Discard all relative layering.
1110 if (mCurrentState.zOrderRelativeOf != nullptr) {
1111 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1112 if (strongRelative != nullptr) {
1113 strongRelative->removeZOrderRelative(this);
1114 }
1115 mCurrentState.zOrderRelativeOf = nullptr;
1116 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001117 setTransactionFlags(eTransactionNeeded);
1118 return true;
1119}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001120
Robert Carrdb66e622017-04-10 16:55:57 -07001121void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1122 mCurrentState.zOrderRelatives.remove(relative);
1123 mCurrentState.sequence++;
1124 mCurrentState.modified = true;
1125 setTransactionFlags(eTransactionNeeded);
1126}
1127
1128void Layer::addZOrderRelative(const wp<Layer>& relative) {
1129 mCurrentState.zOrderRelatives.add(relative);
1130 mCurrentState.modified = true;
1131 mCurrentState.sequence++;
1132 setTransactionFlags(eTransactionNeeded);
1133}
1134
Robert Carr503d2bd2017-12-04 15:49:47 -08001135bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001136 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1137 if (handle == nullptr) {
1138 return false;
1139 }
1140 sp<Layer> relative = handle->owner.promote();
1141 if (relative == nullptr) {
1142 return false;
1143 }
1144
Robert Carr503d2bd2017-12-04 15:49:47 -08001145 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1146 mCurrentState.zOrderRelativeOf == relative) {
1147 return false;
1148 }
1149
Robert Carrdb66e622017-04-10 16:55:57 -07001150 mCurrentState.sequence++;
1151 mCurrentState.modified = true;
Robert Carr503d2bd2017-12-04 15:49:47 -08001152 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001153
chaviw9ab4bd12017-11-03 13:11:00 -07001154 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
1155 if (oldZOrderRelativeOf != nullptr) {
1156 oldZOrderRelativeOf->removeZOrderRelative(this);
1157 }
Robert Carrdb66e622017-04-10 16:55:57 -07001158 mCurrentState.zOrderRelativeOf = relative;
1159 relative->addZOrderRelative(this);
1160
1161 setTransactionFlags(eTransactionNeeded);
1162
1163 return true;
1164}
1165
Mathias Agopian13127d82013-03-05 17:47:11 -08001166bool Layer::setSize(uint32_t w, uint32_t h) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001167 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
1168 return false;
1169 mCurrentState.requested_legacy.w = w;
1170 mCurrentState.requested_legacy.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001171 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001172 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001173
1174 // record the new size, from this point on, when the client request
1175 // a buffer, it'll get the new size.
1176 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001177 return true;
1178}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001179bool Layer::setAlpha(float alpha) {
David Sodman41fdfc92017-11-06 16:09:56 -08001180 if (mCurrentState.color.a == alpha) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001181 mCurrentState.sequence++;
chaviw13fdc492017-06-27 12:40:18 -07001182 mCurrentState.color.a = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001183 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001184 setTransactionFlags(eTransactionNeeded);
1185 return true;
1186}
chaviw13fdc492017-06-27 12:40:18 -07001187
1188bool Layer::setColor(const half3& color) {
David Sodman41fdfc92017-11-06 16:09:56 -08001189 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g &&
1190 color.b == mCurrentState.color.b)
chaviw13fdc492017-06-27 12:40:18 -07001191 return false;
1192
1193 mCurrentState.sequence++;
1194 mCurrentState.color.r = color.r;
1195 mCurrentState.color.g = color.g;
1196 mCurrentState.color.b = color.b;
1197 mCurrentState.modified = true;
1198 setTransactionFlags(eTransactionNeeded);
1199 return true;
1200}
1201
Robert Carrd4ae7f32018-06-07 16:10:57 -07001202bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1203 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001204 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001205 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1206
1207 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1208 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1209 return false;
1210 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001211 mCurrentState.sequence++;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001212 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1213 matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001214 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001215 setTransactionFlags(eTransactionNeeded);
1216 return true;
1217}
Marissa Wall61c58622018-07-18 10:12:20 -07001218
Mathias Agopian13127d82013-03-05 17:47:11 -08001219bool Layer::setTransparentRegionHint(const Region& transparent) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001220 mCurrentState.requestedTransparentRegion_legacy = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001221 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001222 setTransactionFlags(eTransactionNeeded);
1223 return true;
1224}
1225bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1226 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
David Sodman41fdfc92017-11-06 16:09:56 -08001227 if (mCurrentState.flags == newFlags) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001228 mCurrentState.sequence++;
1229 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001230 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001231 setTransactionFlags(eTransactionNeeded);
1232 return true;
1233}
Robert Carr99e27f02016-06-16 15:18:02 -07001234
Marissa Wallf58c14b2018-07-24 10:50:43 -07001235bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
1236 if (mCurrentState.requestedCrop_legacy == crop) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001237 mCurrentState.sequence++;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001238 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001239 if (immediate && !mFreezeGeometryUpdates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001240 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001241 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001242 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1243
Dan Stoza7dde5992015-05-22 09:51:44 -07001244 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001245 setTransactionFlags(eTransactionNeeded);
1246 return true;
1247}
Robert Carr8d5227b2017-03-16 15:41:03 -07001248
Robert Carrc3574f72016-03-24 12:19:32 -07001249bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001250 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001251 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001252 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001253 return true;
1254}
1255
rongliucfb187b2018-03-14 12:26:23 -07001256void Layer::setInfo(int32_t type, int32_t appId) {
David Sodman41fdfc92017-11-06 16:09:56 -08001257 mCurrentState.appId = appId;
1258 mCurrentState.type = type;
1259 mCurrentState.modified = true;
1260 setTransactionFlags(eTransactionNeeded);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001261}
1262
Mathias Agopian13127d82013-03-05 17:47:11 -08001263bool Layer::setLayerStack(uint32_t layerStack) {
David Sodman41fdfc92017-11-06 16:09:56 -08001264 if (mCurrentState.layerStack == layerStack) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001265 mCurrentState.sequence++;
1266 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001267 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001268 setTransactionFlags(eTransactionNeeded);
1269 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001270}
1271
Robert Carr1f0a16a2016-10-24 16:27:39 -07001272uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001273 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001274 if (p == nullptr) {
1275 return getDrawingState().layerStack;
1276 }
1277 return p->getLayerStack();
1278}
1279
Marissa Wallf58c14b2018-07-24 10:50:43 -07001280void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
1281 mCurrentState.barrierLayer_legacy = barrierLayer;
1282 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001283 // We don't set eTransactionNeeded, because just receiving a deferral
1284 // request without any other state updates shouldn't actually induce a delay
1285 mCurrentState.modified = true;
1286 pushPendingState();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001287 mCurrentState.barrierLayer_legacy = nullptr;
1288 mCurrentState.frameNumber_legacy = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001289 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001290}
1291
Marissa Wallf58c14b2018-07-24 10:50:43 -07001292void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001293 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001294 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001295}
1296
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001297// ----------------------------------------------------------------------------
1298// pageflip handling...
1299// ----------------------------------------------------------------------------
1300
Robert Carr1f0a16a2016-10-24 16:27:39 -07001301bool Layer::isHiddenByPolicy() const {
Alec Mourib416efd2018-09-06 21:01:59 +00001302 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001303 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001304 if (parent != nullptr && parent->isHiddenByPolicy()) {
1305 return true;
1306 }
1307 return s.flags & layer_state_t::eLayerHidden;
1308}
1309
David Sodman41fdfc92017-11-06 16:09:56 -08001310uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001311 // TODO: should we do something special if mSecure is set?
1312 if (mProtectedByApp) {
1313 // need a hardware-protected path to external video sink
1314 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001315 }
Riley Andrews03414a12014-07-01 14:22:59 -07001316 if (mPotentialCursor) {
1317 usage |= GraphicBuffer::USAGE_CURSOR;
1318 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001319 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001320 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001321}
1322
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001323void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001324 uint32_t orientation = 0;
1325 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001326 // The transform hint is used to improve performance, but we can
1327 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001328 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001329 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001330 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001331 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001332 orientation = 0;
1333 }
1334 }
David Sodmaneb085e02017-10-05 18:49:04 -07001335 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001336}
1337
Mathias Agopian13127d82013-03-05 17:47:11 -08001338// ----------------------------------------------------------------------------
1339// debugging
1340// ----------------------------------------------------------------------------
1341
Marissa Wall61c58622018-07-18 10:12:20 -07001342// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001343LayerDebugInfo Layer::getLayerDebugInfo() const {
1344 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001345 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001346 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001347 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001348 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
1349 info.mType = String8(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001350 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001351 info.mVisibleRegion = visibleRegion;
1352 info.mSurfaceDamageRegion = surfaceDamageRegion;
1353 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001354 info.mX = ds.active_legacy.transform.tx();
1355 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001356 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001357 info.mWidth = ds.active_legacy.w;
1358 info.mHeight = ds.active_legacy.h;
1359 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001360 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001361 info.mFlags = ds.flags;
1362 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001363 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001364 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1365 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1366 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1367 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001368 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001369 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001370 if (buffer != 0) {
1371 info.mActiveBufferWidth = buffer->getWidth();
1372 info.mActiveBufferHeight = buffer->getHeight();
1373 info.mActiveBufferStride = buffer->getStride();
1374 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001375 } else {
1376 info.mActiveBufferWidth = 0;
1377 info.mActiveBufferHeight = 0;
1378 info.mActiveBufferStride = 0;
1379 info.mActiveBufferFormat = 0;
1380 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001381 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001382 info.mNumQueuedFrames = getQueuedFrameCount();
1383 info.mRefreshPending = isBufferLatched();
1384 info.mIsOpaque = isOpaque(ds);
1385 info.mContentDirty = contentDirty;
1386 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001387}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001388
Dan Stozae22aec72016-08-01 13:20:59 -07001389void Layer::miniDumpHeader(String8& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001390 result.append("-------------------------------");
1391 result.append("-------------------------------");
1392 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001393 result.append(" Layer name\n");
1394 result.append(" Z | ");
1395 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001396 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001397 result.append(" Disp Frame (LTRB) | ");
1398 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001399 result.append("-------------------------------");
1400 result.append("-------------------------------");
1401 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001402}
1403
Dominik Laskowski7e045462018-05-30 13:02:02 -07001404void Layer::miniDump(String8& result, int32_t displayId) const {
1405 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stozae22aec72016-08-01 13:20:59 -07001406 return;
1407 }
1408
1409 String8 name;
1410 if (mName.length() > 77) {
1411 std::string shortened;
1412 shortened.append(mName.string(), 36);
1413 shortened.append("[...]");
1414 shortened.append(mName.string() + (mName.length() - 36), 36);
1415 name = shortened.c_str();
1416 } else {
1417 name = mName;
1418 }
1419
1420 result.appendFormat(" %s\n", name.string());
1421
Alec Mourib416efd2018-09-06 21:01:59 +00001422 const State& layerState(getDrawingState());
Lloyd Pique074e8122018-07-26 12:57:23 -07001423 const LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers.at(displayId);
Chia-I Wu1e043612018-03-01 09:45:09 -08001424 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
1425 result.appendFormat(" rel %6d | ", layerState.z);
1426 } else {
1427 result.appendFormat(" %10d | ", layerState.z);
1428 }
Dominik Laskowski7e045462018-05-30 13:02:02 -07001429 result.appendFormat("%10s | ", to_string(getCompositionType(displayId)).c_str());
Lloyd Pique074e8122018-07-26 12:57:23 -07001430 result.appendFormat("%10s | ", to_string(hwcInfo.transform).c_str());
1431 const Rect& frame = hwcInfo.displayFrame;
David Sodman41fdfc92017-11-06 16:09:56 -08001432 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique074e8122018-07-26 12:57:23 -07001433 const FloatRect& crop = hwcInfo.sourceCrop;
David Sodman41fdfc92017-11-06 16:09:56 -08001434 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 -07001435
David Sodman7e4ae112018-02-09 15:02:28 -08001436 result.append("- - - - - - - - - - - - - - - -\n");
1437
1438 std::string compositionInfoStr;
1439 getBE().compositionInfo.dump(compositionInfoStr, "compositionInfo");
1440 result.append(compositionInfoStr.c_str());
1441
Yichi Chen6ca35192018-05-29 12:20:43 +08001442 result.append("- - - - - - - - - - - - - - - -");
1443 result.append("- - - - - - - - - - - - - - - -");
1444 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001445}
Dan Stozae22aec72016-08-01 13:20:59 -07001446
Svetoslavd85084b2014-03-20 10:28:31 -07001447void Layer::dumpFrameStats(String8& result) const {
1448 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001449}
1450
Svetoslavd85084b2014-03-20 10:28:31 -07001451void Layer::clearFrameStats() {
1452 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001453}
1454
Jamie Gennis6547ff42013-07-16 20:12:42 -07001455void Layer::logFrameStats() {
1456 mFrameTracker.logAndResetStats(mName);
1457}
1458
Svetoslavd85084b2014-03-20 10:28:31 -07001459void Layer::getFrameStats(FrameStats* outStats) const {
1460 mFrameTracker.getStats(outStats);
1461}
1462
Brian Andersond6927fb2016-07-23 23:37:30 -07001463void Layer::dumpFrameEvents(String8& result) {
David Sodman41fdfc92017-11-06 16:09:56 -08001464 result.appendFormat("- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001465 Mutex::Autolock lock(mFrameEventHistoryMutex);
1466 mFrameEventHistory.checkFencesForCompletion();
1467 mFrameEventHistory.dump(result);
1468}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001469
Brian Anderson5ea5e592016-12-01 16:54:33 -08001470void Layer::onDisconnect() {
1471 Mutex::Autolock lock(mFrameEventHistoryMutex);
1472 mFrameEventHistory.onDisconnect();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -07001473 mTimeStats.onDisconnect(getSequence());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001474}
1475
Brian Anderson3890c392016-07-25 12:48:08 -07001476void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001477 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001478 if (newTimestamps) {
Yiwei Zhang8e8fe522018-11-02 18:34:07 -07001479 mTimeStats.setPostTime(getSequence(), newTimestamps->frameNumber, getName().c_str(),
1480 newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001481 }
1482
Brian Andersond6927fb2016-07-23 23:37:30 -07001483 Mutex::Autolock lock(mFrameEventHistoryMutex);
1484 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001485 // If there are any unsignaled fences in the aquire timeline at this
1486 // point, the previously queued frame hasn't been latched yet. Go ahead
1487 // and try to get the signal time here so the syscall is taken out of
1488 // the main thread's critical path.
1489 mAcquireTimeline.updateSignalTimes();
1490 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001491 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001492 mFrameEventHistory.addQueue(*newTimestamps);
1493 }
1494
Brian Anderson3890c392016-07-25 12:48:08 -07001495 if (outDelta) {
1496 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001497 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001498}
Dan Stozae77c7662016-05-13 11:37:28 -07001499
Chia-I Wu98f1c102017-05-30 14:54:08 -07001500size_t Layer::getChildrenCount() const {
1501 size_t count = 0;
1502 for (const sp<Layer>& child : mCurrentChildren) {
1503 count += 1 + child->getChildrenCount();
1504 }
1505 return count;
1506}
1507
Robert Carr1f0a16a2016-10-24 16:27:39 -07001508void Layer::addChild(const sp<Layer>& layer) {
1509 mCurrentChildren.add(layer);
1510 layer->setParent(this);
1511}
1512
1513ssize_t Layer::removeChild(const sp<Layer>& layer) {
1514 layer->setParent(nullptr);
1515 return mCurrentChildren.remove(layer);
1516}
1517
Robert Carr1db73f62016-12-21 12:58:51 -08001518bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1519 sp<Handle> handle = nullptr;
1520 sp<Layer> newParent = nullptr;
1521 if (newParentHandle == nullptr) {
1522 return false;
1523 }
1524 handle = static_cast<Handle*>(newParentHandle.get());
1525 newParent = handle->owner.promote();
1526 if (newParent == nullptr) {
1527 ALOGE("Unable to promote Layer handle");
1528 return false;
1529 }
1530
1531 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001532 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001533
1534 sp<Client> client(child->mClientRef.promote());
1535 if (client != nullptr) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001536 client->updateParent(newParent);
Robert Carr1db73f62016-12-21 12:58:51 -08001537 }
1538 }
1539 mCurrentChildren.clear();
1540
1541 return true;
1542}
1543
Robert Carr15eae092018-03-23 13:43:53 -07001544void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001545 for (const sp<Layer>& child : mDrawingChildren) {
1546 child->mDrawingParent = newParent;
1547 }
1548}
1549
chaviwf1961f72017-09-18 16:41:07 -07001550bool Layer::reparent(const sp<IBinder>& newParentHandle) {
1551 if (newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001552 return false;
1553 }
1554
1555 auto handle = static_cast<Handle*>(newParentHandle.get());
1556 sp<Layer> newParent = handle->owner.promote();
1557 if (newParent == nullptr) {
1558 ALOGE("Unable to promote Layer handle");
1559 return false;
1560 }
1561
chaviwf1961f72017-09-18 16:41:07 -07001562 sp<Layer> parent = getParent();
1563 if (parent != nullptr) {
1564 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001565 }
chaviwf1961f72017-09-18 16:41:07 -07001566 newParent->addChild(this);
chaviw06178942017-07-27 10:25:59 -07001567
chaviwf1961f72017-09-18 16:41:07 -07001568 sp<Client> client(mClientRef.promote());
chaviw06178942017-07-27 10:25:59 -07001569 sp<Client> newParentClient(newParent->mClientRef.promote());
1570
chaviwf1961f72017-09-18 16:41:07 -07001571 if (client != newParentClient) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001572 client->updateParent(newParent);
chaviw06178942017-07-27 10:25:59 -07001573 }
1574
chaviw06178942017-07-27 10:25:59 -07001575 return true;
1576}
1577
Robert Carr9524cb32017-02-13 11:32:32 -08001578bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001579 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001580 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001581 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001582 if (client != nullptr && parentClient != client) {
Robert Carr7f619b22017-11-06 12:56:35 -08001583 client->detachLayer(child.get());
1584 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001585 }
Robert Carr7f619b22017-11-06 12:56:35 -08001586 }
Robert Carr9524cb32017-02-13 11:32:32 -08001587
1588 return true;
1589}
1590
Peiyong Lind3788632018-09-18 16:01:31 -07001591bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001592 static const mat4 identityMatrix = mat4();
1593
Peiyong Lind3788632018-09-18 16:01:31 -07001594 if (mCurrentState.colorTransform == matrix) {
1595 return false;
1596 }
1597 ++mCurrentState.sequence;
1598 mCurrentState.colorTransform = matrix;
Peiyong Lin747321c2018-10-01 10:03:11 -07001599 mCurrentState.hasColorTransform = matrix != identityMatrix;
1600 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001601 setTransactionFlags(eTransactionNeeded);
1602 return true;
1603}
1604
1605const mat4& Layer::getColorTransform() const {
1606 return getDrawingState().colorTransform;
1607}
1608
1609bool Layer::hasColorTransform() const {
Peiyong Lin747321c2018-10-01 10:03:11 -07001610 return getDrawingState().hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001611}
1612
Chia-I Wu11481472018-05-04 10:43:19 -07001613bool Layer::isLegacyDataSpace() const {
1614 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001615 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001616 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001617}
1618
Robert Carr1f0a16a2016-10-24 16:27:39 -07001619void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001620 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001621}
1622
1623void Layer::clearSyncPoints() {
1624 for (const auto& child : mCurrentChildren) {
1625 child->clearSyncPoints();
1626 }
1627
1628 Mutex::Autolock lock(mLocalSyncPointMutex);
1629 for (auto& point : mLocalSyncPoints) {
1630 point->setFrameAvailable();
1631 }
1632 mLocalSyncPoints.clear();
1633}
1634
1635int32_t Layer::getZ() const {
1636 return mDrawingState.z;
1637}
1638
Robert Carr29abff82017-12-04 13:51:20 -08001639bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1640 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1641 const State& state = useDrawing ? mDrawingState : mCurrentState;
1642 return state.zOrderRelativeOf != nullptr;
1643}
1644
David Sodman41fdfc92017-11-06 16:09:56 -08001645__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001646 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001647 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1648 "makeTraversalList received invalid stateSet");
1649 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1650 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1651 const State& state = useDrawing ? mDrawingState : mCurrentState;
1652
Robert Carr29abff82017-12-04 13:51:20 -08001653 if (state.zOrderRelatives.size() == 0) {
1654 *outSkipRelativeZUsers = true;
1655 return children;
1656 }
1657
chaviwfd462612018-05-31 16:11:27 -07001658 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001659 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001660 sp<Layer> strongRelative = weakRelative.promote();
1661 if (strongRelative != nullptr) {
1662 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001663 }
1664 }
1665
Dan Stoza412903f2017-04-27 13:42:17 -07001666 for (const sp<Layer>& child : children) {
Robert Carr503c7042017-09-27 15:06:08 -07001667 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1668 if (childState.zOrderRelativeOf != nullptr) {
1669 continue;
1670 }
Robert Carrdb66e622017-04-10 16:55:57 -07001671 traverse.add(child);
1672 }
1673
1674 return traverse;
1675}
1676
Robert Carr1f0a16a2016-10-24 16:27:39 -07001677/**
Robert Carrdb66e622017-04-10 16:55:57 -07001678 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001679 */
Dan Stoza412903f2017-04-27 13:42:17 -07001680void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001681 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1682 // produce a new list for traversing, including our relatives, and not including our children
1683 // who are relatives of another surface. In the case that there are no relative Z,
1684 // makeTraversalList returns our children directly to avoid significant overhead.
1685 // However in this case we need to take the responsibility for filtering children which
1686 // are relatives of another surface here.
1687 bool skipRelativeZUsers = false;
1688 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001689
Robert Carr1f0a16a2016-10-24 16:27:39 -07001690 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001691 for (; i < list.size(); i++) {
1692 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001693 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1694 continue;
1695 }
1696
Robert Carrdb66e622017-04-10 16:55:57 -07001697 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001698 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001699 }
Dan Stoza412903f2017-04-27 13:42:17 -07001700 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001701 }
Robert Carr29abff82017-12-04 13:51:20 -08001702
Dan Stoza412903f2017-04-27 13:42:17 -07001703 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001704 for (; i < list.size(); i++) {
1705 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001706
1707 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1708 continue;
1709 }
Dan Stoza412903f2017-04-27 13:42:17 -07001710 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001711 }
1712}
1713
1714/**
Robert Carrdb66e622017-04-10 16:55:57 -07001715 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001716 */
Dan Stoza412903f2017-04-27 13:42:17 -07001717void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1718 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001719 // See traverseInZOrder for documentation.
1720 bool skipRelativeZUsers = false;
1721 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001722
Robert Carr1f0a16a2016-10-24 16:27:39 -07001723 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001724 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001725 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001726
1727 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1728 continue;
1729 }
1730
Robert Carrdb66e622017-04-10 16:55:57 -07001731 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001732 break;
1733 }
Dan Stoza412903f2017-04-27 13:42:17 -07001734 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001735 }
Dan Stoza412903f2017-04-27 13:42:17 -07001736 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001737 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001738 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001739
1740 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1741 continue;
1742 }
1743
Dan Stoza412903f2017-04-27 13:42:17 -07001744 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001745 }
1746}
1747
chaviw4b129c22018-04-09 16:19:43 -07001748LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1749 const std::vector<Layer*>& layersInTree) {
1750 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1751 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001752 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1753 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
chaviw4b129c22018-04-09 16:19:43 -07001754 const State& state = useDrawing ? mDrawingState : mCurrentState;
1755
chaviwfd462612018-05-31 16:11:27 -07001756 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001757 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1758 sp<Layer> strongRelative = weakRelative.promote();
1759 // Only add relative layers that are also descendents of the top most parent of the tree.
1760 // If a relative layer is not a descendent, then it should be ignored.
1761 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1762 traverse.add(strongRelative);
1763 }
1764 }
1765
1766 for (const sp<Layer>& child : children) {
1767 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1768 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1769 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1770 // the child here since it won't be added later as a relative.
1771 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1772 childState.zOrderRelativeOf.promote().get())) {
1773 continue;
1774 }
1775 traverse.add(child);
1776 }
1777
1778 return traverse;
1779}
1780
1781void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1782 LayerVector::StateSet stateSet,
1783 const LayerVector::Visitor& visitor) {
1784 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001785
1786 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001787 for (; i < list.size(); i++) {
1788 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001789 if (relative->getZ() >= 0) {
1790 break;
1791 }
chaviw4b129c22018-04-09 16:19:43 -07001792 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001793 }
chaviw4b129c22018-04-09 16:19:43 -07001794
chaviwa76b2712017-09-20 12:02:26 -07001795 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001796 for (; i < list.size(); i++) {
1797 const auto& relative = list[i];
1798 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001799 }
1800}
1801
chaviw4b129c22018-04-09 16:19:43 -07001802std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1803 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1804 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1805
1806 std::vector<Layer*> layersInTree = {this};
1807 for (size_t i = 0; i < children.size(); i++) {
1808 const auto& child = children[i];
1809 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1810 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1811 }
1812
1813 return layersInTree;
1814}
1815
1816void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1817 const LayerVector::Visitor& visitor) {
1818 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1819 std::sort(layersInTree.begin(), layersInTree.end());
1820 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1821}
1822
Peiyong Linefefaac2018-08-17 12:27:51 -07001823ui::Transform Layer::getTransform() const {
1824 ui::Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001825 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001826 if (p != nullptr) {
1827 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07001828
1829 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
1830 // it isFixedSize) then there may be additional scaling not accounted
1831 // for in the transform. We need to mirror this scaling in child surfaces
1832 // or we will break the contract where WM can treat child surfaces as
1833 // pixels in the parent surface.
David Sodman0cc69182017-11-17 12:12:07 -08001834 if (p->isFixedSize() && p->getBE().compositionInfo.mBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07001835 int bufferWidth;
1836 int bufferHeight;
1837 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
David Sodman0cc69182017-11-17 12:12:07 -08001838 bufferWidth = p->getBE().compositionInfo.mBuffer->getWidth();
1839 bufferHeight = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001840 } else {
David Sodman0cc69182017-11-17 12:12:07 -08001841 bufferHeight = p->getBE().compositionInfo.mBuffer->getWidth();
1842 bufferWidth = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001843 }
Marissa Wall61c58622018-07-18 10:12:20 -07001844 float sx = p->getActiveWidth(p->getDrawingState()) / static_cast<float>(bufferWidth);
1845 float sy = p->getActiveHeight(p->getDrawingState()) / static_cast<float>(bufferHeight);
Peiyong Linefefaac2018-08-17 12:27:51 -07001846 ui::Transform extraParentScaling;
Robert Carr9b429f42017-04-17 14:56:57 -07001847 extraParentScaling.set(sx, 0, 0, sy);
1848 t = t * extraParentScaling;
1849 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001850 }
Marissa Wall61c58622018-07-18 10:12:20 -07001851 return t * getActiveTransform(getDrawingState());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001852}
1853
chaviw13fdc492017-06-27 12:40:18 -07001854half Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001855 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07001856
chaviw13fdc492017-06-27 12:40:18 -07001857 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1858 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001859}
Robert Carr6452f122017-03-21 10:41:29 -07001860
chaviw13fdc492017-06-27 12:40:18 -07001861half4 Layer::getColor() const {
1862 const half4 color(getDrawingState().color);
1863 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001864}
Robert Carr6452f122017-03-21 10:41:29 -07001865
Robert Carr1f0a16a2016-10-24 16:27:39 -07001866void Layer::commitChildList() {
1867 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1868 const auto& child = mCurrentChildren[i];
1869 child->commitChildList();
1870 }
1871 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001872 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001873}
1874
chaviw1d044282017-09-27 12:19:28 -07001875void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
1876 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1877 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1878 const State& state = useDrawing ? mDrawingState : mCurrentState;
1879
Peiyong Linefefaac2018-08-17 12:27:51 -07001880 ui::Transform requestedTransform = state.active_legacy.transform;
1881 ui::Transform transform = getTransform();
chaviw1d044282017-09-27 12:19:28 -07001882
1883 layerInfo->set_id(sequence);
1884 layerInfo->set_name(getName().c_str());
1885 layerInfo->set_type(String8(getTypeId()));
1886
1887 for (const auto& child : children) {
1888 layerInfo->add_children(child->sequence);
1889 }
1890
1891 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1892 sp<Layer> strongRelative = weakRelative.promote();
1893 if (strongRelative != nullptr) {
1894 layerInfo->add_relatives(strongRelative->sequence);
1895 }
1896 }
1897
Marissa Wallf58c14b2018-07-24 10:50:43 -07001898 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
chaviw1d044282017-09-27 12:19:28 -07001899 layerInfo->mutable_transparent_region());
1900 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
1901 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
1902
1903 layerInfo->set_layer_stack(getLayerStack());
1904 layerInfo->set_z(state.z);
1905
1906 PositionProto* position = layerInfo->mutable_position();
1907 position->set_x(transform.tx());
1908 position->set_y(transform.ty());
1909
1910 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
1911 requestedPosition->set_x(requestedTransform.tx());
1912 requestedPosition->set_y(requestedTransform.ty());
1913
1914 SizeProto* size = layerInfo->mutable_size();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001915 size->set_w(state.active_legacy.w);
1916 size->set_h(state.active_legacy.h);
chaviw1d044282017-09-27 12:19:28 -07001917
Marissa Wallf58c14b2018-07-24 10:50:43 -07001918 LayerProtoHelper::writeToProto(state.crop_legacy, layerInfo->mutable_crop());
chaviw1d044282017-09-27 12:19:28 -07001919
1920 layerInfo->set_is_opaque(isOpaque(state));
1921 layerInfo->set_invalidate(contentDirty);
Chia-I Wu01591c92018-05-22 12:03:00 -07001922
1923 // XXX (b/79210409) mCurrentDataSpace is not protected
1924 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
1925
chaviw1d044282017-09-27 12:19:28 -07001926 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1927 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
1928 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
1929 layerInfo->set_flags(state.flags);
1930
1931 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1932 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
1933
Jorim Jaggi8e0af362017-11-14 16:28:28 +01001934 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07001935 if (parent != nullptr) {
1936 layerInfo->set_parent(parent->sequence);
1937 }
1938
1939 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1940 if (zOrderRelativeOf != nullptr) {
1941 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1942 }
1943
Chia-I Wu01591c92018-05-22 12:03:00 -07001944 // XXX getBE().compositionInfo.mBuffer is not protected
David Sodman0cc69182017-11-17 12:12:07 -08001945 auto buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001946 if (buffer != nullptr) {
1947 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
Peiyong Linefefaac2018-08-17 12:27:51 -07001948 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
Yichi Chen6ca35192018-05-29 12:20:43 +08001949 layerInfo->mutable_buffer_transform());
chaviw1d044282017-09-27 12:19:28 -07001950 }
1951
1952 layerInfo->set_queued_frames(getQueuedFrameCount());
1953 layerInfo->set_refresh_pending(isBufferLatched());
rongliucfb187b2018-03-14 12:26:23 -07001954 layerInfo->set_window_type(state.type);
1955 layerInfo->set_app_id(state.appId);
chaviwadc40c22018-07-10 16:57:27 -07001956 layerInfo->set_curr_frame(mCurrentFrameNumber);
1957
1958 for (const auto& pendingState : mPendingStates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001959 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
chaviwadc40c22018-07-10 16:57:27 -07001960 if (barrierLayer != nullptr) {
1961 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
1962 barrierLayerProto->set_id(barrierLayer->sequence);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001963 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
chaviwadc40c22018-07-10 16:57:27 -07001964 }
1965 }
chaviw1d044282017-09-27 12:19:28 -07001966}
1967
Dominik Laskowski7e045462018-05-30 13:02:02 -07001968void Layer::writeToProto(LayerProto* layerInfo, int32_t displayId) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07001969 if (!hasHwcLayer(displayId)) {
1970 return;
1971 }
1972
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001973 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
1974
Dominik Laskowski7e045462018-05-30 13:02:02 -07001975 const auto& hwcInfo = getBE().mHwcLayers.at(displayId);
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001976
1977 const Rect& frame = hwcInfo.displayFrame;
1978 LayerProtoHelper::writeToProto(frame, layerInfo->mutable_hwc_frame());
1979
1980 const FloatRect& crop = hwcInfo.sourceCrop;
1981 LayerProtoHelper::writeToProto(crop, layerInfo->mutable_hwc_crop());
1982
1983 const int32_t transform = static_cast<int32_t>(hwcInfo.transform);
1984 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08001985
1986 const int32_t compositionType = static_cast<int32_t>(hwcInfo.compositionType);
1987 layerInfo->set_hwc_composition_type(compositionType);
1988
1989 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
1990 static_cast<BufferLayer*>(this)->isProtected()) {
1991 layerInfo->set_is_protected(true);
1992 } else {
1993 layerInfo->set_is_protected(false);
1994 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001995}
1996
Mathias Agopian13127d82013-03-05 17:47:11 -08001997// ---------------------------------------------------------------------------
1998
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001999}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002000
2001#if defined(__gl_h_)
2002#error "don't include gl/gl.h in this file"
2003#endif
2004
2005#if defined(__gl2_h_)
2006#error "don't include gl2/gl2.h in this file"
2007#endif