blob: f3182be7ccc42e117b993fe6dcc12bb5561e608b [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 Laskowski34157762018-10-31 13:07:19 -0700200 LOG_ALWAYS_FATAL_IF(hasHwcLayer(displayId), "Already have a layer for display %s",
201 to_string(displayId).c_str());
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 Laskowski34157762018-10-31 13:07:19 -0700218 if (!hasHwcLayer(displayId)) {
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
Vishnu Nair60356342018-11-13 13:00:45 -0800278Rect Layer::computeScreenBounds() const {
279 FloatRect bounds = computeBounds();
Peiyong Linefefaac2018-08-17 12:27:51 -0700280 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800281 // Transform to screen space.
282 bounds = t.transform(bounds);
283 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700284}
285
Dan Stoza80d61162017-12-20 15:57:52 -0800286FloatRect Layer::computeBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000287 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700288 return computeBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700289}
290
Dan Stoza80d61162017-12-20 15:57:52 -0800291FloatRect Layer::computeBounds(const Region& activeTransparentRegion) const {
Alec Mourib416efd2018-09-06 21:01:59 +0000292 const State& s(getDrawingState());
Vishnu Nair60356342018-11-13 13:00:45 -0800293 Rect bounds = getCroppedBufferSize(s);
294 FloatRect floatBounds = bounds.toFloatRect();
295 if (bounds.isValid()) {
296 // Layer has bounds. Pass in our bounds as a special case. Then pass on to our parents so
297 // that they can clip it.
298 floatBounds = cropChildBounds(floatBounds);
299 } else {
300 // Layer does not have bounds, so we fill to our parent bounds. This is done by getting our
301 // parent bounds and inverting the transform to get the maximum bounds we can have that
302 // will fit within our parent bounds.
303 const auto& p = mDrawingParent.promote();
304 if (p != nullptr) {
305 ui::Transform t = s.active_legacy.transform;
306 // When calculating the parent bounds for purposes of clipping, we don't need to
307 // constrain the parent to its transparent region. The transparent region is an
308 // optimization based on the buffer contents of the layer, but does not affect the
309 // space allocated to it by policy, and thus children should be allowed to extend into
310 // the parent's transparent region.
311 // One of the main uses is a parent window with a child sitting behind the parent
312 // window, marked by a transparent region. When computing the parent bounds from the
313 // parent's perspective we pass in the transparent region to reduce buffer allocation
314 // size. When computing the parent bounds from the child's perspective, we pass in an
315 // empty transparent region in order to extend into the the parent bounds.
316 floatBounds = p->computeBounds(Region());
317 // Transform back to layer space.
318 floatBounds = t.inverse().transform(floatBounds);
319 }
320 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700321
Vishnu Nair60356342018-11-13 13:00:45 -0800322 // Subtract the transparent region and snap to the bounds.
323 return reduce(floatBounds, activeTransparentRegion);
324}
325
326FloatRect Layer::cropChildBounds(const FloatRect& childBounds) const {
327 const State& s(getDrawingState());
328 Rect bounds = getCroppedBufferSize(s);
329 FloatRect croppedBounds = childBounds;
330
331 // If the layer has bounds, then crop the passed in child bounds and pass
332 // it to our parents so they can crop it as well. If the layer has no bounds,
333 // then pass on the child bounds.
334 if (bounds.isValid()) {
335 croppedBounds = croppedBounds.intersect(bounds.toFloatRect());
Mathias Agopian13127d82013-03-05 17:47:11 -0800336 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700337
Chia-I Wue41dbe62017-06-13 14:10:56 -0700338 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700339 if (p != nullptr) {
Vishnu Nair60356342018-11-13 13:00:45 -0800340 // Transform to parent space and allow parent layer to crop the
341 // child bounds as well.
342 ui::Transform t = s.active_legacy.transform;
343 croppedBounds = t.transform(croppedBounds);
344 croppedBounds = p->cropChildBounds(croppedBounds);
345 croppedBounds = t.inverse().transform(croppedBounds);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700346 }
Vishnu Nair60356342018-11-13 13:00:45 -0800347 return croppedBounds;
348}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700349
Vishnu Nair60356342018-11-13 13:00:45 -0800350Rect Layer::getCroppedBufferSize(const State& s) const {
351 Rect size = getBufferSize(s);
352 Rect crop = getCrop(s);
353 if (!crop.isEmpty() && size.isValid()) {
354 size.intersect(crop, &size);
355 } else if (!crop.isEmpty()) {
356 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700357 }
Vishnu Nair60356342018-11-13 13:00:45 -0800358 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800359}
360
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700361Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& display) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700362 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800363 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700364 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800365
366 // apply the projection's clipping to the window crop in
367 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700368 // if there are no window scaling involved, this operation will map to full
369 // pixels in the buffer.
370 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
371 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700372
Marissa Wall61c58622018-07-18 10:12:20 -0700373 Rect activeCrop(getActiveWidth(s), getActiveHeight(s));
374 Rect crop = getCrop(s);
375 if (!crop.isEmpty()) {
376 activeCrop.intersect(crop, &activeCrop);
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700377 }
378
Peiyong Linefefaac2018-08-17 12:27:51 -0700379 ui::Transform t = getTransform();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700380 activeCrop = t.transform(activeCrop);
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700381 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000382 activeCrop.clear();
383 }
chaviwb1154d12017-10-31 14:15:36 -0700384
385 const auto& p = mDrawingParent.promote();
386 if (p != nullptr) {
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700387 auto parentCrop = p->computeInitialCrop(display);
chaviwb1154d12017-10-31 14:15:36 -0700388 activeCrop.intersect(parentCrop, &activeCrop);
389 }
390
Robert Carr1f0a16a2016-10-24 16:27:39 -0700391 return activeCrop;
392}
393
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700394FloatRect Layer::computeCrop(const sp<const DisplayDevice>& display) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700395 // the content crop is the area of the content that gets scaled to the
396 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800397 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700398
399 // In addition there is a WM-specified crop we pull from our drawing state.
400 const State& s(getDrawingState());
401
402 // Screen space to make reduction to parent crop clearer.
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700403 Rect activeCrop = computeInitialCrop(display);
Peiyong Linefefaac2018-08-17 12:27:51 -0700404 ui::Transform t = getTransform();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700405 // Back to layer space to work with the content crop.
406 activeCrop = t.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800407
Michael Lentine28ea2172014-11-19 18:32:37 -0800408 // This needs to be here as transform.transform(Rect) computes the
409 // transformed rect and then takes the bounding box of the result before
410 // returning. This means
411 // transform.inverse().transform(transform.transform(Rect)) != Rect
412 // in which case we need to make sure the final rect is clipped to the
413 // display bounds.
Marissa Wall61c58622018-07-18 10:12:20 -0700414 if (!activeCrop.intersect(Rect(getActiveWidth(s), getActiveHeight(s)), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000415 activeCrop.clear();
416 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800417
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700418 // subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700419 activeCrop = reduce(activeCrop, getActiveTransparentRegion(s));
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700420
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000421 // Transform the window crop to match the buffer coordinate system,
422 // which means using the inverse of the current transform set on the
423 // SurfaceFlingerConsumer.
424 uint32_t invTransform = mCurrentTransform;
Robert Carrcae605c2017-03-29 12:10:31 -0700425 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000426 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700427 * the code below applies the primary display's inverse transform to the
428 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000429 */
David Sodman41fdfc92017-11-06 16:09:56 -0800430 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000431 // calculate the inverse transform
432 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800433 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800434 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000435 // and apply to the current transform
Peiyong Linefefaac2018-08-17 12:27:51 -0700436 invTransform = (ui::Transform(invTransformOrient) *
437 ui::Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800438 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000439
Marissa Wall61c58622018-07-18 10:12:20 -0700440 int winWidth = getActiveWidth(s);
441 int winHeight = getActiveHeight(s);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000442 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
443 // If the activeCrop has been rotate the ends are rotated but not
444 // the space itself so when transforming ends back we can't rely on
445 // a modification of the axes of rotation. To account for this we
446 // need to reorient the inverse rotation in terms of the current
447 // axes of rotation.
448 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
449 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
450 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800451 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000452 }
Marissa Wall61c58622018-07-18 10:12:20 -0700453 winWidth = getActiveHeight(s);
454 winHeight = getActiveWidth(s);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000455 }
Marissa Wall61c58622018-07-18 10:12:20 -0700456 const Rect winCrop = activeCrop.transform(invTransform, getActiveWidth(s), getActiveHeight(s));
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000457
458 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800459 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000460 float yScale = crop.getHeight() / float(winHeight);
461
David Sodman41fdfc92017-11-06 16:09:56 -0800462 float insetL = winCrop.left * xScale;
463 float insetT = winCrop.top * yScale;
464 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000465 float insetB = (winHeight - winCrop.bottom) * yScale;
466
David Sodman41fdfc92017-11-06 16:09:56 -0800467 crop.left += insetL;
468 crop.top += insetT;
469 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000470 crop.bottom -= insetB;
471
Mathias Agopian13127d82013-03-05 17:47:11 -0800472 return crop;
473}
474
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700475void Layer::setGeometry(const sp<const DisplayDevice>& display, uint32_t z) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700476 const auto displayId = display->getId();
Dominik Laskowski075d3172018-05-24 15:50:06 -0700477 LOG_ALWAYS_FATAL_IF(!displayId);
Dominik Laskowski34157762018-10-31 13:07:19 -0700478 RETURN_IF_NO_HWC_LAYER(*displayId);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700479 auto& hwcInfo = getBE().mHwcLayers[*displayId];
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700480
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700481 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800482 hwcInfo.forceClientComposition = false;
483
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700484 if (isSecure() && !display->isSecure()) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800485 hwcInfo.forceClientComposition = true;
486 }
487
David Sodman15094112018-10-11 09:39:37 -0700488 auto& hwcLayer = hwcInfo.layer;
489
Mathias Agopian13127d82013-03-05 17:47:11 -0800490 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700491 const State& s(getDrawingState());
David Revemanecf0fa52017-03-03 11:32:44 -0500492 auto blendMode = HWC2::BlendMode::None;
Robert Carr6452f122017-03-21 10:41:29 -0700493 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800494 blendMode =
495 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800496 }
David Sodman15094112018-10-11 09:39:37 -0700497 auto error = hwcLayer->setBlendMode(blendMode);
498 ALOGE_IF(error != HWC2::Error::None,
499 "[%s] Failed to set blend mode %s:"
500 " %s (%d)",
501 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
502 static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700503 getBE().compositionInfo.hwc.blendMode = blendMode;
Mathias Agopian13127d82013-03-05 17:47:11 -0800504
505 // apply the layer's transform, followed by the display's global transform
506 // here we're guaranteed that the layer's transform preserves rects
Marissa Wall61c58622018-07-18 10:12:20 -0700507 Region activeTransparentRegion(getActiveTransparentRegion(s));
Peiyong Linefefaac2018-08-17 12:27:51 -0700508 ui::Transform t = getTransform();
Marissa Wall61c58622018-07-18 10:12:20 -0700509 Rect activeCrop = getCrop(s);
510 if (!activeCrop.isEmpty()) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700511 activeCrop = t.transform(activeCrop);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700512 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000513 activeCrop.clear();
514 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700515 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800516 // This needs to be here as transform.transform(Rect) computes the
517 // transformed rect and then takes the bounding box of the result before
518 // returning. This means
519 // transform.inverse().transform(transform.transform(Rect)) != Rect
520 // in which case we need to make sure the final rect is clipped to the
521 // display bounds.
Marissa Wall61c58622018-07-18 10:12:20 -0700522 if (!activeCrop.intersect(Rect(getActiveWidth(s), getActiveHeight(s)), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000523 activeCrop.clear();
524 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700525 // mark regions outside the crop as transparent
Marissa Wall61c58622018-07-18 10:12:20 -0700526 activeTransparentRegion.orSelf(Rect(0, 0, getActiveWidth(s), activeCrop.top));
Marissa Wallf58c14b2018-07-24 10:50:43 -0700527 activeTransparentRegion.orSelf(
Marissa Wall61c58622018-07-18 10:12:20 -0700528 Rect(0, activeCrop.bottom, getActiveWidth(s), getActiveHeight(s)));
David Sodman41fdfc92017-11-06 16:09:56 -0800529 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
530 activeTransparentRegion.orSelf(
Marissa Wall61c58622018-07-18 10:12:20 -0700531 Rect(activeCrop.right, activeCrop.top, getActiveWidth(s), activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700532 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700533
Dan Stoza80d61162017-12-20 15:57:52 -0800534 // computeBounds returns a FloatRect to provide more accuracy during the
535 // transformation. We then round upon constructing 'frame'.
536 Rect frame{t.transform(computeBounds(activeTransparentRegion))};
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700537 if (!frame.intersect(display->getViewport(), &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000538 frame.clear();
539 }
Peiyong Linefefaac2018-08-17 12:27:51 -0700540 const ui::Transform& tr = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800541 Rect transformedFrame = tr.transform(frame);
David Sodman15094112018-10-11 09:39:37 -0700542 error = hwcLayer->setDisplayFrame(transformedFrame);
543 if (error != HWC2::Error::None) {
544 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
545 transformedFrame.left, transformedFrame.top, transformedFrame.right,
546 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
547 } else {
548 hwcInfo.displayFrame = transformedFrame;
549 }
David Sodmanba340492018-08-05 21:51:33 -0700550 getBE().compositionInfo.hwc.displayFrame = transformedFrame;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800551
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700552 FloatRect sourceCrop = computeCrop(display);
David Sodman15094112018-10-11 09:39:37 -0700553 error = hwcLayer->setSourceCrop(sourceCrop);
554 if (error != HWC2::Error::None) {
555 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
556 "%s (%d)",
557 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
558 to_string(error).c_str(), static_cast<int32_t>(error));
559 } else {
560 hwcInfo.sourceCrop = sourceCrop;
561 }
David Sodmanba340492018-08-05 21:51:33 -0700562 getBE().compositionInfo.hwc.sourceCrop = sourceCrop;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800563
chaviw13fdc492017-06-27 12:40:18 -0700564 float alpha = static_cast<float>(getAlpha());
David Sodman15094112018-10-11 09:39:37 -0700565 error = hwcLayer->setPlaneAlpha(alpha);
566 ALOGE_IF(error != HWC2::Error::None,
567 "[%s] Failed to set plane alpha %.3f: "
568 "%s (%d)",
569 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700570 getBE().compositionInfo.hwc.alpha = alpha;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800571
David Sodman15094112018-10-11 09:39:37 -0700572 error = hwcLayer->setZOrder(z);
573 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
574 to_string(error).c_str(), static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700575 getBE().compositionInfo.hwc.z = z;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500576
Albert Chaulk2a589632017-05-04 16:59:44 -0400577 int type = s.type;
578 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700579 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400580 if (parent.get()) {
581 auto& parentState = parent->getDrawingState();
rongliucfb187b2018-03-14 12:26:23 -0700582 if (parentState.type >= 0 || parentState.appId >= 0) {
583 type = parentState.type;
584 appId = parentState.appId;
585 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400586 }
587
David Sodman15094112018-10-11 09:39:37 -0700588 error = hwcLayer->setInfo(type, appId);
589 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
590 static_cast<int32_t>(error));
591
David Sodmanba340492018-08-05 21:51:33 -0700592 getBE().compositionInfo.hwc.type = type;
593 getBE().compositionInfo.hwc.appId = appId;
594
Mathias Agopian29a367b2011-07-12 14:51:45 -0700595 /*
596 * Transformations are applied in this order:
597 * 1) buffer orientation/flip/mirror
598 * 2) state transformation (window manager)
599 * 3) layer orientation (screen orientation)
600 * (NOTE: the matrices are multiplied in reverse order)
601 */
602
Peiyong Linefefaac2018-08-17 12:27:51 -0700603 const ui::Transform bufferOrientation(mCurrentTransform);
604 ui::Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700605
Robert Carrcae605c2017-03-29 12:10:31 -0700606 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700607 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700608 * the code below applies the primary display's inverse transform to the
609 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700610 */
David Sodman41fdfc92017-11-06 16:09:56 -0800611 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700612 // calculate the inverse transform
613 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800614 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700615 }
Robert Carrcae605c2017-03-29 12:10:31 -0700616
617 /*
618 * Here we cancel out the orientation component of the WM transform.
619 * The scaling and translate components are already included in our bounds
620 * computation so it's enough to just omit it in the composition.
621 * See comment in onDraw with ref to b/36727915 for why.
622 */
Peiyong Linefefaac2018-08-17 12:27:51 -0700623 transform = ui::Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700624 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700625
626 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800627 const uint32_t orientation = transform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -0700628 if (orientation & ui::Transform::ROT_INVALID) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800629 // we can only handle simple transformation
Lloyd Pique074e8122018-07-26 12:57:23 -0700630 hwcInfo.forceClientComposition = true;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700631 getBE().mHwcLayers[*displayId].compositionType = HWC2::Composition::Client;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800632 } else {
633 auto transform = static_cast<HWC2::Transform>(orientation);
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800634 hwcInfo.transform = transform;
David Sodman15094112018-10-11 09:39:37 -0700635 auto error = hwcLayer->setTransform(transform);
636 ALOGE_IF(error != HWC2::Error::None,
637 "[%s] Failed to set transform %s: "
638 "%s (%d)",
639 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
640 static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700641 getBE().compositionInfo.hwc.transform = transform;
David Sodman4b7c4bc2017-11-17 12:13:59 -0800642 }
643}
644
Dominik Laskowski075d3172018-05-24 15:50:06 -0700645void Layer::forceClientComposition(DisplayId displayId) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700646 RETURN_IF_NO_HWC_LAYER(displayId);
Dominik Laskowski7e045462018-05-30 13:02:02 -0700647 getBE().mHwcLayers[displayId].forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800648}
Dan Stozaee44edd2015-03-23 15:50:23 -0700649
Dominik Laskowski075d3172018-05-24 15:50:06 -0700650bool Layer::getForceClientComposition(DisplayId displayId) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700651 RETURN_IF_NO_HWC_LAYER(displayId, false);
Dominik Laskowski7e045462018-05-30 13:02:02 -0700652 return getBE().mHwcLayers[displayId].forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800653}
654
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700655void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700656 const auto displayId = display->getId();
Dominik Laskowski34157762018-10-31 13:07:19 -0700657 LOG_ALWAYS_FATAL_IF(!displayId);
658 if (!hasHwcLayer(*displayId) || getCompositionType(displayId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800659 return;
660 }
661
662 // This gives us only the "orientation" component of the transform
663 const State& s(getCurrentState());
664
665 // Apply the layer's transform, followed by the display's global transform
666 // Here we're guaranteed that the layer's transform preserves rects
Marissa Wall61c58622018-07-18 10:12:20 -0700667 Rect win(getActiveWidth(s), getActiveHeight(s));
668 Rect crop = getCrop(s);
669 if (!crop.isEmpty()) {
670 win.intersect(crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800671 }
672 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700673 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Robert Carr1f0a16a2016-10-24 16:27:39 -0700674 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700675 frame.intersect(display->getViewport(), &frame);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700676 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800677 auto position = displayTransform.transform(frame);
678
Dominik Laskowski7e045462018-05-30 13:02:02 -0700679 auto error =
Dominik Laskowski075d3172018-05-24 15:50:06 -0700680 getBE().mHwcLayers[*displayId].layer->setCursorPosition(position.left, position.top);
681
David Sodman41fdfc92017-11-06 16:09:56 -0800682 ALOGE_IF(error != HWC2::Error::None,
683 "[%s] Failed to set cursor position "
684 "to (%d, %d): %s (%d)",
685 mName.string(), position.left, position.top, to_string(error).c_str(),
686 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800687}
Riley Andrews03414a12014-07-01 14:22:59 -0700688
Mathias Agopian13127d82013-03-05 17:47:11 -0800689// ---------------------------------------------------------------------------
690// drawing...
691// ---------------------------------------------------------------------------
692
Marissa Wall61c58622018-07-18 10:12:20 -0700693void Layer::draw(const RenderArea& renderArea, const Region& clip) {
chaviwa76b2712017-09-20 12:02:26 -0700694 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800695}
696
Marissa Wall61c58622018-07-18 10:12:20 -0700697void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) {
chaviwa76b2712017-09-20 12:02:26 -0700698 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800699}
700
David Sodman41fdfc92017-11-06 16:09:56 -0800701void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
702 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800703 auto& engine(mFlinger->getRenderEngine());
David Sodman9eeae692017-11-02 10:53:32 -0700704 computeGeometry(renderArea, getBE().mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700705 engine.setupFillWithColor(red, green, blue, alpha);
David Sodman9eeae692017-11-02 10:53:32 -0700706 engine.drawMesh(getBE().mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800707}
708
David Sodmanc1498e62018-09-12 14:36:26 -0700709void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
710 clearWithOpenGL(renderArea, 0, 0, 0, 0);
711}
712
Dominik Laskowski075d3172018-05-24 15:50:06 -0700713void Layer::setCompositionType(DisplayId displayId, HWC2::Composition type, bool callIntoHwc) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700714 if (getBE().mHwcLayers.count(displayId) == 0) {
Chia-I Wu30505fb2018-03-26 16:20:31 -0700715 ALOGE("setCompositionType called without a valid HWC layer");
716 return;
David Sodman15094112018-10-11 09:39:37 -0700717 }
718 auto& hwcInfo = getBE().mHwcLayers[displayId];
719 auto& hwcLayer = hwcInfo.layer;
720 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", (hwcLayer)->getId(), to_string(type).c_str(),
721 static_cast<int>(callIntoHwc));
722 if (hwcInfo.compositionType != type) {
723 ALOGV(" actually setting");
724 hwcInfo.compositionType = type;
725 if (callIntoHwc) {
726 auto error = (hwcLayer)->setCompositionType(type);
727 ALOGE_IF(error != HWC2::Error::None,
728 "[%s] Failed to set "
729 "composition type %s: %s (%d)",
730 mName.string(), to_string(type).c_str(), to_string(error).c_str(),
731 static_cast<int32_t>(error));
Chia-I Wu30505fb2018-03-26 16:20:31 -0700732 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800733 }
734}
735
Dominik Laskowski075d3172018-05-24 15:50:06 -0700736HWC2::Composition Layer::getCompositionType(const std::optional<DisplayId>& displayId) const {
737 if (!displayId) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700738 // If we're querying the composition type for a display that does not
739 // have a HWC counterpart, then it will always be Client
740 return HWC2::Composition::Client;
741 }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700742 if (getBE().mHwcLayers.count(*displayId) == 0) {
743 ALOGE("getCompositionType called with an invalid HWC layer");
744 return HWC2::Composition::Invalid;
745 }
746 return getBE().mHwcLayers.at(*displayId).compositionType;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800747}
748
Dominik Laskowski075d3172018-05-24 15:50:06 -0700749void Layer::setClearClientTarget(DisplayId displayId, bool clear) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700750 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800751 ALOGE("setClearClientTarget called without a valid HWC layer");
752 return;
753 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700754 getBE().mHwcLayers[displayId].clearClientTarget = clear;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800755}
756
Dominik Laskowski075d3172018-05-24 15:50:06 -0700757bool Layer::getClearClientTarget(DisplayId displayId) const {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700758 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800759 ALOGE("getClearClientTarget called without a valid HWC layer");
760 return false;
761 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700762 return getBE().mHwcLayers.at(displayId).clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800763}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800764
Dan Stozacac35382016-01-27 12:21:06 -0800765bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
766 if (point->getFrameNumber() <= mCurrentFrameNumber) {
767 // Don't bother with a SyncPoint, since we've already latched the
768 // relevant frame
769 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700770 }
Robert Carr2e102c92018-10-23 12:11:15 -0700771 if (isRemovedFromCurrentState()) {
772 return false;
773 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700774
Dan Stozacac35382016-01-27 12:21:06 -0800775 Mutex::Autolock lock(mLocalSyncPointMutex);
776 mLocalSyncPoints.push_back(point);
777 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700778}
779
Mathias Agopian13127d82013-03-05 17:47:11 -0800780// ----------------------------------------------------------------------------
781// local state
782// ----------------------------------------------------------------------------
783
Peiyong Lin833074a2018-08-28 11:53:54 -0700784void Layer::computeGeometry(const RenderArea& renderArea,
785 renderengine::Mesh& mesh,
chaviwa76b2712017-09-20 12:02:26 -0700786 bool useIdentityTransform) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700787 const ui::Transform renderAreaTransform(renderArea.getTransform());
Dan Stoza80d61162017-12-20 15:57:52 -0800788 FloatRect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700789
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000790 vec2 lt = vec2(win.left, win.top);
791 vec2 lb = vec2(win.left, win.bottom);
792 vec2 rb = vec2(win.right, win.bottom);
793 vec2 rt = vec2(win.right, win.top);
794
Peiyong Linefefaac2018-08-17 12:27:51 -0700795 ui::Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000796 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700797 lt = layerTransform.transform(lt);
798 lb = layerTransform.transform(lb);
799 rb = layerTransform.transform(rb);
800 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000801 }
802
Peiyong Lin833074a2018-08-28 11:53:54 -0700803 renderengine::Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700804 position[0] = renderAreaTransform.transform(lt);
805 position[1] = renderAreaTransform.transform(lb);
806 position[2] = renderAreaTransform.transform(rb);
807 position[3] = renderAreaTransform.transform(rt);
Mathias Agopian13127d82013-03-05 17:47:11 -0800808}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800809
David Sodman41fdfc92017-11-06 16:09:56 -0800810bool Layer::isSecure() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000811 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700812 return (s.flags & layer_state_t::eLayerSecure);
813}
814
Mathias Agopian13127d82013-03-05 17:47:11 -0800815void Layer::setVisibleRegion(const Region& visibleRegion) {
816 // always called from main thread
817 this->visibleRegion = visibleRegion;
818}
819
820void Layer::setCoveredRegion(const Region& coveredRegion) {
821 // always called from main thread
822 this->coveredRegion = coveredRegion;
823}
824
David Sodman41fdfc92017-11-06 16:09:56 -0800825void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800826 // always called from main thread
827 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
828}
829
Robert Carre5f4f692018-01-12 13:12:28 -0800830void Layer::clearVisibilityRegions() {
831 visibleRegion.clear();
832 visibleNonTransparentRegion.clear();
833 coveredRegion.clear();
834}
835
Mathias Agopian13127d82013-03-05 17:47:11 -0800836// ----------------------------------------------------------------------------
837// transaction
838// ----------------------------------------------------------------------------
839
Dan Stoza7dde5992015-05-22 09:51:44 -0700840void Layer::pushPendingState() {
841 if (!mCurrentState.modified) {
842 return;
843 }
844
Dan Stoza7dde5992015-05-22 09:51:44 -0700845 // If this transaction is waiting on the receipt of a frame, generate a sync
846 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700847 // We don't allow installing sync points after we are removed from the current state
848 // as we won't be able to signal our end.
849 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700850 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800851 if (barrierLayer == nullptr) {
852 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700853 // If we can't promote the layer we are intended to wait on,
854 // then it is expired or otherwise invalid. Allow this transaction
855 // to be applied as per normal (no synchronization).
Marissa Wallf58c14b2018-07-24 10:50:43 -0700856 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800857 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700858 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy);
Robert Carr0d480722017-01-10 16:42:54 -0800859 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800860 mRemoteSyncPoints.push_back(std::move(syncPoint));
861 } else {
862 // We already missed the frame we're supposed to synchronize
863 // on, so go ahead and apply the state update
Marissa Wallf58c14b2018-07-24 10:50:43 -0700864 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800865 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700866 }
867
Dan Stoza7dde5992015-05-22 09:51:44 -0700868 // Wake us up to check if the frame has been received
869 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700870 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700871 }
872 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700873 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700874}
875
Pablo Ceballos05289c22016-04-14 15:49:55 -0700876void Layer::popPendingState(State* stateToCommit) {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700877 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700878
879 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700880 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700881}
882
Pablo Ceballos05289c22016-04-14 15:49:55 -0700883bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700884 bool stateUpdateAvailable = false;
885 while (!mPendingStates.empty()) {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700886 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700887 if (mRemoteSyncPoints.empty()) {
888 // If we don't have a sync point for this, apply it anyway. It
889 // will be visually wrong, but it should keep us from getting
890 // into too much trouble.
891 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700892 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700893 stateUpdateAvailable = true;
894 continue;
895 }
896
Marissa Wallf58c14b2018-07-24 10:50:43 -0700897 if (mRemoteSyncPoints.front()->getFrameNumber() !=
898 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800899 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800900
901 // Signal our end of the sync point and then dispose of it
902 mRemoteSyncPoints.front()->setTransactionApplied();
903 mRemoteSyncPoints.pop_front();
904 continue;
905 }
906
Dan Stoza7dde5992015-05-22 09:51:44 -0700907 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
908 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700909 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700910 stateUpdateAvailable = true;
911
912 // Signal our end of the sync point and then dispose of it
913 mRemoteSyncPoints.front()->setTransactionApplied();
914 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800915 } else {
916 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700917 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700918 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700919 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700920 stateUpdateAvailable = true;
921 }
922 }
923
924 // If we still have pending updates, wake SurfaceFlinger back up and point
925 // it at this layer so we can process them
926 if (!mPendingStates.empty()) {
927 setTransactionFlags(eTransactionNeeded);
928 mFlinger->setTransactionFlags(eTraversalNeeded);
929 }
930
931 mCurrentState.modified = false;
932 return stateUpdateAvailable;
933}
934
Marissa Wall61c58622018-07-18 10:12:20 -0700935uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000936 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800937
Marissa Wall61c58622018-07-18 10:12:20 -0700938 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
939 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700940
David Sodmaneb085e02017-10-05 18:49:04 -0700941 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700942 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000943 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800944 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
945 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
946 " requested={ wh={%4u,%4u} }}\n"
947 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
948 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700949 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700950 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
951 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
952 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
953 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
954 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700955 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
956 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
957 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800958 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700959
Robert Carre392b552017-09-19 12:16:05 -0700960 // Don't let Layer::doTransaction update the drawing state
961 // if we have a pending resize, unless we are in fixed-size mode.
962 // the drawing state will be updated only once we receive a buffer
963 // with the correct size.
964 //
965 // In particular, we want to make sure the clip (which is part
966 // of the geometry state) is latched together with the size but is
967 // latched immediately when no resizing is involved.
968 //
969 // If a sideband stream is attached, however, we want to skip this
970 // optimization so that transactions aren't missed when a buffer
971 // never arrives
972 //
973 // In the case that we don't have a buffer we ignore other factors
974 // and avoid entering the resizePending state. At a high level the
975 // resizePending state is to avoid applying the state of the new buffer
976 // to the old buffer. However in the state where we don't have an old buffer
977 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700978 const bool resizePending =
979 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
980 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
David Sodman0cc69182017-11-17 12:12:07 -0800981 (getBE().compositionInfo.mBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700982 if (!isFixedSize()) {
David Sodman0cc69182017-11-17 12:12:07 -0800983 if (resizePending && getBE().compositionInfo.hwc.sidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700984 flags |= eDontUpdateGeometryState;
985 }
986 }
987
Robert Carr7bf247e2017-05-18 14:02:49 -0700988 // Here we apply various requested geometry states, depending on our
989 // latching configuration. See Layer.h for a detailed discussion of
990 // how geometry latching is controlled.
991 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000992 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700993
994 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
995 // mode, which causes attributes which normally latch regardless of scaling mode,
996 // to be delayed. We copy the requested state to the active state making sure
997 // to respect these rules (again see Layer.h for a detailed discussion).
998 //
999 // There is an awkward asymmetry in the handling of the crop states in the position
1000 // states, as can be seen below. Largely this arises from position and transform
1001 // being stored in the same data structure while having different latching rules.
1002 // b/38182305
1003 //
Marissa Wall61c58622018-07-18 10:12:20 -07001004 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -07001005 // applyPendingStates in the presence of deferred transactions.
1006 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -07001007 float tx = stateToCommit->active_legacy.transform.tx();
1008 float ty = stateToCommit->active_legacy.transform.ty();
1009 stateToCommit->active_legacy = stateToCommit->requested_legacy;
1010 stateToCommit->active_legacy.transform.set(tx, ty);
1011 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001012 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001013 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -07001014 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001015 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001016 }
1017
Marissa Wall61c58622018-07-18 10:12:20 -07001018 return flags;
1019}
1020
1021uint32_t Layer::doTransaction(uint32_t flags) {
1022 ATRACE_CALL();
1023
chaviw5aedec92018-10-22 10:40:38 -07001024 if (mLayerDetached) {
1025 return 0;
1026 }
1027
Marissa Wall61c58622018-07-18 10:12:20 -07001028 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +00001029 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -07001030 if (!applyPendingStates(&c)) {
1031 return 0;
1032 }
1033
1034 flags = doTransactionResize(flags, &c);
1035
Alec Mourib416efd2018-09-06 21:01:59 +00001036 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -07001037
1038 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001039 // invalidate and recompute the visible regions if needed
1040 flags |= Layer::eVisibleRegion;
1041 }
1042
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001043 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001044 // invalidate and recompute the visible regions if needed
1045 flags |= eVisibleRegion;
1046 this->contentDirty = true;
1047
1048 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -07001049 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -07001050 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -08001051 }
1052
Dan Stozac8145172016-04-28 16:29:06 -07001053 // If the layer is hidden, signal and clear out all local sync points so
1054 // that transactions for layers depending on this layer's frames becoming
1055 // visible are not blocked
1056 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001057 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001058 }
1059
Mathias Agopian13127d82013-03-05 17:47:11 -08001060 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001061 commitTransaction(c);
Marissa Walle2ffb422018-10-12 11:33:52 -07001062 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -08001063 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001064}
1065
Pablo Ceballos05289c22016-04-14 15:49:55 -07001066void Layer::commitTransaction(const State& stateToCommit) {
1067 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001068}
1069
Mathias Agopian13127d82013-03-05 17:47:11 -08001070uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Piquef1c675b2018-09-12 20:45:39 -07001071 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -08001072}
1073
1074uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Piquef1c675b2018-09-12 20:45:39 -07001075 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -08001076}
1077
Robert Carr82364e32016-05-15 11:27:47 -07001078bool Layer::setPosition(float x, float y, bool immediate) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001079 if (mCurrentState.requested_legacy.transform.tx() == x &&
1080 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001081 return false;
1082 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001083
1084 // We update the requested and active position simultaneously because
1085 // we want to apply the position portion of the transform matrix immediately,
1086 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Marissa Wallf58c14b2018-07-24 10:50:43 -07001087 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001088 if (immediate && !mFreezeGeometryUpdates) {
1089 // Here we directly update the active state
1090 // unlike other setters, because we store it within
1091 // the transform, but use different latching rules.
1092 // b/38182305
Marissa Wallf58c14b2018-07-24 10:50:43 -07001093 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001094 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001095 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001096
Dan Stoza7dde5992015-05-22 09:51:44 -07001097 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001098 setTransactionFlags(eTransactionNeeded);
1099 return true;
1100}
Robert Carr82364e32016-05-15 11:27:47 -07001101
Robert Carr1f0a16a2016-10-24 16:27:39 -07001102bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1103 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1104 if (idx < 0) {
1105 return false;
1106 }
1107 if (childLayer->setLayer(z)) {
1108 mCurrentChildren.removeAt(idx);
1109 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001110 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001111 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001112 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001113}
1114
Robert Carr503c7042017-09-27 15:06:08 -07001115bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1116 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1117 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1118 if (idx < 0) {
1119 return false;
1120 }
1121 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1122 mCurrentChildren.removeAt(idx);
1123 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001124 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001125 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001126 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001127}
1128
Robert Carrae060832016-11-28 10:51:00 -08001129bool Layer::setLayer(int32_t z) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001130 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001131 mCurrentState.sequence++;
1132 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001133 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001134
1135 // Discard all relative layering.
1136 if (mCurrentState.zOrderRelativeOf != nullptr) {
1137 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1138 if (strongRelative != nullptr) {
1139 strongRelative->removeZOrderRelative(this);
1140 }
1141 mCurrentState.zOrderRelativeOf = nullptr;
1142 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001143 setTransactionFlags(eTransactionNeeded);
1144 return true;
1145}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001146
Robert Carrdb66e622017-04-10 16:55:57 -07001147void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1148 mCurrentState.zOrderRelatives.remove(relative);
1149 mCurrentState.sequence++;
1150 mCurrentState.modified = true;
1151 setTransactionFlags(eTransactionNeeded);
1152}
1153
1154void Layer::addZOrderRelative(const wp<Layer>& relative) {
1155 mCurrentState.zOrderRelatives.add(relative);
1156 mCurrentState.modified = true;
1157 mCurrentState.sequence++;
1158 setTransactionFlags(eTransactionNeeded);
1159}
1160
Robert Carr503d2bd2017-12-04 15:49:47 -08001161bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001162 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1163 if (handle == nullptr) {
1164 return false;
1165 }
1166 sp<Layer> relative = handle->owner.promote();
1167 if (relative == nullptr) {
1168 return false;
1169 }
1170
Robert Carr503d2bd2017-12-04 15:49:47 -08001171 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1172 mCurrentState.zOrderRelativeOf == relative) {
1173 return false;
1174 }
1175
Robert Carrdb66e622017-04-10 16:55:57 -07001176 mCurrentState.sequence++;
1177 mCurrentState.modified = true;
Robert Carr503d2bd2017-12-04 15:49:47 -08001178 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001179
chaviw9ab4bd12017-11-03 13:11:00 -07001180 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
1181 if (oldZOrderRelativeOf != nullptr) {
1182 oldZOrderRelativeOf->removeZOrderRelative(this);
1183 }
Robert Carrdb66e622017-04-10 16:55:57 -07001184 mCurrentState.zOrderRelativeOf = relative;
1185 relative->addZOrderRelative(this);
1186
1187 setTransactionFlags(eTransactionNeeded);
1188
1189 return true;
1190}
1191
Mathias Agopian13127d82013-03-05 17:47:11 -08001192bool Layer::setSize(uint32_t w, uint32_t h) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001193 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
1194 return false;
1195 mCurrentState.requested_legacy.w = w;
1196 mCurrentState.requested_legacy.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001197 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001198 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001199
1200 // record the new size, from this point on, when the client request
1201 // a buffer, it'll get the new size.
1202 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001203 return true;
1204}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001205bool Layer::setAlpha(float alpha) {
David Sodman41fdfc92017-11-06 16:09:56 -08001206 if (mCurrentState.color.a == alpha) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001207 mCurrentState.sequence++;
chaviw13fdc492017-06-27 12:40:18 -07001208 mCurrentState.color.a = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001209 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001210 setTransactionFlags(eTransactionNeeded);
1211 return true;
1212}
chaviw13fdc492017-06-27 12:40:18 -07001213
1214bool Layer::setColor(const half3& color) {
David Sodman41fdfc92017-11-06 16:09:56 -08001215 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g &&
1216 color.b == mCurrentState.color.b)
chaviw13fdc492017-06-27 12:40:18 -07001217 return false;
1218
1219 mCurrentState.sequence++;
1220 mCurrentState.color.r = color.r;
1221 mCurrentState.color.g = color.g;
1222 mCurrentState.color.b = color.b;
1223 mCurrentState.modified = true;
1224 setTransactionFlags(eTransactionNeeded);
1225 return true;
1226}
1227
Robert Carrd4ae7f32018-06-07 16:10:57 -07001228bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1229 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001230 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001231 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1232
1233 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1234 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1235 return false;
1236 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001237 mCurrentState.sequence++;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001238 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1239 matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001240 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001241 setTransactionFlags(eTransactionNeeded);
1242 return true;
1243}
Marissa Wall61c58622018-07-18 10:12:20 -07001244
Mathias Agopian13127d82013-03-05 17:47:11 -08001245bool Layer::setTransparentRegionHint(const Region& transparent) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001246 mCurrentState.requestedTransparentRegion_legacy = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001247 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001248 setTransactionFlags(eTransactionNeeded);
1249 return true;
1250}
1251bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1252 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
David Sodman41fdfc92017-11-06 16:09:56 -08001253 if (mCurrentState.flags == newFlags) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001254 mCurrentState.sequence++;
1255 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001256 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001257 setTransactionFlags(eTransactionNeeded);
1258 return true;
1259}
Robert Carr99e27f02016-06-16 15:18:02 -07001260
Marissa Wallf58c14b2018-07-24 10:50:43 -07001261bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
1262 if (mCurrentState.requestedCrop_legacy == crop) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001263 mCurrentState.sequence++;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001264 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001265 if (immediate && !mFreezeGeometryUpdates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001266 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001267 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001268 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1269
Dan Stoza7dde5992015-05-22 09:51:44 -07001270 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001271 setTransactionFlags(eTransactionNeeded);
1272 return true;
1273}
Robert Carr8d5227b2017-03-16 15:41:03 -07001274
Robert Carrc3574f72016-03-24 12:19:32 -07001275bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001276 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001277 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001278 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001279 return true;
1280}
1281
rongliucfb187b2018-03-14 12:26:23 -07001282void Layer::setInfo(int32_t type, int32_t appId) {
David Sodman41fdfc92017-11-06 16:09:56 -08001283 mCurrentState.appId = appId;
1284 mCurrentState.type = type;
1285 mCurrentState.modified = true;
1286 setTransactionFlags(eTransactionNeeded);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001287}
1288
Mathias Agopian13127d82013-03-05 17:47:11 -08001289bool Layer::setLayerStack(uint32_t layerStack) {
David Sodman41fdfc92017-11-06 16:09:56 -08001290 if (mCurrentState.layerStack == layerStack) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001291 mCurrentState.sequence++;
1292 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001293 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001294 setTransactionFlags(eTransactionNeeded);
1295 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001296}
1297
Robert Carr1f0a16a2016-10-24 16:27:39 -07001298uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001299 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001300 if (p == nullptr) {
1301 return getDrawingState().layerStack;
1302 }
1303 return p->getLayerStack();
1304}
1305
Marissa Wallf58c14b2018-07-24 10:50:43 -07001306void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
1307 mCurrentState.barrierLayer_legacy = barrierLayer;
1308 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001309 // We don't set eTransactionNeeded, because just receiving a deferral
1310 // request without any other state updates shouldn't actually induce a delay
1311 mCurrentState.modified = true;
1312 pushPendingState();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001313 mCurrentState.barrierLayer_legacy = nullptr;
1314 mCurrentState.frameNumber_legacy = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001315 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001316}
1317
Marissa Wallf58c14b2018-07-24 10:50:43 -07001318void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001319 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001320 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001321}
1322
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001323// ----------------------------------------------------------------------------
1324// pageflip handling...
1325// ----------------------------------------------------------------------------
1326
Robert Carr1f0a16a2016-10-24 16:27:39 -07001327bool Layer::isHiddenByPolicy() const {
Alec Mourib416efd2018-09-06 21:01:59 +00001328 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001329 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001330 if (parent != nullptr && parent->isHiddenByPolicy()) {
1331 return true;
1332 }
1333 return s.flags & layer_state_t::eLayerHidden;
1334}
1335
David Sodman41fdfc92017-11-06 16:09:56 -08001336uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001337 // TODO: should we do something special if mSecure is set?
1338 if (mProtectedByApp) {
1339 // need a hardware-protected path to external video sink
1340 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001341 }
Riley Andrews03414a12014-07-01 14:22:59 -07001342 if (mPotentialCursor) {
1343 usage |= GraphicBuffer::USAGE_CURSOR;
1344 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001345 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001346 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001347}
1348
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001349void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001350 uint32_t orientation = 0;
Vishnu Nairf2deb822018-11-12 17:53:48 -08001351 // Disable setting transform hint if the debug flag is set or if the
1352 // getTransformToDisplayInverse flag is set and the client wants to submit buffers
1353 // in one orientation.
1354 if (!mFlinger->mDebugDisableTransformHint && !getTransformToDisplayInverse()) {
Mathias Agopian84300952012-11-21 16:02:13 -08001355 // The transform hint is used to improve performance, but we can
1356 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001357 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001358 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001359 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001360 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001361 orientation = 0;
1362 }
1363 }
David Sodmaneb085e02017-10-05 18:49:04 -07001364 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001365}
1366
Mathias Agopian13127d82013-03-05 17:47:11 -08001367// ----------------------------------------------------------------------------
1368// debugging
1369// ----------------------------------------------------------------------------
1370
Marissa Wall61c58622018-07-18 10:12:20 -07001371// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001372LayerDebugInfo Layer::getLayerDebugInfo() const {
1373 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001374 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001375 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001376 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001377 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
1378 info.mType = String8(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001379 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001380 info.mVisibleRegion = visibleRegion;
1381 info.mSurfaceDamageRegion = surfaceDamageRegion;
1382 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001383 info.mX = ds.active_legacy.transform.tx();
1384 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001385 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001386 info.mWidth = ds.active_legacy.w;
1387 info.mHeight = ds.active_legacy.h;
1388 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001389 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001390 info.mFlags = ds.flags;
1391 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001392 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001393 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1394 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1395 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1396 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001397 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001398 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001399 if (buffer != 0) {
1400 info.mActiveBufferWidth = buffer->getWidth();
1401 info.mActiveBufferHeight = buffer->getHeight();
1402 info.mActiveBufferStride = buffer->getStride();
1403 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001404 } else {
1405 info.mActiveBufferWidth = 0;
1406 info.mActiveBufferHeight = 0;
1407 info.mActiveBufferStride = 0;
1408 info.mActiveBufferFormat = 0;
1409 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001410 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001411 info.mNumQueuedFrames = getQueuedFrameCount();
1412 info.mRefreshPending = isBufferLatched();
1413 info.mIsOpaque = isOpaque(ds);
1414 info.mContentDirty = contentDirty;
1415 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001416}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001417
Dan Stozae22aec72016-08-01 13:20:59 -07001418void Layer::miniDumpHeader(String8& result) {
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 result.append(" Layer name\n");
1423 result.append(" Z | ");
1424 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001425 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001426 result.append(" Disp Frame (LTRB) | ");
1427 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001428 result.append("-------------------------------");
1429 result.append("-------------------------------");
1430 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001431}
1432
Dominik Laskowski075d3172018-05-24 15:50:06 -07001433void Layer::miniDump(String8& result, DisplayId displayId) const {
Dominik Laskowski34157762018-10-31 13:07:19 -07001434 if (!hasHwcLayer(displayId)) {
Dan Stozae22aec72016-08-01 13:20:59 -07001435 return;
1436 }
1437
1438 String8 name;
1439 if (mName.length() > 77) {
1440 std::string shortened;
1441 shortened.append(mName.string(), 36);
1442 shortened.append("[...]");
1443 shortened.append(mName.string() + (mName.length() - 36), 36);
1444 name = shortened.c_str();
1445 } else {
1446 name = mName;
1447 }
1448
1449 result.appendFormat(" %s\n", name.string());
1450
Alec Mourib416efd2018-09-06 21:01:59 +00001451 const State& layerState(getDrawingState());
Lloyd Pique074e8122018-07-26 12:57:23 -07001452 const LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers.at(displayId);
Chia-I Wu1e043612018-03-01 09:45:09 -08001453 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
1454 result.appendFormat(" rel %6d | ", layerState.z);
1455 } else {
1456 result.appendFormat(" %10d | ", layerState.z);
1457 }
Dominik Laskowski7e045462018-05-30 13:02:02 -07001458 result.appendFormat("%10s | ", to_string(getCompositionType(displayId)).c_str());
Lloyd Pique074e8122018-07-26 12:57:23 -07001459 result.appendFormat("%10s | ", to_string(hwcInfo.transform).c_str());
1460 const Rect& frame = hwcInfo.displayFrame;
David Sodman41fdfc92017-11-06 16:09:56 -08001461 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique074e8122018-07-26 12:57:23 -07001462 const FloatRect& crop = hwcInfo.sourceCrop;
David Sodman41fdfc92017-11-06 16:09:56 -08001463 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 -07001464
David Sodman7e4ae112018-02-09 15:02:28 -08001465 result.append("- - - - - - - - - - - - - - - -\n");
1466
1467 std::string compositionInfoStr;
1468 getBE().compositionInfo.dump(compositionInfoStr, "compositionInfo");
1469 result.append(compositionInfoStr.c_str());
1470
Yichi Chen6ca35192018-05-29 12:20:43 +08001471 result.append("- - - - - - - - - - - - - - - -");
1472 result.append("- - - - - - - - - - - - - - - -");
1473 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001474}
Dan Stozae22aec72016-08-01 13:20:59 -07001475
Svetoslavd85084b2014-03-20 10:28:31 -07001476void Layer::dumpFrameStats(String8& result) const {
1477 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001478}
1479
Svetoslavd85084b2014-03-20 10:28:31 -07001480void Layer::clearFrameStats() {
1481 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001482}
1483
Jamie Gennis6547ff42013-07-16 20:12:42 -07001484void Layer::logFrameStats() {
1485 mFrameTracker.logAndResetStats(mName);
1486}
1487
Svetoslavd85084b2014-03-20 10:28:31 -07001488void Layer::getFrameStats(FrameStats* outStats) const {
1489 mFrameTracker.getStats(outStats);
1490}
1491
Brian Andersond6927fb2016-07-23 23:37:30 -07001492void Layer::dumpFrameEvents(String8& result) {
David Sodman41fdfc92017-11-06 16:09:56 -08001493 result.appendFormat("- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001494 Mutex::Autolock lock(mFrameEventHistoryMutex);
1495 mFrameEventHistory.checkFencesForCompletion();
1496 mFrameEventHistory.dump(result);
1497}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001498
Brian Anderson5ea5e592016-12-01 16:54:33 -08001499void Layer::onDisconnect() {
1500 Mutex::Autolock lock(mFrameEventHistoryMutex);
1501 mFrameEventHistory.onDisconnect();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -07001502 mTimeStats.onDisconnect(getSequence());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001503}
1504
Brian Anderson3890c392016-07-25 12:48:08 -07001505void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001506 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001507 if (newTimestamps) {
Yiwei Zhang8e8fe522018-11-02 18:34:07 -07001508 mTimeStats.setPostTime(getSequence(), newTimestamps->frameNumber, getName().c_str(),
1509 newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001510 }
1511
Brian Andersond6927fb2016-07-23 23:37:30 -07001512 Mutex::Autolock lock(mFrameEventHistoryMutex);
1513 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001514 // If there are any unsignaled fences in the aquire timeline at this
1515 // point, the previously queued frame hasn't been latched yet. Go ahead
1516 // and try to get the signal time here so the syscall is taken out of
1517 // the main thread's critical path.
1518 mAcquireTimeline.updateSignalTimes();
1519 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001520 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001521 mFrameEventHistory.addQueue(*newTimestamps);
1522 }
1523
Brian Anderson3890c392016-07-25 12:48:08 -07001524 if (outDelta) {
1525 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001526 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001527}
Dan Stozae77c7662016-05-13 11:37:28 -07001528
Chia-I Wu98f1c102017-05-30 14:54:08 -07001529size_t Layer::getChildrenCount() const {
1530 size_t count = 0;
1531 for (const sp<Layer>& child : mCurrentChildren) {
1532 count += 1 + child->getChildrenCount();
1533 }
1534 return count;
1535}
1536
Robert Carr1f0a16a2016-10-24 16:27:39 -07001537void Layer::addChild(const sp<Layer>& layer) {
1538 mCurrentChildren.add(layer);
1539 layer->setParent(this);
1540}
1541
1542ssize_t Layer::removeChild(const sp<Layer>& layer) {
1543 layer->setParent(nullptr);
1544 return mCurrentChildren.remove(layer);
1545}
1546
Robert Carr1db73f62016-12-21 12:58:51 -08001547bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1548 sp<Handle> handle = nullptr;
1549 sp<Layer> newParent = nullptr;
1550 if (newParentHandle == nullptr) {
1551 return false;
1552 }
1553 handle = static_cast<Handle*>(newParentHandle.get());
1554 newParent = handle->owner.promote();
1555 if (newParent == nullptr) {
1556 ALOGE("Unable to promote Layer handle");
1557 return false;
1558 }
1559
chaviw5aedec92018-10-22 10:40:38 -07001560 if (attachChildren()) {
1561 setTransactionFlags(eTransactionNeeded);
1562 }
Robert Carr1db73f62016-12-21 12:58:51 -08001563 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001564 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001565
1566 sp<Client> client(child->mClientRef.promote());
1567 if (client != nullptr) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001568 client->updateParent(newParent);
Robert Carr1db73f62016-12-21 12:58:51 -08001569 }
1570 }
1571 mCurrentChildren.clear();
1572
1573 return true;
1574}
1575
Robert Carr15eae092018-03-23 13:43:53 -07001576void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001577 for (const sp<Layer>& child : mDrawingChildren) {
1578 child->mDrawingParent = newParent;
1579 }
1580}
1581
chaviwf1961f72017-09-18 16:41:07 -07001582bool Layer::reparent(const sp<IBinder>& newParentHandle) {
1583 if (newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001584 return false;
1585 }
1586
1587 auto handle = static_cast<Handle*>(newParentHandle.get());
1588 sp<Layer> newParent = handle->owner.promote();
1589 if (newParent == nullptr) {
1590 ALOGE("Unable to promote Layer handle");
1591 return false;
1592 }
1593
chaviwf1961f72017-09-18 16:41:07 -07001594 sp<Layer> parent = getParent();
1595 if (parent != nullptr) {
1596 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001597 }
chaviwf1961f72017-09-18 16:41:07 -07001598 newParent->addChild(this);
chaviw06178942017-07-27 10:25:59 -07001599
chaviwf1961f72017-09-18 16:41:07 -07001600 sp<Client> client(mClientRef.promote());
chaviw06178942017-07-27 10:25:59 -07001601 sp<Client> newParentClient(newParent->mClientRef.promote());
1602
chaviwf1961f72017-09-18 16:41:07 -07001603 if (client != newParentClient) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001604 client->updateParent(newParent);
chaviw06178942017-07-27 10:25:59 -07001605 }
1606
chaviw5aedec92018-10-22 10:40:38 -07001607 if (mLayerDetached) {
1608 mLayerDetached = false;
1609 setTransactionFlags(eTransactionNeeded);
1610 }
1611 if (attachChildren()) {
1612 setTransactionFlags(eTransactionNeeded);
1613 }
chaviw06178942017-07-27 10:25:59 -07001614 return true;
1615}
1616
Robert Carr9524cb32017-02-13 11:32:32 -08001617bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001618 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001619 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001620 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001621 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001622 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001623 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001624 }
Robert Carr7f619b22017-11-06 12:56:35 -08001625 }
Robert Carr9524cb32017-02-13 11:32:32 -08001626
1627 return true;
1628}
1629
chaviw5aedec92018-10-22 10:40:38 -07001630bool Layer::attachChildren() {
1631 bool changed = false;
1632 for (const sp<Layer>& child : mCurrentChildren) {
1633 sp<Client> parentClient = mClientRef.promote();
1634 sp<Client> client(child->mClientRef.promote());
1635 if (client != nullptr && parentClient != client) {
1636 if (child->mLayerDetached) {
1637 child->mLayerDetached = false;
1638 changed = true;
1639 }
1640 changed |= child->attachChildren();
1641 }
1642 }
1643
1644 return changed;
1645}
1646
Peiyong Lind3788632018-09-18 16:01:31 -07001647bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001648 static const mat4 identityMatrix = mat4();
1649
Peiyong Lind3788632018-09-18 16:01:31 -07001650 if (mCurrentState.colorTransform == matrix) {
1651 return false;
1652 }
1653 ++mCurrentState.sequence;
1654 mCurrentState.colorTransform = matrix;
Peiyong Lin747321c2018-10-01 10:03:11 -07001655 mCurrentState.hasColorTransform = matrix != identityMatrix;
1656 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001657 setTransactionFlags(eTransactionNeeded);
1658 return true;
1659}
1660
1661const mat4& Layer::getColorTransform() const {
1662 return getDrawingState().colorTransform;
1663}
1664
1665bool Layer::hasColorTransform() const {
Peiyong Lin747321c2018-10-01 10:03:11 -07001666 return getDrawingState().hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001667}
1668
Chia-I Wu11481472018-05-04 10:43:19 -07001669bool Layer::isLegacyDataSpace() const {
1670 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001671 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001672 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001673}
1674
Robert Carr1f0a16a2016-10-24 16:27:39 -07001675void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001676 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001677}
1678
1679void Layer::clearSyncPoints() {
1680 for (const auto& child : mCurrentChildren) {
1681 child->clearSyncPoints();
1682 }
1683
1684 Mutex::Autolock lock(mLocalSyncPointMutex);
1685 for (auto& point : mLocalSyncPoints) {
1686 point->setFrameAvailable();
1687 }
1688 mLocalSyncPoints.clear();
1689}
1690
1691int32_t Layer::getZ() const {
1692 return mDrawingState.z;
1693}
1694
Robert Carr29abff82017-12-04 13:51:20 -08001695bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1696 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1697 const State& state = useDrawing ? mDrawingState : mCurrentState;
1698 return state.zOrderRelativeOf != nullptr;
1699}
1700
David Sodman41fdfc92017-11-06 16:09:56 -08001701__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001702 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001703 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1704 "makeTraversalList received invalid stateSet");
1705 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1706 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1707 const State& state = useDrawing ? mDrawingState : mCurrentState;
1708
Robert Carr29abff82017-12-04 13:51:20 -08001709 if (state.zOrderRelatives.size() == 0) {
1710 *outSkipRelativeZUsers = true;
1711 return children;
1712 }
1713
chaviwfd462612018-05-31 16:11:27 -07001714 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001715 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001716 sp<Layer> strongRelative = weakRelative.promote();
1717 if (strongRelative != nullptr) {
1718 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001719 }
1720 }
1721
Dan Stoza412903f2017-04-27 13:42:17 -07001722 for (const sp<Layer>& child : children) {
Robert Carr503c7042017-09-27 15:06:08 -07001723 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1724 if (childState.zOrderRelativeOf != nullptr) {
1725 continue;
1726 }
Robert Carrdb66e622017-04-10 16:55:57 -07001727 traverse.add(child);
1728 }
1729
1730 return traverse;
1731}
1732
Robert Carr1f0a16a2016-10-24 16:27:39 -07001733/**
Robert Carrdb66e622017-04-10 16:55:57 -07001734 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001735 */
Dan Stoza412903f2017-04-27 13:42:17 -07001736void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001737 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1738 // produce a new list for traversing, including our relatives, and not including our children
1739 // who are relatives of another surface. In the case that there are no relative Z,
1740 // makeTraversalList returns our children directly to avoid significant overhead.
1741 // However in this case we need to take the responsibility for filtering children which
1742 // are relatives of another surface here.
1743 bool skipRelativeZUsers = false;
1744 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001745
Robert Carr1f0a16a2016-10-24 16:27:39 -07001746 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001747 for (; i < list.size(); i++) {
1748 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001749 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1750 continue;
1751 }
1752
Robert Carrdb66e622017-04-10 16:55:57 -07001753 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001754 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001755 }
Dan Stoza412903f2017-04-27 13:42:17 -07001756 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001757 }
Robert Carr29abff82017-12-04 13:51:20 -08001758
Dan Stoza412903f2017-04-27 13:42:17 -07001759 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001760 for (; i < list.size(); i++) {
1761 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001762
1763 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1764 continue;
1765 }
Dan Stoza412903f2017-04-27 13:42:17 -07001766 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001767 }
1768}
1769
1770/**
Robert Carrdb66e622017-04-10 16:55:57 -07001771 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001772 */
Dan Stoza412903f2017-04-27 13:42:17 -07001773void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1774 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001775 // See traverseInZOrder for documentation.
1776 bool skipRelativeZUsers = false;
1777 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001778
Robert Carr1f0a16a2016-10-24 16:27:39 -07001779 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001780 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001781 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001782
1783 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1784 continue;
1785 }
1786
Robert Carrdb66e622017-04-10 16:55:57 -07001787 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001788 break;
1789 }
Dan Stoza412903f2017-04-27 13:42:17 -07001790 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001791 }
Dan Stoza412903f2017-04-27 13:42:17 -07001792 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001793 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001794 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001795
1796 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1797 continue;
1798 }
1799
Dan Stoza412903f2017-04-27 13:42:17 -07001800 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001801 }
1802}
1803
chaviw4b129c22018-04-09 16:19:43 -07001804LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1805 const std::vector<Layer*>& layersInTree) {
1806 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1807 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001808 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1809 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
chaviw4b129c22018-04-09 16:19:43 -07001810 const State& state = useDrawing ? mDrawingState : mCurrentState;
1811
chaviwfd462612018-05-31 16:11:27 -07001812 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001813 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1814 sp<Layer> strongRelative = weakRelative.promote();
1815 // Only add relative layers that are also descendents of the top most parent of the tree.
1816 // If a relative layer is not a descendent, then it should be ignored.
1817 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1818 traverse.add(strongRelative);
1819 }
1820 }
1821
1822 for (const sp<Layer>& child : children) {
1823 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1824 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1825 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1826 // the child here since it won't be added later as a relative.
1827 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1828 childState.zOrderRelativeOf.promote().get())) {
1829 continue;
1830 }
1831 traverse.add(child);
1832 }
1833
1834 return traverse;
1835}
1836
1837void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1838 LayerVector::StateSet stateSet,
1839 const LayerVector::Visitor& visitor) {
1840 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001841
1842 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001843 for (; i < list.size(); i++) {
1844 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001845 if (relative->getZ() >= 0) {
1846 break;
1847 }
chaviw4b129c22018-04-09 16:19:43 -07001848 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001849 }
chaviw4b129c22018-04-09 16:19:43 -07001850
chaviwa76b2712017-09-20 12:02:26 -07001851 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001852 for (; i < list.size(); i++) {
1853 const auto& relative = list[i];
1854 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001855 }
1856}
1857
chaviw4b129c22018-04-09 16:19:43 -07001858std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1859 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1860 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1861
1862 std::vector<Layer*> layersInTree = {this};
1863 for (size_t i = 0; i < children.size(); i++) {
1864 const auto& child = children[i];
1865 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1866 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1867 }
1868
1869 return layersInTree;
1870}
1871
1872void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1873 const LayerVector::Visitor& visitor) {
1874 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1875 std::sort(layersInTree.begin(), layersInTree.end());
1876 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1877}
1878
Peiyong Linefefaac2018-08-17 12:27:51 -07001879ui::Transform Layer::getTransform() const {
1880 ui::Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001881 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001882 if (p != nullptr) {
1883 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07001884
1885 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
1886 // it isFixedSize) then there may be additional scaling not accounted
1887 // for in the transform. We need to mirror this scaling in child surfaces
1888 // or we will break the contract where WM can treat child surfaces as
1889 // pixels in the parent surface.
David Sodman0cc69182017-11-17 12:12:07 -08001890 if (p->isFixedSize() && p->getBE().compositionInfo.mBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07001891 int bufferWidth;
1892 int bufferHeight;
1893 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
David Sodman0cc69182017-11-17 12:12:07 -08001894 bufferWidth = p->getBE().compositionInfo.mBuffer->getWidth();
1895 bufferHeight = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001896 } else {
David Sodman0cc69182017-11-17 12:12:07 -08001897 bufferHeight = p->getBE().compositionInfo.mBuffer->getWidth();
1898 bufferWidth = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001899 }
Marissa Wall61c58622018-07-18 10:12:20 -07001900 float sx = p->getActiveWidth(p->getDrawingState()) / static_cast<float>(bufferWidth);
1901 float sy = p->getActiveHeight(p->getDrawingState()) / static_cast<float>(bufferHeight);
Peiyong Linefefaac2018-08-17 12:27:51 -07001902 ui::Transform extraParentScaling;
Robert Carr9b429f42017-04-17 14:56:57 -07001903 extraParentScaling.set(sx, 0, 0, sy);
1904 t = t * extraParentScaling;
1905 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001906 }
Marissa Wall61c58622018-07-18 10:12:20 -07001907 return t * getActiveTransform(getDrawingState());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001908}
1909
chaviw13fdc492017-06-27 12:40:18 -07001910half Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001911 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07001912
chaviw13fdc492017-06-27 12:40:18 -07001913 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1914 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001915}
Robert Carr6452f122017-03-21 10:41:29 -07001916
chaviw13fdc492017-06-27 12:40:18 -07001917half4 Layer::getColor() const {
1918 const half4 color(getDrawingState().color);
1919 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001920}
Robert Carr6452f122017-03-21 10:41:29 -07001921
Robert Carr1f0a16a2016-10-24 16:27:39 -07001922void Layer::commitChildList() {
1923 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1924 const auto& child = mCurrentChildren[i];
1925 child->commitChildList();
1926 }
1927 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001928 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001929}
1930
chaviw1d044282017-09-27 12:19:28 -07001931void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
1932 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1933 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1934 const State& state = useDrawing ? mDrawingState : mCurrentState;
1935
Peiyong Linefefaac2018-08-17 12:27:51 -07001936 ui::Transform requestedTransform = state.active_legacy.transform;
1937 ui::Transform transform = getTransform();
chaviw1d044282017-09-27 12:19:28 -07001938
1939 layerInfo->set_id(sequence);
1940 layerInfo->set_name(getName().c_str());
1941 layerInfo->set_type(String8(getTypeId()));
1942
1943 for (const auto& child : children) {
1944 layerInfo->add_children(child->sequence);
1945 }
1946
1947 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1948 sp<Layer> strongRelative = weakRelative.promote();
1949 if (strongRelative != nullptr) {
1950 layerInfo->add_relatives(strongRelative->sequence);
1951 }
1952 }
1953
Marissa Wallf58c14b2018-07-24 10:50:43 -07001954 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
chaviw1d044282017-09-27 12:19:28 -07001955 layerInfo->mutable_transparent_region());
1956 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
1957 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
1958
1959 layerInfo->set_layer_stack(getLayerStack());
1960 layerInfo->set_z(state.z);
1961
1962 PositionProto* position = layerInfo->mutable_position();
1963 position->set_x(transform.tx());
1964 position->set_y(transform.ty());
1965
1966 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
1967 requestedPosition->set_x(requestedTransform.tx());
1968 requestedPosition->set_y(requestedTransform.ty());
1969
1970 SizeProto* size = layerInfo->mutable_size();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001971 size->set_w(state.active_legacy.w);
1972 size->set_h(state.active_legacy.h);
chaviw1d044282017-09-27 12:19:28 -07001973
Marissa Wallf58c14b2018-07-24 10:50:43 -07001974 LayerProtoHelper::writeToProto(state.crop_legacy, layerInfo->mutable_crop());
chaviw1d044282017-09-27 12:19:28 -07001975
1976 layerInfo->set_is_opaque(isOpaque(state));
1977 layerInfo->set_invalidate(contentDirty);
Chia-I Wu01591c92018-05-22 12:03:00 -07001978
1979 // XXX (b/79210409) mCurrentDataSpace is not protected
1980 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
1981
chaviw1d044282017-09-27 12:19:28 -07001982 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1983 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
1984 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
1985 layerInfo->set_flags(state.flags);
1986
1987 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1988 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
1989
Jorim Jaggi8e0af362017-11-14 16:28:28 +01001990 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07001991 if (parent != nullptr) {
1992 layerInfo->set_parent(parent->sequence);
1993 }
1994
1995 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1996 if (zOrderRelativeOf != nullptr) {
1997 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1998 }
1999
Chia-I Wu01591c92018-05-22 12:03:00 -07002000 // XXX getBE().compositionInfo.mBuffer is not protected
David Sodman0cc69182017-11-17 12:12:07 -08002001 auto buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08002002 if (buffer != nullptr) {
2003 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
Peiyong Linefefaac2018-08-17 12:27:51 -07002004 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
Yichi Chen6ca35192018-05-29 12:20:43 +08002005 layerInfo->mutable_buffer_transform());
chaviw1d044282017-09-27 12:19:28 -07002006 }
2007
2008 layerInfo->set_queued_frames(getQueuedFrameCount());
2009 layerInfo->set_refresh_pending(isBufferLatched());
rongliucfb187b2018-03-14 12:26:23 -07002010 layerInfo->set_window_type(state.type);
2011 layerInfo->set_app_id(state.appId);
chaviwadc40c22018-07-10 16:57:27 -07002012 layerInfo->set_curr_frame(mCurrentFrameNumber);
Vishnu Nairf2deb822018-11-12 17:53:48 -08002013 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
chaviwadc40c22018-07-10 16:57:27 -07002014
2015 for (const auto& pendingState : mPendingStates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07002016 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
chaviwadc40c22018-07-10 16:57:27 -07002017 if (barrierLayer != nullptr) {
2018 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
2019 barrierLayerProto->set_id(barrierLayer->sequence);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002020 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
chaviwadc40c22018-07-10 16:57:27 -07002021 }
2022 }
chaviw1d044282017-09-27 12:19:28 -07002023}
2024
Dominik Laskowski075d3172018-05-24 15:50:06 -07002025void Layer::writeToProto(LayerProto* layerInfo, DisplayId displayId) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07002026 if (!hasHwcLayer(displayId)) {
2027 return;
2028 }
2029
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002030 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
2031
Dominik Laskowski7e045462018-05-30 13:02:02 -07002032 const auto& hwcInfo = getBE().mHwcLayers.at(displayId);
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002033
2034 const Rect& frame = hwcInfo.displayFrame;
2035 LayerProtoHelper::writeToProto(frame, layerInfo->mutable_hwc_frame());
2036
2037 const FloatRect& crop = hwcInfo.sourceCrop;
2038 LayerProtoHelper::writeToProto(crop, layerInfo->mutable_hwc_crop());
2039
2040 const int32_t transform = static_cast<int32_t>(hwcInfo.transform);
2041 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002042
2043 const int32_t compositionType = static_cast<int32_t>(hwcInfo.compositionType);
2044 layerInfo->set_hwc_composition_type(compositionType);
2045
2046 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
2047 static_cast<BufferLayer*>(this)->isProtected()) {
2048 layerInfo->set_is_protected(true);
2049 } else {
2050 layerInfo->set_is_protected(false);
2051 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002052}
2053
Robert Carr2e102c92018-10-23 12:11:15 -07002054bool Layer::isRemovedFromCurrentState() const {
2055 return mRemovedFromCurrentState;
2056}
2057
Mathias Agopian13127d82013-03-05 17:47:11 -08002058// ---------------------------------------------------------------------------
2059
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002060}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002061
2062#if defined(__gl_h_)
2063#error "don't include gl/gl.h in this file"
2064#endif
2065
2066#if defined(__gl2_h_)
2067#error "don't include gl2/gl2.h in this file"
2068#endif