blob: dba5f8f081e915526dc34fb78dbc68df75469813 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dan Stoza9e56aa02015-11-02 13:00:03 -080017//#define LOG_NDEBUG 0
18#undef LOG_TAG
19#define LOG_TAG "Layer"
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080020#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21
Mathias Agopian13127d82013-03-05 17:47:11 -080022#include <math.h>
David Sodman41fdfc92017-11-06 16:09:56 -080023#include <stdint.h>
24#include <stdlib.h>
25#include <sys/types.h>
chaviw4b129c22018-04-09 16:19:43 -070026#include <algorithm>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027
Mathias Agopiana67932f2011-04-20 14:20:59 -070028#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070029#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070030#include <cutils/properties.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080031
32#include <utils/Errors.h>
33#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080034#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080036#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037
Courtney Goeltzenleuchter36c44dc2017-04-14 09:33:16 -060038#include <ui/DebugUtils.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070039#include <ui/GraphicBuffer.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080040#include <ui/PixelFormat.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080041
Dan Stoza6b9454d2014-11-07 16:00:59 -080042#include <gui/BufferItem.h>
Kalle Raitaa099a242017-01-11 11:17:29 -080043#include <gui/LayerDebugInfo.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080044#include <gui/Surface.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045
Yiwei Zhang60d1a192018-03-07 14:52:28 -080046#include "BufferLayer.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020047#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070048#include "DisplayDevice.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include "Layer.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070050#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070051#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053
Mathias Agopian1b031492012-06-20 17:51:20 -070054#include "DisplayHardware/HWComposer.h"
55
Peiyong Lincbc184f2018-08-22 13:24:10 -070056#include <renderengine/RenderEngine.h>
Mathias Agopian875d8e12013-06-07 15:35:48 -070057
Dan Stozac5da2712016-07-20 15:38:12 -070058#include <mutex>
chaviw1d044282017-09-27 12:19:28 -070059#include "LayerProtoHelper.h"
Dan Stozac5da2712016-07-20 15:38:12 -070060
David Sodman41fdfc92017-11-06 16:09:56 -080061#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063namespace android {
64
Lloyd Piquef1c675b2018-09-12 20:45:39 -070065std::atomic<int32_t> Layer::sSequence{1};
Mathias Agopian13127d82013-03-05 17:47:11 -080066
Lloyd Pique42ab75e2018-09-12 20:46:03 -070067Layer::Layer(const LayerCreationArgs& args)
68 : mFlinger(args.flinger),
69 mName(args.name),
70 mClientRef(args.client),
71 mBE{this, args.name.string()} {
Mathias Agopiana67932f2011-04-20 14:20:59 -070072 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070073
74 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -070075 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
76 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
77 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070078
Dan Stozaf7ba41a2017-05-10 15:11:11 -070079 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070080
Lloyd Pique42ab75e2018-09-12 20:46:03 -070081 mCurrentState.active_legacy.w = args.w;
82 mCurrentState.active_legacy.h = args.h;
David Sodman0c69cad2017-08-21 12:12:51 -070083 mCurrentState.flags = layerFlags;
Marissa Wallf58c14b2018-07-24 10:50:43 -070084 mCurrentState.active_legacy.transform.set(0, 0);
85 mCurrentState.crop_legacy.makeInvalid();
Marissa Wallf58c14b2018-07-24 10:50:43 -070086 mCurrentState.requestedCrop_legacy = mCurrentState.crop_legacy;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070087 mCurrentState.z = 0;
chaviw13fdc492017-06-27 12:40:18 -070088 mCurrentState.color.a = 1.0f;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070089 mCurrentState.layerStack = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070090 mCurrentState.sequence = 0;
Marissa Wallf58c14b2018-07-24 10:50:43 -070091 mCurrentState.requested_legacy = mCurrentState.active_legacy;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -050092 mCurrentState.appId = 0;
93 mCurrentState.type = 0;
Marissa Wall61c58622018-07-18 10:12:20 -070094 mCurrentState.active.w = 0;
95 mCurrentState.active.h = 0;
96 mCurrentState.active.transform.set(0, 0);
97 mCurrentState.transform = 0;
98 mCurrentState.transformToDisplayInverse = false;
99 mCurrentState.crop.makeInvalid();
100 mCurrentState.acquireFence = new Fence(-1);
101 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
102 mCurrentState.hdrMetadata.validTypes = 0;
103 mCurrentState.surfaceDamageRegion.clear();
104 mCurrentState.api = -1;
Peiyong Lin747321c2018-10-01 10:03:11 -0700105 mCurrentState.hasColorTransform = false;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700106
107 // drawing state & current state are identical
108 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700109
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800110 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700111 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800112 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200113 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Dan Stoza436ccf32018-06-21 12:10:12 -0700114}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700115
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700116Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800117 sp<Client> c(mClientRef.promote());
118 if (c != 0) {
119 c->detachLayer(this);
120 }
121
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000122 for (auto& point : mRemoteSyncPoints) {
123 point->setTransactionApplied();
124 }
Rob Carr4bba3702018-10-08 21:53:30 +0000125 for (auto& point : mLocalSyncPoints) {
126 point->setFrameAvailable();
127 }
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000128 mFrameTracker.logAndResetStats(mName);
Mathias Agopian96f08192010-06-02 23:28:45 -0700129}
130
Mathias Agopian13127d82013-03-05 17:47:11 -0800131// ---------------------------------------------------------------------------
132// callbacks
133// ---------------------------------------------------------------------------
134
David Sodmaneb085e02017-10-05 18:49:04 -0700135/*
136 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
137 * Layer. So, the implementation is done in BufferLayer. When called on a
138 * ColorLayer object, it's essentially a NOP.
139 */
David Sodmaneb085e02017-10-05 18:49:04 -0700140void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800141
Chia-I Wuc6657022017-08-15 11:18:17 -0700142void Layer::onRemovedFromCurrentState() {
143 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000144
145 mPendingRemoval = true;
146
Robert Carr5edb1ad2017-04-25 10:54:24 -0700147 if (mCurrentState.zOrderRelativeOf != nullptr) {
148 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
149 if (strongRelative != nullptr) {
150 strongRelative->removeZOrderRelative(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700151 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700152 }
153 mCurrentState.zOrderRelativeOf = nullptr;
154 }
Rob Carr4bba3702018-10-08 21:53:30 +0000155
Chia-I Wuc6657022017-08-15 11:18:17 -0700156 for (const auto& child : mCurrentChildren) {
157 child->onRemovedFromCurrentState();
158 }
159}
Chia-I Wu38512252017-05-17 14:36:16 -0700160
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000161void Layer::onRemoved() {
162 // the layer is removed from SF mLayersPendingRemoval
163 abandon();
164
165 destroyAllHwcLayers();
166
167 for (const auto& child : mCurrentChildren) {
168 child->onRemoved();
169 }
170}
171
Mathias Agopian13127d82013-03-05 17:47:11 -0800172// ---------------------------------------------------------------------------
173// set-up
174// ---------------------------------------------------------------------------
175
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700176const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800177 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800178}
179
chaviw13fdc492017-06-27 12:40:18 -0700180bool Layer::getPremultipledAlpha() const {
181 return mPremultipliedAlpha;
182}
183
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700184sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800185 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700186 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800187}
188
189// ---------------------------------------------------------------------------
190// h/w composer set-up
191// ---------------------------------------------------------------------------
192
Dominik Laskowski7e045462018-05-30 13:02:02 -0700193bool Layer::createHwcLayer(HWComposer* hwc, int32_t displayId) {
194 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.count(displayId) != 0,
195 "Already have a layer for display %d", displayId);
David Sodmanb8aaea12017-12-14 15:54:51 -0800196 auto layer = std::shared_ptr<HWC2::Layer>(
197 hwc->createLayer(displayId),
198 [hwc, displayId](HWC2::Layer* layer) {
199 hwc->destroyLayer(displayId, layer); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700200 if (!layer) {
201 return false;
202 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700203 LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers[displayId];
Steven Thomasb02664d2017-07-26 18:48:28 -0700204 hwcInfo.hwc = hwc;
205 hwcInfo.layer = layer;
David Sodmanf6a38932018-05-25 15:27:50 -0700206 layer->setLayerDestroyedListener(
Dominik Laskowski7e045462018-05-30 13:02:02 -0700207 [this, displayId](HWC2::Layer* /*layer*/) { getBE().mHwcLayers.erase(displayId); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700208 return true;
209}
210
Dominik Laskowski7e045462018-05-30 13:02:02 -0700211bool Layer::destroyHwcLayer(int32_t displayId) {
212 if (getBE().mHwcLayers.count(displayId) == 0) {
Chia-I Wu83806892017-11-16 10:50:20 -0800213 return false;
Steven Thomasb02664d2017-07-26 18:48:28 -0700214 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700215 auto& hwcInfo = getBE().mHwcLayers[displayId];
David Sodman41fdfc92017-11-06 16:09:56 -0800216 LOG_ALWAYS_FATAL_IF(hwcInfo.layer == nullptr, "Attempt to destroy null layer");
Steven Thomasb02664d2017-07-26 18:48:28 -0700217 LOG_ALWAYS_FATAL_IF(hwcInfo.hwc == nullptr, "Missing HWComposer");
David Sodmanb8aaea12017-12-14 15:54:51 -0800218 hwcInfo.layer = nullptr;
219
Chia-I Wu83806892017-11-16 10:50:20 -0800220 return true;
Steven Thomasb02664d2017-07-26 18:48:28 -0700221}
222
223void Layer::destroyAllHwcLayers() {
David Sodman6f65f3e2017-11-03 14:28:09 -0700224 size_t numLayers = getBE().mHwcLayers.size();
Steven Thomasb02664d2017-07-26 18:48:28 -0700225 for (size_t i = 0; i < numLayers; ++i) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700226 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.empty(), "destroyAllHwcLayers failed");
227 destroyHwcLayer(getBE().mHwcLayers.begin()->first);
Steven Thomasb02664d2017-07-26 18:48:28 -0700228 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700229 LOG_ALWAYS_FATAL_IF(!getBE().mHwcLayers.empty(),
David Sodman41fdfc92017-11-06 16:09:56 -0800230 "All hardware composer layers should have been destroyed");
Steven Thomasb02664d2017-07-26 18:48:28 -0700231}
Steven Thomasb02664d2017-07-26 18:48:28 -0700232
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800233Rect Layer::getContentCrop() const {
234 // this is the crop rectangle that applies to the buffer
235 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700236 Rect crop;
237 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800238 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700239 crop = mCurrentCrop;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800240 } else if (getBE().compositionInfo.mBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800241 // otherwise we use the whole buffer
David Sodman0cc69182017-11-17 12:12:07 -0800242 crop = getBE().compositionInfo.mBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700243 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800244 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700245 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700246 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700247 return crop;
248}
249
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700250static Rect reduce(const Rect& win, const Region& exclude) {
251 if (CC_LIKELY(exclude.isEmpty())) {
252 return win;
253 }
254 if (exclude.isRect()) {
255 return win.reduce(exclude.getBounds());
256 }
257 return Region(win).subtract(exclude).getBounds();
258}
259
Dan Stoza80d61162017-12-20 15:57:52 -0800260static FloatRect reduce(const FloatRect& win, const Region& exclude) {
261 if (CC_LIKELY(exclude.isEmpty())) {
262 return win;
263 }
264 // Convert through Rect (by rounding) for lack of FloatRegion
265 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
266}
267
Vishnu Nair60356342018-11-13 13:00:45 -0800268Rect Layer::computeScreenBounds() const {
269 FloatRect bounds = computeBounds();
Peiyong Linefefaac2018-08-17 12:27:51 -0700270 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800271 // Transform to screen space.
272 bounds = t.transform(bounds);
273 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700274}
275
Dan Stoza80d61162017-12-20 15:57:52 -0800276FloatRect Layer::computeBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000277 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700278 return computeBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700279}
280
Dan Stoza80d61162017-12-20 15:57:52 -0800281FloatRect Layer::computeBounds(const Region& activeTransparentRegion) const {
Alec Mourib416efd2018-09-06 21:01:59 +0000282 const State& s(getDrawingState());
Vishnu Nair60356342018-11-13 13:00:45 -0800283 Rect bounds = getCroppedBufferSize(s);
284 FloatRect floatBounds = bounds.toFloatRect();
285 if (bounds.isValid()) {
286 // Layer has bounds. Pass in our bounds as a special case. Then pass on to our parents so
287 // that they can clip it.
288 floatBounds = cropChildBounds(floatBounds);
289 } else {
290 // Layer does not have bounds, so we fill to our parent bounds. This is done by getting our
291 // parent bounds and inverting the transform to get the maximum bounds we can have that
292 // will fit within our parent bounds.
293 const auto& p = mDrawingParent.promote();
294 if (p != nullptr) {
295 ui::Transform t = s.active_legacy.transform;
296 // When calculating the parent bounds for purposes of clipping, we don't need to
297 // constrain the parent to its transparent region. The transparent region is an
298 // optimization based on the buffer contents of the layer, but does not affect the
299 // space allocated to it by policy, and thus children should be allowed to extend into
300 // the parent's transparent region.
301 // One of the main uses is a parent window with a child sitting behind the parent
302 // window, marked by a transparent region. When computing the parent bounds from the
303 // parent's perspective we pass in the transparent region to reduce buffer allocation
304 // size. When computing the parent bounds from the child's perspective, we pass in an
305 // empty transparent region in order to extend into the the parent bounds.
306 floatBounds = p->computeBounds(Region());
307 // Transform back to layer space.
308 floatBounds = t.inverse().transform(floatBounds);
309 }
310 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700311
Vishnu Nair60356342018-11-13 13:00:45 -0800312 // Subtract the transparent region and snap to the bounds.
313 return reduce(floatBounds, activeTransparentRegion);
314}
315
316FloatRect Layer::cropChildBounds(const FloatRect& childBounds) const {
317 const State& s(getDrawingState());
318 Rect bounds = getCroppedBufferSize(s);
319 FloatRect croppedBounds = childBounds;
320
321 // If the layer has bounds, then crop the passed in child bounds and pass
322 // it to our parents so they can crop it as well. If the layer has no bounds,
323 // then pass on the child bounds.
324 if (bounds.isValid()) {
325 croppedBounds = croppedBounds.intersect(bounds.toFloatRect());
Mathias Agopian13127d82013-03-05 17:47:11 -0800326 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700327
Chia-I Wue41dbe62017-06-13 14:10:56 -0700328 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700329 if (p != nullptr) {
Vishnu Nair60356342018-11-13 13:00:45 -0800330 // Transform to parent space and allow parent layer to crop the
331 // child bounds as well.
332 ui::Transform t = s.active_legacy.transform;
333 croppedBounds = t.transform(croppedBounds);
334 croppedBounds = p->cropChildBounds(croppedBounds);
335 croppedBounds = t.inverse().transform(croppedBounds);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700336 }
Vishnu Nair60356342018-11-13 13:00:45 -0800337 return croppedBounds;
338}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700339
Vishnu Nair60356342018-11-13 13:00:45 -0800340Rect Layer::getCroppedBufferSize(const State& s) const {
341 Rect size = getBufferSize(s);
342 Rect crop = getCrop(s);
343 if (!crop.isEmpty() && size.isValid()) {
344 size.intersect(crop, &size);
345 } else if (!crop.isEmpty()) {
346 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700347 }
Vishnu Nair60356342018-11-13 13:00:45 -0800348 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800349}
350
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700351Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& display) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700352 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800353 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700354 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800355
356 // apply the projection's clipping to the window crop in
357 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700358 // if there are no window scaling involved, this operation will map to full
359 // pixels in the buffer.
360 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
361 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700362
Marissa Wall61c58622018-07-18 10:12:20 -0700363 Rect activeCrop(getActiveWidth(s), getActiveHeight(s));
364 Rect crop = getCrop(s);
365 if (!crop.isEmpty()) {
366 activeCrop.intersect(crop, &activeCrop);
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700367 }
368
Peiyong Linefefaac2018-08-17 12:27:51 -0700369 ui::Transform t = getTransform();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700370 activeCrop = t.transform(activeCrop);
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700371 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000372 activeCrop.clear();
373 }
chaviwb1154d12017-10-31 14:15:36 -0700374
375 const auto& p = mDrawingParent.promote();
376 if (p != nullptr) {
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700377 auto parentCrop = p->computeInitialCrop(display);
chaviwb1154d12017-10-31 14:15:36 -0700378 activeCrop.intersect(parentCrop, &activeCrop);
379 }
380
Robert Carr1f0a16a2016-10-24 16:27:39 -0700381 return activeCrop;
382}
383
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700384FloatRect Layer::computeCrop(const sp<const DisplayDevice>& display) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700385 // the content crop is the area of the content that gets scaled to the
386 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800387 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700388
389 // In addition there is a WM-specified crop we pull from our drawing state.
390 const State& s(getDrawingState());
391
392 // Screen space to make reduction to parent crop clearer.
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700393 Rect activeCrop = computeInitialCrop(display);
Peiyong Linefefaac2018-08-17 12:27:51 -0700394 ui::Transform t = getTransform();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700395 // Back to layer space to work with the content crop.
396 activeCrop = t.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800397
Michael Lentine28ea2172014-11-19 18:32:37 -0800398 // This needs to be here as transform.transform(Rect) computes the
399 // transformed rect and then takes the bounding box of the result before
400 // returning. This means
401 // transform.inverse().transform(transform.transform(Rect)) != Rect
402 // in which case we need to make sure the final rect is clipped to the
403 // display bounds.
Marissa Wall61c58622018-07-18 10:12:20 -0700404 if (!activeCrop.intersect(Rect(getActiveWidth(s), getActiveHeight(s)), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000405 activeCrop.clear();
406 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800407
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700408 // subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700409 activeCrop = reduce(activeCrop, getActiveTransparentRegion(s));
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700410
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000411 // Transform the window crop to match the buffer coordinate system,
412 // which means using the inverse of the current transform set on the
413 // SurfaceFlingerConsumer.
414 uint32_t invTransform = mCurrentTransform;
Robert Carrcae605c2017-03-29 12:10:31 -0700415 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000416 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700417 * the code below applies the primary display's inverse transform to the
418 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000419 */
David Sodman41fdfc92017-11-06 16:09:56 -0800420 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000421 // calculate the inverse transform
422 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800423 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800424 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000425 // and apply to the current transform
Peiyong Linefefaac2018-08-17 12:27:51 -0700426 invTransform = (ui::Transform(invTransformOrient) *
427 ui::Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800428 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000429
Marissa Wall61c58622018-07-18 10:12:20 -0700430 int winWidth = getActiveWidth(s);
431 int winHeight = getActiveHeight(s);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000432 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
433 // If the activeCrop has been rotate the ends are rotated but not
434 // the space itself so when transforming ends back we can't rely on
435 // a modification of the axes of rotation. To account for this we
436 // need to reorient the inverse rotation in terms of the current
437 // axes of rotation.
438 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
439 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
440 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800441 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000442 }
Marissa Wall61c58622018-07-18 10:12:20 -0700443 winWidth = getActiveHeight(s);
444 winHeight = getActiveWidth(s);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000445 }
Marissa Wall61c58622018-07-18 10:12:20 -0700446 const Rect winCrop = activeCrop.transform(invTransform, getActiveWidth(s), getActiveHeight(s));
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000447
448 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800449 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000450 float yScale = crop.getHeight() / float(winHeight);
451
David Sodman41fdfc92017-11-06 16:09:56 -0800452 float insetL = winCrop.left * xScale;
453 float insetT = winCrop.top * yScale;
454 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000455 float insetB = (winHeight - winCrop.bottom) * yScale;
456
David Sodman41fdfc92017-11-06 16:09:56 -0800457 crop.left += insetL;
458 crop.top += insetT;
459 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000460 crop.bottom -= insetB;
461
Mathias Agopian13127d82013-03-05 17:47:11 -0800462 return crop;
463}
464
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700465void Layer::setGeometry(const sp<const DisplayDevice>& display, uint32_t z) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700466 const auto displayId = display->getId();
Peiyong Lin91b1df22018-06-18 18:00:16 -0700467 if (!hasHwcLayer(displayId)) {
468 ALOGE("[%s] failed to setGeometry: no HWC layer found (%d)",
469 mName.string(), displayId);
470 return;
471 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700472 auto& hwcInfo = getBE().mHwcLayers[displayId];
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700473
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700474 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800475 hwcInfo.forceClientComposition = false;
476
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700477 if (isSecure() && !display->isSecure()) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800478 hwcInfo.forceClientComposition = true;
479 }
480
David Sodman15094112018-10-11 09:39:37 -0700481 auto& hwcLayer = hwcInfo.layer;
482
Mathias Agopian13127d82013-03-05 17:47:11 -0800483 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700484 const State& s(getDrawingState());
David Revemanecf0fa52017-03-03 11:32:44 -0500485 auto blendMode = HWC2::BlendMode::None;
Robert Carr6452f122017-03-21 10:41:29 -0700486 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800487 blendMode =
488 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800489 }
David Sodman15094112018-10-11 09:39:37 -0700490 auto error = hwcLayer->setBlendMode(blendMode);
491 ALOGE_IF(error != HWC2::Error::None,
492 "[%s] Failed to set blend mode %s:"
493 " %s (%d)",
494 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
495 static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700496 getBE().compositionInfo.hwc.blendMode = blendMode;
Mathias Agopian13127d82013-03-05 17:47:11 -0800497
498 // apply the layer's transform, followed by the display's global transform
499 // here we're guaranteed that the layer's transform preserves rects
Marissa Wall61c58622018-07-18 10:12:20 -0700500 Region activeTransparentRegion(getActiveTransparentRegion(s));
Peiyong Linefefaac2018-08-17 12:27:51 -0700501 ui::Transform t = getTransform();
Marissa Wall61c58622018-07-18 10:12:20 -0700502 Rect activeCrop = getCrop(s);
503 if (!activeCrop.isEmpty()) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700504 activeCrop = t.transform(activeCrop);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700505 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000506 activeCrop.clear();
507 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700508 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800509 // This needs to be here as transform.transform(Rect) computes the
510 // transformed rect and then takes the bounding box of the result before
511 // returning. This means
512 // transform.inverse().transform(transform.transform(Rect)) != Rect
513 // in which case we need to make sure the final rect is clipped to the
514 // display bounds.
Marissa Wall61c58622018-07-18 10:12:20 -0700515 if (!activeCrop.intersect(Rect(getActiveWidth(s), getActiveHeight(s)), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000516 activeCrop.clear();
517 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700518 // mark regions outside the crop as transparent
Marissa Wall61c58622018-07-18 10:12:20 -0700519 activeTransparentRegion.orSelf(Rect(0, 0, getActiveWidth(s), activeCrop.top));
Marissa Wallf58c14b2018-07-24 10:50:43 -0700520 activeTransparentRegion.orSelf(
Marissa Wall61c58622018-07-18 10:12:20 -0700521 Rect(0, activeCrop.bottom, getActiveWidth(s), getActiveHeight(s)));
David Sodman41fdfc92017-11-06 16:09:56 -0800522 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
523 activeTransparentRegion.orSelf(
Marissa Wall61c58622018-07-18 10:12:20 -0700524 Rect(activeCrop.right, activeCrop.top, getActiveWidth(s), activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700525 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700526
Dan Stoza80d61162017-12-20 15:57:52 -0800527 // computeBounds returns a FloatRect to provide more accuracy during the
528 // transformation. We then round upon constructing 'frame'.
529 Rect frame{t.transform(computeBounds(activeTransparentRegion))};
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700530 if (!frame.intersect(display->getViewport(), &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000531 frame.clear();
532 }
Peiyong Linefefaac2018-08-17 12:27:51 -0700533 const ui::Transform& tr = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800534 Rect transformedFrame = tr.transform(frame);
David Sodman15094112018-10-11 09:39:37 -0700535 error = hwcLayer->setDisplayFrame(transformedFrame);
536 if (error != HWC2::Error::None) {
537 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
538 transformedFrame.left, transformedFrame.top, transformedFrame.right,
539 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
540 } else {
541 hwcInfo.displayFrame = transformedFrame;
542 }
David Sodmanba340492018-08-05 21:51:33 -0700543 getBE().compositionInfo.hwc.displayFrame = transformedFrame;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800544
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700545 FloatRect sourceCrop = computeCrop(display);
David Sodman15094112018-10-11 09:39:37 -0700546 error = hwcLayer->setSourceCrop(sourceCrop);
547 if (error != HWC2::Error::None) {
548 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
549 "%s (%d)",
550 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
551 to_string(error).c_str(), static_cast<int32_t>(error));
552 } else {
553 hwcInfo.sourceCrop = sourceCrop;
554 }
David Sodmanba340492018-08-05 21:51:33 -0700555 getBE().compositionInfo.hwc.sourceCrop = sourceCrop;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800556
chaviw13fdc492017-06-27 12:40:18 -0700557 float alpha = static_cast<float>(getAlpha());
David Sodman15094112018-10-11 09:39:37 -0700558 error = hwcLayer->setPlaneAlpha(alpha);
559 ALOGE_IF(error != HWC2::Error::None,
560 "[%s] Failed to set plane alpha %.3f: "
561 "%s (%d)",
562 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700563 getBE().compositionInfo.hwc.alpha = alpha;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800564
David Sodman15094112018-10-11 09:39:37 -0700565 error = hwcLayer->setZOrder(z);
566 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
567 to_string(error).c_str(), static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700568 getBE().compositionInfo.hwc.z = z;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500569
Albert Chaulk2a589632017-05-04 16:59:44 -0400570 int type = s.type;
571 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700572 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400573 if (parent.get()) {
574 auto& parentState = parent->getDrawingState();
rongliucfb187b2018-03-14 12:26:23 -0700575 if (parentState.type >= 0 || parentState.appId >= 0) {
576 type = parentState.type;
577 appId = parentState.appId;
578 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400579 }
580
David Sodman15094112018-10-11 09:39:37 -0700581 error = hwcLayer->setInfo(type, appId);
582 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
583 static_cast<int32_t>(error));
584
David Sodmanba340492018-08-05 21:51:33 -0700585 getBE().compositionInfo.hwc.type = type;
586 getBE().compositionInfo.hwc.appId = appId;
587
Mathias Agopian29a367b2011-07-12 14:51:45 -0700588 /*
589 * Transformations are applied in this order:
590 * 1) buffer orientation/flip/mirror
591 * 2) state transformation (window manager)
592 * 3) layer orientation (screen orientation)
593 * (NOTE: the matrices are multiplied in reverse order)
594 */
595
Peiyong Linefefaac2018-08-17 12:27:51 -0700596 const ui::Transform bufferOrientation(mCurrentTransform);
597 ui::Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700598
Robert Carrcae605c2017-03-29 12:10:31 -0700599 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700600 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700601 * the code below applies the primary display's inverse transform to the
602 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700603 */
David Sodman41fdfc92017-11-06 16:09:56 -0800604 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700605 // calculate the inverse transform
606 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800607 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700608 }
Robert Carrcae605c2017-03-29 12:10:31 -0700609
610 /*
611 * Here we cancel out the orientation component of the WM transform.
612 * The scaling and translate components are already included in our bounds
613 * computation so it's enough to just omit it in the composition.
614 * See comment in onDraw with ref to b/36727915 for why.
615 */
Peiyong Linefefaac2018-08-17 12:27:51 -0700616 transform = ui::Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700617 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700618
619 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800620 const uint32_t orientation = transform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -0700621 if (orientation & ui::Transform::ROT_INVALID) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800622 // we can only handle simple transformation
Lloyd Pique074e8122018-07-26 12:57:23 -0700623 hwcInfo.forceClientComposition = true;
David Sodman15094112018-10-11 09:39:37 -0700624 getBE().mHwcLayers[displayId].compositionType = HWC2::Composition::Client;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800625 } else {
626 auto transform = static_cast<HWC2::Transform>(orientation);
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800627 hwcInfo.transform = transform;
David Sodman15094112018-10-11 09:39:37 -0700628 auto error = hwcLayer->setTransform(transform);
629 ALOGE_IF(error != HWC2::Error::None,
630 "[%s] Failed to set transform %s: "
631 "%s (%d)",
632 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
633 static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700634 getBE().compositionInfo.hwc.transform = transform;
David Sodman4b7c4bc2017-11-17 12:13:59 -0800635 }
636}
637
Dominik Laskowski7e045462018-05-30 13:02:02 -0700638void Layer::forceClientComposition(int32_t displayId) {
639 if (getBE().mHwcLayers.count(displayId) == 0) {
640 ALOGE("forceClientComposition: no HWC layer found (%d)", displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800641 return;
642 }
643
Dominik Laskowski7e045462018-05-30 13:02:02 -0700644 getBE().mHwcLayers[displayId].forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800645}
Dan Stozaee44edd2015-03-23 15:50:23 -0700646
Dominik Laskowski7e045462018-05-30 13:02:02 -0700647bool Layer::getForceClientComposition(int32_t displayId) {
648 if (getBE().mHwcLayers.count(displayId) == 0) {
649 ALOGE("getForceClientComposition: no HWC layer found (%d)", displayId);
chaviwc9232ed2017-11-14 15:31:15 -0800650 return false;
651 }
652
Dominik Laskowski7e045462018-05-30 13:02:02 -0700653 return getBE().mHwcLayers[displayId].forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800654}
655
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700656void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700657 const auto displayId = display->getId();
658 if (getBE().mHwcLayers.count(displayId) == 0 ||
659 getCompositionType(displayId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800660 return;
661 }
662
663 // This gives us only the "orientation" component of the transform
664 const State& s(getCurrentState());
665
666 // Apply the layer's transform, followed by the display's global transform
667 // Here we're guaranteed that the layer's transform preserves rects
Marissa Wall61c58622018-07-18 10:12:20 -0700668 Rect win(getActiveWidth(s), getActiveHeight(s));
669 Rect crop = getCrop(s);
670 if (!crop.isEmpty()) {
671 win.intersect(crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800672 }
673 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700674 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Robert Carr1f0a16a2016-10-24 16:27:39 -0700675 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700676 frame.intersect(display->getViewport(), &frame);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700677 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800678 auto position = displayTransform.transform(frame);
679
Dominik Laskowski7e045462018-05-30 13:02:02 -0700680 auto error =
David Sodmanb8aaea12017-12-14 15:54:51 -0800681 (getBE().mHwcLayers[displayId].layer)->setCursorPosition(
682 position.left, position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800683 ALOGE_IF(error != HWC2::Error::None,
684 "[%s] Failed to set cursor position "
685 "to (%d, %d): %s (%d)",
686 mName.string(), position.left, position.top, to_string(error).c_str(),
687 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800688}
Riley Andrews03414a12014-07-01 14:22:59 -0700689
Mathias Agopian13127d82013-03-05 17:47:11 -0800690// ---------------------------------------------------------------------------
691// drawing...
692// ---------------------------------------------------------------------------
693
Marissa Wall61c58622018-07-18 10:12:20 -0700694void Layer::draw(const RenderArea& renderArea, const Region& clip) {
chaviwa76b2712017-09-20 12:02:26 -0700695 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800696}
697
Marissa Wall61c58622018-07-18 10:12:20 -0700698void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) {
chaviwa76b2712017-09-20 12:02:26 -0700699 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800700}
701
David Sodman41fdfc92017-11-06 16:09:56 -0800702void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
703 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800704 auto& engine(mFlinger->getRenderEngine());
David Sodman9eeae692017-11-02 10:53:32 -0700705 computeGeometry(renderArea, getBE().mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700706 engine.setupFillWithColor(red, green, blue, alpha);
David Sodman9eeae692017-11-02 10:53:32 -0700707 engine.drawMesh(getBE().mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800708}
709
David Sodmanc1498e62018-09-12 14:36:26 -0700710void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
711 clearWithOpenGL(renderArea, 0, 0, 0, 0);
712}
713
David Sodman15094112018-10-11 09:39:37 -0700714void Layer::setCompositionType(int32_t displayId, HWC2::Composition type, bool callIntoHwc) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700715 if (getBE().mHwcLayers.count(displayId) == 0) {
Chia-I Wu30505fb2018-03-26 16:20:31 -0700716 ALOGE("setCompositionType called without a valid HWC layer");
717 return;
David Sodman15094112018-10-11 09:39:37 -0700718 }
719 auto& hwcInfo = getBE().mHwcLayers[displayId];
720 auto& hwcLayer = hwcInfo.layer;
721 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", (hwcLayer)->getId(), to_string(type).c_str(),
722 static_cast<int>(callIntoHwc));
723 if (hwcInfo.compositionType != type) {
724 ALOGV(" actually setting");
725 hwcInfo.compositionType = type;
726 if (callIntoHwc) {
727 auto error = (hwcLayer)->setCompositionType(type);
728 ALOGE_IF(error != HWC2::Error::None,
729 "[%s] Failed to set "
730 "composition type %s: %s (%d)",
731 mName.string(), to_string(type).c_str(), to_string(error).c_str(),
732 static_cast<int32_t>(error));
Chia-I Wu30505fb2018-03-26 16:20:31 -0700733 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800734 }
735}
736
Dominik Laskowski7e045462018-05-30 13:02:02 -0700737HWC2::Composition Layer::getCompositionType(int32_t displayId) const {
David Sodman15fb96e2018-01-07 10:23:24 -0800738 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700739 // If we're querying the composition type for a display that does not
740 // have a HWC counterpart, then it will always be Client
741 return HWC2::Composition::Client;
742 }
David Sodman15fb96e2018-01-07 10:23:24 -0800743 return getBE().mHwcLayers[displayId].compositionType;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800744}
745
Dominik Laskowski7e045462018-05-30 13:02:02 -0700746void Layer::setClearClientTarget(int32_t displayId, bool clear) {
747 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800748 ALOGE("setClearClientTarget called without a valid HWC layer");
749 return;
750 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700751 getBE().mHwcLayers[displayId].clearClientTarget = clear;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800752}
753
Dominik Laskowski7e045462018-05-30 13:02:02 -0700754bool Layer::getClearClientTarget(int32_t displayId) const {
755 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800756 ALOGE("getClearClientTarget called without a valid HWC layer");
757 return false;
758 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700759 return getBE().mHwcLayers.at(displayId).clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800760}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800761
Dan Stozacac35382016-01-27 12:21:06 -0800762bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
763 if (point->getFrameNumber() <= mCurrentFrameNumber) {
764 // Don't bother with a SyncPoint, since we've already latched the
765 // relevant frame
766 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700767 }
768
Dan Stozacac35382016-01-27 12:21:06 -0800769 Mutex::Autolock lock(mLocalSyncPointMutex);
770 mLocalSyncPoints.push_back(point);
771 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700772}
773
Mathias Agopian13127d82013-03-05 17:47:11 -0800774// ----------------------------------------------------------------------------
775// local state
776// ----------------------------------------------------------------------------
777
Peiyong Lin833074a2018-08-28 11:53:54 -0700778void Layer::computeGeometry(const RenderArea& renderArea,
779 renderengine::Mesh& mesh,
chaviwa76b2712017-09-20 12:02:26 -0700780 bool useIdentityTransform) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700781 const ui::Transform renderAreaTransform(renderArea.getTransform());
Dan Stoza80d61162017-12-20 15:57:52 -0800782 FloatRect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700783
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000784 vec2 lt = vec2(win.left, win.top);
785 vec2 lb = vec2(win.left, win.bottom);
786 vec2 rb = vec2(win.right, win.bottom);
787 vec2 rt = vec2(win.right, win.top);
788
Peiyong Linefefaac2018-08-17 12:27:51 -0700789 ui::Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000790 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700791 lt = layerTransform.transform(lt);
792 lb = layerTransform.transform(lb);
793 rb = layerTransform.transform(rb);
794 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000795 }
796
Peiyong Lin833074a2018-08-28 11:53:54 -0700797 renderengine::Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700798 position[0] = renderAreaTransform.transform(lt);
799 position[1] = renderAreaTransform.transform(lb);
800 position[2] = renderAreaTransform.transform(rb);
801 position[3] = renderAreaTransform.transform(rt);
Mathias Agopian13127d82013-03-05 17:47:11 -0800802}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800803
David Sodman41fdfc92017-11-06 16:09:56 -0800804bool Layer::isSecure() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000805 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700806 return (s.flags & layer_state_t::eLayerSecure);
807}
808
Mathias Agopian13127d82013-03-05 17:47:11 -0800809void Layer::setVisibleRegion(const Region& visibleRegion) {
810 // always called from main thread
811 this->visibleRegion = visibleRegion;
812}
813
814void Layer::setCoveredRegion(const Region& coveredRegion) {
815 // always called from main thread
816 this->coveredRegion = coveredRegion;
817}
818
David Sodman41fdfc92017-11-06 16:09:56 -0800819void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800820 // always called from main thread
821 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
822}
823
Robert Carre5f4f692018-01-12 13:12:28 -0800824void Layer::clearVisibilityRegions() {
825 visibleRegion.clear();
826 visibleNonTransparentRegion.clear();
827 coveredRegion.clear();
828}
829
Mathias Agopian13127d82013-03-05 17:47:11 -0800830// ----------------------------------------------------------------------------
831// transaction
832// ----------------------------------------------------------------------------
833
Dan Stoza7dde5992015-05-22 09:51:44 -0700834void Layer::pushPendingState() {
835 if (!mCurrentState.modified) {
836 return;
837 }
838
Dan Stoza7dde5992015-05-22 09:51:44 -0700839 // If this transaction is waiting on the receipt of a frame, generate a sync
840 // point and send it to the remote layer.
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000841 if (mCurrentState.barrierLayer_legacy != nullptr) {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700842 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800843 if (barrierLayer == nullptr) {
844 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700845 // If we can't promote the layer we are intended to wait on,
846 // then it is expired or otherwise invalid. Allow this transaction
847 // to be applied as per normal (no synchronization).
Marissa Wallf58c14b2018-07-24 10:50:43 -0700848 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800849 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700850 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy);
Robert Carr0d480722017-01-10 16:42:54 -0800851 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800852 mRemoteSyncPoints.push_back(std::move(syncPoint));
853 } else {
854 // We already missed the frame we're supposed to synchronize
855 // on, so go ahead and apply the state update
Marissa Wallf58c14b2018-07-24 10:50:43 -0700856 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800857 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700858 }
859
Dan Stoza7dde5992015-05-22 09:51:44 -0700860 // Wake us up to check if the frame has been received
861 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700862 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700863 }
864 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700865 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700866}
867
Pablo Ceballos05289c22016-04-14 15:49:55 -0700868void Layer::popPendingState(State* stateToCommit) {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700869 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700870
871 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700872 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700873}
874
Pablo Ceballos05289c22016-04-14 15:49:55 -0700875bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700876 bool stateUpdateAvailable = false;
877 while (!mPendingStates.empty()) {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700878 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700879 if (mRemoteSyncPoints.empty()) {
880 // If we don't have a sync point for this, apply it anyway. It
881 // will be visually wrong, but it should keep us from getting
882 // into too much trouble.
883 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700884 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700885 stateUpdateAvailable = true;
886 continue;
887 }
888
Marissa Wallf58c14b2018-07-24 10:50:43 -0700889 if (mRemoteSyncPoints.front()->getFrameNumber() !=
890 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800891 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800892
893 // Signal our end of the sync point and then dispose of it
894 mRemoteSyncPoints.front()->setTransactionApplied();
895 mRemoteSyncPoints.pop_front();
896 continue;
897 }
898
Dan Stoza7dde5992015-05-22 09:51:44 -0700899 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
900 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700901 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700902 stateUpdateAvailable = true;
903
904 // Signal our end of the sync point and then dispose of it
905 mRemoteSyncPoints.front()->setTransactionApplied();
906 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800907 } else {
908 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700909 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700910 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700911 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700912 stateUpdateAvailable = true;
913 }
914 }
915
916 // If we still have pending updates, wake SurfaceFlinger back up and point
917 // it at this layer so we can process them
918 if (!mPendingStates.empty()) {
919 setTransactionFlags(eTransactionNeeded);
920 mFlinger->setTransactionFlags(eTraversalNeeded);
921 }
922
923 mCurrentState.modified = false;
924 return stateUpdateAvailable;
925}
926
Marissa Wall61c58622018-07-18 10:12:20 -0700927uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000928 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800929
Marissa Wall61c58622018-07-18 10:12:20 -0700930 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
931 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700932
David Sodmaneb085e02017-10-05 18:49:04 -0700933 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700934 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000935 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800936 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
937 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
938 " requested={ wh={%4u,%4u} }}\n"
939 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
940 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700941 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700942 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
943 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
944 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
945 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
946 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700947 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
948 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
949 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800950 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700951
Robert Carre392b552017-09-19 12:16:05 -0700952 // Don't let Layer::doTransaction update the drawing state
953 // if we have a pending resize, unless we are in fixed-size mode.
954 // the drawing state will be updated only once we receive a buffer
955 // with the correct size.
956 //
957 // In particular, we want to make sure the clip (which is part
958 // of the geometry state) is latched together with the size but is
959 // latched immediately when no resizing is involved.
960 //
961 // If a sideband stream is attached, however, we want to skip this
962 // optimization so that transactions aren't missed when a buffer
963 // never arrives
964 //
965 // In the case that we don't have a buffer we ignore other factors
966 // and avoid entering the resizePending state. At a high level the
967 // resizePending state is to avoid applying the state of the new buffer
968 // to the old buffer. However in the state where we don't have an old buffer
969 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700970 const bool resizePending =
971 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
972 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
David Sodman0cc69182017-11-17 12:12:07 -0800973 (getBE().compositionInfo.mBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700974 if (!isFixedSize()) {
David Sodman0cc69182017-11-17 12:12:07 -0800975 if (resizePending && getBE().compositionInfo.hwc.sidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700976 flags |= eDontUpdateGeometryState;
977 }
978 }
979
Robert Carr7bf247e2017-05-18 14:02:49 -0700980 // Here we apply various requested geometry states, depending on our
981 // latching configuration. See Layer.h for a detailed discussion of
982 // how geometry latching is controlled.
983 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000984 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700985
986 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
987 // mode, which causes attributes which normally latch regardless of scaling mode,
988 // to be delayed. We copy the requested state to the active state making sure
989 // to respect these rules (again see Layer.h for a detailed discussion).
990 //
991 // There is an awkward asymmetry in the handling of the crop states in the position
992 // states, as can be seen below. Largely this arises from position and transform
993 // being stored in the same data structure while having different latching rules.
994 // b/38182305
995 //
Marissa Wall61c58622018-07-18 10:12:20 -0700996 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700997 // applyPendingStates in the presence of deferred transactions.
998 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -0700999 float tx = stateToCommit->active_legacy.transform.tx();
1000 float ty = stateToCommit->active_legacy.transform.ty();
1001 stateToCommit->active_legacy = stateToCommit->requested_legacy;
1002 stateToCommit->active_legacy.transform.set(tx, ty);
1003 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001004 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001005 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -07001006 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001007 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001008 }
1009
Marissa Wall61c58622018-07-18 10:12:20 -07001010 return flags;
1011}
1012
1013uint32_t Layer::doTransaction(uint32_t flags) {
1014 ATRACE_CALL();
1015
1016 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +00001017 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -07001018 if (!applyPendingStates(&c)) {
1019 return 0;
1020 }
1021
1022 flags = doTransactionResize(flags, &c);
1023
Alec Mourib416efd2018-09-06 21:01:59 +00001024 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -07001025
1026 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001027 // invalidate and recompute the visible regions if needed
1028 flags |= Layer::eVisibleRegion;
1029 }
1030
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001031 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001032 // invalidate and recompute the visible regions if needed
1033 flags |= eVisibleRegion;
1034 this->contentDirty = true;
1035
1036 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -07001037 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -07001038 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -08001039 }
1040
Dan Stozac8145172016-04-28 16:29:06 -07001041 // If the layer is hidden, signal and clear out all local sync points so
1042 // that transactions for layers depending on this layer's frames becoming
1043 // visible are not blocked
1044 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001045 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001046 }
1047
Mathias Agopian13127d82013-03-05 17:47:11 -08001048 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001049 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001050 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001051}
1052
Pablo Ceballos05289c22016-04-14 15:49:55 -07001053void Layer::commitTransaction(const State& stateToCommit) {
1054 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001055}
1056
Mathias Agopian13127d82013-03-05 17:47:11 -08001057uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Piquef1c675b2018-09-12 20:45:39 -07001058 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -08001059}
1060
1061uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Piquef1c675b2018-09-12 20:45:39 -07001062 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -08001063}
1064
Robert Carr82364e32016-05-15 11:27:47 -07001065bool Layer::setPosition(float x, float y, bool immediate) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001066 if (mCurrentState.requested_legacy.transform.tx() == x &&
1067 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001068 return false;
1069 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001070
1071 // We update the requested and active position simultaneously because
1072 // we want to apply the position portion of the transform matrix immediately,
1073 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Marissa Wallf58c14b2018-07-24 10:50:43 -07001074 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001075 if (immediate && !mFreezeGeometryUpdates) {
1076 // Here we directly update the active state
1077 // unlike other setters, because we store it within
1078 // the transform, but use different latching rules.
1079 // b/38182305
Marissa Wallf58c14b2018-07-24 10:50:43 -07001080 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001081 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001082 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001083
Dan Stoza7dde5992015-05-22 09:51:44 -07001084 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001085 setTransactionFlags(eTransactionNeeded);
1086 return true;
1087}
Robert Carr82364e32016-05-15 11:27:47 -07001088
Robert Carr1f0a16a2016-10-24 16:27:39 -07001089bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1090 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1091 if (idx < 0) {
1092 return false;
1093 }
1094 if (childLayer->setLayer(z)) {
1095 mCurrentChildren.removeAt(idx);
1096 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001097 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001098 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001099 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001100}
1101
Robert Carr503c7042017-09-27 15:06:08 -07001102bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1103 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1104 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1105 if (idx < 0) {
1106 return false;
1107 }
1108 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1109 mCurrentChildren.removeAt(idx);
1110 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001111 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001112 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001113 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001114}
1115
Robert Carrae060832016-11-28 10:51:00 -08001116bool Layer::setLayer(int32_t z) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001117 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001118 mCurrentState.sequence++;
1119 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001120 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001121
1122 // Discard all relative layering.
1123 if (mCurrentState.zOrderRelativeOf != nullptr) {
1124 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1125 if (strongRelative != nullptr) {
1126 strongRelative->removeZOrderRelative(this);
1127 }
1128 mCurrentState.zOrderRelativeOf = nullptr;
1129 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001130 setTransactionFlags(eTransactionNeeded);
1131 return true;
1132}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001133
Robert Carrdb66e622017-04-10 16:55:57 -07001134void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1135 mCurrentState.zOrderRelatives.remove(relative);
1136 mCurrentState.sequence++;
1137 mCurrentState.modified = true;
1138 setTransactionFlags(eTransactionNeeded);
1139}
1140
1141void Layer::addZOrderRelative(const wp<Layer>& relative) {
1142 mCurrentState.zOrderRelatives.add(relative);
1143 mCurrentState.modified = true;
1144 mCurrentState.sequence++;
1145 setTransactionFlags(eTransactionNeeded);
1146}
1147
Robert Carr503d2bd2017-12-04 15:49:47 -08001148bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001149 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1150 if (handle == nullptr) {
1151 return false;
1152 }
1153 sp<Layer> relative = handle->owner.promote();
1154 if (relative == nullptr) {
1155 return false;
1156 }
1157
Robert Carr503d2bd2017-12-04 15:49:47 -08001158 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1159 mCurrentState.zOrderRelativeOf == relative) {
1160 return false;
1161 }
1162
Robert Carrdb66e622017-04-10 16:55:57 -07001163 mCurrentState.sequence++;
1164 mCurrentState.modified = true;
Robert Carr503d2bd2017-12-04 15:49:47 -08001165 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001166
chaviw9ab4bd12017-11-03 13:11:00 -07001167 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
1168 if (oldZOrderRelativeOf != nullptr) {
1169 oldZOrderRelativeOf->removeZOrderRelative(this);
1170 }
Robert Carrdb66e622017-04-10 16:55:57 -07001171 mCurrentState.zOrderRelativeOf = relative;
1172 relative->addZOrderRelative(this);
1173
1174 setTransactionFlags(eTransactionNeeded);
1175
1176 return true;
1177}
1178
Mathias Agopian13127d82013-03-05 17:47:11 -08001179bool Layer::setSize(uint32_t w, uint32_t h) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001180 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
1181 return false;
1182 mCurrentState.requested_legacy.w = w;
1183 mCurrentState.requested_legacy.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001184 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001185 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001186
1187 // record the new size, from this point on, when the client request
1188 // a buffer, it'll get the new size.
1189 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001190 return true;
1191}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001192bool Layer::setAlpha(float alpha) {
David Sodman41fdfc92017-11-06 16:09:56 -08001193 if (mCurrentState.color.a == alpha) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001194 mCurrentState.sequence++;
chaviw13fdc492017-06-27 12:40:18 -07001195 mCurrentState.color.a = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001196 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001197 setTransactionFlags(eTransactionNeeded);
1198 return true;
1199}
chaviw13fdc492017-06-27 12:40:18 -07001200
1201bool Layer::setColor(const half3& color) {
David Sodman41fdfc92017-11-06 16:09:56 -08001202 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g &&
1203 color.b == mCurrentState.color.b)
chaviw13fdc492017-06-27 12:40:18 -07001204 return false;
1205
1206 mCurrentState.sequence++;
1207 mCurrentState.color.r = color.r;
1208 mCurrentState.color.g = color.g;
1209 mCurrentState.color.b = color.b;
1210 mCurrentState.modified = true;
1211 setTransactionFlags(eTransactionNeeded);
1212 return true;
1213}
1214
Robert Carrd4ae7f32018-06-07 16:10:57 -07001215bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1216 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001217 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001218 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1219
1220 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1221 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1222 return false;
1223 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001224 mCurrentState.sequence++;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001225 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1226 matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001227 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001228 setTransactionFlags(eTransactionNeeded);
1229 return true;
1230}
Marissa Wall61c58622018-07-18 10:12:20 -07001231
Mathias Agopian13127d82013-03-05 17:47:11 -08001232bool Layer::setTransparentRegionHint(const Region& transparent) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001233 mCurrentState.requestedTransparentRegion_legacy = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001234 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001235 setTransactionFlags(eTransactionNeeded);
1236 return true;
1237}
1238bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1239 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
David Sodman41fdfc92017-11-06 16:09:56 -08001240 if (mCurrentState.flags == newFlags) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001241 mCurrentState.sequence++;
1242 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001243 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001244 setTransactionFlags(eTransactionNeeded);
1245 return true;
1246}
Robert Carr99e27f02016-06-16 15:18:02 -07001247
Marissa Wallf58c14b2018-07-24 10:50:43 -07001248bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
1249 if (mCurrentState.requestedCrop_legacy == crop) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001250 mCurrentState.sequence++;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001251 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001252 if (immediate && !mFreezeGeometryUpdates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001253 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001254 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001255 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1256
Dan Stoza7dde5992015-05-22 09:51:44 -07001257 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001258 setTransactionFlags(eTransactionNeeded);
1259 return true;
1260}
Robert Carr8d5227b2017-03-16 15:41:03 -07001261
Robert Carrc3574f72016-03-24 12:19:32 -07001262bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001263 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001264 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001265 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001266 return true;
1267}
1268
rongliucfb187b2018-03-14 12:26:23 -07001269void Layer::setInfo(int32_t type, int32_t appId) {
David Sodman41fdfc92017-11-06 16:09:56 -08001270 mCurrentState.appId = appId;
1271 mCurrentState.type = type;
1272 mCurrentState.modified = true;
1273 setTransactionFlags(eTransactionNeeded);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001274}
1275
Mathias Agopian13127d82013-03-05 17:47:11 -08001276bool Layer::setLayerStack(uint32_t layerStack) {
David Sodman41fdfc92017-11-06 16:09:56 -08001277 if (mCurrentState.layerStack == layerStack) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001278 mCurrentState.sequence++;
1279 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001280 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001281 setTransactionFlags(eTransactionNeeded);
1282 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001283}
1284
Robert Carr1f0a16a2016-10-24 16:27:39 -07001285uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001286 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001287 if (p == nullptr) {
1288 return getDrawingState().layerStack;
1289 }
1290 return p->getLayerStack();
1291}
1292
Marissa Wallf58c14b2018-07-24 10:50:43 -07001293void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
1294 mCurrentState.barrierLayer_legacy = barrierLayer;
1295 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001296 // We don't set eTransactionNeeded, because just receiving a deferral
1297 // request without any other state updates shouldn't actually induce a delay
1298 mCurrentState.modified = true;
1299 pushPendingState();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001300 mCurrentState.barrierLayer_legacy = nullptr;
1301 mCurrentState.frameNumber_legacy = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001302 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001303}
1304
Marissa Wallf58c14b2018-07-24 10:50:43 -07001305void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001306 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001307 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001308}
1309
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001310// ----------------------------------------------------------------------------
1311// pageflip handling...
1312// ----------------------------------------------------------------------------
1313
Robert Carr1f0a16a2016-10-24 16:27:39 -07001314bool Layer::isHiddenByPolicy() const {
Alec Mourib416efd2018-09-06 21:01:59 +00001315 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001316 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001317 if (parent != nullptr && parent->isHiddenByPolicy()) {
1318 return true;
1319 }
1320 return s.flags & layer_state_t::eLayerHidden;
1321}
1322
David Sodman41fdfc92017-11-06 16:09:56 -08001323uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001324 // TODO: should we do something special if mSecure is set?
1325 if (mProtectedByApp) {
1326 // need a hardware-protected path to external video sink
1327 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001328 }
Riley Andrews03414a12014-07-01 14:22:59 -07001329 if (mPotentialCursor) {
1330 usage |= GraphicBuffer::USAGE_CURSOR;
1331 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001332 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001333 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001334}
1335
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001336void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001337 uint32_t orientation = 0;
1338 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001339 // The transform hint is used to improve performance, but we can
1340 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001341 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001342 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001343 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001344 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001345 orientation = 0;
1346 }
1347 }
David Sodmaneb085e02017-10-05 18:49:04 -07001348 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001349}
1350
Mathias Agopian13127d82013-03-05 17:47:11 -08001351// ----------------------------------------------------------------------------
1352// debugging
1353// ----------------------------------------------------------------------------
1354
Marissa Wall61c58622018-07-18 10:12:20 -07001355// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001356LayerDebugInfo Layer::getLayerDebugInfo() const {
1357 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001358 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001359 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001360 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001361 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
1362 info.mType = String8(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001363 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001364 info.mVisibleRegion = visibleRegion;
1365 info.mSurfaceDamageRegion = surfaceDamageRegion;
1366 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001367 info.mX = ds.active_legacy.transform.tx();
1368 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001369 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001370 info.mWidth = ds.active_legacy.w;
1371 info.mHeight = ds.active_legacy.h;
1372 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001373 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001374 info.mFlags = ds.flags;
1375 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001376 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001377 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1378 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1379 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1380 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001381 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001382 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001383 if (buffer != 0) {
1384 info.mActiveBufferWidth = buffer->getWidth();
1385 info.mActiveBufferHeight = buffer->getHeight();
1386 info.mActiveBufferStride = buffer->getStride();
1387 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001388 } else {
1389 info.mActiveBufferWidth = 0;
1390 info.mActiveBufferHeight = 0;
1391 info.mActiveBufferStride = 0;
1392 info.mActiveBufferFormat = 0;
1393 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001394 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001395 info.mNumQueuedFrames = getQueuedFrameCount();
1396 info.mRefreshPending = isBufferLatched();
1397 info.mIsOpaque = isOpaque(ds);
1398 info.mContentDirty = contentDirty;
1399 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001400}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001401
Dan Stozae22aec72016-08-01 13:20:59 -07001402void Layer::miniDumpHeader(String8& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001403 result.append("-------------------------------");
1404 result.append("-------------------------------");
1405 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001406 result.append(" Layer name\n");
1407 result.append(" Z | ");
1408 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001409 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001410 result.append(" Disp Frame (LTRB) | ");
1411 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001412 result.append("-------------------------------");
1413 result.append("-------------------------------");
1414 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001415}
1416
Dominik Laskowski7e045462018-05-30 13:02:02 -07001417void Layer::miniDump(String8& result, int32_t displayId) const {
1418 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stozae22aec72016-08-01 13:20:59 -07001419 return;
1420 }
1421
1422 String8 name;
1423 if (mName.length() > 77) {
1424 std::string shortened;
1425 shortened.append(mName.string(), 36);
1426 shortened.append("[...]");
1427 shortened.append(mName.string() + (mName.length() - 36), 36);
1428 name = shortened.c_str();
1429 } else {
1430 name = mName;
1431 }
1432
1433 result.appendFormat(" %s\n", name.string());
1434
Alec Mourib416efd2018-09-06 21:01:59 +00001435 const State& layerState(getDrawingState());
Lloyd Pique074e8122018-07-26 12:57:23 -07001436 const LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers.at(displayId);
Chia-I Wu1e043612018-03-01 09:45:09 -08001437 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
1438 result.appendFormat(" rel %6d | ", layerState.z);
1439 } else {
1440 result.appendFormat(" %10d | ", layerState.z);
1441 }
Dominik Laskowski7e045462018-05-30 13:02:02 -07001442 result.appendFormat("%10s | ", to_string(getCompositionType(displayId)).c_str());
Lloyd Pique074e8122018-07-26 12:57:23 -07001443 result.appendFormat("%10s | ", to_string(hwcInfo.transform).c_str());
1444 const Rect& frame = hwcInfo.displayFrame;
David Sodman41fdfc92017-11-06 16:09:56 -08001445 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique074e8122018-07-26 12:57:23 -07001446 const FloatRect& crop = hwcInfo.sourceCrop;
David Sodman41fdfc92017-11-06 16:09:56 -08001447 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 -07001448
David Sodman7e4ae112018-02-09 15:02:28 -08001449 result.append("- - - - - - - - - - - - - - - -\n");
1450
1451 std::string compositionInfoStr;
1452 getBE().compositionInfo.dump(compositionInfoStr, "compositionInfo");
1453 result.append(compositionInfoStr.c_str());
1454
Yichi Chen6ca35192018-05-29 12:20:43 +08001455 result.append("- - - - - - - - - - - - - - - -");
1456 result.append("- - - - - - - - - - - - - - - -");
1457 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001458}
Dan Stozae22aec72016-08-01 13:20:59 -07001459
Svetoslavd85084b2014-03-20 10:28:31 -07001460void Layer::dumpFrameStats(String8& result) const {
1461 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001462}
1463
Svetoslavd85084b2014-03-20 10:28:31 -07001464void Layer::clearFrameStats() {
1465 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001466}
1467
Jamie Gennis6547ff42013-07-16 20:12:42 -07001468void Layer::logFrameStats() {
1469 mFrameTracker.logAndResetStats(mName);
1470}
1471
Svetoslavd85084b2014-03-20 10:28:31 -07001472void Layer::getFrameStats(FrameStats* outStats) const {
1473 mFrameTracker.getStats(outStats);
1474}
1475
Brian Andersond6927fb2016-07-23 23:37:30 -07001476void Layer::dumpFrameEvents(String8& result) {
David Sodman41fdfc92017-11-06 16:09:56 -08001477 result.appendFormat("- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001478 Mutex::Autolock lock(mFrameEventHistoryMutex);
1479 mFrameEventHistory.checkFencesForCompletion();
1480 mFrameEventHistory.dump(result);
1481}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001482
Brian Anderson5ea5e592016-12-01 16:54:33 -08001483void Layer::onDisconnect() {
1484 Mutex::Autolock lock(mFrameEventHistoryMutex);
1485 mFrameEventHistory.onDisconnect();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -07001486 mTimeStats.onDisconnect(getSequence());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001487}
1488
Brian Anderson3890c392016-07-25 12:48:08 -07001489void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001490 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001491 if (newTimestamps) {
Yiwei Zhang9689e2f2018-05-11 12:33:23 -07001492 const int32_t layerID = getSequence();
1493 mTimeStats.setLayerName(layerID, getName().c_str());
1494 mTimeStats.setPostTime(layerID, newTimestamps->frameNumber, newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001495 }
1496
Brian Andersond6927fb2016-07-23 23:37:30 -07001497 Mutex::Autolock lock(mFrameEventHistoryMutex);
1498 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001499 // If there are any unsignaled fences in the aquire timeline at this
1500 // point, the previously queued frame hasn't been latched yet. Go ahead
1501 // and try to get the signal time here so the syscall is taken out of
1502 // the main thread's critical path.
1503 mAcquireTimeline.updateSignalTimes();
1504 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001505 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001506 mFrameEventHistory.addQueue(*newTimestamps);
1507 }
1508
Brian Anderson3890c392016-07-25 12:48:08 -07001509 if (outDelta) {
1510 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001511 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001512}
Dan Stozae77c7662016-05-13 11:37:28 -07001513
Chia-I Wu98f1c102017-05-30 14:54:08 -07001514size_t Layer::getChildrenCount() const {
1515 size_t count = 0;
1516 for (const sp<Layer>& child : mCurrentChildren) {
1517 count += 1 + child->getChildrenCount();
1518 }
1519 return count;
1520}
1521
Robert Carr1f0a16a2016-10-24 16:27:39 -07001522void Layer::addChild(const sp<Layer>& layer) {
1523 mCurrentChildren.add(layer);
1524 layer->setParent(this);
1525}
1526
1527ssize_t Layer::removeChild(const sp<Layer>& layer) {
1528 layer->setParent(nullptr);
1529 return mCurrentChildren.remove(layer);
1530}
1531
Robert Carr1db73f62016-12-21 12:58:51 -08001532bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1533 sp<Handle> handle = nullptr;
1534 sp<Layer> newParent = nullptr;
1535 if (newParentHandle == nullptr) {
1536 return false;
1537 }
1538 handle = static_cast<Handle*>(newParentHandle.get());
1539 newParent = handle->owner.promote();
1540 if (newParent == nullptr) {
1541 ALOGE("Unable to promote Layer handle");
1542 return false;
1543 }
1544
1545 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001546 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001547
1548 sp<Client> client(child->mClientRef.promote());
1549 if (client != nullptr) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001550 client->updateParent(newParent);
Robert Carr1db73f62016-12-21 12:58:51 -08001551 }
1552 }
1553 mCurrentChildren.clear();
1554
1555 return true;
1556}
1557
Robert Carr15eae092018-03-23 13:43:53 -07001558void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001559 for (const sp<Layer>& child : mDrawingChildren) {
1560 child->mDrawingParent = newParent;
1561 }
1562}
1563
chaviwf1961f72017-09-18 16:41:07 -07001564bool Layer::reparent(const sp<IBinder>& newParentHandle) {
1565 if (newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001566 return false;
1567 }
1568
1569 auto handle = static_cast<Handle*>(newParentHandle.get());
1570 sp<Layer> newParent = handle->owner.promote();
1571 if (newParent == nullptr) {
1572 ALOGE("Unable to promote Layer handle");
1573 return false;
1574 }
1575
chaviwf1961f72017-09-18 16:41:07 -07001576 sp<Layer> parent = getParent();
1577 if (parent != nullptr) {
1578 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001579 }
chaviwf1961f72017-09-18 16:41:07 -07001580 newParent->addChild(this);
chaviw06178942017-07-27 10:25:59 -07001581
chaviwf1961f72017-09-18 16:41:07 -07001582 sp<Client> client(mClientRef.promote());
chaviw06178942017-07-27 10:25:59 -07001583 sp<Client> newParentClient(newParent->mClientRef.promote());
1584
chaviwf1961f72017-09-18 16:41:07 -07001585 if (client != newParentClient) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001586 client->updateParent(newParent);
chaviw06178942017-07-27 10:25:59 -07001587 }
1588
chaviw06178942017-07-27 10:25:59 -07001589 return true;
1590}
1591
Robert Carr9524cb32017-02-13 11:32:32 -08001592bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001593 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001594 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001595 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001596 if (client != nullptr && parentClient != client) {
Robert Carr7f619b22017-11-06 12:56:35 -08001597 client->detachLayer(child.get());
1598 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001599 }
Robert Carr7f619b22017-11-06 12:56:35 -08001600 }
Robert Carr9524cb32017-02-13 11:32:32 -08001601
1602 return true;
1603}
1604
Peiyong Lind3788632018-09-18 16:01:31 -07001605bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001606 static const mat4 identityMatrix = mat4();
1607
Peiyong Lind3788632018-09-18 16:01:31 -07001608 if (mCurrentState.colorTransform == matrix) {
1609 return false;
1610 }
1611 ++mCurrentState.sequence;
1612 mCurrentState.colorTransform = matrix;
Peiyong Lin747321c2018-10-01 10:03:11 -07001613 mCurrentState.hasColorTransform = matrix != identityMatrix;
1614 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001615 setTransactionFlags(eTransactionNeeded);
1616 return true;
1617}
1618
1619const mat4& Layer::getColorTransform() const {
1620 return getDrawingState().colorTransform;
1621}
1622
1623bool Layer::hasColorTransform() const {
Peiyong Lin747321c2018-10-01 10:03:11 -07001624 return getDrawingState().hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001625}
1626
Chia-I Wu11481472018-05-04 10:43:19 -07001627bool Layer::isLegacyDataSpace() const {
1628 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001629 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001630 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001631}
1632
Robert Carr1f0a16a2016-10-24 16:27:39 -07001633void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001634 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001635}
1636
1637void Layer::clearSyncPoints() {
1638 for (const auto& child : mCurrentChildren) {
1639 child->clearSyncPoints();
1640 }
1641
1642 Mutex::Autolock lock(mLocalSyncPointMutex);
1643 for (auto& point : mLocalSyncPoints) {
1644 point->setFrameAvailable();
1645 }
1646 mLocalSyncPoints.clear();
1647}
1648
1649int32_t Layer::getZ() const {
1650 return mDrawingState.z;
1651}
1652
Robert Carr29abff82017-12-04 13:51:20 -08001653bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1654 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1655 const State& state = useDrawing ? mDrawingState : mCurrentState;
1656 return state.zOrderRelativeOf != nullptr;
1657}
1658
David Sodman41fdfc92017-11-06 16:09:56 -08001659__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001660 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001661 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1662 "makeTraversalList received invalid stateSet");
1663 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1664 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1665 const State& state = useDrawing ? mDrawingState : mCurrentState;
1666
Robert Carr29abff82017-12-04 13:51:20 -08001667 if (state.zOrderRelatives.size() == 0) {
1668 *outSkipRelativeZUsers = true;
1669 return children;
1670 }
1671
chaviwfd462612018-05-31 16:11:27 -07001672 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001673 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001674 sp<Layer> strongRelative = weakRelative.promote();
1675 if (strongRelative != nullptr) {
1676 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001677 }
1678 }
1679
Dan Stoza412903f2017-04-27 13:42:17 -07001680 for (const sp<Layer>& child : children) {
Robert Carr503c7042017-09-27 15:06:08 -07001681 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1682 if (childState.zOrderRelativeOf != nullptr) {
1683 continue;
1684 }
Robert Carrdb66e622017-04-10 16:55:57 -07001685 traverse.add(child);
1686 }
1687
1688 return traverse;
1689}
1690
Robert Carr1f0a16a2016-10-24 16:27:39 -07001691/**
Robert Carrdb66e622017-04-10 16:55:57 -07001692 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001693 */
Dan Stoza412903f2017-04-27 13:42:17 -07001694void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001695 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1696 // produce a new list for traversing, including our relatives, and not including our children
1697 // who are relatives of another surface. In the case that there are no relative Z,
1698 // makeTraversalList returns our children directly to avoid significant overhead.
1699 // However in this case we need to take the responsibility for filtering children which
1700 // are relatives of another surface here.
1701 bool skipRelativeZUsers = false;
1702 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001703
Robert Carr1f0a16a2016-10-24 16:27:39 -07001704 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001705 for (; i < list.size(); i++) {
1706 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001707 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1708 continue;
1709 }
1710
Robert Carrdb66e622017-04-10 16:55:57 -07001711 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001712 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001713 }
Dan Stoza412903f2017-04-27 13:42:17 -07001714 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001715 }
Robert Carr29abff82017-12-04 13:51:20 -08001716
Dan Stoza412903f2017-04-27 13:42:17 -07001717 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001718 for (; i < list.size(); i++) {
1719 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001720
1721 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1722 continue;
1723 }
Dan Stoza412903f2017-04-27 13:42:17 -07001724 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001725 }
1726}
1727
1728/**
Robert Carrdb66e622017-04-10 16:55:57 -07001729 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001730 */
Dan Stoza412903f2017-04-27 13:42:17 -07001731void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1732 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001733 // See traverseInZOrder for documentation.
1734 bool skipRelativeZUsers = false;
1735 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001736
Robert Carr1f0a16a2016-10-24 16:27:39 -07001737 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001738 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001739 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001740
1741 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1742 continue;
1743 }
1744
Robert Carrdb66e622017-04-10 16:55:57 -07001745 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001746 break;
1747 }
Dan Stoza412903f2017-04-27 13:42:17 -07001748 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001749 }
Dan Stoza412903f2017-04-27 13:42:17 -07001750 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001751 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001752 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001753
1754 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1755 continue;
1756 }
1757
Dan Stoza412903f2017-04-27 13:42:17 -07001758 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001759 }
1760}
1761
chaviw4b129c22018-04-09 16:19:43 -07001762LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1763 const std::vector<Layer*>& layersInTree) {
1764 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1765 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001766 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1767 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
chaviw4b129c22018-04-09 16:19:43 -07001768 const State& state = useDrawing ? mDrawingState : mCurrentState;
1769
chaviwfd462612018-05-31 16:11:27 -07001770 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001771 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1772 sp<Layer> strongRelative = weakRelative.promote();
1773 // Only add relative layers that are also descendents of the top most parent of the tree.
1774 // If a relative layer is not a descendent, then it should be ignored.
1775 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1776 traverse.add(strongRelative);
1777 }
1778 }
1779
1780 for (const sp<Layer>& child : children) {
1781 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1782 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1783 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1784 // the child here since it won't be added later as a relative.
1785 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1786 childState.zOrderRelativeOf.promote().get())) {
1787 continue;
1788 }
1789 traverse.add(child);
1790 }
1791
1792 return traverse;
1793}
1794
1795void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1796 LayerVector::StateSet stateSet,
1797 const LayerVector::Visitor& visitor) {
1798 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001799
1800 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001801 for (; i < list.size(); i++) {
1802 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001803 if (relative->getZ() >= 0) {
1804 break;
1805 }
chaviw4b129c22018-04-09 16:19:43 -07001806 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001807 }
chaviw4b129c22018-04-09 16:19:43 -07001808
chaviwa76b2712017-09-20 12:02:26 -07001809 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001810 for (; i < list.size(); i++) {
1811 const auto& relative = list[i];
1812 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001813 }
1814}
1815
chaviw4b129c22018-04-09 16:19:43 -07001816std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1817 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1818 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1819
1820 std::vector<Layer*> layersInTree = {this};
1821 for (size_t i = 0; i < children.size(); i++) {
1822 const auto& child = children[i];
1823 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1824 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1825 }
1826
1827 return layersInTree;
1828}
1829
1830void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1831 const LayerVector::Visitor& visitor) {
1832 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1833 std::sort(layersInTree.begin(), layersInTree.end());
1834 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1835}
1836
Peiyong Linefefaac2018-08-17 12:27:51 -07001837ui::Transform Layer::getTransform() const {
1838 ui::Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001839 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001840 if (p != nullptr) {
1841 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07001842
1843 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
1844 // it isFixedSize) then there may be additional scaling not accounted
1845 // for in the transform. We need to mirror this scaling in child surfaces
1846 // or we will break the contract where WM can treat child surfaces as
1847 // pixels in the parent surface.
David Sodman0cc69182017-11-17 12:12:07 -08001848 if (p->isFixedSize() && p->getBE().compositionInfo.mBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07001849 int bufferWidth;
1850 int bufferHeight;
1851 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
David Sodman0cc69182017-11-17 12:12:07 -08001852 bufferWidth = p->getBE().compositionInfo.mBuffer->getWidth();
1853 bufferHeight = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001854 } else {
David Sodman0cc69182017-11-17 12:12:07 -08001855 bufferHeight = p->getBE().compositionInfo.mBuffer->getWidth();
1856 bufferWidth = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001857 }
Marissa Wall61c58622018-07-18 10:12:20 -07001858 float sx = p->getActiveWidth(p->getDrawingState()) / static_cast<float>(bufferWidth);
1859 float sy = p->getActiveHeight(p->getDrawingState()) / static_cast<float>(bufferHeight);
Peiyong Linefefaac2018-08-17 12:27:51 -07001860 ui::Transform extraParentScaling;
Robert Carr9b429f42017-04-17 14:56:57 -07001861 extraParentScaling.set(sx, 0, 0, sy);
1862 t = t * extraParentScaling;
1863 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001864 }
Marissa Wall61c58622018-07-18 10:12:20 -07001865 return t * getActiveTransform(getDrawingState());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001866}
1867
chaviw13fdc492017-06-27 12:40:18 -07001868half Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001869 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07001870
chaviw13fdc492017-06-27 12:40:18 -07001871 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1872 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001873}
Robert Carr6452f122017-03-21 10:41:29 -07001874
chaviw13fdc492017-06-27 12:40:18 -07001875half4 Layer::getColor() const {
1876 const half4 color(getDrawingState().color);
1877 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001878}
Robert Carr6452f122017-03-21 10:41:29 -07001879
Robert Carr1f0a16a2016-10-24 16:27:39 -07001880void Layer::commitChildList() {
1881 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1882 const auto& child = mCurrentChildren[i];
1883 child->commitChildList();
1884 }
1885 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001886 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001887}
1888
chaviw1d044282017-09-27 12:19:28 -07001889void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
1890 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1891 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1892 const State& state = useDrawing ? mDrawingState : mCurrentState;
1893
Peiyong Linefefaac2018-08-17 12:27:51 -07001894 ui::Transform requestedTransform = state.active_legacy.transform;
1895 ui::Transform transform = getTransform();
chaviw1d044282017-09-27 12:19:28 -07001896
1897 layerInfo->set_id(sequence);
1898 layerInfo->set_name(getName().c_str());
1899 layerInfo->set_type(String8(getTypeId()));
1900
1901 for (const auto& child : children) {
1902 layerInfo->add_children(child->sequence);
1903 }
1904
1905 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1906 sp<Layer> strongRelative = weakRelative.promote();
1907 if (strongRelative != nullptr) {
1908 layerInfo->add_relatives(strongRelative->sequence);
1909 }
1910 }
1911
Marissa Wallf58c14b2018-07-24 10:50:43 -07001912 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
chaviw1d044282017-09-27 12:19:28 -07001913 layerInfo->mutable_transparent_region());
1914 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
1915 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
1916
1917 layerInfo->set_layer_stack(getLayerStack());
1918 layerInfo->set_z(state.z);
1919
1920 PositionProto* position = layerInfo->mutable_position();
1921 position->set_x(transform.tx());
1922 position->set_y(transform.ty());
1923
1924 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
1925 requestedPosition->set_x(requestedTransform.tx());
1926 requestedPosition->set_y(requestedTransform.ty());
1927
1928 SizeProto* size = layerInfo->mutable_size();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001929 size->set_w(state.active_legacy.w);
1930 size->set_h(state.active_legacy.h);
chaviw1d044282017-09-27 12:19:28 -07001931
Marissa Wallf58c14b2018-07-24 10:50:43 -07001932 LayerProtoHelper::writeToProto(state.crop_legacy, layerInfo->mutable_crop());
chaviw1d044282017-09-27 12:19:28 -07001933
1934 layerInfo->set_is_opaque(isOpaque(state));
1935 layerInfo->set_invalidate(contentDirty);
Chia-I Wu01591c92018-05-22 12:03:00 -07001936
1937 // XXX (b/79210409) mCurrentDataSpace is not protected
1938 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
1939
chaviw1d044282017-09-27 12:19:28 -07001940 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1941 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
1942 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
1943 layerInfo->set_flags(state.flags);
1944
1945 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1946 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
1947
Jorim Jaggi8e0af362017-11-14 16:28:28 +01001948 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07001949 if (parent != nullptr) {
1950 layerInfo->set_parent(parent->sequence);
1951 }
1952
1953 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1954 if (zOrderRelativeOf != nullptr) {
1955 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1956 }
1957
Chia-I Wu01591c92018-05-22 12:03:00 -07001958 // XXX getBE().compositionInfo.mBuffer is not protected
David Sodman0cc69182017-11-17 12:12:07 -08001959 auto buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001960 if (buffer != nullptr) {
1961 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
Peiyong Linefefaac2018-08-17 12:27:51 -07001962 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
Yichi Chen6ca35192018-05-29 12:20:43 +08001963 layerInfo->mutable_buffer_transform());
chaviw1d044282017-09-27 12:19:28 -07001964 }
1965
1966 layerInfo->set_queued_frames(getQueuedFrameCount());
1967 layerInfo->set_refresh_pending(isBufferLatched());
rongliucfb187b2018-03-14 12:26:23 -07001968 layerInfo->set_window_type(state.type);
1969 layerInfo->set_app_id(state.appId);
chaviwadc40c22018-07-10 16:57:27 -07001970 layerInfo->set_curr_frame(mCurrentFrameNumber);
1971
1972 for (const auto& pendingState : mPendingStates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001973 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
chaviwadc40c22018-07-10 16:57:27 -07001974 if (barrierLayer != nullptr) {
1975 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
1976 barrierLayerProto->set_id(barrierLayer->sequence);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001977 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
chaviwadc40c22018-07-10 16:57:27 -07001978 }
1979 }
chaviw1d044282017-09-27 12:19:28 -07001980}
1981
Dominik Laskowski7e045462018-05-30 13:02:02 -07001982void Layer::writeToProto(LayerProto* layerInfo, int32_t displayId) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07001983 if (!hasHwcLayer(displayId)) {
1984 return;
1985 }
1986
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001987 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
1988
Dominik Laskowski7e045462018-05-30 13:02:02 -07001989 const auto& hwcInfo = getBE().mHwcLayers.at(displayId);
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001990
1991 const Rect& frame = hwcInfo.displayFrame;
1992 LayerProtoHelper::writeToProto(frame, layerInfo->mutable_hwc_frame());
1993
1994 const FloatRect& crop = hwcInfo.sourceCrop;
1995 LayerProtoHelper::writeToProto(crop, layerInfo->mutable_hwc_crop());
1996
1997 const int32_t transform = static_cast<int32_t>(hwcInfo.transform);
1998 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08001999
2000 const int32_t compositionType = static_cast<int32_t>(hwcInfo.compositionType);
2001 layerInfo->set_hwc_composition_type(compositionType);
2002
2003 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
2004 static_cast<BufferLayer*>(this)->isProtected()) {
2005 layerInfo->set_is_protected(true);
2006 } else {
2007 layerInfo->set_is_protected(false);
2008 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002009}
2010
Mathias Agopian13127d82013-03-05 17:47:11 -08002011// ---------------------------------------------------------------------------
2012
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002013}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002014
2015#if defined(__gl_h_)
2016#error "don't include gl/gl.h in this file"
2017#endif
2018
2019#if defined(__gl2_h_)
2020#error "don't include gl2/gl2.h in this file"
2021#endif