blob: 2d3fd8ecbb315315fa119f15f306b71b35e6a3af [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
Yiwei Zhang5434a782018-12-05 18:06:32 -080028#include <android-base/stringprintf.h>
29
Mathias Agopiana67932f2011-04-20 14:20:59 -070030#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070031#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070032#include <cutils/properties.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033
34#include <utils/Errors.h>
35#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080036#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080038#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039
Courtney Goeltzenleuchter36c44dc2017-04-14 09:33:16 -060040#include <ui/DebugUtils.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070041#include <ui/GraphicBuffer.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042#include <ui/PixelFormat.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080043
Dan Stoza6b9454d2014-11-07 16:00:59 -080044#include <gui/BufferItem.h>
Kalle Raitaa099a242017-01-11 11:17:29 -080045#include <gui/LayerDebugInfo.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080046#include <gui/Surface.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047
Yiwei Zhang60d1a192018-03-07 14:52:28 -080048#include "BufferLayer.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020049#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070050#include "DisplayDevice.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051#include "Layer.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070052#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070053#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055
Mathias Agopian1b031492012-06-20 17:51:20 -070056#include "DisplayHardware/HWComposer.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080057#include "TimeStats/TimeStats.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070058
Peiyong Lincbc184f2018-08-22 13:24:10 -070059#include <renderengine/RenderEngine.h>
Mathias Agopian875d8e12013-06-07 15:35:48 -070060
Dan Stozac5da2712016-07-20 15:38:12 -070061#include <mutex>
chaviw1d044282017-09-27 12:19:28 -070062#include "LayerProtoHelper.h"
Dan Stozac5da2712016-07-20 15:38:12 -070063
David Sodman41fdfc92017-11-06 16:09:56 -080064#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066namespace android {
67
Yiwei Zhang5434a782018-12-05 18:06:32 -080068using base::StringAppendF;
69
Lloyd Piquef1c675b2018-09-12 20:45:39 -070070std::atomic<int32_t> Layer::sSequence{1};
Mathias Agopian13127d82013-03-05 17:47:11 -080071
Lloyd Pique42ab75e2018-09-12 20:46:03 -070072Layer::Layer(const LayerCreationArgs& args)
73 : mFlinger(args.flinger),
74 mName(args.name),
75 mClientRef(args.client),
76 mBE{this, args.name.string()} {
Mathias Agopiana67932f2011-04-20 14:20:59 -070077 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070078
79 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -070080 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
81 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
82 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070083
Dan Stozaf7ba41a2017-05-10 15:11:11 -070084 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070085
Ady Abraham83729882018-12-07 12:26:48 -080086 mState.current.active_legacy.w = args.w;
87 mState.current.active_legacy.h = args.h;
88 mState.current.flags = layerFlags;
89 mState.current.active_legacy.transform.set(0, 0);
90 mState.current.crop_legacy.makeInvalid();
91 mState.current.requestedCrop_legacy = mState.current.crop_legacy;
92 mState.current.z = 0;
93 mState.current.color.a = 1.0f;
94 mState.current.layerStack = 0;
95 mState.current.sequence = 0;
96 mState.current.requested_legacy = mState.current.active_legacy;
97 mState.current.appId = 0;
98 mState.current.type = 0;
99 mState.current.active.w = UINT32_MAX;
100 mState.current.active.h = UINT32_MAX;
101 mState.current.active.transform.set(0, 0);
102 mState.current.transform = 0;
103 mState.current.transformToDisplayInverse = false;
104 mState.current.crop.makeInvalid();
105 mState.current.acquireFence = new Fence(-1);
106 mState.current.dataspace = ui::Dataspace::UNKNOWN;
107 mState.current.hdrMetadata.validTypes = 0;
108 mState.current.surfaceDamageRegion.clear();
109 mState.current.cornerRadius = 0.0f;
110 mState.current.api = -1;
111 mState.current.hasColorTransform = false;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700112
113 // drawing state & current state are identical
Ady Abraham83729882018-12-07 12:26:48 -0800114 mState.drawing = mState.current;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700115
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800116 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700117 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800118 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200119 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700120
121 mFlinger->onLayerCreated();
Dan Stoza436ccf32018-06-21 12:10:12 -0700122}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700123
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700124Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800125 sp<Client> c(mClientRef.promote());
126 if (c != 0) {
127 c->detachLayer(this);
128 }
129
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000130 mFrameTracker.logAndResetStats(mName);
Robert Carr2e102c92018-10-23 12:11:15 -0700131
chaviw61626f22018-11-15 16:26:27 -0800132 destroyAllHwcLayersPlusChildren();
Robert Carr2e102c92018-10-23 12:11:15 -0700133
134 mFlinger->onLayerDestroyed();
Mathias Agopian96f08192010-06-02 23:28:45 -0700135}
136
Mathias Agopian13127d82013-03-05 17:47:11 -0800137// ---------------------------------------------------------------------------
138// callbacks
139// ---------------------------------------------------------------------------
140
David Sodmaneb085e02017-10-05 18:49:04 -0700141/*
142 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
143 * Layer. So, the implementation is done in BufferLayer. When called on a
144 * ColorLayer object, it's essentially a NOP.
145 */
David Sodmaneb085e02017-10-05 18:49:04 -0700146void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800147
Chia-I Wuc6657022017-08-15 11:18:17 -0700148void Layer::onRemovedFromCurrentState() {
Robert Carr2e102c92018-10-23 12:11:15 -0700149 mRemovedFromCurrentState = true;
150
Ady Abraham83729882018-12-07 12:26:48 -0800151 {
152 Mutex::Autolock lock(mStateMutex);
153 // the layer is removed from SF mState.current to mLayersPendingRemoval
154 if (mState.current.zOrderRelativeOf != nullptr) {
155 sp<Layer> strongRelative = mState.current.zOrderRelativeOf.promote();
156 if (strongRelative != nullptr) {
157 strongRelative->removeZOrderRelative(this);
158 mFlinger->setTransactionFlags(eTraversalNeeded);
159 }
160 mState.current.zOrderRelativeOf = nullptr;
Robert Carr5edb1ad2017-04-25 10:54:24 -0700161 }
Robert Carr5edb1ad2017-04-25 10:54:24 -0700162 }
Rob Carr4bba3702018-10-08 21:53:30 +0000163
Robert Carr2e102c92018-10-23 12:11:15 -0700164 // Since we are no longer reachable from CurrentState SurfaceFlinger
165 // will no longer invoke doTransaction for us, and so we will
166 // never finish applying transactions. We signal the sync point
167 // now so that another layer will not become indefinitely
168 // blocked.
169 for (auto& point: mRemoteSyncPoints) {
170 point->setTransactionApplied();
171 }
172 mRemoteSyncPoints.clear();
173
174 {
175 Mutex::Autolock syncLock(mLocalSyncPointMutex);
176 for (auto& point : mLocalSyncPoints) {
177 point->setFrameAvailable();
178 }
179 mLocalSyncPoints.clear();
180 }
181
Chia-I Wuc6657022017-08-15 11:18:17 -0700182 for (const auto& child : mCurrentChildren) {
183 child->onRemovedFromCurrentState();
184 }
185}
Chia-I Wu38512252017-05-17 14:36:16 -0700186
chaviw61626f22018-11-15 16:26:27 -0800187void Layer::addToCurrentState() {
188 mRemovedFromCurrentState = false;
189
190 for (const auto& child : mCurrentChildren) {
191 child->addToCurrentState();
192 }
193}
194
Mathias Agopian13127d82013-03-05 17:47:11 -0800195// ---------------------------------------------------------------------------
196// set-up
197// ---------------------------------------------------------------------------
198
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700199const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800200 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800201}
202
chaviw13fdc492017-06-27 12:40:18 -0700203bool Layer::getPremultipledAlpha() const {
204 return mPremultipliedAlpha;
205}
206
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700207sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800208 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700209 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800210}
211
212// ---------------------------------------------------------------------------
213// h/w composer set-up
214// ---------------------------------------------------------------------------
215
Dominik Laskowski075d3172018-05-24 15:50:06 -0700216bool Layer::createHwcLayer(HWComposer* hwc, DisplayId displayId) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700217 LOG_ALWAYS_FATAL_IF(hasHwcLayer(displayId), "Already have a layer for display %s",
218 to_string(displayId).c_str());
David Sodmanb8aaea12017-12-14 15:54:51 -0800219 auto layer = std::shared_ptr<HWC2::Layer>(
220 hwc->createLayer(displayId),
221 [hwc, displayId](HWC2::Layer* layer) {
222 hwc->destroyLayer(displayId, layer); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700223 if (!layer) {
224 return false;
225 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700226 LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers[displayId];
Steven Thomasb02664d2017-07-26 18:48:28 -0700227 hwcInfo.hwc = hwc;
228 hwcInfo.layer = layer;
David Sodmanf6a38932018-05-25 15:27:50 -0700229 layer->setLayerDestroyedListener(
Dominik Laskowski7e045462018-05-30 13:02:02 -0700230 [this, displayId](HWC2::Layer* /*layer*/) { getBE().mHwcLayers.erase(displayId); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700231 return true;
232}
233
Dominik Laskowski075d3172018-05-24 15:50:06 -0700234bool Layer::destroyHwcLayer(DisplayId displayId) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700235 if (!hasHwcLayer(displayId)) {
Chia-I Wu83806892017-11-16 10:50:20 -0800236 return false;
Steven Thomasb02664d2017-07-26 18:48:28 -0700237 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700238 auto& hwcInfo = getBE().mHwcLayers[displayId];
David Sodman41fdfc92017-11-06 16:09:56 -0800239 LOG_ALWAYS_FATAL_IF(hwcInfo.layer == nullptr, "Attempt to destroy null layer");
Steven Thomasb02664d2017-07-26 18:48:28 -0700240 LOG_ALWAYS_FATAL_IF(hwcInfo.hwc == nullptr, "Missing HWComposer");
David Sodmanb8aaea12017-12-14 15:54:51 -0800241 hwcInfo.layer = nullptr;
242
Chia-I Wu83806892017-11-16 10:50:20 -0800243 return true;
Steven Thomasb02664d2017-07-26 18:48:28 -0700244}
245
chaviw61626f22018-11-15 16:26:27 -0800246void Layer::destroyHwcLayersForAllDisplays() {
David Sodman6f65f3e2017-11-03 14:28:09 -0700247 size_t numLayers = getBE().mHwcLayers.size();
Steven Thomasb02664d2017-07-26 18:48:28 -0700248 for (size_t i = 0; i < numLayers; ++i) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700249 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.empty(), "destroyAllHwcLayers failed");
250 destroyHwcLayer(getBE().mHwcLayers.begin()->first);
Steven Thomasb02664d2017-07-26 18:48:28 -0700251 }
chaviw61626f22018-11-15 16:26:27 -0800252}
253
254void Layer::destroyAllHwcLayersPlusChildren() {
255 destroyHwcLayersForAllDisplays();
David Sodman6f65f3e2017-11-03 14:28:09 -0700256 LOG_ALWAYS_FATAL_IF(!getBE().mHwcLayers.empty(),
David Sodman41fdfc92017-11-06 16:09:56 -0800257 "All hardware composer layers should have been destroyed");
Robert Carr2e102c92018-10-23 12:11:15 -0700258
259 for (const sp<Layer>& child : mDrawingChildren) {
chaviw61626f22018-11-15 16:26:27 -0800260 child->destroyAllHwcLayersPlusChildren();
Robert Carr2e102c92018-10-23 12:11:15 -0700261 }
Steven Thomasb02664d2017-07-26 18:48:28 -0700262}
Steven Thomasb02664d2017-07-26 18:48:28 -0700263
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800264Rect Layer::getContentCrop() const {
265 // this is the crop rectangle that applies to the buffer
266 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700267 Rect crop;
268 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800269 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700270 crop = mCurrentCrop;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800271 } else if (getBE().compositionInfo.mBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800272 // otherwise we use the whole buffer
David Sodman0cc69182017-11-17 12:12:07 -0800273 crop = getBE().compositionInfo.mBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700274 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800275 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700276 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700277 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700278 return crop;
279}
280
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700281static Rect reduce(const Rect& win, const Region& exclude) {
282 if (CC_LIKELY(exclude.isEmpty())) {
283 return win;
284 }
285 if (exclude.isRect()) {
286 return win.reduce(exclude.getBounds());
287 }
288 return Region(win).subtract(exclude).getBounds();
289}
290
Dan Stoza80d61162017-12-20 15:57:52 -0800291static FloatRect reduce(const FloatRect& win, const Region& exclude) {
292 if (CC_LIKELY(exclude.isEmpty())) {
293 return win;
294 }
295 // Convert through Rect (by rounding) for lack of FloatRegion
296 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
297}
298
Vishnu Nair51625fa2018-12-06 13:54:33 -0800299Rect Layer::computeScreenBounds(bool reduceTransparentRegion) const {
Ady Abraham83729882018-12-07 12:26:48 -0800300 Mutex::Autolock lock(mStateMutex);
Vishnu Nair51625fa2018-12-06 13:54:33 -0800301 const State& s(getDrawingState());
302 Region transparentRegion = reduceTransparentRegion ? getActiveTransparentRegion(s) : Region();
Ady Abraham83729882018-12-07 12:26:48 -0800303 FloatRect bounds = computeBoundsLocked(transparentRegion);
304 ui::Transform t = getTransformLocked();
Vishnu Nair60356342018-11-13 13:00:45 -0800305 // Transform to screen space.
306 bounds = t.transform(bounds);
307 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700308}
309
Dan Stoza80d61162017-12-20 15:57:52 -0800310FloatRect Layer::computeBounds() const {
Ady Abraham83729882018-12-07 12:26:48 -0800311 Mutex::Autolock lock(mStateMutex);
312 return computeBoundsLocked();
313}
314
315FloatRect Layer::computeBoundsLocked() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000316 const State& s(getDrawingState());
Ady Abraham83729882018-12-07 12:26:48 -0800317 return computeBoundsLocked(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700318}
319
Dan Stoza80d61162017-12-20 15:57:52 -0800320FloatRect Layer::computeBounds(const Region& activeTransparentRegion) const {
Ady Abraham83729882018-12-07 12:26:48 -0800321 Mutex::Autolock lock(mStateMutex);
322 return computeBoundsLocked(activeTransparentRegion);
323}
324
325FloatRect Layer::computeBoundsLocked(const Region& activeTransparentRegion) const {
Alec Mourib416efd2018-09-06 21:01:59 +0000326 const State& s(getDrawingState());
Vishnu Nair60356342018-11-13 13:00:45 -0800327 Rect bounds = getCroppedBufferSize(s);
328 FloatRect floatBounds = bounds.toFloatRect();
329 if (bounds.isValid()) {
330 // Layer has bounds. Pass in our bounds as a special case. Then pass on to our parents so
331 // that they can clip it.
332 floatBounds = cropChildBounds(floatBounds);
333 } else {
334 // Layer does not have bounds, so we fill to our parent bounds. This is done by getting our
335 // parent bounds and inverting the transform to get the maximum bounds we can have that
336 // will fit within our parent bounds.
337 const auto& p = mDrawingParent.promote();
338 if (p != nullptr) {
339 ui::Transform t = s.active_legacy.transform;
340 // When calculating the parent bounds for purposes of clipping, we don't need to
341 // constrain the parent to its transparent region. The transparent region is an
342 // optimization based on the buffer contents of the layer, but does not affect the
343 // space allocated to it by policy, and thus children should be allowed to extend into
344 // the parent's transparent region.
345 // One of the main uses is a parent window with a child sitting behind the parent
346 // window, marked by a transparent region. When computing the parent bounds from the
347 // parent's perspective we pass in the transparent region to reduce buffer allocation
348 // size. When computing the parent bounds from the child's perspective, we pass in an
349 // empty transparent region in order to extend into the the parent bounds.
350 floatBounds = p->computeBounds(Region());
351 // Transform back to layer space.
352 floatBounds = t.inverse().transform(floatBounds);
353 }
354 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700355
Vishnu Nair60356342018-11-13 13:00:45 -0800356 // Subtract the transparent region and snap to the bounds.
357 return reduce(floatBounds, activeTransparentRegion);
358}
359
360FloatRect Layer::cropChildBounds(const FloatRect& childBounds) const {
361 const State& s(getDrawingState());
362 Rect bounds = getCroppedBufferSize(s);
363 FloatRect croppedBounds = childBounds;
364
365 // If the layer has bounds, then crop the passed in child bounds and pass
366 // it to our parents so they can crop it as well. If the layer has no bounds,
367 // then pass on the child bounds.
368 if (bounds.isValid()) {
369 croppedBounds = croppedBounds.intersect(bounds.toFloatRect());
Mathias Agopian13127d82013-03-05 17:47:11 -0800370 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700371
Chia-I Wue41dbe62017-06-13 14:10:56 -0700372 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700373 if (p != nullptr) {
Vishnu Nair60356342018-11-13 13:00:45 -0800374 // Transform to parent space and allow parent layer to crop the
375 // child bounds as well.
376 ui::Transform t = s.active_legacy.transform;
377 croppedBounds = t.transform(croppedBounds);
Ady Abraham83729882018-12-07 12:26:48 -0800378 Mutex::Autolock lock(p->mStateMutex);
Vishnu Nair60356342018-11-13 13:00:45 -0800379 croppedBounds = p->cropChildBounds(croppedBounds);
380 croppedBounds = t.inverse().transform(croppedBounds);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700381 }
Vishnu Nair60356342018-11-13 13:00:45 -0800382 return croppedBounds;
383}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700384
Vishnu Nair60356342018-11-13 13:00:45 -0800385Rect Layer::getCroppedBufferSize(const State& s) const {
386 Rect size = getBufferSize(s);
387 Rect crop = getCrop(s);
388 if (!crop.isEmpty() && size.isValid()) {
389 size.intersect(crop, &size);
390 } else if (!crop.isEmpty()) {
391 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700392 }
Vishnu Nair60356342018-11-13 13:00:45 -0800393 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800394}
395
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700396Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& display) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700397 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800398 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700399 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800400
401 // apply the projection's clipping to the window crop in
402 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700403 // if there are no window scaling involved, this operation will map to full
404 // pixels in the buffer.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700405
Ady Abraham83729882018-12-07 12:26:48 -0800406 FloatRect activeCropFloat = computeBoundsLocked();
407 ui::Transform t = getTransformLocked();
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800408 // Transform to screen space.
409 activeCropFloat = t.transform(activeCropFloat);
410 activeCropFloat = activeCropFloat.intersect(display->getViewport().toFloatRect());
411 // Back to layer space to work with the content crop.
412 activeCropFloat = t.inverse().transform(activeCropFloat);
413 // This needs to be here as transform.transform(Rect) computes the
414 // transformed rect and then takes the bounding box of the result before
415 // returning. This means
416 // transform.inverse().transform(transform.transform(Rect)) != Rect
417 // in which case we need to make sure the final rect is clipped to the
418 // display bounds.
419 Rect activeCrop{activeCropFloat};
420 if (!activeCrop.intersect(getBufferSize(s), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000421 activeCrop.clear();
422 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700423 return activeCrop;
424}
425
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700426void Layer::setupRoundedCornersCropCoordinates(Rect win,
427 const FloatRect& roundedCornersCrop) const {
428 // Translate win by the rounded corners rect coordinates, to have all values in
429 // layer coordinate space.
430 win.left -= roundedCornersCrop.left;
431 win.right -= roundedCornersCrop.left;
432 win.top -= roundedCornersCrop.top;
433 win.bottom -= roundedCornersCrop.top;
434
435 renderengine::Mesh::VertexArray<vec2> cropCoords(getBE().mMesh.getCropCoordArray<vec2>());
436 cropCoords[0] = vec2(win.left, win.top);
437 cropCoords[1] = vec2(win.left, win.top + win.getHeight());
438 cropCoords[2] = vec2(win.right, win.top + win.getHeight());
439 cropCoords[3] = vec2(win.right, win.top);
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700440}
441
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700442FloatRect Layer::computeCrop(const sp<const DisplayDevice>& display) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700443 // the content crop is the area of the content that gets scaled to the
444 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800445 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700446
447 // In addition there is a WM-specified crop we pull from our drawing state.
448 const State& s(getDrawingState());
449
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700450 Rect activeCrop = computeInitialCrop(display);
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800451 Rect bufferSize = getBufferSize(s);
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700452
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000453 // Transform the window crop to match the buffer coordinate system,
454 // which means using the inverse of the current transform set on the
455 // SurfaceFlingerConsumer.
456 uint32_t invTransform = mCurrentTransform;
Ady Abraham83729882018-12-07 12:26:48 -0800457 if (getTransformToDisplayInverseLocked()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000458 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700459 * the code below applies the primary display's inverse transform to the
460 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000461 */
David Sodman41fdfc92017-11-06 16:09:56 -0800462 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000463 // calculate the inverse transform
464 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800465 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800466 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000467 // and apply to the current transform
Peiyong Linefefaac2018-08-17 12:27:51 -0700468 invTransform = (ui::Transform(invTransformOrient) *
469 ui::Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800470 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000471
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800472 int winWidth = bufferSize.getWidth();
473 int winHeight = bufferSize.getHeight();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000474 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
475 // If the activeCrop has been rotate the ends are rotated but not
476 // the space itself so when transforming ends back we can't rely on
477 // a modification of the axes of rotation. To account for this we
478 // need to reorient the inverse rotation in terms of the current
479 // axes of rotation.
480 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
481 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
482 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800483 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000484 }
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800485 std::swap(winWidth, winHeight);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000486 }
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800487 const Rect winCrop =
488 activeCrop.transform(invTransform, bufferSize.getWidth(), bufferSize.getHeight());
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000489
490 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800491 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000492 float yScale = crop.getHeight() / float(winHeight);
493
David Sodman41fdfc92017-11-06 16:09:56 -0800494 float insetL = winCrop.left * xScale;
495 float insetT = winCrop.top * yScale;
496 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000497 float insetB = (winHeight - winCrop.bottom) * yScale;
498
David Sodman41fdfc92017-11-06 16:09:56 -0800499 crop.left += insetL;
500 crop.top += insetT;
501 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000502 crop.bottom -= insetB;
503
Mathias Agopian13127d82013-03-05 17:47:11 -0800504 return crop;
505}
506
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700507void Layer::setGeometry(const sp<const DisplayDevice>& display, uint32_t z) {
Ady Abraham83729882018-12-07 12:26:48 -0800508 Mutex::Autolock lock(mStateMutex);
Dominik Laskowski7e045462018-05-30 13:02:02 -0700509 const auto displayId = display->getId();
Dominik Laskowski075d3172018-05-24 15:50:06 -0700510 LOG_ALWAYS_FATAL_IF(!displayId);
Dominik Laskowski34157762018-10-31 13:07:19 -0700511 RETURN_IF_NO_HWC_LAYER(*displayId);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700512 auto& hwcInfo = getBE().mHwcLayers[*displayId];
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700513
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700514 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800515 hwcInfo.forceClientComposition = false;
516
Ady Abraham83729882018-12-07 12:26:48 -0800517 if (isSecureLocked() && !display->isSecure()) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800518 hwcInfo.forceClientComposition = true;
519 }
520
David Sodman15094112018-10-11 09:39:37 -0700521 auto& hwcLayer = hwcInfo.layer;
522
Mathias Agopian13127d82013-03-05 17:47:11 -0800523 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700524 const State& s(getDrawingState());
Vishnu Naira6733b32018-12-06 18:13:16 -0800525 const Rect bufferSize = getBufferSize(s);
David Revemanecf0fa52017-03-03 11:32:44 -0500526 auto blendMode = HWC2::BlendMode::None;
Ady Abraham83729882018-12-07 12:26:48 -0800527 if (!isOpaque(s) || getAlphaLocked() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800528 blendMode =
529 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800530 }
David Sodman15094112018-10-11 09:39:37 -0700531 auto error = hwcLayer->setBlendMode(blendMode);
532 ALOGE_IF(error != HWC2::Error::None,
533 "[%s] Failed to set blend mode %s:"
534 " %s (%d)",
535 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
536 static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700537 getBE().compositionInfo.hwc.blendMode = blendMode;
Mathias Agopian13127d82013-03-05 17:47:11 -0800538
539 // apply the layer's transform, followed by the display's global transform
540 // here we're guaranteed that the layer's transform preserves rects
Marissa Wall61c58622018-07-18 10:12:20 -0700541 Region activeTransparentRegion(getActiveTransparentRegion(s));
Ady Abraham83729882018-12-07 12:26:48 -0800542 ui::Transform t = getTransformLocked();
Marissa Wall61c58622018-07-18 10:12:20 -0700543 Rect activeCrop = getCrop(s);
Vishnu Naira6733b32018-12-06 18:13:16 -0800544 if (!activeCrop.isEmpty() && bufferSize.isValid()) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700545 activeCrop = t.transform(activeCrop);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700546 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000547 activeCrop.clear();
548 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700549 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800550 // This needs to be here as transform.transform(Rect) computes the
551 // transformed rect and then takes the bounding box of the result before
552 // returning. This means
553 // transform.inverse().transform(transform.transform(Rect)) != Rect
554 // in which case we need to make sure the final rect is clipped to the
555 // display bounds.
Vishnu Naira6733b32018-12-06 18:13:16 -0800556 if (!activeCrop.intersect(bufferSize, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000557 activeCrop.clear();
558 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700559 // mark regions outside the crop as transparent
Vishnu Naira6733b32018-12-06 18:13:16 -0800560 activeTransparentRegion.orSelf(Rect(0, 0, bufferSize.getWidth(), activeCrop.top));
561 activeTransparentRegion.orSelf(
562 Rect(0, activeCrop.bottom, bufferSize.getWidth(), bufferSize.getHeight()));
David Sodman41fdfc92017-11-06 16:09:56 -0800563 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
564 activeTransparentRegion.orSelf(
Vishnu Naira6733b32018-12-06 18:13:16 -0800565 Rect(activeCrop.right, activeCrop.top, bufferSize.getWidth(), activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700566 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700567
Dan Stoza80d61162017-12-20 15:57:52 -0800568 // computeBounds returns a FloatRect to provide more accuracy during the
569 // transformation. We then round upon constructing 'frame'.
Ady Abraham83729882018-12-07 12:26:48 -0800570 Rect frame{t.transform(computeBoundsLocked(activeTransparentRegion))};
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700571 if (!frame.intersect(display->getViewport(), &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000572 frame.clear();
573 }
Peiyong Linefefaac2018-08-17 12:27:51 -0700574 const ui::Transform& tr = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800575 Rect transformedFrame = tr.transform(frame);
David Sodman15094112018-10-11 09:39:37 -0700576 error = hwcLayer->setDisplayFrame(transformedFrame);
577 if (error != HWC2::Error::None) {
578 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
579 transformedFrame.left, transformedFrame.top, transformedFrame.right,
580 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
581 } else {
582 hwcInfo.displayFrame = transformedFrame;
583 }
David Sodmanba340492018-08-05 21:51:33 -0700584 getBE().compositionInfo.hwc.displayFrame = transformedFrame;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800585
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700586 FloatRect sourceCrop = computeCrop(display);
David Sodman15094112018-10-11 09:39:37 -0700587 error = hwcLayer->setSourceCrop(sourceCrop);
588 if (error != HWC2::Error::None) {
589 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
590 "%s (%d)",
591 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
592 to_string(error).c_str(), static_cast<int32_t>(error));
593 } else {
594 hwcInfo.sourceCrop = sourceCrop;
595 }
David Sodmanba340492018-08-05 21:51:33 -0700596 getBE().compositionInfo.hwc.sourceCrop = sourceCrop;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800597
Ady Abraham83729882018-12-07 12:26:48 -0800598 float alpha = static_cast<float>(getAlphaLocked());
David Sodman15094112018-10-11 09:39:37 -0700599 error = hwcLayer->setPlaneAlpha(alpha);
600 ALOGE_IF(error != HWC2::Error::None,
601 "[%s] Failed to set plane alpha %.3f: "
602 "%s (%d)",
603 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700604 getBE().compositionInfo.hwc.alpha = alpha;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800605
David Sodman15094112018-10-11 09:39:37 -0700606 error = hwcLayer->setZOrder(z);
607 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
608 to_string(error).c_str(), static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700609 getBE().compositionInfo.hwc.z = z;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500610
Albert Chaulk2a589632017-05-04 16:59:44 -0400611 int type = s.type;
612 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700613 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400614 if (parent.get()) {
Ady Abraham83729882018-12-07 12:26:48 -0800615 Mutex::Autolock lock(parent->mStateMutex);
Albert Chaulk2a589632017-05-04 16:59:44 -0400616 auto& parentState = parent->getDrawingState();
rongliucfb187b2018-03-14 12:26:23 -0700617 if (parentState.type >= 0 || parentState.appId >= 0) {
618 type = parentState.type;
619 appId = parentState.appId;
620 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400621 }
622
David Sodman15094112018-10-11 09:39:37 -0700623 error = hwcLayer->setInfo(type, appId);
624 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
625 static_cast<int32_t>(error));
626
David Sodmanba340492018-08-05 21:51:33 -0700627 getBE().compositionInfo.hwc.type = type;
628 getBE().compositionInfo.hwc.appId = appId;
629
Mathias Agopian29a367b2011-07-12 14:51:45 -0700630 /*
631 * Transformations are applied in this order:
632 * 1) buffer orientation/flip/mirror
633 * 2) state transformation (window manager)
634 * 3) layer orientation (screen orientation)
635 * (NOTE: the matrices are multiplied in reverse order)
636 */
637
Peiyong Linefefaac2018-08-17 12:27:51 -0700638 const ui::Transform bufferOrientation(mCurrentTransform);
639 ui::Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700640
Ady Abraham83729882018-12-07 12:26:48 -0800641 if (getTransformToDisplayInverseLocked()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700642 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700643 * the code below applies the primary display's inverse transform to the
644 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700645 */
David Sodman41fdfc92017-11-06 16:09:56 -0800646 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700647 // calculate the inverse transform
648 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800649 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700650 }
Robert Carrcae605c2017-03-29 12:10:31 -0700651
652 /*
653 * Here we cancel out the orientation component of the WM transform.
654 * The scaling and translate components are already included in our bounds
655 * computation so it's enough to just omit it in the composition.
656 * See comment in onDraw with ref to b/36727915 for why.
657 */
Peiyong Linefefaac2018-08-17 12:27:51 -0700658 transform = ui::Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700659 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700660
661 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800662 const uint32_t orientation = transform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -0700663 if (orientation & ui::Transform::ROT_INVALID) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800664 // we can only handle simple transformation
Lloyd Pique074e8122018-07-26 12:57:23 -0700665 hwcInfo.forceClientComposition = true;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700666 getBE().mHwcLayers[*displayId].compositionType = HWC2::Composition::Client;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800667 } else {
668 auto transform = static_cast<HWC2::Transform>(orientation);
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800669 hwcInfo.transform = transform;
David Sodman15094112018-10-11 09:39:37 -0700670 auto error = hwcLayer->setTransform(transform);
671 ALOGE_IF(error != HWC2::Error::None,
672 "[%s] Failed to set transform %s: "
673 "%s (%d)",
674 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
675 static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700676 getBE().compositionInfo.hwc.transform = transform;
David Sodman4b7c4bc2017-11-17 12:13:59 -0800677 }
678}
679
Dominik Laskowski075d3172018-05-24 15:50:06 -0700680void Layer::forceClientComposition(DisplayId displayId) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700681 RETURN_IF_NO_HWC_LAYER(displayId);
Dominik Laskowski7e045462018-05-30 13:02:02 -0700682 getBE().mHwcLayers[displayId].forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800683}
Dan Stozaee44edd2015-03-23 15:50:23 -0700684
Dominik Laskowski075d3172018-05-24 15:50:06 -0700685bool Layer::getForceClientComposition(DisplayId displayId) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700686 RETURN_IF_NO_HWC_LAYER(displayId, false);
Dominik Laskowski7e045462018-05-30 13:02:02 -0700687 return getBE().mHwcLayers[displayId].forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800688}
689
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700690void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Ady Abraham83729882018-12-07 12:26:48 -0800691 Mutex::Autolock lock(mStateMutex);
Dominik Laskowski7e045462018-05-30 13:02:02 -0700692 const auto displayId = display->getId();
Dominik Laskowski34157762018-10-31 13:07:19 -0700693 LOG_ALWAYS_FATAL_IF(!displayId);
694 if (!hasHwcLayer(*displayId) || getCompositionType(displayId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800695 return;
696 }
697
698 // This gives us only the "orientation" component of the transform
699 const State& s(getCurrentState());
700
701 // Apply the layer's transform, followed by the display's global transform
702 // Here we're guaranteed that the layer's transform preserves rects
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800703 Rect win = getCroppedBufferSize(s);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800704 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700705 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Ady Abraham83729882018-12-07 12:26:48 -0800706 Rect frame(getTransformLocked().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700707 frame.intersect(display->getViewport(), &frame);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700708 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800709 auto position = displayTransform.transform(frame);
710
Dominik Laskowski7e045462018-05-30 13:02:02 -0700711 auto error =
Dominik Laskowski075d3172018-05-24 15:50:06 -0700712 getBE().mHwcLayers[*displayId].layer->setCursorPosition(position.left, position.top);
713
David Sodman41fdfc92017-11-06 16:09:56 -0800714 ALOGE_IF(error != HWC2::Error::None,
715 "[%s] Failed to set cursor position "
716 "to (%d, %d): %s (%d)",
717 mName.string(), position.left, position.top, to_string(error).c_str(),
718 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800719}
Riley Andrews03414a12014-07-01 14:22:59 -0700720
Mathias Agopian13127d82013-03-05 17:47:11 -0800721// ---------------------------------------------------------------------------
722// drawing...
723// ---------------------------------------------------------------------------
724
Marissa Wall61c58622018-07-18 10:12:20 -0700725void Layer::draw(const RenderArea& renderArea, const Region& clip) {
chaviwa76b2712017-09-20 12:02:26 -0700726 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800727}
728
Marissa Wall61c58622018-07-18 10:12:20 -0700729void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) {
chaviwa76b2712017-09-20 12:02:26 -0700730 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800731}
732
David Sodman41fdfc92017-11-06 16:09:56 -0800733void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
734 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800735 auto& engine(mFlinger->getRenderEngine());
Ady Abraham83729882018-12-07 12:26:48 -0800736 Mutex::Autolock lock(mStateMutex);
David Sodman9eeae692017-11-02 10:53:32 -0700737 computeGeometry(renderArea, getBE().mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700738 engine.setupFillWithColor(red, green, blue, alpha);
David Sodman9eeae692017-11-02 10:53:32 -0700739 engine.drawMesh(getBE().mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800740}
741
David Sodmanc1498e62018-09-12 14:36:26 -0700742void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
743 clearWithOpenGL(renderArea, 0, 0, 0, 0);
744}
745
Dominik Laskowski075d3172018-05-24 15:50:06 -0700746void Layer::setCompositionType(DisplayId displayId, HWC2::Composition type, bool callIntoHwc) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700747 if (getBE().mHwcLayers.count(displayId) == 0) {
Chia-I Wu30505fb2018-03-26 16:20:31 -0700748 ALOGE("setCompositionType called without a valid HWC layer");
749 return;
David Sodman15094112018-10-11 09:39:37 -0700750 }
751 auto& hwcInfo = getBE().mHwcLayers[displayId];
752 auto& hwcLayer = hwcInfo.layer;
753 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", (hwcLayer)->getId(), to_string(type).c_str(),
754 static_cast<int>(callIntoHwc));
755 if (hwcInfo.compositionType != type) {
756 ALOGV(" actually setting");
757 hwcInfo.compositionType = type;
758 if (callIntoHwc) {
759 auto error = (hwcLayer)->setCompositionType(type);
760 ALOGE_IF(error != HWC2::Error::None,
761 "[%s] Failed to set "
762 "composition type %s: %s (%d)",
763 mName.string(), to_string(type).c_str(), to_string(error).c_str(),
764 static_cast<int32_t>(error));
Chia-I Wu30505fb2018-03-26 16:20:31 -0700765 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800766 }
767}
768
Dominik Laskowski075d3172018-05-24 15:50:06 -0700769HWC2::Composition Layer::getCompositionType(const std::optional<DisplayId>& displayId) const {
770 if (!displayId) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700771 // If we're querying the composition type for a display that does not
772 // have a HWC counterpart, then it will always be Client
773 return HWC2::Composition::Client;
774 }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700775 if (getBE().mHwcLayers.count(*displayId) == 0) {
776 ALOGE("getCompositionType called with an invalid HWC layer");
777 return HWC2::Composition::Invalid;
778 }
779 return getBE().mHwcLayers.at(*displayId).compositionType;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800780}
781
Dominik Laskowski075d3172018-05-24 15:50:06 -0700782void Layer::setClearClientTarget(DisplayId displayId, bool clear) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700783 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800784 ALOGE("setClearClientTarget called without a valid HWC layer");
785 return;
786 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700787 getBE().mHwcLayers[displayId].clearClientTarget = clear;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800788}
789
Dominik Laskowski075d3172018-05-24 15:50:06 -0700790bool Layer::getClearClientTarget(DisplayId displayId) const {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700791 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800792 ALOGE("getClearClientTarget called without a valid HWC layer");
793 return false;
794 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700795 return getBE().mHwcLayers.at(displayId).clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800796}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800797
Dan Stozacac35382016-01-27 12:21:06 -0800798bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
799 if (point->getFrameNumber() <= mCurrentFrameNumber) {
800 // Don't bother with a SyncPoint, since we've already latched the
801 // relevant frame
802 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700803 }
Robert Carr2e102c92018-10-23 12:11:15 -0700804 if (isRemovedFromCurrentState()) {
805 return false;
806 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700807
Dan Stozacac35382016-01-27 12:21:06 -0800808 Mutex::Autolock lock(mLocalSyncPointMutex);
809 mLocalSyncPoints.push_back(point);
810 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700811}
812
Mathias Agopian13127d82013-03-05 17:47:11 -0800813// ----------------------------------------------------------------------------
814// local state
815// ----------------------------------------------------------------------------
816
Peiyong Lin833074a2018-08-28 11:53:54 -0700817void Layer::computeGeometry(const RenderArea& renderArea,
818 renderengine::Mesh& mesh,
chaviwa76b2712017-09-20 12:02:26 -0700819 bool useIdentityTransform) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700820 const ui::Transform renderAreaTransform(renderArea.getTransform());
Ady Abraham83729882018-12-07 12:26:48 -0800821 FloatRect win = computeBoundsLocked();
Mathias Agopian3f844832013-08-07 21:24:32 -0700822
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000823 vec2 lt = vec2(win.left, win.top);
824 vec2 lb = vec2(win.left, win.bottom);
825 vec2 rb = vec2(win.right, win.bottom);
826 vec2 rt = vec2(win.right, win.top);
827
Ady Abraham83729882018-12-07 12:26:48 -0800828 ui::Transform layerTransform = getTransformLocked();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000829 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700830 lt = layerTransform.transform(lt);
831 lb = layerTransform.transform(lb);
832 rb = layerTransform.transform(rb);
833 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000834 }
835
Peiyong Lin833074a2018-08-28 11:53:54 -0700836 renderengine::Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700837 position[0] = renderAreaTransform.transform(lt);
838 position[1] = renderAreaTransform.transform(lb);
839 position[2] = renderAreaTransform.transform(rb);
840 position[3] = renderAreaTransform.transform(rt);
Mathias Agopian13127d82013-03-05 17:47:11 -0800841}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800842
David Sodman41fdfc92017-11-06 16:09:56 -0800843bool Layer::isSecure() const {
Ady Abraham83729882018-12-07 12:26:48 -0800844 Mutex::Autolock lock(mStateMutex);
845 return isSecureLocked();
846}
847
848bool Layer::isSecureLocked() const {
849 const State& s(mState.drawing);
Dan Stoza23116082015-06-18 14:58:39 -0700850 return (s.flags & layer_state_t::eLayerSecure);
851}
852
Mathias Agopian13127d82013-03-05 17:47:11 -0800853void Layer::setVisibleRegion(const Region& visibleRegion) {
854 // always called from main thread
855 this->visibleRegion = visibleRegion;
856}
857
858void Layer::setCoveredRegion(const Region& coveredRegion) {
859 // always called from main thread
860 this->coveredRegion = coveredRegion;
861}
862
David Sodman41fdfc92017-11-06 16:09:56 -0800863void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800864 // always called from main thread
865 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
866}
867
Robert Carre5f4f692018-01-12 13:12:28 -0800868void Layer::clearVisibilityRegions() {
869 visibleRegion.clear();
870 visibleNonTransparentRegion.clear();
871 coveredRegion.clear();
872}
873
Mathias Agopian13127d82013-03-05 17:47:11 -0800874// ----------------------------------------------------------------------------
875// transaction
876// ----------------------------------------------------------------------------
Dan Stoza7dde5992015-05-22 09:51:44 -0700877void Layer::pushPendingState() {
Ady Abraham83729882018-12-07 12:26:48 -0800878 Mutex::Autolock lock(mStateMutex);
879 pushPendingStateLocked();
880}
881
882void Layer::pushPendingStateLocked() {
883 if (!mState.current.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700884 return;
885 }
886
Dan Stoza7dde5992015-05-22 09:51:44 -0700887 // If this transaction is waiting on the receipt of a frame, generate a sync
888 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700889 // We don't allow installing sync points after we are removed from the current state
890 // as we won't be able to signal our end.
Ady Abraham83729882018-12-07 12:26:48 -0800891 if (mState.current.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
892 sp<Layer> barrierLayer = mState.current.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800893 if (barrierLayer == nullptr) {
894 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700895 // If we can't promote the layer we are intended to wait on,
896 // then it is expired or otherwise invalid. Allow this transaction
897 // to be applied as per normal (no synchronization).
Ady Abraham83729882018-12-07 12:26:48 -0800898 mState.current.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800899 } else {
Ady Abraham83729882018-12-07 12:26:48 -0800900 auto syncPoint = std::make_shared<SyncPoint>(mState.current.frameNumber_legacy);
Robert Carr0d480722017-01-10 16:42:54 -0800901 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800902 mRemoteSyncPoints.push_back(std::move(syncPoint));
903 } else {
904 // We already missed the frame we're supposed to synchronize
905 // on, so go ahead and apply the state update
Ady Abraham83729882018-12-07 12:26:48 -0800906 mState.current.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800907 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700908 }
909
Dan Stoza7dde5992015-05-22 09:51:44 -0700910 // Wake us up to check if the frame has been received
911 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700912 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700913 }
Ady Abraham83729882018-12-07 12:26:48 -0800914 mState.pending.push_back(mState.current);
915 ATRACE_INT(mTransactionName.string(), mState.pending.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700916}
917
Pablo Ceballos05289c22016-04-14 15:49:55 -0700918void Layer::popPendingState(State* stateToCommit) {
Ady Abraham83729882018-12-07 12:26:48 -0800919 *stateToCommit = mState.pending[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700920
Ady Abraham83729882018-12-07 12:26:48 -0800921 mState.pending.removeAt(0);
922 ATRACE_INT(mTransactionName.string(), mState.pending.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700923}
924
Pablo Ceballos05289c22016-04-14 15:49:55 -0700925bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700926 bool stateUpdateAvailable = false;
Ady Abraham83729882018-12-07 12:26:48 -0800927 while (!mState.pending.empty()) {
928 if (mState.pending[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700929 if (mRemoteSyncPoints.empty()) {
930 // If we don't have a sync point for this, apply it anyway. It
931 // will be visually wrong, but it should keep us from getting
932 // into too much trouble.
933 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700934 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700935 stateUpdateAvailable = true;
936 continue;
937 }
938
Marissa Wallf58c14b2018-07-24 10:50:43 -0700939 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Ady Abraham83729882018-12-07 12:26:48 -0800940 mState.pending[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800941 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800942
943 // Signal our end of the sync point and then dispose of it
944 mRemoteSyncPoints.front()->setTransactionApplied();
945 mRemoteSyncPoints.pop_front();
946 continue;
947 }
948
Dan Stoza7dde5992015-05-22 09:51:44 -0700949 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
950 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700951 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700952 stateUpdateAvailable = true;
953
954 // Signal our end of the sync point and then dispose of it
955 mRemoteSyncPoints.front()->setTransactionApplied();
956 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800957 } else {
958 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700959 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700960 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700961 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700962 stateUpdateAvailable = true;
963 }
964 }
965
966 // If we still have pending updates, wake SurfaceFlinger back up and point
967 // it at this layer so we can process them
Ady Abraham83729882018-12-07 12:26:48 -0800968 if (!mState.pending.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700969 setTransactionFlags(eTransactionNeeded);
970 mFlinger->setTransactionFlags(eTraversalNeeded);
971 }
972
Ady Abraham83729882018-12-07 12:26:48 -0800973 mState.current.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700974 return stateUpdateAvailable;
975}
976
Marissa Wall61c58622018-07-18 10:12:20 -0700977uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000978 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800979
Marissa Wall61c58622018-07-18 10:12:20 -0700980 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
981 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700982
David Sodmaneb085e02017-10-05 18:49:04 -0700983 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700984 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000985 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800986 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
987 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
988 " requested={ wh={%4u,%4u} }}\n"
989 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
990 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700991 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700992 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
993 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
994 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
995 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
996 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700997 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
998 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
999 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001000 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001001
Robert Carre392b552017-09-19 12:16:05 -07001002 // Don't let Layer::doTransaction update the drawing state
1003 // if we have a pending resize, unless we are in fixed-size mode.
1004 // the drawing state will be updated only once we receive a buffer
1005 // with the correct size.
1006 //
1007 // In particular, we want to make sure the clip (which is part
1008 // of the geometry state) is latched together with the size but is
1009 // latched immediately when no resizing is involved.
1010 //
1011 // If a sideband stream is attached, however, we want to skip this
1012 // optimization so that transactions aren't missed when a buffer
1013 // never arrives
1014 //
1015 // In the case that we don't have a buffer we ignore other factors
1016 // and avoid entering the resizePending state. At a high level the
1017 // resizePending state is to avoid applying the state of the new buffer
1018 // to the old buffer. However in the state where we don't have an old buffer
1019 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -07001020 const bool resizePending =
1021 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
1022 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
David Sodman0cc69182017-11-17 12:12:07 -08001023 (getBE().compositionInfo.mBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001024 if (!isFixedSize()) {
David Sodman0cc69182017-11-17 12:12:07 -08001025 if (resizePending && getBE().compositionInfo.hwc.sidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001026 flags |= eDontUpdateGeometryState;
1027 }
1028 }
1029
Robert Carr7bf247e2017-05-18 14:02:49 -07001030 // Here we apply various requested geometry states, depending on our
1031 // latching configuration. See Layer.h for a detailed discussion of
1032 // how geometry latching is controlled.
1033 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +00001034 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001035
1036 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1037 // mode, which causes attributes which normally latch regardless of scaling mode,
1038 // to be delayed. We copy the requested state to the active state making sure
1039 // to respect these rules (again see Layer.h for a detailed discussion).
1040 //
1041 // There is an awkward asymmetry in the handling of the crop states in the position
1042 // states, as can be seen below. Largely this arises from position and transform
1043 // being stored in the same data structure while having different latching rules.
1044 // b/38182305
1045 //
Marissa Wall61c58622018-07-18 10:12:20 -07001046 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -07001047 // applyPendingStates in the presence of deferred transactions.
1048 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -07001049 float tx = stateToCommit->active_legacy.transform.tx();
1050 float ty = stateToCommit->active_legacy.transform.ty();
1051 stateToCommit->active_legacy = stateToCommit->requested_legacy;
1052 stateToCommit->active_legacy.transform.set(tx, ty);
1053 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001054 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001055 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -07001056 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001057 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001058 }
1059
Marissa Wall61c58622018-07-18 10:12:20 -07001060 return flags;
1061}
1062
1063uint32_t Layer::doTransaction(uint32_t flags) {
1064 ATRACE_CALL();
1065
chaviw5aedec92018-10-22 10:40:38 -07001066 if (mLayerDetached) {
1067 return 0;
1068 }
1069
Ady Abraham83729882018-12-07 12:26:48 -08001070 Mutex::Autolock lock(mStateMutex);
1071 pushPendingStateLocked();
Alec Mourib416efd2018-09-06 21:01:59 +00001072 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -07001073 if (!applyPendingStates(&c)) {
1074 return 0;
1075 }
1076
1077 flags = doTransactionResize(flags, &c);
1078
Alec Mourib416efd2018-09-06 21:01:59 +00001079 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -07001080
1081 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001082 // invalidate and recompute the visible regions if needed
1083 flags |= Layer::eVisibleRegion;
1084 }
1085
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001086 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001087 // invalidate and recompute the visible regions if needed
1088 flags |= eVisibleRegion;
1089 this->contentDirty = true;
1090
1091 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -07001092 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -07001093 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -08001094 }
1095
Dan Stozac8145172016-04-28 16:29:06 -07001096 // If the layer is hidden, signal and clear out all local sync points so
1097 // that transactions for layers depending on this layer's frames becoming
1098 // visible are not blocked
1099 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001100 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001101 }
1102
Ady Abraham83729882018-12-07 12:26:48 -08001103 if (mState.current.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -07001104 flags |= eInputInfoChanged;
Ady Abraham83729882018-12-07 12:26:48 -08001105 mState.current.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -07001106 }
1107
Mathias Agopian13127d82013-03-05 17:47:11 -08001108 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001109 commitTransaction(c);
Ady Abraham83729882018-12-07 12:26:48 -08001110 mState.current.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -08001111 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001112}
1113
Pablo Ceballos05289c22016-04-14 15:49:55 -07001114void Layer::commitTransaction(const State& stateToCommit) {
Ady Abraham83729882018-12-07 12:26:48 -08001115 mState.drawing = stateToCommit;
1116}
1117
1118uint32_t Layer::getTransactionFlags() const {
1119 Mutex::Autolock lock(mStateMutex);
1120 return mState.transactionFlags;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001121}
1122
Mathias Agopian13127d82013-03-05 17:47:11 -08001123uint32_t Layer::getTransactionFlags(uint32_t flags) {
Ady Abraham83729882018-12-07 12:26:48 -08001124 Mutex::Autolock lock(mStateMutex);
1125 uint32_t and_flags = mState.transactionFlags & flags;
1126 mState.transactionFlags &= ~flags;
1127 return and_flags;
Mathias Agopian13127d82013-03-05 17:47:11 -08001128}
1129
1130uint32_t Layer::setTransactionFlags(uint32_t flags) {
Ady Abraham83729882018-12-07 12:26:48 -08001131 uint32_t old_flags = mState.transactionFlags;
1132 mState.transactionFlags |= flags;
1133 return old_flags;
Mathias Agopian13127d82013-03-05 17:47:11 -08001134}
1135
Robert Carr82364e32016-05-15 11:27:47 -07001136bool Layer::setPosition(float x, float y, bool immediate) {
Ady Abraham83729882018-12-07 12:26:48 -08001137 Mutex::Autolock lock(mStateMutex);
1138 if (mState.current.requested_legacy.transform.tx() == x &&
1139 mState.current.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001140 return false;
Ady Abraham83729882018-12-07 12:26:48 -08001141 mState.current.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001142
1143 // We update the requested and active position simultaneously because
1144 // we want to apply the position portion of the transform matrix immediately,
1145 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Ady Abraham83729882018-12-07 12:26:48 -08001146 mState.current.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001147 if (immediate && !mFreezeGeometryUpdates) {
1148 // Here we directly update the active state
1149 // unlike other setters, because we store it within
1150 // the transform, but use different latching rules.
1151 // b/38182305
Ady Abraham83729882018-12-07 12:26:48 -08001152 mState.current.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001153 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001154 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001155
Ady Abraham83729882018-12-07 12:26:48 -08001156 mState.current.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001157 setTransactionFlags(eTransactionNeeded);
1158 return true;
1159}
Robert Carr82364e32016-05-15 11:27:47 -07001160
Robert Carr1f0a16a2016-10-24 16:27:39 -07001161bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1162 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1163 if (idx < 0) {
1164 return false;
1165 }
1166 if (childLayer->setLayer(z)) {
1167 mCurrentChildren.removeAt(idx);
1168 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001169 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001170 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001171 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001172}
1173
Robert Carr503c7042017-09-27 15:06:08 -07001174bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1175 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1176 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1177 if (idx < 0) {
1178 return false;
1179 }
1180 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1181 mCurrentChildren.removeAt(idx);
1182 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001183 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001184 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001185 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001186}
1187
Robert Carrae060832016-11-28 10:51:00 -08001188bool Layer::setLayer(int32_t z) {
Ady Abraham83729882018-12-07 12:26:48 -08001189 Mutex::Autolock lock(mStateMutex);
1190 if (mState.current.z == z && !usingRelativeZLocked(LayerVector::StateSet::Current))
1191 return false;
1192
1193 mState.current.sequence++;
1194 mState.current.z = z;
1195 mState.current.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001196
1197 // Discard all relative layering.
Ady Abraham83729882018-12-07 12:26:48 -08001198 if (mState.current.zOrderRelativeOf != nullptr) {
1199 sp<Layer> strongRelative = mState.current.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -07001200 if (strongRelative != nullptr) {
1201 strongRelative->removeZOrderRelative(this);
1202 }
Ady Abraham83729882018-12-07 12:26:48 -08001203 mState.current.zOrderRelativeOf = nullptr;
Robert Carrdb66e622017-04-10 16:55:57 -07001204 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001205 setTransactionFlags(eTransactionNeeded);
1206 return true;
1207}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001208
Robert Carrdb66e622017-04-10 16:55:57 -07001209void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Ady Abraham83729882018-12-07 12:26:48 -08001210 Mutex::Autolock lock(mStateMutex);
1211 mState.current.zOrderRelatives.remove(relative);
1212 mState.current.sequence++;
1213 mState.current.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001214 setTransactionFlags(eTransactionNeeded);
1215}
1216
1217void Layer::addZOrderRelative(const wp<Layer>& relative) {
Ady Abraham83729882018-12-07 12:26:48 -08001218 Mutex::Autolock lock(mStateMutex);
1219 mState.current.zOrderRelatives.add(relative);
1220 mState.current.modified = true;
1221 mState.current.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -07001222 setTransactionFlags(eTransactionNeeded);
1223}
1224
Robert Carr503d2bd2017-12-04 15:49:47 -08001225bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Ady Abraham83729882018-12-07 12:26:48 -08001226 Mutex::Autolock lock(mStateMutex);
Robert Carrdb66e622017-04-10 16:55:57 -07001227 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1228 if (handle == nullptr) {
1229 return false;
1230 }
1231 sp<Layer> relative = handle->owner.promote();
1232 if (relative == nullptr) {
1233 return false;
1234 }
1235
Ady Abraham83729882018-12-07 12:26:48 -08001236 if (mState.current.z == relativeZ && usingRelativeZLocked(LayerVector::StateSet::Current) &&
1237 mState.current.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001238 return false;
1239 }
1240
Ady Abraham83729882018-12-07 12:26:48 -08001241 mState.current.sequence++;
1242 mState.current.modified = true;
1243 mState.current.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001244
Ady Abraham83729882018-12-07 12:26:48 -08001245 auto oldZOrderRelativeOf = mState.current.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -07001246 if (oldZOrderRelativeOf != nullptr) {
1247 oldZOrderRelativeOf->removeZOrderRelative(this);
1248 }
Ady Abraham83729882018-12-07 12:26:48 -08001249 mState.current.zOrderRelativeOf = relative;
Robert Carrdb66e622017-04-10 16:55:57 -07001250 relative->addZOrderRelative(this);
1251
1252 setTransactionFlags(eTransactionNeeded);
1253
1254 return true;
1255}
1256
Mathias Agopian13127d82013-03-05 17:47:11 -08001257bool Layer::setSize(uint32_t w, uint32_t h) {
Ady Abraham83729882018-12-07 12:26:48 -08001258 Mutex::Autolock lock(mStateMutex);
1259 if (mState.current.requested_legacy.w == w && mState.current.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001260 return false;
Ady Abraham83729882018-12-07 12:26:48 -08001261 mState.current.requested_legacy.w = w;
1262 mState.current.requested_legacy.h = h;
1263 mState.current.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001264 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001265
1266 // record the new size, from this point on, when the client request
1267 // a buffer, it'll get the new size.
Ady Abraham83729882018-12-07 12:26:48 -08001268 setDefaultBufferSize(mState.current.requested_legacy.w, mState.current.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001269 return true;
1270}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001271bool Layer::setAlpha(float alpha) {
Ady Abraham83729882018-12-07 12:26:48 -08001272 Mutex::Autolock lock(mStateMutex);
1273 if (mState.current.color.a == alpha) return false;
1274 mState.current.sequence++;
1275 mState.current.color.a = alpha;
1276 mState.current.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001277 setTransactionFlags(eTransactionNeeded);
1278 return true;
1279}
chaviw13fdc492017-06-27 12:40:18 -07001280
1281bool Layer::setColor(const half3& color) {
Ady Abraham83729882018-12-07 12:26:48 -08001282 Mutex::Autolock lock(mStateMutex);
1283 if (color.r == mState.current.color.r && color.g == mState.current.color.g &&
1284 color.b == mState.current.color.b)
chaviw13fdc492017-06-27 12:40:18 -07001285 return false;
1286
Ady Abraham83729882018-12-07 12:26:48 -08001287 mState.current.sequence++;
1288 mState.current.color.r = color.r;
1289 mState.current.color.g = color.g;
1290 mState.current.color.b = color.b;
1291 mState.current.modified = true;
chaviw13fdc492017-06-27 12:40:18 -07001292 setTransactionFlags(eTransactionNeeded);
1293 return true;
1294}
1295
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001296bool Layer::setCornerRadius(float cornerRadius) {
Ady Abraham83729882018-12-07 12:26:48 -08001297 Mutex::Autolock lock(mStateMutex);
1298 if (mState.current.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001299
Ady Abraham83729882018-12-07 12:26:48 -08001300 mState.current.sequence++;
1301 mState.current.cornerRadius = cornerRadius;
1302 mState.current.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001303 setTransactionFlags(eTransactionNeeded);
1304 return true;
1305}
1306
Robert Carrd4ae7f32018-06-07 16:10:57 -07001307bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1308 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001309 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001310 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1311
1312 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1313 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1314 return false;
1315 }
Ady Abraham83729882018-12-07 12:26:48 -08001316 Mutex::Autolock lock(mStateMutex);
1317 mState.current.sequence++;
1318 mState.current.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1319 matrix.dsdy);
1320 mState.current.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001321 setTransactionFlags(eTransactionNeeded);
1322 return true;
1323}
Marissa Wall61c58622018-07-18 10:12:20 -07001324
Mathias Agopian13127d82013-03-05 17:47:11 -08001325bool Layer::setTransparentRegionHint(const Region& transparent) {
Ady Abraham83729882018-12-07 12:26:48 -08001326 Mutex::Autolock lock(mStateMutex);
1327 mState.current.requestedTransparentRegion_legacy = transparent;
1328 mState.current.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001329 setTransactionFlags(eTransactionNeeded);
1330 return true;
1331}
1332bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Ady Abraham83729882018-12-07 12:26:48 -08001333 Mutex::Autolock lock(mStateMutex);
1334 const uint32_t newFlags = (mState.current.flags & ~mask) | (flags & mask);
1335 if (mState.current.flags == newFlags) return false;
1336 mState.current.sequence++;
1337 mState.current.flags = newFlags;
1338 mState.current.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001339 setTransactionFlags(eTransactionNeeded);
1340 return true;
1341}
Robert Carr99e27f02016-06-16 15:18:02 -07001342
Marissa Wallf58c14b2018-07-24 10:50:43 -07001343bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
Ady Abraham83729882018-12-07 12:26:48 -08001344 Mutex::Autolock lock(mStateMutex);
1345 if (mState.current.requestedCrop_legacy == crop) return false;
1346 mState.current.sequence++;
1347 mState.current.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001348 if (immediate && !mFreezeGeometryUpdates) {
Ady Abraham83729882018-12-07 12:26:48 -08001349 mState.current.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001350 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001351 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1352
Ady Abraham83729882018-12-07 12:26:48 -08001353 mState.current.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001354 setTransactionFlags(eTransactionNeeded);
1355 return true;
1356}
Robert Carr8d5227b2017-03-16 15:41:03 -07001357
Robert Carrc3574f72016-03-24 12:19:32 -07001358bool Layer::setOverrideScalingMode(int32_t scalingMode) {
Ady Abraham83729882018-12-07 12:26:48 -08001359 Mutex::Autolock lock(mStateMutex);
David Sodman41fdfc92017-11-06 16:09:56 -08001360 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001361 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001362 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001363 return true;
1364}
1365
rongliucfb187b2018-03-14 12:26:23 -07001366void Layer::setInfo(int32_t type, int32_t appId) {
Ady Abraham83729882018-12-07 12:26:48 -08001367 Mutex::Autolock lock(mStateMutex);
1368 mState.current.appId = appId;
1369 mState.current.type = type;
1370 mState.current.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001371 setTransactionFlags(eTransactionNeeded);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001372}
1373
Mathias Agopian13127d82013-03-05 17:47:11 -08001374bool Layer::setLayerStack(uint32_t layerStack) {
Ady Abraham83729882018-12-07 12:26:48 -08001375 Mutex::Autolock lock(mStateMutex);
1376 if (mState.current.layerStack == layerStack) return false;
1377 mState.current.sequence++;
1378 mState.current.layerStack = layerStack;
1379 mState.current.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001380 setTransactionFlags(eTransactionNeeded);
1381 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001382}
1383
Robert Carr1f0a16a2016-10-24 16:27:39 -07001384uint32_t Layer::getLayerStack() const {
Ady Abraham83729882018-12-07 12:26:48 -08001385 Mutex::Autolock lock(mStateMutex);
1386 return getLayerStackLocked();
1387}
1388
1389uint32_t Layer::getLayerStackLocked() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001390 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001391 if (p == nullptr) {
1392 return getDrawingState().layerStack;
1393 }
1394 return p->getLayerStack();
1395}
1396
Marissa Wallf58c14b2018-07-24 10:50:43 -07001397void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Ady Abraham83729882018-12-07 12:26:48 -08001398 Mutex::Autolock lock(mStateMutex);
1399 mState.current.barrierLayer_legacy = barrierLayer;
1400 mState.current.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001401 // We don't set eTransactionNeeded, because just receiving a deferral
1402 // request without any other state updates shouldn't actually induce a delay
Ady Abraham83729882018-12-07 12:26:48 -08001403 mState.current.modified = true;
1404 pushPendingStateLocked();
1405 mState.current.barrierLayer_legacy = nullptr;
1406 mState.current.frameNumber_legacy = 0;
1407 mState.current.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001408}
1409
Marissa Wallf58c14b2018-07-24 10:50:43 -07001410void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001411 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001412 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001413}
1414
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001415// ----------------------------------------------------------------------------
1416// pageflip handling...
1417// ----------------------------------------------------------------------------
1418
Robert Carr1f0a16a2016-10-24 16:27:39 -07001419bool Layer::isHiddenByPolicy() const {
Ady Abraham83729882018-12-07 12:26:48 -08001420 Mutex::Autolock lock(mStateMutex);
1421 const State& s(mState.drawing);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001422 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001423 if (parent != nullptr && parent->isHiddenByPolicy()) {
1424 return true;
1425 }
1426 return s.flags & layer_state_t::eLayerHidden;
1427}
1428
David Sodman41fdfc92017-11-06 16:09:56 -08001429uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001430 // TODO: should we do something special if mSecure is set?
1431 if (mProtectedByApp) {
1432 // need a hardware-protected path to external video sink
1433 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001434 }
Riley Andrews03414a12014-07-01 14:22:59 -07001435 if (mPotentialCursor) {
1436 usage |= GraphicBuffer::USAGE_CURSOR;
1437 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001438 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001439 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001440}
1441
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001442void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001443 uint32_t orientation = 0;
Vishnu Nairf2deb822018-11-12 17:53:48 -08001444 // Disable setting transform hint if the debug flag is set or if the
1445 // getTransformToDisplayInverse flag is set and the client wants to submit buffers
1446 // in one orientation.
1447 if (!mFlinger->mDebugDisableTransformHint && !getTransformToDisplayInverse()) {
Mathias Agopian84300952012-11-21 16:02:13 -08001448 // The transform hint is used to improve performance, but we can
1449 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001450 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001451 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001452 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001453 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001454 orientation = 0;
1455 }
1456 }
David Sodmaneb085e02017-10-05 18:49:04 -07001457 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001458}
1459
Mathias Agopian13127d82013-03-05 17:47:11 -08001460// ----------------------------------------------------------------------------
1461// debugging
1462// ----------------------------------------------------------------------------
1463
Marissa Wall61c58622018-07-18 10:12:20 -07001464// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001465LayerDebugInfo Layer::getLayerDebugInfo() const {
Ady Abraham83729882018-12-07 12:26:48 -08001466 Mutex::Autolock lock(mStateMutex);
Kalle Raitaa099a242017-01-11 11:17:29 -08001467 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001468 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001469 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001470 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001471 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
Yiwei Zhang5434a782018-12-05 18:06:32 -08001472 info.mType = std::string(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001473 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001474 info.mVisibleRegion = visibleRegion;
1475 info.mSurfaceDamageRegion = surfaceDamageRegion;
Ady Abraham83729882018-12-07 12:26:48 -08001476 info.mLayerStack = getLayerStackLocked();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001477 info.mX = ds.active_legacy.transform.tx();
1478 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001479 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001480 info.mWidth = ds.active_legacy.w;
1481 info.mHeight = ds.active_legacy.h;
1482 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001483 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001484 info.mFlags = ds.flags;
1485 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001486 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001487 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1488 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1489 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1490 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001491 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001492 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001493 if (buffer != 0) {
1494 info.mActiveBufferWidth = buffer->getWidth();
1495 info.mActiveBufferHeight = buffer->getHeight();
1496 info.mActiveBufferStride = buffer->getStride();
1497 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001498 } else {
1499 info.mActiveBufferWidth = 0;
1500 info.mActiveBufferHeight = 0;
1501 info.mActiveBufferStride = 0;
1502 info.mActiveBufferFormat = 0;
1503 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001504 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001505 info.mNumQueuedFrames = getQueuedFrameCount();
1506 info.mRefreshPending = isBufferLatched();
1507 info.mIsOpaque = isOpaque(ds);
1508 info.mContentDirty = contentDirty;
1509 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001510}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001511
Ady Abraham83729882018-12-07 12:26:48 -08001512std::tuple<uint32_t, int32_t> Layer::getLayerStackAndZ(StateSet stateSet) {
1513 Mutex::Autolock lock(mStateMutex);
1514 const State& state = (stateSet == StateSet::Current) ? mState.current : mState.drawing;
1515
1516 return {state.layerStack, state.z};
1517}
1518
Yiwei Zhang5434a782018-12-05 18:06:32 -08001519void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001520 result.append("-------------------------------");
1521 result.append("-------------------------------");
1522 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001523 result.append(" Layer name\n");
1524 result.append(" Z | ");
1525 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001526 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001527 result.append(" Disp Frame (LTRB) | ");
1528 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001529 result.append("-------------------------------");
1530 result.append("-------------------------------");
1531 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001532}
1533
Yiwei Zhang5434a782018-12-05 18:06:32 -08001534void Layer::miniDump(std::string& result, DisplayId displayId) const {
Ady Abraham83729882018-12-07 12:26:48 -08001535 Mutex::Autolock lock(mStateMutex);
Dominik Laskowski34157762018-10-31 13:07:19 -07001536 if (!hasHwcLayer(displayId)) {
Dan Stozae22aec72016-08-01 13:20:59 -07001537 return;
1538 }
1539
Yiwei Zhang5434a782018-12-05 18:06:32 -08001540 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001541 if (mName.length() > 77) {
1542 std::string shortened;
1543 shortened.append(mName.string(), 36);
1544 shortened.append("[...]");
1545 shortened.append(mName.string() + (mName.length() - 36), 36);
Yiwei Zhang5434a782018-12-05 18:06:32 -08001546 name = shortened;
Dan Stozae22aec72016-08-01 13:20:59 -07001547 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001548 name = std::string(mName.string(), mName.size());
Dan Stozae22aec72016-08-01 13:20:59 -07001549 }
1550
Yiwei Zhang5434a782018-12-05 18:06:32 -08001551 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001552
Alec Mourib416efd2018-09-06 21:01:59 +00001553 const State& layerState(getDrawingState());
Lloyd Pique074e8122018-07-26 12:57:23 -07001554 const LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers.at(displayId);
Chia-I Wu1e043612018-03-01 09:45:09 -08001555 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001556 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001557 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001558 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001559 }
Yiwei Zhang5434a782018-12-05 18:06:32 -08001560 StringAppendF(&result, "%10s | ", to_string(getCompositionType(displayId)).c_str());
1561 StringAppendF(&result, "%10s | ", to_string(hwcInfo.transform).c_str());
Lloyd Pique074e8122018-07-26 12:57:23 -07001562 const Rect& frame = hwcInfo.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001563 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique074e8122018-07-26 12:57:23 -07001564 const FloatRect& crop = hwcInfo.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001565 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1566 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001567
David Sodman7e4ae112018-02-09 15:02:28 -08001568 result.append("- - - - - - - - - - - - - - - -\n");
1569
1570 std::string compositionInfoStr;
1571 getBE().compositionInfo.dump(compositionInfoStr, "compositionInfo");
Yiwei Zhang5434a782018-12-05 18:06:32 -08001572 result.append(compositionInfoStr);
David Sodman7e4ae112018-02-09 15:02:28 -08001573
Yichi Chen6ca35192018-05-29 12:20:43 +08001574 result.append("- - - - - - - - - - - - - - - -");
1575 result.append("- - - - - - - - - - - - - - - -");
1576 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001577}
Dan Stozae22aec72016-08-01 13:20:59 -07001578
Yiwei Zhang5434a782018-12-05 18:06:32 -08001579void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001580 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001581}
1582
Svetoslavd85084b2014-03-20 10:28:31 -07001583void Layer::clearFrameStats() {
1584 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001585}
1586
Jamie Gennis6547ff42013-07-16 20:12:42 -07001587void Layer::logFrameStats() {
1588 mFrameTracker.logAndResetStats(mName);
1589}
1590
Svetoslavd85084b2014-03-20 10:28:31 -07001591void Layer::getFrameStats(FrameStats* outStats) const {
1592 mFrameTracker.getStats(outStats);
1593}
1594
Yiwei Zhang5434a782018-12-05 18:06:32 -08001595void Layer::dumpFrameEvents(std::string& result) {
1596 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001597 Mutex::Autolock lock(mFrameEventHistoryMutex);
1598 mFrameEventHistory.checkFencesForCompletion();
1599 mFrameEventHistory.dump(result);
1600}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001601
Brian Anderson5ea5e592016-12-01 16:54:33 -08001602void Layer::onDisconnect() {
1603 Mutex::Autolock lock(mFrameEventHistoryMutex);
1604 mFrameEventHistory.onDisconnect();
Yiwei Zhangaf8ee942018-11-22 00:15:23 -08001605 mFlinger->mTimeStats->onDestroy(getSequence());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001606}
1607
Brian Anderson3890c392016-07-25 12:48:08 -07001608void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001609 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001610 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001611 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1612 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001613 }
1614
Brian Andersond6927fb2016-07-23 23:37:30 -07001615 Mutex::Autolock lock(mFrameEventHistoryMutex);
1616 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001617 // If there are any unsignaled fences in the aquire timeline at this
1618 // point, the previously queued frame hasn't been latched yet. Go ahead
1619 // and try to get the signal time here so the syscall is taken out of
1620 // the main thread's critical path.
1621 mAcquireTimeline.updateSignalTimes();
1622 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001623 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001624 mFrameEventHistory.addQueue(*newTimestamps);
1625 }
1626
Brian Anderson3890c392016-07-25 12:48:08 -07001627 if (outDelta) {
1628 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001629 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001630}
Dan Stozae77c7662016-05-13 11:37:28 -07001631
Chia-I Wu98f1c102017-05-30 14:54:08 -07001632size_t Layer::getChildrenCount() const {
1633 size_t count = 0;
1634 for (const sp<Layer>& child : mCurrentChildren) {
1635 count += 1 + child->getChildrenCount();
1636 }
1637 return count;
1638}
1639
Robert Carr1f0a16a2016-10-24 16:27:39 -07001640void Layer::addChild(const sp<Layer>& layer) {
1641 mCurrentChildren.add(layer);
1642 layer->setParent(this);
1643}
1644
1645ssize_t Layer::removeChild(const sp<Layer>& layer) {
1646 layer->setParent(nullptr);
1647 return mCurrentChildren.remove(layer);
1648}
1649
Robert Carr1db73f62016-12-21 12:58:51 -08001650bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1651 sp<Handle> handle = nullptr;
1652 sp<Layer> newParent = nullptr;
1653 if (newParentHandle == nullptr) {
1654 return false;
1655 }
1656 handle = static_cast<Handle*>(newParentHandle.get());
1657 newParent = handle->owner.promote();
1658 if (newParent == nullptr) {
1659 ALOGE("Unable to promote Layer handle");
1660 return false;
1661 }
1662
chaviw5aedec92018-10-22 10:40:38 -07001663 if (attachChildren()) {
Ady Abraham83729882018-12-07 12:26:48 -08001664 Mutex::Autolock lock(mStateMutex);
chaviw5aedec92018-10-22 10:40:38 -07001665 setTransactionFlags(eTransactionNeeded);
1666 }
Robert Carr1db73f62016-12-21 12:58:51 -08001667 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001668 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001669 }
1670 mCurrentChildren.clear();
1671
1672 return true;
1673}
1674
Robert Carr15eae092018-03-23 13:43:53 -07001675void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001676 for (const sp<Layer>& child : mDrawingChildren) {
1677 child->mDrawingParent = newParent;
1678 }
1679}
1680
chaviwf1961f72017-09-18 16:41:07 -07001681bool Layer::reparent(const sp<IBinder>& newParentHandle) {
1682 if (newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001683 return false;
1684 }
1685
1686 auto handle = static_cast<Handle*>(newParentHandle.get());
1687 sp<Layer> newParent = handle->owner.promote();
1688 if (newParent == nullptr) {
1689 ALOGE("Unable to promote Layer handle");
1690 return false;
1691 }
1692
chaviwf1961f72017-09-18 16:41:07 -07001693 sp<Layer> parent = getParent();
1694 if (parent != nullptr) {
1695 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001696 }
chaviwf1961f72017-09-18 16:41:07 -07001697 newParent->addChild(this);
chaviw06178942017-07-27 10:25:59 -07001698
chaviw61626f22018-11-15 16:26:27 -08001699 if (!newParent->isRemovedFromCurrentState()) {
1700 addToCurrentState();
1701 }
1702
Ady Abraham83729882018-12-07 12:26:48 -08001703 Mutex::Autolock lock(mStateMutex);
chaviw5aedec92018-10-22 10:40:38 -07001704 if (mLayerDetached) {
1705 mLayerDetached = false;
1706 setTransactionFlags(eTransactionNeeded);
1707 }
1708 if (attachChildren()) {
1709 setTransactionFlags(eTransactionNeeded);
1710 }
chaviw06178942017-07-27 10:25:59 -07001711 return true;
1712}
1713
Robert Carr9524cb32017-02-13 11:32:32 -08001714bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001715 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001716 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001717 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001718 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001719 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001720 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001721 }
Robert Carr7f619b22017-11-06 12:56:35 -08001722 }
Robert Carr9524cb32017-02-13 11:32:32 -08001723
1724 return true;
1725}
1726
chaviw5aedec92018-10-22 10:40:38 -07001727bool Layer::attachChildren() {
1728 bool changed = false;
1729 for (const sp<Layer>& child : mCurrentChildren) {
1730 sp<Client> parentClient = mClientRef.promote();
1731 sp<Client> client(child->mClientRef.promote());
1732 if (client != nullptr && parentClient != client) {
1733 if (child->mLayerDetached) {
1734 child->mLayerDetached = false;
1735 changed = true;
1736 }
1737 changed |= child->attachChildren();
1738 }
1739 }
1740
1741 return changed;
1742}
1743
Peiyong Lind3788632018-09-18 16:01:31 -07001744bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001745 static const mat4 identityMatrix = mat4();
1746
Ady Abraham83729882018-12-07 12:26:48 -08001747 Mutex::Autolock lock(mStateMutex);
1748 if (mState.current.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001749 return false;
1750 }
Ady Abraham83729882018-12-07 12:26:48 -08001751 ++mState.current.sequence;
1752 mState.current.colorTransform = matrix;
1753 mState.current.hasColorTransform = matrix != identityMatrix;
1754 mState.current.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001755 setTransactionFlags(eTransactionNeeded);
1756 return true;
1757}
1758
chaviwf66724d2018-11-28 16:35:21 -08001759mat4 Layer::getColorTransform() const {
Ady Abraham83729882018-12-07 12:26:48 -08001760 Mutex::Autolock lock(mStateMutex);
1761 return getColorTransformLocked();
1762}
1763
1764mat4 Layer::getColorTransformLocked() const {
chaviwf66724d2018-11-28 16:35:21 -08001765 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1766 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1767 colorTransform = parent->getColorTransform() * colorTransform;
1768 }
1769 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001770}
1771
1772bool Layer::hasColorTransform() const {
Ady Abraham83729882018-12-07 12:26:48 -08001773 Mutex::Autolock lock(mStateMutex);
chaviwf66724d2018-11-28 16:35:21 -08001774 bool hasColorTransform = getDrawingState().hasColorTransform;
1775 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1776 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1777 }
1778 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001779}
1780
Chia-I Wu11481472018-05-04 10:43:19 -07001781bool Layer::isLegacyDataSpace() const {
1782 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001783 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001784 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001785}
1786
Robert Carr1f0a16a2016-10-24 16:27:39 -07001787void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001788 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001789}
1790
1791void Layer::clearSyncPoints() {
1792 for (const auto& child : mCurrentChildren) {
1793 child->clearSyncPoints();
1794 }
1795
1796 Mutex::Autolock lock(mLocalSyncPointMutex);
1797 for (auto& point : mLocalSyncPoints) {
1798 point->setFrameAvailable();
1799 }
1800 mLocalSyncPoints.clear();
1801}
1802
1803int32_t Layer::getZ() const {
Ady Abraham83729882018-12-07 12:26:48 -08001804 Mutex::Autolock lock(mStateMutex);
1805 return mState.drawing.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001806}
1807
Robert Carr29abff82017-12-04 13:51:20 -08001808bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
Ady Abraham83729882018-12-07 12:26:48 -08001809 Mutex::Autolock lock(mStateMutex);
1810 return usingRelativeZLocked(stateSet);
1811}
1812
1813bool Layer::usingRelativeZLocked(LayerVector::StateSet stateSet) {
Robert Carr29abff82017-12-04 13:51:20 -08001814 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Ady Abraham83729882018-12-07 12:26:48 -08001815 const State& state = useDrawing ? mState.drawing : mState.current;
Robert Carr29abff82017-12-04 13:51:20 -08001816 return state.zOrderRelativeOf != nullptr;
1817}
1818
David Sodman41fdfc92017-11-06 16:09:56 -08001819__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001820 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001821 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1822 "makeTraversalList received invalid stateSet");
1823 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1824 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Ady Abraham83729882018-12-07 12:26:48 -08001825 const State& state = useDrawing ? mState.drawing : mState.current;
Dan Stoza412903f2017-04-27 13:42:17 -07001826
Robert Carr29abff82017-12-04 13:51:20 -08001827 if (state.zOrderRelatives.size() == 0) {
1828 *outSkipRelativeZUsers = true;
1829 return children;
1830 }
1831
chaviwfd462612018-05-31 16:11:27 -07001832 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001833 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001834 sp<Layer> strongRelative = weakRelative.promote();
1835 if (strongRelative != nullptr) {
1836 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001837 }
1838 }
1839
Dan Stoza412903f2017-04-27 13:42:17 -07001840 for (const sp<Layer>& child : children) {
Ady Abraham83729882018-12-07 12:26:48 -08001841 const State& childState = useDrawing ? child->mState.drawing : child->mState.current;
Robert Carr503c7042017-09-27 15:06:08 -07001842 if (childState.zOrderRelativeOf != nullptr) {
1843 continue;
1844 }
Robert Carrdb66e622017-04-10 16:55:57 -07001845 traverse.add(child);
1846 }
1847
1848 return traverse;
1849}
1850
Robert Carr1f0a16a2016-10-24 16:27:39 -07001851/**
Robert Carrdb66e622017-04-10 16:55:57 -07001852 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001853 */
Dan Stoza412903f2017-04-27 13:42:17 -07001854void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001855 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1856 // produce a new list for traversing, including our relatives, and not including our children
1857 // who are relatives of another surface. In the case that there are no relative Z,
1858 // makeTraversalList returns our children directly to avoid significant overhead.
1859 // However in this case we need to take the responsibility for filtering children which
1860 // are relatives of another surface here.
1861 bool skipRelativeZUsers = false;
1862 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001863
Robert Carr1f0a16a2016-10-24 16:27:39 -07001864 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001865 for (; i < list.size(); i++) {
1866 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001867 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1868 continue;
1869 }
1870
Robert Carrdb66e622017-04-10 16:55:57 -07001871 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001872 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001873 }
Dan Stoza412903f2017-04-27 13:42:17 -07001874 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001875 }
Robert Carr29abff82017-12-04 13:51:20 -08001876
Dan Stoza412903f2017-04-27 13:42:17 -07001877 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001878 for (; i < list.size(); i++) {
1879 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001880 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1881 continue;
1882 }
Dan Stoza412903f2017-04-27 13:42:17 -07001883 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001884 }
1885}
1886
1887/**
Robert Carrdb66e622017-04-10 16:55:57 -07001888 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001889 */
Dan Stoza412903f2017-04-27 13:42:17 -07001890void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1891 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001892 // See traverseInZOrder for documentation.
1893 bool skipRelativeZUsers = false;
1894 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001895
Robert Carr1f0a16a2016-10-24 16:27:39 -07001896 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001897 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001898 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001899
1900 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1901 continue;
1902 }
1903
Robert Carrdb66e622017-04-10 16:55:57 -07001904 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001905 break;
1906 }
Dan Stoza412903f2017-04-27 13:42:17 -07001907 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001908 }
Dan Stoza412903f2017-04-27 13:42:17 -07001909 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001910 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001911 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001912
1913 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1914 continue;
1915 }
1916
Dan Stoza412903f2017-04-27 13:42:17 -07001917 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001918 }
1919}
1920
chaviw4b129c22018-04-09 16:19:43 -07001921LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1922 const std::vector<Layer*>& layersInTree) {
1923 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1924 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001925 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1926 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Ady Abraham83729882018-12-07 12:26:48 -08001927 const State& state = useDrawing ? mState.drawing : mState.current;
chaviw4b129c22018-04-09 16:19:43 -07001928
chaviwfd462612018-05-31 16:11:27 -07001929 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001930 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1931 sp<Layer> strongRelative = weakRelative.promote();
1932 // Only add relative layers that are also descendents of the top most parent of the tree.
1933 // If a relative layer is not a descendent, then it should be ignored.
1934 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1935 traverse.add(strongRelative);
1936 }
1937 }
1938
1939 for (const sp<Layer>& child : children) {
Ady Abraham83729882018-12-07 12:26:48 -08001940 const State& childState = useDrawing ? child->mState.drawing : child->mState.current;
chaviw4b129c22018-04-09 16:19:43 -07001941 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1942 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1943 // the child here since it won't be added later as a relative.
1944 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1945 childState.zOrderRelativeOf.promote().get())) {
1946 continue;
1947 }
1948 traverse.add(child);
1949 }
1950
1951 return traverse;
1952}
1953
1954void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1955 LayerVector::StateSet stateSet,
1956 const LayerVector::Visitor& visitor) {
1957 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001958
1959 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001960 for (; i < list.size(); i++) {
1961 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001962 if (relative->getZ() >= 0) {
1963 break;
1964 }
chaviw4b129c22018-04-09 16:19:43 -07001965 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001966 }
chaviw4b129c22018-04-09 16:19:43 -07001967
chaviwa76b2712017-09-20 12:02:26 -07001968 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001969 for (; i < list.size(); i++) {
1970 const auto& relative = list[i];
1971 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001972 }
1973}
1974
chaviw4b129c22018-04-09 16:19:43 -07001975std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1976 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1977 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1978
1979 std::vector<Layer*> layersInTree = {this};
1980 for (size_t i = 0; i < children.size(); i++) {
1981 const auto& child = children[i];
1982 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1983 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1984 }
1985
1986 return layersInTree;
1987}
1988
1989void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1990 const LayerVector::Visitor& visitor) {
1991 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1992 std::sort(layersInTree.begin(), layersInTree.end());
1993 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1994}
1995
Peiyong Linefefaac2018-08-17 12:27:51 -07001996ui::Transform Layer::getTransform() const {
Ady Abraham83729882018-12-07 12:26:48 -08001997 Mutex::Autolock lock(mStateMutex);
1998 return getTransformLocked();
1999}
2000
2001ui::Transform Layer::getTransformLocked() const {
Peiyong Linefefaac2018-08-17 12:27:51 -07002002 ui::Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07002003 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07002004 if (p != nullptr) {
Ady Abraham83729882018-12-07 12:26:48 -08002005 Mutex::Autolock lock(p->mStateMutex);
2006 t = p->getTransformLocked();
Robert Carr9b429f42017-04-17 14:56:57 -07002007
2008 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
2009 // it isFixedSize) then there may be additional scaling not accounted
2010 // for in the transform. We need to mirror this scaling in child surfaces
2011 // or we will break the contract where WM can treat child surfaces as
2012 // pixels in the parent surface.
David Sodman0cc69182017-11-17 12:12:07 -08002013 if (p->isFixedSize() && p->getBE().compositionInfo.mBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07002014 int bufferWidth;
2015 int bufferHeight;
2016 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
David Sodman0cc69182017-11-17 12:12:07 -08002017 bufferWidth = p->getBE().compositionInfo.mBuffer->getWidth();
2018 bufferHeight = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07002019 } else {
David Sodman0cc69182017-11-17 12:12:07 -08002020 bufferHeight = p->getBE().compositionInfo.mBuffer->getWidth();
2021 bufferWidth = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07002022 }
Marissa Wall61c58622018-07-18 10:12:20 -07002023 float sx = p->getActiveWidth(p->getDrawingState()) / static_cast<float>(bufferWidth);
2024 float sy = p->getActiveHeight(p->getDrawingState()) / static_cast<float>(bufferHeight);
Peiyong Linefefaac2018-08-17 12:27:51 -07002025 ui::Transform extraParentScaling;
Robert Carr9b429f42017-04-17 14:56:57 -07002026 extraParentScaling.set(sx, 0, 0, sy);
2027 t = t * extraParentScaling;
2028 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07002029 }
Marissa Wall61c58622018-07-18 10:12:20 -07002030 return t * getActiveTransform(getDrawingState());
Robert Carr1f0a16a2016-10-24 16:27:39 -07002031}
2032
chaviw13fdc492017-06-27 12:40:18 -07002033half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08002034 Mutex::Autolock lock(mStateMutex);
2035 return getAlphaLocked();
2036}
Robert Carr6452f122017-03-21 10:41:29 -07002037
Ady Abraham83729882018-12-07 12:26:48 -08002038half Layer::getAlphaLocked() const {
2039 const auto& p = mDrawingParent.promote();
chaviw13fdc492017-06-27 12:40:18 -07002040 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
2041 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07002042}
Robert Carr6452f122017-03-21 10:41:29 -07002043
chaviw13fdc492017-06-27 12:40:18 -07002044half4 Layer::getColor() const {
2045 const half4 color(getDrawingState().color);
Ady Abraham83729882018-12-07 12:26:48 -08002046 return half4(color.r, color.g, color.b, getAlphaLocked());
Robert Carr6452f122017-03-21 10:41:29 -07002047}
Robert Carr6452f122017-03-21 10:41:29 -07002048
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002049Layer::RoundedCornerState Layer::getRoundedCornerState() const {
Ady Abraham83729882018-12-07 12:26:48 -08002050 Mutex::Autolock lock(mStateMutex);
2051 return getRoundedCornerStateLocked();
2052}
2053
2054Layer::RoundedCornerState Layer::getRoundedCornerStateLocked() const {
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002055 const auto& p = mDrawingParent.promote();
2056 if (p != nullptr) {
2057 RoundedCornerState parentState = p->getRoundedCornerState();
2058 if (parentState.radius > 0) {
2059 ui::Transform t = getActiveTransform(getDrawingState());
2060 t = t.inverse();
2061 parentState.cropRect = t.transform(parentState.cropRect);
2062 // The rounded corners shader only accepts 1 corner radius for performance reasons,
2063 // but a transform matrix can define horizontal and vertical scales.
2064 // Let's take the average between both of them and pass into the shader, practically we
2065 // never do this type of transformation on windows anyway.
2066 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
2067 return parentState;
2068 }
2069 }
2070 const float radius = getDrawingState().cornerRadius;
Ady Abraham83729882018-12-07 12:26:48 -08002071 return radius > 0 ? RoundedCornerState(computeBoundsLocked(), radius) : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002072}
2073
Robert Carr1f0a16a2016-10-24 16:27:39 -07002074void Layer::commitChildList() {
2075 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
2076 const auto& child = mCurrentChildren[i];
2077 child->commitChildList();
2078 }
2079 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07002080 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002081}
2082
Robert Carr720e5062018-07-30 17:45:14 -07002083void Layer::setInputInfo(const InputWindowInfo& info) {
Ady Abraham83729882018-12-07 12:26:48 -08002084 Mutex::Autolock lock(mStateMutex);
2085 mState.current.inputInfo = info;
2086 mState.current.modified = true;
2087 mState.current.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07002088 setTransactionFlags(eTransactionNeeded);
2089}
2090
chaviw1d044282017-09-27 12:19:28 -07002091void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
Ady Abraham83729882018-12-07 12:26:48 -08002092 Mutex::Autolock lock(mStateMutex);
chaviw1d044282017-09-27 12:19:28 -07002093 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2094 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Ady Abraham83729882018-12-07 12:26:48 -08002095 const State& state = useDrawing ? mState.drawing : mState.current;
chaviw1d044282017-09-27 12:19:28 -07002096
Peiyong Linefefaac2018-08-17 12:27:51 -07002097 ui::Transform requestedTransform = state.active_legacy.transform;
Ady Abraham83729882018-12-07 12:26:48 -08002098 ui::Transform transform = getTransformLocked();
chaviw1d044282017-09-27 12:19:28 -07002099
2100 layerInfo->set_id(sequence);
2101 layerInfo->set_name(getName().c_str());
2102 layerInfo->set_type(String8(getTypeId()));
2103
2104 for (const auto& child : children) {
2105 layerInfo->add_children(child->sequence);
2106 }
2107
2108 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
2109 sp<Layer> strongRelative = weakRelative.promote();
2110 if (strongRelative != nullptr) {
2111 layerInfo->add_relatives(strongRelative->sequence);
2112 }
2113 }
2114
Marissa Wallf58c14b2018-07-24 10:50:43 -07002115 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
chaviw1d044282017-09-27 12:19:28 -07002116 layerInfo->mutable_transparent_region());
2117 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
2118 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
2119
Ady Abraham83729882018-12-07 12:26:48 -08002120 layerInfo->set_layer_stack(getLayerStackLocked());
chaviw1d044282017-09-27 12:19:28 -07002121 layerInfo->set_z(state.z);
2122
2123 PositionProto* position = layerInfo->mutable_position();
2124 position->set_x(transform.tx());
2125 position->set_y(transform.ty());
2126
2127 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
2128 requestedPosition->set_x(requestedTransform.tx());
2129 requestedPosition->set_y(requestedTransform.ty());
2130
2131 SizeProto* size = layerInfo->mutable_size();
Marissa Wallf58c14b2018-07-24 10:50:43 -07002132 size->set_w(state.active_legacy.w);
2133 size->set_h(state.active_legacy.h);
chaviw1d044282017-09-27 12:19:28 -07002134
Marissa Wallf58c14b2018-07-24 10:50:43 -07002135 LayerProtoHelper::writeToProto(state.crop_legacy, layerInfo->mutable_crop());
Ady Abraham83729882018-12-07 12:26:48 -08002136 layerInfo->set_corner_radius(getRoundedCornerStateLocked().radius);
chaviw1d044282017-09-27 12:19:28 -07002137
2138 layerInfo->set_is_opaque(isOpaque(state));
2139 layerInfo->set_invalidate(contentDirty);
Chia-I Wu01591c92018-05-22 12:03:00 -07002140
2141 // XXX (b/79210409) mCurrentDataSpace is not protected
2142 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
2143
chaviw1d044282017-09-27 12:19:28 -07002144 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
2145 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
2146 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
2147 layerInfo->set_flags(state.flags);
2148
2149 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
2150 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
2151
Jorim Jaggi8e0af362017-11-14 16:28:28 +01002152 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07002153 if (parent != nullptr) {
2154 layerInfo->set_parent(parent->sequence);
2155 }
2156
2157 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2158 if (zOrderRelativeOf != nullptr) {
2159 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
2160 }
2161
Chia-I Wu01591c92018-05-22 12:03:00 -07002162 // XXX getBE().compositionInfo.mBuffer is not protected
David Sodman0cc69182017-11-17 12:12:07 -08002163 auto buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08002164 if (buffer != nullptr) {
2165 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
Peiyong Linefefaac2018-08-17 12:27:51 -07002166 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
Yichi Chen6ca35192018-05-29 12:20:43 +08002167 layerInfo->mutable_buffer_transform());
chaviw1d044282017-09-27 12:19:28 -07002168 }
2169
2170 layerInfo->set_queued_frames(getQueuedFrameCount());
2171 layerInfo->set_refresh_pending(isBufferLatched());
rongliucfb187b2018-03-14 12:26:23 -07002172 layerInfo->set_window_type(state.type);
2173 layerInfo->set_app_id(state.appId);
chaviwadc40c22018-07-10 16:57:27 -07002174 layerInfo->set_curr_frame(mCurrentFrameNumber);
Vishnu Nairf2deb822018-11-12 17:53:48 -08002175 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
chaviwadc40c22018-07-10 16:57:27 -07002176
Ady Abraham83729882018-12-07 12:26:48 -08002177 for (const auto& pendingState : mState.pending) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07002178 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
chaviwadc40c22018-07-10 16:57:27 -07002179 if (barrierLayer != nullptr) {
2180 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
2181 barrierLayerProto->set_id(barrierLayer->sequence);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002182 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
chaviwadc40c22018-07-10 16:57:27 -07002183 }
2184 }
chaviw1d044282017-09-27 12:19:28 -07002185}
2186
Dominik Laskowski075d3172018-05-24 15:50:06 -07002187void Layer::writeToProto(LayerProto* layerInfo, DisplayId displayId) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07002188 if (!hasHwcLayer(displayId)) {
2189 return;
2190 }
2191
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002192 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
2193
Dominik Laskowski7e045462018-05-30 13:02:02 -07002194 const auto& hwcInfo = getBE().mHwcLayers.at(displayId);
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002195
2196 const Rect& frame = hwcInfo.displayFrame;
2197 LayerProtoHelper::writeToProto(frame, layerInfo->mutable_hwc_frame());
2198
2199 const FloatRect& crop = hwcInfo.sourceCrop;
2200 LayerProtoHelper::writeToProto(crop, layerInfo->mutable_hwc_crop());
2201
2202 const int32_t transform = static_cast<int32_t>(hwcInfo.transform);
2203 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002204
2205 const int32_t compositionType = static_cast<int32_t>(hwcInfo.compositionType);
2206 layerInfo->set_hwc_composition_type(compositionType);
2207
2208 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
2209 static_cast<BufferLayer*>(this)->isProtected()) {
2210 layerInfo->set_is_protected(true);
2211 } else {
2212 layerInfo->set_is_protected(false);
2213 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002214}
2215
Robert Carr2e102c92018-10-23 12:11:15 -07002216bool Layer::isRemovedFromCurrentState() const {
2217 return mRemovedFromCurrentState;
2218}
2219
Robert Carr720e5062018-07-30 17:45:14 -07002220InputWindowInfo Layer::fillInputInfo(const Rect& screenBounds) {
Ady Abraham83729882018-12-07 12:26:48 -08002221 InputWindowInfo info;
2222 ui::Transform t;
2223 Rect layerBounds;
2224 {
2225 Mutex::Autolock lock(mStateMutex);
2226 info = mState.drawing.inputInfo;
2227 t = getTransformLocked();
2228 const float xScale = t.sx();
2229 const float yScale = t.sy();
2230 if (xScale != 1.0f || yScale != 1.0f) {
2231 info.windowXScale *= 1.0f / xScale;
2232 info.windowYScale *= 1.0f / yScale;
2233 info.touchableRegion.scaleSelf(xScale, yScale);
2234 }
Robert Carr720e5062018-07-30 17:45:14 -07002235
Ady Abraham83729882018-12-07 12:26:48 -08002236 // Transform layer size to screen space and inset it by surface insets.
2237 layerBounds = getCroppedBufferSize(getDrawingState());
Riddle Hsu39d4aa52018-11-30 20:46:53 +08002238 }
Robert Carre07e1032018-11-26 12:55:53 -08002239
Vishnu Nair8033a492018-12-05 07:27:23 -08002240 layerBounds = t.transform(layerBounds);
2241 layerBounds.inset(info.surfaceInset, info.surfaceInset, info.surfaceInset, info.surfaceInset);
2242
2243 // Intersect with screen bounds to shrink the frame by the surface insets. The surface insets
2244 // are not set on the screen bounds directly since the surface inset region may already be
2245 // cropped by a parent layer.
2246 Rect frame;
2247 screenBounds.intersect(layerBounds, &frame);
2248
2249 info.frameLeft = frame.left;
2250 info.frameTop = frame.top;
2251 info.frameRight = frame.right;
2252 info.frameBottom = frame.bottom;
2253
2254 // Position the touchable region relative to frame screen location and restrict it to frame
2255 // bounds.
2256 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
Robert Carr720e5062018-07-30 17:45:14 -07002257 info.visible = isVisible();
2258 return info;
2259}
2260
2261bool Layer::hasInput() const {
Ady Abraham83729882018-12-07 12:26:48 -08002262 Mutex::Autolock lock(mStateMutex);
2263 return mState.drawing.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002264}
2265
Mathias Agopian13127d82013-03-05 17:47:11 -08002266// ---------------------------------------------------------------------------
2267
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002268}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002269
2270#if defined(__gl_h_)
2271#error "don't include gl/gl.h in this file"
2272#endif
2273
2274#if defined(__gl2_h_)
2275#error "don't include gl2/gl2.h in this file"
2276#endif