blob: de728b49b0533f9654d9c27e355bb16b7ea69a32 [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"
Valerie Haudd0b7572019-01-29 14:59:27 -080049#include "ColorLayer.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020050#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070051#include "DisplayDevice.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052#include "Layer.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070053#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070054#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056
Mathias Agopian1b031492012-06-20 17:51:20 -070057#include "DisplayHardware/HWComposer.h"
Yiwei Zhang7e666a52018-11-15 13:33:42 -080058#include "TimeStats/TimeStats.h"
Mathias Agopian1b031492012-06-20 17:51:20 -070059
Peiyong Lincbc184f2018-08-22 13:24:10 -070060#include <renderengine/RenderEngine.h>
Mathias Agopian875d8e12013-06-07 15:35:48 -070061
Dan Stozac5da2712016-07-20 15:38:12 -070062#include <mutex>
chaviw1d044282017-09-27 12:19:28 -070063#include "LayerProtoHelper.h"
Dan Stozac5da2712016-07-20 15:38:12 -070064
David Sodman41fdfc92017-11-06 16:09:56 -080065#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067namespace android {
68
Yiwei Zhang5434a782018-12-05 18:06:32 -080069using base::StringAppendF;
70
Lloyd Piquef1c675b2018-09-12 20:45:39 -070071std::atomic<int32_t> Layer::sSequence{1};
Mathias Agopian13127d82013-03-05 17:47:11 -080072
Lloyd Pique42ab75e2018-09-12 20:46:03 -070073Layer::Layer(const LayerCreationArgs& args)
74 : mFlinger(args.flinger),
75 mName(args.name),
76 mClientRef(args.client),
77 mBE{this, args.name.string()} {
Mathias Agopiana67932f2011-04-20 14:20:59 -070078 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070079
80 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -070081 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
82 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
83 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070084
Dan Stozaf7ba41a2017-05-10 15:11:11 -070085 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070086
Lloyd Pique0449b0f2018-12-20 16:23:45 -080087 mCurrentState.active_legacy.w = args.w;
88 mCurrentState.active_legacy.h = args.h;
89 mCurrentState.flags = layerFlags;
90 mCurrentState.active_legacy.transform.set(0, 0);
91 mCurrentState.crop_legacy.makeInvalid();
92 mCurrentState.requestedCrop_legacy = mCurrentState.crop_legacy;
93 mCurrentState.z = 0;
94 mCurrentState.color.a = 1.0f;
95 mCurrentState.layerStack = 0;
96 mCurrentState.sequence = 0;
97 mCurrentState.requested_legacy = mCurrentState.active_legacy;
Lloyd Pique0449b0f2018-12-20 16:23:45 -080098 mCurrentState.active.w = UINT32_MAX;
99 mCurrentState.active.h = UINT32_MAX;
100 mCurrentState.active.transform.set(0, 0);
101 mCurrentState.transform = 0;
102 mCurrentState.transformToDisplayInverse = false;
103 mCurrentState.crop.makeInvalid();
104 mCurrentState.acquireFence = new Fence(-1);
105 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
106 mCurrentState.hdrMetadata.validTypes = 0;
107 mCurrentState.surfaceDamageRegion.clear();
108 mCurrentState.cornerRadius = 0.0f;
109 mCurrentState.api = -1;
110 mCurrentState.hasColorTransform = false;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700111
112 // drawing state & current state are identical
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800113 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700114
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800115 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700116 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800117 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200118 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Robert Carr2e102c92018-10-23 12:11:15 -0700119
120 mFlinger->onLayerCreated();
Dan Stoza436ccf32018-06-21 12:10:12 -0700121}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700122
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700123Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800124 sp<Client> c(mClientRef.promote());
125 if (c != 0) {
126 c->detachLayer(this);
127 }
128
Jorim Jaggi10c985e2018-10-23 11:17:45 +0000129 mFrameTracker.logAndResetStats(mName);
Robert Carr2e102c92018-10-23 12:11:15 -0700130
chaviw61626f22018-11-15 16:26:27 -0800131 destroyAllHwcLayersPlusChildren();
Robert Carr2e102c92018-10-23 12:11:15 -0700132
133 mFlinger->onLayerDestroyed();
Mathias Agopian96f08192010-06-02 23:28:45 -0700134}
135
Mathias Agopian13127d82013-03-05 17:47:11 -0800136// ---------------------------------------------------------------------------
137// callbacks
138// ---------------------------------------------------------------------------
139
David Sodmaneb085e02017-10-05 18:49:04 -0700140/*
141 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
142 * Layer. So, the implementation is done in BufferLayer. When called on a
143 * ColorLayer object, it's essentially a NOP.
144 */
David Sodmaneb085e02017-10-05 18:49:04 -0700145void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800146
Chia-I Wuc6657022017-08-15 11:18:17 -0700147void Layer::onRemovedFromCurrentState() {
Robert Carr2e102c92018-10-23 12:11:15 -0700148 mRemovedFromCurrentState = true;
149
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800150 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
151 if (mCurrentState.zOrderRelativeOf != nullptr) {
152 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
153 if (strongRelative != nullptr) {
154 strongRelative->removeZOrderRelative(this);
155 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700156 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800157 mCurrentState.zOrderRelativeOf = nullptr;
Robert Carr5edb1ad2017-04-25 10:54:24 -0700158 }
Rob Carr4bba3702018-10-08 21:53:30 +0000159
Robert Carr2e102c92018-10-23 12:11:15 -0700160 // Since we are no longer reachable from CurrentState SurfaceFlinger
161 // will no longer invoke doTransaction for us, and so we will
162 // never finish applying transactions. We signal the sync point
163 // now so that another layer will not become indefinitely
164 // blocked.
165 for (auto& point: mRemoteSyncPoints) {
166 point->setTransactionApplied();
167 }
168 mRemoteSyncPoints.clear();
169
170 {
171 Mutex::Autolock syncLock(mLocalSyncPointMutex);
172 for (auto& point : mLocalSyncPoints) {
173 point->setFrameAvailable();
174 }
175 mLocalSyncPoints.clear();
176 }
177
Chia-I Wuc6657022017-08-15 11:18:17 -0700178 for (const auto& child : mCurrentChildren) {
179 child->onRemovedFromCurrentState();
180 }
Robert Carr6fb1a7e2018-12-11 12:07:25 -0800181
182 mFlinger->markLayerPendingRemovalLocked(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700183}
Chia-I Wu38512252017-05-17 14:36:16 -0700184
chaviw61626f22018-11-15 16:26:27 -0800185void Layer::addToCurrentState() {
186 mRemovedFromCurrentState = false;
187
188 for (const auto& child : mCurrentChildren) {
189 child->addToCurrentState();
190 }
191}
192
Mathias Agopian13127d82013-03-05 17:47:11 -0800193// ---------------------------------------------------------------------------
194// set-up
195// ---------------------------------------------------------------------------
196
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700197const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800198 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800199}
200
chaviw13fdc492017-06-27 12:40:18 -0700201bool Layer::getPremultipledAlpha() const {
202 return mPremultipliedAlpha;
203}
204
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700205sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800206 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700207 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800208}
209
210// ---------------------------------------------------------------------------
211// h/w composer set-up
212// ---------------------------------------------------------------------------
213
Dominik Laskowski075d3172018-05-24 15:50:06 -0700214bool Layer::createHwcLayer(HWComposer* hwc, DisplayId displayId) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700215 LOG_ALWAYS_FATAL_IF(hasHwcLayer(displayId), "Already have a layer for display %s",
216 to_string(displayId).c_str());
David Sodmanb8aaea12017-12-14 15:54:51 -0800217 auto layer = std::shared_ptr<HWC2::Layer>(
218 hwc->createLayer(displayId),
219 [hwc, displayId](HWC2::Layer* layer) {
220 hwc->destroyLayer(displayId, layer); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700221 if (!layer) {
222 return false;
223 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700224 LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers[displayId];
Steven Thomasb02664d2017-07-26 18:48:28 -0700225 hwcInfo.hwc = hwc;
226 hwcInfo.layer = layer;
David Sodmanf6a38932018-05-25 15:27:50 -0700227 layer->setLayerDestroyedListener(
Dominik Laskowski7e045462018-05-30 13:02:02 -0700228 [this, displayId](HWC2::Layer* /*layer*/) { getBE().mHwcLayers.erase(displayId); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700229 return true;
230}
231
Dominik Laskowski075d3172018-05-24 15:50:06 -0700232bool Layer::destroyHwcLayer(DisplayId displayId) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700233 if (!hasHwcLayer(displayId)) {
Chia-I Wu83806892017-11-16 10:50:20 -0800234 return false;
Steven Thomasb02664d2017-07-26 18:48:28 -0700235 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700236 auto& hwcInfo = getBE().mHwcLayers[displayId];
David Sodman41fdfc92017-11-06 16:09:56 -0800237 LOG_ALWAYS_FATAL_IF(hwcInfo.layer == nullptr, "Attempt to destroy null layer");
Steven Thomasb02664d2017-07-26 18:48:28 -0700238 LOG_ALWAYS_FATAL_IF(hwcInfo.hwc == nullptr, "Missing HWComposer");
David Sodmanb8aaea12017-12-14 15:54:51 -0800239 hwcInfo.layer = nullptr;
240
Chia-I Wu83806892017-11-16 10:50:20 -0800241 return true;
Steven Thomasb02664d2017-07-26 18:48:28 -0700242}
243
chaviw61626f22018-11-15 16:26:27 -0800244void Layer::destroyHwcLayersForAllDisplays() {
David Sodman6f65f3e2017-11-03 14:28:09 -0700245 size_t numLayers = getBE().mHwcLayers.size();
Steven Thomasb02664d2017-07-26 18:48:28 -0700246 for (size_t i = 0; i < numLayers; ++i) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700247 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.empty(), "destroyAllHwcLayers failed");
248 destroyHwcLayer(getBE().mHwcLayers.begin()->first);
Steven Thomasb02664d2017-07-26 18:48:28 -0700249 }
chaviw61626f22018-11-15 16:26:27 -0800250}
251
252void Layer::destroyAllHwcLayersPlusChildren() {
253 destroyHwcLayersForAllDisplays();
David Sodman6f65f3e2017-11-03 14:28:09 -0700254 LOG_ALWAYS_FATAL_IF(!getBE().mHwcLayers.empty(),
David Sodman41fdfc92017-11-06 16:09:56 -0800255 "All hardware composer layers should have been destroyed");
Robert Carr2e102c92018-10-23 12:11:15 -0700256
257 for (const sp<Layer>& child : mDrawingChildren) {
chaviw61626f22018-11-15 16:26:27 -0800258 child->destroyAllHwcLayersPlusChildren();
Robert Carr2e102c92018-10-23 12:11:15 -0700259 }
Steven Thomasb02664d2017-07-26 18:48:28 -0700260}
Steven Thomasb02664d2017-07-26 18:48:28 -0700261
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800262Rect Layer::getContentCrop() const {
263 // this is the crop rectangle that applies to the buffer
264 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700265 Rect crop;
266 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800267 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700268 crop = mCurrentCrop;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800269 } else if (getBE().compositionInfo.mBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800270 // otherwise we use the whole buffer
David Sodman0cc69182017-11-17 12:12:07 -0800271 crop = getBE().compositionInfo.mBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700272 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800273 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700274 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700275 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700276 return crop;
277}
278
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700279static Rect reduce(const Rect& win, const Region& exclude) {
280 if (CC_LIKELY(exclude.isEmpty())) {
281 return win;
282 }
283 if (exclude.isRect()) {
284 return win.reduce(exclude.getBounds());
285 }
286 return Region(win).subtract(exclude).getBounds();
287}
288
Dan Stoza80d61162017-12-20 15:57:52 -0800289static FloatRect reduce(const FloatRect& win, const Region& exclude) {
290 if (CC_LIKELY(exclude.isEmpty())) {
291 return win;
292 }
293 // Convert through Rect (by rounding) for lack of FloatRegion
294 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
295}
296
Vishnu Nair51625fa2018-12-06 13:54:33 -0800297Rect Layer::computeScreenBounds(bool reduceTransparentRegion) const {
298 const State& s(getDrawingState());
299 Region transparentRegion = reduceTransparentRegion ? getActiveTransparentRegion(s) : Region();
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800300 FloatRect bounds = computeBounds(transparentRegion);
301 ui::Transform t = getTransform();
Vishnu Nair60356342018-11-13 13:00:45 -0800302 // Transform to screen space.
303 bounds = t.transform(bounds);
304 return Rect{bounds};
Robert Carr1f0a16a2016-10-24 16:27:39 -0700305}
306
Dan Stoza80d61162017-12-20 15:57:52 -0800307FloatRect Layer::computeBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000308 const State& s(getDrawingState());
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800309 return computeBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700310}
311
Dan Stoza80d61162017-12-20 15:57:52 -0800312FloatRect Layer::computeBounds(const Region& activeTransparentRegion) const {
Alec Mourib416efd2018-09-06 21:01:59 +0000313 const State& s(getDrawingState());
Vishnu Nair60356342018-11-13 13:00:45 -0800314 Rect bounds = getCroppedBufferSize(s);
315 FloatRect floatBounds = bounds.toFloatRect();
316 if (bounds.isValid()) {
317 // Layer has bounds. Pass in our bounds as a special case. Then pass on to our parents so
318 // that they can clip it.
319 floatBounds = cropChildBounds(floatBounds);
320 } else {
321 // Layer does not have bounds, so we fill to our parent bounds. This is done by getting our
322 // parent bounds and inverting the transform to get the maximum bounds we can have that
323 // will fit within our parent bounds.
324 const auto& p = mDrawingParent.promote();
325 if (p != nullptr) {
326 ui::Transform t = s.active_legacy.transform;
327 // When calculating the parent bounds for purposes of clipping, we don't need to
328 // constrain the parent to its transparent region. The transparent region is an
329 // optimization based on the buffer contents of the layer, but does not affect the
330 // space allocated to it by policy, and thus children should be allowed to extend into
331 // the parent's transparent region.
332 // One of the main uses is a parent window with a child sitting behind the parent
333 // window, marked by a transparent region. When computing the parent bounds from the
334 // parent's perspective we pass in the transparent region to reduce buffer allocation
335 // size. When computing the parent bounds from the child's perspective, we pass in an
336 // empty transparent region in order to extend into the the parent bounds.
337 floatBounds = p->computeBounds(Region());
338 // Transform back to layer space.
339 floatBounds = t.inverse().transform(floatBounds);
340 }
341 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700342
Vishnu Nair60356342018-11-13 13:00:45 -0800343 // Subtract the transparent region and snap to the bounds.
344 return reduce(floatBounds, activeTransparentRegion);
345}
346
347FloatRect Layer::cropChildBounds(const FloatRect& childBounds) const {
348 const State& s(getDrawingState());
349 Rect bounds = getCroppedBufferSize(s);
350 FloatRect croppedBounds = childBounds;
351
352 // If the layer has bounds, then crop the passed in child bounds and pass
353 // it to our parents so they can crop it as well. If the layer has no bounds,
354 // then pass on the child bounds.
355 if (bounds.isValid()) {
356 croppedBounds = croppedBounds.intersect(bounds.toFloatRect());
Mathias Agopian13127d82013-03-05 17:47:11 -0800357 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700358
Chia-I Wue41dbe62017-06-13 14:10:56 -0700359 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700360 if (p != nullptr) {
Vishnu Nair60356342018-11-13 13:00:45 -0800361 // Transform to parent space and allow parent layer to crop the
362 // child bounds as well.
363 ui::Transform t = s.active_legacy.transform;
364 croppedBounds = t.transform(croppedBounds);
365 croppedBounds = p->cropChildBounds(croppedBounds);
366 croppedBounds = t.inverse().transform(croppedBounds);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700367 }
Vishnu Nair60356342018-11-13 13:00:45 -0800368 return croppedBounds;
369}
Robert Carr1f0a16a2016-10-24 16:27:39 -0700370
Vishnu Nair60356342018-11-13 13:00:45 -0800371Rect Layer::getCroppedBufferSize(const State& s) const {
372 Rect size = getBufferSize(s);
373 Rect crop = getCrop(s);
374 if (!crop.isEmpty() && size.isValid()) {
375 size.intersect(crop, &size);
376 } else if (!crop.isEmpty()) {
377 size = crop;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700378 }
Vishnu Nair60356342018-11-13 13:00:45 -0800379 return size;
Mathias Agopian13127d82013-03-05 17:47:11 -0800380}
381
Alec Mouri1f3bd182019-01-24 15:20:18 +0000382Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& display) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700383 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800384 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700385 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800386
387 // apply the projection's clipping to the window crop in
388 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700389 // if there are no window scaling involved, this operation will map to full
390 // pixels in the buffer.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700391
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800392 FloatRect activeCropFloat = computeBounds();
Alec Mouri1f3bd182019-01-24 15:20:18 +0000393 ui::Transform t = getTransform();
394 // Transform to screen space.
395 activeCropFloat = t.transform(activeCropFloat);
396 activeCropFloat = activeCropFloat.intersect(display->getViewport().toFloatRect());
397 // Back to layer space to work with the content crop.
398 activeCropFloat = t.inverse().transform(activeCropFloat);
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800399 // This needs to be here as transform.transform(Rect) computes the
400 // transformed rect and then takes the bounding box of the result before
401 // returning. This means
402 // transform.inverse().transform(transform.transform(Rect)) != Rect
403 // in which case we need to make sure the final rect is clipped to the
404 // display bounds.
405 Rect activeCrop{activeCropFloat};
406 if (!activeCrop.intersect(getBufferSize(s), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000407 activeCrop.clear();
408 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700409 return activeCrop;
410}
411
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700412void Layer::setupRoundedCornersCropCoordinates(Rect win,
413 const FloatRect& roundedCornersCrop) const {
414 // Translate win by the rounded corners rect coordinates, to have all values in
415 // layer coordinate space.
416 win.left -= roundedCornersCrop.left;
417 win.right -= roundedCornersCrop.left;
418 win.top -= roundedCornersCrop.top;
419 win.bottom -= roundedCornersCrop.top;
420
421 renderengine::Mesh::VertexArray<vec2> cropCoords(getBE().mMesh.getCropCoordArray<vec2>());
422 cropCoords[0] = vec2(win.left, win.top);
423 cropCoords[1] = vec2(win.left, win.top + win.getHeight());
424 cropCoords[2] = vec2(win.right, win.top + win.getHeight());
425 cropCoords[3] = vec2(win.right, win.top);
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700426}
427
Alec Mouri1f3bd182019-01-24 15:20:18 +0000428FloatRect Layer::computeCrop(const sp<const DisplayDevice>& display) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700429 // the content crop is the area of the content that gets scaled to the
430 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800431 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700432
433 // In addition there is a WM-specified crop we pull from our drawing state.
434 const State& s(getDrawingState());
435
Alec Mouri1f3bd182019-01-24 15:20:18 +0000436 Rect activeCrop = computeInitialCrop(display);
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800437 Rect bufferSize = getBufferSize(s);
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700438
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000439 // Transform the window crop to match the buffer coordinate system,
440 // which means using the inverse of the current transform set on the
441 // SurfaceFlingerConsumer.
442 uint32_t invTransform = mCurrentTransform;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800443 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000444 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700445 * the code below applies the primary display's inverse transform to the
446 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000447 */
David Sodman41fdfc92017-11-06 16:09:56 -0800448 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000449 // calculate the inverse transform
450 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800451 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800452 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000453 // and apply to the current transform
Peiyong Linefefaac2018-08-17 12:27:51 -0700454 invTransform = (ui::Transform(invTransformOrient) *
455 ui::Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800456 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000457
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800458 int winWidth = bufferSize.getWidth();
459 int winHeight = bufferSize.getHeight();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000460 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
461 // If the activeCrop has been rotate the ends are rotated but not
462 // the space itself so when transforming ends back we can't rely on
463 // a modification of the axes of rotation. To account for this we
464 // need to reorient the inverse rotation in terms of the current
465 // axes of rotation.
466 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
467 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
468 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800469 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000470 }
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800471 std::swap(winWidth, winHeight);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000472 }
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800473 const Rect winCrop =
474 activeCrop.transform(invTransform, bufferSize.getWidth(), bufferSize.getHeight());
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000475
476 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800477 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000478 float yScale = crop.getHeight() / float(winHeight);
479
David Sodman41fdfc92017-11-06 16:09:56 -0800480 float insetL = winCrop.left * xScale;
481 float insetT = winCrop.top * yScale;
482 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000483 float insetB = (winHeight - winCrop.bottom) * yScale;
484
David Sodman41fdfc92017-11-06 16:09:56 -0800485 crop.left += insetL;
486 crop.top += insetT;
487 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000488 crop.bottom -= insetB;
489
Mathias Agopian13127d82013-03-05 17:47:11 -0800490 return crop;
491}
492
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700493void Layer::setGeometry(const sp<const DisplayDevice>& display, uint32_t z) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700494 const auto displayId = display->getId();
Dominik Laskowski075d3172018-05-24 15:50:06 -0700495 LOG_ALWAYS_FATAL_IF(!displayId);
Dominik Laskowski34157762018-10-31 13:07:19 -0700496 RETURN_IF_NO_HWC_LAYER(*displayId);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700497 auto& hwcInfo = getBE().mHwcLayers[*displayId];
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700498
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700499 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800500 hwcInfo.forceClientComposition = false;
501
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800502 if (isSecure() && !display->isSecure()) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800503 hwcInfo.forceClientComposition = true;
504 }
505
David Sodman15094112018-10-11 09:39:37 -0700506 auto& hwcLayer = hwcInfo.layer;
507
Mathias Agopian13127d82013-03-05 17:47:11 -0800508 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700509 const State& s(getDrawingState());
Vishnu Naira6733b32018-12-06 18:13:16 -0800510 const Rect bufferSize = getBufferSize(s);
David Revemanecf0fa52017-03-03 11:32:44 -0500511 auto blendMode = HWC2::BlendMode::None;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800512 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800513 blendMode =
514 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800515 }
David Sodman15094112018-10-11 09:39:37 -0700516 auto error = hwcLayer->setBlendMode(blendMode);
517 ALOGE_IF(error != HWC2::Error::None,
518 "[%s] Failed to set blend mode %s:"
519 " %s (%d)",
520 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
521 static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700522 getBE().compositionInfo.hwc.blendMode = blendMode;
Mathias Agopian13127d82013-03-05 17:47:11 -0800523
524 // apply the layer's transform, followed by the display's global transform
525 // here we're guaranteed that the layer's transform preserves rects
Marissa Wall61c58622018-07-18 10:12:20 -0700526 Region activeTransparentRegion(getActiveTransparentRegion(s));
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800527 ui::Transform t = getTransform();
Marissa Wall61c58622018-07-18 10:12:20 -0700528 Rect activeCrop = getCrop(s);
Vishnu Naira6733b32018-12-06 18:13:16 -0800529 if (!activeCrop.isEmpty() && bufferSize.isValid()) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700530 activeCrop = t.transform(activeCrop);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700531 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000532 activeCrop.clear();
533 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700534 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800535 // This needs to be here as transform.transform(Rect) computes the
536 // transformed rect and then takes the bounding box of the result before
537 // returning. This means
538 // transform.inverse().transform(transform.transform(Rect)) != Rect
539 // in which case we need to make sure the final rect is clipped to the
540 // display bounds.
Vishnu Naira6733b32018-12-06 18:13:16 -0800541 if (!activeCrop.intersect(bufferSize, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000542 activeCrop.clear();
543 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700544 // mark regions outside the crop as transparent
Vishnu Naira6733b32018-12-06 18:13:16 -0800545 activeTransparentRegion.orSelf(Rect(0, 0, bufferSize.getWidth(), activeCrop.top));
546 activeTransparentRegion.orSelf(
547 Rect(0, activeCrop.bottom, bufferSize.getWidth(), bufferSize.getHeight()));
David Sodman41fdfc92017-11-06 16:09:56 -0800548 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
549 activeTransparentRegion.orSelf(
Vishnu Naira6733b32018-12-06 18:13:16 -0800550 Rect(activeCrop.right, activeCrop.top, bufferSize.getWidth(), activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700551 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700552
Dan Stoza80d61162017-12-20 15:57:52 -0800553 // computeBounds returns a FloatRect to provide more accuracy during the
554 // transformation. We then round upon constructing 'frame'.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800555 Rect frame{t.transform(computeBounds(activeTransparentRegion))};
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700556 if (!frame.intersect(display->getViewport(), &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000557 frame.clear();
558 }
Peiyong Linefefaac2018-08-17 12:27:51 -0700559 const ui::Transform& tr = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800560 Rect transformedFrame = tr.transform(frame);
David Sodman15094112018-10-11 09:39:37 -0700561 error = hwcLayer->setDisplayFrame(transformedFrame);
562 if (error != HWC2::Error::None) {
563 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
564 transformedFrame.left, transformedFrame.top, transformedFrame.right,
565 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
566 } else {
567 hwcInfo.displayFrame = transformedFrame;
568 }
David Sodmanba340492018-08-05 21:51:33 -0700569 getBE().compositionInfo.hwc.displayFrame = transformedFrame;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800570
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700571 FloatRect sourceCrop = computeCrop(display);
David Sodman15094112018-10-11 09:39:37 -0700572 error = hwcLayer->setSourceCrop(sourceCrop);
573 if (error != HWC2::Error::None) {
574 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
575 "%s (%d)",
576 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
577 to_string(error).c_str(), static_cast<int32_t>(error));
578 } else {
579 hwcInfo.sourceCrop = sourceCrop;
580 }
David Sodmanba340492018-08-05 21:51:33 -0700581 getBE().compositionInfo.hwc.sourceCrop = sourceCrop;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800582
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800583 float alpha = static_cast<float>(getAlpha());
David Sodman15094112018-10-11 09:39:37 -0700584 error = hwcLayer->setPlaneAlpha(alpha);
585 ALOGE_IF(error != HWC2::Error::None,
586 "[%s] Failed to set plane alpha %.3f: "
587 "%s (%d)",
588 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700589 getBE().compositionInfo.hwc.alpha = alpha;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800590
David Sodman15094112018-10-11 09:39:37 -0700591 error = hwcLayer->setZOrder(z);
592 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
593 to_string(error).c_str(), static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700594 getBE().compositionInfo.hwc.z = z;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500595
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800596 int type = s.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
597 int appId = s.metadata.getInt32(METADATA_OWNER_UID, 0);
Chia-I Wue41dbe62017-06-13 14:10:56 -0700598 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400599 if (parent.get()) {
600 auto& parentState = parent->getDrawingState();
Evan Rosky1f6d6d52018-12-06 10:47:26 -0800601 const int parentType = parentState.metadata.getInt32(METADATA_WINDOW_TYPE, 0);
602 const int parentAppId = parentState.metadata.getInt32(METADATA_OWNER_UID, 0);
603 if (parentType >= 0 || parentAppId >= 0) {
604 type = parentType;
605 appId = parentAppId;
rongliucfb187b2018-03-14 12:26:23 -0700606 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400607 }
608
David Sodman15094112018-10-11 09:39:37 -0700609 error = hwcLayer->setInfo(type, appId);
610 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
611 static_cast<int32_t>(error));
612
David Sodmanba340492018-08-05 21:51:33 -0700613 getBE().compositionInfo.hwc.type = type;
614 getBE().compositionInfo.hwc.appId = appId;
615
Mathias Agopian29a367b2011-07-12 14:51:45 -0700616 /*
617 * Transformations are applied in this order:
618 * 1) buffer orientation/flip/mirror
619 * 2) state transformation (window manager)
620 * 3) layer orientation (screen orientation)
621 * (NOTE: the matrices are multiplied in reverse order)
622 */
623
Peiyong Linefefaac2018-08-17 12:27:51 -0700624 const ui::Transform bufferOrientation(mCurrentTransform);
625 ui::Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700626
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800627 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700628 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700629 * the code below applies the primary display's inverse transform to the
630 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700631 */
David Sodman41fdfc92017-11-06 16:09:56 -0800632 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700633 // calculate the inverse transform
634 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800635 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700636 }
Robert Carrcae605c2017-03-29 12:10:31 -0700637
638 /*
639 * Here we cancel out the orientation component of the WM transform.
640 * The scaling and translate components are already included in our bounds
641 * computation so it's enough to just omit it in the composition.
Alec Mouri79108df2019-02-04 19:33:44 +0000642 * See comment in onDraw with ref to b/36727915 for why.
Robert Carrcae605c2017-03-29 12:10:31 -0700643 */
Peiyong Linefefaac2018-08-17 12:27:51 -0700644 transform = ui::Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700645 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700646
647 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800648 const uint32_t orientation = transform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -0700649 if (orientation & ui::Transform::ROT_INVALID) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800650 // we can only handle simple transformation
Lloyd Pique074e8122018-07-26 12:57:23 -0700651 hwcInfo.forceClientComposition = true;
Dominik Laskowski075d3172018-05-24 15:50:06 -0700652 getBE().mHwcLayers[*displayId].compositionType = HWC2::Composition::Client;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800653 } else {
654 auto transform = static_cast<HWC2::Transform>(orientation);
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800655 hwcInfo.transform = transform;
David Sodman15094112018-10-11 09:39:37 -0700656 auto error = hwcLayer->setTransform(transform);
657 ALOGE_IF(error != HWC2::Error::None,
658 "[%s] Failed to set transform %s: "
659 "%s (%d)",
660 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
661 static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700662 getBE().compositionInfo.hwc.transform = transform;
David Sodman4b7c4bc2017-11-17 12:13:59 -0800663 }
664}
665
Dominik Laskowski075d3172018-05-24 15:50:06 -0700666void Layer::forceClientComposition(DisplayId displayId) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700667 RETURN_IF_NO_HWC_LAYER(displayId);
Dominik Laskowski7e045462018-05-30 13:02:02 -0700668 getBE().mHwcLayers[displayId].forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800669}
Dan Stozaee44edd2015-03-23 15:50:23 -0700670
Dominik Laskowski075d3172018-05-24 15:50:06 -0700671bool Layer::getForceClientComposition(DisplayId displayId) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700672 RETURN_IF_NO_HWC_LAYER(displayId, false);
Dominik Laskowski7e045462018-05-30 13:02:02 -0700673 return getBE().mHwcLayers[displayId].forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800674}
675
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700676void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700677 const auto displayId = display->getId();
Dominik Laskowski34157762018-10-31 13:07:19 -0700678 LOG_ALWAYS_FATAL_IF(!displayId);
679 if (!hasHwcLayer(*displayId) || getCompositionType(displayId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800680 return;
681 }
682
683 // This gives us only the "orientation" component of the transform
684 const State& s(getCurrentState());
685
686 // Apply the layer's transform, followed by the display's global transform
687 // Here we're guaranteed that the layer's transform preserves rects
Vishnu Nairfb5594c2018-11-28 12:38:35 -0800688 Rect win = getCroppedBufferSize(s);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800689 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700690 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800691 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700692 frame.intersect(display->getViewport(), &frame);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700693 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800694 auto position = displayTransform.transform(frame);
695
Dominik Laskowski7e045462018-05-30 13:02:02 -0700696 auto error =
Dominik Laskowski075d3172018-05-24 15:50:06 -0700697 getBE().mHwcLayers[*displayId].layer->setCursorPosition(position.left, position.top);
698
David Sodman41fdfc92017-11-06 16:09:56 -0800699 ALOGE_IF(error != HWC2::Error::None,
700 "[%s] Failed to set cursor position "
701 "to (%d, %d): %s (%d)",
702 mName.string(), position.left, position.top, to_string(error).c_str(),
703 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800704}
Riley Andrews03414a12014-07-01 14:22:59 -0700705
Mathias Agopian13127d82013-03-05 17:47:11 -0800706// ---------------------------------------------------------------------------
707// drawing...
708// ---------------------------------------------------------------------------
709
Alec Mouri79108df2019-02-04 19:33:44 +0000710void Layer::draw(const RenderArea& renderArea, const Region& clip) {
711 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800712}
713
Alec Mouri79108df2019-02-04 19:33:44 +0000714void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) {
715 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800716}
717
David Sodman41fdfc92017-11-06 16:09:56 -0800718void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
719 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800720 auto& engine(mFlinger->getRenderEngine());
David Sodman9eeae692017-11-02 10:53:32 -0700721 computeGeometry(renderArea, getBE().mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700722 engine.setupFillWithColor(red, green, blue, alpha);
David Sodman9eeae692017-11-02 10:53:32 -0700723 engine.drawMesh(getBE().mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800724}
725
David Sodmanc1498e62018-09-12 14:36:26 -0700726void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
727 clearWithOpenGL(renderArea, 0, 0, 0, 0);
728}
729
Dominik Laskowski075d3172018-05-24 15:50:06 -0700730void Layer::setCompositionType(DisplayId displayId, HWC2::Composition type, bool callIntoHwc) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700731 if (getBE().mHwcLayers.count(displayId) == 0) {
Chia-I Wu30505fb2018-03-26 16:20:31 -0700732 ALOGE("setCompositionType called without a valid HWC layer");
733 return;
David Sodman15094112018-10-11 09:39:37 -0700734 }
735 auto& hwcInfo = getBE().mHwcLayers[displayId];
736 auto& hwcLayer = hwcInfo.layer;
737 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", (hwcLayer)->getId(), to_string(type).c_str(),
738 static_cast<int>(callIntoHwc));
739 if (hwcInfo.compositionType != type) {
740 ALOGV(" actually setting");
741 hwcInfo.compositionType = type;
742 if (callIntoHwc) {
743 auto error = (hwcLayer)->setCompositionType(type);
744 ALOGE_IF(error != HWC2::Error::None,
745 "[%s] Failed to set "
746 "composition type %s: %s (%d)",
747 mName.string(), to_string(type).c_str(), to_string(error).c_str(),
748 static_cast<int32_t>(error));
Chia-I Wu30505fb2018-03-26 16:20:31 -0700749 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800750 }
751}
752
Dominik Laskowski075d3172018-05-24 15:50:06 -0700753HWC2::Composition Layer::getCompositionType(const std::optional<DisplayId>& displayId) const {
754 if (!displayId) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700755 // If we're querying the composition type for a display that does not
756 // have a HWC counterpart, then it will always be Client
757 return HWC2::Composition::Client;
758 }
Dominik Laskowski075d3172018-05-24 15:50:06 -0700759 if (getBE().mHwcLayers.count(*displayId) == 0) {
760 ALOGE("getCompositionType called with an invalid HWC layer");
761 return HWC2::Composition::Invalid;
762 }
763 return getBE().mHwcLayers.at(*displayId).compositionType;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800764}
765
Dominik Laskowski075d3172018-05-24 15:50:06 -0700766void Layer::setClearClientTarget(DisplayId displayId, bool clear) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700767 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800768 ALOGE("setClearClientTarget called without a valid HWC layer");
769 return;
770 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700771 getBE().mHwcLayers[displayId].clearClientTarget = clear;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800772}
773
Dominik Laskowski075d3172018-05-24 15:50:06 -0700774bool Layer::getClearClientTarget(DisplayId displayId) const {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700775 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800776 ALOGE("getClearClientTarget called without a valid HWC layer");
777 return false;
778 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700779 return getBE().mHwcLayers.at(displayId).clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800780}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800781
Dan Stozacac35382016-01-27 12:21:06 -0800782bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
783 if (point->getFrameNumber() <= mCurrentFrameNumber) {
784 // Don't bother with a SyncPoint, since we've already latched the
785 // relevant frame
786 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700787 }
Robert Carr2e102c92018-10-23 12:11:15 -0700788 if (isRemovedFromCurrentState()) {
789 return false;
790 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700791
Dan Stozacac35382016-01-27 12:21:06 -0800792 Mutex::Autolock lock(mLocalSyncPointMutex);
793 mLocalSyncPoints.push_back(point);
794 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700795}
796
Mathias Agopian13127d82013-03-05 17:47:11 -0800797// ----------------------------------------------------------------------------
798// local state
799// ----------------------------------------------------------------------------
800
Peiyong Lin833074a2018-08-28 11:53:54 -0700801void Layer::computeGeometry(const RenderArea& renderArea,
802 renderengine::Mesh& mesh,
chaviwa76b2712017-09-20 12:02:26 -0700803 bool useIdentityTransform) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700804 const ui::Transform renderAreaTransform(renderArea.getTransform());
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800805 FloatRect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700806
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000807 vec2 lt = vec2(win.left, win.top);
808 vec2 lb = vec2(win.left, win.bottom);
809 vec2 rb = vec2(win.right, win.bottom);
810 vec2 rt = vec2(win.right, win.top);
811
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800812 ui::Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000813 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700814 lt = layerTransform.transform(lt);
815 lb = layerTransform.transform(lb);
816 rb = layerTransform.transform(rb);
817 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000818 }
819
Peiyong Lin833074a2018-08-28 11:53:54 -0700820 renderengine::Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700821 position[0] = renderAreaTransform.transform(lt);
822 position[1] = renderAreaTransform.transform(lb);
823 position[2] = renderAreaTransform.transform(rb);
824 position[3] = renderAreaTransform.transform(rt);
Mathias Agopian13127d82013-03-05 17:47:11 -0800825}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800826
David Sodman41fdfc92017-11-06 16:09:56 -0800827bool Layer::isSecure() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800828 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700829 return (s.flags & layer_state_t::eLayerSecure);
830}
831
Mathias Agopian13127d82013-03-05 17:47:11 -0800832void Layer::setVisibleRegion(const Region& visibleRegion) {
833 // always called from main thread
834 this->visibleRegion = visibleRegion;
835}
836
837void Layer::setCoveredRegion(const Region& coveredRegion) {
838 // always called from main thread
839 this->coveredRegion = coveredRegion;
840}
841
David Sodman41fdfc92017-11-06 16:09:56 -0800842void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800843 // always called from main thread
844 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
845}
846
Robert Carre5f4f692018-01-12 13:12:28 -0800847void Layer::clearVisibilityRegions() {
848 visibleRegion.clear();
849 visibleNonTransparentRegion.clear();
850 coveredRegion.clear();
851}
852
Mathias Agopian13127d82013-03-05 17:47:11 -0800853// ----------------------------------------------------------------------------
854// transaction
855// ----------------------------------------------------------------------------
Ady Abraham83729882018-12-07 12:26:48 -0800856
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800857void Layer::pushPendingState() {
858 if (!mCurrentState.modified) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700859 return;
860 }
861
Dan Stoza7dde5992015-05-22 09:51:44 -0700862 // If this transaction is waiting on the receipt of a frame, generate a sync
863 // point and send it to the remote layer.
Robert Carr2e102c92018-10-23 12:11:15 -0700864 // We don't allow installing sync points after we are removed from the current state
865 // as we won't be able to signal our end.
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800866 if (mCurrentState.barrierLayer_legacy != nullptr && !isRemovedFromCurrentState()) {
867 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800868 if (barrierLayer == nullptr) {
869 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700870 // If we can't promote the layer we are intended to wait on,
871 // then it is expired or otherwise invalid. Allow this transaction
872 // to be applied as per normal (no synchronization).
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800873 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800874 } else {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800875 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy);
Robert Carr0d480722017-01-10 16:42:54 -0800876 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800877 mRemoteSyncPoints.push_back(std::move(syncPoint));
878 } else {
879 // We already missed the frame we're supposed to synchronize
880 // on, so go ahead and apply the state update
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800881 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800882 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700883 }
884
Dan Stoza7dde5992015-05-22 09:51:44 -0700885 // Wake us up to check if the frame has been received
886 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700887 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700888 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800889 mPendingStates.push_back(mCurrentState);
890 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700891}
892
Pablo Ceballos05289c22016-04-14 15:49:55 -0700893void Layer::popPendingState(State* stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800894 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700895
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800896 mPendingStates.removeAt(0);
897 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700898}
899
Pablo Ceballos05289c22016-04-14 15:49:55 -0700900bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700901 bool stateUpdateAvailable = false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800902 while (!mPendingStates.empty()) {
903 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700904 if (mRemoteSyncPoints.empty()) {
905 // If we don't have a sync point for this, apply it anyway. It
906 // will be visually wrong, but it should keep us from getting
907 // into too much trouble.
908 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700909 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700910 stateUpdateAvailable = true;
911 continue;
912 }
913
Marissa Wallf58c14b2018-07-24 10:50:43 -0700914 if (mRemoteSyncPoints.front()->getFrameNumber() !=
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800915 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800916 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800917
918 // Signal our end of the sync point and then dispose of it
919 mRemoteSyncPoints.front()->setTransactionApplied();
920 mRemoteSyncPoints.pop_front();
921 continue;
922 }
923
Dan Stoza7dde5992015-05-22 09:51:44 -0700924 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
925 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700926 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700927 stateUpdateAvailable = true;
928
929 // Signal our end of the sync point and then dispose of it
930 mRemoteSyncPoints.front()->setTransactionApplied();
931 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800932 } else {
933 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700934 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700935 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700936 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700937 stateUpdateAvailable = true;
938 }
939 }
940
941 // If we still have pending updates, wake SurfaceFlinger back up and point
942 // it at this layer so we can process them
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800943 if (!mPendingStates.empty()) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700944 setTransactionFlags(eTransactionNeeded);
945 mFlinger->setTransactionFlags(eTraversalNeeded);
946 }
947
Lloyd Pique0449b0f2018-12-20 16:23:45 -0800948 mCurrentState.modified = false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700949 return stateUpdateAvailable;
950}
951
Marissa Wall61c58622018-07-18 10:12:20 -0700952uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000953 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800954
Marissa Wall61c58622018-07-18 10:12:20 -0700955 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
956 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700957
David Sodmaneb085e02017-10-05 18:49:04 -0700958 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700959 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000960 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800961 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
962 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
963 " requested={ wh={%4u,%4u} }}\n"
964 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
965 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700966 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700967 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
968 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
969 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
970 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
971 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700972 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
973 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
974 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800975 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700976
Robert Carre392b552017-09-19 12:16:05 -0700977 // Don't let Layer::doTransaction update the drawing state
978 // if we have a pending resize, unless we are in fixed-size mode.
979 // the drawing state will be updated only once we receive a buffer
980 // with the correct size.
981 //
982 // In particular, we want to make sure the clip (which is part
983 // of the geometry state) is latched together with the size but is
984 // latched immediately when no resizing is involved.
985 //
986 // If a sideband stream is attached, however, we want to skip this
987 // optimization so that transactions aren't missed when a buffer
988 // never arrives
989 //
990 // In the case that we don't have a buffer we ignore other factors
991 // and avoid entering the resizePending state. At a high level the
992 // resizePending state is to avoid applying the state of the new buffer
993 // to the old buffer. However in the state where we don't have an old buffer
994 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700995 const bool resizePending =
996 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
997 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
David Sodman0cc69182017-11-17 12:12:07 -0800998 (getBE().compositionInfo.mBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700999 if (!isFixedSize()) {
David Sodman0cc69182017-11-17 12:12:07 -08001000 if (resizePending && getBE().compositionInfo.hwc.sidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001001 flags |= eDontUpdateGeometryState;
1002 }
1003 }
1004
Robert Carr7bf247e2017-05-18 14:02:49 -07001005 // Here we apply various requested geometry states, depending on our
1006 // latching configuration. See Layer.h for a detailed discussion of
1007 // how geometry latching is controlled.
1008 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +00001009 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001010
1011 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1012 // mode, which causes attributes which normally latch regardless of scaling mode,
1013 // to be delayed. We copy the requested state to the active state making sure
1014 // to respect these rules (again see Layer.h for a detailed discussion).
1015 //
1016 // There is an awkward asymmetry in the handling of the crop states in the position
1017 // states, as can be seen below. Largely this arises from position and transform
1018 // being stored in the same data structure while having different latching rules.
1019 // b/38182305
1020 //
Marissa Wall61c58622018-07-18 10:12:20 -07001021 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -07001022 // applyPendingStates in the presence of deferred transactions.
1023 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -07001024 float tx = stateToCommit->active_legacy.transform.tx();
1025 float ty = stateToCommit->active_legacy.transform.ty();
1026 stateToCommit->active_legacy = stateToCommit->requested_legacy;
1027 stateToCommit->active_legacy.transform.set(tx, ty);
1028 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001029 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001030 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -07001031 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001032 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001033 }
1034
Marissa Wall61c58622018-07-18 10:12:20 -07001035 return flags;
1036}
1037
1038uint32_t Layer::doTransaction(uint32_t flags) {
1039 ATRACE_CALL();
1040
chaviw5aedec92018-10-22 10:40:38 -07001041 if (mLayerDetached) {
1042 return 0;
1043 }
1044
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001045 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +00001046 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -07001047 if (!applyPendingStates(&c)) {
1048 return 0;
1049 }
1050
1051 flags = doTransactionResize(flags, &c);
1052
Alec Mourib416efd2018-09-06 21:01:59 +00001053 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -07001054
1055 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001056 // invalidate and recompute the visible regions if needed
1057 flags |= Layer::eVisibleRegion;
1058 }
1059
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001060 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001061 // invalidate and recompute the visible regions if needed
1062 flags |= eVisibleRegion;
1063 this->contentDirty = true;
1064
1065 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -07001066 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -07001067 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -08001068 }
1069
Robert Carr1323c952019-01-28 18:13:27 -08001070 if (mChildrenChanged) {
1071 flags |= eVisibleRegion;
1072 mChildrenChanged = false;
1073 }
1074
Dan Stozac8145172016-04-28 16:29:06 -07001075 // If the layer is hidden, signal and clear out all local sync points so
1076 // that transactions for layers depending on this layer's frames becoming
1077 // visible are not blocked
1078 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001079 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001080 }
1081
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001082 if (mCurrentState.inputInfoChanged) {
Robert Carr720e5062018-07-30 17:45:14 -07001083 flags |= eInputInfoChanged;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001084 mCurrentState.inputInfoChanged = false;
Robert Carr720e5062018-07-30 17:45:14 -07001085 }
1086
Mathias Agopian13127d82013-03-05 17:47:11 -08001087 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001088 commitTransaction(c);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001089 mCurrentState.callbackHandles = {};
Mathias Agopian13127d82013-03-05 17:47:11 -08001090 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001091}
1092
Pablo Ceballos05289c22016-04-14 15:49:55 -07001093void Layer::commitTransaction(const State& stateToCommit) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001094 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001095}
1096
Mathias Agopian13127d82013-03-05 17:47:11 -08001097uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001098 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -08001099}
1100
1101uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001102 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -08001103}
1104
Robert Carr82364e32016-05-15 11:27:47 -07001105bool Layer::setPosition(float x, float y, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001106 if (mCurrentState.requested_legacy.transform.tx() == x &&
1107 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001108 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001109 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001110
1111 // We update the requested and active position simultaneously because
1112 // we want to apply the position portion of the transform matrix immediately,
1113 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001114 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001115 if (immediate && !mFreezeGeometryUpdates) {
1116 // Here we directly update the active state
1117 // unlike other setters, because we store it within
1118 // the transform, but use different latching rules.
1119 // b/38182305
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001120 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001121 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001122 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001123
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001124 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001125 setTransactionFlags(eTransactionNeeded);
1126 return true;
1127}
Robert Carr82364e32016-05-15 11:27:47 -07001128
Robert Carr1f0a16a2016-10-24 16:27:39 -07001129bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1130 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1131 if (idx < 0) {
1132 return false;
1133 }
1134 if (childLayer->setLayer(z)) {
1135 mCurrentChildren.removeAt(idx);
1136 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001137 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001138 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001139 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001140}
1141
Robert Carr503c7042017-09-27 15:06:08 -07001142bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1143 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1144 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1145 if (idx < 0) {
1146 return false;
1147 }
1148 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1149 mCurrentChildren.removeAt(idx);
1150 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001151 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001152 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001153 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001154}
1155
Robert Carrae060832016-11-28 10:51:00 -08001156bool Layer::setLayer(int32_t z) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001157 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
1158 mCurrentState.sequence++;
1159 mCurrentState.z = z;
1160 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001161
1162 // Discard all relative layering.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001163 if (mCurrentState.zOrderRelativeOf != nullptr) {
1164 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
Robert Carrdb66e622017-04-10 16:55:57 -07001165 if (strongRelative != nullptr) {
1166 strongRelative->removeZOrderRelative(this);
1167 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001168 mCurrentState.zOrderRelativeOf = nullptr;
Robert Carrdb66e622017-04-10 16:55:57 -07001169 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001170 setTransactionFlags(eTransactionNeeded);
1171 return true;
1172}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001173
Robert Carrdb66e622017-04-10 16:55:57 -07001174void Layer::removeZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001175 mCurrentState.zOrderRelatives.remove(relative);
1176 mCurrentState.sequence++;
1177 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001178 setTransactionFlags(eTransactionNeeded);
1179}
1180
1181void Layer::addZOrderRelative(const wp<Layer>& relative) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001182 mCurrentState.zOrderRelatives.add(relative);
1183 mCurrentState.modified = true;
1184 mCurrentState.sequence++;
Robert Carrdb66e622017-04-10 16:55:57 -07001185 setTransactionFlags(eTransactionNeeded);
1186}
1187
Robert Carr503d2bd2017-12-04 15:49:47 -08001188bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001189 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1190 if (handle == nullptr) {
1191 return false;
1192 }
1193 sp<Layer> relative = handle->owner.promote();
1194 if (relative == nullptr) {
1195 return false;
1196 }
1197
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001198 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1199 mCurrentState.zOrderRelativeOf == relative) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001200 return false;
1201 }
1202
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001203 mCurrentState.sequence++;
1204 mCurrentState.modified = true;
1205 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001206
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001207 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
chaviw9ab4bd12017-11-03 13:11:00 -07001208 if (oldZOrderRelativeOf != nullptr) {
1209 oldZOrderRelativeOf->removeZOrderRelative(this);
1210 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001211 mCurrentState.zOrderRelativeOf = relative;
Robert Carrdb66e622017-04-10 16:55:57 -07001212 relative->addZOrderRelative(this);
1213
1214 setTransactionFlags(eTransactionNeeded);
1215
1216 return true;
1217}
1218
Mathias Agopian13127d82013-03-05 17:47:11 -08001219bool Layer::setSize(uint32_t w, uint32_t h) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001220 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
Marissa Wallf58c14b2018-07-24 10:50:43 -07001221 return false;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001222 mCurrentState.requested_legacy.w = w;
1223 mCurrentState.requested_legacy.h = h;
1224 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001225 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001226
1227 // record the new size, from this point on, when the client request
1228 // a buffer, it'll get the new size.
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001229 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001230 return true;
1231}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001232bool Layer::setAlpha(float alpha) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001233 if (mCurrentState.color.a == alpha) return false;
1234 mCurrentState.sequence++;
1235 mCurrentState.color.a = alpha;
1236 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001237 setTransactionFlags(eTransactionNeeded);
1238 return true;
1239}
chaviw13fdc492017-06-27 12:40:18 -07001240
Valerie Haudd0b7572019-01-29 14:59:27 -08001241bool Layer::setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace) {
1242 if (!mCurrentState.bgColorLayer && alpha == 0) {
chaviw13fdc492017-06-27 12:40:18 -07001243 return false;
Valerie Haudd0b7572019-01-29 14:59:27 -08001244 } else if (!mCurrentState.bgColorLayer && alpha != 0) {
1245 // create background color layer if one does not yet exist
1246 uint32_t flags = ISurfaceComposerClient::eFXSurfaceColor;
1247 const String8& name = mName + "BackgroundColorLayer";
1248 mCurrentState.bgColorLayer =
1249 new ColorLayer(LayerCreationArgs(mFlinger.get(), nullptr, name, 0, 0, flags));
chaviw13fdc492017-06-27 12:40:18 -07001250
Valerie Haudd0b7572019-01-29 14:59:27 -08001251 // add to child list
1252 addChild(mCurrentState.bgColorLayer);
1253 mFlinger->mLayersAdded = true;
1254 // set up SF to handle added color layer
1255 if (isRemovedFromCurrentState()) {
1256 mCurrentState.bgColorLayer->onRemovedFromCurrentState();
1257 }
1258 mFlinger->setTransactionFlags(eTransactionNeeded);
1259 } else if (mCurrentState.bgColorLayer && alpha == 0) {
1260 mCurrentState.bgColorLayer->reparent(nullptr);
1261 mCurrentState.bgColorLayer = nullptr;
1262 return true;
1263 }
1264
1265 mCurrentState.bgColorLayer->setColor(color);
1266 mCurrentState.bgColorLayer->setLayer(std::numeric_limits<int32_t>::min());
1267 mCurrentState.bgColorLayer->setAlpha(alpha);
1268 mCurrentState.bgColorLayer->setDataspace(dataspace);
1269
chaviw13fdc492017-06-27 12:40:18 -07001270 return true;
1271}
1272
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001273bool Layer::setCornerRadius(float cornerRadius) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001274 if (mCurrentState.cornerRadius == cornerRadius) return false;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001275
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001276 mCurrentState.sequence++;
1277 mCurrentState.cornerRadius = cornerRadius;
1278 mCurrentState.modified = true;
Lucas Dupin1b6531c2018-07-05 17:18:21 -07001279 setTransactionFlags(eTransactionNeeded);
1280 return true;
1281}
1282
Robert Carrd4ae7f32018-06-07 16:10:57 -07001283bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1284 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001285 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001286 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1287
1288 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1289 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1290 return false;
1291 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001292 mCurrentState.sequence++;
1293 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1294 matrix.dsdy);
1295 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001296 setTransactionFlags(eTransactionNeeded);
1297 return true;
1298}
Marissa Wall61c58622018-07-18 10:12:20 -07001299
Mathias Agopian13127d82013-03-05 17:47:11 -08001300bool Layer::setTransparentRegionHint(const Region& transparent) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001301 mCurrentState.requestedTransparentRegion_legacy = transparent;
1302 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001303 setTransactionFlags(eTransactionNeeded);
1304 return true;
1305}
1306bool Layer::setFlags(uint8_t flags, uint8_t mask) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001307 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
1308 if (mCurrentState.flags == newFlags) return false;
1309 mCurrentState.sequence++;
1310 mCurrentState.flags = newFlags;
1311 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001312 setTransactionFlags(eTransactionNeeded);
1313 return true;
1314}
Robert Carr99e27f02016-06-16 15:18:02 -07001315
Marissa Wallf58c14b2018-07-24 10:50:43 -07001316bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001317 if (mCurrentState.requestedCrop_legacy == crop) return false;
1318 mCurrentState.sequence++;
1319 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001320 if (immediate && !mFreezeGeometryUpdates) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001321 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001322 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001323 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1324
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001325 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001326 setTransactionFlags(eTransactionNeeded);
1327 return true;
1328}
Robert Carr8d5227b2017-03-16 15:41:03 -07001329
Robert Carrc3574f72016-03-24 12:19:32 -07001330bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001331 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001332 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001333 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001334 return true;
1335}
1336
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001337bool Layer::setMetadata(LayerMetadata data) {
1338 bool changed = data.mMap != mCurrentState.metadata.mMap;
1339 if (!changed) return false;
1340 mCurrentState.metadata = std::move(data);
1341 mCurrentState.sequence++;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001342 mCurrentState.modified = true;
David Sodman41fdfc92017-11-06 16:09:56 -08001343 setTransactionFlags(eTransactionNeeded);
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001344 return true;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001345}
1346
Mathias Agopian13127d82013-03-05 17:47:11 -08001347bool Layer::setLayerStack(uint32_t layerStack) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001348 if (mCurrentState.layerStack == layerStack) return false;
1349 mCurrentState.sequence++;
1350 mCurrentState.layerStack = layerStack;
1351 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001352 setTransactionFlags(eTransactionNeeded);
1353 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001354}
1355
Robert Carr1f0a16a2016-10-24 16:27:39 -07001356uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001357 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001358 if (p == nullptr) {
1359 return getDrawingState().layerStack;
1360 }
1361 return p->getLayerStack();
1362}
1363
Marissa Wallf58c14b2018-07-24 10:50:43 -07001364void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001365 mCurrentState.barrierLayer_legacy = barrierLayer;
1366 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001367 // We don't set eTransactionNeeded, because just receiving a deferral
1368 // request without any other state updates shouldn't actually induce a delay
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001369 mCurrentState.modified = true;
1370 pushPendingState();
1371 mCurrentState.barrierLayer_legacy = nullptr;
1372 mCurrentState.frameNumber_legacy = 0;
1373 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001374}
1375
Marissa Wallf58c14b2018-07-24 10:50:43 -07001376void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001377 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001378 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001379}
1380
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001381// ----------------------------------------------------------------------------
1382// pageflip handling...
1383// ----------------------------------------------------------------------------
1384
Robert Carr1f0a16a2016-10-24 16:27:39 -07001385bool Layer::isHiddenByPolicy() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001386 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001387 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001388 if (parent != nullptr && parent->isHiddenByPolicy()) {
1389 return true;
1390 }
1391 return s.flags & layer_state_t::eLayerHidden;
1392}
1393
David Sodman41fdfc92017-11-06 16:09:56 -08001394uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001395 // TODO: should we do something special if mSecure is set?
1396 if (mProtectedByApp) {
1397 // need a hardware-protected path to external video sink
1398 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001399 }
Riley Andrews03414a12014-07-01 14:22:59 -07001400 if (mPotentialCursor) {
1401 usage |= GraphicBuffer::USAGE_CURSOR;
1402 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001403 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001404 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001405}
1406
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001407void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001408 uint32_t orientation = 0;
Vishnu Nairf2deb822018-11-12 17:53:48 -08001409 // Disable setting transform hint if the debug flag is set or if the
1410 // getTransformToDisplayInverse flag is set and the client wants to submit buffers
1411 // in one orientation.
1412 if (!mFlinger->mDebugDisableTransformHint && !getTransformToDisplayInverse()) {
Mathias Agopian84300952012-11-21 16:02:13 -08001413 // The transform hint is used to improve performance, but we can
1414 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001415 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001416 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001417 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001418 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001419 orientation = 0;
1420 }
1421 }
David Sodmaneb085e02017-10-05 18:49:04 -07001422 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001423}
1424
Mathias Agopian13127d82013-03-05 17:47:11 -08001425// ----------------------------------------------------------------------------
1426// debugging
1427// ----------------------------------------------------------------------------
1428
Marissa Wall61c58622018-07-18 10:12:20 -07001429// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001430LayerDebugInfo Layer::getLayerDebugInfo() const {
1431 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001432 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001433 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001434 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001435 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
Yiwei Zhang5434a782018-12-05 18:06:32 -08001436 info.mType = std::string(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001437 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001438 info.mVisibleRegion = visibleRegion;
1439 info.mSurfaceDamageRegion = surfaceDamageRegion;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001440 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001441 info.mX = ds.active_legacy.transform.tx();
1442 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001443 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001444 info.mWidth = ds.active_legacy.w;
1445 info.mHeight = ds.active_legacy.h;
1446 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001447 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001448 info.mFlags = ds.flags;
1449 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001450 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001451 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1452 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1453 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1454 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001455 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001456 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001457 if (buffer != 0) {
1458 info.mActiveBufferWidth = buffer->getWidth();
1459 info.mActiveBufferHeight = buffer->getHeight();
1460 info.mActiveBufferStride = buffer->getStride();
1461 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001462 } else {
1463 info.mActiveBufferWidth = 0;
1464 info.mActiveBufferHeight = 0;
1465 info.mActiveBufferStride = 0;
1466 info.mActiveBufferFormat = 0;
1467 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001468 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001469 info.mNumQueuedFrames = getQueuedFrameCount();
1470 info.mRefreshPending = isBufferLatched();
1471 info.mIsOpaque = isOpaque(ds);
1472 info.mContentDirty = contentDirty;
1473 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001474}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001475
Yiwei Zhang5434a782018-12-05 18:06:32 -08001476void Layer::miniDumpHeader(std::string& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001477 result.append("-------------------------------");
1478 result.append("-------------------------------");
1479 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001480 result.append(" Layer name\n");
1481 result.append(" Z | ");
1482 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001483 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001484 result.append(" Disp Frame (LTRB) | ");
1485 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001486 result.append("-------------------------------");
1487 result.append("-------------------------------");
1488 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001489}
1490
Yiwei Zhang5434a782018-12-05 18:06:32 -08001491void Layer::miniDump(std::string& result, DisplayId displayId) const {
Dominik Laskowski34157762018-10-31 13:07:19 -07001492 if (!hasHwcLayer(displayId)) {
Dan Stozae22aec72016-08-01 13:20:59 -07001493 return;
1494 }
1495
Yiwei Zhang5434a782018-12-05 18:06:32 -08001496 std::string name;
Dan Stozae22aec72016-08-01 13:20:59 -07001497 if (mName.length() > 77) {
1498 std::string shortened;
1499 shortened.append(mName.string(), 36);
1500 shortened.append("[...]");
1501 shortened.append(mName.string() + (mName.length() - 36), 36);
Yiwei Zhang5434a782018-12-05 18:06:32 -08001502 name = shortened;
Dan Stozae22aec72016-08-01 13:20:59 -07001503 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001504 name = std::string(mName.string(), mName.size());
Dan Stozae22aec72016-08-01 13:20:59 -07001505 }
1506
Yiwei Zhang5434a782018-12-05 18:06:32 -08001507 StringAppendF(&result, " %s\n", name.c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001508
Alec Mourib416efd2018-09-06 21:01:59 +00001509 const State& layerState(getDrawingState());
Lloyd Pique074e8122018-07-26 12:57:23 -07001510 const LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers.at(displayId);
Chia-I Wu1e043612018-03-01 09:45:09 -08001511 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001512 StringAppendF(&result, " rel %6d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001513 } else {
Yiwei Zhang5434a782018-12-05 18:06:32 -08001514 StringAppendF(&result, " %10d | ", layerState.z);
Chia-I Wu1e043612018-03-01 09:45:09 -08001515 }
Yiwei Zhang5434a782018-12-05 18:06:32 -08001516 StringAppendF(&result, "%10s | ", to_string(getCompositionType(displayId)).c_str());
1517 StringAppendF(&result, "%10s | ", to_string(hwcInfo.transform).c_str());
Lloyd Pique074e8122018-07-26 12:57:23 -07001518 const Rect& frame = hwcInfo.displayFrame;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001519 StringAppendF(&result, "%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique074e8122018-07-26 12:57:23 -07001520 const FloatRect& crop = hwcInfo.sourceCrop;
Yiwei Zhang5434a782018-12-05 18:06:32 -08001521 StringAppendF(&result, "%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right,
1522 crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001523
David Sodman7e4ae112018-02-09 15:02:28 -08001524 result.append("- - - - - - - - - - - - - - - -\n");
1525
1526 std::string compositionInfoStr;
1527 getBE().compositionInfo.dump(compositionInfoStr, "compositionInfo");
Yiwei Zhang5434a782018-12-05 18:06:32 -08001528 result.append(compositionInfoStr);
David Sodman7e4ae112018-02-09 15:02:28 -08001529
Yichi Chen6ca35192018-05-29 12:20:43 +08001530 result.append("- - - - - - - - - - - - - - - -");
1531 result.append("- - - - - - - - - - - - - - - -");
1532 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001533}
Dan Stozae22aec72016-08-01 13:20:59 -07001534
Yiwei Zhang5434a782018-12-05 18:06:32 -08001535void Layer::dumpFrameStats(std::string& result) const {
Svetoslavd85084b2014-03-20 10:28:31 -07001536 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001537}
1538
Svetoslavd85084b2014-03-20 10:28:31 -07001539void Layer::clearFrameStats() {
1540 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001541}
1542
Jamie Gennis6547ff42013-07-16 20:12:42 -07001543void Layer::logFrameStats() {
1544 mFrameTracker.logAndResetStats(mName);
1545}
1546
Svetoslavd85084b2014-03-20 10:28:31 -07001547void Layer::getFrameStats(FrameStats* outStats) const {
1548 mFrameTracker.getStats(outStats);
1549}
1550
Yiwei Zhang5434a782018-12-05 18:06:32 -08001551void Layer::dumpFrameEvents(std::string& result) {
1552 StringAppendF(&result, "- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001553 Mutex::Autolock lock(mFrameEventHistoryMutex);
1554 mFrameEventHistory.checkFencesForCompletion();
1555 mFrameEventHistory.dump(result);
1556}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001557
Brian Anderson5ea5e592016-12-01 16:54:33 -08001558void Layer::onDisconnect() {
1559 Mutex::Autolock lock(mFrameEventHistoryMutex);
1560 mFrameEventHistory.onDisconnect();
Yiwei Zhangaf8ee942018-11-22 00:15:23 -08001561 mFlinger->mTimeStats->onDestroy(getSequence());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001562}
1563
Brian Anderson3890c392016-07-25 12:48:08 -07001564void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001565 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001566 if (newTimestamps) {
Yiwei Zhang7e666a52018-11-15 13:33:42 -08001567 mFlinger->mTimeStats->setPostTime(getSequence(), newTimestamps->frameNumber,
1568 getName().c_str(), newTimestamps->postedTime);
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001569 }
1570
Brian Andersond6927fb2016-07-23 23:37:30 -07001571 Mutex::Autolock lock(mFrameEventHistoryMutex);
1572 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001573 // If there are any unsignaled fences in the aquire timeline at this
1574 // point, the previously queued frame hasn't been latched yet. Go ahead
1575 // and try to get the signal time here so the syscall is taken out of
1576 // the main thread's critical path.
1577 mAcquireTimeline.updateSignalTimes();
1578 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001579 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001580 mFrameEventHistory.addQueue(*newTimestamps);
1581 }
1582
Brian Anderson3890c392016-07-25 12:48:08 -07001583 if (outDelta) {
1584 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001585 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001586}
Dan Stozae77c7662016-05-13 11:37:28 -07001587
Chia-I Wu98f1c102017-05-30 14:54:08 -07001588size_t Layer::getChildrenCount() const {
1589 size_t count = 0;
1590 for (const sp<Layer>& child : mCurrentChildren) {
1591 count += 1 + child->getChildrenCount();
1592 }
1593 return count;
1594}
1595
Robert Carr1f0a16a2016-10-24 16:27:39 -07001596void Layer::addChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001597 mChildrenChanged = true;
1598
Robert Carr1f0a16a2016-10-24 16:27:39 -07001599 mCurrentChildren.add(layer);
1600 layer->setParent(this);
1601}
1602
1603ssize_t Layer::removeChild(const sp<Layer>& layer) {
Robert Carr1323c952019-01-28 18:13:27 -08001604 mChildrenChanged = true;
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001605
Robert Carr1323c952019-01-28 18:13:27 -08001606 layer->setParent(nullptr);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001607 return mCurrentChildren.remove(layer);
1608}
1609
Robert Carr1db73f62016-12-21 12:58:51 -08001610bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1611 sp<Handle> handle = nullptr;
1612 sp<Layer> newParent = nullptr;
1613 if (newParentHandle == nullptr) {
1614 return false;
1615 }
1616 handle = static_cast<Handle*>(newParentHandle.get());
1617 newParent = handle->owner.promote();
1618 if (newParent == nullptr) {
1619 ALOGE("Unable to promote Layer handle");
1620 return false;
1621 }
1622
chaviw5aedec92018-10-22 10:40:38 -07001623 if (attachChildren()) {
1624 setTransactionFlags(eTransactionNeeded);
1625 }
Robert Carr1db73f62016-12-21 12:58:51 -08001626 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001627 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001628 }
1629 mCurrentChildren.clear();
1630
1631 return true;
1632}
1633
Robert Carr15eae092018-03-23 13:43:53 -07001634void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001635 for (const sp<Layer>& child : mDrawingChildren) {
1636 child->mDrawingParent = newParent;
1637 }
1638}
1639
chaviwf1961f72017-09-18 16:41:07 -07001640bool Layer::reparent(const sp<IBinder>& newParentHandle) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001641 bool callSetTransactionFlags = false;
chaviw06178942017-07-27 10:25:59 -07001642
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001643 // While layers are detached, we allow most operations
1644 // and simply halt performing the actual transaction. However
1645 // for reparent != null we would enter the mRemovedFromCurrentState
1646 // state, regardless of whether doTransaction was called, and
1647 // so we need to prevent the update here.
1648 if (mLayerDetached && newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001649 return false;
1650 }
1651
Robert Carr54cf5b12019-01-25 14:02:28 -08001652 sp<Layer> newParent;
1653 if (newParentHandle != nullptr) {
1654 auto handle = static_cast<Handle*>(newParentHandle.get());
1655 newParent = handle->owner.promote();
1656 if (newParent == nullptr) {
1657 ALOGE("Unable to promote Layer handle");
1658 return false;
1659 }
1660 if (newParent == this) {
1661 ALOGE("Invalid attempt to reparent Layer (%s) to itself", getName().c_str());
1662 return false;
1663 }
1664 }
1665
chaviwf1961f72017-09-18 16:41:07 -07001666 sp<Layer> parent = getParent();
1667 if (parent != nullptr) {
1668 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001669 }
1670
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001671 if (newParentHandle != nullptr) {
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001672 newParent->addChild(this);
1673 if (!newParent->isRemovedFromCurrentState()) {
1674 addToCurrentState();
1675 } else {
1676 onRemovedFromCurrentState();
1677 }
1678
1679 if (mLayerDetached) {
1680 mLayerDetached = false;
1681 callSetTransactionFlags = true;
1682 }
1683 } else {
1684 onRemovedFromCurrentState();
chaviw61626f22018-11-15 16:26:27 -08001685 }
1686
Robert Carr6fb1a7e2018-12-11 12:07:25 -08001687 if (callSetTransactionFlags || attachChildren()) {
chaviw5aedec92018-10-22 10:40:38 -07001688 setTransactionFlags(eTransactionNeeded);
1689 }
chaviw06178942017-07-27 10:25:59 -07001690 return true;
1691}
1692
Robert Carr9524cb32017-02-13 11:32:32 -08001693bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001694 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001695 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001696 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001697 if (client != nullptr && parentClient != client) {
chaviw5aedec92018-10-22 10:40:38 -07001698 child->mLayerDetached = true;
Robert Carr7f619b22017-11-06 12:56:35 -08001699 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001700 }
Robert Carr7f619b22017-11-06 12:56:35 -08001701 }
Robert Carr9524cb32017-02-13 11:32:32 -08001702
1703 return true;
1704}
1705
chaviw5aedec92018-10-22 10:40:38 -07001706bool Layer::attachChildren() {
1707 bool changed = false;
1708 for (const sp<Layer>& child : mCurrentChildren) {
1709 sp<Client> parentClient = mClientRef.promote();
1710 sp<Client> client(child->mClientRef.promote());
1711 if (client != nullptr && parentClient != client) {
1712 if (child->mLayerDetached) {
1713 child->mLayerDetached = false;
1714 changed = true;
1715 }
1716 changed |= child->attachChildren();
1717 }
1718 }
1719
1720 return changed;
1721}
1722
Peiyong Lind3788632018-09-18 16:01:31 -07001723bool Layer::setColorTransform(const mat4& matrix) {
Peiyong Lin747321c2018-10-01 10:03:11 -07001724 static const mat4 identityMatrix = mat4();
1725
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001726 if (mCurrentState.colorTransform == matrix) {
Peiyong Lind3788632018-09-18 16:01:31 -07001727 return false;
1728 }
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001729 ++mCurrentState.sequence;
1730 mCurrentState.colorTransform = matrix;
1731 mCurrentState.hasColorTransform = matrix != identityMatrix;
1732 mCurrentState.modified = true;
Peiyong Lind3788632018-09-18 16:01:31 -07001733 setTransactionFlags(eTransactionNeeded);
1734 return true;
1735}
1736
chaviwf66724d2018-11-28 16:35:21 -08001737mat4 Layer::getColorTransform() const {
1738 mat4 colorTransform = mat4(getDrawingState().colorTransform);
1739 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1740 colorTransform = parent->getColorTransform() * colorTransform;
1741 }
1742 return colorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001743}
1744
1745bool Layer::hasColorTransform() const {
chaviwf66724d2018-11-28 16:35:21 -08001746 bool hasColorTransform = getDrawingState().hasColorTransform;
1747 if (sp<Layer> parent = mDrawingParent.promote(); parent != nullptr) {
1748 hasColorTransform = hasColorTransform || parent->hasColorTransform();
1749 }
1750 return hasColorTransform;
Peiyong Lind3788632018-09-18 16:01:31 -07001751}
1752
Chia-I Wu11481472018-05-04 10:43:19 -07001753bool Layer::isLegacyDataSpace() const {
1754 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001755 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001756 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001757}
1758
Robert Carr1f0a16a2016-10-24 16:27:39 -07001759void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001760 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001761}
1762
1763void Layer::clearSyncPoints() {
1764 for (const auto& child : mCurrentChildren) {
1765 child->clearSyncPoints();
1766 }
1767
1768 Mutex::Autolock lock(mLocalSyncPointMutex);
1769 for (auto& point : mLocalSyncPoints) {
1770 point->setFrameAvailable();
1771 }
1772 mLocalSyncPoints.clear();
1773}
1774
1775int32_t Layer::getZ() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001776 return mDrawingState.z;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001777}
1778
Robert Carr29abff82017-12-04 13:51:20 -08001779bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1780 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001781 const State& state = useDrawing ? mDrawingState : mCurrentState;
Robert Carr29abff82017-12-04 13:51:20 -08001782 return state.zOrderRelativeOf != nullptr;
1783}
1784
David Sodman41fdfc92017-11-06 16:09:56 -08001785__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001786 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001787 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1788 "makeTraversalList received invalid stateSet");
1789 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1790 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001791 const State& state = useDrawing ? mDrawingState : mCurrentState;
Dan Stoza412903f2017-04-27 13:42:17 -07001792
Robert Carr29abff82017-12-04 13:51:20 -08001793 if (state.zOrderRelatives.size() == 0) {
1794 *outSkipRelativeZUsers = true;
1795 return children;
1796 }
1797
chaviwfd462612018-05-31 16:11:27 -07001798 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001799 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001800 sp<Layer> strongRelative = weakRelative.promote();
1801 if (strongRelative != nullptr) {
1802 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001803 }
1804 }
1805
Dan Stoza412903f2017-04-27 13:42:17 -07001806 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001807 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
Robert Carr503c7042017-09-27 15:06:08 -07001808 if (childState.zOrderRelativeOf != nullptr) {
1809 continue;
1810 }
Robert Carrdb66e622017-04-10 16:55:57 -07001811 traverse.add(child);
1812 }
1813
1814 return traverse;
1815}
1816
Robert Carr1f0a16a2016-10-24 16:27:39 -07001817/**
Robert Carrdb66e622017-04-10 16:55:57 -07001818 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001819 */
Dan Stoza412903f2017-04-27 13:42:17 -07001820void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001821 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1822 // produce a new list for traversing, including our relatives, and not including our children
1823 // who are relatives of another surface. In the case that there are no relative Z,
1824 // makeTraversalList returns our children directly to avoid significant overhead.
1825 // However in this case we need to take the responsibility for filtering children which
1826 // are relatives of another surface here.
1827 bool skipRelativeZUsers = false;
1828 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001829
Robert Carr1f0a16a2016-10-24 16:27:39 -07001830 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001831 for (; i < list.size(); i++) {
1832 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001833 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1834 continue;
1835 }
1836
Robert Carrdb66e622017-04-10 16:55:57 -07001837 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001838 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001839 }
Dan Stoza412903f2017-04-27 13:42:17 -07001840 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001841 }
Robert Carr29abff82017-12-04 13:51:20 -08001842
Dan Stoza412903f2017-04-27 13:42:17 -07001843 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001844 for (; i < list.size(); i++) {
1845 const auto& relative = list[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001846
Robert Carr29abff82017-12-04 13:51:20 -08001847 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1848 continue;
1849 }
Dan Stoza412903f2017-04-27 13:42:17 -07001850 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001851 }
1852}
1853
1854/**
Robert Carrdb66e622017-04-10 16:55:57 -07001855 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001856 */
Dan Stoza412903f2017-04-27 13:42:17 -07001857void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1858 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001859 // See traverseInZOrder for documentation.
1860 bool skipRelativeZUsers = false;
1861 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001862
Robert Carr1f0a16a2016-10-24 16:27:39 -07001863 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001864 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001865 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001866
1867 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1868 continue;
1869 }
1870
Robert Carrdb66e622017-04-10 16:55:57 -07001871 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001872 break;
1873 }
Dan Stoza412903f2017-04-27 13:42:17 -07001874 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001875 }
Dan Stoza412903f2017-04-27 13:42:17 -07001876 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001877 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001878 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001879
1880 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1881 continue;
1882 }
1883
Dan Stoza412903f2017-04-27 13:42:17 -07001884 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001885 }
1886}
1887
chaviw4b129c22018-04-09 16:19:43 -07001888LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1889 const std::vector<Layer*>& layersInTree) {
1890 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1891 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001892 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1893 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001894 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001895
chaviwfd462612018-05-31 16:11:27 -07001896 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001897 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1898 sp<Layer> strongRelative = weakRelative.promote();
1899 // Only add relative layers that are also descendents of the top most parent of the tree.
1900 // If a relative layer is not a descendent, then it should be ignored.
1901 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1902 traverse.add(strongRelative);
1903 }
1904 }
1905
1906 for (const sp<Layer>& child : children) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001907 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
chaviw4b129c22018-04-09 16:19:43 -07001908 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1909 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1910 // the child here since it won't be added later as a relative.
1911 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1912 childState.zOrderRelativeOf.promote().get())) {
1913 continue;
1914 }
1915 traverse.add(child);
1916 }
1917
1918 return traverse;
1919}
1920
1921void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1922 LayerVector::StateSet stateSet,
1923 const LayerVector::Visitor& visitor) {
1924 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001925
1926 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001927 for (; i < list.size(); i++) {
1928 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001929 if (relative->getZ() >= 0) {
1930 break;
1931 }
chaviw4b129c22018-04-09 16:19:43 -07001932 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001933 }
chaviw4b129c22018-04-09 16:19:43 -07001934
chaviwa76b2712017-09-20 12:02:26 -07001935 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001936 for (; i < list.size(); i++) {
1937 const auto& relative = list[i];
1938 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001939 }
1940}
1941
chaviw4b129c22018-04-09 16:19:43 -07001942std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1943 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1944 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1945
1946 std::vector<Layer*> layersInTree = {this};
1947 for (size_t i = 0; i < children.size(); i++) {
1948 const auto& child = children[i];
1949 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1950 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1951 }
1952
1953 return layersInTree;
1954}
1955
1956void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1957 const LayerVector::Visitor& visitor) {
1958 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1959 std::sort(layersInTree.begin(), layersInTree.end());
1960 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1961}
1962
Peiyong Linefefaac2018-08-17 12:27:51 -07001963ui::Transform Layer::getTransform() const {
1964 ui::Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001965 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001966 if (p != nullptr) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001967 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07001968
1969 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
1970 // it isFixedSize) then there may be additional scaling not accounted
1971 // for in the transform. We need to mirror this scaling in child surfaces
1972 // or we will break the contract where WM can treat child surfaces as
1973 // pixels in the parent surface.
David Sodman0cc69182017-11-17 12:12:07 -08001974 if (p->isFixedSize() && p->getBE().compositionInfo.mBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07001975 int bufferWidth;
1976 int bufferHeight;
1977 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
David Sodman0cc69182017-11-17 12:12:07 -08001978 bufferWidth = p->getBE().compositionInfo.mBuffer->getWidth();
1979 bufferHeight = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001980 } else {
David Sodman0cc69182017-11-17 12:12:07 -08001981 bufferHeight = p->getBE().compositionInfo.mBuffer->getWidth();
1982 bufferWidth = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001983 }
Marissa Wall61c58622018-07-18 10:12:20 -07001984 float sx = p->getActiveWidth(p->getDrawingState()) / static_cast<float>(bufferWidth);
1985 float sy = p->getActiveHeight(p->getDrawingState()) / static_cast<float>(bufferHeight);
Peiyong Linefefaac2018-08-17 12:27:51 -07001986 ui::Transform extraParentScaling;
Robert Carr9b429f42017-04-17 14:56:57 -07001987 extraParentScaling.set(sx, 0, 0, sy);
1988 t = t * extraParentScaling;
1989 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001990 }
Marissa Wall61c58622018-07-18 10:12:20 -07001991 return t * getActiveTransform(getDrawingState());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001992}
1993
chaviw13fdc492017-06-27 12:40:18 -07001994half Layer::getAlpha() const {
Ady Abraham83729882018-12-07 12:26:48 -08001995 const auto& p = mDrawingParent.promote();
Lloyd Pique0449b0f2018-12-20 16:23:45 -08001996
chaviw13fdc492017-06-27 12:40:18 -07001997 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1998 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001999}
Robert Carr6452f122017-03-21 10:41:29 -07002000
chaviw13fdc492017-06-27 12:40:18 -07002001half4 Layer::getColor() const {
2002 const half4 color(getDrawingState().color);
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002003 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07002004}
Robert Carr6452f122017-03-21 10:41:29 -07002005
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002006Layer::RoundedCornerState Layer::getRoundedCornerState() const {
2007 const auto& p = mDrawingParent.promote();
2008 if (p != nullptr) {
2009 RoundedCornerState parentState = p->getRoundedCornerState();
2010 if (parentState.radius > 0) {
2011 ui::Transform t = getActiveTransform(getDrawingState());
2012 t = t.inverse();
2013 parentState.cropRect = t.transform(parentState.cropRect);
2014 // The rounded corners shader only accepts 1 corner radius for performance reasons,
2015 // but a transform matrix can define horizontal and vertical scales.
2016 // Let's take the average between both of them and pass into the shader, practically we
2017 // never do this type of transformation on windows anyway.
2018 parentState.radius *= (t[0][0] + t[1][1]) / 2.0f;
2019 return parentState;
2020 }
2021 }
2022 const float radius = getDrawingState().cornerRadius;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002023 return radius > 0 ? RoundedCornerState(computeBounds(), radius) : RoundedCornerState();
Lucas Dupin1b6531c2018-07-05 17:18:21 -07002024}
2025
Robert Carr1f0a16a2016-10-24 16:27:39 -07002026void Layer::commitChildList() {
2027 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
2028 const auto& child = mCurrentChildren[i];
2029 child->commitChildList();
2030 }
2031 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07002032 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07002033}
2034
Robert Carr720e5062018-07-30 17:45:14 -07002035void Layer::setInputInfo(const InputWindowInfo& info) {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002036 mCurrentState.inputInfo = info;
2037 mCurrentState.modified = true;
2038 mCurrentState.inputInfoChanged = true;
Robert Carr720e5062018-07-30 17:45:14 -07002039 setTransactionFlags(eTransactionNeeded);
2040}
2041
chaviw1d044282017-09-27 12:19:28 -07002042void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
2043 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
2044 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002045 const State& state = useDrawing ? mDrawingState : mCurrentState;
chaviw1d044282017-09-27 12:19:28 -07002046
Peiyong Linefefaac2018-08-17 12:27:51 -07002047 ui::Transform requestedTransform = state.active_legacy.transform;
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002048 ui::Transform transform = getTransform();
chaviw1d044282017-09-27 12:19:28 -07002049
2050 layerInfo->set_id(sequence);
2051 layerInfo->set_name(getName().c_str());
2052 layerInfo->set_type(String8(getTypeId()));
2053
2054 for (const auto& child : children) {
2055 layerInfo->add_children(child->sequence);
2056 }
2057
2058 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
2059 sp<Layer> strongRelative = weakRelative.promote();
2060 if (strongRelative != nullptr) {
2061 layerInfo->add_relatives(strongRelative->sequence);
2062 }
2063 }
2064
Marissa Wallf58c14b2018-07-24 10:50:43 -07002065 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
chaviw1d044282017-09-27 12:19:28 -07002066 layerInfo->mutable_transparent_region());
2067 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
2068 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
2069
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002070 layerInfo->set_layer_stack(getLayerStack());
chaviw1d044282017-09-27 12:19:28 -07002071 layerInfo->set_z(state.z);
2072
2073 PositionProto* position = layerInfo->mutable_position();
2074 position->set_x(transform.tx());
2075 position->set_y(transform.ty());
2076
2077 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
2078 requestedPosition->set_x(requestedTransform.tx());
2079 requestedPosition->set_y(requestedTransform.ty());
2080
2081 SizeProto* size = layerInfo->mutable_size();
Marissa Wallf58c14b2018-07-24 10:50:43 -07002082 size->set_w(state.active_legacy.w);
2083 size->set_h(state.active_legacy.h);
chaviw1d044282017-09-27 12:19:28 -07002084
Marissa Wallf58c14b2018-07-24 10:50:43 -07002085 LayerProtoHelper::writeToProto(state.crop_legacy, layerInfo->mutable_crop());
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002086 layerInfo->set_corner_radius(getRoundedCornerState().radius);
chaviw1d044282017-09-27 12:19:28 -07002087
2088 layerInfo->set_is_opaque(isOpaque(state));
2089 layerInfo->set_invalidate(contentDirty);
Chia-I Wu01591c92018-05-22 12:03:00 -07002090
2091 // XXX (b/79210409) mCurrentDataSpace is not protected
2092 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
2093
chaviw1d044282017-09-27 12:19:28 -07002094 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
2095 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
2096 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
2097 layerInfo->set_flags(state.flags);
2098
2099 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
2100 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
2101
Jorim Jaggi8e0af362017-11-14 16:28:28 +01002102 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07002103 if (parent != nullptr) {
2104 layerInfo->set_parent(parent->sequence);
2105 }
2106
2107 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2108 if (zOrderRelativeOf != nullptr) {
2109 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
2110 }
2111
Chia-I Wu01591c92018-05-22 12:03:00 -07002112 // XXX getBE().compositionInfo.mBuffer is not protected
David Sodman0cc69182017-11-17 12:12:07 -08002113 auto buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08002114 if (buffer != nullptr) {
2115 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
Peiyong Linefefaac2018-08-17 12:27:51 -07002116 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
Yichi Chen6ca35192018-05-29 12:20:43 +08002117 layerInfo->mutable_buffer_transform());
chaviw1d044282017-09-27 12:19:28 -07002118 }
2119
2120 layerInfo->set_queued_frames(getQueuedFrameCount());
2121 layerInfo->set_refresh_pending(isBufferLatched());
chaviwadc40c22018-07-10 16:57:27 -07002122 layerInfo->set_curr_frame(mCurrentFrameNumber);
Vishnu Nairf2deb822018-11-12 17:53:48 -08002123 layerInfo->set_effective_scaling_mode(getEffectiveScalingMode());
chaviwadc40c22018-07-10 16:57:27 -07002124
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002125 for (const auto& pendingState : mPendingStates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07002126 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
chaviwadc40c22018-07-10 16:57:27 -07002127 if (barrierLayer != nullptr) {
2128 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
2129 barrierLayerProto->set_id(barrierLayer->sequence);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002130 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
chaviwadc40c22018-07-10 16:57:27 -07002131 }
2132 }
Evan Rosky1f6d6d52018-12-06 10:47:26 -08002133
2134 auto protoMap = layerInfo->mutable_metadata();
2135 for (const auto& entry : state.metadata.mMap) {
2136 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
2137 }
chaviw1d044282017-09-27 12:19:28 -07002138}
2139
Dominik Laskowski075d3172018-05-24 15:50:06 -07002140void Layer::writeToProto(LayerProto* layerInfo, DisplayId displayId) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07002141 if (!hasHwcLayer(displayId)) {
2142 return;
2143 }
2144
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002145 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
2146
Dominik Laskowski7e045462018-05-30 13:02:02 -07002147 const auto& hwcInfo = getBE().mHwcLayers.at(displayId);
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002148
2149 const Rect& frame = hwcInfo.displayFrame;
2150 LayerProtoHelper::writeToProto(frame, layerInfo->mutable_hwc_frame());
2151
2152 const FloatRect& crop = hwcInfo.sourceCrop;
2153 LayerProtoHelper::writeToProto(crop, layerInfo->mutable_hwc_crop());
2154
2155 const int32_t transform = static_cast<int32_t>(hwcInfo.transform);
2156 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002157
2158 const int32_t compositionType = static_cast<int32_t>(hwcInfo.compositionType);
2159 layerInfo->set_hwc_composition_type(compositionType);
2160
2161 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
2162 static_cast<BufferLayer*>(this)->isProtected()) {
2163 layerInfo->set_is_protected(true);
2164 } else {
2165 layerInfo->set_is_protected(false);
2166 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002167}
2168
Robert Carr2e102c92018-10-23 12:11:15 -07002169bool Layer::isRemovedFromCurrentState() const {
2170 return mRemovedFromCurrentState;
2171}
2172
Arthur Hungd20b2702019-01-14 18:16:16 +08002173InputWindowInfo Layer::fillInputInfo() {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002174 InputWindowInfo info = mDrawingState.inputInfo;
Robert Carr720e5062018-07-30 17:45:14 -07002175
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002176 if (info.displayId == ADISPLAY_ID_NONE) {
2177 info.displayId = mDrawingState.layerStack;
2178 }
2179
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002180 ui::Transform t = getTransform();
2181 const float xScale = t.sx();
2182 const float yScale = t.sy();
2183 if (xScale != 1.0f || yScale != 1.0f) {
2184 info.windowXScale *= 1.0f / xScale;
2185 info.windowYScale *= 1.0f / yScale;
2186 info.touchableRegion.scaleSelf(xScale, yScale);
Riddle Hsu39d4aa52018-11-30 20:46:53 +08002187 }
Robert Carre07e1032018-11-26 12:55:53 -08002188
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002189 // Transform layer size to screen space and inset it by surface insets.
Tiger Huang85b8c5e2019-01-17 18:34:54 +08002190 // If this is a portal window, set the touchableRegion to the layerBounds.
2191 Rect layerBounds = info.portalToDisplayId == ADISPLAY_ID_NONE
2192 ? getBufferSize(getDrawingState())
2193 : info.touchableRegion.getBounds();
Arthur Hungd20b2702019-01-14 18:16:16 +08002194 if (!layerBounds.isValid()) {
2195 layerBounds = getCroppedBufferSize(getDrawingState());
2196 }
Vishnu Nair8033a492018-12-05 07:27:23 -08002197 layerBounds = t.transform(layerBounds);
2198 layerBounds.inset(info.surfaceInset, info.surfaceInset, info.surfaceInset, info.surfaceInset);
2199
Arthur Hungd20b2702019-01-14 18:16:16 +08002200 // Input coordinate should match the layer bounds.
2201 info.frameLeft = layerBounds.left;
2202 info.frameTop = layerBounds.top;
2203 info.frameRight = layerBounds.right;
2204 info.frameBottom = layerBounds.bottom;
Vishnu Nair8033a492018-12-05 07:27:23 -08002205
2206 // Position the touchable region relative to frame screen location and restrict it to frame
2207 // bounds.
2208 info.touchableRegion = info.touchableRegion.translate(info.frameLeft, info.frameTop);
chaviw3e727cd2019-01-31 13:41:05 -08002209 info.visible = canReceiveInput();
Robert Carr720e5062018-07-30 17:45:14 -07002210 return info;
2211}
2212
2213bool Layer::hasInput() const {
Lloyd Pique0449b0f2018-12-20 16:23:45 -08002214 return mDrawingState.inputInfo.token != nullptr;
Robert Carr720e5062018-07-30 17:45:14 -07002215}
2216
Lloyd Piquefeb73d72018-12-04 17:23:44 -08002217std::shared_ptr<compositionengine::Layer> Layer::getCompositionLayer() const {
2218 return nullptr;
2219}
2220
chaviw3e727cd2019-01-31 13:41:05 -08002221bool Layer::canReceiveInput() const {
2222 return isVisible();
2223}
2224
Mathias Agopian13127d82013-03-05 17:47:11 -08002225// ---------------------------------------------------------------------------
2226
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002227}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002228
2229#if defined(__gl_h_)
2230#error "don't include gl/gl.h in this file"
2231#endif
2232
2233#if defined(__gl2_h_)
2234#error "don't include gl2/gl2.h in this file"
2235#endif