blob: 1f3f91991f2bd2cc97557d076961439ba717ec53 [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;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700105
106 // drawing state & current state are identical
107 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700108
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800109 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700110 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800111 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200112 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Dan Stoza436ccf32018-06-21 12:10:12 -0700113}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700114
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700115Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800116 sp<Client> c(mClientRef.promote());
117 if (c != 0) {
118 c->detachLayer(this);
119 }
120
Rob Carr4bba3702018-10-08 21:53:30 +0000121 for (auto& point : mRemoteSyncPoints) {
122 point->setTransactionApplied();
123 }
124 for (auto& point : mLocalSyncPoints) {
125 point->setFrameAvailable();
126 }
Jamie Gennis6547ff42013-07-16 20:12:42 -0700127 mFrameTracker.logAndResetStats(mName);
Mathias Agopian96f08192010-06-02 23:28:45 -0700128}
129
Mathias Agopian13127d82013-03-05 17:47:11 -0800130// ---------------------------------------------------------------------------
131// callbacks
132// ---------------------------------------------------------------------------
133
David Sodmaneb085e02017-10-05 18:49:04 -0700134/*
135 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
136 * Layer. So, the implementation is done in BufferLayer. When called on a
137 * ColorLayer object, it's essentially a NOP.
138 */
David Sodmaneb085e02017-10-05 18:49:04 -0700139void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800140
Chia-I Wuc6657022017-08-15 11:18:17 -0700141void Layer::onRemovedFromCurrentState() {
142 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
143
Rob Carr4bba3702018-10-08 21:53:30 +0000144 mPendingRemoval = true;
145
Robert Carr5edb1ad2017-04-25 10:54:24 -0700146 if (mCurrentState.zOrderRelativeOf != nullptr) {
147 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
148 if (strongRelative != nullptr) {
149 strongRelative->removeZOrderRelative(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700150 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700151 }
152 mCurrentState.zOrderRelativeOf = nullptr;
153 }
Rob Carr4bba3702018-10-08 21:53:30 +0000154
Chia-I Wuc6657022017-08-15 11:18:17 -0700155 for (const auto& child : mCurrentChildren) {
156 child->onRemovedFromCurrentState();
157 }
158}
Chia-I Wu38512252017-05-17 14:36:16 -0700159
Chia-I Wuc6657022017-08-15 11:18:17 -0700160void Layer::onRemoved() {
161 // the layer is removed from SF mLayersPendingRemoval
David Sodmaneb085e02017-10-05 18:49:04 -0700162 abandon();
Chia-I Wuc6657022017-08-15 11:18:17 -0700163
Steven Thomasb02664d2017-07-26 18:48:28 -0700164 destroyAllHwcLayers();
Chia-I Wu38512252017-05-17 14:36:16 -0700165
Rob Carr4bba3702018-10-08 21:53:30 +0000166 for (const auto& child : mCurrentChildren) {
167 child->onRemoved();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700168 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700169}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700170
Mathias Agopian13127d82013-03-05 17:47:11 -0800171// ---------------------------------------------------------------------------
172// set-up
173// ---------------------------------------------------------------------------
174
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700175const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800176 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800177}
178
chaviw13fdc492017-06-27 12:40:18 -0700179bool Layer::getPremultipledAlpha() const {
180 return mPremultipliedAlpha;
181}
182
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700183sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800184 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700185 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800186}
187
188// ---------------------------------------------------------------------------
189// h/w composer set-up
190// ---------------------------------------------------------------------------
191
Dominik Laskowski7e045462018-05-30 13:02:02 -0700192bool Layer::createHwcLayer(HWComposer* hwc, int32_t displayId) {
193 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.count(displayId) != 0,
194 "Already have a layer for display %d", displayId);
David Sodmanb8aaea12017-12-14 15:54:51 -0800195 auto layer = std::shared_ptr<HWC2::Layer>(
196 hwc->createLayer(displayId),
197 [hwc, displayId](HWC2::Layer* layer) {
198 hwc->destroyLayer(displayId, layer); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700199 if (!layer) {
200 return false;
201 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700202 LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers[displayId];
Steven Thomasb02664d2017-07-26 18:48:28 -0700203 hwcInfo.hwc = hwc;
204 hwcInfo.layer = layer;
David Sodmanf6a38932018-05-25 15:27:50 -0700205 layer->setLayerDestroyedListener(
Dominik Laskowski7e045462018-05-30 13:02:02 -0700206 [this, displayId](HWC2::Layer* /*layer*/) { getBE().mHwcLayers.erase(displayId); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700207 return true;
208}
209
Dominik Laskowski7e045462018-05-30 13:02:02 -0700210bool Layer::destroyHwcLayer(int32_t displayId) {
211 if (getBE().mHwcLayers.count(displayId) == 0) {
Chia-I Wu83806892017-11-16 10:50:20 -0800212 return false;
Steven Thomasb02664d2017-07-26 18:48:28 -0700213 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700214 auto& hwcInfo = getBE().mHwcLayers[displayId];
David Sodman41fdfc92017-11-06 16:09:56 -0800215 LOG_ALWAYS_FATAL_IF(hwcInfo.layer == nullptr, "Attempt to destroy null layer");
Steven Thomasb02664d2017-07-26 18:48:28 -0700216 LOG_ALWAYS_FATAL_IF(hwcInfo.hwc == nullptr, "Missing HWComposer");
David Sodmanb8aaea12017-12-14 15:54:51 -0800217 hwcInfo.layer = nullptr;
218
Chia-I Wu83806892017-11-16 10:50:20 -0800219 return true;
Steven Thomasb02664d2017-07-26 18:48:28 -0700220}
221
222void Layer::destroyAllHwcLayers() {
David Sodman6f65f3e2017-11-03 14:28:09 -0700223 size_t numLayers = getBE().mHwcLayers.size();
Steven Thomasb02664d2017-07-26 18:48:28 -0700224 for (size_t i = 0; i < numLayers; ++i) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700225 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.empty(), "destroyAllHwcLayers failed");
226 destroyHwcLayer(getBE().mHwcLayers.begin()->first);
Steven Thomasb02664d2017-07-26 18:48:28 -0700227 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700228 LOG_ALWAYS_FATAL_IF(!getBE().mHwcLayers.empty(),
David Sodman41fdfc92017-11-06 16:09:56 -0800229 "All hardware composer layers should have been destroyed");
Steven Thomasb02664d2017-07-26 18:48:28 -0700230}
Steven Thomasb02664d2017-07-26 18:48:28 -0700231
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800232Rect Layer::getContentCrop() const {
233 // this is the crop rectangle that applies to the buffer
234 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700235 Rect crop;
236 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800237 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700238 crop = mCurrentCrop;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800239 } else if (getBE().compositionInfo.mBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800240 // otherwise we use the whole buffer
David Sodman0cc69182017-11-17 12:12:07 -0800241 crop = getBE().compositionInfo.mBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700242 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800243 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700244 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700245 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700246 return crop;
247}
248
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700249static Rect reduce(const Rect& win, const Region& exclude) {
250 if (CC_LIKELY(exclude.isEmpty())) {
251 return win;
252 }
253 if (exclude.isRect()) {
254 return win.reduce(exclude.getBounds());
255 }
256 return Region(win).subtract(exclude).getBounds();
257}
258
Dan Stoza80d61162017-12-20 15:57:52 -0800259static FloatRect reduce(const FloatRect& win, const Region& exclude) {
260 if (CC_LIKELY(exclude.isEmpty())) {
261 return win;
262 }
263 // Convert through Rect (by rounding) for lack of FloatRegion
264 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
265}
266
Robert Carr1f0a16a2016-10-24 16:27:39 -0700267Rect Layer::computeScreenBounds(bool reduceTransparentRegion) const {
Alec Mourib416efd2018-09-06 21:01:59 +0000268 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700269 Rect win(getActiveWidth(s), getActiveHeight(s));
Robert Carr1f0a16a2016-10-24 16:27:39 -0700270
Marissa Wall61c58622018-07-18 10:12:20 -0700271 Rect crop = getCrop(s);
272 if (!crop.isEmpty()) {
273 win.intersect(crop, &win);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700274 }
275
Peiyong Linefefaac2018-08-17 12:27:51 -0700276 ui::Transform t = getTransform();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700277 win = t.transform(win);
278
Chia-I Wue41dbe62017-06-13 14:10:56 -0700279 const sp<Layer>& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700280 // Now we need to calculate the parent bounds, so we can clip ourselves to those.
281 // When calculating the parent bounds for purposes of clipping,
282 // we don't need to constrain the parent to its transparent region.
283 // The transparent region is an optimization based on the
284 // buffer contents of the layer, but does not affect the space allocated to
285 // it by policy, and thus children should be allowed to extend into the
286 // parent's transparent region. In fact one of the main uses, is to reduce
287 // buffer allocation size in cases where a child window sits behind a main window
288 // (by marking the hole in the parent window as a transparent region)
289 if (p != nullptr) {
290 Rect bounds = p->computeScreenBounds(false);
291 bounds.intersect(win, &win);
292 }
293
294 if (reduceTransparentRegion) {
Marissa Wall61c58622018-07-18 10:12:20 -0700295 auto const screenTransparentRegion = t.transform(getActiveTransparentRegion(s));
Robert Carr1f0a16a2016-10-24 16:27:39 -0700296 win = reduce(win, screenTransparentRegion);
297 }
298
299 return win;
300}
301
Dan Stoza80d61162017-12-20 15:57:52 -0800302FloatRect Layer::computeBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000303 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700304 return computeBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700305}
306
Dan Stoza80d61162017-12-20 15:57:52 -0800307FloatRect Layer::computeBounds(const Region& activeTransparentRegion) const {
Alec Mourib416efd2018-09-06 21:01:59 +0000308 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700309 Rect win(getActiveWidth(s), getActiveHeight(s));
Robert Carrb5d3d262016-03-25 15:08:13 -0700310
Marissa Wall61c58622018-07-18 10:12:20 -0700311 Rect crop = getCrop(s);
312 if (!crop.isEmpty()) {
313 win.intersect(crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800314 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700315
Chia-I Wue41dbe62017-06-13 14:10:56 -0700316 const auto& p = mDrawingParent.promote();
Robert Carrd4ae7f32018-06-07 16:10:57 -0700317 FloatRect floatWin = win.toFloatRect();
318 FloatRect parentBounds = floatWin;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700319 if (p != nullptr) {
Robert Carrd4ae7f32018-06-07 16:10:57 -0700320 // We pass an empty Region here for reasons mirroring that of the case described in
321 // the computeScreenBounds reduceTransparentRegion=false case.
322 parentBounds = p->computeBounds(Region());
Robert Carr1f0a16a2016-10-24 16:27:39 -0700323 }
324
Peiyong Linefefaac2018-08-17 12:27:51 -0700325 ui::Transform t = s.active_legacy.transform;
Dan Stoza80d61162017-12-20 15:57:52 -0800326
Vishnu Nairdcce0e22018-08-23 08:35:19 -0700327 if (p != nullptr) {
Dan Stoza80d61162017-12-20 15:57:52 -0800328 floatWin = t.transform(floatWin);
Robert Carrd4ae7f32018-06-07 16:10:57 -0700329 floatWin = floatWin.intersect(parentBounds);
Dan Stoza80d61162017-12-20 15:57:52 -0800330 floatWin = t.inverse().transform(floatWin);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700331 }
332
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700333 // subtract the transparent region and snap to the bounds
Dan Stoza80d61162017-12-20 15:57:52 -0800334 return reduce(floatWin, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800335}
336
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700337Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& display) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700338 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800339 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700340 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800341
342 // apply the projection's clipping to the window crop in
343 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700344 // if there are no window scaling involved, this operation will map to full
345 // pixels in the buffer.
346 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
347 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700348
Marissa Wall61c58622018-07-18 10:12:20 -0700349 Rect activeCrop(getActiveWidth(s), getActiveHeight(s));
350 Rect crop = getCrop(s);
351 if (!crop.isEmpty()) {
352 activeCrop.intersect(crop, &activeCrop);
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700353 }
354
Peiyong Linefefaac2018-08-17 12:27:51 -0700355 ui::Transform t = getTransform();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700356 activeCrop = t.transform(activeCrop);
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700357 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000358 activeCrop.clear();
359 }
chaviwb1154d12017-10-31 14:15:36 -0700360
361 const auto& p = mDrawingParent.promote();
362 if (p != nullptr) {
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700363 auto parentCrop = p->computeInitialCrop(display);
chaviwb1154d12017-10-31 14:15:36 -0700364 activeCrop.intersect(parentCrop, &activeCrop);
365 }
366
Robert Carr1f0a16a2016-10-24 16:27:39 -0700367 return activeCrop;
368}
369
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700370FloatRect Layer::computeCrop(const sp<const DisplayDevice>& display) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700371 // the content crop is the area of the content that gets scaled to the
372 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800373 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700374
375 // In addition there is a WM-specified crop we pull from our drawing state.
376 const State& s(getDrawingState());
377
378 // Screen space to make reduction to parent crop clearer.
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700379 Rect activeCrop = computeInitialCrop(display);
Peiyong Linefefaac2018-08-17 12:27:51 -0700380 ui::Transform t = getTransform();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700381 // Back to layer space to work with the content crop.
382 activeCrop = t.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800383
Michael Lentine28ea2172014-11-19 18:32:37 -0800384 // This needs to be here as transform.transform(Rect) computes the
385 // transformed rect and then takes the bounding box of the result before
386 // returning. This means
387 // transform.inverse().transform(transform.transform(Rect)) != Rect
388 // in which case we need to make sure the final rect is clipped to the
389 // display bounds.
Marissa Wall61c58622018-07-18 10:12:20 -0700390 if (!activeCrop.intersect(Rect(getActiveWidth(s), getActiveHeight(s)), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000391 activeCrop.clear();
392 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800393
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700394 // subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700395 activeCrop = reduce(activeCrop, getActiveTransparentRegion(s));
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700396
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000397 // Transform the window crop to match the buffer coordinate system,
398 // which means using the inverse of the current transform set on the
399 // SurfaceFlingerConsumer.
400 uint32_t invTransform = mCurrentTransform;
Robert Carrcae605c2017-03-29 12:10:31 -0700401 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000402 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700403 * the code below applies the primary display's inverse transform to the
404 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000405 */
David Sodman41fdfc92017-11-06 16:09:56 -0800406 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000407 // calculate the inverse transform
408 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800409 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800410 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000411 // and apply to the current transform
Peiyong Linefefaac2018-08-17 12:27:51 -0700412 invTransform = (ui::Transform(invTransformOrient) *
413 ui::Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800414 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000415
Marissa Wall61c58622018-07-18 10:12:20 -0700416 int winWidth = getActiveWidth(s);
417 int winHeight = getActiveHeight(s);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000418 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
419 // If the activeCrop has been rotate the ends are rotated but not
420 // the space itself so when transforming ends back we can't rely on
421 // a modification of the axes of rotation. To account for this we
422 // need to reorient the inverse rotation in terms of the current
423 // axes of rotation.
424 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
425 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
426 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800427 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000428 }
Marissa Wall61c58622018-07-18 10:12:20 -0700429 winWidth = getActiveHeight(s);
430 winHeight = getActiveWidth(s);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000431 }
Marissa Wall61c58622018-07-18 10:12:20 -0700432 const Rect winCrop = activeCrop.transform(invTransform, getActiveWidth(s), getActiveHeight(s));
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000433
434 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800435 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000436 float yScale = crop.getHeight() / float(winHeight);
437
David Sodman41fdfc92017-11-06 16:09:56 -0800438 float insetL = winCrop.left * xScale;
439 float insetT = winCrop.top * yScale;
440 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000441 float insetB = (winHeight - winCrop.bottom) * yScale;
442
David Sodman41fdfc92017-11-06 16:09:56 -0800443 crop.left += insetL;
444 crop.top += insetT;
445 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000446 crop.bottom -= insetB;
447
Mathias Agopian13127d82013-03-05 17:47:11 -0800448 return crop;
449}
450
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700451void Layer::setGeometry(const sp<const DisplayDevice>& display, uint32_t z) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700452 const auto displayId = display->getId();
Peiyong Lin91b1df22018-06-18 18:00:16 -0700453 if (!hasHwcLayer(displayId)) {
454 ALOGE("[%s] failed to setGeometry: no HWC layer found (%d)",
455 mName.string(), displayId);
456 return;
457 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700458 auto& hwcInfo = getBE().mHwcLayers[displayId];
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700459
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700460 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800461 hwcInfo.forceClientComposition = false;
462
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700463 if (isSecure() && !display->isSecure()) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800464 hwcInfo.forceClientComposition = true;
465 }
466
David Sodman15094112018-10-11 09:39:37 -0700467 auto& hwcLayer = hwcInfo.layer;
468
Mathias Agopian13127d82013-03-05 17:47:11 -0800469 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700470 const State& s(getDrawingState());
David Revemanecf0fa52017-03-03 11:32:44 -0500471 auto blendMode = HWC2::BlendMode::None;
Robert Carr6452f122017-03-21 10:41:29 -0700472 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800473 blendMode =
474 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800475 }
David Sodman15094112018-10-11 09:39:37 -0700476 auto error = hwcLayer->setBlendMode(blendMode);
477 ALOGE_IF(error != HWC2::Error::None,
478 "[%s] Failed to set blend mode %s:"
479 " %s (%d)",
480 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
481 static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700482 getBE().compositionInfo.hwc.blendMode = blendMode;
Mathias Agopian13127d82013-03-05 17:47:11 -0800483
484 // apply the layer's transform, followed by the display's global transform
485 // here we're guaranteed that the layer's transform preserves rects
Marissa Wall61c58622018-07-18 10:12:20 -0700486 Region activeTransparentRegion(getActiveTransparentRegion(s));
Peiyong Linefefaac2018-08-17 12:27:51 -0700487 ui::Transform t = getTransform();
Marissa Wall61c58622018-07-18 10:12:20 -0700488 Rect activeCrop = getCrop(s);
489 if (!activeCrop.isEmpty()) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700490 activeCrop = t.transform(activeCrop);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700491 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000492 activeCrop.clear();
493 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700494 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800495 // This needs to be here as transform.transform(Rect) computes the
496 // transformed rect and then takes the bounding box of the result before
497 // returning. This means
498 // transform.inverse().transform(transform.transform(Rect)) != Rect
499 // in which case we need to make sure the final rect is clipped to the
500 // display bounds.
Marissa Wall61c58622018-07-18 10:12:20 -0700501 if (!activeCrop.intersect(Rect(getActiveWidth(s), getActiveHeight(s)), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000502 activeCrop.clear();
503 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700504 // mark regions outside the crop as transparent
Marissa Wall61c58622018-07-18 10:12:20 -0700505 activeTransparentRegion.orSelf(Rect(0, 0, getActiveWidth(s), activeCrop.top));
Marissa Wallf58c14b2018-07-24 10:50:43 -0700506 activeTransparentRegion.orSelf(
Marissa Wall61c58622018-07-18 10:12:20 -0700507 Rect(0, activeCrop.bottom, getActiveWidth(s), getActiveHeight(s)));
David Sodman41fdfc92017-11-06 16:09:56 -0800508 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
509 activeTransparentRegion.orSelf(
Marissa Wall61c58622018-07-18 10:12:20 -0700510 Rect(activeCrop.right, activeCrop.top, getActiveWidth(s), activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700511 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700512
Dan Stoza80d61162017-12-20 15:57:52 -0800513 // computeBounds returns a FloatRect to provide more accuracy during the
514 // transformation. We then round upon constructing 'frame'.
515 Rect frame{t.transform(computeBounds(activeTransparentRegion))};
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700516 if (!frame.intersect(display->getViewport(), &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000517 frame.clear();
518 }
Peiyong Linefefaac2018-08-17 12:27:51 -0700519 const ui::Transform& tr = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800520 Rect transformedFrame = tr.transform(frame);
David Sodman15094112018-10-11 09:39:37 -0700521 error = hwcLayer->setDisplayFrame(transformedFrame);
522 if (error != HWC2::Error::None) {
523 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
524 transformedFrame.left, transformedFrame.top, transformedFrame.right,
525 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
526 } else {
527 hwcInfo.displayFrame = transformedFrame;
528 }
David Sodmanba340492018-08-05 21:51:33 -0700529 getBE().compositionInfo.hwc.displayFrame = transformedFrame;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800530
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700531 FloatRect sourceCrop = computeCrop(display);
David Sodman15094112018-10-11 09:39:37 -0700532 error = hwcLayer->setSourceCrop(sourceCrop);
533 if (error != HWC2::Error::None) {
534 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
535 "%s (%d)",
536 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
537 to_string(error).c_str(), static_cast<int32_t>(error));
538 } else {
539 hwcInfo.sourceCrop = sourceCrop;
540 }
David Sodmanba340492018-08-05 21:51:33 -0700541 getBE().compositionInfo.hwc.sourceCrop = sourceCrop;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800542
chaviw13fdc492017-06-27 12:40:18 -0700543 float alpha = static_cast<float>(getAlpha());
David Sodman15094112018-10-11 09:39:37 -0700544 error = hwcLayer->setPlaneAlpha(alpha);
545 ALOGE_IF(error != HWC2::Error::None,
546 "[%s] Failed to set plane alpha %.3f: "
547 "%s (%d)",
548 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700549 getBE().compositionInfo.hwc.alpha = alpha;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800550
David Sodman15094112018-10-11 09:39:37 -0700551 error = hwcLayer->setZOrder(z);
552 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
553 to_string(error).c_str(), static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700554 getBE().compositionInfo.hwc.z = z;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500555
Albert Chaulk2a589632017-05-04 16:59:44 -0400556 int type = s.type;
557 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700558 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400559 if (parent.get()) {
560 auto& parentState = parent->getDrawingState();
rongliucfb187b2018-03-14 12:26:23 -0700561 if (parentState.type >= 0 || parentState.appId >= 0) {
562 type = parentState.type;
563 appId = parentState.appId;
564 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400565 }
566
David Sodman15094112018-10-11 09:39:37 -0700567 error = hwcLayer->setInfo(type, appId);
568 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
569 static_cast<int32_t>(error));
570
David Sodmanba340492018-08-05 21:51:33 -0700571 getBE().compositionInfo.hwc.type = type;
572 getBE().compositionInfo.hwc.appId = appId;
573
Mathias Agopian29a367b2011-07-12 14:51:45 -0700574 /*
575 * Transformations are applied in this order:
576 * 1) buffer orientation/flip/mirror
577 * 2) state transformation (window manager)
578 * 3) layer orientation (screen orientation)
579 * (NOTE: the matrices are multiplied in reverse order)
580 */
581
Peiyong Linefefaac2018-08-17 12:27:51 -0700582 const ui::Transform bufferOrientation(mCurrentTransform);
583 ui::Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700584
Robert Carrcae605c2017-03-29 12:10:31 -0700585 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700586 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700587 * the code below applies the primary display's inverse transform to the
588 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700589 */
David Sodman41fdfc92017-11-06 16:09:56 -0800590 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700591 // calculate the inverse transform
592 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800593 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700594 }
Robert Carrcae605c2017-03-29 12:10:31 -0700595
596 /*
597 * Here we cancel out the orientation component of the WM transform.
598 * The scaling and translate components are already included in our bounds
599 * computation so it's enough to just omit it in the composition.
600 * See comment in onDraw with ref to b/36727915 for why.
601 */
Peiyong Linefefaac2018-08-17 12:27:51 -0700602 transform = ui::Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700603 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700604
605 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800606 const uint32_t orientation = transform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -0700607 if (orientation & ui::Transform::ROT_INVALID) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800608 // we can only handle simple transformation
Lloyd Pique074e8122018-07-26 12:57:23 -0700609 hwcInfo.forceClientComposition = true;
David Sodman15094112018-10-11 09:39:37 -0700610 getBE().mHwcLayers[displayId].compositionType = HWC2::Composition::Client;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800611 } else {
612 auto transform = static_cast<HWC2::Transform>(orientation);
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800613 hwcInfo.transform = transform;
David Sodman15094112018-10-11 09:39:37 -0700614 auto error = hwcLayer->setTransform(transform);
615 ALOGE_IF(error != HWC2::Error::None,
616 "[%s] Failed to set transform %s: "
617 "%s (%d)",
618 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
619 static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700620 getBE().compositionInfo.hwc.transform = transform;
David Sodman4b7c4bc2017-11-17 12:13:59 -0800621 }
622}
623
Dominik Laskowski7e045462018-05-30 13:02:02 -0700624void Layer::forceClientComposition(int32_t displayId) {
625 if (getBE().mHwcLayers.count(displayId) == 0) {
626 ALOGE("forceClientComposition: no HWC layer found (%d)", displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800627 return;
628 }
629
Dominik Laskowski7e045462018-05-30 13:02:02 -0700630 getBE().mHwcLayers[displayId].forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800631}
Dan Stozaee44edd2015-03-23 15:50:23 -0700632
Dominik Laskowski7e045462018-05-30 13:02:02 -0700633bool Layer::getForceClientComposition(int32_t displayId) {
634 if (getBE().mHwcLayers.count(displayId) == 0) {
635 ALOGE("getForceClientComposition: no HWC layer found (%d)", displayId);
chaviwc9232ed2017-11-14 15:31:15 -0800636 return false;
637 }
638
Dominik Laskowski7e045462018-05-30 13:02:02 -0700639 return getBE().mHwcLayers[displayId].forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800640}
641
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700642void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700643 const auto displayId = display->getId();
644 if (getBE().mHwcLayers.count(displayId) == 0 ||
645 getCompositionType(displayId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800646 return;
647 }
648
649 // This gives us only the "orientation" component of the transform
650 const State& s(getCurrentState());
651
652 // Apply the layer's transform, followed by the display's global transform
653 // Here we're guaranteed that the layer's transform preserves rects
Marissa Wall61c58622018-07-18 10:12:20 -0700654 Rect win(getActiveWidth(s), getActiveHeight(s));
655 Rect crop = getCrop(s);
656 if (!crop.isEmpty()) {
657 win.intersect(crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800658 }
659 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700660 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Robert Carr1f0a16a2016-10-24 16:27:39 -0700661 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700662 frame.intersect(display->getViewport(), &frame);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700663 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800664 auto position = displayTransform.transform(frame);
665
Dominik Laskowski7e045462018-05-30 13:02:02 -0700666 auto error =
David Sodmanb8aaea12017-12-14 15:54:51 -0800667 (getBE().mHwcLayers[displayId].layer)->setCursorPosition(
668 position.left, position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800669 ALOGE_IF(error != HWC2::Error::None,
670 "[%s] Failed to set cursor position "
671 "to (%d, %d): %s (%d)",
672 mName.string(), position.left, position.top, to_string(error).c_str(),
673 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800674}
Riley Andrews03414a12014-07-01 14:22:59 -0700675
Mathias Agopian13127d82013-03-05 17:47:11 -0800676// ---------------------------------------------------------------------------
677// drawing...
678// ---------------------------------------------------------------------------
679
Marissa Wall61c58622018-07-18 10:12:20 -0700680void Layer::draw(const RenderArea& renderArea, const Region& clip) {
chaviwa76b2712017-09-20 12:02:26 -0700681 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800682}
683
Marissa Wall61c58622018-07-18 10:12:20 -0700684void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) {
chaviwa76b2712017-09-20 12:02:26 -0700685 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800686}
687
David Sodman41fdfc92017-11-06 16:09:56 -0800688void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
689 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800690 auto& engine(mFlinger->getRenderEngine());
David Sodman9eeae692017-11-02 10:53:32 -0700691 computeGeometry(renderArea, getBE().mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700692 engine.setupFillWithColor(red, green, blue, alpha);
David Sodman9eeae692017-11-02 10:53:32 -0700693 engine.drawMesh(getBE().mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800694}
695
David Sodmanc1498e62018-09-12 14:36:26 -0700696void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
697 clearWithOpenGL(renderArea, 0, 0, 0, 0);
698}
699
David Sodman15094112018-10-11 09:39:37 -0700700void Layer::setCompositionType(int32_t displayId, HWC2::Composition type, bool callIntoHwc) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700701 if (getBE().mHwcLayers.count(displayId) == 0) {
Chia-I Wu30505fb2018-03-26 16:20:31 -0700702 ALOGE("setCompositionType called without a valid HWC layer");
703 return;
David Sodman15094112018-10-11 09:39:37 -0700704 }
705 auto& hwcInfo = getBE().mHwcLayers[displayId];
706 auto& hwcLayer = hwcInfo.layer;
707 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", (hwcLayer)->getId(), to_string(type).c_str(),
708 static_cast<int>(callIntoHwc));
709 if (hwcInfo.compositionType != type) {
710 ALOGV(" actually setting");
711 hwcInfo.compositionType = type;
712 if (callIntoHwc) {
713 auto error = (hwcLayer)->setCompositionType(type);
714 ALOGE_IF(error != HWC2::Error::None,
715 "[%s] Failed to set "
716 "composition type %s: %s (%d)",
717 mName.string(), to_string(type).c_str(), to_string(error).c_str(),
718 static_cast<int32_t>(error));
Chia-I Wu30505fb2018-03-26 16:20:31 -0700719 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800720 }
721}
722
Dominik Laskowski7e045462018-05-30 13:02:02 -0700723HWC2::Composition Layer::getCompositionType(int32_t displayId) const {
David Sodman15fb96e2018-01-07 10:23:24 -0800724 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700725 // If we're querying the composition type for a display that does not
726 // have a HWC counterpart, then it will always be Client
727 return HWC2::Composition::Client;
728 }
David Sodman15fb96e2018-01-07 10:23:24 -0800729 return getBE().mHwcLayers[displayId].compositionType;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800730}
731
Dominik Laskowski7e045462018-05-30 13:02:02 -0700732void Layer::setClearClientTarget(int32_t displayId, bool clear) {
733 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800734 ALOGE("setClearClientTarget called without a valid HWC layer");
735 return;
736 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700737 getBE().mHwcLayers[displayId].clearClientTarget = clear;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800738}
739
Dominik Laskowski7e045462018-05-30 13:02:02 -0700740bool Layer::getClearClientTarget(int32_t displayId) const {
741 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800742 ALOGE("getClearClientTarget called without a valid HWC layer");
743 return false;
744 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700745 return getBE().mHwcLayers.at(displayId).clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800746}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800747
Dan Stozacac35382016-01-27 12:21:06 -0800748bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
749 if (point->getFrameNumber() <= mCurrentFrameNumber) {
750 // Don't bother with a SyncPoint, since we've already latched the
751 // relevant frame
752 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700753 }
754
Dan Stozacac35382016-01-27 12:21:06 -0800755 Mutex::Autolock lock(mLocalSyncPointMutex);
756 mLocalSyncPoints.push_back(point);
757 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700758}
759
Mathias Agopian13127d82013-03-05 17:47:11 -0800760// ----------------------------------------------------------------------------
761// local state
762// ----------------------------------------------------------------------------
763
Peiyong Lin833074a2018-08-28 11:53:54 -0700764void Layer::computeGeometry(const RenderArea& renderArea,
765 renderengine::Mesh& mesh,
chaviwa76b2712017-09-20 12:02:26 -0700766 bool useIdentityTransform) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700767 const ui::Transform renderAreaTransform(renderArea.getTransform());
Dan Stoza80d61162017-12-20 15:57:52 -0800768 FloatRect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700769
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000770 vec2 lt = vec2(win.left, win.top);
771 vec2 lb = vec2(win.left, win.bottom);
772 vec2 rb = vec2(win.right, win.bottom);
773 vec2 rt = vec2(win.right, win.top);
774
Peiyong Linefefaac2018-08-17 12:27:51 -0700775 ui::Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000776 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700777 lt = layerTransform.transform(lt);
778 lb = layerTransform.transform(lb);
779 rb = layerTransform.transform(rb);
780 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000781 }
782
Peiyong Lin833074a2018-08-28 11:53:54 -0700783 renderengine::Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700784 position[0] = renderAreaTransform.transform(lt);
785 position[1] = renderAreaTransform.transform(lb);
786 position[2] = renderAreaTransform.transform(rb);
787 position[3] = renderAreaTransform.transform(rt);
Mathias Agopian13127d82013-03-05 17:47:11 -0800788}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800789
David Sodman41fdfc92017-11-06 16:09:56 -0800790bool Layer::isSecure() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000791 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700792 return (s.flags & layer_state_t::eLayerSecure);
793}
794
Mathias Agopian13127d82013-03-05 17:47:11 -0800795void Layer::setVisibleRegion(const Region& visibleRegion) {
796 // always called from main thread
797 this->visibleRegion = visibleRegion;
798}
799
800void Layer::setCoveredRegion(const Region& coveredRegion) {
801 // always called from main thread
802 this->coveredRegion = coveredRegion;
803}
804
David Sodman41fdfc92017-11-06 16:09:56 -0800805void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800806 // always called from main thread
807 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
808}
809
Robert Carre5f4f692018-01-12 13:12:28 -0800810void Layer::clearVisibilityRegions() {
811 visibleRegion.clear();
812 visibleNonTransparentRegion.clear();
813 coveredRegion.clear();
814}
815
Mathias Agopian13127d82013-03-05 17:47:11 -0800816// ----------------------------------------------------------------------------
817// transaction
818// ----------------------------------------------------------------------------
819
Dan Stoza7dde5992015-05-22 09:51:44 -0700820void Layer::pushPendingState() {
821 if (!mCurrentState.modified) {
822 return;
823 }
824
Dan Stoza7dde5992015-05-22 09:51:44 -0700825 // If this transaction is waiting on the receipt of a frame, generate a sync
826 // point and send it to the remote layer.
Marissa Wallf58c14b2018-07-24 10:50:43 -0700827 if (mCurrentState.barrierLayer_legacy != nullptr) {
828 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800829 if (barrierLayer == nullptr) {
830 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700831 // If we can't promote the layer we are intended to wait on,
832 // then it is expired or otherwise invalid. Allow this transaction
833 // to be applied as per normal (no synchronization).
Marissa Wallf58c14b2018-07-24 10:50:43 -0700834 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800835 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700836 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy);
Robert Carr0d480722017-01-10 16:42:54 -0800837 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800838 mRemoteSyncPoints.push_back(std::move(syncPoint));
839 } else {
840 // We already missed the frame we're supposed to synchronize
841 // on, so go ahead and apply the state update
Marissa Wallf58c14b2018-07-24 10:50:43 -0700842 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800843 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700844 }
845
Dan Stoza7dde5992015-05-22 09:51:44 -0700846 // Wake us up to check if the frame has been received
847 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700848 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700849 }
850 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700851 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700852}
853
Pablo Ceballos05289c22016-04-14 15:49:55 -0700854void Layer::popPendingState(State* stateToCommit) {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700855 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700856
857 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700858 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700859}
860
Pablo Ceballos05289c22016-04-14 15:49:55 -0700861bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700862 bool stateUpdateAvailable = false;
863 while (!mPendingStates.empty()) {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700864 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700865 if (mRemoteSyncPoints.empty()) {
866 // If we don't have a sync point for this, apply it anyway. It
867 // will be visually wrong, but it should keep us from getting
868 // into too much trouble.
869 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700870 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700871 stateUpdateAvailable = true;
872 continue;
873 }
874
Marissa Wallf58c14b2018-07-24 10:50:43 -0700875 if (mRemoteSyncPoints.front()->getFrameNumber() !=
876 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800877 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800878
879 // Signal our end of the sync point and then dispose of it
880 mRemoteSyncPoints.front()->setTransactionApplied();
881 mRemoteSyncPoints.pop_front();
882 continue;
883 }
884
Dan Stoza7dde5992015-05-22 09:51:44 -0700885 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
886 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700887 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700888 stateUpdateAvailable = true;
889
890 // Signal our end of the sync point and then dispose of it
891 mRemoteSyncPoints.front()->setTransactionApplied();
892 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800893 } else {
894 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700895 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700896 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700897 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700898 stateUpdateAvailable = true;
899 }
900 }
901
902 // If we still have pending updates, wake SurfaceFlinger back up and point
903 // it at this layer so we can process them
904 if (!mPendingStates.empty()) {
905 setTransactionFlags(eTransactionNeeded);
906 mFlinger->setTransactionFlags(eTraversalNeeded);
907 }
908
909 mCurrentState.modified = false;
910 return stateUpdateAvailable;
911}
912
Marissa Wall61c58622018-07-18 10:12:20 -0700913uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000914 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800915
Marissa Wall61c58622018-07-18 10:12:20 -0700916 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
917 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700918
David Sodmaneb085e02017-10-05 18:49:04 -0700919 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700920 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000921 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800922 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
923 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
924 " requested={ wh={%4u,%4u} }}\n"
925 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
926 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700927 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700928 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
929 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
930 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
931 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
932 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700933 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
934 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
935 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800936 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700937
Robert Carre392b552017-09-19 12:16:05 -0700938 // Don't let Layer::doTransaction update the drawing state
939 // if we have a pending resize, unless we are in fixed-size mode.
940 // the drawing state will be updated only once we receive a buffer
941 // with the correct size.
942 //
943 // In particular, we want to make sure the clip (which is part
944 // of the geometry state) is latched together with the size but is
945 // latched immediately when no resizing is involved.
946 //
947 // If a sideband stream is attached, however, we want to skip this
948 // optimization so that transactions aren't missed when a buffer
949 // never arrives
950 //
951 // In the case that we don't have a buffer we ignore other factors
952 // and avoid entering the resizePending state. At a high level the
953 // resizePending state is to avoid applying the state of the new buffer
954 // to the old buffer. However in the state where we don't have an old buffer
955 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700956 const bool resizePending =
957 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
958 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
David Sodman0cc69182017-11-17 12:12:07 -0800959 (getBE().compositionInfo.mBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700960 if (!isFixedSize()) {
David Sodman0cc69182017-11-17 12:12:07 -0800961 if (resizePending && getBE().compositionInfo.hwc.sidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700962 flags |= eDontUpdateGeometryState;
963 }
964 }
965
Robert Carr7bf247e2017-05-18 14:02:49 -0700966 // Here we apply various requested geometry states, depending on our
967 // latching configuration. See Layer.h for a detailed discussion of
968 // how geometry latching is controlled.
969 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000970 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700971
972 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
973 // mode, which causes attributes which normally latch regardless of scaling mode,
974 // to be delayed. We copy the requested state to the active state making sure
975 // to respect these rules (again see Layer.h for a detailed discussion).
976 //
977 // There is an awkward asymmetry in the handling of the crop states in the position
978 // states, as can be seen below. Largely this arises from position and transform
979 // being stored in the same data structure while having different latching rules.
980 // b/38182305
981 //
Marissa Wall61c58622018-07-18 10:12:20 -0700982 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700983 // applyPendingStates in the presence of deferred transactions.
984 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -0700985 float tx = stateToCommit->active_legacy.transform.tx();
986 float ty = stateToCommit->active_legacy.transform.ty();
987 stateToCommit->active_legacy = stateToCommit->requested_legacy;
988 stateToCommit->active_legacy.transform.set(tx, ty);
989 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700990 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700991 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -0700992 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700993 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800994 }
995
Marissa Wall61c58622018-07-18 10:12:20 -0700996 return flags;
997}
998
999uint32_t Layer::doTransaction(uint32_t flags) {
1000 ATRACE_CALL();
1001
1002 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +00001003 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -07001004 if (!applyPendingStates(&c)) {
1005 return 0;
1006 }
1007
1008 flags = doTransactionResize(flags, &c);
1009
Alec Mourib416efd2018-09-06 21:01:59 +00001010 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -07001011
1012 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001013 // invalidate and recompute the visible regions if needed
1014 flags |= Layer::eVisibleRegion;
1015 }
1016
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001017 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001018 // invalidate and recompute the visible regions if needed
1019 flags |= eVisibleRegion;
1020 this->contentDirty = true;
1021
1022 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -07001023 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -07001024 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -08001025 }
1026
Dan Stozac8145172016-04-28 16:29:06 -07001027 // If the layer is hidden, signal and clear out all local sync points so
1028 // that transactions for layers depending on this layer's frames becoming
1029 // visible are not blocked
1030 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001031 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001032 }
1033
Mathias Agopian13127d82013-03-05 17:47:11 -08001034 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001035 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001036 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001037}
1038
Pablo Ceballos05289c22016-04-14 15:49:55 -07001039void Layer::commitTransaction(const State& stateToCommit) {
1040 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001041}
1042
Mathias Agopian13127d82013-03-05 17:47:11 -08001043uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Piquef1c675b2018-09-12 20:45:39 -07001044 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -08001045}
1046
1047uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Piquef1c675b2018-09-12 20:45:39 -07001048 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -08001049}
1050
Robert Carr82364e32016-05-15 11:27:47 -07001051bool Layer::setPosition(float x, float y, bool immediate) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001052 if (mCurrentState.requested_legacy.transform.tx() == x &&
1053 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001054 return false;
1055 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001056
1057 // We update the requested and active position simultaneously because
1058 // we want to apply the position portion of the transform matrix immediately,
1059 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Marissa Wallf58c14b2018-07-24 10:50:43 -07001060 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001061 if (immediate && !mFreezeGeometryUpdates) {
1062 // Here we directly update the active state
1063 // unlike other setters, because we store it within
1064 // the transform, but use different latching rules.
1065 // b/38182305
Marissa Wallf58c14b2018-07-24 10:50:43 -07001066 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001067 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001068 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001069
Dan Stoza7dde5992015-05-22 09:51:44 -07001070 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001071 setTransactionFlags(eTransactionNeeded);
1072 return true;
1073}
Robert Carr82364e32016-05-15 11:27:47 -07001074
Robert Carr1f0a16a2016-10-24 16:27:39 -07001075bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1076 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1077 if (idx < 0) {
1078 return false;
1079 }
1080 if (childLayer->setLayer(z)) {
1081 mCurrentChildren.removeAt(idx);
1082 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001083 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001084 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001085 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001086}
1087
Robert Carr503c7042017-09-27 15:06:08 -07001088bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1089 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1090 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1091 if (idx < 0) {
1092 return false;
1093 }
1094 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1095 mCurrentChildren.removeAt(idx);
1096 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001097 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001098 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001099 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001100}
1101
Robert Carrae060832016-11-28 10:51:00 -08001102bool Layer::setLayer(int32_t z) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001103 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001104 mCurrentState.sequence++;
1105 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001106 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001107
1108 // Discard all relative layering.
1109 if (mCurrentState.zOrderRelativeOf != nullptr) {
1110 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1111 if (strongRelative != nullptr) {
1112 strongRelative->removeZOrderRelative(this);
1113 }
1114 mCurrentState.zOrderRelativeOf = nullptr;
1115 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001116 setTransactionFlags(eTransactionNeeded);
1117 return true;
1118}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001119
Robert Carrdb66e622017-04-10 16:55:57 -07001120void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1121 mCurrentState.zOrderRelatives.remove(relative);
1122 mCurrentState.sequence++;
1123 mCurrentState.modified = true;
1124 setTransactionFlags(eTransactionNeeded);
1125}
1126
1127void Layer::addZOrderRelative(const wp<Layer>& relative) {
1128 mCurrentState.zOrderRelatives.add(relative);
1129 mCurrentState.modified = true;
1130 mCurrentState.sequence++;
1131 setTransactionFlags(eTransactionNeeded);
1132}
1133
Robert Carr503d2bd2017-12-04 15:49:47 -08001134bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001135 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1136 if (handle == nullptr) {
1137 return false;
1138 }
1139 sp<Layer> relative = handle->owner.promote();
1140 if (relative == nullptr) {
1141 return false;
1142 }
1143
Robert Carr503d2bd2017-12-04 15:49:47 -08001144 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1145 mCurrentState.zOrderRelativeOf == relative) {
1146 return false;
1147 }
1148
Robert Carrdb66e622017-04-10 16:55:57 -07001149 mCurrentState.sequence++;
1150 mCurrentState.modified = true;
Robert Carr503d2bd2017-12-04 15:49:47 -08001151 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001152
chaviw9ab4bd12017-11-03 13:11:00 -07001153 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
1154 if (oldZOrderRelativeOf != nullptr) {
1155 oldZOrderRelativeOf->removeZOrderRelative(this);
1156 }
Robert Carrdb66e622017-04-10 16:55:57 -07001157 mCurrentState.zOrderRelativeOf = relative;
1158 relative->addZOrderRelative(this);
1159
1160 setTransactionFlags(eTransactionNeeded);
1161
1162 return true;
1163}
1164
Mathias Agopian13127d82013-03-05 17:47:11 -08001165bool Layer::setSize(uint32_t w, uint32_t h) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001166 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
1167 return false;
1168 mCurrentState.requested_legacy.w = w;
1169 mCurrentState.requested_legacy.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001170 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001171 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001172
1173 // record the new size, from this point on, when the client request
1174 // a buffer, it'll get the new size.
1175 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001176 return true;
1177}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001178bool Layer::setAlpha(float alpha) {
David Sodman41fdfc92017-11-06 16:09:56 -08001179 if (mCurrentState.color.a == alpha) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001180 mCurrentState.sequence++;
chaviw13fdc492017-06-27 12:40:18 -07001181 mCurrentState.color.a = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001182 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001183 setTransactionFlags(eTransactionNeeded);
1184 return true;
1185}
chaviw13fdc492017-06-27 12:40:18 -07001186
1187bool Layer::setColor(const half3& color) {
David Sodman41fdfc92017-11-06 16:09:56 -08001188 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g &&
1189 color.b == mCurrentState.color.b)
chaviw13fdc492017-06-27 12:40:18 -07001190 return false;
1191
1192 mCurrentState.sequence++;
1193 mCurrentState.color.r = color.r;
1194 mCurrentState.color.g = color.g;
1195 mCurrentState.color.b = color.b;
1196 mCurrentState.modified = true;
1197 setTransactionFlags(eTransactionNeeded);
1198 return true;
1199}
1200
Robert Carrd4ae7f32018-06-07 16:10:57 -07001201bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1202 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001203 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001204 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1205
1206 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1207 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1208 return false;
1209 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001210 mCurrentState.sequence++;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001211 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1212 matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001213 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001214 setTransactionFlags(eTransactionNeeded);
1215 return true;
1216}
Marissa Wall61c58622018-07-18 10:12:20 -07001217
Mathias Agopian13127d82013-03-05 17:47:11 -08001218bool Layer::setTransparentRegionHint(const Region& transparent) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001219 mCurrentState.requestedTransparentRegion_legacy = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001220 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001221 setTransactionFlags(eTransactionNeeded);
1222 return true;
1223}
1224bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1225 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
David Sodman41fdfc92017-11-06 16:09:56 -08001226 if (mCurrentState.flags == newFlags) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001227 mCurrentState.sequence++;
1228 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001229 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001230 setTransactionFlags(eTransactionNeeded);
1231 return true;
1232}
Robert Carr99e27f02016-06-16 15:18:02 -07001233
Marissa Wallf58c14b2018-07-24 10:50:43 -07001234bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
1235 if (mCurrentState.requestedCrop_legacy == crop) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001236 mCurrentState.sequence++;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001237 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001238 if (immediate && !mFreezeGeometryUpdates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001239 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001240 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001241 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1242
Dan Stoza7dde5992015-05-22 09:51:44 -07001243 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001244 setTransactionFlags(eTransactionNeeded);
1245 return true;
1246}
Robert Carr8d5227b2017-03-16 15:41:03 -07001247
Robert Carrc3574f72016-03-24 12:19:32 -07001248bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001249 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001250 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001251 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001252 return true;
1253}
1254
rongliucfb187b2018-03-14 12:26:23 -07001255void Layer::setInfo(int32_t type, int32_t appId) {
David Sodman41fdfc92017-11-06 16:09:56 -08001256 mCurrentState.appId = appId;
1257 mCurrentState.type = type;
1258 mCurrentState.modified = true;
1259 setTransactionFlags(eTransactionNeeded);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001260}
1261
Mathias Agopian13127d82013-03-05 17:47:11 -08001262bool Layer::setLayerStack(uint32_t layerStack) {
David Sodman41fdfc92017-11-06 16:09:56 -08001263 if (mCurrentState.layerStack == layerStack) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001264 mCurrentState.sequence++;
1265 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001266 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001267 setTransactionFlags(eTransactionNeeded);
1268 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001269}
1270
Robert Carr1f0a16a2016-10-24 16:27:39 -07001271uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001272 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001273 if (p == nullptr) {
1274 return getDrawingState().layerStack;
1275 }
1276 return p->getLayerStack();
1277}
1278
Marissa Wallf58c14b2018-07-24 10:50:43 -07001279void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
1280 mCurrentState.barrierLayer_legacy = barrierLayer;
1281 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001282 // We don't set eTransactionNeeded, because just receiving a deferral
1283 // request without any other state updates shouldn't actually induce a delay
1284 mCurrentState.modified = true;
1285 pushPendingState();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001286 mCurrentState.barrierLayer_legacy = nullptr;
1287 mCurrentState.frameNumber_legacy = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001288 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001289}
1290
Marissa Wallf58c14b2018-07-24 10:50:43 -07001291void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001292 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001293 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001294}
1295
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001296// ----------------------------------------------------------------------------
1297// pageflip handling...
1298// ----------------------------------------------------------------------------
1299
Robert Carr1f0a16a2016-10-24 16:27:39 -07001300bool Layer::isHiddenByPolicy() const {
Alec Mourib416efd2018-09-06 21:01:59 +00001301 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001302 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001303 if (parent != nullptr && parent->isHiddenByPolicy()) {
1304 return true;
1305 }
1306 return s.flags & layer_state_t::eLayerHidden;
1307}
1308
David Sodman41fdfc92017-11-06 16:09:56 -08001309uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001310 // TODO: should we do something special if mSecure is set?
1311 if (mProtectedByApp) {
1312 // need a hardware-protected path to external video sink
1313 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001314 }
Riley Andrews03414a12014-07-01 14:22:59 -07001315 if (mPotentialCursor) {
1316 usage |= GraphicBuffer::USAGE_CURSOR;
1317 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001318 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001319 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001320}
1321
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001322void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001323 uint32_t orientation = 0;
1324 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001325 // The transform hint is used to improve performance, but we can
1326 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001327 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001328 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001329 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001330 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001331 orientation = 0;
1332 }
1333 }
David Sodmaneb085e02017-10-05 18:49:04 -07001334 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001335}
1336
Mathias Agopian13127d82013-03-05 17:47:11 -08001337// ----------------------------------------------------------------------------
1338// debugging
1339// ----------------------------------------------------------------------------
1340
Marissa Wall61c58622018-07-18 10:12:20 -07001341// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001342LayerDebugInfo Layer::getLayerDebugInfo() const {
1343 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001344 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001345 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001346 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001347 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
1348 info.mType = String8(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001349 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001350 info.mVisibleRegion = visibleRegion;
1351 info.mSurfaceDamageRegion = surfaceDamageRegion;
1352 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001353 info.mX = ds.active_legacy.transform.tx();
1354 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001355 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001356 info.mWidth = ds.active_legacy.w;
1357 info.mHeight = ds.active_legacy.h;
1358 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001359 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001360 info.mFlags = ds.flags;
1361 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001362 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001363 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1364 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1365 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1366 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001367 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001368 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001369 if (buffer != 0) {
1370 info.mActiveBufferWidth = buffer->getWidth();
1371 info.mActiveBufferHeight = buffer->getHeight();
1372 info.mActiveBufferStride = buffer->getStride();
1373 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001374 } else {
1375 info.mActiveBufferWidth = 0;
1376 info.mActiveBufferHeight = 0;
1377 info.mActiveBufferStride = 0;
1378 info.mActiveBufferFormat = 0;
1379 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001380 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001381 info.mNumQueuedFrames = getQueuedFrameCount();
1382 info.mRefreshPending = isBufferLatched();
1383 info.mIsOpaque = isOpaque(ds);
1384 info.mContentDirty = contentDirty;
1385 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001386}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001387
Dan Stozae22aec72016-08-01 13:20:59 -07001388void Layer::miniDumpHeader(String8& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001389 result.append("-------------------------------");
1390 result.append("-------------------------------");
1391 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001392 result.append(" Layer name\n");
1393 result.append(" Z | ");
1394 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001395 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001396 result.append(" Disp Frame (LTRB) | ");
1397 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001398 result.append("-------------------------------");
1399 result.append("-------------------------------");
1400 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001401}
1402
Dominik Laskowski7e045462018-05-30 13:02:02 -07001403void Layer::miniDump(String8& result, int32_t displayId) const {
1404 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stozae22aec72016-08-01 13:20:59 -07001405 return;
1406 }
1407
1408 String8 name;
1409 if (mName.length() > 77) {
1410 std::string shortened;
1411 shortened.append(mName.string(), 36);
1412 shortened.append("[...]");
1413 shortened.append(mName.string() + (mName.length() - 36), 36);
1414 name = shortened.c_str();
1415 } else {
1416 name = mName;
1417 }
1418
1419 result.appendFormat(" %s\n", name.string());
1420
Alec Mourib416efd2018-09-06 21:01:59 +00001421 const State& layerState(getDrawingState());
Lloyd Pique074e8122018-07-26 12:57:23 -07001422 const LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers.at(displayId);
Chia-I Wu1e043612018-03-01 09:45:09 -08001423 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
1424 result.appendFormat(" rel %6d | ", layerState.z);
1425 } else {
1426 result.appendFormat(" %10d | ", layerState.z);
1427 }
Dominik Laskowski7e045462018-05-30 13:02:02 -07001428 result.appendFormat("%10s | ", to_string(getCompositionType(displayId)).c_str());
Lloyd Pique074e8122018-07-26 12:57:23 -07001429 result.appendFormat("%10s | ", to_string(hwcInfo.transform).c_str());
1430 const Rect& frame = hwcInfo.displayFrame;
David Sodman41fdfc92017-11-06 16:09:56 -08001431 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique074e8122018-07-26 12:57:23 -07001432 const FloatRect& crop = hwcInfo.sourceCrop;
David Sodman41fdfc92017-11-06 16:09:56 -08001433 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 -07001434
David Sodman7e4ae112018-02-09 15:02:28 -08001435 result.append("- - - - - - - - - - - - - - - -\n");
1436
1437 std::string compositionInfoStr;
1438 getBE().compositionInfo.dump(compositionInfoStr, "compositionInfo");
1439 result.append(compositionInfoStr.c_str());
1440
Yichi Chen6ca35192018-05-29 12:20:43 +08001441 result.append("- - - - - - - - - - - - - - - -");
1442 result.append("- - - - - - - - - - - - - - - -");
1443 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001444}
Dan Stozae22aec72016-08-01 13:20:59 -07001445
Svetoslavd85084b2014-03-20 10:28:31 -07001446void Layer::dumpFrameStats(String8& result) const {
1447 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001448}
1449
Svetoslavd85084b2014-03-20 10:28:31 -07001450void Layer::clearFrameStats() {
1451 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001452}
1453
Jamie Gennis6547ff42013-07-16 20:12:42 -07001454void Layer::logFrameStats() {
1455 mFrameTracker.logAndResetStats(mName);
1456}
1457
Svetoslavd85084b2014-03-20 10:28:31 -07001458void Layer::getFrameStats(FrameStats* outStats) const {
1459 mFrameTracker.getStats(outStats);
1460}
1461
Brian Andersond6927fb2016-07-23 23:37:30 -07001462void Layer::dumpFrameEvents(String8& result) {
David Sodman41fdfc92017-11-06 16:09:56 -08001463 result.appendFormat("- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001464 Mutex::Autolock lock(mFrameEventHistoryMutex);
1465 mFrameEventHistory.checkFencesForCompletion();
1466 mFrameEventHistory.dump(result);
1467}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001468
Brian Anderson5ea5e592016-12-01 16:54:33 -08001469void Layer::onDisconnect() {
1470 Mutex::Autolock lock(mFrameEventHistoryMutex);
1471 mFrameEventHistory.onDisconnect();
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001472 mTimeStats.onDisconnect(getName().c_str());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001473}
1474
Brian Anderson3890c392016-07-25 12:48:08 -07001475void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001476 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001477 if (newTimestamps) {
1478 mTimeStats.setPostTime(getName().c_str(), newTimestamps->frameNumber,
1479 newTimestamps->postedTime);
1480 }
1481
Brian Andersond6927fb2016-07-23 23:37:30 -07001482 Mutex::Autolock lock(mFrameEventHistoryMutex);
1483 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001484 // If there are any unsignaled fences in the aquire timeline at this
1485 // point, the previously queued frame hasn't been latched yet. Go ahead
1486 // and try to get the signal time here so the syscall is taken out of
1487 // the main thread's critical path.
1488 mAcquireTimeline.updateSignalTimes();
1489 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001490 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001491 mFrameEventHistory.addQueue(*newTimestamps);
1492 }
1493
Brian Anderson3890c392016-07-25 12:48:08 -07001494 if (outDelta) {
1495 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001496 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001497}
Dan Stozae77c7662016-05-13 11:37:28 -07001498
Chia-I Wu98f1c102017-05-30 14:54:08 -07001499size_t Layer::getChildrenCount() const {
1500 size_t count = 0;
1501 for (const sp<Layer>& child : mCurrentChildren) {
1502 count += 1 + child->getChildrenCount();
1503 }
1504 return count;
1505}
1506
Robert Carr1f0a16a2016-10-24 16:27:39 -07001507void Layer::addChild(const sp<Layer>& layer) {
1508 mCurrentChildren.add(layer);
1509 layer->setParent(this);
1510}
1511
1512ssize_t Layer::removeChild(const sp<Layer>& layer) {
1513 layer->setParent(nullptr);
1514 return mCurrentChildren.remove(layer);
1515}
1516
Robert Carr1db73f62016-12-21 12:58:51 -08001517bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1518 sp<Handle> handle = nullptr;
1519 sp<Layer> newParent = nullptr;
1520 if (newParentHandle == nullptr) {
1521 return false;
1522 }
1523 handle = static_cast<Handle*>(newParentHandle.get());
1524 newParent = handle->owner.promote();
1525 if (newParent == nullptr) {
1526 ALOGE("Unable to promote Layer handle");
1527 return false;
1528 }
1529
1530 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001531 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001532
1533 sp<Client> client(child->mClientRef.promote());
1534 if (client != nullptr) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001535 client->updateParent(newParent);
Robert Carr1db73f62016-12-21 12:58:51 -08001536 }
1537 }
1538 mCurrentChildren.clear();
1539
1540 return true;
1541}
1542
Robert Carr15eae092018-03-23 13:43:53 -07001543void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001544 for (const sp<Layer>& child : mDrawingChildren) {
1545 child->mDrawingParent = newParent;
1546 }
1547}
1548
chaviwf1961f72017-09-18 16:41:07 -07001549bool Layer::reparent(const sp<IBinder>& newParentHandle) {
1550 if (newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001551 return false;
1552 }
1553
1554 auto handle = static_cast<Handle*>(newParentHandle.get());
1555 sp<Layer> newParent = handle->owner.promote();
1556 if (newParent == nullptr) {
1557 ALOGE("Unable to promote Layer handle");
1558 return false;
1559 }
1560
chaviwf1961f72017-09-18 16:41:07 -07001561 sp<Layer> parent = getParent();
1562 if (parent != nullptr) {
1563 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001564 }
chaviwf1961f72017-09-18 16:41:07 -07001565 newParent->addChild(this);
chaviw06178942017-07-27 10:25:59 -07001566
chaviwf1961f72017-09-18 16:41:07 -07001567 sp<Client> client(mClientRef.promote());
chaviw06178942017-07-27 10:25:59 -07001568 sp<Client> newParentClient(newParent->mClientRef.promote());
1569
chaviwf1961f72017-09-18 16:41:07 -07001570 if (client != newParentClient) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001571 client->updateParent(newParent);
chaviw06178942017-07-27 10:25:59 -07001572 }
1573
chaviw06178942017-07-27 10:25:59 -07001574 return true;
1575}
1576
Robert Carr9524cb32017-02-13 11:32:32 -08001577bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001578 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001579 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001580 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001581 if (client != nullptr && parentClient != client) {
Robert Carr7f619b22017-11-06 12:56:35 -08001582 client->detachLayer(child.get());
1583 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001584 }
Robert Carr7f619b22017-11-06 12:56:35 -08001585 }
Robert Carr9524cb32017-02-13 11:32:32 -08001586
1587 return true;
1588}
1589
Peiyong Lind3788632018-09-18 16:01:31 -07001590bool Layer::setColorTransform(const mat4& matrix) {
1591 if (mCurrentState.colorTransform == matrix) {
1592 return false;
1593 }
1594 ++mCurrentState.sequence;
1595 mCurrentState.colorTransform = matrix;
1596 setTransactionFlags(eTransactionNeeded);
1597 return true;
1598}
1599
1600const mat4& Layer::getColorTransform() const {
1601 return getDrawingState().colorTransform;
1602}
1603
1604bool Layer::hasColorTransform() const {
1605 static const mat4 identityMatrix = mat4();
1606 return getDrawingState().colorTransform != identityMatrix;
1607}
1608
Chia-I Wu11481472018-05-04 10:43:19 -07001609bool Layer::isLegacyDataSpace() const {
1610 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001611 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001612 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001613}
1614
Robert Carr1f0a16a2016-10-24 16:27:39 -07001615void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001616 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001617}
1618
1619void Layer::clearSyncPoints() {
1620 for (const auto& child : mCurrentChildren) {
1621 child->clearSyncPoints();
1622 }
1623
1624 Mutex::Autolock lock(mLocalSyncPointMutex);
1625 for (auto& point : mLocalSyncPoints) {
1626 point->setFrameAvailable();
1627 }
1628 mLocalSyncPoints.clear();
1629}
1630
1631int32_t Layer::getZ() const {
1632 return mDrawingState.z;
1633}
1634
Robert Carr29abff82017-12-04 13:51:20 -08001635bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1636 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1637 const State& state = useDrawing ? mDrawingState : mCurrentState;
1638 return state.zOrderRelativeOf != nullptr;
1639}
1640
David Sodman41fdfc92017-11-06 16:09:56 -08001641__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001642 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001643 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1644 "makeTraversalList received invalid stateSet");
1645 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1646 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1647 const State& state = useDrawing ? mDrawingState : mCurrentState;
1648
Robert Carr29abff82017-12-04 13:51:20 -08001649 if (state.zOrderRelatives.size() == 0) {
1650 *outSkipRelativeZUsers = true;
1651 return children;
1652 }
1653
chaviwfd462612018-05-31 16:11:27 -07001654 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001655 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001656 sp<Layer> strongRelative = weakRelative.promote();
1657 if (strongRelative != nullptr) {
1658 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001659 }
1660 }
1661
Dan Stoza412903f2017-04-27 13:42:17 -07001662 for (const sp<Layer>& child : children) {
Robert Carr503c7042017-09-27 15:06:08 -07001663 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1664 if (childState.zOrderRelativeOf != nullptr) {
1665 continue;
1666 }
Robert Carrdb66e622017-04-10 16:55:57 -07001667 traverse.add(child);
1668 }
1669
1670 return traverse;
1671}
1672
Robert Carr1f0a16a2016-10-24 16:27:39 -07001673/**
Robert Carrdb66e622017-04-10 16:55:57 -07001674 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001675 */
Dan Stoza412903f2017-04-27 13:42:17 -07001676void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001677 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1678 // produce a new list for traversing, including our relatives, and not including our children
1679 // who are relatives of another surface. In the case that there are no relative Z,
1680 // makeTraversalList returns our children directly to avoid significant overhead.
1681 // However in this case we need to take the responsibility for filtering children which
1682 // are relatives of another surface here.
1683 bool skipRelativeZUsers = false;
1684 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001685
Robert Carr1f0a16a2016-10-24 16:27:39 -07001686 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001687 for (; i < list.size(); i++) {
1688 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001689 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1690 continue;
1691 }
1692
Robert Carrdb66e622017-04-10 16:55:57 -07001693 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001694 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001695 }
Dan Stoza412903f2017-04-27 13:42:17 -07001696 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001697 }
Robert Carr29abff82017-12-04 13:51:20 -08001698
Dan Stoza412903f2017-04-27 13:42:17 -07001699 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001700 for (; i < list.size(); i++) {
1701 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001702
1703 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1704 continue;
1705 }
Dan Stoza412903f2017-04-27 13:42:17 -07001706 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001707 }
1708}
1709
1710/**
Robert Carrdb66e622017-04-10 16:55:57 -07001711 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001712 */
Dan Stoza412903f2017-04-27 13:42:17 -07001713void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1714 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001715 // See traverseInZOrder for documentation.
1716 bool skipRelativeZUsers = false;
1717 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001718
Robert Carr1f0a16a2016-10-24 16:27:39 -07001719 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001720 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001721 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001722
1723 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1724 continue;
1725 }
1726
Robert Carrdb66e622017-04-10 16:55:57 -07001727 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001728 break;
1729 }
Dan Stoza412903f2017-04-27 13:42:17 -07001730 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001731 }
Dan Stoza412903f2017-04-27 13:42:17 -07001732 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001733 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001734 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001735
1736 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1737 continue;
1738 }
1739
Dan Stoza412903f2017-04-27 13:42:17 -07001740 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001741 }
1742}
1743
chaviw4b129c22018-04-09 16:19:43 -07001744LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1745 const std::vector<Layer*>& layersInTree) {
1746 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1747 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001748 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1749 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
chaviw4b129c22018-04-09 16:19:43 -07001750 const State& state = useDrawing ? mDrawingState : mCurrentState;
1751
chaviwfd462612018-05-31 16:11:27 -07001752 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001753 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1754 sp<Layer> strongRelative = weakRelative.promote();
1755 // Only add relative layers that are also descendents of the top most parent of the tree.
1756 // If a relative layer is not a descendent, then it should be ignored.
1757 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1758 traverse.add(strongRelative);
1759 }
1760 }
1761
1762 for (const sp<Layer>& child : children) {
1763 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1764 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1765 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1766 // the child here since it won't be added later as a relative.
1767 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1768 childState.zOrderRelativeOf.promote().get())) {
1769 continue;
1770 }
1771 traverse.add(child);
1772 }
1773
1774 return traverse;
1775}
1776
1777void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1778 LayerVector::StateSet stateSet,
1779 const LayerVector::Visitor& visitor) {
1780 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001781
1782 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001783 for (; i < list.size(); i++) {
1784 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001785 if (relative->getZ() >= 0) {
1786 break;
1787 }
chaviw4b129c22018-04-09 16:19:43 -07001788 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001789 }
chaviw4b129c22018-04-09 16:19:43 -07001790
chaviwa76b2712017-09-20 12:02:26 -07001791 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001792 for (; i < list.size(); i++) {
1793 const auto& relative = list[i];
1794 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001795 }
1796}
1797
chaviw4b129c22018-04-09 16:19:43 -07001798std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1799 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1800 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1801
1802 std::vector<Layer*> layersInTree = {this};
1803 for (size_t i = 0; i < children.size(); i++) {
1804 const auto& child = children[i];
1805 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1806 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1807 }
1808
1809 return layersInTree;
1810}
1811
1812void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1813 const LayerVector::Visitor& visitor) {
1814 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1815 std::sort(layersInTree.begin(), layersInTree.end());
1816 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1817}
1818
Peiyong Linefefaac2018-08-17 12:27:51 -07001819ui::Transform Layer::getTransform() const {
1820 ui::Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001821 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001822 if (p != nullptr) {
1823 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07001824
1825 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
1826 // it isFixedSize) then there may be additional scaling not accounted
1827 // for in the transform. We need to mirror this scaling in child surfaces
1828 // or we will break the contract where WM can treat child surfaces as
1829 // pixels in the parent surface.
David Sodman0cc69182017-11-17 12:12:07 -08001830 if (p->isFixedSize() && p->getBE().compositionInfo.mBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07001831 int bufferWidth;
1832 int bufferHeight;
1833 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
David Sodman0cc69182017-11-17 12:12:07 -08001834 bufferWidth = p->getBE().compositionInfo.mBuffer->getWidth();
1835 bufferHeight = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001836 } else {
David Sodman0cc69182017-11-17 12:12:07 -08001837 bufferHeight = p->getBE().compositionInfo.mBuffer->getWidth();
1838 bufferWidth = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001839 }
Marissa Wall61c58622018-07-18 10:12:20 -07001840 float sx = p->getActiveWidth(p->getDrawingState()) / static_cast<float>(bufferWidth);
1841 float sy = p->getActiveHeight(p->getDrawingState()) / static_cast<float>(bufferHeight);
Peiyong Linefefaac2018-08-17 12:27:51 -07001842 ui::Transform extraParentScaling;
Robert Carr9b429f42017-04-17 14:56:57 -07001843 extraParentScaling.set(sx, 0, 0, sy);
1844 t = t * extraParentScaling;
1845 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001846 }
Marissa Wall61c58622018-07-18 10:12:20 -07001847 return t * getActiveTransform(getDrawingState());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001848}
1849
chaviw13fdc492017-06-27 12:40:18 -07001850half Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001851 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07001852
chaviw13fdc492017-06-27 12:40:18 -07001853 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1854 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001855}
Robert Carr6452f122017-03-21 10:41:29 -07001856
chaviw13fdc492017-06-27 12:40:18 -07001857half4 Layer::getColor() const {
1858 const half4 color(getDrawingState().color);
1859 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001860}
Robert Carr6452f122017-03-21 10:41:29 -07001861
Robert Carr1f0a16a2016-10-24 16:27:39 -07001862void Layer::commitChildList() {
1863 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1864 const auto& child = mCurrentChildren[i];
1865 child->commitChildList();
1866 }
1867 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001868 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001869}
1870
chaviw1d044282017-09-27 12:19:28 -07001871void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
1872 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1873 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1874 const State& state = useDrawing ? mDrawingState : mCurrentState;
1875
Peiyong Linefefaac2018-08-17 12:27:51 -07001876 ui::Transform requestedTransform = state.active_legacy.transform;
1877 ui::Transform transform = getTransform();
chaviw1d044282017-09-27 12:19:28 -07001878
1879 layerInfo->set_id(sequence);
1880 layerInfo->set_name(getName().c_str());
1881 layerInfo->set_type(String8(getTypeId()));
1882
1883 for (const auto& child : children) {
1884 layerInfo->add_children(child->sequence);
1885 }
1886
1887 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1888 sp<Layer> strongRelative = weakRelative.promote();
1889 if (strongRelative != nullptr) {
1890 layerInfo->add_relatives(strongRelative->sequence);
1891 }
1892 }
1893
Marissa Wallf58c14b2018-07-24 10:50:43 -07001894 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
chaviw1d044282017-09-27 12:19:28 -07001895 layerInfo->mutable_transparent_region());
1896 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
1897 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
1898
1899 layerInfo->set_layer_stack(getLayerStack());
1900 layerInfo->set_z(state.z);
1901
1902 PositionProto* position = layerInfo->mutable_position();
1903 position->set_x(transform.tx());
1904 position->set_y(transform.ty());
1905
1906 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
1907 requestedPosition->set_x(requestedTransform.tx());
1908 requestedPosition->set_y(requestedTransform.ty());
1909
1910 SizeProto* size = layerInfo->mutable_size();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001911 size->set_w(state.active_legacy.w);
1912 size->set_h(state.active_legacy.h);
chaviw1d044282017-09-27 12:19:28 -07001913
Marissa Wallf58c14b2018-07-24 10:50:43 -07001914 LayerProtoHelper::writeToProto(state.crop_legacy, layerInfo->mutable_crop());
chaviw1d044282017-09-27 12:19:28 -07001915
1916 layerInfo->set_is_opaque(isOpaque(state));
1917 layerInfo->set_invalidate(contentDirty);
Chia-I Wu01591c92018-05-22 12:03:00 -07001918
1919 // XXX (b/79210409) mCurrentDataSpace is not protected
1920 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
1921
chaviw1d044282017-09-27 12:19:28 -07001922 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1923 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
1924 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
1925 layerInfo->set_flags(state.flags);
1926
1927 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1928 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
1929
Jorim Jaggi8e0af362017-11-14 16:28:28 +01001930 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07001931 if (parent != nullptr) {
1932 layerInfo->set_parent(parent->sequence);
1933 }
1934
1935 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1936 if (zOrderRelativeOf != nullptr) {
1937 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1938 }
1939
Chia-I Wu01591c92018-05-22 12:03:00 -07001940 // XXX getBE().compositionInfo.mBuffer is not protected
David Sodman0cc69182017-11-17 12:12:07 -08001941 auto buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001942 if (buffer != nullptr) {
1943 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
Peiyong Linefefaac2018-08-17 12:27:51 -07001944 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
Yichi Chen6ca35192018-05-29 12:20:43 +08001945 layerInfo->mutable_buffer_transform());
chaviw1d044282017-09-27 12:19:28 -07001946 }
1947
1948 layerInfo->set_queued_frames(getQueuedFrameCount());
1949 layerInfo->set_refresh_pending(isBufferLatched());
rongliucfb187b2018-03-14 12:26:23 -07001950 layerInfo->set_window_type(state.type);
1951 layerInfo->set_app_id(state.appId);
chaviwadc40c22018-07-10 16:57:27 -07001952 layerInfo->set_curr_frame(mCurrentFrameNumber);
1953
1954 for (const auto& pendingState : mPendingStates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001955 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
chaviwadc40c22018-07-10 16:57:27 -07001956 if (barrierLayer != nullptr) {
1957 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
1958 barrierLayerProto->set_id(barrierLayer->sequence);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001959 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
chaviwadc40c22018-07-10 16:57:27 -07001960 }
1961 }
chaviw1d044282017-09-27 12:19:28 -07001962}
1963
Dominik Laskowski7e045462018-05-30 13:02:02 -07001964void Layer::writeToProto(LayerProto* layerInfo, int32_t displayId) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07001965 if (!hasHwcLayer(displayId)) {
1966 return;
1967 }
1968
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001969 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
1970
Dominik Laskowski7e045462018-05-30 13:02:02 -07001971 const auto& hwcInfo = getBE().mHwcLayers.at(displayId);
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001972
1973 const Rect& frame = hwcInfo.displayFrame;
1974 LayerProtoHelper::writeToProto(frame, layerInfo->mutable_hwc_frame());
1975
1976 const FloatRect& crop = hwcInfo.sourceCrop;
1977 LayerProtoHelper::writeToProto(crop, layerInfo->mutable_hwc_crop());
1978
1979 const int32_t transform = static_cast<int32_t>(hwcInfo.transform);
1980 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08001981
1982 const int32_t compositionType = static_cast<int32_t>(hwcInfo.compositionType);
1983 layerInfo->set_hwc_composition_type(compositionType);
1984
1985 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
1986 static_cast<BufferLayer*>(this)->isProtected()) {
1987 layerInfo->set_is_protected(true);
1988 } else {
1989 layerInfo->set_is_protected(false);
1990 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001991}
1992
Mathias Agopian13127d82013-03-05 17:47:11 -08001993// ---------------------------------------------------------------------------
1994
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001995}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07001996
1997#if defined(__gl_h_)
1998#error "don't include gl/gl.h in this file"
1999#endif
2000
2001#if defined(__gl2_h_)
2002#error "don't include gl2/gl2.h in this file"
2003#endif