blob: c1f0587b3c41aa9fea0f2aee3ae1884cd597a865 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dan Stoza9e56aa02015-11-02 13:00:03 -080017//#define LOG_NDEBUG 0
18#undef LOG_TAG
19#define LOG_TAG "Layer"
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080020#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21
Mathias Agopian13127d82013-03-05 17:47:11 -080022#include <math.h>
David Sodman41fdfc92017-11-06 16:09:56 -080023#include <stdint.h>
24#include <stdlib.h>
25#include <sys/types.h>
chaviw4b129c22018-04-09 16:19:43 -070026#include <algorithm>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027
Mathias Agopiana67932f2011-04-20 14:20:59 -070028#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070029#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070030#include <cutils/properties.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080031
32#include <utils/Errors.h>
33#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080034#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080036#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037
Courtney Goeltzenleuchter36c44dc2017-04-14 09:33:16 -060038#include <ui/DebugUtils.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070039#include <ui/GraphicBuffer.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080040#include <ui/PixelFormat.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080041
Dan Stoza6b9454d2014-11-07 16:00:59 -080042#include <gui/BufferItem.h>
Kalle Raitaa099a242017-01-11 11:17:29 -080043#include <gui/LayerDebugInfo.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080044#include <gui/Surface.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045
Yiwei Zhang60d1a192018-03-07 14:52:28 -080046#include "BufferLayer.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020047#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070048#include "DisplayDevice.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include "Layer.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070050#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070051#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053
Mathias Agopian1b031492012-06-20 17:51:20 -070054#include "DisplayHardware/HWComposer.h"
55
Peiyong Lincbc184f2018-08-22 13:24:10 -070056#include <renderengine/RenderEngine.h>
Mathias Agopian875d8e12013-06-07 15:35:48 -070057
Dan Stozac5da2712016-07-20 15:38:12 -070058#include <mutex>
chaviw1d044282017-09-27 12:19:28 -070059#include "LayerProtoHelper.h"
Dan Stozac5da2712016-07-20 15:38:12 -070060
David Sodman41fdfc92017-11-06 16:09:56 -080061#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063namespace android {
64
Lloyd Piquef1c675b2018-09-12 20:45:39 -070065std::atomic<int32_t> Layer::sSequence{1};
Mathias Agopian13127d82013-03-05 17:47:11 -080066
Lloyd Pique42ab75e2018-09-12 20:46:03 -070067Layer::Layer(const LayerCreationArgs& args)
68 : mFlinger(args.flinger),
69 mName(args.name),
70 mClientRef(args.client),
71 mBE{this, args.name.string()} {
Mathias Agopiana67932f2011-04-20 14:20:59 -070072 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070073
74 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -070075 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
76 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
77 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070078
Dan Stozaf7ba41a2017-05-10 15:11:11 -070079 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070080
Lloyd Pique42ab75e2018-09-12 20:46:03 -070081 mCurrentState.active_legacy.w = args.w;
82 mCurrentState.active_legacy.h = args.h;
David Sodman0c69cad2017-08-21 12:12:51 -070083 mCurrentState.flags = layerFlags;
Marissa Wallf58c14b2018-07-24 10:50:43 -070084 mCurrentState.active_legacy.transform.set(0, 0);
85 mCurrentState.crop_legacy.makeInvalid();
Marissa Wallf58c14b2018-07-24 10:50:43 -070086 mCurrentState.requestedCrop_legacy = mCurrentState.crop_legacy;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070087 mCurrentState.z = 0;
chaviw13fdc492017-06-27 12:40:18 -070088 mCurrentState.color.a = 1.0f;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070089 mCurrentState.layerStack = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070090 mCurrentState.sequence = 0;
Marissa Wallf58c14b2018-07-24 10:50:43 -070091 mCurrentState.requested_legacy = mCurrentState.active_legacy;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -050092 mCurrentState.appId = 0;
93 mCurrentState.type = 0;
Marissa Wall61c58622018-07-18 10:12:20 -070094 mCurrentState.active.w = 0;
95 mCurrentState.active.h = 0;
96 mCurrentState.active.transform.set(0, 0);
97 mCurrentState.transform = 0;
98 mCurrentState.transformToDisplayInverse = false;
99 mCurrentState.crop.makeInvalid();
100 mCurrentState.acquireFence = new Fence(-1);
101 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
102 mCurrentState.hdrMetadata.validTypes = 0;
103 mCurrentState.surfaceDamageRegion.clear();
104 mCurrentState.api = -1;
Peiyong Lin747321c2018-10-01 10:03:11 -0700105 mCurrentState.hasColorTransform = false;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700106
107 // drawing state & current state are identical
108 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700109
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800110 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700111 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800112 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200113 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700114
115 mFlinger->onLayerCreated();
Dan Stoza436ccf32018-06-21 12:10:12 -0700116}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700117
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700118Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800119 sp<Client> c(mClientRef.promote());
120 if (c != 0) {
121 c->detachLayer(this);
122 }
123
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000124 mFrameTracker.logAndResetStats(mName);
Robert Carr2e102c92018-10-23 12:11:15 -0700125
126 destroyAllHwcLayers();
127
128 mFlinger->onLayerDestroyed();
Mathias Agopian96f08192010-06-02 23:28:45 -0700129}
130
Mathias Agopian13127d82013-03-05 17:47:11 -0800131// ---------------------------------------------------------------------------
132// callbacks
133// ---------------------------------------------------------------------------
134
David Sodmaneb085e02017-10-05 18:49:04 -0700135/*
136 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
137 * Layer. So, the implementation is done in BufferLayer. When called on a
138 * ColorLayer object, it's essentially a NOP.
139 */
David Sodmaneb085e02017-10-05 18:49:04 -0700140void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800141
Chia-I Wuc6657022017-08-15 11:18:17 -0700142void Layer::onRemovedFromCurrentState() {
Robert Carr2e102c92018-10-23 12:11:15 -0700143 mRemovedFromCurrentState = true;
144
Chia-I Wuc6657022017-08-15 11:18:17 -0700145 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
Robert Carr5edb1ad2017-04-25 10:54:24 -0700146 if (mCurrentState.zOrderRelativeOf != nullptr) {
147 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
148 if (strongRelative != nullptr) {
149 strongRelative->removeZOrderRelative(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700150 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700151 }
152 mCurrentState.zOrderRelativeOf = nullptr;
153 }
Rob Carr4bba3702018-10-08 21:53:30 +0000154
Robert Carr2e102c92018-10-23 12:11:15 -0700155 // Since we are no longer reachable from CurrentState SurfaceFlinger
156 // will no longer invoke doTransaction for us, and so we will
157 // never finish applying transactions. We signal the sync point
158 // now so that another layer will not become indefinitely
159 // blocked.
160 for (auto& point: mRemoteSyncPoints) {
161 point->setTransactionApplied();
162 }
163 mRemoteSyncPoints.clear();
164
165 {
166 Mutex::Autolock syncLock(mLocalSyncPointMutex);
167 for (auto& point : mLocalSyncPoints) {
168 point->setFrameAvailable();
169 }
170 mLocalSyncPoints.clear();
171 }
172
Chia-I Wuc6657022017-08-15 11:18:17 -0700173 for (const auto& child : mCurrentChildren) {
174 child->onRemovedFromCurrentState();
175 }
176}
Chia-I Wu38512252017-05-17 14:36:16 -0700177
Mathias Agopian13127d82013-03-05 17:47:11 -0800178// ---------------------------------------------------------------------------
179// set-up
180// ---------------------------------------------------------------------------
181
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700182const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800183 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184}
185
chaviw13fdc492017-06-27 12:40:18 -0700186bool Layer::getPremultipledAlpha() const {
187 return mPremultipliedAlpha;
188}
189
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700190sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800191 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700192 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800193}
194
195// ---------------------------------------------------------------------------
196// h/w composer set-up
197// ---------------------------------------------------------------------------
198
Dominik Laskowski7e045462018-05-30 13:02:02 -0700199bool Layer::createHwcLayer(HWComposer* hwc, int32_t displayId) {
200 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.count(displayId) != 0,
201 "Already have a layer for display %d", displayId);
David Sodmanb8aaea12017-12-14 15:54:51 -0800202 auto layer = std::shared_ptr<HWC2::Layer>(
203 hwc->createLayer(displayId),
204 [hwc, displayId](HWC2::Layer* layer) {
205 hwc->destroyLayer(displayId, layer); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700206 if (!layer) {
207 return false;
208 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700209 LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers[displayId];
Steven Thomasb02664d2017-07-26 18:48:28 -0700210 hwcInfo.hwc = hwc;
211 hwcInfo.layer = layer;
David Sodmanf6a38932018-05-25 15:27:50 -0700212 layer->setLayerDestroyedListener(
Dominik Laskowski7e045462018-05-30 13:02:02 -0700213 [this, displayId](HWC2::Layer* /*layer*/) { getBE().mHwcLayers.erase(displayId); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700214 return true;
215}
216
Dominik Laskowski7e045462018-05-30 13:02:02 -0700217bool Layer::destroyHwcLayer(int32_t displayId) {
218 if (getBE().mHwcLayers.count(displayId) == 0) {
Chia-I Wu83806892017-11-16 10:50:20 -0800219 return false;
Steven Thomasb02664d2017-07-26 18:48:28 -0700220 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700221 auto& hwcInfo = getBE().mHwcLayers[displayId];
David Sodman41fdfc92017-11-06 16:09:56 -0800222 LOG_ALWAYS_FATAL_IF(hwcInfo.layer == nullptr, "Attempt to destroy null layer");
Steven Thomasb02664d2017-07-26 18:48:28 -0700223 LOG_ALWAYS_FATAL_IF(hwcInfo.hwc == nullptr, "Missing HWComposer");
David Sodmanb8aaea12017-12-14 15:54:51 -0800224 hwcInfo.layer = nullptr;
225
Chia-I Wu83806892017-11-16 10:50:20 -0800226 return true;
Steven Thomasb02664d2017-07-26 18:48:28 -0700227}
228
229void Layer::destroyAllHwcLayers() {
David Sodman6f65f3e2017-11-03 14:28:09 -0700230 size_t numLayers = getBE().mHwcLayers.size();
Steven Thomasb02664d2017-07-26 18:48:28 -0700231 for (size_t i = 0; i < numLayers; ++i) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700232 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.empty(), "destroyAllHwcLayers failed");
233 destroyHwcLayer(getBE().mHwcLayers.begin()->first);
Steven Thomasb02664d2017-07-26 18:48:28 -0700234 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700235 LOG_ALWAYS_FATAL_IF(!getBE().mHwcLayers.empty(),
David Sodman41fdfc92017-11-06 16:09:56 -0800236 "All hardware composer layers should have been destroyed");
Robert Carr2e102c92018-10-23 12:11:15 -0700237
238 for (const sp<Layer>& child : mDrawingChildren) {
239 child->destroyAllHwcLayers();
240 }
Steven Thomasb02664d2017-07-26 18:48:28 -0700241}
Steven Thomasb02664d2017-07-26 18:48:28 -0700242
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800243Rect Layer::getContentCrop() const {
244 // this is the crop rectangle that applies to the buffer
245 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700246 Rect crop;
247 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800248 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700249 crop = mCurrentCrop;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800250 } else if (getBE().compositionInfo.mBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800251 // otherwise we use the whole buffer
David Sodman0cc69182017-11-17 12:12:07 -0800252 crop = getBE().compositionInfo.mBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700253 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800254 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700255 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700256 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700257 return crop;
258}
259
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700260static Rect reduce(const Rect& win, const Region& exclude) {
261 if (CC_LIKELY(exclude.isEmpty())) {
262 return win;
263 }
264 if (exclude.isRect()) {
265 return win.reduce(exclude.getBounds());
266 }
267 return Region(win).subtract(exclude).getBounds();
268}
269
Dan Stoza80d61162017-12-20 15:57:52 -0800270static FloatRect reduce(const FloatRect& win, const Region& exclude) {
271 if (CC_LIKELY(exclude.isEmpty())) {
272 return win;
273 }
274 // Convert through Rect (by rounding) for lack of FloatRegion
275 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
276}
277
Robert Carr1f0a16a2016-10-24 16:27:39 -0700278Rect Layer::computeScreenBounds(bool reduceTransparentRegion) const {
Alec Mourib416efd2018-09-06 21:01:59 +0000279 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700280 Rect win(getActiveWidth(s), getActiveHeight(s));
Robert Carr1f0a16a2016-10-24 16:27:39 -0700281
Marissa Wall61c58622018-07-18 10:12:20 -0700282 Rect crop = getCrop(s);
283 if (!crop.isEmpty()) {
284 win.intersect(crop, &win);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700285 }
286
Peiyong Linefefaac2018-08-17 12:27:51 -0700287 ui::Transform t = getTransform();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700288 win = t.transform(win);
289
Chia-I Wue41dbe62017-06-13 14:10:56 -0700290 const sp<Layer>& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700291 // Now we need to calculate the parent bounds, so we can clip ourselves to those.
292 // When calculating the parent bounds for purposes of clipping,
293 // we don't need to constrain the parent to its transparent region.
294 // The transparent region is an optimization based on the
295 // buffer contents of the layer, but does not affect the space allocated to
296 // it by policy, and thus children should be allowed to extend into the
297 // parent's transparent region. In fact one of the main uses, is to reduce
298 // buffer allocation size in cases where a child window sits behind a main window
299 // (by marking the hole in the parent window as a transparent region)
300 if (p != nullptr) {
301 Rect bounds = p->computeScreenBounds(false);
302 bounds.intersect(win, &win);
303 }
304
305 if (reduceTransparentRegion) {
Marissa Wall61c58622018-07-18 10:12:20 -0700306 auto const screenTransparentRegion = t.transform(getActiveTransparentRegion(s));
Robert Carr1f0a16a2016-10-24 16:27:39 -0700307 win = reduce(win, screenTransparentRegion);
308 }
309
310 return win;
311}
312
Dan Stoza80d61162017-12-20 15:57:52 -0800313FloatRect Layer::computeBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000314 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700315 return computeBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700316}
317
Dan Stoza80d61162017-12-20 15:57:52 -0800318FloatRect Layer::computeBounds(const Region& activeTransparentRegion) const {
Alec Mourib416efd2018-09-06 21:01:59 +0000319 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700320 Rect win(getActiveWidth(s), getActiveHeight(s));
Robert Carrb5d3d262016-03-25 15:08:13 -0700321
Marissa Wall61c58622018-07-18 10:12:20 -0700322 Rect crop = getCrop(s);
323 if (!crop.isEmpty()) {
324 win.intersect(crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800325 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700326
Chia-I Wue41dbe62017-06-13 14:10:56 -0700327 const auto& p = mDrawingParent.promote();
Robert Carrd4ae7f32018-06-07 16:10:57 -0700328 FloatRect floatWin = win.toFloatRect();
329 FloatRect parentBounds = floatWin;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700330 if (p != nullptr) {
Robert Carrd4ae7f32018-06-07 16:10:57 -0700331 // We pass an empty Region here for reasons mirroring that of the case described in
332 // the computeScreenBounds reduceTransparentRegion=false case.
333 parentBounds = p->computeBounds(Region());
Robert Carr1f0a16a2016-10-24 16:27:39 -0700334 }
335
Peiyong Linefefaac2018-08-17 12:27:51 -0700336 ui::Transform t = s.active_legacy.transform;
Dan Stoza80d61162017-12-20 15:57:52 -0800337
Vishnu Nairdcce0e22018-08-23 08:35:19 -0700338 if (p != nullptr) {
Dan Stoza80d61162017-12-20 15:57:52 -0800339 floatWin = t.transform(floatWin);
Robert Carrd4ae7f32018-06-07 16:10:57 -0700340 floatWin = floatWin.intersect(parentBounds);
Dan Stoza80d61162017-12-20 15:57:52 -0800341 floatWin = t.inverse().transform(floatWin);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700342 }
343
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700344 // subtract the transparent region and snap to the bounds
Dan Stoza80d61162017-12-20 15:57:52 -0800345 return reduce(floatWin, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800346}
347
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700348Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& display) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700349 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800350 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700351 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800352
353 // apply the projection's clipping to the window crop in
354 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700355 // if there are no window scaling involved, this operation will map to full
356 // pixels in the buffer.
357 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
358 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700359
Marissa Wall61c58622018-07-18 10:12:20 -0700360 Rect activeCrop(getActiveWidth(s), getActiveHeight(s));
361 Rect crop = getCrop(s);
362 if (!crop.isEmpty()) {
363 activeCrop.intersect(crop, &activeCrop);
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700364 }
365
Peiyong Linefefaac2018-08-17 12:27:51 -0700366 ui::Transform t = getTransform();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700367 activeCrop = t.transform(activeCrop);
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700368 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000369 activeCrop.clear();
370 }
chaviwb1154d12017-10-31 14:15:36 -0700371
372 const auto& p = mDrawingParent.promote();
373 if (p != nullptr) {
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700374 auto parentCrop = p->computeInitialCrop(display);
chaviwb1154d12017-10-31 14:15:36 -0700375 activeCrop.intersect(parentCrop, &activeCrop);
376 }
377
Robert Carr1f0a16a2016-10-24 16:27:39 -0700378 return activeCrop;
379}
380
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700381FloatRect Layer::computeCrop(const sp<const DisplayDevice>& display) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700382 // the content crop is the area of the content that gets scaled to the
383 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800384 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700385
386 // In addition there is a WM-specified crop we pull from our drawing state.
387 const State& s(getDrawingState());
388
389 // Screen space to make reduction to parent crop clearer.
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700390 Rect activeCrop = computeInitialCrop(display);
Peiyong Linefefaac2018-08-17 12:27:51 -0700391 ui::Transform t = getTransform();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700392 // Back to layer space to work with the content crop.
393 activeCrop = t.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800394
Michael Lentine28ea2172014-11-19 18:32:37 -0800395 // This needs to be here as transform.transform(Rect) computes the
396 // transformed rect and then takes the bounding box of the result before
397 // returning. This means
398 // transform.inverse().transform(transform.transform(Rect)) != Rect
399 // in which case we need to make sure the final rect is clipped to the
400 // display bounds.
Marissa Wall61c58622018-07-18 10:12:20 -0700401 if (!activeCrop.intersect(Rect(getActiveWidth(s), getActiveHeight(s)), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000402 activeCrop.clear();
403 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800404
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700405 // subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700406 activeCrop = reduce(activeCrop, getActiveTransparentRegion(s));
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700407
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000408 // Transform the window crop to match the buffer coordinate system,
409 // which means using the inverse of the current transform set on the
410 // SurfaceFlingerConsumer.
411 uint32_t invTransform = mCurrentTransform;
Robert Carrcae605c2017-03-29 12:10:31 -0700412 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000413 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700414 * the code below applies the primary display's inverse transform to the
415 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000416 */
David Sodman41fdfc92017-11-06 16:09:56 -0800417 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000418 // calculate the inverse transform
419 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800420 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800421 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000422 // and apply to the current transform
Peiyong Linefefaac2018-08-17 12:27:51 -0700423 invTransform = (ui::Transform(invTransformOrient) *
424 ui::Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800425 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000426
Marissa Wall61c58622018-07-18 10:12:20 -0700427 int winWidth = getActiveWidth(s);
428 int winHeight = getActiveHeight(s);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000429 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
430 // If the activeCrop has been rotate the ends are rotated but not
431 // the space itself so when transforming ends back we can't rely on
432 // a modification of the axes of rotation. To account for this we
433 // need to reorient the inverse rotation in terms of the current
434 // axes of rotation.
435 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
436 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
437 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800438 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000439 }
Marissa Wall61c58622018-07-18 10:12:20 -0700440 winWidth = getActiveHeight(s);
441 winHeight = getActiveWidth(s);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000442 }
Marissa Wall61c58622018-07-18 10:12:20 -0700443 const Rect winCrop = activeCrop.transform(invTransform, getActiveWidth(s), getActiveHeight(s));
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000444
445 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800446 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000447 float yScale = crop.getHeight() / float(winHeight);
448
David Sodman41fdfc92017-11-06 16:09:56 -0800449 float insetL = winCrop.left * xScale;
450 float insetT = winCrop.top * yScale;
451 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000452 float insetB = (winHeight - winCrop.bottom) * yScale;
453
David Sodman41fdfc92017-11-06 16:09:56 -0800454 crop.left += insetL;
455 crop.top += insetT;
456 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000457 crop.bottom -= insetB;
458
Mathias Agopian13127d82013-03-05 17:47:11 -0800459 return crop;
460}
461
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700462void Layer::setGeometry(const sp<const DisplayDevice>& display, uint32_t z) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700463 const auto displayId = display->getId();
Peiyong Lin91b1df22018-06-18 18:00:16 -0700464 if (!hasHwcLayer(displayId)) {
465 ALOGE("[%s] failed to setGeometry: no HWC layer found (%d)",
466 mName.string(), displayId);
467 return;
468 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700469 auto& hwcInfo = getBE().mHwcLayers[displayId];
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700470
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700471 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800472 hwcInfo.forceClientComposition = false;
473
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700474 if (isSecure() && !display->isSecure()) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800475 hwcInfo.forceClientComposition = true;
476 }
477
David Sodman15094112018-10-11 09:39:37 -0700478 auto& hwcLayer = hwcInfo.layer;
479
Mathias Agopian13127d82013-03-05 17:47:11 -0800480 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700481 const State& s(getDrawingState());
David Revemanecf0fa52017-03-03 11:32:44 -0500482 auto blendMode = HWC2::BlendMode::None;
Robert Carr6452f122017-03-21 10:41:29 -0700483 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800484 blendMode =
485 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800486 }
David Sodman15094112018-10-11 09:39:37 -0700487 auto error = hwcLayer->setBlendMode(blendMode);
488 ALOGE_IF(error != HWC2::Error::None,
489 "[%s] Failed to set blend mode %s:"
490 " %s (%d)",
491 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
492 static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700493 getBE().compositionInfo.hwc.blendMode = blendMode;
Mathias Agopian13127d82013-03-05 17:47:11 -0800494
495 // apply the layer's transform, followed by the display's global transform
496 // here we're guaranteed that the layer's transform preserves rects
Marissa Wall61c58622018-07-18 10:12:20 -0700497 Region activeTransparentRegion(getActiveTransparentRegion(s));
Peiyong Linefefaac2018-08-17 12:27:51 -0700498 ui::Transform t = getTransform();
Marissa Wall61c58622018-07-18 10:12:20 -0700499 Rect activeCrop = getCrop(s);
500 if (!activeCrop.isEmpty()) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700501 activeCrop = t.transform(activeCrop);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700502 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000503 activeCrop.clear();
504 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700505 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800506 // This needs to be here as transform.transform(Rect) computes the
507 // transformed rect and then takes the bounding box of the result before
508 // returning. This means
509 // transform.inverse().transform(transform.transform(Rect)) != Rect
510 // in which case we need to make sure the final rect is clipped to the
511 // display bounds.
Marissa Wall61c58622018-07-18 10:12:20 -0700512 if (!activeCrop.intersect(Rect(getActiveWidth(s), getActiveHeight(s)), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000513 activeCrop.clear();
514 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700515 // mark regions outside the crop as transparent
Marissa Wall61c58622018-07-18 10:12:20 -0700516 activeTransparentRegion.orSelf(Rect(0, 0, getActiveWidth(s), activeCrop.top));
Marissa Wallf58c14b2018-07-24 10:50:43 -0700517 activeTransparentRegion.orSelf(
Marissa Wall61c58622018-07-18 10:12:20 -0700518 Rect(0, activeCrop.bottom, getActiveWidth(s), getActiveHeight(s)));
David Sodman41fdfc92017-11-06 16:09:56 -0800519 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
520 activeTransparentRegion.orSelf(
Marissa Wall61c58622018-07-18 10:12:20 -0700521 Rect(activeCrop.right, activeCrop.top, getActiveWidth(s), activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700522 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700523
Dan Stoza80d61162017-12-20 15:57:52 -0800524 // computeBounds returns a FloatRect to provide more accuracy during the
525 // transformation. We then round upon constructing 'frame'.
526 Rect frame{t.transform(computeBounds(activeTransparentRegion))};
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700527 if (!frame.intersect(display->getViewport(), &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000528 frame.clear();
529 }
Peiyong Linefefaac2018-08-17 12:27:51 -0700530 const ui::Transform& tr = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800531 Rect transformedFrame = tr.transform(frame);
David Sodman15094112018-10-11 09:39:37 -0700532 error = hwcLayer->setDisplayFrame(transformedFrame);
533 if (error != HWC2::Error::None) {
534 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
535 transformedFrame.left, transformedFrame.top, transformedFrame.right,
536 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
537 } else {
538 hwcInfo.displayFrame = transformedFrame;
539 }
David Sodmanba340492018-08-05 21:51:33 -0700540 getBE().compositionInfo.hwc.displayFrame = transformedFrame;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800541
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700542 FloatRect sourceCrop = computeCrop(display);
David Sodman15094112018-10-11 09:39:37 -0700543 error = hwcLayer->setSourceCrop(sourceCrop);
544 if (error != HWC2::Error::None) {
545 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
546 "%s (%d)",
547 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
548 to_string(error).c_str(), static_cast<int32_t>(error));
549 } else {
550 hwcInfo.sourceCrop = sourceCrop;
551 }
David Sodmanba340492018-08-05 21:51:33 -0700552 getBE().compositionInfo.hwc.sourceCrop = sourceCrop;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800553
chaviw13fdc492017-06-27 12:40:18 -0700554 float alpha = static_cast<float>(getAlpha());
David Sodman15094112018-10-11 09:39:37 -0700555 error = hwcLayer->setPlaneAlpha(alpha);
556 ALOGE_IF(error != HWC2::Error::None,
557 "[%s] Failed to set plane alpha %.3f: "
558 "%s (%d)",
559 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700560 getBE().compositionInfo.hwc.alpha = alpha;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800561
David Sodman15094112018-10-11 09:39:37 -0700562 error = hwcLayer->setZOrder(z);
563 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
564 to_string(error).c_str(), static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700565 getBE().compositionInfo.hwc.z = z;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500566
Albert Chaulk2a589632017-05-04 16:59:44 -0400567 int type = s.type;
568 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700569 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400570 if (parent.get()) {
571 auto& parentState = parent->getDrawingState();
rongliucfb187b2018-03-14 12:26:23 -0700572 if (parentState.type >= 0 || parentState.appId >= 0) {
573 type = parentState.type;
574 appId = parentState.appId;
575 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400576 }
577
David Sodman15094112018-10-11 09:39:37 -0700578 error = hwcLayer->setInfo(type, appId);
579 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
580 static_cast<int32_t>(error));
581
David Sodmanba340492018-08-05 21:51:33 -0700582 getBE().compositionInfo.hwc.type = type;
583 getBE().compositionInfo.hwc.appId = appId;
584
Mathias Agopian29a367b2011-07-12 14:51:45 -0700585 /*
586 * Transformations are applied in this order:
587 * 1) buffer orientation/flip/mirror
588 * 2) state transformation (window manager)
589 * 3) layer orientation (screen orientation)
590 * (NOTE: the matrices are multiplied in reverse order)
591 */
592
Peiyong Linefefaac2018-08-17 12:27:51 -0700593 const ui::Transform bufferOrientation(mCurrentTransform);
594 ui::Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700595
Robert Carrcae605c2017-03-29 12:10:31 -0700596 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700597 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700598 * the code below applies the primary display's inverse transform to the
599 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700600 */
David Sodman41fdfc92017-11-06 16:09:56 -0800601 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700602 // calculate the inverse transform
603 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800604 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700605 }
Robert Carrcae605c2017-03-29 12:10:31 -0700606
607 /*
608 * Here we cancel out the orientation component of the WM transform.
609 * The scaling and translate components are already included in our bounds
610 * computation so it's enough to just omit it in the composition.
611 * See comment in onDraw with ref to b/36727915 for why.
612 */
Peiyong Linefefaac2018-08-17 12:27:51 -0700613 transform = ui::Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700614 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700615
616 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800617 const uint32_t orientation = transform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -0700618 if (orientation & ui::Transform::ROT_INVALID) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800619 // we can only handle simple transformation
Lloyd Pique074e8122018-07-26 12:57:23 -0700620 hwcInfo.forceClientComposition = true;
David Sodman15094112018-10-11 09:39:37 -0700621 getBE().mHwcLayers[displayId].compositionType = HWC2::Composition::Client;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800622 } else {
623 auto transform = static_cast<HWC2::Transform>(orientation);
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800624 hwcInfo.transform = transform;
David Sodman15094112018-10-11 09:39:37 -0700625 auto error = hwcLayer->setTransform(transform);
626 ALOGE_IF(error != HWC2::Error::None,
627 "[%s] Failed to set transform %s: "
628 "%s (%d)",
629 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
630 static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700631 getBE().compositionInfo.hwc.transform = transform;
David Sodman4b7c4bc2017-11-17 12:13:59 -0800632 }
633}
634
Dominik Laskowski7e045462018-05-30 13:02:02 -0700635void Layer::forceClientComposition(int32_t displayId) {
636 if (getBE().mHwcLayers.count(displayId) == 0) {
637 ALOGE("forceClientComposition: no HWC layer found (%d)", displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800638 return;
639 }
640
Dominik Laskowski7e045462018-05-30 13:02:02 -0700641 getBE().mHwcLayers[displayId].forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800642}
Dan Stozaee44edd2015-03-23 15:50:23 -0700643
Dominik Laskowski7e045462018-05-30 13:02:02 -0700644bool Layer::getForceClientComposition(int32_t displayId) {
645 if (getBE().mHwcLayers.count(displayId) == 0) {
646 ALOGE("getForceClientComposition: no HWC layer found (%d)", displayId);
chaviwc9232ed2017-11-14 15:31:15 -0800647 return false;
648 }
649
Dominik Laskowski7e045462018-05-30 13:02:02 -0700650 return getBE().mHwcLayers[displayId].forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800651}
652
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700653void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700654 const auto displayId = display->getId();
655 if (getBE().mHwcLayers.count(displayId) == 0 ||
656 getCompositionType(displayId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800657 return;
658 }
659
660 // This gives us only the "orientation" component of the transform
661 const State& s(getCurrentState());
662
663 // Apply the layer's transform, followed by the display's global transform
664 // Here we're guaranteed that the layer's transform preserves rects
Marissa Wall61c58622018-07-18 10:12:20 -0700665 Rect win(getActiveWidth(s), getActiveHeight(s));
666 Rect crop = getCrop(s);
667 if (!crop.isEmpty()) {
668 win.intersect(crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800669 }
670 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700671 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Robert Carr1f0a16a2016-10-24 16:27:39 -0700672 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700673 frame.intersect(display->getViewport(), &frame);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700674 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800675 auto position = displayTransform.transform(frame);
676
Dominik Laskowski7e045462018-05-30 13:02:02 -0700677 auto error =
David Sodmanb8aaea12017-12-14 15:54:51 -0800678 (getBE().mHwcLayers[displayId].layer)->setCursorPosition(
679 position.left, position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800680 ALOGE_IF(error != HWC2::Error::None,
681 "[%s] Failed to set cursor position "
682 "to (%d, %d): %s (%d)",
683 mName.string(), position.left, position.top, to_string(error).c_str(),
684 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800685}
Riley Andrews03414a12014-07-01 14:22:59 -0700686
Mathias Agopian13127d82013-03-05 17:47:11 -0800687// ---------------------------------------------------------------------------
688// drawing...
689// ---------------------------------------------------------------------------
690
Marissa Wall61c58622018-07-18 10:12:20 -0700691void Layer::draw(const RenderArea& renderArea, const Region& clip) {
chaviwa76b2712017-09-20 12:02:26 -0700692 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800693}
694
Marissa Wall61c58622018-07-18 10:12:20 -0700695void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) {
chaviwa76b2712017-09-20 12:02:26 -0700696 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800697}
698
David Sodman41fdfc92017-11-06 16:09:56 -0800699void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
700 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800701 auto& engine(mFlinger->getRenderEngine());
David Sodman9eeae692017-11-02 10:53:32 -0700702 computeGeometry(renderArea, getBE().mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700703 engine.setupFillWithColor(red, green, blue, alpha);
David Sodman9eeae692017-11-02 10:53:32 -0700704 engine.drawMesh(getBE().mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800705}
706
David Sodmanc1498e62018-09-12 14:36:26 -0700707void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
708 clearWithOpenGL(renderArea, 0, 0, 0, 0);
709}
710
David Sodman15094112018-10-11 09:39:37 -0700711void Layer::setCompositionType(int32_t displayId, HWC2::Composition type, bool callIntoHwc) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700712 if (getBE().mHwcLayers.count(displayId) == 0) {
Chia-I Wu30505fb2018-03-26 16:20:31 -0700713 ALOGE("setCompositionType called without a valid HWC layer");
714 return;
David Sodman15094112018-10-11 09:39:37 -0700715 }
716 auto& hwcInfo = getBE().mHwcLayers[displayId];
717 auto& hwcLayer = hwcInfo.layer;
718 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", (hwcLayer)->getId(), to_string(type).c_str(),
719 static_cast<int>(callIntoHwc));
720 if (hwcInfo.compositionType != type) {
721 ALOGV(" actually setting");
722 hwcInfo.compositionType = type;
723 if (callIntoHwc) {
724 auto error = (hwcLayer)->setCompositionType(type);
725 ALOGE_IF(error != HWC2::Error::None,
726 "[%s] Failed to set "
727 "composition type %s: %s (%d)",
728 mName.string(), to_string(type).c_str(), to_string(error).c_str(),
729 static_cast<int32_t>(error));
Chia-I Wu30505fb2018-03-26 16:20:31 -0700730 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800731 }
732}
733
Dominik Laskowski7e045462018-05-30 13:02:02 -0700734HWC2::Composition Layer::getCompositionType(int32_t displayId) const {
David Sodman15fb96e2018-01-07 10:23:24 -0800735 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700736 // If we're querying the composition type for a display that does not
737 // have a HWC counterpart, then it will always be Client
738 return HWC2::Composition::Client;
739 }
David Sodman15fb96e2018-01-07 10:23:24 -0800740 return getBE().mHwcLayers[displayId].compositionType;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800741}
742
Dominik Laskowski7e045462018-05-30 13:02:02 -0700743void Layer::setClearClientTarget(int32_t displayId, bool clear) {
744 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800745 ALOGE("setClearClientTarget called without a valid HWC layer");
746 return;
747 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700748 getBE().mHwcLayers[displayId].clearClientTarget = clear;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800749}
750
Dominik Laskowski7e045462018-05-30 13:02:02 -0700751bool Layer::getClearClientTarget(int32_t displayId) const {
752 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800753 ALOGE("getClearClientTarget called without a valid HWC layer");
754 return false;
755 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700756 return getBE().mHwcLayers.at(displayId).clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800757}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800758
Dan Stozacac35382016-01-27 12:21:06 -0800759bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
760 if (point->getFrameNumber() <= mCurrentFrameNumber) {
761 // Don't bother with a SyncPoint, since we've already latched the
762 // relevant frame
763 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700764 }
Robert Carr2e102c92018-10-23 12:11:15 -0700765 if (isRemovedFromCurrentState()) {
766 return false;
767 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700768
Dan Stozacac35382016-01-27 12:21:06 -0800769 Mutex::Autolock lock(mLocalSyncPointMutex);
770 mLocalSyncPoints.push_back(point);
771 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700772}
773
Mathias Agopian13127d82013-03-05 17:47:11 -0800774// ----------------------------------------------------------------------------
775// local state
776// ----------------------------------------------------------------------------
777
Peiyong Lin833074a2018-08-28 11:53:54 -0700778void Layer::computeGeometry(const RenderArea& renderArea,
779 renderengine::Mesh& mesh,
chaviwa76b2712017-09-20 12:02:26 -0700780 bool useIdentityTransform) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700781 const ui::Transform renderAreaTransform(renderArea.getTransform());
Dan Stoza80d61162017-12-20 15:57:52 -0800782 FloatRect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700783
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000784 vec2 lt = vec2(win.left, win.top);
785 vec2 lb = vec2(win.left, win.bottom);
786 vec2 rb = vec2(win.right, win.bottom);
787 vec2 rt = vec2(win.right, win.top);
788
Peiyong Linefefaac2018-08-17 12:27:51 -0700789 ui::Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000790 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700791 lt = layerTransform.transform(lt);
792 lb = layerTransform.transform(lb);
793 rb = layerTransform.transform(rb);
794 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000795 }
796
Peiyong Lin833074a2018-08-28 11:53:54 -0700797 renderengine::Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700798 position[0] = renderAreaTransform.transform(lt);
799 position[1] = renderAreaTransform.transform(lb);
800 position[2] = renderAreaTransform.transform(rb);
801 position[3] = renderAreaTransform.transform(rt);
Mathias Agopian13127d82013-03-05 17:47:11 -0800802}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800803
David Sodman41fdfc92017-11-06 16:09:56 -0800804bool Layer::isSecure() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000805 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700806 return (s.flags & layer_state_t::eLayerSecure);
807}
808
Mathias Agopian13127d82013-03-05 17:47:11 -0800809void Layer::setVisibleRegion(const Region& visibleRegion) {
810 // always called from main thread
811 this->visibleRegion = visibleRegion;
812}
813
814void Layer::setCoveredRegion(const Region& coveredRegion) {
815 // always called from main thread
816 this->coveredRegion = coveredRegion;
817}
818
David Sodman41fdfc92017-11-06 16:09:56 -0800819void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800820 // always called from main thread
821 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
822}
823
Robert Carre5f4f692018-01-12 13:12:28 -0800824void Layer::clearVisibilityRegions() {
825 visibleRegion.clear();
826 visibleNonTransparentRegion.clear();
827 coveredRegion.clear();
828}
829
Mathias Agopian13127d82013-03-05 17:47:11 -0800830// ----------------------------------------------------------------------------
831// transaction
832// ----------------------------------------------------------------------------
833
Dan Stoza7dde5992015-05-22 09:51:44 -0700834void Layer::pushPendingState() {
835 if (!mCurrentState.modified) {
836 return;
837 }
838
Dan Stoza7dde5992015-05-22 09:51:44 -0700839 // If this transaction is waiting on the receipt of a frame, generate a sync
840 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700841 // We don't allow installing sync points after we are removed from the current state
842 // as we won't be able to signal our end.
843 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700844 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800845 if (barrierLayer == nullptr) {
846 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700847 // If we can't promote the layer we are intended to wait on,
848 // then it is expired or otherwise invalid. Allow this transaction
849 // to be applied as per normal (no synchronization).
Marissa Wallf58c14b2018-07-24 10:50:43 -0700850 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800851 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700852 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy);
Robert Carr0d480722017-01-10 16:42:54 -0800853 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800854 mRemoteSyncPoints.push_back(std::move(syncPoint));
855 } else {
856 // We already missed the frame we're supposed to synchronize
857 // on, so go ahead and apply the state update
Marissa Wallf58c14b2018-07-24 10:50:43 -0700858 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800859 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700860 }
861
Dan Stoza7dde5992015-05-22 09:51:44 -0700862 // Wake us up to check if the frame has been received
863 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700864 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700865 }
866 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700867 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700868}
869
Pablo Ceballos05289c22016-04-14 15:49:55 -0700870void Layer::popPendingState(State* stateToCommit) {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700871 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700872
873 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700874 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700875}
876
Pablo Ceballos05289c22016-04-14 15:49:55 -0700877bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700878 bool stateUpdateAvailable = false;
879 while (!mPendingStates.empty()) {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700880 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700881 if (mRemoteSyncPoints.empty()) {
882 // If we don't have a sync point for this, apply it anyway. It
883 // will be visually wrong, but it should keep us from getting
884 // into too much trouble.
885 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700886 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700887 stateUpdateAvailable = true;
888 continue;
889 }
890
Marissa Wallf58c14b2018-07-24 10:50:43 -0700891 if (mRemoteSyncPoints.front()->getFrameNumber() !=
892 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800893 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800894
895 // Signal our end of the sync point and then dispose of it
896 mRemoteSyncPoints.front()->setTransactionApplied();
897 mRemoteSyncPoints.pop_front();
898 continue;
899 }
900
Dan Stoza7dde5992015-05-22 09:51:44 -0700901 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
902 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700903 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700904 stateUpdateAvailable = true;
905
906 // Signal our end of the sync point and then dispose of it
907 mRemoteSyncPoints.front()->setTransactionApplied();
908 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800909 } else {
910 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700911 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700912 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700913 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700914 stateUpdateAvailable = true;
915 }
916 }
917
918 // If we still have pending updates, wake SurfaceFlinger back up and point
919 // it at this layer so we can process them
920 if (!mPendingStates.empty()) {
921 setTransactionFlags(eTransactionNeeded);
922 mFlinger->setTransactionFlags(eTraversalNeeded);
923 }
924
925 mCurrentState.modified = false;
926 return stateUpdateAvailable;
927}
928
Marissa Wall61c58622018-07-18 10:12:20 -0700929uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000930 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800931
Marissa Wall61c58622018-07-18 10:12:20 -0700932 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
933 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700934
David Sodmaneb085e02017-10-05 18:49:04 -0700935 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700936 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000937 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800938 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
939 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
940 " requested={ wh={%4u,%4u} }}\n"
941 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
942 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700943 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700944 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
945 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
946 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
947 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
948 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700949 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
950 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
951 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800952 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700953
Robert Carre392b552017-09-19 12:16:05 -0700954 // Don't let Layer::doTransaction update the drawing state
955 // if we have a pending resize, unless we are in fixed-size mode.
956 // the drawing state will be updated only once we receive a buffer
957 // with the correct size.
958 //
959 // In particular, we want to make sure the clip (which is part
960 // of the geometry state) is latched together with the size but is
961 // latched immediately when no resizing is involved.
962 //
963 // If a sideband stream is attached, however, we want to skip this
964 // optimization so that transactions aren't missed when a buffer
965 // never arrives
966 //
967 // In the case that we don't have a buffer we ignore other factors
968 // and avoid entering the resizePending state. At a high level the
969 // resizePending state is to avoid applying the state of the new buffer
970 // to the old buffer. However in the state where we don't have an old buffer
971 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700972 const bool resizePending =
973 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
974 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
David Sodman0cc69182017-11-17 12:12:07 -0800975 (getBE().compositionInfo.mBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700976 if (!isFixedSize()) {
David Sodman0cc69182017-11-17 12:12:07 -0800977 if (resizePending && getBE().compositionInfo.hwc.sidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700978 flags |= eDontUpdateGeometryState;
979 }
980 }
981
Robert Carr7bf247e2017-05-18 14:02:49 -0700982 // Here we apply various requested geometry states, depending on our
983 // latching configuration. See Layer.h for a detailed discussion of
984 // how geometry latching is controlled.
985 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000986 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700987
988 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
989 // mode, which causes attributes which normally latch regardless of scaling mode,
990 // to be delayed. We copy the requested state to the active state making sure
991 // to respect these rules (again see Layer.h for a detailed discussion).
992 //
993 // There is an awkward asymmetry in the handling of the crop states in the position
994 // states, as can be seen below. Largely this arises from position and transform
995 // being stored in the same data structure while having different latching rules.
996 // b/38182305
997 //
Marissa Wall61c58622018-07-18 10:12:20 -0700998 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700999 // applyPendingStates in the presence of deferred transactions.
1000 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -07001001 float tx = stateToCommit->active_legacy.transform.tx();
1002 float ty = stateToCommit->active_legacy.transform.ty();
1003 stateToCommit->active_legacy = stateToCommit->requested_legacy;
1004 stateToCommit->active_legacy.transform.set(tx, ty);
1005 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001006 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001007 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -07001008 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001009 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001010 }
1011
Marissa Wall61c58622018-07-18 10:12:20 -07001012 return flags;
1013}
1014
1015uint32_t Layer::doTransaction(uint32_t flags) {
1016 ATRACE_CALL();
1017
1018 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +00001019 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -07001020 if (!applyPendingStates(&c)) {
1021 return 0;
1022 }
1023
1024 flags = doTransactionResize(flags, &c);
1025
Alec Mourib416efd2018-09-06 21:01:59 +00001026 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -07001027
1028 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001029 // invalidate and recompute the visible regions if needed
1030 flags |= Layer::eVisibleRegion;
1031 }
1032
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001033 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001034 // invalidate and recompute the visible regions if needed
1035 flags |= eVisibleRegion;
1036 this->contentDirty = true;
1037
1038 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -07001039 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -07001040 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -08001041 }
1042
Dan Stozac8145172016-04-28 16:29:06 -07001043 // If the layer is hidden, signal and clear out all local sync points so
1044 // that transactions for layers depending on this layer's frames becoming
1045 // visible are not blocked
1046 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001047 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001048 }
1049
Mathias Agopian13127d82013-03-05 17:47:11 -08001050 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001051 commitTransaction(c);
Marissa Walle2ffb422018-10-12 11:33:52 -07001052 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -08001053 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001054}
1055
Pablo Ceballos05289c22016-04-14 15:49:55 -07001056void Layer::commitTransaction(const State& stateToCommit) {
1057 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001058}
1059
Mathias Agopian13127d82013-03-05 17:47:11 -08001060uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Piquef1c675b2018-09-12 20:45:39 -07001061 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -08001062}
1063
1064uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Piquef1c675b2018-09-12 20:45:39 -07001065 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -08001066}
1067
Robert Carr82364e32016-05-15 11:27:47 -07001068bool Layer::setPosition(float x, float y, bool immediate) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001069 if (mCurrentState.requested_legacy.transform.tx() == x &&
1070 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001071 return false;
1072 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001073
1074 // We update the requested and active position simultaneously because
1075 // we want to apply the position portion of the transform matrix immediately,
1076 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Marissa Wallf58c14b2018-07-24 10:50:43 -07001077 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001078 if (immediate && !mFreezeGeometryUpdates) {
1079 // Here we directly update the active state
1080 // unlike other setters, because we store it within
1081 // the transform, but use different latching rules.
1082 // b/38182305
Marissa Wallf58c14b2018-07-24 10:50:43 -07001083 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001084 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001085 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001086
Dan Stoza7dde5992015-05-22 09:51:44 -07001087 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001088 setTransactionFlags(eTransactionNeeded);
1089 return true;
1090}
Robert Carr82364e32016-05-15 11:27:47 -07001091
Robert Carr1f0a16a2016-10-24 16:27:39 -07001092bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1093 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1094 if (idx < 0) {
1095 return false;
1096 }
1097 if (childLayer->setLayer(z)) {
1098 mCurrentChildren.removeAt(idx);
1099 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001100 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001101 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001102 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001103}
1104
Robert Carr503c7042017-09-27 15:06:08 -07001105bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1106 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1107 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1108 if (idx < 0) {
1109 return false;
1110 }
1111 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1112 mCurrentChildren.removeAt(idx);
1113 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001114 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001115 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001116 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001117}
1118
Robert Carrae060832016-11-28 10:51:00 -08001119bool Layer::setLayer(int32_t z) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001120 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001121 mCurrentState.sequence++;
1122 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001123 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001124
1125 // Discard all relative layering.
1126 if (mCurrentState.zOrderRelativeOf != nullptr) {
1127 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1128 if (strongRelative != nullptr) {
1129 strongRelative->removeZOrderRelative(this);
1130 }
1131 mCurrentState.zOrderRelativeOf = nullptr;
1132 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001133 setTransactionFlags(eTransactionNeeded);
1134 return true;
1135}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001136
Robert Carrdb66e622017-04-10 16:55:57 -07001137void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1138 mCurrentState.zOrderRelatives.remove(relative);
1139 mCurrentState.sequence++;
1140 mCurrentState.modified = true;
1141 setTransactionFlags(eTransactionNeeded);
1142}
1143
1144void Layer::addZOrderRelative(const wp<Layer>& relative) {
1145 mCurrentState.zOrderRelatives.add(relative);
1146 mCurrentState.modified = true;
1147 mCurrentState.sequence++;
1148 setTransactionFlags(eTransactionNeeded);
1149}
1150
Robert Carr503d2bd2017-12-04 15:49:47 -08001151bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001152 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1153 if (handle == nullptr) {
1154 return false;
1155 }
1156 sp<Layer> relative = handle->owner.promote();
1157 if (relative == nullptr) {
1158 return false;
1159 }
1160
Robert Carr503d2bd2017-12-04 15:49:47 -08001161 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1162 mCurrentState.zOrderRelativeOf == relative) {
1163 return false;
1164 }
1165
Robert Carrdb66e622017-04-10 16:55:57 -07001166 mCurrentState.sequence++;
1167 mCurrentState.modified = true;
Robert Carr503d2bd2017-12-04 15:49:47 -08001168 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001169
chaviw9ab4bd12017-11-03 13:11:00 -07001170 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
1171 if (oldZOrderRelativeOf != nullptr) {
1172 oldZOrderRelativeOf->removeZOrderRelative(this);
1173 }
Robert Carrdb66e622017-04-10 16:55:57 -07001174 mCurrentState.zOrderRelativeOf = relative;
1175 relative->addZOrderRelative(this);
1176
1177 setTransactionFlags(eTransactionNeeded);
1178
1179 return true;
1180}
1181
Mathias Agopian13127d82013-03-05 17:47:11 -08001182bool Layer::setSize(uint32_t w, uint32_t h) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001183 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
1184 return false;
1185 mCurrentState.requested_legacy.w = w;
1186 mCurrentState.requested_legacy.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001187 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001188 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001189
1190 // record the new size, from this point on, when the client request
1191 // a buffer, it'll get the new size.
1192 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001193 return true;
1194}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001195bool Layer::setAlpha(float alpha) {
David Sodman41fdfc92017-11-06 16:09:56 -08001196 if (mCurrentState.color.a == alpha) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001197 mCurrentState.sequence++;
chaviw13fdc492017-06-27 12:40:18 -07001198 mCurrentState.color.a = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001199 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001200 setTransactionFlags(eTransactionNeeded);
1201 return true;
1202}
chaviw13fdc492017-06-27 12:40:18 -07001203
1204bool Layer::setColor(const half3& color) {
David Sodman41fdfc92017-11-06 16:09:56 -08001205 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g &&
1206 color.b == mCurrentState.color.b)
chaviw13fdc492017-06-27 12:40:18 -07001207 return false;
1208
1209 mCurrentState.sequence++;
1210 mCurrentState.color.r = color.r;
1211 mCurrentState.color.g = color.g;
1212 mCurrentState.color.b = color.b;
1213 mCurrentState.modified = true;
1214 setTransactionFlags(eTransactionNeeded);
1215 return true;
1216}
1217
Robert Carrd4ae7f32018-06-07 16:10:57 -07001218bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1219 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001220 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001221 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1222
1223 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1224 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1225 return false;
1226 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001227 mCurrentState.sequence++;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001228 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1229 matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001230 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001231 setTransactionFlags(eTransactionNeeded);
1232 return true;
1233}
Marissa Wall61c58622018-07-18 10:12:20 -07001234
Mathias Agopian13127d82013-03-05 17:47:11 -08001235bool Layer::setTransparentRegionHint(const Region& transparent) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001236 mCurrentState.requestedTransparentRegion_legacy = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001237 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001238 setTransactionFlags(eTransactionNeeded);
1239 return true;
1240}
1241bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1242 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
David Sodman41fdfc92017-11-06 16:09:56 -08001243 if (mCurrentState.flags == newFlags) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001244 mCurrentState.sequence++;
1245 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001246 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001247 setTransactionFlags(eTransactionNeeded);
1248 return true;
1249}
Robert Carr99e27f02016-06-16 15:18:02 -07001250
Marissa Wallf58c14b2018-07-24 10:50:43 -07001251bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
1252 if (mCurrentState.requestedCrop_legacy == crop) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001253 mCurrentState.sequence++;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001254 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001255 if (immediate && !mFreezeGeometryUpdates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001256 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001257 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001258 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1259
Dan Stoza7dde5992015-05-22 09:51:44 -07001260 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001261 setTransactionFlags(eTransactionNeeded);
1262 return true;
1263}
Robert Carr8d5227b2017-03-16 15:41:03 -07001264
Robert Carrc3574f72016-03-24 12:19:32 -07001265bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001266 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001267 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001268 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001269 return true;
1270}
1271
rongliucfb187b2018-03-14 12:26:23 -07001272void Layer::setInfo(int32_t type, int32_t appId) {
David Sodman41fdfc92017-11-06 16:09:56 -08001273 mCurrentState.appId = appId;
1274 mCurrentState.type = type;
1275 mCurrentState.modified = true;
1276 setTransactionFlags(eTransactionNeeded);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001277}
1278
Mathias Agopian13127d82013-03-05 17:47:11 -08001279bool Layer::setLayerStack(uint32_t layerStack) {
David Sodman41fdfc92017-11-06 16:09:56 -08001280 if (mCurrentState.layerStack == layerStack) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001281 mCurrentState.sequence++;
1282 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001283 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001284 setTransactionFlags(eTransactionNeeded);
1285 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001286}
1287
Robert Carr1f0a16a2016-10-24 16:27:39 -07001288uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001289 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001290 if (p == nullptr) {
1291 return getDrawingState().layerStack;
1292 }
1293 return p->getLayerStack();
1294}
1295
Marissa Wallf58c14b2018-07-24 10:50:43 -07001296void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
1297 mCurrentState.barrierLayer_legacy = barrierLayer;
1298 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001299 // We don't set eTransactionNeeded, because just receiving a deferral
1300 // request without any other state updates shouldn't actually induce a delay
1301 mCurrentState.modified = true;
1302 pushPendingState();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001303 mCurrentState.barrierLayer_legacy = nullptr;
1304 mCurrentState.frameNumber_legacy = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001305 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001306}
1307
Marissa Wallf58c14b2018-07-24 10:50:43 -07001308void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001309 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001310 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001311}
1312
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001313// ----------------------------------------------------------------------------
1314// pageflip handling...
1315// ----------------------------------------------------------------------------
1316
Robert Carr1f0a16a2016-10-24 16:27:39 -07001317bool Layer::isHiddenByPolicy() const {
Alec Mourib416efd2018-09-06 21:01:59 +00001318 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001319 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001320 if (parent != nullptr && parent->isHiddenByPolicy()) {
1321 return true;
1322 }
1323 return s.flags & layer_state_t::eLayerHidden;
1324}
1325
David Sodman41fdfc92017-11-06 16:09:56 -08001326uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001327 // TODO: should we do something special if mSecure is set?
1328 if (mProtectedByApp) {
1329 // need a hardware-protected path to external video sink
1330 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001331 }
Riley Andrews03414a12014-07-01 14:22:59 -07001332 if (mPotentialCursor) {
1333 usage |= GraphicBuffer::USAGE_CURSOR;
1334 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001335 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001336 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001337}
1338
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001339void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001340 uint32_t orientation = 0;
1341 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001342 // The transform hint is used to improve performance, but we can
1343 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001344 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001345 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001346 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001347 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001348 orientation = 0;
1349 }
1350 }
David Sodmaneb085e02017-10-05 18:49:04 -07001351 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001352}
1353
Mathias Agopian13127d82013-03-05 17:47:11 -08001354// ----------------------------------------------------------------------------
1355// debugging
1356// ----------------------------------------------------------------------------
1357
Marissa Wall61c58622018-07-18 10:12:20 -07001358// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001359LayerDebugInfo Layer::getLayerDebugInfo() const {
1360 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001361 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001362 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001363 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001364 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
1365 info.mType = String8(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001366 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001367 info.mVisibleRegion = visibleRegion;
1368 info.mSurfaceDamageRegion = surfaceDamageRegion;
1369 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001370 info.mX = ds.active_legacy.transform.tx();
1371 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001372 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001373 info.mWidth = ds.active_legacy.w;
1374 info.mHeight = ds.active_legacy.h;
1375 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001376 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001377 info.mFlags = ds.flags;
1378 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001379 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001380 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1381 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1382 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1383 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001384 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001385 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001386 if (buffer != 0) {
1387 info.mActiveBufferWidth = buffer->getWidth();
1388 info.mActiveBufferHeight = buffer->getHeight();
1389 info.mActiveBufferStride = buffer->getStride();
1390 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001391 } else {
1392 info.mActiveBufferWidth = 0;
1393 info.mActiveBufferHeight = 0;
1394 info.mActiveBufferStride = 0;
1395 info.mActiveBufferFormat = 0;
1396 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001397 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001398 info.mNumQueuedFrames = getQueuedFrameCount();
1399 info.mRefreshPending = isBufferLatched();
1400 info.mIsOpaque = isOpaque(ds);
1401 info.mContentDirty = contentDirty;
1402 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001403}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001404
Dan Stozae22aec72016-08-01 13:20:59 -07001405void Layer::miniDumpHeader(String8& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001406 result.append("-------------------------------");
1407 result.append("-------------------------------");
1408 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001409 result.append(" Layer name\n");
1410 result.append(" Z | ");
1411 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001412 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001413 result.append(" Disp Frame (LTRB) | ");
1414 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001415 result.append("-------------------------------");
1416 result.append("-------------------------------");
1417 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001418}
1419
Dominik Laskowski7e045462018-05-30 13:02:02 -07001420void Layer::miniDump(String8& result, int32_t displayId) const {
1421 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stozae22aec72016-08-01 13:20:59 -07001422 return;
1423 }
1424
1425 String8 name;
1426 if (mName.length() > 77) {
1427 std::string shortened;
1428 shortened.append(mName.string(), 36);
1429 shortened.append("[...]");
1430 shortened.append(mName.string() + (mName.length() - 36), 36);
1431 name = shortened.c_str();
1432 } else {
1433 name = mName;
1434 }
1435
1436 result.appendFormat(" %s\n", name.string());
1437
Alec Mourib416efd2018-09-06 21:01:59 +00001438 const State& layerState(getDrawingState());
Lloyd Pique074e8122018-07-26 12:57:23 -07001439 const LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers.at(displayId);
Chia-I Wu1e043612018-03-01 09:45:09 -08001440 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
1441 result.appendFormat(" rel %6d | ", layerState.z);
1442 } else {
1443 result.appendFormat(" %10d | ", layerState.z);
1444 }
Dominik Laskowski7e045462018-05-30 13:02:02 -07001445 result.appendFormat("%10s | ", to_string(getCompositionType(displayId)).c_str());
Lloyd Pique074e8122018-07-26 12:57:23 -07001446 result.appendFormat("%10s | ", to_string(hwcInfo.transform).c_str());
1447 const Rect& frame = hwcInfo.displayFrame;
David Sodman41fdfc92017-11-06 16:09:56 -08001448 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique074e8122018-07-26 12:57:23 -07001449 const FloatRect& crop = hwcInfo.sourceCrop;
David Sodman41fdfc92017-11-06 16:09:56 -08001450 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 -07001451
David Sodman7e4ae112018-02-09 15:02:28 -08001452 result.append("- - - - - - - - - - - - - - - -\n");
1453
1454 std::string compositionInfoStr;
1455 getBE().compositionInfo.dump(compositionInfoStr, "compositionInfo");
1456 result.append(compositionInfoStr.c_str());
1457
Yichi Chen6ca35192018-05-29 12:20:43 +08001458 result.append("- - - - - - - - - - - - - - - -");
1459 result.append("- - - - - - - - - - - - - - - -");
1460 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001461}
Dan Stozae22aec72016-08-01 13:20:59 -07001462
Svetoslavd85084b2014-03-20 10:28:31 -07001463void Layer::dumpFrameStats(String8& result) const {
1464 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001465}
1466
Svetoslavd85084b2014-03-20 10:28:31 -07001467void Layer::clearFrameStats() {
1468 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001469}
1470
Jamie Gennis6547ff42013-07-16 20:12:42 -07001471void Layer::logFrameStats() {
1472 mFrameTracker.logAndResetStats(mName);
1473}
1474
Svetoslavd85084b2014-03-20 10:28:31 -07001475void Layer::getFrameStats(FrameStats* outStats) const {
1476 mFrameTracker.getStats(outStats);
1477}
1478
Brian Andersond6927fb2016-07-23 23:37:30 -07001479void Layer::dumpFrameEvents(String8& result) {
David Sodman41fdfc92017-11-06 16:09:56 -08001480 result.appendFormat("- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001481 Mutex::Autolock lock(mFrameEventHistoryMutex);
1482 mFrameEventHistory.checkFencesForCompletion();
1483 mFrameEventHistory.dump(result);
1484}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001485
Brian Anderson5ea5e592016-12-01 16:54:33 -08001486void Layer::onDisconnect() {
1487 Mutex::Autolock lock(mFrameEventHistoryMutex);
1488 mFrameEventHistory.onDisconnect();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -07001489 mTimeStats.onDisconnect(getSequence());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001490}
1491
Brian Anderson3890c392016-07-25 12:48:08 -07001492void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001493 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001494 if (newTimestamps) {
Yiwei Zhang8e8fe522018-11-02 18:34:07 -07001495 mTimeStats.setPostTime(getSequence(), newTimestamps->frameNumber, getName().c_str(),
1496 newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001497 }
1498
Brian Andersond6927fb2016-07-23 23:37:30 -07001499 Mutex::Autolock lock(mFrameEventHistoryMutex);
1500 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001501 // If there are any unsignaled fences in the aquire timeline at this
1502 // point, the previously queued frame hasn't been latched yet. Go ahead
1503 // and try to get the signal time here so the syscall is taken out of
1504 // the main thread's critical path.
1505 mAcquireTimeline.updateSignalTimes();
1506 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001507 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001508 mFrameEventHistory.addQueue(*newTimestamps);
1509 }
1510
Brian Anderson3890c392016-07-25 12:48:08 -07001511 if (outDelta) {
1512 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001513 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001514}
Dan Stozae77c7662016-05-13 11:37:28 -07001515
Chia-I Wu98f1c102017-05-30 14:54:08 -07001516size_t Layer::getChildrenCount() const {
1517 size_t count = 0;
1518 for (const sp<Layer>& child : mCurrentChildren) {
1519 count += 1 + child->getChildrenCount();
1520 }
1521 return count;
1522}
1523
Robert Carr1f0a16a2016-10-24 16:27:39 -07001524void Layer::addChild(const sp<Layer>& layer) {
1525 mCurrentChildren.add(layer);
1526 layer->setParent(this);
1527}
1528
1529ssize_t Layer::removeChild(const sp<Layer>& layer) {
1530 layer->setParent(nullptr);
1531 return mCurrentChildren.remove(layer);
1532}
1533
Robert Carr1db73f62016-12-21 12:58:51 -08001534bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1535 sp<Handle> handle = nullptr;
1536 sp<Layer> newParent = nullptr;
1537 if (newParentHandle == nullptr) {
1538 return false;
1539 }
1540 handle = static_cast<Handle*>(newParentHandle.get());
1541 newParent = handle->owner.promote();
1542 if (newParent == nullptr) {
1543 ALOGE("Unable to promote Layer handle");
1544 return false;
1545 }
1546
1547 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001548 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001549
1550 sp<Client> client(child->mClientRef.promote());
1551 if (client != nullptr) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001552 client->updateParent(newParent);
Robert Carr1db73f62016-12-21 12:58:51 -08001553 }
1554 }
1555 mCurrentChildren.clear();
1556
1557 return true;
1558}
1559
Robert Carr15eae092018-03-23 13:43:53 -07001560void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001561 for (const sp<Layer>& child : mDrawingChildren) {
1562 child->mDrawingParent = newParent;
1563 }
1564}
1565
chaviwf1961f72017-09-18 16:41:07 -07001566bool Layer::reparent(const sp<IBinder>& newParentHandle) {
1567 if (newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001568 return false;
1569 }
1570
1571 auto handle = static_cast<Handle*>(newParentHandle.get());
1572 sp<Layer> newParent = handle->owner.promote();
1573 if (newParent == nullptr) {
1574 ALOGE("Unable to promote Layer handle");
1575 return false;
1576 }
1577
chaviwf1961f72017-09-18 16:41:07 -07001578 sp<Layer> parent = getParent();
1579 if (parent != nullptr) {
1580 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001581 }
chaviwf1961f72017-09-18 16:41:07 -07001582 newParent->addChild(this);
chaviw06178942017-07-27 10:25:59 -07001583
chaviwf1961f72017-09-18 16:41:07 -07001584 sp<Client> client(mClientRef.promote());
chaviw06178942017-07-27 10:25:59 -07001585 sp<Client> newParentClient(newParent->mClientRef.promote());
1586
chaviwf1961f72017-09-18 16:41:07 -07001587 if (client != newParentClient) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001588 client->updateParent(newParent);
chaviw06178942017-07-27 10:25:59 -07001589 }
1590
chaviw06178942017-07-27 10:25:59 -07001591 return true;
1592}
1593
Robert Carr9524cb32017-02-13 11:32:32 -08001594bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001595 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001596 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001597 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001598 if (client != nullptr && parentClient != client) {
Robert Carr7f619b22017-11-06 12:56:35 -08001599 client->detachLayer(child.get());
1600 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001601 }
Robert Carr7f619b22017-11-06 12:56:35 -08001602 }
Robert Carr9524cb32017-02-13 11:32:32 -08001603
1604 return true;
1605}
1606
Peiyong Lind3788632018-09-18 16:01:31 -07001607bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001608 static const mat4 identityMatrix = mat4();
1609
Peiyong Lind3788632018-09-18 16:01:31 -07001610 if (mCurrentState.colorTransform == matrix) {
1611 return false;
1612 }
1613 ++mCurrentState.sequence;
1614 mCurrentState.colorTransform = matrix;
Peiyong Lin747321c2018-10-01 10:03:11 -07001615 mCurrentState.hasColorTransform = matrix != identityMatrix;
1616 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001617 setTransactionFlags(eTransactionNeeded);
1618 return true;
1619}
1620
1621const mat4& Layer::getColorTransform() const {
1622 return getDrawingState().colorTransform;
1623}
1624
1625bool Layer::hasColorTransform() const {
Peiyong Lin747321c2018-10-01 10:03:11 -07001626 return getDrawingState().hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001627}
1628
Chia-I Wu11481472018-05-04 10:43:19 -07001629bool Layer::isLegacyDataSpace() const {
1630 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001631 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001632 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001633}
1634
Robert Carr1f0a16a2016-10-24 16:27:39 -07001635void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001636 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001637}
1638
1639void Layer::clearSyncPoints() {
1640 for (const auto& child : mCurrentChildren) {
1641 child->clearSyncPoints();
1642 }
1643
1644 Mutex::Autolock lock(mLocalSyncPointMutex);
1645 for (auto& point : mLocalSyncPoints) {
1646 point->setFrameAvailable();
1647 }
1648 mLocalSyncPoints.clear();
1649}
1650
1651int32_t Layer::getZ() const {
1652 return mDrawingState.z;
1653}
1654
Robert Carr29abff82017-12-04 13:51:20 -08001655bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1656 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1657 const State& state = useDrawing ? mDrawingState : mCurrentState;
1658 return state.zOrderRelativeOf != nullptr;
1659}
1660
David Sodman41fdfc92017-11-06 16:09:56 -08001661__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001662 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001663 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1664 "makeTraversalList received invalid stateSet");
1665 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1666 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1667 const State& state = useDrawing ? mDrawingState : mCurrentState;
1668
Robert Carr29abff82017-12-04 13:51:20 -08001669 if (state.zOrderRelatives.size() == 0) {
1670 *outSkipRelativeZUsers = true;
1671 return children;
1672 }
1673
chaviwfd462612018-05-31 16:11:27 -07001674 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001675 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001676 sp<Layer> strongRelative = weakRelative.promote();
1677 if (strongRelative != nullptr) {
1678 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001679 }
1680 }
1681
Dan Stoza412903f2017-04-27 13:42:17 -07001682 for (const sp<Layer>& child : children) {
Robert Carr503c7042017-09-27 15:06:08 -07001683 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1684 if (childState.zOrderRelativeOf != nullptr) {
1685 continue;
1686 }
Robert Carrdb66e622017-04-10 16:55:57 -07001687 traverse.add(child);
1688 }
1689
1690 return traverse;
1691}
1692
Robert Carr1f0a16a2016-10-24 16:27:39 -07001693/**
Robert Carrdb66e622017-04-10 16:55:57 -07001694 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001695 */
Dan Stoza412903f2017-04-27 13:42:17 -07001696void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001697 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1698 // produce a new list for traversing, including our relatives, and not including our children
1699 // who are relatives of another surface. In the case that there are no relative Z,
1700 // makeTraversalList returns our children directly to avoid significant overhead.
1701 // However in this case we need to take the responsibility for filtering children which
1702 // are relatives of another surface here.
1703 bool skipRelativeZUsers = false;
1704 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001705
Robert Carr1f0a16a2016-10-24 16:27:39 -07001706 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001707 for (; i < list.size(); i++) {
1708 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001709 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1710 continue;
1711 }
1712
Robert Carrdb66e622017-04-10 16:55:57 -07001713 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001714 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001715 }
Dan Stoza412903f2017-04-27 13:42:17 -07001716 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001717 }
Robert Carr29abff82017-12-04 13:51:20 -08001718
Dan Stoza412903f2017-04-27 13:42:17 -07001719 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001720 for (; i < list.size(); i++) {
1721 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001722
1723 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1724 continue;
1725 }
Dan Stoza412903f2017-04-27 13:42:17 -07001726 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001727 }
1728}
1729
1730/**
Robert Carrdb66e622017-04-10 16:55:57 -07001731 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001732 */
Dan Stoza412903f2017-04-27 13:42:17 -07001733void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1734 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001735 // See traverseInZOrder for documentation.
1736 bool skipRelativeZUsers = false;
1737 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001738
Robert Carr1f0a16a2016-10-24 16:27:39 -07001739 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001740 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001741 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001742
1743 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1744 continue;
1745 }
1746
Robert Carrdb66e622017-04-10 16:55:57 -07001747 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001748 break;
1749 }
Dan Stoza412903f2017-04-27 13:42:17 -07001750 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001751 }
Dan Stoza412903f2017-04-27 13:42:17 -07001752 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001753 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001754 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001755
1756 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1757 continue;
1758 }
1759
Dan Stoza412903f2017-04-27 13:42:17 -07001760 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001761 }
1762}
1763
chaviw4b129c22018-04-09 16:19:43 -07001764LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1765 const std::vector<Layer*>& layersInTree) {
1766 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1767 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001768 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1769 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
chaviw4b129c22018-04-09 16:19:43 -07001770 const State& state = useDrawing ? mDrawingState : mCurrentState;
1771
chaviwfd462612018-05-31 16:11:27 -07001772 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001773 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1774 sp<Layer> strongRelative = weakRelative.promote();
1775 // Only add relative layers that are also descendents of the top most parent of the tree.
1776 // If a relative layer is not a descendent, then it should be ignored.
1777 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1778 traverse.add(strongRelative);
1779 }
1780 }
1781
1782 for (const sp<Layer>& child : children) {
1783 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1784 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1785 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1786 // the child here since it won't be added later as a relative.
1787 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1788 childState.zOrderRelativeOf.promote().get())) {
1789 continue;
1790 }
1791 traverse.add(child);
1792 }
1793
1794 return traverse;
1795}
1796
1797void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1798 LayerVector::StateSet stateSet,
1799 const LayerVector::Visitor& visitor) {
1800 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001801
1802 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001803 for (; i < list.size(); i++) {
1804 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001805 if (relative->getZ() >= 0) {
1806 break;
1807 }
chaviw4b129c22018-04-09 16:19:43 -07001808 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001809 }
chaviw4b129c22018-04-09 16:19:43 -07001810
chaviwa76b2712017-09-20 12:02:26 -07001811 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001812 for (; i < list.size(); i++) {
1813 const auto& relative = list[i];
1814 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001815 }
1816}
1817
chaviw4b129c22018-04-09 16:19:43 -07001818std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1819 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1820 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1821
1822 std::vector<Layer*> layersInTree = {this};
1823 for (size_t i = 0; i < children.size(); i++) {
1824 const auto& child = children[i];
1825 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1826 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1827 }
1828
1829 return layersInTree;
1830}
1831
1832void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1833 const LayerVector::Visitor& visitor) {
1834 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1835 std::sort(layersInTree.begin(), layersInTree.end());
1836 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1837}
1838
Peiyong Linefefaac2018-08-17 12:27:51 -07001839ui::Transform Layer::getTransform() const {
1840 ui::Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001841 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001842 if (p != nullptr) {
1843 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07001844
1845 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
1846 // it isFixedSize) then there may be additional scaling not accounted
1847 // for in the transform. We need to mirror this scaling in child surfaces
1848 // or we will break the contract where WM can treat child surfaces as
1849 // pixels in the parent surface.
David Sodman0cc69182017-11-17 12:12:07 -08001850 if (p->isFixedSize() && p->getBE().compositionInfo.mBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07001851 int bufferWidth;
1852 int bufferHeight;
1853 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
David Sodman0cc69182017-11-17 12:12:07 -08001854 bufferWidth = p->getBE().compositionInfo.mBuffer->getWidth();
1855 bufferHeight = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001856 } else {
David Sodman0cc69182017-11-17 12:12:07 -08001857 bufferHeight = p->getBE().compositionInfo.mBuffer->getWidth();
1858 bufferWidth = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001859 }
Marissa Wall61c58622018-07-18 10:12:20 -07001860 float sx = p->getActiveWidth(p->getDrawingState()) / static_cast<float>(bufferWidth);
1861 float sy = p->getActiveHeight(p->getDrawingState()) / static_cast<float>(bufferHeight);
Peiyong Linefefaac2018-08-17 12:27:51 -07001862 ui::Transform extraParentScaling;
Robert Carr9b429f42017-04-17 14:56:57 -07001863 extraParentScaling.set(sx, 0, 0, sy);
1864 t = t * extraParentScaling;
1865 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001866 }
Marissa Wall61c58622018-07-18 10:12:20 -07001867 return t * getActiveTransform(getDrawingState());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001868}
1869
chaviw13fdc492017-06-27 12:40:18 -07001870half Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001871 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07001872
chaviw13fdc492017-06-27 12:40:18 -07001873 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1874 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001875}
Robert Carr6452f122017-03-21 10:41:29 -07001876
chaviw13fdc492017-06-27 12:40:18 -07001877half4 Layer::getColor() const {
1878 const half4 color(getDrawingState().color);
1879 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001880}
Robert Carr6452f122017-03-21 10:41:29 -07001881
Robert Carr1f0a16a2016-10-24 16:27:39 -07001882void Layer::commitChildList() {
1883 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1884 const auto& child = mCurrentChildren[i];
1885 child->commitChildList();
1886 }
1887 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001888 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001889}
1890
chaviw1d044282017-09-27 12:19:28 -07001891void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
1892 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1893 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1894 const State& state = useDrawing ? mDrawingState : mCurrentState;
1895
Peiyong Linefefaac2018-08-17 12:27:51 -07001896 ui::Transform requestedTransform = state.active_legacy.transform;
1897 ui::Transform transform = getTransform();
chaviw1d044282017-09-27 12:19:28 -07001898
1899 layerInfo->set_id(sequence);
1900 layerInfo->set_name(getName().c_str());
1901 layerInfo->set_type(String8(getTypeId()));
1902
1903 for (const auto& child : children) {
1904 layerInfo->add_children(child->sequence);
1905 }
1906
1907 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1908 sp<Layer> strongRelative = weakRelative.promote();
1909 if (strongRelative != nullptr) {
1910 layerInfo->add_relatives(strongRelative->sequence);
1911 }
1912 }
1913
Marissa Wallf58c14b2018-07-24 10:50:43 -07001914 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
chaviw1d044282017-09-27 12:19:28 -07001915 layerInfo->mutable_transparent_region());
1916 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
1917 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
1918
1919 layerInfo->set_layer_stack(getLayerStack());
1920 layerInfo->set_z(state.z);
1921
1922 PositionProto* position = layerInfo->mutable_position();
1923 position->set_x(transform.tx());
1924 position->set_y(transform.ty());
1925
1926 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
1927 requestedPosition->set_x(requestedTransform.tx());
1928 requestedPosition->set_y(requestedTransform.ty());
1929
1930 SizeProto* size = layerInfo->mutable_size();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001931 size->set_w(state.active_legacy.w);
1932 size->set_h(state.active_legacy.h);
chaviw1d044282017-09-27 12:19:28 -07001933
Marissa Wallf58c14b2018-07-24 10:50:43 -07001934 LayerProtoHelper::writeToProto(state.crop_legacy, layerInfo->mutable_crop());
chaviw1d044282017-09-27 12:19:28 -07001935
1936 layerInfo->set_is_opaque(isOpaque(state));
1937 layerInfo->set_invalidate(contentDirty);
Chia-I Wu01591c92018-05-22 12:03:00 -07001938
1939 // XXX (b/79210409) mCurrentDataSpace is not protected
1940 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
1941
chaviw1d044282017-09-27 12:19:28 -07001942 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1943 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
1944 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
1945 layerInfo->set_flags(state.flags);
1946
1947 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1948 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
1949
Jorim Jaggi8e0af362017-11-14 16:28:28 +01001950 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07001951 if (parent != nullptr) {
1952 layerInfo->set_parent(parent->sequence);
1953 }
1954
1955 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1956 if (zOrderRelativeOf != nullptr) {
1957 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1958 }
1959
Chia-I Wu01591c92018-05-22 12:03:00 -07001960 // XXX getBE().compositionInfo.mBuffer is not protected
David Sodman0cc69182017-11-17 12:12:07 -08001961 auto buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001962 if (buffer != nullptr) {
1963 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
Peiyong Linefefaac2018-08-17 12:27:51 -07001964 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
Yichi Chen6ca35192018-05-29 12:20:43 +08001965 layerInfo->mutable_buffer_transform());
chaviw1d044282017-09-27 12:19:28 -07001966 }
1967
1968 layerInfo->set_queued_frames(getQueuedFrameCount());
1969 layerInfo->set_refresh_pending(isBufferLatched());
rongliucfb187b2018-03-14 12:26:23 -07001970 layerInfo->set_window_type(state.type);
1971 layerInfo->set_app_id(state.appId);
chaviwadc40c22018-07-10 16:57:27 -07001972 layerInfo->set_curr_frame(mCurrentFrameNumber);
1973
1974 for (const auto& pendingState : mPendingStates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001975 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
chaviwadc40c22018-07-10 16:57:27 -07001976 if (barrierLayer != nullptr) {
1977 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
1978 barrierLayerProto->set_id(barrierLayer->sequence);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001979 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
chaviwadc40c22018-07-10 16:57:27 -07001980 }
1981 }
chaviw1d044282017-09-27 12:19:28 -07001982}
1983
Dominik Laskowski7e045462018-05-30 13:02:02 -07001984void Layer::writeToProto(LayerProto* layerInfo, int32_t displayId) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07001985 if (!hasHwcLayer(displayId)) {
1986 return;
1987 }
1988
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001989 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
1990
Dominik Laskowski7e045462018-05-30 13:02:02 -07001991 const auto& hwcInfo = getBE().mHwcLayers.at(displayId);
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001992
1993 const Rect& frame = hwcInfo.displayFrame;
1994 LayerProtoHelper::writeToProto(frame, layerInfo->mutable_hwc_frame());
1995
1996 const FloatRect& crop = hwcInfo.sourceCrop;
1997 LayerProtoHelper::writeToProto(crop, layerInfo->mutable_hwc_crop());
1998
1999 const int32_t transform = static_cast<int32_t>(hwcInfo.transform);
2000 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002001
2002 const int32_t compositionType = static_cast<int32_t>(hwcInfo.compositionType);
2003 layerInfo->set_hwc_composition_type(compositionType);
2004
2005 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
2006 static_cast<BufferLayer*>(this)->isProtected()) {
2007 layerInfo->set_is_protected(true);
2008 } else {
2009 layerInfo->set_is_protected(false);
2010 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002011}
2012
Robert Carr2e102c92018-10-23 12:11:15 -07002013bool Layer::isRemovedFromCurrentState() const {
2014 return mRemovedFromCurrentState;
2015}
2016
Mathias Agopian13127d82013-03-05 17:47:11 -08002017// ---------------------------------------------------------------------------
2018
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002019}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002020
2021#if defined(__gl_h_)
2022#error "don't include gl/gl.h in this file"
2023#endif
2024
2025#if defined(__gl2_h_)
2026#error "don't include gl2/gl2.h in this file"
2027#endif