blob: 6faba524bc74862774a8bd92903c9286b7318cab [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
Lloyd Pique0449b0f2018-12-20 16:23:45 -080086 mCurrentState.active_legacy.w = args.w;
87 mCurrentState.active_legacy.h = args.h;
88 mCurrentState.flags = layerFlags;
89 mCurrentState.active_legacy.transform.set(0, 0);
90 mCurrentState.crop_legacy.makeInvalid();
91 mCurrentState.requestedCrop_legacy = mCurrentState.crop_legacy;
92 mCurrentState.z = 0;
93 mCurrentState.color.a = 1.0f;
94 mCurrentState.layerStack = 0;
95 mCurrentState.sequence = 0;
96 mCurrentState.requested_legacy = mCurrentState.active_legacy;
97 mCurrentState.appId = 0;
98 mCurrentState.type = 0;
99 mCurrentState.active.w = UINT32_MAX;
100 mCurrentState.active.h = UINT32_MAX;
101 mCurrentState.active.transform.set(0, 0);
102 mCurrentState.transform = 0;
103 mCurrentState.transformToDisplayInverse = false;
104 mCurrentState.crop.makeInvalid();
105 mCurrentState.acquireFence = new Fence(-1);
106 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
107 mCurrentState.hdrMetadata.validTypes = 0;
108 mCurrentState.surfaceDamageRegion.clear();
109 mCurrentState.cornerRadius = 0.0f;
110 mCurrentState.api = -1;
111 mCurrentState.hasColorTransform = false;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700112
113 // drawing state & current state are identical
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800114 mDrawingState = mCurrentState;
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
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800151 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
152 if (mCurrentState.zOrderRelativeOf != nullptr) {
153 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
154 if (strongRelative != nullptr) {
155 strongRelative->removeZOrderRelative(this);
156 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700157 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800158 mCurrentState.zOrderRelativeOf = nullptr;
Robert Carr5edb1ad2017-04-25 10:54:24 -0700159 }
Rob Carr4bba3702018-10-08 21:53:30 +0000160
Robert Carr2e102c92018-10-23 12:11:15 -0700161 // Since we are no longer reachable from CurrentState SurfaceFlinger
162 // will no longer invoke doTransaction for us, and so we will
163 // never finish applying transactions. We signal the sync point
164 // now so that another layer will not become indefinitely
165 // blocked.
166 for (auto& point: mRemoteSyncPoints) {
167 point->setTransactionApplied();
168 }
169 mRemoteSyncPoints.clear();
170
171 {
172 Mutex::Autolock syncLock(mLocalSyncPointMutex);
173 for (auto& point : mLocalSyncPoints) {
174 point->setFrameAvailable();
175 }
176 mLocalSyncPoints.clear();
177 }
178
Chia-I Wuc6657022017-08-15 11:18:17 -0700179 for (const auto& child : mCurrentChildren) {
180 child->onRemovedFromCurrentState();
181 }
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800182
183 mFlinger->markLayerPendingRemovalLocked(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700184}
Chia-I Wu38512252017-05-17 14:36:16 -0700185
chaviw61626f22018-11-15 16:26:27 -0800186void Layer::addToCurrentState() {
187 mRemovedFromCurrentState = false;
188
189 for (const auto& child : mCurrentChildren) {
190 child->addToCurrentState();
191 }
192}
193
Mathias Agopian13127d82013-03-05 17:47:11 -0800194// ---------------------------------------------------------------------------
195// set-up
196// ---------------------------------------------------------------------------
197
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700198const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800199 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800200}
201
chaviw13fdc492017-06-27 12:40:18 -0700202bool Layer::getPremultipledAlpha() const {
203 return mPremultipliedAlpha;
204}
205
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700206sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800207 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700208 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800209}
210
211// ---------------------------------------------------------------------------
212// h/w composer set-up
213// ---------------------------------------------------------------------------
214
Dominik Laskowski075d3172018-05-24 15:50:06 -0700215bool Layer::createHwcLayer(HWComposer* hwc, DisplayId displayId) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700216 LOG_ALWAYS_FATAL_IF(hasHwcLayer(displayId), "Already have a layer for display %s",
217 to_string(displayId).c_str());
David Sodmanb8aaea12017-12-14 15:54:51 -0800218 auto layer = std::shared_ptr<HWC2::Layer>(
219 hwc->createLayer(displayId),
220 [hwc, displayId](HWC2::Layer* layer) {
221 hwc->destroyLayer(displayId, layer); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700222 if (!layer) {
223 return false;
224 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700225 LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers[displayId];
Steven Thomasb02664d2017-07-26 18:48:28 -0700226 hwcInfo.hwc = hwc;
227 hwcInfo.layer = layer;
David Sodmanf6a38932018-05-25 15:27:50 -0700228 layer->setLayerDestroyedListener(
Dominik Laskowski7e045462018-05-30 13:02:02 -0700229 [this, displayId](HWC2::Layer* /*layer*/) { getBE().mHwcLayers.erase(displayId); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700230 return true;
231}
232
Dominik Laskowski075d3172018-05-24 15:50:06 -0700233bool Layer::destroyHwcLayer(DisplayId displayId) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700234 if (!hasHwcLayer(displayId)) {
Chia-I Wu83806892017-11-16 10:50:20 -0800235 return false;
Steven Thomasb02664d2017-07-26 18:48:28 -0700236 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700237 auto& hwcInfo = getBE().mHwcLayers[displayId];
David Sodman41fdfc92017-11-06 16:09:56 -0800238 LOG_ALWAYS_FATAL_IF(hwcInfo.layer == nullptr, "Attempt to destroy null layer");
Steven Thomasb02664d2017-07-26 18:48:28 -0700239 LOG_ALWAYS_FATAL_IF(hwcInfo.hwc == nullptr, "Missing HWComposer");
David Sodmanb8aaea12017-12-14 15:54:51 -0800240 hwcInfo.layer = nullptr;
241
Chia-I Wu83806892017-11-16 10:50:20 -0800242 return true;
Steven Thomasb02664d2017-07-26 18:48:28 -0700243}
244
chaviw61626f22018-11-15 16:26:27 -0800245void Layer::destroyHwcLayersForAllDisplays() {
David Sodman6f65f3e2017-11-03 14:28:09 -0700246 size_t numLayers = getBE().mHwcLayers.size();
Steven Thomasb02664d2017-07-26 18:48:28 -0700247 for (size_t i = 0; i < numLayers; ++i) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700248 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.empty(), "destroyAllHwcLayers failed");
249 destroyHwcLayer(getBE().mHwcLayers.begin()->first);
Steven Thomasb02664d2017-07-26 18:48:28 -0700250 }
chaviw61626f22018-11-15 16:26:27 -0800251}
252
253void Layer::destroyAllHwcLayersPlusChildren() {
254 destroyHwcLayersForAllDisplays();
David Sodman6f65f3e2017-11-03 14:28:09 -0700255 LOG_ALWAYS_FATAL_IF(!getBE().mHwcLayers.empty(),
David Sodman41fdfc92017-11-06 16:09:56 -0800256 "All hardware composer layers should have been destroyed");
Robert Carr2e102c92018-10-23 12:11:15 -0700257
258 for (const sp<Layer>& child : mDrawingChildren) {
chaviw61626f22018-11-15 16:26:27 -0800259 child->destroyAllHwcLayersPlusChildren();
Robert Carr2e102c92018-10-23 12:11:15 -0700260 }
Steven Thomasb02664d2017-07-26 18:48:28 -0700261}
Steven Thomasb02664d2017-07-26 18:48:28 -0700262
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800263Rect Layer::getContentCrop() const {
264 // this is the crop rectangle that applies to the buffer
265 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700266 Rect crop;
267 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800268 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700269 crop = mCurrentCrop;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800270 } else if (getBE().compositionInfo.mBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800271 // otherwise we use the whole buffer
David Sodman0cc69182017-11-17 12:12:07 -0800272 crop = getBE().compositionInfo.mBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700273 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800274 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700275 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700276 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700277 return crop;
278}
279
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700280static Rect reduce(const Rect& win, const Region& exclude) {
281 if (CC_LIKELY(exclude.isEmpty())) {
282 return win;
283 }
284 if (exclude.isRect()) {
285 return win.reduce(exclude.getBounds());
286 }
287 return Region(win).subtract(exclude).getBounds();
288}
289
Dan Stoza80d61162017-12-20 15:57:52 -0800290static FloatRect reduce(const FloatRect& win, const Region& exclude) {
291 if (CC_LIKELY(exclude.isEmpty())) {
292 return win;
293 }
294 // Convert through Rect (by rounding) for lack of FloatRegion
295 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
296}
297
Vishnu Nair51625fa2018-12-06 13:54:33 -0800298Rect Layer::computeScreenBounds(bool reduceTransparentRegion) const {
299 const State& s(getDrawingState());
300 Region transparentRegion = reduceTransparentRegion ? getActiveTransparentRegion(s) : Region();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800301 FloatRect bounds = computeBounds(transparentRegion);
302 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800303 // Transform to screen space.
304 bounds = t.transform(bounds);
305 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700306}
307
Dan Stoza80d61162017-12-20 15:57:52 -0800308FloatRect Layer::computeBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000309 const State& s(getDrawingState());
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800310 return computeBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700311}
312
Dan Stoza80d61162017-12-20 15:57:52 -0800313FloatRect Layer::computeBounds(const Region& activeTransparentRegion) const {
Alec Mourib416efd2018-09-06 21:01:59 +0000314 const State& s(getDrawingState());
Vishnu Nair60356342018-11-13 13:00:45 -0800315 Rect bounds = getCroppedBufferSize(s);
316 FloatRect floatBounds = bounds.toFloatRect();
317 if (bounds.isValid()) {
318 // Layer has bounds. Pass in our bounds as a special case. Then pass on to our parents so
319 // that they can clip it.
320 floatBounds = cropChildBounds(floatBounds);
321 } else {
322 // Layer does not have bounds, so we fill to our parent bounds. This is done by getting our
323 // parent bounds and inverting the transform to get the maximum bounds we can have that
324 // will fit within our parent bounds.
325 const auto& p = mDrawingParent.promote();
326 if (p != nullptr) {
327 ui::Transform t = s.active_legacy.transform;
328 // When calculating the parent bounds for purposes of clipping, we don't need to
329 // constrain the parent to its transparent region. The transparent region is an
330 // optimization based on the buffer contents of the layer, but does not affect the
331 // space allocated to it by policy, and thus children should be allowed to extend into
332 // the parent's transparent region.
333 // One of the main uses is a parent window with a child sitting behind the parent
334 // window, marked by a transparent region. When computing the parent bounds from the
335 // parent's perspective we pass in the transparent region to reduce buffer allocation
336 // size. When computing the parent bounds from the child's perspective, we pass in an
337 // empty transparent region in order to extend into the the parent bounds.
338 floatBounds = p->computeBounds(Region());
339 // Transform back to layer space.
340 floatBounds = t.inverse().transform(floatBounds);
341 }
342 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700343
Vishnu Nair60356342018-11-13 13:00:45 -0800344 // Subtract the transparent region and snap to the bounds.
345 return reduce(floatBounds, activeTransparentRegion);
346}
347
348FloatRect Layer::cropChildBounds(const FloatRect& childBounds) const {
349 const State& s(getDrawingState());
350 Rect bounds = getCroppedBufferSize(s);
351 FloatRect croppedBounds = childBounds;
352
353 // If the layer has bounds, then crop the passed in child bounds and pass
354 // it to our parents so they can crop it as well. If the layer has no bounds,
355 // then pass on the child bounds.
356 if (bounds.isValid()) {
357 croppedBounds = croppedBounds.intersect(bounds.toFloatRect());
Mathias Agopian13127d82013-03-05 17:47:11 -0800358 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700359
Chia-I Wue41dbe62017-06-13 14:10:56 -0700360 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700361 if (p != nullptr) {
Vishnu Nair60356342018-11-13 13:00:45 -0800362 // Transform to parent space and allow parent layer to crop the
363 // child bounds as well.
364 ui::Transform t = s.active_legacy.transform;
365 croppedBounds = t.transform(croppedBounds);
366 croppedBounds = p->cropChildBounds(croppedBounds);
367 croppedBounds = t.inverse().transform(croppedBounds);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700368 }
Vishnu Nair60356342018-11-13 13:00:45 -0800369 return croppedBounds;
370}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700371
Vishnu Nair60356342018-11-13 13:00:45 -0800372Rect Layer::getCroppedBufferSize(const State& s) const {
373 Rect size = getBufferSize(s);
374 Rect crop = getCrop(s);
375 if (!crop.isEmpty() && size.isValid()) {
376 size.intersect(crop, &size);
377 } else if (!crop.isEmpty()) {
378 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700379 }
Vishnu Nair60356342018-11-13 13:00:45 -0800380 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800381}
382
Alec Mourie0451f72019-01-07 19:42:02 -0800383Rect Layer::computeInitialCrop(const Rect& windowBounds) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700384 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800385 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700386 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800387
388 // apply the projection's clipping to the window crop in
389 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700390 // if there are no window scaling involved, this operation will map to full
391 // pixels in the buffer.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700392
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800393 FloatRect activeCropFloat = computeBounds();
Alec Mourie0451f72019-01-07 19:42:02 -0800394
395 // If we have valid window boundaries then we need to crop to the window
396 // boundaries in layer space.
397 if (windowBounds.isValid()) {
398 const ui::Transform t = getTransform();
399 // Transform to screen space.
400 activeCropFloat = t.transform(activeCropFloat);
401 activeCropFloat = activeCropFloat.intersect(windowBounds.toFloatRect());
402 // Back to layer space to work with the content crop.
403 activeCropFloat = t.inverse().transform(activeCropFloat);
404 }
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800405 // This needs to be here as transform.transform(Rect) computes the
406 // transformed rect and then takes the bounding box of the result before
407 // returning. This means
408 // transform.inverse().transform(transform.transform(Rect)) != Rect
409 // in which case we need to make sure the final rect is clipped to the
410 // display bounds.
411 Rect activeCrop{activeCropFloat};
412 if (!activeCrop.intersect(getBufferSize(s), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000413 activeCrop.clear();
414 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700415 return activeCrop;
416}
417
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700418void Layer::setupRoundedCornersCropCoordinates(Rect win,
419 const FloatRect& roundedCornersCrop) const {
420 // Translate win by the rounded corners rect coordinates, to have all values in
421 // layer coordinate space.
422 win.left -= roundedCornersCrop.left;
423 win.right -= roundedCornersCrop.left;
424 win.top -= roundedCornersCrop.top;
425 win.bottom -= roundedCornersCrop.top;
426
427 renderengine::Mesh::VertexArray<vec2> cropCoords(getBE().mMesh.getCropCoordArray<vec2>());
428 cropCoords[0] = vec2(win.left, win.top);
429 cropCoords[1] = vec2(win.left, win.top + win.getHeight());
430 cropCoords[2] = vec2(win.right, win.top + win.getHeight());
431 cropCoords[3] = vec2(win.right, win.top);
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700432}
433
Alec Mourie0451f72019-01-07 19:42:02 -0800434FloatRect Layer::computeCrop(const Rect& windowBounds) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700435 // the content crop is the area of the content that gets scaled to the
436 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800437 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700438
439 // In addition there is a WM-specified crop we pull from our drawing state.
440 const State& s(getDrawingState());
441
Alec Mourie0451f72019-01-07 19:42:02 -0800442 Rect activeCrop = computeInitialCrop(windowBounds);
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800443 Rect bufferSize = getBufferSize(s);
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700444
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000445 // Transform the window crop to match the buffer coordinate system,
446 // which means using the inverse of the current transform set on the
447 // SurfaceFlingerConsumer.
448 uint32_t invTransform = mCurrentTransform;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800449 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000450 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700451 * the code below applies the primary display's inverse transform to the
452 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000453 */
David Sodman41fdfc92017-11-06 16:09:56 -0800454 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000455 // calculate the inverse transform
456 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800457 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800458 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000459 // and apply to the current transform
Peiyong Linefefaac2018-08-17 12:27:51 -0700460 invTransform = (ui::Transform(invTransformOrient) *
461 ui::Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800462 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000463
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800464 int winWidth = bufferSize.getWidth();
465 int winHeight = bufferSize.getHeight();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000466 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
467 // If the activeCrop has been rotate the ends are rotated but not
468 // the space itself so when transforming ends back we can't rely on
469 // a modification of the axes of rotation. To account for this we
470 // need to reorient the inverse rotation in terms of the current
471 // axes of rotation.
472 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
473 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
474 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800475 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000476 }
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800477 std::swap(winWidth, winHeight);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000478 }
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800479 const Rect winCrop =
480 activeCrop.transform(invTransform, bufferSize.getWidth(), bufferSize.getHeight());
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000481
482 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800483 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000484 float yScale = crop.getHeight() / float(winHeight);
485
David Sodman41fdfc92017-11-06 16:09:56 -0800486 float insetL = winCrop.left * xScale;
487 float insetT = winCrop.top * yScale;
488 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000489 float insetB = (winHeight - winCrop.bottom) * yScale;
490
David Sodman41fdfc92017-11-06 16:09:56 -0800491 crop.left += insetL;
492 crop.top += insetT;
493 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000494 crop.bottom -= insetB;
495
Mathias Agopian13127d82013-03-05 17:47:11 -0800496 return crop;
497}
498
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700499void Layer::setGeometry(const sp<const DisplayDevice>& display, uint32_t z) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700500 const auto displayId = display->getId();
Dominik Laskowski075d3172018-05-24 15:50:06 -0700501 LOG_ALWAYS_FATAL_IF(!displayId);
Dominik Laskowski34157762018-10-31 13:07:19 -0700502 RETURN_IF_NO_HWC_LAYER(*displayId);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700503 auto& hwcInfo = getBE().mHwcLayers[*displayId];
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700504
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700505 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800506 hwcInfo.forceClientComposition = false;
507
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800508 if (isSecure() && !display->isSecure()) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800509 hwcInfo.forceClientComposition = true;
510 }
511
David Sodman15094112018-10-11 09:39:37 -0700512 auto& hwcLayer = hwcInfo.layer;
513
Mathias Agopian13127d82013-03-05 17:47:11 -0800514 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700515 const State& s(getDrawingState());
Vishnu Naira6733b32018-12-06 18:13:16 -0800516 const Rect bufferSize = getBufferSize(s);
David Revemanecf0fa52017-03-03 11:32:44 -0500517 auto blendMode = HWC2::BlendMode::None;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800518 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800519 blendMode =
520 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800521 }
David Sodman15094112018-10-11 09:39:37 -0700522 auto error = hwcLayer->setBlendMode(blendMode);
523 ALOGE_IF(error != HWC2::Error::None,
524 "[%s] Failed to set blend mode %s:"
525 " %s (%d)",
526 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
527 static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700528 getBE().compositionInfo.hwc.blendMode = blendMode;
Mathias Agopian13127d82013-03-05 17:47:11 -0800529
530 // apply the layer's transform, followed by the display's global transform
531 // here we're guaranteed that the layer's transform preserves rects
Marissa Wall61c58622018-07-18 10:12:20 -0700532 Region activeTransparentRegion(getActiveTransparentRegion(s));
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800533 ui::Transform t = getTransform();
Marissa Wall61c58622018-07-18 10:12:20 -0700534 Rect activeCrop = getCrop(s);
Vishnu Naira6733b32018-12-06 18:13:16 -0800535 if (!activeCrop.isEmpty() && bufferSize.isValid()) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700536 activeCrop = t.transform(activeCrop);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700537 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000538 activeCrop.clear();
539 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700540 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800541 // This needs to be here as transform.transform(Rect) computes the
542 // transformed rect and then takes the bounding box of the result before
543 // returning. This means
544 // transform.inverse().transform(transform.transform(Rect)) != Rect
545 // in which case we need to make sure the final rect is clipped to the
546 // display bounds.
Vishnu Naira6733b32018-12-06 18:13:16 -0800547 if (!activeCrop.intersect(bufferSize, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000548 activeCrop.clear();
549 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700550 // mark regions outside the crop as transparent
Vishnu Naira6733b32018-12-06 18:13:16 -0800551 activeTransparentRegion.orSelf(Rect(0, 0, bufferSize.getWidth(), activeCrop.top));
552 activeTransparentRegion.orSelf(
553 Rect(0, activeCrop.bottom, bufferSize.getWidth(), bufferSize.getHeight()));
David Sodman41fdfc92017-11-06 16:09:56 -0800554 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
555 activeTransparentRegion.orSelf(
Vishnu Naira6733b32018-12-06 18:13:16 -0800556 Rect(activeCrop.right, activeCrop.top, bufferSize.getWidth(), activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700557 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700558
Dan Stoza80d61162017-12-20 15:57:52 -0800559 // computeBounds returns a FloatRect to provide more accuracy during the
560 // transformation. We then round upon constructing 'frame'.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800561 Rect frame{t.transform(computeBounds(activeTransparentRegion))};
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700562 if (!frame.intersect(display->getViewport(), &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000563 frame.clear();
564 }
Peiyong Linefefaac2018-08-17 12:27:51 -0700565 const ui::Transform& tr = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800566 Rect transformedFrame = tr.transform(frame);
David Sodman15094112018-10-11 09:39:37 -0700567 error = hwcLayer->setDisplayFrame(transformedFrame);
568 if (error != HWC2::Error::None) {
569 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
570 transformedFrame.left, transformedFrame.top, transformedFrame.right,
571 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
572 } else {
573 hwcInfo.displayFrame = transformedFrame;
574 }
David Sodmanba340492018-08-05 21:51:33 -0700575 getBE().compositionInfo.hwc.displayFrame = transformedFrame;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800576
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700577 FloatRect sourceCrop = computeCrop(display);
David Sodman15094112018-10-11 09:39:37 -0700578 error = hwcLayer->setSourceCrop(sourceCrop);
579 if (error != HWC2::Error::None) {
580 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
581 "%s (%d)",
582 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
583 to_string(error).c_str(), static_cast<int32_t>(error));
584 } else {
585 hwcInfo.sourceCrop = sourceCrop;
586 }
David Sodmanba340492018-08-05 21:51:33 -0700587 getBE().compositionInfo.hwc.sourceCrop = sourceCrop;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800588
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800589 float alpha = static_cast<float>(getAlpha());
David Sodman15094112018-10-11 09:39:37 -0700590 error = hwcLayer->setPlaneAlpha(alpha);
591 ALOGE_IF(error != HWC2::Error::None,
592 "[%s] Failed to set plane alpha %.3f: "
593 "%s (%d)",
594 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700595 getBE().compositionInfo.hwc.alpha = alpha;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800596
David Sodman15094112018-10-11 09:39:37 -0700597 error = hwcLayer->setZOrder(z);
598 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
599 to_string(error).c_str(), static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700600 getBE().compositionInfo.hwc.z = z;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500601
Albert Chaulk2a589632017-05-04 16:59:44 -0400602 int type = s.type;
603 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700604 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400605 if (parent.get()) {
606 auto& parentState = parent->getDrawingState();
rongliucfb187b2018-03-14 12:26:23 -0700607 if (parentState.type >= 0 || parentState.appId >= 0) {
608 type = parentState.type;
609 appId = parentState.appId;
610 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400611 }
612
David Sodman15094112018-10-11 09:39:37 -0700613 error = hwcLayer->setInfo(type, appId);
614 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
615 static_cast<int32_t>(error));
616
David Sodmanba340492018-08-05 21:51:33 -0700617 getBE().compositionInfo.hwc.type = type;
618 getBE().compositionInfo.hwc.appId = appId;
619
Mathias Agopian29a367b2011-07-12 14:51:45 -0700620 /*
621 * Transformations are applied in this order:
622 * 1) buffer orientation/flip/mirror
623 * 2) state transformation (window manager)
624 * 3) layer orientation (screen orientation)
625 * (NOTE: the matrices are multiplied in reverse order)
626 */
627
Peiyong Linefefaac2018-08-17 12:27:51 -0700628 const ui::Transform bufferOrientation(mCurrentTransform);
629 ui::Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700630
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800631 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700632 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700633 * the code below applies the primary display's inverse transform to the
634 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700635 */
David Sodman41fdfc92017-11-06 16:09:56 -0800636 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700637 // calculate the inverse transform
638 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800639 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700640 }
Robert Carrcae605c2017-03-29 12:10:31 -0700641
642 /*
643 * Here we cancel out the orientation component of the WM transform.
644 * The scaling and translate components are already included in our bounds
645 * computation so it's enough to just omit it in the composition.
646 * See comment in onDraw with ref to b/36727915 for why.
647 */
Peiyong Linefefaac2018-08-17 12:27:51 -0700648 transform = ui::Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700649 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700650
651 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800652 const uint32_t orientation = transform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -0700653 if (orientation & ui::Transform::ROT_INVALID) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800654 // we can only handle simple transformation
Lloyd Pique074e8122018-07-26 12:57:23 -0700655 hwcInfo.forceClientComposition = true;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700656 getBE().mHwcLayers[*displayId].compositionType = HWC2::Composition::Client;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800657 } else {
658 auto transform = static_cast<HWC2::Transform>(orientation);
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800659 hwcInfo.transform = transform;
David Sodman15094112018-10-11 09:39:37 -0700660 auto error = hwcLayer->setTransform(transform);
661 ALOGE_IF(error != HWC2::Error::None,
662 "[%s] Failed to set transform %s: "
663 "%s (%d)",
664 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
665 static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700666 getBE().compositionInfo.hwc.transform = transform;
David Sodman4b7c4bc2017-11-17 12:13:59 -0800667 }
668}
669
Dominik Laskowski075d3172018-05-24 15:50:06 -0700670void Layer::forceClientComposition(DisplayId displayId) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700671 RETURN_IF_NO_HWC_LAYER(displayId);
Dominik Laskowski7e045462018-05-30 13:02:02 -0700672 getBE().mHwcLayers[displayId].forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800673}
Dan Stozaee44edd2015-03-23 15:50:23 -0700674
Dominik Laskowski075d3172018-05-24 15:50:06 -0700675bool Layer::getForceClientComposition(DisplayId displayId) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700676 RETURN_IF_NO_HWC_LAYER(displayId, false);
Dominik Laskowski7e045462018-05-30 13:02:02 -0700677 return getBE().mHwcLayers[displayId].forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800678}
679
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700680void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700681 const auto displayId = display->getId();
Dominik Laskowski34157762018-10-31 13:07:19 -0700682 LOG_ALWAYS_FATAL_IF(!displayId);
683 if (!hasHwcLayer(*displayId) || getCompositionType(displayId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800684 return;
685 }
686
687 // This gives us only the "orientation" component of the transform
688 const State& s(getCurrentState());
689
690 // Apply the layer's transform, followed by the display's global transform
691 // Here we're guaranteed that the layer's transform preserves rects
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800692 Rect win = getCroppedBufferSize(s);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800693 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700694 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800695 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700696 frame.intersect(display->getViewport(), &frame);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700697 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800698 auto position = displayTransform.transform(frame);
699
Dominik Laskowski7e045462018-05-30 13:02:02 -0700700 auto error =
Dominik Laskowski075d3172018-05-24 15:50:06 -0700701 getBE().mHwcLayers[*displayId].layer->setCursorPosition(position.left, position.top);
702
David Sodman41fdfc92017-11-06 16:09:56 -0800703 ALOGE_IF(error != HWC2::Error::None,
704 "[%s] Failed to set cursor position "
705 "to (%d, %d): %s (%d)",
706 mName.string(), position.left, position.top, to_string(error).c_str(),
707 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800708}
Riley Andrews03414a12014-07-01 14:22:59 -0700709
Mathias Agopian13127d82013-03-05 17:47:11 -0800710// ---------------------------------------------------------------------------
711// drawing...
712// ---------------------------------------------------------------------------
713
Marissa Wall61c58622018-07-18 10:12:20 -0700714void Layer::draw(const RenderArea& renderArea, const Region& clip) {
chaviwa76b2712017-09-20 12:02:26 -0700715 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800716}
717
Marissa Wall61c58622018-07-18 10:12:20 -0700718void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) {
chaviwa76b2712017-09-20 12:02:26 -0700719 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800720}
721
David Sodman41fdfc92017-11-06 16:09:56 -0800722void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
723 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800724 auto& engine(mFlinger->getRenderEngine());
David Sodman9eeae692017-11-02 10:53:32 -0700725 computeGeometry(renderArea, getBE().mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700726 engine.setupFillWithColor(red, green, blue, alpha);
David Sodman9eeae692017-11-02 10:53:32 -0700727 engine.drawMesh(getBE().mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800728}
729
David Sodmanc1498e62018-09-12 14:36:26 -0700730void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
731 clearWithOpenGL(renderArea, 0, 0, 0, 0);
732}
733
Dominik Laskowski075d3172018-05-24 15:50:06 -0700734void Layer::setCompositionType(DisplayId displayId, HWC2::Composition type, bool callIntoHwc) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700735 if (getBE().mHwcLayers.count(displayId) == 0) {
Chia-I Wu30505fb2018-03-26 16:20:31 -0700736 ALOGE("setCompositionType called without a valid HWC layer");
737 return;
David Sodman15094112018-10-11 09:39:37 -0700738 }
739 auto& hwcInfo = getBE().mHwcLayers[displayId];
740 auto& hwcLayer = hwcInfo.layer;
741 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", (hwcLayer)->getId(), to_string(type).c_str(),
742 static_cast<int>(callIntoHwc));
743 if (hwcInfo.compositionType != type) {
744 ALOGV(" actually setting");
745 hwcInfo.compositionType = type;
746 if (callIntoHwc) {
747 auto error = (hwcLayer)->setCompositionType(type);
748 ALOGE_IF(error != HWC2::Error::None,
749 "[%s] Failed to set "
750 "composition type %s: %s (%d)",
751 mName.string(), to_string(type).c_str(), to_string(error).c_str(),
752 static_cast<int32_t>(error));
Chia-I Wu30505fb2018-03-26 16:20:31 -0700753 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800754 }
755}
756
Dominik Laskowski075d3172018-05-24 15:50:06 -0700757HWC2::Composition Layer::getCompositionType(const std::optional<DisplayId>& displayId) const {
758 if (!displayId) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700759 // If we're querying the composition type for a display that does not
760 // have a HWC counterpart, then it will always be Client
761 return HWC2::Composition::Client;
762 }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700763 if (getBE().mHwcLayers.count(*displayId) == 0) {
764 ALOGE("getCompositionType called with an invalid HWC layer");
765 return HWC2::Composition::Invalid;
766 }
767 return getBE().mHwcLayers.at(*displayId).compositionType;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800768}
769
Dominik Laskowski075d3172018-05-24 15:50:06 -0700770void Layer::setClearClientTarget(DisplayId displayId, bool clear) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700771 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800772 ALOGE("setClearClientTarget called without a valid HWC layer");
773 return;
774 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700775 getBE().mHwcLayers[displayId].clearClientTarget = clear;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800776}
777
Dominik Laskowski075d3172018-05-24 15:50:06 -0700778bool Layer::getClearClientTarget(DisplayId displayId) const {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700779 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800780 ALOGE("getClearClientTarget called without a valid HWC layer");
781 return false;
782 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700783 return getBE().mHwcLayers.at(displayId).clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800784}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800785
Dan Stozacac35382016-01-27 12:21:06 -0800786bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
787 if (point->getFrameNumber() <= mCurrentFrameNumber) {
788 // Don't bother with a SyncPoint, since we've already latched the
789 // relevant frame
790 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700791 }
Robert Carr2e102c92018-10-23 12:11:15 -0700792 if (isRemovedFromCurrentState()) {
793 return false;
794 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700795
Dan Stozacac35382016-01-27 12:21:06 -0800796 Mutex::Autolock lock(mLocalSyncPointMutex);
797 mLocalSyncPoints.push_back(point);
798 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700799}
800
Mathias Agopian13127d82013-03-05 17:47:11 -0800801// ----------------------------------------------------------------------------
802// local state
803// ----------------------------------------------------------------------------
804
Peiyong Lin833074a2018-08-28 11:53:54 -0700805void Layer::computeGeometry(const RenderArea& renderArea,
806 renderengine::Mesh& mesh,
chaviwa76b2712017-09-20 12:02:26 -0700807 bool useIdentityTransform) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700808 const ui::Transform renderAreaTransform(renderArea.getTransform());
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800809 FloatRect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700810
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000811 vec2 lt = vec2(win.left, win.top);
812 vec2 lb = vec2(win.left, win.bottom);
813 vec2 rb = vec2(win.right, win.bottom);
814 vec2 rt = vec2(win.right, win.top);
815
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800816 ui::Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000817 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700818 lt = layerTransform.transform(lt);
819 lb = layerTransform.transform(lb);
820 rb = layerTransform.transform(rb);
821 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000822 }
823
Peiyong Lin833074a2018-08-28 11:53:54 -0700824 renderengine::Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700825 position[0] = renderAreaTransform.transform(lt);
826 position[1] = renderAreaTransform.transform(lb);
827 position[2] = renderAreaTransform.transform(rb);
828 position[3] = renderAreaTransform.transform(rt);
Mathias Agopian13127d82013-03-05 17:47:11 -0800829}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800830
David Sodman41fdfc92017-11-06 16:09:56 -0800831bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800832 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700833 return (s.flags & layer_state_t::eLayerSecure);
834}
835
Mathias Agopian13127d82013-03-05 17:47:11 -0800836void Layer::setVisibleRegion(const Region& visibleRegion) {
837 // always called from main thread
838 this->visibleRegion = visibleRegion;
839}
840
841void Layer::setCoveredRegion(const Region& coveredRegion) {
842 // always called from main thread
843 this->coveredRegion = coveredRegion;
844}
845
David Sodman41fdfc92017-11-06 16:09:56 -0800846void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800847 // always called from main thread
848 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
849}
850
Robert Carre5f4f692018-01-12 13:12:28 -0800851void Layer::clearVisibilityRegions() {
852 visibleRegion.clear();
853 visibleNonTransparentRegion.clear();
854 coveredRegion.clear();
855}
856
Mathias Agopian13127d82013-03-05 17:47:11 -0800857// ----------------------------------------------------------------------------
858// transaction
859// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800860
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800861void Layer::pushPendingState() {
862 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700863 return;
864 }
865
Dan Stoza7dde5992015-05-22 09:51:44 -0700866 // If this transaction is waiting on the receipt of a frame, generate a sync
867 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700868 // We don't allow installing sync points after we are removed from the current state
869 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800870 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
871 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800872 if (barrierLayer == nullptr) {
873 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700874 // If we can't promote the layer we are intended to wait on,
875 // then it is expired or otherwise invalid. Allow this transaction
876 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800877 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800878 } else {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800879 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy);
Robert Carr0d480722017-01-10 16:42:54 -0800880 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800881 mRemoteSyncPoints.push_back(std::move(syncPoint));
882 } else {
883 // We already missed the frame we're supposed to synchronize
884 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800885 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800886 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700887 }
888
Dan Stoza7dde5992015-05-22 09:51:44 -0700889 // Wake us up to check if the frame has been received
890 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700891 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700892 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800893 mPendingStates.push_back(mCurrentState);
894 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700895}
896
Pablo Ceballos05289c22016-04-14 15:49:55 -0700897void Layer::popPendingState(State* stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800898 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700899
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800900 mPendingStates.removeAt(0);
901 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700902}
903
Pablo Ceballos05289c22016-04-14 15:49:55 -0700904bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700905 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800906 while (!mPendingStates.empty()) {
907 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700908 if (mRemoteSyncPoints.empty()) {
909 // If we don't have a sync point for this, apply it anyway. It
910 // will be visually wrong, but it should keep us from getting
911 // into too much trouble.
912 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700913 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700914 stateUpdateAvailable = true;
915 continue;
916 }
917
Marissa Wallf58c14b2018-07-24 10:50:43 -0700918 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800919 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800920 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800921
922 // Signal our end of the sync point and then dispose of it
923 mRemoteSyncPoints.front()->setTransactionApplied();
924 mRemoteSyncPoints.pop_front();
925 continue;
926 }
927
Dan Stoza7dde5992015-05-22 09:51:44 -0700928 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
929 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700930 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700931 stateUpdateAvailable = true;
932
933 // Signal our end of the sync point and then dispose of it
934 mRemoteSyncPoints.front()->setTransactionApplied();
935 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800936 } else {
937 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700938 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700939 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700940 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700941 stateUpdateAvailable = true;
942 }
943 }
944
945 // If we still have pending updates, wake SurfaceFlinger back up and point
946 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800947 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700948 setTransactionFlags(eTransactionNeeded);
949 mFlinger->setTransactionFlags(eTraversalNeeded);
950 }
951
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800952 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700953 return stateUpdateAvailable;
954}
955
Marissa Wall61c58622018-07-18 10:12:20 -0700956uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000957 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800958
Marissa Wall61c58622018-07-18 10:12:20 -0700959 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
960 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700961
David Sodmaneb085e02017-10-05 18:49:04 -0700962 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700963 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000964 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800965 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
966 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
967 " requested={ wh={%4u,%4u} }}\n"
968 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
969 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700970 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700971 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
972 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
973 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
974 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
975 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700976 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
977 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
978 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800979 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700980
Robert Carre392b552017-09-19 12:16:05 -0700981 // Don't let Layer::doTransaction update the drawing state
982 // if we have a pending resize, unless we are in fixed-size mode.
983 // the drawing state will be updated only once we receive a buffer
984 // with the correct size.
985 //
986 // In particular, we want to make sure the clip (which is part
987 // of the geometry state) is latched together with the size but is
988 // latched immediately when no resizing is involved.
989 //
990 // If a sideband stream is attached, however, we want to skip this
991 // optimization so that transactions aren't missed when a buffer
992 // never arrives
993 //
994 // In the case that we don't have a buffer we ignore other factors
995 // and avoid entering the resizePending state. At a high level the
996 // resizePending state is to avoid applying the state of the new buffer
997 // to the old buffer. However in the state where we don't have an old buffer
998 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700999 const bool resizePending =
1000 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
1001 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
David Sodman0cc69182017-11-17 12:12:07 -08001002 (getBE().compositionInfo.mBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001003 if (!isFixedSize()) {
David Sodman0cc69182017-11-17 12:12:07 -08001004 if (resizePending && getBE().compositionInfo.hwc.sidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001005 flags |= eDontUpdateGeometryState;
1006 }
1007 }
1008
Robert Carr7bf247e2017-05-18 14:02:49 -07001009 // Here we apply various requested geometry states, depending on our
1010 // latching configuration. See Layer.h for a detailed discussion of
1011 // how geometry latching is controlled.
1012 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +00001013 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001014
1015 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1016 // mode, which causes attributes which normally latch regardless of scaling mode,
1017 // to be delayed. We copy the requested state to the active state making sure
1018 // to respect these rules (again see Layer.h for a detailed discussion).
1019 //
1020 // There is an awkward asymmetry in the handling of the crop states in the position
1021 // states, as can be seen below. Largely this arises from position and transform
1022 // being stored in the same data structure while having different latching rules.
1023 // b/38182305
1024 //
Marissa Wall61c58622018-07-18 10:12:20 -07001025 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -07001026 // applyPendingStates in the presence of deferred transactions.
1027 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -07001028 float tx = stateToCommit->active_legacy.transform.tx();
1029 float ty = stateToCommit->active_legacy.transform.ty();
1030 stateToCommit->active_legacy = stateToCommit->requested_legacy;
1031 stateToCommit->active_legacy.transform.set(tx, ty);
1032 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001033 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001034 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -07001035 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001036 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001037 }
1038
Marissa Wall61c58622018-07-18 10:12:20 -07001039 return flags;
1040}
1041
1042uint32_t Layer::doTransaction(uint32_t flags) {
1043 ATRACE_CALL();
1044
chaviw5aedec92018-10-22 10:40:38 -07001045 if (mLayerDetached) {
1046 return 0;
1047 }
1048
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001049 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +00001050 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -07001051 if (!applyPendingStates(&c)) {
1052 return 0;
1053 }
1054
1055 flags = doTransactionResize(flags, &c);
1056
Alec Mourib416efd2018-09-06 21:01:59 +00001057 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -07001058
1059 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001060 // invalidate and recompute the visible regions if needed
1061 flags |= Layer::eVisibleRegion;
1062 }
1063
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001064 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001065 // invalidate and recompute the visible regions if needed
1066 flags |= eVisibleRegion;
1067 this->contentDirty = true;
1068
1069 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -07001070 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -07001071 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -08001072 }
1073
Dan Stozac8145172016-04-28 16:29:06 -07001074 // If the layer is hidden, signal and clear out all local sync points so
1075 // that transactions for layers depending on this layer's frames becoming
1076 // visible are not blocked
1077 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001078 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001079 }
1080
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001081 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -07001082 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001083 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -07001084 }
1085
Mathias Agopian13127d82013-03-05 17:47:11 -08001086 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001087 commitTransaction(c);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001088 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -08001089 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001090}
1091
Pablo Ceballos05289c22016-04-14 15:49:55 -07001092void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001093 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001094}
1095
Mathias Agopian13127d82013-03-05 17:47:11 -08001096uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001097 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -08001098}
1099
1100uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001101 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -08001102}
1103
Robert Carr82364e32016-05-15 11:27:47 -07001104bool Layer::setPosition(float x, float y, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001105 if (mCurrentState.requested_legacy.transform.tx() == x &&
1106 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001107 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001108 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001109
1110 // We update the requested and active position simultaneously because
1111 // we want to apply the position portion of the transform matrix immediately,
1112 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001113 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001114 if (immediate && !mFreezeGeometryUpdates) {
1115 // Here we directly update the active state
1116 // unlike other setters, because we store it within
1117 // the transform, but use different latching rules.
1118 // b/38182305
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001119 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001120 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001121 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001122
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001123 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001124 setTransactionFlags(eTransactionNeeded);
1125 return true;
1126}
Robert Carr82364e32016-05-15 11:27:47 -07001127
Robert Carr1f0a16a2016-10-24 16:27:39 -07001128bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1129 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1130 if (idx < 0) {
1131 return false;
1132 }
1133 if (childLayer->setLayer(z)) {
1134 mCurrentChildren.removeAt(idx);
1135 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001136 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001137 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001138 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001139}
1140
Robert Carr503c7042017-09-27 15:06:08 -07001141bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1142 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1143 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1144 if (idx < 0) {
1145 return false;
1146 }
1147 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1148 mCurrentChildren.removeAt(idx);
1149 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001150 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001151 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001152 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001153}
1154
Robert Carrae060832016-11-28 10:51:00 -08001155bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001156 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
1157 mCurrentState.sequence++;
1158 mCurrentState.z = z;
1159 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001160
1161 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001162 if (mCurrentState.zOrderRelativeOf != nullptr) {
1163 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -07001164 if (strongRelative != nullptr) {
1165 strongRelative->removeZOrderRelative(this);
1166 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001167 mCurrentState.zOrderRelativeOf = nullptr;
Robert Carrdb66e622017-04-10 16:55:57 -07001168 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001169 setTransactionFlags(eTransactionNeeded);
1170 return true;
1171}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001172
Robert Carrdb66e622017-04-10 16:55:57 -07001173void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001174 mCurrentState.zOrderRelatives.remove(relative);
1175 mCurrentState.sequence++;
1176 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001177 setTransactionFlags(eTransactionNeeded);
1178}
1179
1180void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001181 mCurrentState.zOrderRelatives.add(relative);
1182 mCurrentState.modified = true;
1183 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -07001184 setTransactionFlags(eTransactionNeeded);
1185}
1186
Robert Carr503d2bd2017-12-04 15:49:47 -08001187bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001188 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1189 if (handle == nullptr) {
1190 return false;
1191 }
1192 sp<Layer> relative = handle->owner.promote();
1193 if (relative == nullptr) {
1194 return false;
1195 }
1196
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001197 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1198 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001199 return false;
1200 }
1201
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001202 mCurrentState.sequence++;
1203 mCurrentState.modified = true;
1204 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001205
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001206 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -07001207 if (oldZOrderRelativeOf != nullptr) {
1208 oldZOrderRelativeOf->removeZOrderRelative(this);
1209 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001210 mCurrentState.zOrderRelativeOf = relative;
Robert Carrdb66e622017-04-10 16:55:57 -07001211 relative->addZOrderRelative(this);
1212
1213 setTransactionFlags(eTransactionNeeded);
1214
1215 return true;
1216}
1217
Mathias Agopian13127d82013-03-05 17:47:11 -08001218bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001219 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001220 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001221 mCurrentState.requested_legacy.w = w;
1222 mCurrentState.requested_legacy.h = h;
1223 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001224 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001225
1226 // record the new size, from this point on, when the client request
1227 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001228 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001229 return true;
1230}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001231bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001232 if (mCurrentState.color.a == alpha) return false;
1233 mCurrentState.sequence++;
1234 mCurrentState.color.a = alpha;
1235 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001236 setTransactionFlags(eTransactionNeeded);
1237 return true;
1238}
chaviw13fdc492017-06-27 12:40:18 -07001239
1240bool Layer::setColor(const half3& color) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001241 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g &&
1242 color.b == mCurrentState.color.b)
chaviw13fdc492017-06-27 12:40:18 -07001243 return false;
1244
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001245 mCurrentState.sequence++;
1246 mCurrentState.color.r = color.r;
1247 mCurrentState.color.g = color.g;
1248 mCurrentState.color.b = color.b;
1249 mCurrentState.modified = true;
chaviw13fdc492017-06-27 12:40:18 -07001250 setTransactionFlags(eTransactionNeeded);
1251 return true;
1252}
1253
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001254bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001255 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001256
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001257 mCurrentState.sequence++;
1258 mCurrentState.cornerRadius = cornerRadius;
1259 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001260 setTransactionFlags(eTransactionNeeded);
1261 return true;
1262}
1263
Robert Carrd4ae7f32018-06-07 16:10:57 -07001264bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1265 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001266 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001267 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1268
1269 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1270 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1271 return false;
1272 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001273 mCurrentState.sequence++;
1274 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1275 matrix.dsdy);
1276 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001277 setTransactionFlags(eTransactionNeeded);
1278 return true;
1279}
Marissa Wall61c58622018-07-18 10:12:20 -07001280
Mathias Agopian13127d82013-03-05 17:47:11 -08001281bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001282 mCurrentState.requestedTransparentRegion_legacy = transparent;
1283 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001284 setTransactionFlags(eTransactionNeeded);
1285 return true;
1286}
1287bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001288 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1289 if (mCurrentState.flags == newFlags) return false;
1290 mCurrentState.sequence++;
1291 mCurrentState.flags = newFlags;
1292 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001293 setTransactionFlags(eTransactionNeeded);
1294 return true;
1295}
Robert Carr99e27f02016-06-16 15:18:02 -07001296
Marissa Wallf58c14b2018-07-24 10:50:43 -07001297bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001298 if (mCurrentState.requestedCrop_legacy == crop) return false;
1299 mCurrentState.sequence++;
1300 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001301 if (immediate && !mFreezeGeometryUpdates) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001302 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001303 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001304 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1305
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001306 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001307 setTransactionFlags(eTransactionNeeded);
1308 return true;
1309}
Robert Carr8d5227b2017-03-16 15:41:03 -07001310
Robert Carrc3574f72016-03-24 12:19:32 -07001311bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001312 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001313 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001314 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001315 return true;
1316}
1317
rongliucfb187b2018-03-14 12:26:23 -07001318void Layer::setInfo(int32_t type, int32_t appId) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001319 mCurrentState.appId = appId;
1320 mCurrentState.type = type;
1321 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001322 setTransactionFlags(eTransactionNeeded);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001323}
1324
Mathias Agopian13127d82013-03-05 17:47:11 -08001325bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001326 if (mCurrentState.layerStack == layerStack) return false;
1327 mCurrentState.sequence++;
1328 mCurrentState.layerStack = layerStack;
1329 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001330 setTransactionFlags(eTransactionNeeded);
1331 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001332}
1333
Robert Carr1f0a16a2016-10-24 16:27:39 -07001334uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001335 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001336 if (p == nullptr) {
1337 return getDrawingState().layerStack;
1338 }
1339 return p->getLayerStack();
1340}
1341
Marissa Wallf58c14b2018-07-24 10:50:43 -07001342void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001343 mCurrentState.barrierLayer_legacy = barrierLayer;
1344 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001345 // We don't set eTransactionNeeded, because just receiving a deferral
1346 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001347 mCurrentState.modified = true;
1348 pushPendingState();
1349 mCurrentState.barrierLayer_legacy = nullptr;
1350 mCurrentState.frameNumber_legacy = 0;
1351 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001352}
1353
Marissa Wallf58c14b2018-07-24 10:50:43 -07001354void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001355 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001356 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001357}
1358
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001359// ----------------------------------------------------------------------------
1360// pageflip handling...
1361// ----------------------------------------------------------------------------
1362
Robert Carr1f0a16a2016-10-24 16:27:39 -07001363bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001364 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001365 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001366 if (parent != nullptr && parent->isHiddenByPolicy()) {
1367 return true;
1368 }
1369 return s.flags & layer_state_t::eLayerHidden;
1370}
1371
David Sodman41fdfc92017-11-06 16:09:56 -08001372uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001373 // TODO: should we do something special if mSecure is set?
1374 if (mProtectedByApp) {
1375 // need a hardware-protected path to external video sink
1376 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001377 }
Riley Andrews03414a12014-07-01 14:22:59 -07001378 if (mPotentialCursor) {
1379 usage |= GraphicBuffer::USAGE_CURSOR;
1380 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001381 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001382 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001383}
1384
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001385void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001386 uint32_t orientation = 0;
Vishnu Nairf2deb822018-11-12 17:53:48 -08001387 // Disable setting transform hint if the debug flag is set or if the
1388 // getTransformToDisplayInverse flag is set and the client wants to submit buffers
1389 // in one orientation.
1390 if (!mFlinger->mDebugDisableTransformHint && !getTransformToDisplayInverse()) {
Mathias Agopian84300952012-11-21 16:02:13 -08001391 // The transform hint is used to improve performance, but we can
1392 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001393 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001394 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001395 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001396 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001397 orientation = 0;
1398 }
1399 }
David Sodmaneb085e02017-10-05 18:49:04 -07001400 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001401}
1402
Mathias Agopian13127d82013-03-05 17:47:11 -08001403// ----------------------------------------------------------------------------
1404// debugging
1405// ----------------------------------------------------------------------------
1406
Marissa Wall61c58622018-07-18 10:12:20 -07001407// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001408LayerDebugInfo Layer::getLayerDebugInfo() const {
1409 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001410 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001411 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001412 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001413 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
Yiwei Zhang5434a782018-12-05 18:06:32 -08001414 info.mType = std::string(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001415 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001416 info.mVisibleRegion = visibleRegion;
1417 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001418 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001419 info.mX = ds.active_legacy.transform.tx();
1420 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001421 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001422 info.mWidth = ds.active_legacy.w;
1423 info.mHeight = ds.active_legacy.h;
1424 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001425 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001426 info.mFlags = ds.flags;
1427 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001428 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001429 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1430 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1431 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1432 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001433 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001434 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001435 if (buffer != 0) {
1436 info.mActiveBufferWidth = buffer->getWidth();
1437 info.mActiveBufferHeight = buffer->getHeight();
1438 info.mActiveBufferStride = buffer->getStride();
1439 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001440 } else {
1441 info.mActiveBufferWidth = 0;
1442 info.mActiveBufferHeight = 0;
1443 info.mActiveBufferStride = 0;
1444 info.mActiveBufferFormat = 0;
1445 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001446 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001447 info.mNumQueuedFrames = getQueuedFrameCount();
1448 info.mRefreshPending = isBufferLatched();
1449 info.mIsOpaque = isOpaque(ds);
1450 info.mContentDirty = contentDirty;
1451 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001452}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001453
Yiwei Zhang5434a782018-12-05 18:06:32 -08001454void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001455 result.append("-------------------------------");
1456 result.append("-------------------------------");
1457 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001458 result.append(" Layer name\n");
1459 result.append(" Z | ");
1460 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001461 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001462 result.append(" Disp Frame (LTRB) | ");
1463 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001464 result.append("-------------------------------");
1465 result.append("-------------------------------");
1466 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001467}
1468
Yiwei Zhang5434a782018-12-05 18:06:32 -08001469void Layer::miniDump(std::string& result, DisplayId displayId) const {
Dominik Laskowski34157762018-10-31 13:07:19 -07001470 if (!hasHwcLayer(displayId)) {
Dan Stozae22aec72016-08-01 13:20:59 -07001471 return;
1472 }
1473
Yiwei Zhang5434a782018-12-05 18:06:32 -08001474 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001475 if (mName.length() > 77) {
1476 std::string shortened;
1477 shortened.append(mName.string(), 36);
1478 shortened.append("[...]");
1479 shortened.append(mName.string() + (mName.length() - 36), 36);
Yiwei Zhang5434a782018-12-05 18:06:32 -08001480 name = shortened;
Dan Stozae22aec72016-08-01 13:20:59 -07001481 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001482 name = std::string(mName.string(), mName.size());
Dan Stozae22aec72016-08-01 13:20:59 -07001483 }
1484
Yiwei Zhang5434a782018-12-05 18:06:32 -08001485 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001486
Alec Mourib416efd2018-09-06 21:01:59 +00001487 const State& layerState(getDrawingState());
Lloyd Pique074e8122018-07-26 12:57:23 -07001488 const LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers.at(displayId);
Chia-I Wu1e043612018-03-01 09:45:09 -08001489 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001490 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001491 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001492 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001493 }
Yiwei Zhang5434a782018-12-05 18:06:32 -08001494 StringAppendF(&result, "%10s | ", to_string(getCompositionType(displayId)).c_str());
1495 StringAppendF(&result, "%10s | ", to_string(hwcInfo.transform).c_str());
Lloyd Pique074e8122018-07-26 12:57:23 -07001496 const Rect& frame = hwcInfo.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001497 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique074e8122018-07-26 12:57:23 -07001498 const FloatRect& crop = hwcInfo.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001499 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1500 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001501
David Sodman7e4ae112018-02-09 15:02:28 -08001502 result.append("- - - - - - - - - - - - - - - -\n");
1503
1504 std::string compositionInfoStr;
1505 getBE().compositionInfo.dump(compositionInfoStr, "compositionInfo");
Yiwei Zhang5434a782018-12-05 18:06:32 -08001506 result.append(compositionInfoStr);
David Sodman7e4ae112018-02-09 15:02:28 -08001507
Yichi Chen6ca35192018-05-29 12:20:43 +08001508 result.append("- - - - - - - - - - - - - - - -");
1509 result.append("- - - - - - - - - - - - - - - -");
1510 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001511}
Dan Stozae22aec72016-08-01 13:20:59 -07001512
Yiwei Zhang5434a782018-12-05 18:06:32 -08001513void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001514 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001515}
1516
Svetoslavd85084b2014-03-20 10:28:31 -07001517void Layer::clearFrameStats() {
1518 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001519}
1520
Jamie Gennis6547ff42013-07-16 20:12:42 -07001521void Layer::logFrameStats() {
1522 mFrameTracker.logAndResetStats(mName);
1523}
1524
Svetoslavd85084b2014-03-20 10:28:31 -07001525void Layer::getFrameStats(FrameStats* outStats) const {
1526 mFrameTracker.getStats(outStats);
1527}
1528
Yiwei Zhang5434a782018-12-05 18:06:32 -08001529void Layer::dumpFrameEvents(std::string& result) {
1530 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001531 Mutex::Autolock lock(mFrameEventHistoryMutex);
1532 mFrameEventHistory.checkFencesForCompletion();
1533 mFrameEventHistory.dump(result);
1534}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001535
Brian Anderson5ea5e592016-12-01 16:54:33 -08001536void Layer::onDisconnect() {
1537 Mutex::Autolock lock(mFrameEventHistoryMutex);
1538 mFrameEventHistory.onDisconnect();
Yiwei Zhangaf8ee942018-11-22 00:15:23 -08001539 mFlinger->mTimeStats->onDestroy(getSequence());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001540}
1541
Brian Anderson3890c392016-07-25 12:48:08 -07001542void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001543 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001544 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001545 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1546 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001547 }
1548
Brian Andersond6927fb2016-07-23 23:37:30 -07001549 Mutex::Autolock lock(mFrameEventHistoryMutex);
1550 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001551 // If there are any unsignaled fences in the aquire timeline at this
1552 // point, the previously queued frame hasn't been latched yet. Go ahead
1553 // and try to get the signal time here so the syscall is taken out of
1554 // the main thread's critical path.
1555 mAcquireTimeline.updateSignalTimes();
1556 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001557 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001558 mFrameEventHistory.addQueue(*newTimestamps);
1559 }
1560
Brian Anderson3890c392016-07-25 12:48:08 -07001561 if (outDelta) {
1562 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001563 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001564}
Dan Stozae77c7662016-05-13 11:37:28 -07001565
Chia-I Wu98f1c102017-05-30 14:54:08 -07001566size_t Layer::getChildrenCount() const {
1567 size_t count = 0;
1568 for (const sp<Layer>& child : mCurrentChildren) {
1569 count += 1 + child->getChildrenCount();
1570 }
1571 return count;
1572}
1573
Robert Carr1f0a16a2016-10-24 16:27:39 -07001574void Layer::addChild(const sp<Layer>& layer) {
1575 mCurrentChildren.add(layer);
1576 layer->setParent(this);
1577}
1578
1579ssize_t Layer::removeChild(const sp<Layer>& layer) {
1580 layer->setParent(nullptr);
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001581
Robert Carr1f0a16a2016-10-24 16:27:39 -07001582 return mCurrentChildren.remove(layer);
1583}
1584
Robert Carr1db73f62016-12-21 12:58:51 -08001585bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1586 sp<Handle> handle = nullptr;
1587 sp<Layer> newParent = nullptr;
1588 if (newParentHandle == nullptr) {
1589 return false;
1590 }
1591 handle = static_cast<Handle*>(newParentHandle.get());
1592 newParent = handle->owner.promote();
1593 if (newParent == nullptr) {
1594 ALOGE("Unable to promote Layer handle");
1595 return false;
1596 }
1597
chaviw5aedec92018-10-22 10:40:38 -07001598 if (attachChildren()) {
1599 setTransactionFlags(eTransactionNeeded);
1600 }
Robert Carr1db73f62016-12-21 12:58:51 -08001601 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001602 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001603 }
1604 mCurrentChildren.clear();
1605
1606 return true;
1607}
1608
Robert Carr15eae092018-03-23 13:43:53 -07001609void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001610 for (const sp<Layer>& child : mDrawingChildren) {
1611 child->mDrawingParent = newParent;
1612 }
1613}
1614
chaviwf1961f72017-09-18 16:41:07 -07001615bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001616 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001617
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001618 // While layers are detached, we allow most operations
1619 // and simply halt performing the actual transaction. However
1620 // for reparent != null we would enter the mRemovedFromCurrentState
1621 // state, regardless of whether doTransaction was called, and
1622 // so we need to prevent the update here.
1623 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001624 return false;
1625 }
1626
chaviwf1961f72017-09-18 16:41:07 -07001627 sp<Layer> parent = getParent();
1628 if (parent != nullptr) {
1629 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001630 }
1631
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001632 if (newParentHandle != nullptr) {
1633 auto handle = static_cast<Handle*>(newParentHandle.get());
1634 sp<Layer> newParent = handle->owner.promote();
1635 if (newParent == nullptr) {
1636 ALOGE("Unable to promote Layer handle");
1637 return false;
1638 }
1639
1640 newParent->addChild(this);
1641 if (!newParent->isRemovedFromCurrentState()) {
1642 addToCurrentState();
1643 } else {
1644 onRemovedFromCurrentState();
1645 }
1646
1647 if (mLayerDetached) {
1648 mLayerDetached = false;
1649 callSetTransactionFlags = true;
1650 }
1651 } else {
1652 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001653 }
1654
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001655 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001656 setTransactionFlags(eTransactionNeeded);
1657 }
chaviw06178942017-07-27 10:25:59 -07001658 return true;
1659}
1660
Robert Carr9524cb32017-02-13 11:32:32 -08001661bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001662 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001663 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001664 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001665 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001666 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001667 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001668 }
Robert Carr7f619b22017-11-06 12:56:35 -08001669 }
Robert Carr9524cb32017-02-13 11:32:32 -08001670
1671 return true;
1672}
1673
chaviw5aedec92018-10-22 10:40:38 -07001674bool Layer::attachChildren() {
1675 bool changed = false;
1676 for (const sp<Layer>& child : mCurrentChildren) {
1677 sp<Client> parentClient = mClientRef.promote();
1678 sp<Client> client(child->mClientRef.promote());
1679 if (client != nullptr && parentClient != client) {
1680 if (child->mLayerDetached) {
1681 child->mLayerDetached = false;
1682 changed = true;
1683 }
1684 changed |= child->attachChildren();
1685 }
1686 }
1687
1688 return changed;
1689}
1690
Peiyong Lind3788632018-09-18 16:01:31 -07001691bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001692 static const mat4 identityMatrix = mat4();
1693
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001694 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001695 return false;
1696 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001697 ++mCurrentState.sequence;
1698 mCurrentState.colorTransform = matrix;
1699 mCurrentState.hasColorTransform = matrix != identityMatrix;
1700 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001701 setTransactionFlags(eTransactionNeeded);
1702 return true;
1703}
1704
chaviwf66724d2018-11-28 16:35:21 -08001705mat4 Layer::getColorTransform() const {
1706 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1707 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1708 colorTransform = parent->getColorTransform() * colorTransform;
1709 }
1710 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001711}
1712
1713bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001714 bool hasColorTransform = getDrawingState().hasColorTransform;
1715 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1716 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1717 }
1718 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001719}
1720
Chia-I Wu11481472018-05-04 10:43:19 -07001721bool Layer::isLegacyDataSpace() const {
1722 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001723 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001724 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001725}
1726
Robert Carr1f0a16a2016-10-24 16:27:39 -07001727void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001728 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001729}
1730
1731void Layer::clearSyncPoints() {
1732 for (const auto& child : mCurrentChildren) {
1733 child->clearSyncPoints();
1734 }
1735
1736 Mutex::Autolock lock(mLocalSyncPointMutex);
1737 for (auto& point : mLocalSyncPoints) {
1738 point->setFrameAvailable();
1739 }
1740 mLocalSyncPoints.clear();
1741}
1742
1743int32_t Layer::getZ() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001744 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001745}
1746
Robert Carr29abff82017-12-04 13:51:20 -08001747bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1748 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001749 const State& state = useDrawing ? mDrawingState : mCurrentState;
Robert Carr29abff82017-12-04 13:51:20 -08001750 return state.zOrderRelativeOf != nullptr;
1751}
1752
David Sodman41fdfc92017-11-06 16:09:56 -08001753__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001754 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001755 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1756 "makeTraversalList received invalid stateSet");
1757 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1758 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001759 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001760
Robert Carr29abff82017-12-04 13:51:20 -08001761 if (state.zOrderRelatives.size() == 0) {
1762 *outSkipRelativeZUsers = true;
1763 return children;
1764 }
1765
chaviwfd462612018-05-31 16:11:27 -07001766 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001767 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001768 sp<Layer> strongRelative = weakRelative.promote();
1769 if (strongRelative != nullptr) {
1770 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001771 }
1772 }
1773
Dan Stoza412903f2017-04-27 13:42:17 -07001774 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001775 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
Robert Carr503c7042017-09-27 15:06:08 -07001776 if (childState.zOrderRelativeOf != nullptr) {
1777 continue;
1778 }
Robert Carrdb66e622017-04-10 16:55:57 -07001779 traverse.add(child);
1780 }
1781
1782 return traverse;
1783}
1784
Robert Carr1f0a16a2016-10-24 16:27:39 -07001785/**
Robert Carrdb66e622017-04-10 16:55:57 -07001786 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001787 */
Dan Stoza412903f2017-04-27 13:42:17 -07001788void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001789 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1790 // produce a new list for traversing, including our relatives, and not including our children
1791 // who are relatives of another surface. In the case that there are no relative Z,
1792 // makeTraversalList returns our children directly to avoid significant overhead.
1793 // However in this case we need to take the responsibility for filtering children which
1794 // are relatives of another surface here.
1795 bool skipRelativeZUsers = false;
1796 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001797
Robert Carr1f0a16a2016-10-24 16:27:39 -07001798 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001799 for (; i < list.size(); i++) {
1800 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001801 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1802 continue;
1803 }
1804
Robert Carrdb66e622017-04-10 16:55:57 -07001805 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001806 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001807 }
Dan Stoza412903f2017-04-27 13:42:17 -07001808 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001809 }
Robert Carr29abff82017-12-04 13:51:20 -08001810
Dan Stoza412903f2017-04-27 13:42:17 -07001811 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001812 for (; i < list.size(); i++) {
1813 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001814
Robert Carr29abff82017-12-04 13:51:20 -08001815 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1816 continue;
1817 }
Dan Stoza412903f2017-04-27 13:42:17 -07001818 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001819 }
1820}
1821
1822/**
Robert Carrdb66e622017-04-10 16:55:57 -07001823 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001824 */
Dan Stoza412903f2017-04-27 13:42:17 -07001825void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1826 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001827 // See traverseInZOrder for documentation.
1828 bool skipRelativeZUsers = false;
1829 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001830
Robert Carr1f0a16a2016-10-24 16:27:39 -07001831 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001832 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001833 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001834
1835 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1836 continue;
1837 }
1838
Robert Carrdb66e622017-04-10 16:55:57 -07001839 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001840 break;
1841 }
Dan Stoza412903f2017-04-27 13:42:17 -07001842 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001843 }
Dan Stoza412903f2017-04-27 13:42:17 -07001844 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001845 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001846 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001847
1848 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1849 continue;
1850 }
1851
Dan Stoza412903f2017-04-27 13:42:17 -07001852 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001853 }
1854}
1855
chaviw4b129c22018-04-09 16:19:43 -07001856LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1857 const std::vector<Layer*>& layersInTree) {
1858 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1859 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001860 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1861 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001862 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001863
chaviwfd462612018-05-31 16:11:27 -07001864 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001865 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1866 sp<Layer> strongRelative = weakRelative.promote();
1867 // Only add relative layers that are also descendents of the top most parent of the tree.
1868 // If a relative layer is not a descendent, then it should be ignored.
1869 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1870 traverse.add(strongRelative);
1871 }
1872 }
1873
1874 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001875 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001876 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1877 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1878 // the child here since it won't be added later as a relative.
1879 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1880 childState.zOrderRelativeOf.promote().get())) {
1881 continue;
1882 }
1883 traverse.add(child);
1884 }
1885
1886 return traverse;
1887}
1888
1889void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1890 LayerVector::StateSet stateSet,
1891 const LayerVector::Visitor& visitor) {
1892 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001893
1894 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001895 for (; i < list.size(); i++) {
1896 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001897 if (relative->getZ() >= 0) {
1898 break;
1899 }
chaviw4b129c22018-04-09 16:19:43 -07001900 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001901 }
chaviw4b129c22018-04-09 16:19:43 -07001902
chaviwa76b2712017-09-20 12:02:26 -07001903 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001904 for (; i < list.size(); i++) {
1905 const auto& relative = list[i];
1906 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001907 }
1908}
1909
chaviw4b129c22018-04-09 16:19:43 -07001910std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1911 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1912 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1913
1914 std::vector<Layer*> layersInTree = {this};
1915 for (size_t i = 0; i < children.size(); i++) {
1916 const auto& child = children[i];
1917 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1918 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1919 }
1920
1921 return layersInTree;
1922}
1923
1924void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1925 const LayerVector::Visitor& visitor) {
1926 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1927 std::sort(layersInTree.begin(), layersInTree.end());
1928 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1929}
1930
Peiyong Linefefaac2018-08-17 12:27:51 -07001931ui::Transform Layer::getTransform() const {
1932 ui::Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001933 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001934 if (p != nullptr) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001935 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07001936
1937 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
1938 // it isFixedSize) then there may be additional scaling not accounted
1939 // for in the transform. We need to mirror this scaling in child surfaces
1940 // or we will break the contract where WM can treat child surfaces as
1941 // pixels in the parent surface.
David Sodman0cc69182017-11-17 12:12:07 -08001942 if (p->isFixedSize() && p->getBE().compositionInfo.mBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07001943 int bufferWidth;
1944 int bufferHeight;
1945 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
David Sodman0cc69182017-11-17 12:12:07 -08001946 bufferWidth = p->getBE().compositionInfo.mBuffer->getWidth();
1947 bufferHeight = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001948 } else {
David Sodman0cc69182017-11-17 12:12:07 -08001949 bufferHeight = p->getBE().compositionInfo.mBuffer->getWidth();
1950 bufferWidth = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001951 }
Marissa Wall61c58622018-07-18 10:12:20 -07001952 float sx = p->getActiveWidth(p->getDrawingState()) / static_cast<float>(bufferWidth);
1953 float sy = p->getActiveHeight(p->getDrawingState()) / static_cast<float>(bufferHeight);
Peiyong Linefefaac2018-08-17 12:27:51 -07001954 ui::Transform extraParentScaling;
Robert Carr9b429f42017-04-17 14:56:57 -07001955 extraParentScaling.set(sx, 0, 0, sy);
1956 t = t * extraParentScaling;
1957 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001958 }
Marissa Wall61c58622018-07-18 10:12:20 -07001959 return t * getActiveTransform(getDrawingState());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001960}
1961
chaviw13fdc492017-06-27 12:40:18 -07001962half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001963 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001964
chaviw13fdc492017-06-27 12:40:18 -07001965 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1966 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001967}
Robert Carr6452f122017-03-21 10:41:29 -07001968
chaviw13fdc492017-06-27 12:40:18 -07001969half4 Layer::getColor() const {
1970 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001971 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001972}
Robert Carr6452f122017-03-21 10:41:29 -07001973
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001974Layer::RoundedCornerState Layer::getRoundedCornerState() const {
1975 const auto& p = mDrawingParent.promote();
1976 if (p != nullptr) {
1977 RoundedCornerState parentState = p->getRoundedCornerState();
1978 if (parentState.radius > 0) {
1979 ui::Transform t = getActiveTransform(getDrawingState());
1980 t = t.inverse();
1981 parentState.cropRect = t.transform(parentState.cropRect);
1982 // The rounded corners shader only accepts 1 corner radius for performance reasons,
1983 // but a transform matrix can define horizontal and vertical scales.
1984 // Let's take the average between both of them and pass into the shader, practically we
1985 // never do this type of transformation on windows anyway.
1986 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
1987 return parentState;
1988 }
1989 }
1990 const float radius = getDrawingState().cornerRadius;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001991 return radius > 0 ? RoundedCornerState(computeBounds(), radius) : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001992}
1993
Robert Carr1f0a16a2016-10-24 16:27:39 -07001994void Layer::commitChildList() {
1995 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1996 const auto& child = mCurrentChildren[i];
1997 child->commitChildList();
1998 }
1999 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07002000 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002001}
2002
Robert Carr720e5062018-07-30 17:45:14 -07002003void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002004 mCurrentState.inputInfo = info;
2005 mCurrentState.modified = true;
2006 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07002007 setTransactionFlags(eTransactionNeeded);
2008}
2009
chaviw1d044282017-09-27 12:19:28 -07002010void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
2011 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2012 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002013 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07002014
Peiyong Linefefaac2018-08-17 12:27:51 -07002015 ui::Transform requestedTransform = state.active_legacy.transform;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002016 ui::Transform transform = getTransform();
chaviw1d044282017-09-27 12:19:28 -07002017
2018 layerInfo->set_id(sequence);
2019 layerInfo->set_name(getName().c_str());
2020 layerInfo->set_type(String8(getTypeId()));
2021
2022 for (const auto& child : children) {
2023 layerInfo->add_children(child->sequence);
2024 }
2025
2026 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
2027 sp<Layer> strongRelative = weakRelative.promote();
2028 if (strongRelative != nullptr) {
2029 layerInfo->add_relatives(strongRelative->sequence);
2030 }
2031 }
2032
Marissa Wallf58c14b2018-07-24 10:50:43 -07002033 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
chaviw1d044282017-09-27 12:19:28 -07002034 layerInfo->mutable_transparent_region());
2035 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
2036 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
2037
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002038 layerInfo->set_layer_stack(getLayerStack());
chaviw1d044282017-09-27 12:19:28 -07002039 layerInfo->set_z(state.z);
2040
2041 PositionProto* position = layerInfo->mutable_position();
2042 position->set_x(transform.tx());
2043 position->set_y(transform.ty());
2044
2045 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
2046 requestedPosition->set_x(requestedTransform.tx());
2047 requestedPosition->set_y(requestedTransform.ty());
2048
2049 SizeProto* size = layerInfo->mutable_size();
Marissa Wallf58c14b2018-07-24 10:50:43 -07002050 size->set_w(state.active_legacy.w);
2051 size->set_h(state.active_legacy.h);
chaviw1d044282017-09-27 12:19:28 -07002052
Marissa Wallf58c14b2018-07-24 10:50:43 -07002053 LayerProtoHelper::writeToProto(state.crop_legacy, layerInfo->mutable_crop());
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002054 layerInfo->set_corner_radius(getRoundedCornerState().radius);
chaviw1d044282017-09-27 12:19:28 -07002055
2056 layerInfo->set_is_opaque(isOpaque(state));
2057 layerInfo->set_invalidate(contentDirty);
Chia-I Wu01591c92018-05-22 12:03:00 -07002058
2059 // XXX (b/79210409) mCurrentDataSpace is not protected
2060 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
2061
chaviw1d044282017-09-27 12:19:28 -07002062 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
2063 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
2064 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
2065 layerInfo->set_flags(state.flags);
2066
2067 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
2068 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
2069
Jorim Jaggi8e0af362017-11-14 16:28:28 +01002070 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07002071 if (parent != nullptr) {
2072 layerInfo->set_parent(parent->sequence);
2073 }
2074
2075 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2076 if (zOrderRelativeOf != nullptr) {
2077 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
2078 }
2079
Chia-I Wu01591c92018-05-22 12:03:00 -07002080 // XXX getBE().compositionInfo.mBuffer is not protected
David Sodman0cc69182017-11-17 12:12:07 -08002081 auto buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08002082 if (buffer != nullptr) {
2083 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
Peiyong Linefefaac2018-08-17 12:27:51 -07002084 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
Yichi Chen6ca35192018-05-29 12:20:43 +08002085 layerInfo->mutable_buffer_transform());
chaviw1d044282017-09-27 12:19:28 -07002086 }
2087
2088 layerInfo->set_queued_frames(getQueuedFrameCount());
2089 layerInfo->set_refresh_pending(isBufferLatched());
rongliucfb187b2018-03-14 12:26:23 -07002090 layerInfo->set_window_type(state.type);
2091 layerInfo->set_app_id(state.appId);
chaviwadc40c22018-07-10 16:57:27 -07002092 layerInfo->set_curr_frame(mCurrentFrameNumber);
Vishnu Nairf2deb822018-11-12 17:53:48 -08002093 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
chaviwadc40c22018-07-10 16:57:27 -07002094
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002095 for (const auto& pendingState : mPendingStates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07002096 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
chaviwadc40c22018-07-10 16:57:27 -07002097 if (barrierLayer != nullptr) {
2098 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
2099 barrierLayerProto->set_id(barrierLayer->sequence);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002100 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
chaviwadc40c22018-07-10 16:57:27 -07002101 }
2102 }
chaviw1d044282017-09-27 12:19:28 -07002103}
2104
Dominik Laskowski075d3172018-05-24 15:50:06 -07002105void Layer::writeToProto(LayerProto* layerInfo, DisplayId displayId) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07002106 if (!hasHwcLayer(displayId)) {
2107 return;
2108 }
2109
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002110 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
2111
Dominik Laskowski7e045462018-05-30 13:02:02 -07002112 const auto& hwcInfo = getBE().mHwcLayers.at(displayId);
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002113
2114 const Rect& frame = hwcInfo.displayFrame;
2115 LayerProtoHelper::writeToProto(frame, layerInfo->mutable_hwc_frame());
2116
2117 const FloatRect& crop = hwcInfo.sourceCrop;
2118 LayerProtoHelper::writeToProto(crop, layerInfo->mutable_hwc_crop());
2119
2120 const int32_t transform = static_cast<int32_t>(hwcInfo.transform);
2121 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002122
2123 const int32_t compositionType = static_cast<int32_t>(hwcInfo.compositionType);
2124 layerInfo->set_hwc_composition_type(compositionType);
2125
2126 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
2127 static_cast<BufferLayer*>(this)->isProtected()) {
2128 layerInfo->set_is_protected(true);
2129 } else {
2130 layerInfo->set_is_protected(false);
2131 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002132}
2133
Robert Carr2e102c92018-10-23 12:11:15 -07002134bool Layer::isRemovedFromCurrentState() const {
2135 return mRemovedFromCurrentState;
2136}
2137
Arthur Hungd20b2702019-01-14 18:16:16 +08002138InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002139 InputWindowInfo info = mDrawingState.inputInfo;
Robert Carr720e5062018-07-30 17:45:14 -07002140
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002141 if (info.displayId == ADISPLAY_ID_NONE) {
2142 info.displayId = mDrawingState.layerStack;
2143 }
2144
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002145 ui::Transform t = getTransform();
2146 const float xScale = t.sx();
2147 const float yScale = t.sy();
2148 if (xScale != 1.0f || yScale != 1.0f) {
2149 info.windowXScale *= 1.0f / xScale;
2150 info.windowYScale *= 1.0f / yScale;
2151 info.touchableRegion.scaleSelf(xScale, yScale);
Riddle Hsu39d4aa52018-11-30 20:46:53 +08002152 }
Robert Carre07e1032018-11-26 12:55:53 -08002153
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002154 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002155 // If this is a portal window, set the touchableRegion to the layerBounds.
2156 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
2157 ? getBufferSize(getDrawingState())
2158 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002159 if (!layerBounds.isValid()) {
2160 layerBounds = getCroppedBufferSize(getDrawingState());
2161 }
Vishnu Nair8033a492018-12-05 07:27:23 -08002162 layerBounds = t.transform(layerBounds);
2163 layerBounds.inset(info.surfaceInset, info.surfaceInset, info.surfaceInset, info.surfaceInset);
2164
Arthur Hungd20b2702019-01-14 18:16:16 +08002165 // Input coordinate should match the layer bounds.
2166 info.frameLeft = layerBounds.left;
2167 info.frameTop = layerBounds.top;
2168 info.frameRight = layerBounds.right;
2169 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08002170
2171 // Position the touchable region relative to frame screen location and restrict it to frame
2172 // bounds.
2173 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
Robert Carr720e5062018-07-30 17:45:14 -07002174 info.visible = isVisible();
2175 return info;
2176}
2177
2178bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002179 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002180}
2181
Mathias Agopian13127d82013-03-05 17:47:11 -08002182// ---------------------------------------------------------------------------
2183
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002184}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002185
2186#if defined(__gl_h_)
2187#error "don't include gl/gl.h in this file"
2188#endif
2189
2190#if defined(__gl2_h_)
2191#error "don't include gl2/gl2.h in this file"
2192#endif