blob: f41a753a7a1969a15fa52cd667150d3c31e33b88 [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 Laskowski075d3172018-05-24 15:50:06 -0700199bool Layer::createHwcLayer(HWComposer* hwc, DisplayId displayId) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700200 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.count(displayId) != 0,
Dominik Laskowski075d3172018-05-24 15:50:06 -0700201 "Already have a layer for display %" PRIu64, 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 Laskowski075d3172018-05-24 15:50:06 -0700217bool Layer::destroyHwcLayer(DisplayId displayId) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700218 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();
Dominik Laskowski075d3172018-05-24 15:50:06 -0700464 LOG_ALWAYS_FATAL_IF(!displayId);
465 if (!hasHwcLayer(*displayId)) {
466 ALOGE("[%s] failed to setGeometry: no HWC layer found for display %" PRIu64, mName.string(),
467 *displayId);
Peiyong Lin91b1df22018-06-18 18:00:16 -0700468 return;
469 }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700470 auto& hwcInfo = getBE().mHwcLayers[*displayId];
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700471
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700472 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800473 hwcInfo.forceClientComposition = false;
474
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700475 if (isSecure() && !display->isSecure()) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800476 hwcInfo.forceClientComposition = true;
477 }
478
David Sodman15094112018-10-11 09:39:37 -0700479 auto& hwcLayer = hwcInfo.layer;
480
Mathias Agopian13127d82013-03-05 17:47:11 -0800481 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700482 const State& s(getDrawingState());
David Revemanecf0fa52017-03-03 11:32:44 -0500483 auto blendMode = HWC2::BlendMode::None;
Robert Carr6452f122017-03-21 10:41:29 -0700484 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800485 blendMode =
486 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800487 }
David Sodman15094112018-10-11 09:39:37 -0700488 auto error = hwcLayer->setBlendMode(blendMode);
489 ALOGE_IF(error != HWC2::Error::None,
490 "[%s] Failed to set blend mode %s:"
491 " %s (%d)",
492 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
493 static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700494 getBE().compositionInfo.hwc.blendMode = blendMode;
Mathias Agopian13127d82013-03-05 17:47:11 -0800495
496 // apply the layer's transform, followed by the display's global transform
497 // here we're guaranteed that the layer's transform preserves rects
Marissa Wall61c58622018-07-18 10:12:20 -0700498 Region activeTransparentRegion(getActiveTransparentRegion(s));
Peiyong Linefefaac2018-08-17 12:27:51 -0700499 ui::Transform t = getTransform();
Marissa Wall61c58622018-07-18 10:12:20 -0700500 Rect activeCrop = getCrop(s);
501 if (!activeCrop.isEmpty()) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700502 activeCrop = t.transform(activeCrop);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700503 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000504 activeCrop.clear();
505 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700506 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800507 // This needs to be here as transform.transform(Rect) computes the
508 // transformed rect and then takes the bounding box of the result before
509 // returning. This means
510 // transform.inverse().transform(transform.transform(Rect)) != Rect
511 // in which case we need to make sure the final rect is clipped to the
512 // display bounds.
Marissa Wall61c58622018-07-18 10:12:20 -0700513 if (!activeCrop.intersect(Rect(getActiveWidth(s), getActiveHeight(s)), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000514 activeCrop.clear();
515 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700516 // mark regions outside the crop as transparent
Marissa Wall61c58622018-07-18 10:12:20 -0700517 activeTransparentRegion.orSelf(Rect(0, 0, getActiveWidth(s), activeCrop.top));
Marissa Wallf58c14b2018-07-24 10:50:43 -0700518 activeTransparentRegion.orSelf(
Marissa Wall61c58622018-07-18 10:12:20 -0700519 Rect(0, activeCrop.bottom, getActiveWidth(s), getActiveHeight(s)));
David Sodman41fdfc92017-11-06 16:09:56 -0800520 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
521 activeTransparentRegion.orSelf(
Marissa Wall61c58622018-07-18 10:12:20 -0700522 Rect(activeCrop.right, activeCrop.top, getActiveWidth(s), activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700523 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700524
Dan Stoza80d61162017-12-20 15:57:52 -0800525 // computeBounds returns a FloatRect to provide more accuracy during the
526 // transformation. We then round upon constructing 'frame'.
527 Rect frame{t.transform(computeBounds(activeTransparentRegion))};
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700528 if (!frame.intersect(display->getViewport(), &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000529 frame.clear();
530 }
Peiyong Linefefaac2018-08-17 12:27:51 -0700531 const ui::Transform& tr = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800532 Rect transformedFrame = tr.transform(frame);
David Sodman15094112018-10-11 09:39:37 -0700533 error = hwcLayer->setDisplayFrame(transformedFrame);
534 if (error != HWC2::Error::None) {
535 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
536 transformedFrame.left, transformedFrame.top, transformedFrame.right,
537 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
538 } else {
539 hwcInfo.displayFrame = transformedFrame;
540 }
David Sodmanba340492018-08-05 21:51:33 -0700541 getBE().compositionInfo.hwc.displayFrame = transformedFrame;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800542
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700543 FloatRect sourceCrop = computeCrop(display);
David Sodman15094112018-10-11 09:39:37 -0700544 error = hwcLayer->setSourceCrop(sourceCrop);
545 if (error != HWC2::Error::None) {
546 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
547 "%s (%d)",
548 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
549 to_string(error).c_str(), static_cast<int32_t>(error));
550 } else {
551 hwcInfo.sourceCrop = sourceCrop;
552 }
David Sodmanba340492018-08-05 21:51:33 -0700553 getBE().compositionInfo.hwc.sourceCrop = sourceCrop;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800554
chaviw13fdc492017-06-27 12:40:18 -0700555 float alpha = static_cast<float>(getAlpha());
David Sodman15094112018-10-11 09:39:37 -0700556 error = hwcLayer->setPlaneAlpha(alpha);
557 ALOGE_IF(error != HWC2::Error::None,
558 "[%s] Failed to set plane alpha %.3f: "
559 "%s (%d)",
560 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700561 getBE().compositionInfo.hwc.alpha = alpha;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800562
David Sodman15094112018-10-11 09:39:37 -0700563 error = hwcLayer->setZOrder(z);
564 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
565 to_string(error).c_str(), static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700566 getBE().compositionInfo.hwc.z = z;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500567
Albert Chaulk2a589632017-05-04 16:59:44 -0400568 int type = s.type;
569 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700570 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400571 if (parent.get()) {
572 auto& parentState = parent->getDrawingState();
rongliucfb187b2018-03-14 12:26:23 -0700573 if (parentState.type >= 0 || parentState.appId >= 0) {
574 type = parentState.type;
575 appId = parentState.appId;
576 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400577 }
578
David Sodman15094112018-10-11 09:39:37 -0700579 error = hwcLayer->setInfo(type, appId);
580 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
581 static_cast<int32_t>(error));
582
David Sodmanba340492018-08-05 21:51:33 -0700583 getBE().compositionInfo.hwc.type = type;
584 getBE().compositionInfo.hwc.appId = appId;
585
Mathias Agopian29a367b2011-07-12 14:51:45 -0700586 /*
587 * Transformations are applied in this order:
588 * 1) buffer orientation/flip/mirror
589 * 2) state transformation (window manager)
590 * 3) layer orientation (screen orientation)
591 * (NOTE: the matrices are multiplied in reverse order)
592 */
593
Peiyong Linefefaac2018-08-17 12:27:51 -0700594 const ui::Transform bufferOrientation(mCurrentTransform);
595 ui::Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700596
Robert Carrcae605c2017-03-29 12:10:31 -0700597 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700598 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700599 * the code below applies the primary display's inverse transform to the
600 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700601 */
David Sodman41fdfc92017-11-06 16:09:56 -0800602 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700603 // calculate the inverse transform
604 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800605 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700606 }
Robert Carrcae605c2017-03-29 12:10:31 -0700607
608 /*
609 * Here we cancel out the orientation component of the WM transform.
610 * The scaling and translate components are already included in our bounds
611 * computation so it's enough to just omit it in the composition.
612 * See comment in onDraw with ref to b/36727915 for why.
613 */
Peiyong Linefefaac2018-08-17 12:27:51 -0700614 transform = ui::Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700615 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700616
617 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800618 const uint32_t orientation = transform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -0700619 if (orientation & ui::Transform::ROT_INVALID) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800620 // we can only handle simple transformation
Lloyd Pique074e8122018-07-26 12:57:23 -0700621 hwcInfo.forceClientComposition = true;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700622 getBE().mHwcLayers[*displayId].compositionType = HWC2::Composition::Client;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800623 } else {
624 auto transform = static_cast<HWC2::Transform>(orientation);
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800625 hwcInfo.transform = transform;
David Sodman15094112018-10-11 09:39:37 -0700626 auto error = hwcLayer->setTransform(transform);
627 ALOGE_IF(error != HWC2::Error::None,
628 "[%s] Failed to set transform %s: "
629 "%s (%d)",
630 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
631 static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700632 getBE().compositionInfo.hwc.transform = transform;
David Sodman4b7c4bc2017-11-17 12:13:59 -0800633 }
634}
635
Dominik Laskowski075d3172018-05-24 15:50:06 -0700636void Layer::forceClientComposition(DisplayId displayId) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700637 if (getBE().mHwcLayers.count(displayId) == 0) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700638 ALOGE("forceClientComposition: no HWC layer found (display %" PRIu64 ")", displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800639 return;
640 }
641
Dominik Laskowski7e045462018-05-30 13:02:02 -0700642 getBE().mHwcLayers[displayId].forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800643}
Dan Stozaee44edd2015-03-23 15:50:23 -0700644
Dominik Laskowski075d3172018-05-24 15:50:06 -0700645bool Layer::getForceClientComposition(DisplayId displayId) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700646 if (getBE().mHwcLayers.count(displayId) == 0) {
Dominik Laskowski075d3172018-05-24 15:50:06 -0700647 ALOGE("getForceClientComposition: no HWC layer found (display %" PRIu64 ")", displayId);
chaviwc9232ed2017-11-14 15:31:15 -0800648 return false;
649 }
650
Dominik Laskowski7e045462018-05-30 13:02:02 -0700651 return getBE().mHwcLayers[displayId].forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800652}
653
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700654void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700655 const auto displayId = display->getId();
Dominik Laskowski075d3172018-05-24 15:50:06 -0700656 if (getBE().mHwcLayers.count(*displayId) == 0 ||
Dominik Laskowski7e045462018-05-30 13:02:02 -0700657 getCompositionType(displayId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800658 return;
659 }
660
661 // This gives us only the "orientation" component of the transform
662 const State& s(getCurrentState());
663
664 // Apply the layer's transform, followed by the display's global transform
665 // Here we're guaranteed that the layer's transform preserves rects
Marissa Wall61c58622018-07-18 10:12:20 -0700666 Rect win(getActiveWidth(s), getActiveHeight(s));
667 Rect crop = getCrop(s);
668 if (!crop.isEmpty()) {
669 win.intersect(crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800670 }
671 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700672 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Robert Carr1f0a16a2016-10-24 16:27:39 -0700673 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700674 frame.intersect(display->getViewport(), &frame);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700675 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800676 auto position = displayTransform.transform(frame);
677
Dominik Laskowski7e045462018-05-30 13:02:02 -0700678 auto error =
Dominik Laskowski075d3172018-05-24 15:50:06 -0700679 getBE().mHwcLayers[*displayId].layer->setCursorPosition(position.left, position.top);
680
David Sodman41fdfc92017-11-06 16:09:56 -0800681 ALOGE_IF(error != HWC2::Error::None,
682 "[%s] Failed to set cursor position "
683 "to (%d, %d): %s (%d)",
684 mName.string(), position.left, position.top, to_string(error).c_str(),
685 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800686}
Riley Andrews03414a12014-07-01 14:22:59 -0700687
Mathias Agopian13127d82013-03-05 17:47:11 -0800688// ---------------------------------------------------------------------------
689// drawing...
690// ---------------------------------------------------------------------------
691
Marissa Wall61c58622018-07-18 10:12:20 -0700692void Layer::draw(const RenderArea& renderArea, const Region& clip) {
chaviwa76b2712017-09-20 12:02:26 -0700693 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800694}
695
Marissa Wall61c58622018-07-18 10:12:20 -0700696void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) {
chaviwa76b2712017-09-20 12:02:26 -0700697 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800698}
699
David Sodman41fdfc92017-11-06 16:09:56 -0800700void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
701 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800702 auto& engine(mFlinger->getRenderEngine());
David Sodman9eeae692017-11-02 10:53:32 -0700703 computeGeometry(renderArea, getBE().mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700704 engine.setupFillWithColor(red, green, blue, alpha);
David Sodman9eeae692017-11-02 10:53:32 -0700705 engine.drawMesh(getBE().mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800706}
707
David Sodmanc1498e62018-09-12 14:36:26 -0700708void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
709 clearWithOpenGL(renderArea, 0, 0, 0, 0);
710}
711
Dominik Laskowski075d3172018-05-24 15:50:06 -0700712void Layer::setCompositionType(DisplayId displayId, HWC2::Composition type, bool callIntoHwc) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700713 if (getBE().mHwcLayers.count(displayId) == 0) {
Chia-I Wu30505fb2018-03-26 16:20:31 -0700714 ALOGE("setCompositionType called without a valid HWC layer");
715 return;
David Sodman15094112018-10-11 09:39:37 -0700716 }
717 auto& hwcInfo = getBE().mHwcLayers[displayId];
718 auto& hwcLayer = hwcInfo.layer;
719 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", (hwcLayer)->getId(), to_string(type).c_str(),
720 static_cast<int>(callIntoHwc));
721 if (hwcInfo.compositionType != type) {
722 ALOGV(" actually setting");
723 hwcInfo.compositionType = type;
724 if (callIntoHwc) {
725 auto error = (hwcLayer)->setCompositionType(type);
726 ALOGE_IF(error != HWC2::Error::None,
727 "[%s] Failed to set "
728 "composition type %s: %s (%d)",
729 mName.string(), to_string(type).c_str(), to_string(error).c_str(),
730 static_cast<int32_t>(error));
Chia-I Wu30505fb2018-03-26 16:20:31 -0700731 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800732 }
733}
734
Dominik Laskowski075d3172018-05-24 15:50:06 -0700735HWC2::Composition Layer::getCompositionType(const std::optional<DisplayId>& displayId) const {
736 if (!displayId) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700737 // If we're querying the composition type for a display that does not
738 // have a HWC counterpart, then it will always be Client
739 return HWC2::Composition::Client;
740 }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700741 if (getBE().mHwcLayers.count(*displayId) == 0) {
742 ALOGE("getCompositionType called with an invalid HWC layer");
743 return HWC2::Composition::Invalid;
744 }
745 return getBE().mHwcLayers.at(*displayId).compositionType;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800746}
747
Dominik Laskowski075d3172018-05-24 15:50:06 -0700748void Layer::setClearClientTarget(DisplayId displayId, bool clear) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700749 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800750 ALOGE("setClearClientTarget called without a valid HWC layer");
751 return;
752 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700753 getBE().mHwcLayers[displayId].clearClientTarget = clear;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800754}
755
Dominik Laskowski075d3172018-05-24 15:50:06 -0700756bool Layer::getClearClientTarget(DisplayId displayId) const {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700757 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800758 ALOGE("getClearClientTarget called without a valid HWC layer");
759 return false;
760 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700761 return getBE().mHwcLayers.at(displayId).clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800762}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800763
Dan Stozacac35382016-01-27 12:21:06 -0800764bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
765 if (point->getFrameNumber() <= mCurrentFrameNumber) {
766 // Don't bother with a SyncPoint, since we've already latched the
767 // relevant frame
768 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700769 }
Robert Carr2e102c92018-10-23 12:11:15 -0700770 if (isRemovedFromCurrentState()) {
771 return false;
772 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700773
Dan Stozacac35382016-01-27 12:21:06 -0800774 Mutex::Autolock lock(mLocalSyncPointMutex);
775 mLocalSyncPoints.push_back(point);
776 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700777}
778
Mathias Agopian13127d82013-03-05 17:47:11 -0800779// ----------------------------------------------------------------------------
780// local state
781// ----------------------------------------------------------------------------
782
Peiyong Lin833074a2018-08-28 11:53:54 -0700783void Layer::computeGeometry(const RenderArea& renderArea,
784 renderengine::Mesh& mesh,
chaviwa76b2712017-09-20 12:02:26 -0700785 bool useIdentityTransform) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700786 const ui::Transform renderAreaTransform(renderArea.getTransform());
Dan Stoza80d61162017-12-20 15:57:52 -0800787 FloatRect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700788
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000789 vec2 lt = vec2(win.left, win.top);
790 vec2 lb = vec2(win.left, win.bottom);
791 vec2 rb = vec2(win.right, win.bottom);
792 vec2 rt = vec2(win.right, win.top);
793
Peiyong Linefefaac2018-08-17 12:27:51 -0700794 ui::Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000795 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700796 lt = layerTransform.transform(lt);
797 lb = layerTransform.transform(lb);
798 rb = layerTransform.transform(rb);
799 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000800 }
801
Peiyong Lin833074a2018-08-28 11:53:54 -0700802 renderengine::Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700803 position[0] = renderAreaTransform.transform(lt);
804 position[1] = renderAreaTransform.transform(lb);
805 position[2] = renderAreaTransform.transform(rb);
806 position[3] = renderAreaTransform.transform(rt);
Mathias Agopian13127d82013-03-05 17:47:11 -0800807}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800808
David Sodman41fdfc92017-11-06 16:09:56 -0800809bool Layer::isSecure() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000810 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700811 return (s.flags & layer_state_t::eLayerSecure);
812}
813
Mathias Agopian13127d82013-03-05 17:47:11 -0800814void Layer::setVisibleRegion(const Region& visibleRegion) {
815 // always called from main thread
816 this->visibleRegion = visibleRegion;
817}
818
819void Layer::setCoveredRegion(const Region& coveredRegion) {
820 // always called from main thread
821 this->coveredRegion = coveredRegion;
822}
823
David Sodman41fdfc92017-11-06 16:09:56 -0800824void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800825 // always called from main thread
826 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
827}
828
Robert Carre5f4f692018-01-12 13:12:28 -0800829void Layer::clearVisibilityRegions() {
830 visibleRegion.clear();
831 visibleNonTransparentRegion.clear();
832 coveredRegion.clear();
833}
834
Mathias Agopian13127d82013-03-05 17:47:11 -0800835// ----------------------------------------------------------------------------
836// transaction
837// ----------------------------------------------------------------------------
838
Dan Stoza7dde5992015-05-22 09:51:44 -0700839void Layer::pushPendingState() {
840 if (!mCurrentState.modified) {
841 return;
842 }
843
Dan Stoza7dde5992015-05-22 09:51:44 -0700844 // If this transaction is waiting on the receipt of a frame, generate a sync
845 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700846 // We don't allow installing sync points after we are removed from the current state
847 // as we won't be able to signal our end.
848 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700849 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800850 if (barrierLayer == nullptr) {
851 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700852 // If we can't promote the layer we are intended to wait on,
853 // then it is expired or otherwise invalid. Allow this transaction
854 // to be applied as per normal (no synchronization).
Marissa Wallf58c14b2018-07-24 10:50:43 -0700855 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800856 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700857 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy);
Robert Carr0d480722017-01-10 16:42:54 -0800858 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800859 mRemoteSyncPoints.push_back(std::move(syncPoint));
860 } else {
861 // We already missed the frame we're supposed to synchronize
862 // on, so go ahead and apply the state update
Marissa Wallf58c14b2018-07-24 10:50:43 -0700863 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800864 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700865 }
866
Dan Stoza7dde5992015-05-22 09:51:44 -0700867 // Wake us up to check if the frame has been received
868 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700869 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700870 }
871 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700872 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700873}
874
Pablo Ceballos05289c22016-04-14 15:49:55 -0700875void Layer::popPendingState(State* stateToCommit) {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700876 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700877
878 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700879 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700880}
881
Pablo Ceballos05289c22016-04-14 15:49:55 -0700882bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700883 bool stateUpdateAvailable = false;
884 while (!mPendingStates.empty()) {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700885 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700886 if (mRemoteSyncPoints.empty()) {
887 // If we don't have a sync point for this, apply it anyway. It
888 // will be visually wrong, but it should keep us from getting
889 // into too much trouble.
890 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700891 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700892 stateUpdateAvailable = true;
893 continue;
894 }
895
Marissa Wallf58c14b2018-07-24 10:50:43 -0700896 if (mRemoteSyncPoints.front()->getFrameNumber() !=
897 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800898 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800899
900 // Signal our end of the sync point and then dispose of it
901 mRemoteSyncPoints.front()->setTransactionApplied();
902 mRemoteSyncPoints.pop_front();
903 continue;
904 }
905
Dan Stoza7dde5992015-05-22 09:51:44 -0700906 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
907 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700908 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700909 stateUpdateAvailable = true;
910
911 // Signal our end of the sync point and then dispose of it
912 mRemoteSyncPoints.front()->setTransactionApplied();
913 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800914 } else {
915 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700916 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700917 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700918 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700919 stateUpdateAvailable = true;
920 }
921 }
922
923 // If we still have pending updates, wake SurfaceFlinger back up and point
924 // it at this layer so we can process them
925 if (!mPendingStates.empty()) {
926 setTransactionFlags(eTransactionNeeded);
927 mFlinger->setTransactionFlags(eTraversalNeeded);
928 }
929
930 mCurrentState.modified = false;
931 return stateUpdateAvailable;
932}
933
Marissa Wall61c58622018-07-18 10:12:20 -0700934uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000935 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800936
Marissa Wall61c58622018-07-18 10:12:20 -0700937 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
938 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700939
David Sodmaneb085e02017-10-05 18:49:04 -0700940 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700941 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000942 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800943 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
944 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
945 " requested={ wh={%4u,%4u} }}\n"
946 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
947 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700948 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700949 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
950 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
951 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
952 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
953 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700954 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
955 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
956 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800957 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700958
Robert Carre392b552017-09-19 12:16:05 -0700959 // Don't let Layer::doTransaction update the drawing state
960 // if we have a pending resize, unless we are in fixed-size mode.
961 // the drawing state will be updated only once we receive a buffer
962 // with the correct size.
963 //
964 // In particular, we want to make sure the clip (which is part
965 // of the geometry state) is latched together with the size but is
966 // latched immediately when no resizing is involved.
967 //
968 // If a sideband stream is attached, however, we want to skip this
969 // optimization so that transactions aren't missed when a buffer
970 // never arrives
971 //
972 // In the case that we don't have a buffer we ignore other factors
973 // and avoid entering the resizePending state. At a high level the
974 // resizePending state is to avoid applying the state of the new buffer
975 // to the old buffer. However in the state where we don't have an old buffer
976 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700977 const bool resizePending =
978 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
979 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
David Sodman0cc69182017-11-17 12:12:07 -0800980 (getBE().compositionInfo.mBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700981 if (!isFixedSize()) {
David Sodman0cc69182017-11-17 12:12:07 -0800982 if (resizePending && getBE().compositionInfo.hwc.sidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700983 flags |= eDontUpdateGeometryState;
984 }
985 }
986
Robert Carr7bf247e2017-05-18 14:02:49 -0700987 // Here we apply various requested geometry states, depending on our
988 // latching configuration. See Layer.h for a detailed discussion of
989 // how geometry latching is controlled.
990 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000991 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700992
993 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
994 // mode, which causes attributes which normally latch regardless of scaling mode,
995 // to be delayed. We copy the requested state to the active state making sure
996 // to respect these rules (again see Layer.h for a detailed discussion).
997 //
998 // There is an awkward asymmetry in the handling of the crop states in the position
999 // states, as can be seen below. Largely this arises from position and transform
1000 // being stored in the same data structure while having different latching rules.
1001 // b/38182305
1002 //
Marissa Wall61c58622018-07-18 10:12:20 -07001003 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -07001004 // applyPendingStates in the presence of deferred transactions.
1005 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -07001006 float tx = stateToCommit->active_legacy.transform.tx();
1007 float ty = stateToCommit->active_legacy.transform.ty();
1008 stateToCommit->active_legacy = stateToCommit->requested_legacy;
1009 stateToCommit->active_legacy.transform.set(tx, ty);
1010 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001011 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001012 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -07001013 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001014 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001015 }
1016
Marissa Wall61c58622018-07-18 10:12:20 -07001017 return flags;
1018}
1019
1020uint32_t Layer::doTransaction(uint32_t flags) {
1021 ATRACE_CALL();
1022
1023 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +00001024 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -07001025 if (!applyPendingStates(&c)) {
1026 return 0;
1027 }
1028
1029 flags = doTransactionResize(flags, &c);
1030
Alec Mourib416efd2018-09-06 21:01:59 +00001031 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -07001032
1033 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001034 // invalidate and recompute the visible regions if needed
1035 flags |= Layer::eVisibleRegion;
1036 }
1037
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001038 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001039 // invalidate and recompute the visible regions if needed
1040 flags |= eVisibleRegion;
1041 this->contentDirty = true;
1042
1043 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -07001044 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -07001045 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -08001046 }
1047
Dan Stozac8145172016-04-28 16:29:06 -07001048 // If the layer is hidden, signal and clear out all local sync points so
1049 // that transactions for layers depending on this layer's frames becoming
1050 // visible are not blocked
1051 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001052 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001053 }
1054
Mathias Agopian13127d82013-03-05 17:47:11 -08001055 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001056 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001057 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001058}
1059
Pablo Ceballos05289c22016-04-14 15:49:55 -07001060void Layer::commitTransaction(const State& stateToCommit) {
1061 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001062}
1063
Mathias Agopian13127d82013-03-05 17:47:11 -08001064uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Piquef1c675b2018-09-12 20:45:39 -07001065 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -08001066}
1067
1068uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Piquef1c675b2018-09-12 20:45:39 -07001069 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -08001070}
1071
Robert Carr82364e32016-05-15 11:27:47 -07001072bool Layer::setPosition(float x, float y, bool immediate) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001073 if (mCurrentState.requested_legacy.transform.tx() == x &&
1074 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001075 return false;
1076 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001077
1078 // We update the requested and active position simultaneously because
1079 // we want to apply the position portion of the transform matrix immediately,
1080 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Marissa Wallf58c14b2018-07-24 10:50:43 -07001081 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001082 if (immediate && !mFreezeGeometryUpdates) {
1083 // Here we directly update the active state
1084 // unlike other setters, because we store it within
1085 // the transform, but use different latching rules.
1086 // b/38182305
Marissa Wallf58c14b2018-07-24 10:50:43 -07001087 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001088 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001089 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001090
Dan Stoza7dde5992015-05-22 09:51:44 -07001091 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001092 setTransactionFlags(eTransactionNeeded);
1093 return true;
1094}
Robert Carr82364e32016-05-15 11:27:47 -07001095
Robert Carr1f0a16a2016-10-24 16:27:39 -07001096bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1097 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1098 if (idx < 0) {
1099 return false;
1100 }
1101 if (childLayer->setLayer(z)) {
1102 mCurrentChildren.removeAt(idx);
1103 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001104 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001105 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001106 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001107}
1108
Robert Carr503c7042017-09-27 15:06:08 -07001109bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1110 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1111 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1112 if (idx < 0) {
1113 return false;
1114 }
1115 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1116 mCurrentChildren.removeAt(idx);
1117 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001118 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001119 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001120 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001121}
1122
Robert Carrae060832016-11-28 10:51:00 -08001123bool Layer::setLayer(int32_t z) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001124 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001125 mCurrentState.sequence++;
1126 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001127 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001128
1129 // Discard all relative layering.
1130 if (mCurrentState.zOrderRelativeOf != nullptr) {
1131 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1132 if (strongRelative != nullptr) {
1133 strongRelative->removeZOrderRelative(this);
1134 }
1135 mCurrentState.zOrderRelativeOf = nullptr;
1136 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001137 setTransactionFlags(eTransactionNeeded);
1138 return true;
1139}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001140
Robert Carrdb66e622017-04-10 16:55:57 -07001141void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1142 mCurrentState.zOrderRelatives.remove(relative);
1143 mCurrentState.sequence++;
1144 mCurrentState.modified = true;
1145 setTransactionFlags(eTransactionNeeded);
1146}
1147
1148void Layer::addZOrderRelative(const wp<Layer>& relative) {
1149 mCurrentState.zOrderRelatives.add(relative);
1150 mCurrentState.modified = true;
1151 mCurrentState.sequence++;
1152 setTransactionFlags(eTransactionNeeded);
1153}
1154
Robert Carr503d2bd2017-12-04 15:49:47 -08001155bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001156 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1157 if (handle == nullptr) {
1158 return false;
1159 }
1160 sp<Layer> relative = handle->owner.promote();
1161 if (relative == nullptr) {
1162 return false;
1163 }
1164
Robert Carr503d2bd2017-12-04 15:49:47 -08001165 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1166 mCurrentState.zOrderRelativeOf == relative) {
1167 return false;
1168 }
1169
Robert Carrdb66e622017-04-10 16:55:57 -07001170 mCurrentState.sequence++;
1171 mCurrentState.modified = true;
Robert Carr503d2bd2017-12-04 15:49:47 -08001172 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001173
chaviw9ab4bd12017-11-03 13:11:00 -07001174 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
1175 if (oldZOrderRelativeOf != nullptr) {
1176 oldZOrderRelativeOf->removeZOrderRelative(this);
1177 }
Robert Carrdb66e622017-04-10 16:55:57 -07001178 mCurrentState.zOrderRelativeOf = relative;
1179 relative->addZOrderRelative(this);
1180
1181 setTransactionFlags(eTransactionNeeded);
1182
1183 return true;
1184}
1185
Mathias Agopian13127d82013-03-05 17:47:11 -08001186bool Layer::setSize(uint32_t w, uint32_t h) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001187 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
1188 return false;
1189 mCurrentState.requested_legacy.w = w;
1190 mCurrentState.requested_legacy.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001191 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001192 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001193
1194 // record the new size, from this point on, when the client request
1195 // a buffer, it'll get the new size.
1196 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001197 return true;
1198}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001199bool Layer::setAlpha(float alpha) {
David Sodman41fdfc92017-11-06 16:09:56 -08001200 if (mCurrentState.color.a == alpha) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001201 mCurrentState.sequence++;
chaviw13fdc492017-06-27 12:40:18 -07001202 mCurrentState.color.a = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001203 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001204 setTransactionFlags(eTransactionNeeded);
1205 return true;
1206}
chaviw13fdc492017-06-27 12:40:18 -07001207
1208bool Layer::setColor(const half3& color) {
David Sodman41fdfc92017-11-06 16:09:56 -08001209 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g &&
1210 color.b == mCurrentState.color.b)
chaviw13fdc492017-06-27 12:40:18 -07001211 return false;
1212
1213 mCurrentState.sequence++;
1214 mCurrentState.color.r = color.r;
1215 mCurrentState.color.g = color.g;
1216 mCurrentState.color.b = color.b;
1217 mCurrentState.modified = true;
1218 setTransactionFlags(eTransactionNeeded);
1219 return true;
1220}
1221
Robert Carrd4ae7f32018-06-07 16:10:57 -07001222bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1223 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001224 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001225 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1226
1227 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1228 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1229 return false;
1230 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001231 mCurrentState.sequence++;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001232 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1233 matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001234 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001235 setTransactionFlags(eTransactionNeeded);
1236 return true;
1237}
Marissa Wall61c58622018-07-18 10:12:20 -07001238
Mathias Agopian13127d82013-03-05 17:47:11 -08001239bool Layer::setTransparentRegionHint(const Region& transparent) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001240 mCurrentState.requestedTransparentRegion_legacy = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001241 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001242 setTransactionFlags(eTransactionNeeded);
1243 return true;
1244}
1245bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1246 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
David Sodman41fdfc92017-11-06 16:09:56 -08001247 if (mCurrentState.flags == newFlags) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001248 mCurrentState.sequence++;
1249 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001250 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001251 setTransactionFlags(eTransactionNeeded);
1252 return true;
1253}
Robert Carr99e27f02016-06-16 15:18:02 -07001254
Marissa Wallf58c14b2018-07-24 10:50:43 -07001255bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
1256 if (mCurrentState.requestedCrop_legacy == crop) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001257 mCurrentState.sequence++;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001258 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001259 if (immediate && !mFreezeGeometryUpdates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001260 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001261 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001262 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1263
Dan Stoza7dde5992015-05-22 09:51:44 -07001264 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001265 setTransactionFlags(eTransactionNeeded);
1266 return true;
1267}
Robert Carr8d5227b2017-03-16 15:41:03 -07001268
Robert Carrc3574f72016-03-24 12:19:32 -07001269bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001270 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001271 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001272 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001273 return true;
1274}
1275
rongliucfb187b2018-03-14 12:26:23 -07001276void Layer::setInfo(int32_t type, int32_t appId) {
David Sodman41fdfc92017-11-06 16:09:56 -08001277 mCurrentState.appId = appId;
1278 mCurrentState.type = type;
1279 mCurrentState.modified = true;
1280 setTransactionFlags(eTransactionNeeded);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001281}
1282
Mathias Agopian13127d82013-03-05 17:47:11 -08001283bool Layer::setLayerStack(uint32_t layerStack) {
David Sodman41fdfc92017-11-06 16:09:56 -08001284 if (mCurrentState.layerStack == layerStack) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001285 mCurrentState.sequence++;
1286 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001287 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001288 setTransactionFlags(eTransactionNeeded);
1289 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001290}
1291
Robert Carr1f0a16a2016-10-24 16:27:39 -07001292uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001293 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001294 if (p == nullptr) {
1295 return getDrawingState().layerStack;
1296 }
1297 return p->getLayerStack();
1298}
1299
Marissa Wallf58c14b2018-07-24 10:50:43 -07001300void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
1301 mCurrentState.barrierLayer_legacy = barrierLayer;
1302 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001303 // We don't set eTransactionNeeded, because just receiving a deferral
1304 // request without any other state updates shouldn't actually induce a delay
1305 mCurrentState.modified = true;
1306 pushPendingState();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001307 mCurrentState.barrierLayer_legacy = nullptr;
1308 mCurrentState.frameNumber_legacy = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001309 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001310}
1311
Marissa Wallf58c14b2018-07-24 10:50:43 -07001312void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001313 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001314 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001315}
1316
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001317// ----------------------------------------------------------------------------
1318// pageflip handling...
1319// ----------------------------------------------------------------------------
1320
Robert Carr1f0a16a2016-10-24 16:27:39 -07001321bool Layer::isHiddenByPolicy() const {
Alec Mourib416efd2018-09-06 21:01:59 +00001322 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001323 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001324 if (parent != nullptr && parent->isHiddenByPolicy()) {
1325 return true;
1326 }
1327 return s.flags & layer_state_t::eLayerHidden;
1328}
1329
David Sodman41fdfc92017-11-06 16:09:56 -08001330uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001331 // TODO: should we do something special if mSecure is set?
1332 if (mProtectedByApp) {
1333 // need a hardware-protected path to external video sink
1334 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001335 }
Riley Andrews03414a12014-07-01 14:22:59 -07001336 if (mPotentialCursor) {
1337 usage |= GraphicBuffer::USAGE_CURSOR;
1338 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001339 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001340 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001341}
1342
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001343void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001344 uint32_t orientation = 0;
1345 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001346 // The transform hint is used to improve performance, but we can
1347 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001348 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001349 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001350 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001351 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001352 orientation = 0;
1353 }
1354 }
David Sodmaneb085e02017-10-05 18:49:04 -07001355 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001356}
1357
Mathias Agopian13127d82013-03-05 17:47:11 -08001358// ----------------------------------------------------------------------------
1359// debugging
1360// ----------------------------------------------------------------------------
1361
Marissa Wall61c58622018-07-18 10:12:20 -07001362// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001363LayerDebugInfo Layer::getLayerDebugInfo() const {
1364 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001365 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001366 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001367 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001368 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
1369 info.mType = String8(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001370 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001371 info.mVisibleRegion = visibleRegion;
1372 info.mSurfaceDamageRegion = surfaceDamageRegion;
1373 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001374 info.mX = ds.active_legacy.transform.tx();
1375 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001376 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001377 info.mWidth = ds.active_legacy.w;
1378 info.mHeight = ds.active_legacy.h;
1379 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001380 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001381 info.mFlags = ds.flags;
1382 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001383 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001384 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1385 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1386 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1387 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001388 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001389 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001390 if (buffer != 0) {
1391 info.mActiveBufferWidth = buffer->getWidth();
1392 info.mActiveBufferHeight = buffer->getHeight();
1393 info.mActiveBufferStride = buffer->getStride();
1394 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001395 } else {
1396 info.mActiveBufferWidth = 0;
1397 info.mActiveBufferHeight = 0;
1398 info.mActiveBufferStride = 0;
1399 info.mActiveBufferFormat = 0;
1400 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001401 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001402 info.mNumQueuedFrames = getQueuedFrameCount();
1403 info.mRefreshPending = isBufferLatched();
1404 info.mIsOpaque = isOpaque(ds);
1405 info.mContentDirty = contentDirty;
1406 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001407}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001408
Dan Stozae22aec72016-08-01 13:20:59 -07001409void Layer::miniDumpHeader(String8& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001410 result.append("-------------------------------");
1411 result.append("-------------------------------");
1412 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001413 result.append(" Layer name\n");
1414 result.append(" Z | ");
1415 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001416 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001417 result.append(" Disp Frame (LTRB) | ");
1418 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001419 result.append("-------------------------------");
1420 result.append("-------------------------------");
1421 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001422}
1423
Dominik Laskowski075d3172018-05-24 15:50:06 -07001424void Layer::miniDump(String8& result, DisplayId displayId) const {
Dominik Laskowski7e045462018-05-30 13:02:02 -07001425 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stozae22aec72016-08-01 13:20:59 -07001426 return;
1427 }
1428
1429 String8 name;
1430 if (mName.length() > 77) {
1431 std::string shortened;
1432 shortened.append(mName.string(), 36);
1433 shortened.append("[...]");
1434 shortened.append(mName.string() + (mName.length() - 36), 36);
1435 name = shortened.c_str();
1436 } else {
1437 name = mName;
1438 }
1439
1440 result.appendFormat(" %s\n", name.string());
1441
Alec Mourib416efd2018-09-06 21:01:59 +00001442 const State& layerState(getDrawingState());
Lloyd Pique074e8122018-07-26 12:57:23 -07001443 const LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers.at(displayId);
Chia-I Wu1e043612018-03-01 09:45:09 -08001444 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
1445 result.appendFormat(" rel %6d | ", layerState.z);
1446 } else {
1447 result.appendFormat(" %10d | ", layerState.z);
1448 }
Dominik Laskowski7e045462018-05-30 13:02:02 -07001449 result.appendFormat("%10s | ", to_string(getCompositionType(displayId)).c_str());
Lloyd Pique074e8122018-07-26 12:57:23 -07001450 result.appendFormat("%10s | ", to_string(hwcInfo.transform).c_str());
1451 const Rect& frame = hwcInfo.displayFrame;
David Sodman41fdfc92017-11-06 16:09:56 -08001452 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique074e8122018-07-26 12:57:23 -07001453 const FloatRect& crop = hwcInfo.sourceCrop;
David Sodman41fdfc92017-11-06 16:09:56 -08001454 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 -07001455
David Sodman7e4ae112018-02-09 15:02:28 -08001456 result.append("- - - - - - - - - - - - - - - -\n");
1457
1458 std::string compositionInfoStr;
1459 getBE().compositionInfo.dump(compositionInfoStr, "compositionInfo");
1460 result.append(compositionInfoStr.c_str());
1461
Yichi Chen6ca35192018-05-29 12:20:43 +08001462 result.append("- - - - - - - - - - - - - - - -");
1463 result.append("- - - - - - - - - - - - - - - -");
1464 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001465}
Dan Stozae22aec72016-08-01 13:20:59 -07001466
Svetoslavd85084b2014-03-20 10:28:31 -07001467void Layer::dumpFrameStats(String8& result) const {
1468 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001469}
1470
Svetoslavd85084b2014-03-20 10:28:31 -07001471void Layer::clearFrameStats() {
1472 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001473}
1474
Jamie Gennis6547ff42013-07-16 20:12:42 -07001475void Layer::logFrameStats() {
1476 mFrameTracker.logAndResetStats(mName);
1477}
1478
Svetoslavd85084b2014-03-20 10:28:31 -07001479void Layer::getFrameStats(FrameStats* outStats) const {
1480 mFrameTracker.getStats(outStats);
1481}
1482
Brian Andersond6927fb2016-07-23 23:37:30 -07001483void Layer::dumpFrameEvents(String8& result) {
David Sodman41fdfc92017-11-06 16:09:56 -08001484 result.appendFormat("- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001485 Mutex::Autolock lock(mFrameEventHistoryMutex);
1486 mFrameEventHistory.checkFencesForCompletion();
1487 mFrameEventHistory.dump(result);
1488}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001489
Brian Anderson5ea5e592016-12-01 16:54:33 -08001490void Layer::onDisconnect() {
1491 Mutex::Autolock lock(mFrameEventHistoryMutex);
1492 mFrameEventHistory.onDisconnect();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -07001493 mTimeStats.onDisconnect(getSequence());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001494}
1495
Brian Anderson3890c392016-07-25 12:48:08 -07001496void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001497 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001498 if (newTimestamps) {
Yiwei Zhang8e8fe522018-11-02 18:34:07 -07001499 mTimeStats.setPostTime(getSequence(), newTimestamps->frameNumber, getName().c_str(),
1500 newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001501 }
1502
Brian Andersond6927fb2016-07-23 23:37:30 -07001503 Mutex::Autolock lock(mFrameEventHistoryMutex);
1504 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001505 // If there are any unsignaled fences in the aquire timeline at this
1506 // point, the previously queued frame hasn't been latched yet. Go ahead
1507 // and try to get the signal time here so the syscall is taken out of
1508 // the main thread's critical path.
1509 mAcquireTimeline.updateSignalTimes();
1510 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001511 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001512 mFrameEventHistory.addQueue(*newTimestamps);
1513 }
1514
Brian Anderson3890c392016-07-25 12:48:08 -07001515 if (outDelta) {
1516 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001517 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001518}
Dan Stozae77c7662016-05-13 11:37:28 -07001519
Chia-I Wu98f1c102017-05-30 14:54:08 -07001520size_t Layer::getChildrenCount() const {
1521 size_t count = 0;
1522 for (const sp<Layer>& child : mCurrentChildren) {
1523 count += 1 + child->getChildrenCount();
1524 }
1525 return count;
1526}
1527
Robert Carr1f0a16a2016-10-24 16:27:39 -07001528void Layer::addChild(const sp<Layer>& layer) {
1529 mCurrentChildren.add(layer);
1530 layer->setParent(this);
1531}
1532
1533ssize_t Layer::removeChild(const sp<Layer>& layer) {
1534 layer->setParent(nullptr);
1535 return mCurrentChildren.remove(layer);
1536}
1537
Robert Carr1db73f62016-12-21 12:58:51 -08001538bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1539 sp<Handle> handle = nullptr;
1540 sp<Layer> newParent = nullptr;
1541 if (newParentHandle == nullptr) {
1542 return false;
1543 }
1544 handle = static_cast<Handle*>(newParentHandle.get());
1545 newParent = handle->owner.promote();
1546 if (newParent == nullptr) {
1547 ALOGE("Unable to promote Layer handle");
1548 return false;
1549 }
1550
1551 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001552 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001553
1554 sp<Client> client(child->mClientRef.promote());
1555 if (client != nullptr) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001556 client->updateParent(newParent);
Robert Carr1db73f62016-12-21 12:58:51 -08001557 }
1558 }
1559 mCurrentChildren.clear();
1560
1561 return true;
1562}
1563
Robert Carr15eae092018-03-23 13:43:53 -07001564void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001565 for (const sp<Layer>& child : mDrawingChildren) {
1566 child->mDrawingParent = newParent;
1567 }
1568}
1569
chaviwf1961f72017-09-18 16:41:07 -07001570bool Layer::reparent(const sp<IBinder>& newParentHandle) {
1571 if (newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001572 return false;
1573 }
1574
1575 auto handle = static_cast<Handle*>(newParentHandle.get());
1576 sp<Layer> newParent = handle->owner.promote();
1577 if (newParent == nullptr) {
1578 ALOGE("Unable to promote Layer handle");
1579 return false;
1580 }
1581
chaviwf1961f72017-09-18 16:41:07 -07001582 sp<Layer> parent = getParent();
1583 if (parent != nullptr) {
1584 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001585 }
chaviwf1961f72017-09-18 16:41:07 -07001586 newParent->addChild(this);
chaviw06178942017-07-27 10:25:59 -07001587
chaviwf1961f72017-09-18 16:41:07 -07001588 sp<Client> client(mClientRef.promote());
chaviw06178942017-07-27 10:25:59 -07001589 sp<Client> newParentClient(newParent->mClientRef.promote());
1590
chaviwf1961f72017-09-18 16:41:07 -07001591 if (client != newParentClient) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001592 client->updateParent(newParent);
chaviw06178942017-07-27 10:25:59 -07001593 }
1594
chaviw06178942017-07-27 10:25:59 -07001595 return true;
1596}
1597
Robert Carr9524cb32017-02-13 11:32:32 -08001598bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001599 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001600 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001601 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001602 if (client != nullptr && parentClient != client) {
Robert Carr7f619b22017-11-06 12:56:35 -08001603 client->detachLayer(child.get());
1604 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001605 }
Robert Carr7f619b22017-11-06 12:56:35 -08001606 }
Robert Carr9524cb32017-02-13 11:32:32 -08001607
1608 return true;
1609}
1610
Peiyong Lind3788632018-09-18 16:01:31 -07001611bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001612 static const mat4 identityMatrix = mat4();
1613
Peiyong Lind3788632018-09-18 16:01:31 -07001614 if (mCurrentState.colorTransform == matrix) {
1615 return false;
1616 }
1617 ++mCurrentState.sequence;
1618 mCurrentState.colorTransform = matrix;
Peiyong Lin747321c2018-10-01 10:03:11 -07001619 mCurrentState.hasColorTransform = matrix != identityMatrix;
1620 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001621 setTransactionFlags(eTransactionNeeded);
1622 return true;
1623}
1624
1625const mat4& Layer::getColorTransform() const {
1626 return getDrawingState().colorTransform;
1627}
1628
1629bool Layer::hasColorTransform() const {
Peiyong Lin747321c2018-10-01 10:03:11 -07001630 return getDrawingState().hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001631}
1632
Chia-I Wu11481472018-05-04 10:43:19 -07001633bool Layer::isLegacyDataSpace() const {
1634 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001635 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001636 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001637}
1638
Robert Carr1f0a16a2016-10-24 16:27:39 -07001639void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001640 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001641}
1642
1643void Layer::clearSyncPoints() {
1644 for (const auto& child : mCurrentChildren) {
1645 child->clearSyncPoints();
1646 }
1647
1648 Mutex::Autolock lock(mLocalSyncPointMutex);
1649 for (auto& point : mLocalSyncPoints) {
1650 point->setFrameAvailable();
1651 }
1652 mLocalSyncPoints.clear();
1653}
1654
1655int32_t Layer::getZ() const {
1656 return mDrawingState.z;
1657}
1658
Robert Carr29abff82017-12-04 13:51:20 -08001659bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1660 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1661 const State& state = useDrawing ? mDrawingState : mCurrentState;
1662 return state.zOrderRelativeOf != nullptr;
1663}
1664
David Sodman41fdfc92017-11-06 16:09:56 -08001665__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001666 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001667 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1668 "makeTraversalList received invalid stateSet");
1669 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1670 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1671 const State& state = useDrawing ? mDrawingState : mCurrentState;
1672
Robert Carr29abff82017-12-04 13:51:20 -08001673 if (state.zOrderRelatives.size() == 0) {
1674 *outSkipRelativeZUsers = true;
1675 return children;
1676 }
1677
chaviwfd462612018-05-31 16:11:27 -07001678 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001679 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001680 sp<Layer> strongRelative = weakRelative.promote();
1681 if (strongRelative != nullptr) {
1682 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001683 }
1684 }
1685
Dan Stoza412903f2017-04-27 13:42:17 -07001686 for (const sp<Layer>& child : children) {
Robert Carr503c7042017-09-27 15:06:08 -07001687 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1688 if (childState.zOrderRelativeOf != nullptr) {
1689 continue;
1690 }
Robert Carrdb66e622017-04-10 16:55:57 -07001691 traverse.add(child);
1692 }
1693
1694 return traverse;
1695}
1696
Robert Carr1f0a16a2016-10-24 16:27:39 -07001697/**
Robert Carrdb66e622017-04-10 16:55:57 -07001698 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001699 */
Dan Stoza412903f2017-04-27 13:42:17 -07001700void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001701 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1702 // produce a new list for traversing, including our relatives, and not including our children
1703 // who are relatives of another surface. In the case that there are no relative Z,
1704 // makeTraversalList returns our children directly to avoid significant overhead.
1705 // However in this case we need to take the responsibility for filtering children which
1706 // are relatives of another surface here.
1707 bool skipRelativeZUsers = false;
1708 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001709
Robert Carr1f0a16a2016-10-24 16:27:39 -07001710 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001711 for (; i < list.size(); i++) {
1712 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001713 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1714 continue;
1715 }
1716
Robert Carrdb66e622017-04-10 16:55:57 -07001717 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001718 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001719 }
Dan Stoza412903f2017-04-27 13:42:17 -07001720 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001721 }
Robert Carr29abff82017-12-04 13:51:20 -08001722
Dan Stoza412903f2017-04-27 13:42:17 -07001723 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001724 for (; i < list.size(); i++) {
1725 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001726
1727 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1728 continue;
1729 }
Dan Stoza412903f2017-04-27 13:42:17 -07001730 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001731 }
1732}
1733
1734/**
Robert Carrdb66e622017-04-10 16:55:57 -07001735 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001736 */
Dan Stoza412903f2017-04-27 13:42:17 -07001737void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1738 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001739 // See traverseInZOrder for documentation.
1740 bool skipRelativeZUsers = false;
1741 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001742
Robert Carr1f0a16a2016-10-24 16:27:39 -07001743 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001744 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001745 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001746
1747 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1748 continue;
1749 }
1750
Robert Carrdb66e622017-04-10 16:55:57 -07001751 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001752 break;
1753 }
Dan Stoza412903f2017-04-27 13:42:17 -07001754 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001755 }
Dan Stoza412903f2017-04-27 13:42:17 -07001756 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001757 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001758 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001759
1760 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1761 continue;
1762 }
1763
Dan Stoza412903f2017-04-27 13:42:17 -07001764 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001765 }
1766}
1767
chaviw4b129c22018-04-09 16:19:43 -07001768LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1769 const std::vector<Layer*>& layersInTree) {
1770 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1771 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001772 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1773 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
chaviw4b129c22018-04-09 16:19:43 -07001774 const State& state = useDrawing ? mDrawingState : mCurrentState;
1775
chaviwfd462612018-05-31 16:11:27 -07001776 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001777 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1778 sp<Layer> strongRelative = weakRelative.promote();
1779 // Only add relative layers that are also descendents of the top most parent of the tree.
1780 // If a relative layer is not a descendent, then it should be ignored.
1781 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1782 traverse.add(strongRelative);
1783 }
1784 }
1785
1786 for (const sp<Layer>& child : children) {
1787 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1788 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1789 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1790 // the child here since it won't be added later as a relative.
1791 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1792 childState.zOrderRelativeOf.promote().get())) {
1793 continue;
1794 }
1795 traverse.add(child);
1796 }
1797
1798 return traverse;
1799}
1800
1801void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1802 LayerVector::StateSet stateSet,
1803 const LayerVector::Visitor& visitor) {
1804 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001805
1806 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001807 for (; i < list.size(); i++) {
1808 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001809 if (relative->getZ() >= 0) {
1810 break;
1811 }
chaviw4b129c22018-04-09 16:19:43 -07001812 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001813 }
chaviw4b129c22018-04-09 16:19:43 -07001814
chaviwa76b2712017-09-20 12:02:26 -07001815 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001816 for (; i < list.size(); i++) {
1817 const auto& relative = list[i];
1818 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001819 }
1820}
1821
chaviw4b129c22018-04-09 16:19:43 -07001822std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1823 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1824 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1825
1826 std::vector<Layer*> layersInTree = {this};
1827 for (size_t i = 0; i < children.size(); i++) {
1828 const auto& child = children[i];
1829 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1830 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1831 }
1832
1833 return layersInTree;
1834}
1835
1836void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1837 const LayerVector::Visitor& visitor) {
1838 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1839 std::sort(layersInTree.begin(), layersInTree.end());
1840 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1841}
1842
Peiyong Linefefaac2018-08-17 12:27:51 -07001843ui::Transform Layer::getTransform() const {
1844 ui::Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001845 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001846 if (p != nullptr) {
1847 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07001848
1849 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
1850 // it isFixedSize) then there may be additional scaling not accounted
1851 // for in the transform. We need to mirror this scaling in child surfaces
1852 // or we will break the contract where WM can treat child surfaces as
1853 // pixels in the parent surface.
David Sodman0cc69182017-11-17 12:12:07 -08001854 if (p->isFixedSize() && p->getBE().compositionInfo.mBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07001855 int bufferWidth;
1856 int bufferHeight;
1857 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
David Sodman0cc69182017-11-17 12:12:07 -08001858 bufferWidth = p->getBE().compositionInfo.mBuffer->getWidth();
1859 bufferHeight = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001860 } else {
David Sodman0cc69182017-11-17 12:12:07 -08001861 bufferHeight = p->getBE().compositionInfo.mBuffer->getWidth();
1862 bufferWidth = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001863 }
Marissa Wall61c58622018-07-18 10:12:20 -07001864 float sx = p->getActiveWidth(p->getDrawingState()) / static_cast<float>(bufferWidth);
1865 float sy = p->getActiveHeight(p->getDrawingState()) / static_cast<float>(bufferHeight);
Peiyong Linefefaac2018-08-17 12:27:51 -07001866 ui::Transform extraParentScaling;
Robert Carr9b429f42017-04-17 14:56:57 -07001867 extraParentScaling.set(sx, 0, 0, sy);
1868 t = t * extraParentScaling;
1869 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001870 }
Marissa Wall61c58622018-07-18 10:12:20 -07001871 return t * getActiveTransform(getDrawingState());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001872}
1873
chaviw13fdc492017-06-27 12:40:18 -07001874half Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001875 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07001876
chaviw13fdc492017-06-27 12:40:18 -07001877 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1878 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001879}
Robert Carr6452f122017-03-21 10:41:29 -07001880
chaviw13fdc492017-06-27 12:40:18 -07001881half4 Layer::getColor() const {
1882 const half4 color(getDrawingState().color);
1883 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001884}
Robert Carr6452f122017-03-21 10:41:29 -07001885
Robert Carr1f0a16a2016-10-24 16:27:39 -07001886void Layer::commitChildList() {
1887 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1888 const auto& child = mCurrentChildren[i];
1889 child->commitChildList();
1890 }
1891 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001892 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001893}
1894
chaviw1d044282017-09-27 12:19:28 -07001895void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
1896 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1897 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1898 const State& state = useDrawing ? mDrawingState : mCurrentState;
1899
Peiyong Linefefaac2018-08-17 12:27:51 -07001900 ui::Transform requestedTransform = state.active_legacy.transform;
1901 ui::Transform transform = getTransform();
chaviw1d044282017-09-27 12:19:28 -07001902
1903 layerInfo->set_id(sequence);
1904 layerInfo->set_name(getName().c_str());
1905 layerInfo->set_type(String8(getTypeId()));
1906
1907 for (const auto& child : children) {
1908 layerInfo->add_children(child->sequence);
1909 }
1910
1911 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1912 sp<Layer> strongRelative = weakRelative.promote();
1913 if (strongRelative != nullptr) {
1914 layerInfo->add_relatives(strongRelative->sequence);
1915 }
1916 }
1917
Marissa Wallf58c14b2018-07-24 10:50:43 -07001918 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
chaviw1d044282017-09-27 12:19:28 -07001919 layerInfo->mutable_transparent_region());
1920 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
1921 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
1922
1923 layerInfo->set_layer_stack(getLayerStack());
1924 layerInfo->set_z(state.z);
1925
1926 PositionProto* position = layerInfo->mutable_position();
1927 position->set_x(transform.tx());
1928 position->set_y(transform.ty());
1929
1930 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
1931 requestedPosition->set_x(requestedTransform.tx());
1932 requestedPosition->set_y(requestedTransform.ty());
1933
1934 SizeProto* size = layerInfo->mutable_size();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001935 size->set_w(state.active_legacy.w);
1936 size->set_h(state.active_legacy.h);
chaviw1d044282017-09-27 12:19:28 -07001937
Marissa Wallf58c14b2018-07-24 10:50:43 -07001938 LayerProtoHelper::writeToProto(state.crop_legacy, layerInfo->mutable_crop());
chaviw1d044282017-09-27 12:19:28 -07001939
1940 layerInfo->set_is_opaque(isOpaque(state));
1941 layerInfo->set_invalidate(contentDirty);
Chia-I Wu01591c92018-05-22 12:03:00 -07001942
1943 // XXX (b/79210409) mCurrentDataSpace is not protected
1944 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
1945
chaviw1d044282017-09-27 12:19:28 -07001946 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1947 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
1948 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
1949 layerInfo->set_flags(state.flags);
1950
1951 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1952 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
1953
Jorim Jaggi8e0af362017-11-14 16:28:28 +01001954 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07001955 if (parent != nullptr) {
1956 layerInfo->set_parent(parent->sequence);
1957 }
1958
1959 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1960 if (zOrderRelativeOf != nullptr) {
1961 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1962 }
1963
Chia-I Wu01591c92018-05-22 12:03:00 -07001964 // XXX getBE().compositionInfo.mBuffer is not protected
David Sodman0cc69182017-11-17 12:12:07 -08001965 auto buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001966 if (buffer != nullptr) {
1967 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
Peiyong Linefefaac2018-08-17 12:27:51 -07001968 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
Yichi Chen6ca35192018-05-29 12:20:43 +08001969 layerInfo->mutable_buffer_transform());
chaviw1d044282017-09-27 12:19:28 -07001970 }
1971
1972 layerInfo->set_queued_frames(getQueuedFrameCount());
1973 layerInfo->set_refresh_pending(isBufferLatched());
rongliucfb187b2018-03-14 12:26:23 -07001974 layerInfo->set_window_type(state.type);
1975 layerInfo->set_app_id(state.appId);
chaviwadc40c22018-07-10 16:57:27 -07001976 layerInfo->set_curr_frame(mCurrentFrameNumber);
1977
1978 for (const auto& pendingState : mPendingStates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001979 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
chaviwadc40c22018-07-10 16:57:27 -07001980 if (barrierLayer != nullptr) {
1981 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
1982 barrierLayerProto->set_id(barrierLayer->sequence);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001983 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
chaviwadc40c22018-07-10 16:57:27 -07001984 }
1985 }
chaviw1d044282017-09-27 12:19:28 -07001986}
1987
Dominik Laskowski075d3172018-05-24 15:50:06 -07001988void Layer::writeToProto(LayerProto* layerInfo, DisplayId displayId) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07001989 if (!hasHwcLayer(displayId)) {
1990 return;
1991 }
1992
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001993 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
1994
Dominik Laskowski7e045462018-05-30 13:02:02 -07001995 const auto& hwcInfo = getBE().mHwcLayers.at(displayId);
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001996
1997 const Rect& frame = hwcInfo.displayFrame;
1998 LayerProtoHelper::writeToProto(frame, layerInfo->mutable_hwc_frame());
1999
2000 const FloatRect& crop = hwcInfo.sourceCrop;
2001 LayerProtoHelper::writeToProto(crop, layerInfo->mutable_hwc_crop());
2002
2003 const int32_t transform = static_cast<int32_t>(hwcInfo.transform);
2004 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002005
2006 const int32_t compositionType = static_cast<int32_t>(hwcInfo.compositionType);
2007 layerInfo->set_hwc_composition_type(compositionType);
2008
2009 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
2010 static_cast<BufferLayer*>(this)->isProtected()) {
2011 layerInfo->set_is_protected(true);
2012 } else {
2013 layerInfo->set_is_protected(false);
2014 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002015}
2016
Robert Carr2e102c92018-10-23 12:11:15 -07002017bool Layer::isRemovedFromCurrentState() const {
2018 return mRemovedFromCurrentState;
2019}
2020
Mathias Agopian13127d82013-03-05 17:47:11 -08002021// ---------------------------------------------------------------------------
2022
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002023}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002024
2025#if defined(__gl_h_)
2026#error "don't include gl/gl.h in this file"
2027#endif
2028
2029#if defined(__gl2_h_)
2030#error "don't include gl2/gl2.h in this file"
2031#endif